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 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 5c860e29 2018-03-12 stsp #endif
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp }
70 99437157 2018-11-11 stsp
71 99437157 2018-11-11 stsp static void
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp {
74 99437157 2018-11-11 stsp sigpipe_received = 1;
75 99437157 2018-11-11 stsp }
76 5c860e29 2018-03-12 stsp
77 99437157 2018-11-11 stsp
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
83 5c860e29 2018-03-12 stsp };
84 5c860e29 2018-03-12 stsp
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
112 5c860e29 2018-03-12 stsp
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
139 5c860e29 2018-03-12 stsp
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
167 5c860e29 2018-03-12 stsp };
168 ce5b7c56 2019-07-09 stsp
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
171 ce5b7c56 2019-07-09 stsp {
172 ce5b7c56 2019-07-09 stsp int i;
173 ce5b7c56 2019-07-09 stsp
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
178 ce5b7c56 2019-07-09 stsp }
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
180 ce5b7c56 2019-07-09 stsp }
181 5c860e29 2018-03-12 stsp
182 5c860e29 2018-03-12 stsp int
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
184 5c860e29 2018-03-12 stsp {
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
187 5c860e29 2018-03-12 stsp int ch;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
192 83cd27f8 2020-01-13 stsp };
193 5c860e29 2018-03-12 stsp
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
195 5c860e29 2018-03-12 stsp
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
198 1b6b95a8 2018-03-12 stsp case 'h':
199 1b6b95a8 2018-03-12 stsp hflag = 1;
200 53ccebc2 2019-07-30 stsp break;
201 53ccebc2 2019-07-30 stsp case 'V':
202 53ccebc2 2019-07-30 stsp Vflag = 1;
203 1b6b95a8 2018-03-12 stsp break;
204 5c860e29 2018-03-12 stsp default:
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
207 5c860e29 2018-03-12 stsp }
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
213 53ccebc2 2019-07-30 stsp
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
216 53ccebc2 2019-07-30 stsp return 1;
217 53ccebc2 2019-07-30 stsp }
218 5c860e29 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
221 5c860e29 2018-03-12 stsp
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
224 99437157 2018-11-11 stsp
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
227 d7d4f210 2018-03-12 stsp
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
229 5c860e29 2018-03-12 stsp
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
232 5c860e29 2018-03-12 stsp continue;
233 5c860e29 2018-03-12 stsp
234 1b6b95a8 2018-03-12 stsp if (hflag)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
236 1b6b95a8 2018-03-12 stsp
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 d7d4f210 2018-03-12 stsp return 1;
246 d7d4f210 2018-03-12 stsp }
247 d7d4f210 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp return 0;
249 5c860e29 2018-03-12 stsp }
250 5c860e29 2018-03-12 stsp
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
253 5c860e29 2018-03-12 stsp return 1;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
258 5c860e29 2018-03-12 stsp {
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
261 ce5b7c56 2019-07-09 stsp if (hflag)
262 ce5b7c56 2019-07-09 stsp list_commands();
263 5c860e29 2018-03-12 stsp exit(1);
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
268 e2ba3d07 2019-05-13 stsp {
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
271 8920fa04 2019-08-18 stsp
272 8920fa04 2019-08-18 stsp *abspath = NULL;
273 e2ba3d07 2019-05-13 stsp
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
277 e2ba3d07 2019-05-13 stsp
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
280 0ee7065d 2019-05-13 stsp if (err)
281 0ee7065d 2019-05-13 stsp return err;
282 0ee7065d 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
288 0ee7065d 2019-05-13 stsp }
289 0ee7065d 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp return NULL;
291 e2ba3d07 2019-05-13 stsp }
292 e2ba3d07 2019-05-13 stsp
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
296 0266afb7 2019-01-04 stsp {
297 163ce85a 2019-05-13 stsp const struct got_error *err;
298 0266afb7 2019-01-04 stsp
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
302 37c06ea4 2019-07-15 stsp #endif
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
305 0266afb7 2019-01-04 stsp
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
308 0266afb7 2019-01-04 stsp
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
311 0266afb7 2019-01-04 stsp
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp return NULL;
320 2c7829a4 2019-06-17 stsp }
321 2c7829a4 2019-06-17 stsp
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
324 2c7829a4 2019-06-17 stsp {
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 2c7829a4 2019-06-17 stsp exit(1);
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
331 2c7829a4 2019-06-17 stsp {
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
334 2c7829a4 2019-06-17 stsp int ch;
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
338 2c7829a4 2019-06-17 stsp default:
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
341 2c7829a4 2019-06-17 stsp }
342 2c7829a4 2019-06-17 stsp }
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
346 2c7829a4 2019-06-17 stsp
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
350 2c7829a4 2019-06-17 stsp #endif
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
366 2c7829a4 2019-06-17 stsp if (error)
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
370 3ce1b845 2019-07-15 stsp done:
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
373 3ce1b845 2019-07-15 stsp }
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
377 3ce1b845 2019-07-15 stsp {
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp exit(1);
381 3ce1b845 2019-07-15 stsp }
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp int
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
385 3ce1b845 2019-07-15 stsp {
386 3ce1b845 2019-07-15 stsp pid_t pid;
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
395 3ce1b845 2019-07-15 stsp case -1:
396 3ce1b845 2019-07-15 stsp goto doneediting;
397 3ce1b845 2019-07-15 stsp case 0:
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
400 3ce1b845 2019-07-15 stsp }
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
404 3ce1b845 2019-07-15 stsp break;
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
413 3ce1b845 2019-07-15 stsp return -1;
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
422 3ce1b845 2019-07-15 stsp {
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
426 3ce1b845 2019-07-15 stsp FILE *fp;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
449 3ce1b845 2019-07-15 stsp len = 0;
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
454 3ce1b845 2019-07-15 stsp goto done;
455 3ce1b845 2019-07-15 stsp }
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
462 3ce1b845 2019-07-15 stsp }
463 3ce1b845 2019-07-15 stsp fclose(fp);
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
467 3ce1b845 2019-07-15 stsp len--;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
473 3ce1b845 2019-07-15 stsp done:
474 3ce1b845 2019-07-15 stsp if (err) {
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
477 3ce1b845 2019-07-15 stsp }
478 3ce1b845 2019-07-15 stsp return err;
479 3ce1b845 2019-07-15 stsp }
480 3ce1b845 2019-07-15 stsp
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
484 3ce1b845 2019-07-15 stsp {
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
487 3ce1b845 2019-07-15 stsp int fd;
488 3ce1b845 2019-07-15 stsp
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
493 3ce1b845 2019-07-15 stsp
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
496 3ce1b845 2019-07-15 stsp if (err)
497 3ce1b845 2019-07-15 stsp goto done;
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
500 3ce1b845 2019-07-15 stsp close(fd);
501 3ce1b845 2019-07-15 stsp
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 3ce1b845 2019-07-15 stsp done:
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
506 3ce1b845 2019-07-15 stsp }
507 3ce1b845 2019-07-15 stsp
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
510 3ce1b845 2019-07-15 stsp {
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
513 3ce1b845 2019-07-15 stsp }
514 3ce1b845 2019-07-15 stsp
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
517 84792843 2019-08-09 stsp {
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
520 84792843 2019-08-09 stsp
521 84792843 2019-08-09 stsp *author = NULL;
522 aba9c984 2019-09-08 stsp
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
529 aba9c984 2019-09-08 stsp }
530 84792843 2019-08-09 stsp
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
539 c9956ddf 2019-09-08 stsp }
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
542 84792843 2019-08-09 stsp }
543 84792843 2019-08-09 stsp
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
547 84792843 2019-08-09 stsp
548 84792843 2019-08-09 stsp /*
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
551 84792843 2019-08-09 stsp */
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp goto done;
557 aba9c984 2019-09-08 stsp }
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 aba9c984 2019-09-08 stsp goto done;
563 aba9c984 2019-09-08 stsp }
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 aba9c984 2019-09-08 stsp done:
569 aba9c984 2019-09-08 stsp if (err) {
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
572 aba9c984 2019-09-08 stsp }
573 aba9c984 2019-09-08 stsp return err;
574 c9956ddf 2019-09-08 stsp }
575 c9956ddf 2019-09-08 stsp
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
578 c9956ddf 2019-09-08 stsp {
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
580 c9956ddf 2019-09-08 stsp
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
585 c9956ddf 2019-09-08 stsp
586 c9956ddf 2019-09-08 stsp }
587 c9956ddf 2019-09-08 stsp return NULL;
588 84792843 2019-08-09 stsp }
589 84792843 2019-08-09 stsp
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
592 3ce1b845 2019-07-15 stsp {
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
601 3ce1b845 2019-07-15 stsp int ch;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
605 3ce1b845 2019-07-15 stsp
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
607 3ce1b845 2019-07-15 stsp
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
610 3ce1b845 2019-07-15 stsp case 'b':
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
612 3ce1b845 2019-07-15 stsp break;
613 3ce1b845 2019-07-15 stsp case 'm':
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp goto done;
618 3ce1b845 2019-07-15 stsp }
619 3ce1b845 2019-07-15 stsp break;
620 3ce1b845 2019-07-15 stsp case 'r':
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
624 9ba1d308 2019-10-21 stsp optarg);
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp case 'I':
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
630 3ce1b845 2019-07-15 stsp break;
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
632 3ce1b845 2019-07-15 stsp NULL);
633 3ce1b845 2019-07-15 stsp if (error)
634 3ce1b845 2019-07-15 stsp goto done;
635 3ce1b845 2019-07-15 stsp break;
636 3ce1b845 2019-07-15 stsp default:
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
639 3ce1b845 2019-07-15 stsp }
640 3ce1b845 2019-07-15 stsp }
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
644 3ce1b845 2019-07-15 stsp
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 aba9c984 2019-09-08 stsp "unveil",
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
650 3ce1b845 2019-07-15 stsp #endif
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
653 2c7829a4 2019-06-17 stsp
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
658 3ce1b845 2019-07-15 stsp }
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
661 c9956ddf 2019-09-08 stsp if (error)
662 c9956ddf 2019-09-08 stsp goto done;
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
664 3ce1b845 2019-07-15 stsp if (error)
665 3ce1b845 2019-07-15 stsp goto done;
666 aba9c984 2019-09-08 stsp
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
668 aba9c984 2019-09-08 stsp if (error)
669 aba9c984 2019-09-08 stsp return error;
670 e560b7e0 2019-11-28 stsp
671 e560b7e0 2019-11-28 stsp /*
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
675 e560b7e0 2019-11-28 stsp */
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
681 3ce1b845 2019-07-15 stsp goto done;
682 3ce1b845 2019-07-15 stsp }
683 3ce1b845 2019-07-15 stsp
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp } else {
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
691 3ce1b845 2019-07-15 stsp goto done;
692 3ce1b845 2019-07-15 stsp }
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
697 3ce1b845 2019-07-15 stsp goto done;
698 3ce1b845 2019-07-15 stsp }
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
700 3ce1b845 2019-07-15 stsp
701 3ce1b845 2019-07-15 stsp /*
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
704 3ce1b845 2019-07-15 stsp */
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
707 3ce1b845 2019-07-15 stsp if (error)
708 3ce1b845 2019-07-15 stsp goto done;
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 3ce1b845 2019-07-15 stsp goto done;
717 ef293bdd 2019-10-21 stsp }
718 3ce1b845 2019-07-15 stsp }
719 3ce1b845 2019-07-15 stsp
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 3ce1b845 2019-07-15 stsp
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 ef293bdd 2019-10-21 stsp goto done;
732 ef293bdd 2019-10-21 stsp }
733 ef293bdd 2019-10-21 stsp
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp goto done;
740 ef293bdd 2019-10-21 stsp }
741 3ce1b845 2019-07-15 stsp
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp goto done;
754 ef293bdd 2019-10-21 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp goto done;
761 ef293bdd 2019-10-21 stsp }
762 3ce1b845 2019-07-15 stsp
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp goto done;
769 ef293bdd 2019-10-21 stsp }
770 3ce1b845 2019-07-15 stsp
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp goto done;
777 ef293bdd 2019-10-21 stsp }
778 3ce1b845 2019-07-15 stsp
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp goto done;
784 ef293bdd 2019-10-21 stsp }
785 3ce1b845 2019-07-15 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
789 2c7829a4 2019-06-17 stsp done:
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
809 93658fb9 2020-03-18 stsp }
810 93658fb9 2020-03-18 stsp
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
813 93658fb9 2020-03-18 stsp {
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
816 93658fb9 2020-03-18 stsp exit(1);
817 93658fb9 2020-03-18 stsp }
818 892ac3b6 2020-03-18 stsp
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
824 892ac3b6 2020-03-18 stsp };
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
829 531c3985 2020-03-18 stsp {
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
834 b2409d58 2020-03-18 stsp
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
837 68999b92 2020-03-18 stsp
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
842 b2409d58 2020-03-18 stsp }
843 b2409d58 2020-03-18 stsp
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
852 892ac3b6 2020-03-18 stsp }
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
859 892ac3b6 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp }
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp }
870 61cc1a7a 2020-03-18 stsp }
871 b2409d58 2020-03-18 stsp
872 d2cdc636 2020-03-18 stsp }
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
883 892ac3b6 2020-03-18 stsp
884 531c3985 2020-03-18 stsp return NULL;
885 531c3985 2020-03-18 stsp }
886 531c3985 2020-03-18 stsp
887 531c3985 2020-03-18 stsp static const struct got_error *
888 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
890 4ba14133 2020-03-20 stsp {
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
893 4ba14133 2020-03-20 stsp
894 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
895 4ba14133 2020-03-20 stsp if (err)
896 4ba14133 2020-03-20 stsp return err;
897 4ba14133 2020-03-20 stsp
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
903 6338a6a1 2020-03-21 stsp }
904 4ba14133 2020-03-20 stsp return err;
905 4ba14133 2020-03-20 stsp }
906 4ba14133 2020-03-20 stsp
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
910 41b0de12 2020-03-21 stsp {
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
913 41b0de12 2020-03-21 stsp
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
917 41b0de12 2020-03-21 stsp
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
919 41b0de12 2020-03-21 stsp }
920 41b0de12 2020-03-21 stsp
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
925 41b0de12 2020-03-21 stsp
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
927 41b0de12 2020-03-21 stsp if (err)
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
931 41b0de12 2020-03-21 stsp }
932 41b0de12 2020-03-21 stsp
933 41b0de12 2020-03-21 stsp return NULL;
934 6338a6a1 2020-03-21 stsp }
935 6338a6a1 2020-03-21 stsp
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
939 6338a6a1 2020-03-21 stsp {
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
943 6338a6a1 2020-03-21 stsp
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
945 6338a6a1 2020-03-21 stsp if (err)
946 6338a6a1 2020-03-21 stsp return err;
947 6338a6a1 2020-03-21 stsp
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
949 6338a6a1 2020-03-21 stsp if (err)
950 6338a6a1 2020-03-21 stsp goto done;
951 6338a6a1 2020-03-21 stsp
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
954 6338a6a1 2020-03-21 stsp
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
957 6338a6a1 2020-03-21 stsp done:
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
960 0e4002ca 2020-03-21 stsp }
961 0e4002ca 2020-03-21 stsp
962 0e4002ca 2020-03-21 stsp static int
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
964 0e4002ca 2020-03-21 stsp {
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
966 0e4002ca 2020-03-21 stsp return 0;
967 0e4002ca 2020-03-21 stsp refname += 5;
968 0e4002ca 2020-03-21 stsp
969 0e4002ca 2020-03-21 stsp /*
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
972 0e4002ca 2020-03-21 stsp */
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
974 0e4002ca 2020-03-21 stsp return 0;
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
976 0e4002ca 2020-03-21 stsp return 0;
977 0e4002ca 2020-03-21 stsp
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
980 0e4002ca 2020-03-21 stsp
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
983 0e4002ca 2020-03-21 stsp return 1;
984 0e4002ca 2020-03-21 stsp
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
987 41b0de12 2020-03-21 stsp }
988 41b0de12 2020-03-21 stsp
989 0e4002ca 2020-03-21 stsp static int
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
991 0e4002ca 2020-03-21 stsp {
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
993 0e4002ca 2020-03-21 stsp
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
996 0e4002ca 2020-03-21 stsp return 1;
997 0e4002ca 2020-03-21 stsp }
998 0e4002ca 2020-03-21 stsp
999 0e4002ca 2020-03-21 stsp return 0;
1000 0e4002ca 2020-03-21 stsp }
1001 0e4002ca 2020-03-21 stsp
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1005 0e4002ca 2020-03-21 stsp {
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1008 0e4002ca 2020-03-21 stsp
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1011 0e4002ca 2020-03-21 stsp
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1019 0e4002ca 2020-03-21 stsp }
1020 0e4002ca 2020-03-21 stsp
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1023 93658fb9 2020-03-18 stsp {
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1044 93658fb9 2020-03-18 stsp
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1049 d9b4d0c0 2020-03-18 stsp
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1052 659e7fbd 2020-03-20 stsp case 'a':
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1054 4ba14133 2020-03-20 stsp break;
1055 4ba14133 2020-03-20 stsp case 'b':
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1060 659e7fbd 2020-03-20 stsp break;
1061 41b0de12 2020-03-21 stsp case 'l':
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1063 41b0de12 2020-03-21 stsp break;
1064 469dd726 2020-03-20 stsp case 'm':
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1066 469dd726 2020-03-20 stsp break;
1067 68999b92 2020-03-18 stsp case 'v':
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1072 68999b92 2020-03-18 stsp break;
1073 68999b92 2020-03-18 stsp case 'q':
1074 68999b92 2020-03-18 stsp verbosity = -1;
1075 68999b92 2020-03-18 stsp break;
1076 0e4002ca 2020-03-21 stsp case 'R':
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1081 0e4002ca 2020-03-21 stsp break;
1082 93658fb9 2020-03-18 stsp default:
1083 93658fb9 2020-03-18 stsp usage_clone();
1084 93658fb9 2020-03-18 stsp break;
1085 93658fb9 2020-03-18 stsp }
1086 93658fb9 2020-03-18 stsp }
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1089 39c64a6a 2020-03-18 stsp
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1103 41b0de12 2020-03-21 stsp }
1104 4ba14133 2020-03-20 stsp
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1106 9df6f38b 2020-03-18 stsp
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1111 93658fb9 2020-03-18 stsp else
1112 93658fb9 2020-03-18 stsp usage_clone();
1113 09838ffc 2020-03-18 stsp
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1118 09f63084 2020-03-20 stsp
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1124 09f63084 2020-03-20 stsp }
1125 09838ffc 2020-03-18 stsp
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1131 b46f3e71 2020-03-18 stsp #endif
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1138 b46f3e71 2020-03-18 stsp #endif
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1143 39c64a6a 2020-03-18 stsp } else {
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1146 39c64a6a 2020-03-18 stsp }
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1151 bb64b798 2020-03-18 stsp }
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1153 bb64b798 2020-03-18 stsp } else
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1155 bb64b798 2020-03-18 stsp
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1160 bb64b798 2020-03-18 stsp
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1167 41b0de12 2020-03-21 stsp }
1168 bb64b798 2020-03-18 stsp
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1174 ee448f5f 2020-03-18 stsp }
1175 ee448f5f 2020-03-18 stsp }
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1179 ee448f5f 2020-03-18 stsp
1180 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 9c52365f 2020-03-21 stsp server_path, verbosity);
1182 39c64a6a 2020-03-18 stsp if (error)
1183 bb64b798 2020-03-18 stsp goto done;
1184 294dfefd 2020-03-18 stsp
1185 68999b92 2020-03-18 stsp if (verbosity >= 0)
1186 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1187 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1188 bb64b798 2020-03-18 stsp
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1200 d9b4d0c0 2020-03-18 stsp
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1204 41b0de12 2020-03-21 stsp }
1205 41b0de12 2020-03-21 stsp
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1212 d9b4d0c0 2020-03-18 stsp
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1218 0e4002ca 2020-03-21 stsp
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1226 0e4002ca 2020-03-21 stsp continue;
1227 0e4002ca 2020-03-21 stsp }
1228 668a20f6 2020-03-18 stsp
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1232 d9b4d0c0 2020-03-18 stsp
1233 469dd726 2020-03-20 stsp if (mirror_references)
1234 469dd726 2020-03-20 stsp continue;
1235 469dd726 2020-03-20 stsp
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1237 7ebc0570 2020-03-18 stsp continue;
1238 7ebc0570 2020-03-18 stsp
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1244 7ebc0570 2020-03-18 stsp }
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1249 d9b4d0c0 2020-03-18 stsp }
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1257 d9b4d0c0 2020-03-18 stsp
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1259 d9b4d0c0 2020-03-18 stsp continue;
1260 d9b4d0c0 2020-03-18 stsp
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1265 d9b4d0c0 2020-03-18 stsp continue;
1266 55330abe 2020-03-20 stsp }
1267 d9b4d0c0 2020-03-18 stsp goto done;
1268 d9b4d0c0 2020-03-18 stsp }
1269 d9b4d0c0 2020-03-18 stsp
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1274 f298ae0f 2020-03-25 stsp
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1276 f298ae0f 2020-03-25 stsp continue;
1277 f298ae0f 2020-03-25 stsp
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1279 f298ae0f 2020-03-25 stsp continue;
1280 f298ae0f 2020-03-25 stsp
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1286 f298ae0f 2020-03-25 stsp }
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1293 f298ae0f 2020-03-25 stsp }
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1300 f298ae0f 2020-03-25 stsp continue;
1301 f298ae0f 2020-03-25 stsp }
1302 f298ae0f 2020-03-25 stsp goto done;
1303 f298ae0f 2020-03-25 stsp }
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1311 4ba14133 2020-03-20 stsp }
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1313 4ba14133 2020-03-20 stsp /*
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1317 4ba14133 2020-03-20 stsp */
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1321 d9b4d0c0 2020-03-18 stsp
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1326 4ba14133 2020-03-20 stsp continue;
1327 4ba14133 2020-03-20 stsp }
1328 4ba14133 2020-03-20 stsp goto done;
1329 4ba14133 2020-03-20 stsp }
1330 4ba14133 2020-03-20 stsp
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1336 4ba14133 2020-03-20 stsp break;
1337 4ba14133 2020-03-20 stsp }
1338 659e7fbd 2020-03-20 stsp }
1339 659e7fbd 2020-03-20 stsp
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1345 659e7fbd 2020-03-20 stsp }
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1350 b46f3e71 2020-03-18 stsp }
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1360 659e7fbd 2020-03-20 stsp }
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1370 659e7fbd 2020-03-20 stsp }
1371 659e7fbd 2020-03-20 stsp } else {
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1373 d715f13e 2020-03-19 stsp
1374 659e7fbd 2020-03-20 stsp /*
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1377 659e7fbd 2020-03-20 stsp */
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1382 659e7fbd 2020-03-20 stsp } else
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1393 659e7fbd 2020-03-20 stsp }
1394 659e7fbd 2020-03-20 stsp }
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1399 659e7fbd 2020-03-20 stsp }
1400 659e7fbd 2020-03-20 stsp
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1404 09838ffc 2020-03-18 stsp done:
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1410 9c52365f 2020-03-21 stsp }
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1415 bb64b798 2020-03-18 stsp if (repo)
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1422 d9b4d0c0 2020-03-18 stsp }
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1427 d9b4d0c0 2020-03-18 stsp }
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1441 7848a0e1 2020-03-19 stsp }
1442 7848a0e1 2020-03-19 stsp
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1446 7848a0e1 2020-03-19 stsp {
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1450 7848a0e1 2020-03-19 stsp
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1452 7848a0e1 2020-03-19 stsp if (err)
1453 7848a0e1 2020-03-19 stsp goto done;
1454 7848a0e1 2020-03-19 stsp
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1458 88609724 2020-03-21 stsp if (err)
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1465 db6d8ad8 2020-03-21 stsp }
1466 db6d8ad8 2020-03-21 stsp goto done;
1467 db6d8ad8 2020-03-21 stsp }
1468 db6d8ad8 2020-03-21 stsp
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1474 688f11b3 2020-03-21 stsp }
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1476 7848a0e1 2020-03-19 stsp if (err)
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1479 e8a967e0 2020-03-21 stsp if (err)
1480 e8a967e0 2020-03-21 stsp goto done;
1481 7848a0e1 2020-03-19 stsp } else {
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1483 7848a0e1 2020-03-19 stsp if (err)
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1487 6338a6a1 2020-03-21 stsp
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1489 6338a6a1 2020-03-21 stsp if (err)
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1492 6338a6a1 2020-03-21 stsp if (err)
1493 6338a6a1 2020-03-21 stsp goto done;
1494 7848a0e1 2020-03-19 stsp }
1495 6338a6a1 2020-03-21 stsp
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1499 7848a0e1 2020-03-19 stsp done:
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1503 2ab43947 2020-03-18 stsp }
1504 2ab43947 2020-03-18 stsp
1505 2ab43947 2020-03-18 stsp __dead static void
1506 7848a0e1 2020-03-19 stsp usage_fetch(void)
1507 7848a0e1 2020-03-19 stsp {
1508 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1509 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1510 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1511 13f12b09 2020-03-21 stsp getprogname());
1512 7848a0e1 2020-03-19 stsp exit(1);
1513 7848a0e1 2020-03-19 stsp }
1514 7848a0e1 2020-03-19 stsp
1515 7848a0e1 2020-03-19 stsp static const struct got_error *
1516 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1517 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1518 f21ec2f0 2020-03-21 stsp {
1519 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1520 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1521 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1522 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1523 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1524 f21ec2f0 2020-03-21 stsp char *id_str;
1525 f21ec2f0 2020-03-21 stsp
1526 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1527 f21ec2f0 2020-03-21 stsp
1528 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1529 f21ec2f0 2020-03-21 stsp if (err)
1530 f21ec2f0 2020-03-21 stsp return err;
1531 f21ec2f0 2020-03-21 stsp
1532 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1533 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1534 f21ec2f0 2020-03-21 stsp
1535 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1536 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1537 f21ec2f0 2020-03-21 stsp continue;
1538 f21ec2f0 2020-03-21 stsp
1539 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1540 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1541 f21ec2f0 2020-03-21 stsp break;
1542 f21ec2f0 2020-03-21 stsp }
1543 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1544 f21ec2f0 2020-03-21 stsp continue;
1545 f21ec2f0 2020-03-21 stsp
1546 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1547 f21ec2f0 2020-03-21 stsp if (err)
1548 f21ec2f0 2020-03-21 stsp break;
1549 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1550 f21ec2f0 2020-03-21 stsp free(id);
1551 f21ec2f0 2020-03-21 stsp if (err)
1552 f21ec2f0 2020-03-21 stsp break;
1553 f21ec2f0 2020-03-21 stsp
1554 f21ec2f0 2020-03-21 stsp free(id_str);
1555 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1556 f21ec2f0 2020-03-21 stsp if (err)
1557 f21ec2f0 2020-03-21 stsp break;
1558 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1559 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1560 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1561 6338a6a1 2020-03-21 stsp }
1562 f21ec2f0 2020-03-21 stsp }
1563 0e4002ca 2020-03-21 stsp
1564 0e4002ca 2020-03-21 stsp return err;
1565 0e4002ca 2020-03-21 stsp }
1566 0e4002ca 2020-03-21 stsp
1567 0e4002ca 2020-03-21 stsp static const struct got_error *
1568 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1569 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1570 0e4002ca 2020-03-21 stsp {
1571 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1572 0e4002ca 2020-03-21 stsp char *remote_refname;
1573 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1574 0e4002ca 2020-03-21 stsp
1575 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1576 0e4002ca 2020-03-21 stsp refname += 5;
1577 0e4002ca 2020-03-21 stsp
1578 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1579 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1580 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1581 f21ec2f0 2020-03-21 stsp
1582 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1583 0e4002ca 2020-03-21 stsp if (err) {
1584 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1585 0e4002ca 2020-03-21 stsp goto done;
1586 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1587 0e4002ca 2020-03-21 stsp } else {
1588 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1589 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1590 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1591 9f142382 2020-03-21 stsp err = unlock_err;
1592 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1593 0e4002ca 2020-03-21 stsp }
1594 0e4002ca 2020-03-21 stsp done:
1595 0e4002ca 2020-03-21 stsp free(remote_refname);
1596 f21ec2f0 2020-03-21 stsp return err;
1597 f21ec2f0 2020-03-21 stsp }
1598 f21ec2f0 2020-03-21 stsp
1599 f21ec2f0 2020-03-21 stsp static const struct got_error *
1600 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1601 7848a0e1 2020-03-19 stsp {
1602 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1603 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1604 7848a0e1 2020-03-19 stsp const char *remote_name;
1605 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1606 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1607 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1608 7848a0e1 2020-03-19 stsp int nremotes;
1609 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1610 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1611 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1612 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1613 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1614 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1615 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1616 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1617 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1618 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1619 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1620 7848a0e1 2020-03-19 stsp
1621 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1622 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1623 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1624 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1625 7848a0e1 2020-03-19 stsp
1626 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1627 7848a0e1 2020-03-19 stsp switch (ch) {
1628 659e7fbd 2020-03-20 stsp case 'a':
1629 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1630 4ba14133 2020-03-20 stsp break;
1631 4ba14133 2020-03-20 stsp case 'b':
1632 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1633 4ba14133 2020-03-20 stsp optarg, NULL);
1634 4ba14133 2020-03-20 stsp if (error)
1635 4ba14133 2020-03-20 stsp return error;
1636 41b0de12 2020-03-21 stsp break;
1637 f21ec2f0 2020-03-21 stsp case 'd':
1638 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1639 f21ec2f0 2020-03-21 stsp break;
1640 41b0de12 2020-03-21 stsp case 'l':
1641 41b0de12 2020-03-21 stsp list_refs_only = 1;
1642 659e7fbd 2020-03-20 stsp break;
1643 7848a0e1 2020-03-19 stsp case 'r':
1644 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1645 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1646 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1647 7848a0e1 2020-03-19 stsp optarg);
1648 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1649 7848a0e1 2020-03-19 stsp break;
1650 db6d8ad8 2020-03-21 stsp case 't':
1651 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1652 db6d8ad8 2020-03-21 stsp break;
1653 7848a0e1 2020-03-19 stsp case 'v':
1654 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1655 7848a0e1 2020-03-19 stsp verbosity = 0;
1656 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1657 7848a0e1 2020-03-19 stsp verbosity++;
1658 7848a0e1 2020-03-19 stsp break;
1659 7848a0e1 2020-03-19 stsp case 'q':
1660 7848a0e1 2020-03-19 stsp verbosity = -1;
1661 7848a0e1 2020-03-19 stsp break;
1662 0e4002ca 2020-03-21 stsp case 'R':
1663 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1664 0e4002ca 2020-03-21 stsp optarg, NULL);
1665 0e4002ca 2020-03-21 stsp if (error)
1666 0e4002ca 2020-03-21 stsp return error;
1667 0e4002ca 2020-03-21 stsp break;
1668 7848a0e1 2020-03-19 stsp default:
1669 7848a0e1 2020-03-19 stsp usage_fetch();
1670 7848a0e1 2020-03-19 stsp break;
1671 7848a0e1 2020-03-19 stsp }
1672 7848a0e1 2020-03-19 stsp }
1673 7848a0e1 2020-03-19 stsp argc -= optind;
1674 7848a0e1 2020-03-19 stsp argv += optind;
1675 7848a0e1 2020-03-19 stsp
1676 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1677 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1678 41b0de12 2020-03-21 stsp if (list_refs_only) {
1679 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1680 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1681 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1682 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1683 f21ec2f0 2020-03-21 stsp if (delete_refs)
1684 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1685 41b0de12 2020-03-21 stsp if (verbosity == -1)
1686 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1687 41b0de12 2020-03-21 stsp }
1688 4ba14133 2020-03-20 stsp
1689 7848a0e1 2020-03-19 stsp if (argc == 0)
1690 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1691 7848a0e1 2020-03-19 stsp else if (argc == 1)
1692 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1693 7848a0e1 2020-03-19 stsp else
1694 7848a0e1 2020-03-19 stsp usage_fetch();
1695 7848a0e1 2020-03-19 stsp
1696 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1697 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1698 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1699 7848a0e1 2020-03-19 stsp goto done;
1700 7848a0e1 2020-03-19 stsp }
1701 7848a0e1 2020-03-19 stsp
1702 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1703 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1704 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1705 7848a0e1 2020-03-19 stsp goto done;
1706 7848a0e1 2020-03-19 stsp else
1707 7848a0e1 2020-03-19 stsp error = NULL;
1708 7848a0e1 2020-03-19 stsp if (worktree) {
1709 7848a0e1 2020-03-19 stsp repo_path =
1710 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1711 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1712 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1713 7848a0e1 2020-03-19 stsp if (error)
1714 7848a0e1 2020-03-19 stsp goto done;
1715 7848a0e1 2020-03-19 stsp } else {
1716 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1717 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1718 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1719 7848a0e1 2020-03-19 stsp goto done;
1720 7848a0e1 2020-03-19 stsp }
1721 7848a0e1 2020-03-19 stsp }
1722 7848a0e1 2020-03-19 stsp }
1723 7848a0e1 2020-03-19 stsp
1724 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1725 7848a0e1 2020-03-19 stsp if (error)
1726 7848a0e1 2020-03-19 stsp goto done;
1727 7848a0e1 2020-03-19 stsp
1728 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1729 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1730 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1731 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1732 7848a0e1 2020-03-19 stsp break;
1733 7848a0e1 2020-03-19 stsp }
1734 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1735 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1736 7848a0e1 2020-03-19 stsp goto done;
1737 7848a0e1 2020-03-19 stsp }
1738 7848a0e1 2020-03-19 stsp
1739 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1740 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1741 7848a0e1 2020-03-19 stsp if (error)
1742 7848a0e1 2020-03-19 stsp goto done;
1743 7848a0e1 2020-03-19 stsp
1744 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1745 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1746 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1747 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1748 7848a0e1 2020-03-19 stsp err(1, "pledge");
1749 7848a0e1 2020-03-19 stsp #endif
1750 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1751 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1752 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1753 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1754 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1755 7848a0e1 2020-03-19 stsp err(1, "pledge");
1756 7848a0e1 2020-03-19 stsp #endif
1757 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1758 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1759 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1760 7848a0e1 2020-03-19 stsp goto done;
1761 7848a0e1 2020-03-19 stsp } else {
1762 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1763 7848a0e1 2020-03-19 stsp goto done;
1764 7848a0e1 2020-03-19 stsp }
1765 7848a0e1 2020-03-19 stsp
1766 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1767 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1768 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1769 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1770 7848a0e1 2020-03-19 stsp goto done;
1771 7848a0e1 2020-03-19 stsp }
1772 7848a0e1 2020-03-19 stsp }
1773 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1774 7848a0e1 2020-03-19 stsp if (error)
1775 7848a0e1 2020-03-19 stsp goto done;
1776 7848a0e1 2020-03-19 stsp
1777 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1778 9c52365f 2020-03-21 stsp server_path, verbosity);
1779 7848a0e1 2020-03-19 stsp if (error)
1780 7848a0e1 2020-03-19 stsp goto done;
1781 7848a0e1 2020-03-19 stsp
1782 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1783 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1784 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1785 7848a0e1 2020-03-19 stsp
1786 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1787 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1788 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1789 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1790 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1791 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1792 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1793 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1794 7848a0e1 2020-03-19 stsp if (error)
1795 7848a0e1 2020-03-19 stsp goto done;
1796 7848a0e1 2020-03-19 stsp
1797 41b0de12 2020-03-21 stsp if (list_refs_only) {
1798 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1799 41b0de12 2020-03-21 stsp goto done;
1800 41b0de12 2020-03-21 stsp }
1801 41b0de12 2020-03-21 stsp
1802 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1803 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1804 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1805 f21ec2f0 2020-03-21 stsp if (delete_refs)
1806 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1807 7848a0e1 2020-03-19 stsp goto done;
1808 7848a0e1 2020-03-19 stsp }
1809 7848a0e1 2020-03-19 stsp
1810 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1811 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1812 984065c8 2020-03-19 stsp if (error)
1813 984065c8 2020-03-19 stsp goto done;
1814 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1815 984065c8 2020-03-19 stsp free(id_str);
1816 984065c8 2020-03-19 stsp id_str = NULL;
1817 984065c8 2020-03-19 stsp }
1818 7848a0e1 2020-03-19 stsp
1819 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1820 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1821 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1822 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1823 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1824 7848a0e1 2020-03-19 stsp char *remote_refname;
1825 7848a0e1 2020-03-19 stsp
1826 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1827 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1828 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1829 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1830 0e4002ca 2020-03-21 stsp if (error)
1831 0e4002ca 2020-03-21 stsp goto done;
1832 0e4002ca 2020-03-21 stsp continue;
1833 0e4002ca 2020-03-21 stsp }
1834 0e4002ca 2020-03-21 stsp
1835 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1836 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1837 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1838 7848a0e1 2020-03-19 stsp if (error) {
1839 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1840 7848a0e1 2020-03-19 stsp goto done;
1841 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1842 6338a6a1 2020-03-21 stsp repo);
1843 7848a0e1 2020-03-19 stsp if (error)
1844 7848a0e1 2020-03-19 stsp goto done;
1845 7848a0e1 2020-03-19 stsp } else {
1846 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1847 db6d8ad8 2020-03-21 stsp verbosity, repo);
1848 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1849 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1850 9f142382 2020-03-21 stsp error = unlock_err;
1851 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1852 7848a0e1 2020-03-19 stsp if (error)
1853 7848a0e1 2020-03-19 stsp goto done;
1854 7848a0e1 2020-03-19 stsp }
1855 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1856 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1857 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1858 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1859 7848a0e1 2020-03-19 stsp goto done;
1860 7848a0e1 2020-03-19 stsp }
1861 7848a0e1 2020-03-19 stsp
1862 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1863 7848a0e1 2020-03-19 stsp if (error) {
1864 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1865 7848a0e1 2020-03-19 stsp goto done;
1866 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1867 688f11b3 2020-03-21 stsp verbosity, repo);
1868 7848a0e1 2020-03-19 stsp if (error)
1869 7848a0e1 2020-03-19 stsp goto done;
1870 7848a0e1 2020-03-19 stsp } else {
1871 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1872 db6d8ad8 2020-03-21 stsp verbosity, repo);
1873 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1874 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1875 9f142382 2020-03-21 stsp error = unlock_err;
1876 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1877 7848a0e1 2020-03-19 stsp if (error)
1878 7848a0e1 2020-03-19 stsp goto done;
1879 7848a0e1 2020-03-19 stsp }
1880 2ec30c80 2020-03-20 stsp
1881 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1882 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1883 2ec30c80 2020-03-20 stsp if (error) {
1884 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1885 2ec30c80 2020-03-20 stsp goto done;
1886 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1887 6338a6a1 2020-03-21 stsp repo);
1888 2ec30c80 2020-03-20 stsp if (error)
1889 2ec30c80 2020-03-20 stsp goto done;
1890 9f142382 2020-03-21 stsp } else {
1891 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1892 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1893 9f142382 2020-03-21 stsp error = unlock_err;
1894 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1895 9f142382 2020-03-21 stsp }
1896 7848a0e1 2020-03-19 stsp }
1897 7848a0e1 2020-03-19 stsp }
1898 f21ec2f0 2020-03-21 stsp if (delete_refs)
1899 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1900 7848a0e1 2020-03-19 stsp done:
1901 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1902 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1903 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1904 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1905 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1906 9c52365f 2020-03-21 stsp }
1907 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1908 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1909 7848a0e1 2020-03-19 stsp if (repo)
1910 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1911 7848a0e1 2020-03-19 stsp if (worktree)
1912 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1913 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1914 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1915 7848a0e1 2020-03-19 stsp free(pe->data);
1916 7848a0e1 2020-03-19 stsp }
1917 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1918 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1919 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1920 7848a0e1 2020-03-19 stsp free(pe->data);
1921 7848a0e1 2020-03-19 stsp }
1922 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1923 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1924 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1925 7848a0e1 2020-03-19 stsp free(id_str);
1926 7848a0e1 2020-03-19 stsp free(cwd);
1927 7848a0e1 2020-03-19 stsp free(repo_path);
1928 7848a0e1 2020-03-19 stsp free(pack_hash);
1929 7848a0e1 2020-03-19 stsp free(proto);
1930 7848a0e1 2020-03-19 stsp free(host);
1931 7848a0e1 2020-03-19 stsp free(port);
1932 7848a0e1 2020-03-19 stsp free(server_path);
1933 7848a0e1 2020-03-19 stsp free(repo_name);
1934 7848a0e1 2020-03-19 stsp return error;
1935 7848a0e1 2020-03-19 stsp }
1936 7848a0e1 2020-03-19 stsp
1937 7848a0e1 2020-03-19 stsp
1938 7848a0e1 2020-03-19 stsp __dead static void
1939 2ab43947 2020-03-18 stsp usage_checkout(void)
1940 2ab43947 2020-03-18 stsp {
1941 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1942 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1943 2ab43947 2020-03-18 stsp exit(1);
1944 2ab43947 2020-03-18 stsp }
1945 2ab43947 2020-03-18 stsp
1946 2ab43947 2020-03-18 stsp static void
1947 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1948 2ab43947 2020-03-18 stsp {
1949 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1950 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1951 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1952 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1953 2ab43947 2020-03-18 stsp getprogname());
1954 2ab43947 2020-03-18 stsp }
1955 2ab43947 2020-03-18 stsp
1956 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1957 2ab43947 2020-03-18 stsp const char *worktree_path;
1958 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1959 2ab43947 2020-03-18 stsp };
1960 2ab43947 2020-03-18 stsp
1961 2ab43947 2020-03-18 stsp static const struct got_error *
1962 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1963 2ab43947 2020-03-18 stsp {
1964 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1965 2ab43947 2020-03-18 stsp
1966 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1967 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1968 2ab43947 2020-03-18 stsp return NULL;
1969 2ab43947 2020-03-18 stsp
1970 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1971 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1972 2ab43947 2020-03-18 stsp return NULL;
1973 2ab43947 2020-03-18 stsp }
1974 2ab43947 2020-03-18 stsp
1975 2ab43947 2020-03-18 stsp while (path[0] == '/')
1976 2ab43947 2020-03-18 stsp path++;
1977 2ab43947 2020-03-18 stsp
1978 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1979 2ab43947 2020-03-18 stsp return NULL;
1980 2ab43947 2020-03-18 stsp }
1981 2ab43947 2020-03-18 stsp
1982 2ab43947 2020-03-18 stsp static const struct got_error *
1983 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1984 2ab43947 2020-03-18 stsp {
1985 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1986 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1987 2ab43947 2020-03-18 stsp return NULL;
1988 2ab43947 2020-03-18 stsp }
1989 2ab43947 2020-03-18 stsp
1990 2ab43947 2020-03-18 stsp static const struct got_error *
1991 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1992 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1993 2ab43947 2020-03-18 stsp struct got_repository *repo)
1994 2ab43947 2020-03-18 stsp {
1995 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1996 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1997 2ab43947 2020-03-18 stsp
1998 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1999 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2000 2ab43947 2020-03-18 stsp if (err)
2001 2ab43947 2020-03-18 stsp return err;
2002 2ab43947 2020-03-18 stsp
2003 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2004 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2005 2ab43947 2020-03-18 stsp
2006 2ab43947 2020-03-18 stsp /*
2007 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2008 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2009 2ab43947 2020-03-18 stsp *
2010 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2011 2ab43947 2020-03-18 stsp *
2012 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2013 2ab43947 2020-03-18 stsp * \ /
2014 2ab43947 2020-03-18 stsp * C E
2015 2ab43947 2020-03-18 stsp * \ /
2016 2ab43947 2020-03-18 stsp * B (yca)
2017 2ab43947 2020-03-18 stsp * |
2018 2ab43947 2020-03-18 stsp * A
2019 2ab43947 2020-03-18 stsp *
2020 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2021 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2022 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2023 2ab43947 2020-03-18 stsp */
2024 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2025 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2026 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2027 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2028 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2029 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2030 2ab43947 2020-03-18 stsp
2031 2ab43947 2020-03-18 stsp free(yca_id);
2032 2ab43947 2020-03-18 stsp return NULL;
2033 2ab43947 2020-03-18 stsp }
2034 2ab43947 2020-03-18 stsp
2035 2ab43947 2020-03-18 stsp static const struct got_error *
2036 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2037 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2038 2ab43947 2020-03-18 stsp struct got_repository *repo)
2039 2ab43947 2020-03-18 stsp {
2040 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2041 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2042 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2043 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2044 2ab43947 2020-03-18 stsp
2045 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2046 2ab43947 2020-03-18 stsp if (err)
2047 2ab43947 2020-03-18 stsp goto done;
2048 2ab43947 2020-03-18 stsp
2049 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2050 2ab43947 2020-03-18 stsp is_same_branch = 1;
2051 2ab43947 2020-03-18 stsp goto done;
2052 2ab43947 2020-03-18 stsp }
2053 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2054 2ab43947 2020-03-18 stsp is_same_branch = 1;
2055 2ab43947 2020-03-18 stsp goto done;
2056 2ab43947 2020-03-18 stsp }
2057 2ab43947 2020-03-18 stsp
2058 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2059 2ab43947 2020-03-18 stsp if (err)
2060 2ab43947 2020-03-18 stsp goto done;
2061 2ab43947 2020-03-18 stsp
2062 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2063 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2064 2ab43947 2020-03-18 stsp if (err)
2065 2ab43947 2020-03-18 stsp goto done;
2066 2ab43947 2020-03-18 stsp
2067 2ab43947 2020-03-18 stsp for (;;) {
2068 2ab43947 2020-03-18 stsp struct got_object_id *id;
2069 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2070 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2071 2ab43947 2020-03-18 stsp if (err) {
2072 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2073 2ab43947 2020-03-18 stsp err = NULL;
2074 2ab43947 2020-03-18 stsp break;
2075 2ab43947 2020-03-18 stsp }
2076 2ab43947 2020-03-18 stsp
2077 2ab43947 2020-03-18 stsp if (id) {
2078 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2079 2ab43947 2020-03-18 stsp break;
2080 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2081 2ab43947 2020-03-18 stsp is_same_branch = 1;
2082 2ab43947 2020-03-18 stsp break;
2083 2ab43947 2020-03-18 stsp }
2084 2ab43947 2020-03-18 stsp }
2085 2ab43947 2020-03-18 stsp }
2086 2ab43947 2020-03-18 stsp done:
2087 2ab43947 2020-03-18 stsp if (graph)
2088 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2089 2ab43947 2020-03-18 stsp free(head_commit_id);
2090 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2091 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2092 2ab43947 2020-03-18 stsp return err;
2093 a367ff0f 2019-05-14 stsp }
2094 8069f636 2019-01-12 stsp
2095 4ed7e80c 2018-05-20 stsp static const struct got_error *
2096 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2097 4b6c9460 2020-03-05 stsp {
2098 4b6c9460 2020-03-05 stsp static char msg[512];
2099 4b6c9460 2020-03-05 stsp const char *branch_name;
2100 4b6c9460 2020-03-05 stsp
2101 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2102 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2103 4b6c9460 2020-03-05 stsp else
2104 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2105 4b6c9460 2020-03-05 stsp
2106 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2107 4b6c9460 2020-03-05 stsp branch_name += 11;
2108 4b6c9460 2020-03-05 stsp
2109 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2110 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2111 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2112 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2113 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2114 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2115 4b6c9460 2020-03-05 stsp
2116 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2117 4b6c9460 2020-03-05 stsp }
2118 4b6c9460 2020-03-05 stsp
2119 4b6c9460 2020-03-05 stsp static const struct got_error *
2120 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2121 c09a553d 2018-03-12 stsp {
2122 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2123 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2124 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2125 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2126 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2127 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2128 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2129 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2130 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2131 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2132 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2133 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2134 f2ea84fa 2019-07-27 stsp
2135 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2136 c09a553d 2018-03-12 stsp
2137 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2138 0bb8a95e 2018-03-12 stsp switch (ch) {
2139 08573d5b 2019-05-14 stsp case 'b':
2140 08573d5b 2019-05-14 stsp branch_name = optarg;
2141 08573d5b 2019-05-14 stsp break;
2142 8069f636 2019-01-12 stsp case 'c':
2143 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2144 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2145 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2146 bb51a5b4 2020-01-13 stsp break;
2147 bb51a5b4 2020-01-13 stsp case 'E':
2148 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2149 8069f636 2019-01-12 stsp break;
2150 0bb8a95e 2018-03-12 stsp case 'p':
2151 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2152 0bb8a95e 2018-03-12 stsp break;
2153 0bb8a95e 2018-03-12 stsp default:
2154 2deda0b9 2019-03-07 stsp usage_checkout();
2155 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2156 0bb8a95e 2018-03-12 stsp }
2157 0bb8a95e 2018-03-12 stsp }
2158 0bb8a95e 2018-03-12 stsp
2159 0bb8a95e 2018-03-12 stsp argc -= optind;
2160 0bb8a95e 2018-03-12 stsp argv += optind;
2161 0bb8a95e 2018-03-12 stsp
2162 6715a751 2018-03-16 stsp #ifndef PROFILE
2163 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2164 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2165 c09a553d 2018-03-12 stsp err(1, "pledge");
2166 6715a751 2018-03-16 stsp #endif
2167 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2168 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2169 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2170 76089277 2018-04-01 stsp if (repo_path == NULL)
2171 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2172 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2173 76089277 2018-04-01 stsp if (cwd == NULL) {
2174 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2175 76089277 2018-04-01 stsp goto done;
2176 76089277 2018-04-01 stsp }
2177 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2178 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2179 230a42bd 2019-05-11 jcs if (base == NULL) {
2180 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2181 230a42bd 2019-05-11 jcs path_prefix);
2182 230a42bd 2019-05-11 jcs goto done;
2183 230a42bd 2019-05-11 jcs }
2184 230a42bd 2019-05-11 jcs } else {
2185 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2186 230a42bd 2019-05-11 jcs if (base == NULL) {
2187 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2188 230a42bd 2019-05-11 jcs repo_path);
2189 230a42bd 2019-05-11 jcs goto done;
2190 230a42bd 2019-05-11 jcs }
2191 76089277 2018-04-01 stsp }
2192 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2193 c09a553d 2018-03-12 stsp if (dotgit)
2194 c09a553d 2018-03-12 stsp *dotgit = '\0';
2195 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2196 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2197 c09a553d 2018-03-12 stsp free(cwd);
2198 76089277 2018-04-01 stsp goto done;
2199 c09a553d 2018-03-12 stsp }
2200 c09a553d 2018-03-12 stsp free(cwd);
2201 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2202 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2203 76089277 2018-04-01 stsp if (repo_path == NULL) {
2204 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2205 76089277 2018-04-01 stsp goto done;
2206 76089277 2018-04-01 stsp }
2207 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2208 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2209 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2210 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2211 b4b3a7dd 2019-07-22 stsp argv[1]);
2212 b4b3a7dd 2019-07-22 stsp goto done;
2213 b4b3a7dd 2019-07-22 stsp }
2214 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2215 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2216 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2217 b4b3a7dd 2019-07-22 stsp goto done;
2218 b4b3a7dd 2019-07-22 stsp }
2219 76089277 2018-04-01 stsp }
2220 c09a553d 2018-03-12 stsp } else
2221 c09a553d 2018-03-12 stsp usage_checkout();
2222 c09a553d 2018-03-12 stsp
2223 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2224 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2225 13bfb272 2019-05-10 jcs
2226 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2227 c09a553d 2018-03-12 stsp if (error != NULL)
2228 c02c541e 2019-03-29 stsp goto done;
2229 c02c541e 2019-03-29 stsp
2230 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2231 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2232 c530dc23 2019-07-23 stsp if (error) {
2233 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2234 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2235 c530dc23 2019-07-23 stsp goto done;
2236 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2237 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2238 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2239 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2240 c530dc23 2019-07-23 stsp goto done;
2241 c530dc23 2019-07-23 stsp }
2242 c530dc23 2019-07-23 stsp }
2243 c530dc23 2019-07-23 stsp
2244 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2245 c02c541e 2019-03-29 stsp if (error)
2246 c09a553d 2018-03-12 stsp goto done;
2247 8069f636 2019-01-12 stsp
2248 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2249 c09a553d 2018-03-12 stsp if (error != NULL)
2250 c09a553d 2018-03-12 stsp goto done;
2251 c09a553d 2018-03-12 stsp
2252 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2253 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2254 c09a553d 2018-03-12 stsp goto done;
2255 c09a553d 2018-03-12 stsp
2256 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2257 c09a553d 2018-03-12 stsp if (error != NULL)
2258 c09a553d 2018-03-12 stsp goto done;
2259 c09a553d 2018-03-12 stsp
2260 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2261 e5dc7198 2018-12-29 stsp path_prefix);
2262 e5dc7198 2018-12-29 stsp if (error != NULL)
2263 e5dc7198 2018-12-29 stsp goto done;
2264 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2265 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2266 49520a32 2018-12-29 stsp goto done;
2267 49520a32 2018-12-29 stsp }
2268 49520a32 2018-12-29 stsp
2269 8069f636 2019-01-12 stsp if (commit_id_str) {
2270 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2271 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2272 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2273 30837e32 2019-07-25 stsp if (error)
2274 8069f636 2019-01-12 stsp goto done;
2275 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2276 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2277 8069f636 2019-01-12 stsp if (error != NULL) {
2278 8069f636 2019-01-12 stsp free(commit_id);
2279 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2280 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2281 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2282 4b6c9460 2020-03-05 stsp }
2283 8069f636 2019-01-12 stsp goto done;
2284 8069f636 2019-01-12 stsp }
2285 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2286 4b6c9460 2020-03-05 stsp if (error) {
2287 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2288 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2289 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2290 4b6c9460 2020-03-05 stsp }
2291 45d344f6 2019-05-14 stsp goto done;
2292 4b6c9460 2020-03-05 stsp }
2293 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2294 8069f636 2019-01-12 stsp commit_id);
2295 8069f636 2019-01-12 stsp free(commit_id);
2296 8069f636 2019-01-12 stsp if (error)
2297 8069f636 2019-01-12 stsp goto done;
2298 8069f636 2019-01-12 stsp }
2299 8069f636 2019-01-12 stsp
2300 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2301 f2ea84fa 2019-07-27 stsp if (error)
2302 f2ea84fa 2019-07-27 stsp goto done;
2303 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2304 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2305 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2306 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2307 c09a553d 2018-03-12 stsp if (error != NULL)
2308 c09a553d 2018-03-12 stsp goto done;
2309 c09a553d 2018-03-12 stsp
2310 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2311 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2312 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2313 c09a553d 2018-03-12 stsp done:
2314 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2315 8069f636 2019-01-12 stsp free(commit_id_str);
2316 76089277 2018-04-01 stsp free(repo_path);
2317 507dc3bb 2018-12-29 stsp free(worktree_path);
2318 507dc3bb 2018-12-29 stsp return error;
2319 507dc3bb 2018-12-29 stsp }
2320 507dc3bb 2018-12-29 stsp
2321 507dc3bb 2018-12-29 stsp __dead static void
2322 507dc3bb 2018-12-29 stsp usage_update(void)
2323 507dc3bb 2018-12-29 stsp {
2324 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2325 507dc3bb 2018-12-29 stsp getprogname());
2326 507dc3bb 2018-12-29 stsp exit(1);
2327 507dc3bb 2018-12-29 stsp }
2328 507dc3bb 2018-12-29 stsp
2329 1ee397ad 2019-07-12 stsp static const struct got_error *
2330 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2331 507dc3bb 2018-12-29 stsp {
2332 784955db 2019-01-12 stsp int *did_something = arg;
2333 784955db 2019-01-12 stsp
2334 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2335 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2336 1ee397ad 2019-07-12 stsp return NULL;
2337 507dc3bb 2018-12-29 stsp
2338 1545c615 2019-02-10 stsp *did_something = 1;
2339 a484d721 2019-06-10 stsp
2340 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2341 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2342 1ee397ad 2019-07-12 stsp return NULL;
2343 a484d721 2019-06-10 stsp
2344 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2345 507dc3bb 2018-12-29 stsp path++;
2346 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2347 1ee397ad 2019-07-12 stsp return NULL;
2348 be7061eb 2018-12-30 stsp }
2349 be7061eb 2018-12-30 stsp
2350 be7061eb 2018-12-30 stsp static const struct got_error *
2351 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2352 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2353 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2354 a1fb16d8 2019-05-24 stsp {
2355 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2356 a1fb16d8 2019-05-24 stsp char *base_id_str;
2357 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2358 a1fb16d8 2019-05-24 stsp
2359 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2360 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2361 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2362 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2363 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2364 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2365 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2366 a1fb16d8 2019-05-24 stsp }
2367 a1fb16d8 2019-05-24 stsp
2368 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2369 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2370 a1fb16d8 2019-05-24 stsp if (err) {
2371 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2372 a1fb16d8 2019-05-24 stsp return err;
2373 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2374 a1fb16d8 2019-05-24 stsp }
2375 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2376 a1fb16d8 2019-05-24 stsp return NULL;
2377 a1fb16d8 2019-05-24 stsp
2378 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2379 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2380 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2381 a1fb16d8 2019-05-24 stsp if (err)
2382 a1fb16d8 2019-05-24 stsp return err;
2383 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2384 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2385 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2386 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2387 0ebf8283 2019-07-24 stsp return NULL;
2388 0ebf8283 2019-07-24 stsp }
2389 0ebf8283 2019-07-24 stsp
2390 0ebf8283 2019-07-24 stsp static const struct got_error *
2391 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2392 0ebf8283 2019-07-24 stsp {
2393 0ebf8283 2019-07-24 stsp const struct got_error *err;
2394 0ebf8283 2019-07-24 stsp int in_progress;
2395 0ebf8283 2019-07-24 stsp
2396 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2397 0ebf8283 2019-07-24 stsp if (err)
2398 0ebf8283 2019-07-24 stsp return err;
2399 0ebf8283 2019-07-24 stsp if (in_progress)
2400 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2401 0ebf8283 2019-07-24 stsp
2402 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2403 0ebf8283 2019-07-24 stsp if (err)
2404 0ebf8283 2019-07-24 stsp return err;
2405 0ebf8283 2019-07-24 stsp if (in_progress)
2406 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2407 0ebf8283 2019-07-24 stsp
2408 a1fb16d8 2019-05-24 stsp return NULL;
2409 a5edda0a 2019-07-27 stsp }
2410 a5edda0a 2019-07-27 stsp
2411 a5edda0a 2019-07-27 stsp static const struct got_error *
2412 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2413 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2414 a5edda0a 2019-07-27 stsp {
2415 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2416 a5edda0a 2019-07-27 stsp char *path;
2417 a5edda0a 2019-07-27 stsp int i;
2418 a5edda0a 2019-07-27 stsp
2419 a5edda0a 2019-07-27 stsp if (argc == 0) {
2420 a5edda0a 2019-07-27 stsp path = strdup("");
2421 a5edda0a 2019-07-27 stsp if (path == NULL)
2422 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2423 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2424 a5edda0a 2019-07-27 stsp }
2425 a5edda0a 2019-07-27 stsp
2426 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2427 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2428 a5edda0a 2019-07-27 stsp if (err)
2429 a5edda0a 2019-07-27 stsp break;
2430 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2431 a5edda0a 2019-07-27 stsp if (err) {
2432 a5edda0a 2019-07-27 stsp free(path);
2433 a5edda0a 2019-07-27 stsp break;
2434 a5edda0a 2019-07-27 stsp }
2435 a5edda0a 2019-07-27 stsp }
2436 a5edda0a 2019-07-27 stsp
2437 a5edda0a 2019-07-27 stsp return err;
2438 a1fb16d8 2019-05-24 stsp }
2439 a1fb16d8 2019-05-24 stsp
2440 a1fb16d8 2019-05-24 stsp static const struct got_error *
2441 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2442 507dc3bb 2018-12-29 stsp {
2443 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2444 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2445 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2446 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2447 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2448 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2449 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2450 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2451 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2452 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2453 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2454 507dc3bb 2018-12-29 stsp
2455 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2456 f2ea84fa 2019-07-27 stsp
2457 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2458 507dc3bb 2018-12-29 stsp switch (ch) {
2459 024e9686 2019-05-14 stsp case 'b':
2460 024e9686 2019-05-14 stsp branch_name = optarg;
2461 024e9686 2019-05-14 stsp break;
2462 507dc3bb 2018-12-29 stsp case 'c':
2463 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2464 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2465 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2466 507dc3bb 2018-12-29 stsp break;
2467 507dc3bb 2018-12-29 stsp default:
2468 2deda0b9 2019-03-07 stsp usage_update();
2469 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2470 507dc3bb 2018-12-29 stsp }
2471 507dc3bb 2018-12-29 stsp }
2472 507dc3bb 2018-12-29 stsp
2473 507dc3bb 2018-12-29 stsp argc -= optind;
2474 507dc3bb 2018-12-29 stsp argv += optind;
2475 507dc3bb 2018-12-29 stsp
2476 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2477 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2478 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2479 507dc3bb 2018-12-29 stsp err(1, "pledge");
2480 507dc3bb 2018-12-29 stsp #endif
2481 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2482 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2483 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2484 c4cdcb68 2019-04-03 stsp goto done;
2485 c4cdcb68 2019-04-03 stsp }
2486 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2487 7d5807f4 2019-07-11 stsp if (error)
2488 7d5807f4 2019-07-11 stsp goto done;
2489 7d5807f4 2019-07-11 stsp
2490 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2491 f2ea84fa 2019-07-27 stsp if (error)
2492 f2ea84fa 2019-07-27 stsp goto done;
2493 507dc3bb 2018-12-29 stsp
2494 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2495 c9956ddf 2019-09-08 stsp NULL);
2496 507dc3bb 2018-12-29 stsp if (error != NULL)
2497 507dc3bb 2018-12-29 stsp goto done;
2498 507dc3bb 2018-12-29 stsp
2499 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2500 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2501 087fb88c 2019-08-04 stsp if (error)
2502 087fb88c 2019-08-04 stsp goto done;
2503 087fb88c 2019-08-04 stsp
2504 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2505 0266afb7 2019-01-04 stsp if (error)
2506 0266afb7 2019-01-04 stsp goto done;
2507 0266afb7 2019-01-04 stsp
2508 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2509 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2510 024e9686 2019-05-14 stsp if (error != NULL)
2511 024e9686 2019-05-14 stsp goto done;
2512 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2513 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2514 9c4b8182 2019-01-02 stsp if (error != NULL)
2515 9c4b8182 2019-01-02 stsp goto done;
2516 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2517 507dc3bb 2018-12-29 stsp if (error != NULL)
2518 507dc3bb 2018-12-29 stsp goto done;
2519 507dc3bb 2018-12-29 stsp } else {
2520 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2521 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2522 04f57cb3 2019-07-25 stsp free(commit_id_str);
2523 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2524 30837e32 2019-07-25 stsp if (error)
2525 a297e751 2019-07-11 stsp goto done;
2526 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2527 a297e751 2019-07-11 stsp if (error)
2528 507dc3bb 2018-12-29 stsp goto done;
2529 507dc3bb 2018-12-29 stsp }
2530 35c965b2 2018-12-29 stsp
2531 a1fb16d8 2019-05-24 stsp if (branch_name) {
2532 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2533 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2534 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2535 f2ea84fa 2019-07-27 stsp continue;
2536 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2537 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2538 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2539 024e9686 2019-05-14 stsp goto done;
2540 024e9686 2019-05-14 stsp }
2541 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2542 024e9686 2019-05-14 stsp if (error)
2543 024e9686 2019-05-14 stsp goto done;
2544 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2545 3aef623b 2019-10-15 stsp repo);
2546 a367ff0f 2019-05-14 stsp free(head_commit_id);
2547 024e9686 2019-05-14 stsp if (error != NULL)
2548 024e9686 2019-05-14 stsp goto done;
2549 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2550 a367ff0f 2019-05-14 stsp if (error)
2551 a367ff0f 2019-05-14 stsp goto done;
2552 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2553 024e9686 2019-05-14 stsp if (error)
2554 024e9686 2019-05-14 stsp goto done;
2555 024e9686 2019-05-14 stsp } else {
2556 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2557 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2558 a367ff0f 2019-05-14 stsp if (error != NULL) {
2559 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2560 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2561 024e9686 2019-05-14 stsp goto done;
2562 a367ff0f 2019-05-14 stsp }
2563 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2564 a367ff0f 2019-05-14 stsp if (error)
2565 a367ff0f 2019-05-14 stsp goto done;
2566 024e9686 2019-05-14 stsp }
2567 507dc3bb 2018-12-29 stsp
2568 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2569 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2570 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2571 507dc3bb 2018-12-29 stsp commit_id);
2572 507dc3bb 2018-12-29 stsp if (error)
2573 507dc3bb 2018-12-29 stsp goto done;
2574 507dc3bb 2018-12-29 stsp }
2575 507dc3bb 2018-12-29 stsp
2576 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2577 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2578 507dc3bb 2018-12-29 stsp if (error != NULL)
2579 507dc3bb 2018-12-29 stsp goto done;
2580 9c4b8182 2019-01-02 stsp
2581 784955db 2019-01-12 stsp if (did_something)
2582 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2583 784955db 2019-01-12 stsp else
2584 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2585 507dc3bb 2018-12-29 stsp done:
2586 c09a553d 2018-03-12 stsp free(worktree_path);
2587 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2588 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2589 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2590 507dc3bb 2018-12-29 stsp free(commit_id);
2591 9c4b8182 2019-01-02 stsp free(commit_id_str);
2592 c09a553d 2018-03-12 stsp return error;
2593 c09a553d 2018-03-12 stsp }
2594 c09a553d 2018-03-12 stsp
2595 f42b1b34 2018-03-12 stsp static const struct got_error *
2596 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2597 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2598 63035f9f 2019-10-06 stsp struct got_repository *repo)
2599 5c860e29 2018-03-12 stsp {
2600 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2601 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2602 79109fed 2018-03-27 stsp
2603 44392932 2019-08-25 stsp if (blob_id1) {
2604 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2605 44392932 2019-08-25 stsp if (err)
2606 44392932 2019-08-25 stsp goto done;
2607 44392932 2019-08-25 stsp }
2608 79109fed 2018-03-27 stsp
2609 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2610 44392932 2019-08-25 stsp if (err)
2611 44392932 2019-08-25 stsp goto done;
2612 79109fed 2018-03-27 stsp
2613 44392932 2019-08-25 stsp while (path[0] == '/')
2614 44392932 2019-08-25 stsp path++;
2615 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2616 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2617 44392932 2019-08-25 stsp done:
2618 44392932 2019-08-25 stsp if (blob1)
2619 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2620 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2621 44392932 2019-08-25 stsp return err;
2622 44392932 2019-08-25 stsp }
2623 44392932 2019-08-25 stsp
2624 44392932 2019-08-25 stsp static const struct got_error *
2625 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2626 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2627 63035f9f 2019-10-06 stsp struct got_repository *repo)
2628 44392932 2019-08-25 stsp {
2629 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2630 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2631 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2632 44392932 2019-08-25 stsp
2633 44392932 2019-08-25 stsp if (tree_id1) {
2634 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2635 79109fed 2018-03-27 stsp if (err)
2636 44392932 2019-08-25 stsp goto done;
2637 79109fed 2018-03-27 stsp }
2638 79109fed 2018-03-27 stsp
2639 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2640 0f2b3dca 2018-12-22 stsp if (err)
2641 0f2b3dca 2018-12-22 stsp goto done;
2642 0f2b3dca 2018-12-22 stsp
2643 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2644 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2645 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2646 44392932 2019-08-25 stsp while (path[0] == '/')
2647 44392932 2019-08-25 stsp path++;
2648 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2649 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2650 0f2b3dca 2018-12-22 stsp done:
2651 79109fed 2018-03-27 stsp if (tree1)
2652 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2653 366e0a5f 2019-10-10 stsp if (tree2)
2654 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2655 44392932 2019-08-25 stsp return err;
2656 44392932 2019-08-25 stsp }
2657 44392932 2019-08-25 stsp
2658 44392932 2019-08-25 stsp static const struct got_error *
2659 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2660 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2661 44392932 2019-08-25 stsp {
2662 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2663 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2664 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2665 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2666 44392932 2019-08-25 stsp struct got_object_qid *qid;
2667 44392932 2019-08-25 stsp
2668 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2669 44392932 2019-08-25 stsp if (qid != NULL) {
2670 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2671 44392932 2019-08-25 stsp qid->id);
2672 44392932 2019-08-25 stsp if (err)
2673 44392932 2019-08-25 stsp return err;
2674 44392932 2019-08-25 stsp }
2675 44392932 2019-08-25 stsp
2676 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2677 44392932 2019-08-25 stsp int obj_type;
2678 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2679 44392932 2019-08-25 stsp if (err)
2680 44392932 2019-08-25 stsp goto done;
2681 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2682 44392932 2019-08-25 stsp if (err) {
2683 44392932 2019-08-25 stsp free(obj_id2);
2684 44392932 2019-08-25 stsp goto done;
2685 44392932 2019-08-25 stsp }
2686 44392932 2019-08-25 stsp if (pcommit) {
2687 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2688 44392932 2019-08-25 stsp qid->id, path);
2689 44392932 2019-08-25 stsp if (err) {
2690 44392932 2019-08-25 stsp free(obj_id2);
2691 44392932 2019-08-25 stsp goto done;
2692 44392932 2019-08-25 stsp }
2693 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2694 44392932 2019-08-25 stsp if (err) {
2695 44392932 2019-08-25 stsp free(obj_id2);
2696 44392932 2019-08-25 stsp goto done;
2697 44392932 2019-08-25 stsp }
2698 44392932 2019-08-25 stsp }
2699 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2700 44392932 2019-08-25 stsp if (err) {
2701 44392932 2019-08-25 stsp free(obj_id2);
2702 44392932 2019-08-25 stsp goto done;
2703 44392932 2019-08-25 stsp }
2704 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2705 44392932 2019-08-25 stsp switch (obj_type) {
2706 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2707 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2708 63035f9f 2019-10-06 stsp 0, repo);
2709 44392932 2019-08-25 stsp break;
2710 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2711 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2712 63035f9f 2019-10-06 stsp 0, repo);
2713 44392932 2019-08-25 stsp break;
2714 44392932 2019-08-25 stsp default:
2715 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2716 44392932 2019-08-25 stsp break;
2717 44392932 2019-08-25 stsp }
2718 44392932 2019-08-25 stsp free(obj_id1);
2719 44392932 2019-08-25 stsp free(obj_id2);
2720 44392932 2019-08-25 stsp } else {
2721 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2722 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2723 44392932 2019-08-25 stsp if (err)
2724 44392932 2019-08-25 stsp goto done;
2725 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2726 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2727 44392932 2019-08-25 stsp if (err)
2728 44392932 2019-08-25 stsp goto done;
2729 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2730 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2731 44392932 2019-08-25 stsp }
2732 44392932 2019-08-25 stsp done:
2733 0f2b3dca 2018-12-22 stsp free(id_str1);
2734 0f2b3dca 2018-12-22 stsp free(id_str2);
2735 44392932 2019-08-25 stsp if (pcommit)
2736 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2737 79109fed 2018-03-27 stsp return err;
2738 79109fed 2018-03-27 stsp }
2739 79109fed 2018-03-27 stsp
2740 4bb494d5 2018-06-16 stsp static char *
2741 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2742 6c281f94 2018-06-11 stsp {
2743 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2744 09867e48 2019-08-13 stsp char *p, *s;
2745 09867e48 2019-08-13 stsp
2746 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2747 09867e48 2019-08-13 stsp if (tm == NULL)
2748 09867e48 2019-08-13 stsp return NULL;
2749 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2750 09867e48 2019-08-13 stsp if (s == NULL)
2751 09867e48 2019-08-13 stsp return NULL;
2752 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2753 6c281f94 2018-06-11 stsp if (p)
2754 6c281f94 2018-06-11 stsp *p = '\0';
2755 6c281f94 2018-06-11 stsp return s;
2756 6c281f94 2018-06-11 stsp }
2757 dc424a06 2019-08-07 stsp
2758 6841bf13 2019-11-29 kn static const struct got_error *
2759 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2760 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2761 6841bf13 2019-11-29 kn {
2762 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2763 6841bf13 2019-11-29 kn regmatch_t regmatch;
2764 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2765 6841bf13 2019-11-29 kn
2766 6841bf13 2019-11-29 kn *have_match = 0;
2767 6841bf13 2019-11-29 kn
2768 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2769 6841bf13 2019-11-29 kn if (err)
2770 6841bf13 2019-11-29 kn return err;
2771 6841bf13 2019-11-29 kn
2772 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2773 6841bf13 2019-11-29 kn if (err)
2774 6841bf13 2019-11-29 kn goto done;
2775 6841bf13 2019-11-29 kn
2776 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2777 6841bf13 2019-11-29 kn *have_match = 1;
2778 6841bf13 2019-11-29 kn done:
2779 6841bf13 2019-11-29 kn free(id_str);
2780 6841bf13 2019-11-29 kn free(logmsg);
2781 6841bf13 2019-11-29 kn return err;
2782 6841bf13 2019-11-29 kn }
2783 6841bf13 2019-11-29 kn
2784 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2785 6c281f94 2018-06-11 stsp
2786 79109fed 2018-03-27 stsp static const struct got_error *
2787 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2788 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2789 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2790 79109fed 2018-03-27 stsp {
2791 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2792 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2793 6c281f94 2018-06-11 stsp char datebuf[26];
2794 45d799e2 2018-12-23 stsp time_t committer_time;
2795 45d799e2 2018-12-23 stsp const char *author, *committer;
2796 199a4027 2019-02-02 stsp char *refs_str = NULL;
2797 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2798 5c860e29 2018-03-12 stsp
2799 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2800 199a4027 2019-02-02 stsp char *s;
2801 199a4027 2019-02-02 stsp const char *name;
2802 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2803 a436ad14 2019-08-13 stsp int cmp;
2804 a436ad14 2019-08-13 stsp
2805 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2806 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2807 d9498b20 2019-02-05 stsp continue;
2808 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2809 199a4027 2019-02-02 stsp name += 5;
2810 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2811 7143d404 2019-03-12 stsp continue;
2812 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2813 e34f9ed6 2019-02-02 stsp name += 6;
2814 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2815 141c2bff 2019-02-04 stsp name += 8;
2816 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2817 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2818 5d844a1e 2019-08-13 stsp if (err) {
2819 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2820 5d844a1e 2019-08-13 stsp return err;
2821 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2822 5d844a1e 2019-08-13 stsp err = NULL;
2823 5d844a1e 2019-08-13 stsp tag = NULL;
2824 5d844a1e 2019-08-13 stsp }
2825 a436ad14 2019-08-13 stsp }
2826 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2827 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2828 a436ad14 2019-08-13 stsp if (tag)
2829 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2830 a436ad14 2019-08-13 stsp if (cmp != 0)
2831 a436ad14 2019-08-13 stsp continue;
2832 199a4027 2019-02-02 stsp s = refs_str;
2833 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2834 199a4027 2019-02-02 stsp name) == -1) {
2835 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2836 199a4027 2019-02-02 stsp free(s);
2837 34ca4898 2019-08-13 stsp return err;
2838 199a4027 2019-02-02 stsp }
2839 199a4027 2019-02-02 stsp free(s);
2840 199a4027 2019-02-02 stsp }
2841 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2842 8bf5b3c9 2018-03-17 stsp if (err)
2843 8bf5b3c9 2018-03-17 stsp return err;
2844 788c352e 2018-06-16 stsp
2845 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2846 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2847 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2848 832c249c 2018-06-10 stsp free(id_str);
2849 d3d493d7 2019-02-21 stsp id_str = NULL;
2850 d3d493d7 2019-02-21 stsp free(refs_str);
2851 d3d493d7 2019-02-21 stsp refs_str = NULL;
2852 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2853 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2854 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2855 09867e48 2019-08-13 stsp if (datestr)
2856 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2857 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2858 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2859 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2860 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2861 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2862 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2863 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2864 3fe1abad 2018-06-10 stsp int n = 1;
2865 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2866 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2867 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2868 a0603db2 2018-06-10 stsp if (err)
2869 a0603db2 2018-06-10 stsp return err;
2870 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2871 a0603db2 2018-06-10 stsp free(id_str);
2872 a0603db2 2018-06-10 stsp }
2873 a0603db2 2018-06-10 stsp }
2874 832c249c 2018-06-10 stsp
2875 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2876 5943eee2 2019-08-13 stsp if (err)
2877 5943eee2 2019-08-13 stsp return err;
2878 8bf5b3c9 2018-03-17 stsp
2879 621015ac 2018-07-23 stsp logmsg = logmsg0;
2880 832c249c 2018-06-10 stsp do {
2881 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2882 832c249c 2018-06-10 stsp if (line)
2883 832c249c 2018-06-10 stsp printf(" %s\n", line);
2884 832c249c 2018-06-10 stsp } while (line);
2885 621015ac 2018-07-23 stsp free(logmsg0);
2886 832c249c 2018-06-10 stsp
2887 971751ac 2018-03-27 stsp if (show_patch) {
2888 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2889 971751ac 2018-03-27 stsp if (err == 0)
2890 971751ac 2018-03-27 stsp printf("\n");
2891 971751ac 2018-03-27 stsp }
2892 07862c20 2018-09-15 stsp
2893 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2894 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2895 07862c20 2018-09-15 stsp return err;
2896 f42b1b34 2018-03-12 stsp }
2897 5c860e29 2018-03-12 stsp
2898 f42b1b34 2018-03-12 stsp static const struct got_error *
2899 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2900 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2901 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2902 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2903 f42b1b34 2018-03-12 stsp {
2904 f42b1b34 2018-03-12 stsp const struct got_error *err;
2905 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2906 6841bf13 2019-11-29 kn regex_t regex;
2907 6841bf13 2019-11-29 kn int have_match;
2908 372ccdbb 2018-06-10 stsp
2909 6841bf13 2019-11-29 kn if (search_pattern &&
2910 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2911 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2912 6841bf13 2019-11-29 kn
2913 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2914 f42b1b34 2018-03-12 stsp if (err)
2915 f42b1b34 2018-03-12 stsp return err;
2916 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2917 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2918 372ccdbb 2018-06-10 stsp if (err)
2919 fcc85cad 2018-07-22 stsp goto done;
2920 656b1f76 2019-05-11 jcs for (;;) {
2921 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2922 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2923 8bf5b3c9 2018-03-17 stsp
2924 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2925 84453469 2018-11-11 stsp break;
2926 84453469 2018-11-11 stsp
2927 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2928 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2929 372ccdbb 2018-06-10 stsp if (err) {
2930 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2931 9ba79e04 2018-06-11 stsp err = NULL;
2932 ee780d5c 2020-01-04 stsp break;
2933 7e665116 2018-04-02 stsp }
2934 b43fbaa0 2018-06-11 stsp if (id == NULL)
2935 7e665116 2018-04-02 stsp break;
2936 b43fbaa0 2018-06-11 stsp
2937 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2938 b43fbaa0 2018-06-11 stsp if (err)
2939 fcc85cad 2018-07-22 stsp break;
2940 6841bf13 2019-11-29 kn
2941 6841bf13 2019-11-29 kn if (search_pattern) {
2942 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2943 6841bf13 2019-11-29 kn if (err) {
2944 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2945 6841bf13 2019-11-29 kn break;
2946 6841bf13 2019-11-29 kn }
2947 6841bf13 2019-11-29 kn if (have_match == 0) {
2948 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2949 6841bf13 2019-11-29 kn continue;
2950 6841bf13 2019-11-29 kn }
2951 6841bf13 2019-11-29 kn }
2952 6841bf13 2019-11-29 kn
2953 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2954 44392932 2019-08-25 stsp diff_context, refs);
2955 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2956 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2957 7e665116 2018-04-02 stsp break;
2958 31cedeaf 2018-09-15 stsp }
2959 fcc85cad 2018-07-22 stsp done:
2960 6841bf13 2019-11-29 kn if (search_pattern)
2961 6841bf13 2019-11-29 kn regfree(&regex);
2962 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2963 f42b1b34 2018-03-12 stsp return err;
2964 f42b1b34 2018-03-12 stsp }
2965 5c860e29 2018-03-12 stsp
2966 4ed7e80c 2018-05-20 stsp __dead static void
2967 6f3d1eb0 2018-03-12 stsp usage_log(void)
2968 6f3d1eb0 2018-03-12 stsp {
2969 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2970 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2971 6f3d1eb0 2018-03-12 stsp exit(1);
2972 b1ebc001 2019-08-13 stsp }
2973 b1ebc001 2019-08-13 stsp
2974 b1ebc001 2019-08-13 stsp static int
2975 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2976 b1ebc001 2019-08-13 stsp {
2977 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2978 b1ebc001 2019-08-13 stsp long long n;
2979 b1ebc001 2019-08-13 stsp const char *errstr;
2980 b1ebc001 2019-08-13 stsp
2981 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2982 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2983 b1ebc001 2019-08-13 stsp return 0;
2984 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2985 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2986 b1ebc001 2019-08-13 stsp return 0;
2987 b1ebc001 2019-08-13 stsp return n;
2988 6f3d1eb0 2018-03-12 stsp }
2989 6f3d1eb0 2018-03-12 stsp
2990 4ed7e80c 2018-05-20 stsp static const struct got_error *
2991 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2992 f42b1b34 2018-03-12 stsp {
2993 f42b1b34 2018-03-12 stsp const struct got_error *error;
2994 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2995 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2996 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2997 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2998 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2999 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
3000 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3001 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
3002 64a96a6d 2018-04-01 stsp const char *errstr;
3003 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3004 1b3893a2 2019-03-18 stsp
3005 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3006 5c860e29 2018-03-12 stsp
3007 6715a751 2018-03-16 stsp #ifndef PROFILE
3008 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3009 6098196c 2019-01-04 stsp NULL)
3010 ad242220 2018-09-08 stsp == -1)
3011 f42b1b34 2018-03-12 stsp err(1, "pledge");
3012 6715a751 2018-03-16 stsp #endif
3013 79109fed 2018-03-27 stsp
3014 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3015 b1ebc001 2019-08-13 stsp
3016 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
3017 79109fed 2018-03-27 stsp switch (ch) {
3018 79109fed 2018-03-27 stsp case 'p':
3019 79109fed 2018-03-27 stsp show_patch = 1;
3020 d142fc45 2018-04-01 stsp break;
3021 d142fc45 2018-04-01 stsp case 'c':
3022 d142fc45 2018-04-01 stsp start_commit = optarg;
3023 64a96a6d 2018-04-01 stsp break;
3024 c0cc5c62 2018-10-18 stsp case 'C':
3025 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3026 4a8520aa 2018-10-18 stsp &errstr);
3027 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3028 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3029 c0cc5c62 2018-10-18 stsp break;
3030 64a96a6d 2018-04-01 stsp case 'l':
3031 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3032 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3033 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3034 cc54c501 2019-07-15 stsp break;
3035 48c8c60d 2020-01-27 stsp case 'b':
3036 48c8c60d 2020-01-27 stsp log_branches = 1;
3037 a0603db2 2018-06-10 stsp break;
3038 04ca23f4 2018-07-16 stsp case 'r':
3039 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3040 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3041 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3042 9ba1d308 2019-10-21 stsp optarg);
3043 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3044 04ca23f4 2018-07-16 stsp break;
3045 6841bf13 2019-11-29 kn case 's':
3046 6841bf13 2019-11-29 kn search_pattern = optarg;
3047 6841bf13 2019-11-29 kn break;
3048 79109fed 2018-03-27 stsp default:
3049 2deda0b9 2019-03-07 stsp usage_log();
3050 79109fed 2018-03-27 stsp /* NOTREACHED */
3051 79109fed 2018-03-27 stsp }
3052 79109fed 2018-03-27 stsp }
3053 79109fed 2018-03-27 stsp
3054 79109fed 2018-03-27 stsp argc -= optind;
3055 79109fed 2018-03-27 stsp argv += optind;
3056 f42b1b34 2018-03-12 stsp
3057 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3058 dc1edbfa 2019-11-29 kn diff_context = 3;
3059 dc1edbfa 2019-11-29 kn else if (!show_patch)
3060 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3061 dc1edbfa 2019-11-29 kn
3062 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3063 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3064 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3065 04ca23f4 2018-07-16 stsp goto done;
3066 04ca23f4 2018-07-16 stsp }
3067 cffc0aa4 2019-02-05 stsp
3068 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3069 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3070 cffc0aa4 2019-02-05 stsp goto done;
3071 cffc0aa4 2019-02-05 stsp error = NULL;
3072 cffc0aa4 2019-02-05 stsp
3073 e7301579 2019-03-18 stsp if (argc == 0) {
3074 cbd1af7a 2019-03-18 stsp path = strdup("");
3075 e7301579 2019-03-18 stsp if (path == NULL) {
3076 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3077 cbd1af7a 2019-03-18 stsp goto done;
3078 e7301579 2019-03-18 stsp }
3079 e7301579 2019-03-18 stsp } else if (argc == 1) {
3080 e7301579 2019-03-18 stsp if (worktree) {
3081 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3082 e7301579 2019-03-18 stsp argv[0]);
3083 e7301579 2019-03-18 stsp if (error)
3084 e7301579 2019-03-18 stsp goto done;
3085 e7301579 2019-03-18 stsp } else {
3086 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3087 e7301579 2019-03-18 stsp if (path == NULL) {
3088 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3089 e7301579 2019-03-18 stsp goto done;
3090 e7301579 2019-03-18 stsp }
3091 e7301579 2019-03-18 stsp }
3092 cbd1af7a 2019-03-18 stsp } else
3093 cbd1af7a 2019-03-18 stsp usage_log();
3094 cbd1af7a 2019-03-18 stsp
3095 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3096 5486daa2 2019-05-11 stsp repo_path = worktree ?
3097 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3098 5486daa2 2019-05-11 stsp }
3099 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3100 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3101 cffc0aa4 2019-02-05 stsp goto done;
3102 04ca23f4 2018-07-16 stsp }
3103 6098196c 2019-01-04 stsp
3104 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3105 d7d4f210 2018-03-12 stsp if (error != NULL)
3106 04ca23f4 2018-07-16 stsp goto done;
3107 f42b1b34 2018-03-12 stsp
3108 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3109 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3110 c02c541e 2019-03-29 stsp if (error)
3111 c02c541e 2019-03-29 stsp goto done;
3112 c02c541e 2019-03-29 stsp
3113 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3114 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3115 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3116 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3117 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3118 3235492e 2018-04-01 stsp if (error != NULL)
3119 3235492e 2018-04-01 stsp return error;
3120 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
3121 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3122 3235492e 2018-04-01 stsp if (error != NULL)
3123 3235492e 2018-04-01 stsp return error;
3124 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3125 3235492e 2018-04-01 stsp } else {
3126 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
3127 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3128 3235492e 2018-04-01 stsp if (error == NULL) {
3129 1caad61b 2019-02-01 stsp int obj_type;
3130 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
3131 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
3132 d1f2edc9 2018-06-13 stsp if (error != NULL)
3133 1caad61b 2019-02-01 stsp goto done;
3134 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
3135 1caad61b 2019-02-01 stsp if (error != NULL)
3136 1caad61b 2019-02-01 stsp goto done;
3137 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3138 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
3139 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
3140 1caad61b 2019-02-01 stsp if (error != NULL)
3141 1caad61b 2019-02-01 stsp goto done;
3142 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
3143 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
3144 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3145 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3146 1caad61b 2019-02-01 stsp goto done;
3147 1caad61b 2019-02-01 stsp }
3148 1caad61b 2019-02-01 stsp free(id);
3149 1caad61b 2019-02-01 stsp id = got_object_id_dup(
3150 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
3151 1caad61b 2019-02-01 stsp if (id == NULL)
3152 638f9024 2019-05-13 stsp error = got_error_from_errno(
3153 230a42bd 2019-05-11 jcs "got_object_id_dup");
3154 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3155 1caad61b 2019-02-01 stsp if (error)
3156 1caad61b 2019-02-01 stsp goto done;
3157 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3158 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3159 1caad61b 2019-02-01 stsp goto done;
3160 1caad61b 2019-02-01 stsp }
3161 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3162 d1f2edc9 2018-06-13 stsp if (error != NULL)
3163 1caad61b 2019-02-01 stsp goto done;
3164 d1f2edc9 2018-06-13 stsp }
3165 15a94983 2018-12-23 stsp if (commit == NULL) {
3166 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
3167 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3168 d1f2edc9 2018-06-13 stsp if (error != NULL)
3169 d1f2edc9 2018-06-13 stsp return error;
3170 3235492e 2018-04-01 stsp }
3171 3235492e 2018-04-01 stsp }
3172 d7d4f210 2018-03-12 stsp if (error != NULL)
3173 04ca23f4 2018-07-16 stsp goto done;
3174 04ca23f4 2018-07-16 stsp
3175 deeabeae 2019-08-27 stsp if (worktree) {
3176 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3177 deeabeae 2019-08-27 stsp char *p;
3178 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3179 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3180 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3181 deeabeae 2019-08-27 stsp goto done;
3182 deeabeae 2019-08-27 stsp }
3183 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3184 deeabeae 2019-08-27 stsp free(p);
3185 deeabeae 2019-08-27 stsp } else
3186 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3187 04ca23f4 2018-07-16 stsp if (error != NULL)
3188 04ca23f4 2018-07-16 stsp goto done;
3189 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3190 04ca23f4 2018-07-16 stsp free(path);
3191 04ca23f4 2018-07-16 stsp path = in_repo_path;
3192 04ca23f4 2018-07-16 stsp }
3193 199a4027 2019-02-02 stsp
3194 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3195 199a4027 2019-02-02 stsp if (error)
3196 199a4027 2019-02-02 stsp goto done;
3197 04ca23f4 2018-07-16 stsp
3198 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3199 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3200 04ca23f4 2018-07-16 stsp done:
3201 04ca23f4 2018-07-16 stsp free(path);
3202 04ca23f4 2018-07-16 stsp free(repo_path);
3203 04ca23f4 2018-07-16 stsp free(cwd);
3204 f42b1b34 2018-03-12 stsp free(id);
3205 cffc0aa4 2019-02-05 stsp if (worktree)
3206 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3207 ad242220 2018-09-08 stsp if (repo) {
3208 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3209 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3210 ad242220 2018-09-08 stsp if (error == NULL)
3211 ad242220 2018-09-08 stsp error = repo_error;
3212 ad242220 2018-09-08 stsp }
3213 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3214 8bf5b3c9 2018-03-17 stsp return error;
3215 5c860e29 2018-03-12 stsp }
3216 5c860e29 2018-03-12 stsp
3217 4ed7e80c 2018-05-20 stsp __dead static void
3218 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3219 3f8b7d6a 2018-04-01 stsp {
3220 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3221 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3222 3f8b7d6a 2018-04-01 stsp exit(1);
3223 b00d56cd 2018-04-01 stsp }
3224 b00d56cd 2018-04-01 stsp
3225 b72f483a 2019-02-05 stsp struct print_diff_arg {
3226 b72f483a 2019-02-05 stsp struct got_repository *repo;
3227 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3228 b72f483a 2019-02-05 stsp int diff_context;
3229 3fc0c068 2019-02-10 stsp const char *id_str;
3230 3fc0c068 2019-02-10 stsp int header_shown;
3231 98eaaa12 2019-08-03 stsp int diff_staged;
3232 63035f9f 2019-10-06 stsp int ignore_whitespace;
3233 b72f483a 2019-02-05 stsp };
3234 b72f483a 2019-02-05 stsp
3235 4ed7e80c 2018-05-20 stsp static const struct got_error *
3236 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3237 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3238 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3239 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3240 b72f483a 2019-02-05 stsp {
3241 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3242 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3243 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3244 12463d8b 2019-12-13 stsp int fd = -1;
3245 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3246 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3247 b72f483a 2019-02-05 stsp struct stat sb;
3248 b72f483a 2019-02-05 stsp
3249 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3250 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3251 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3252 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3253 98eaaa12 2019-08-03 stsp return NULL;
3254 98eaaa12 2019-08-03 stsp } else {
3255 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3256 98eaaa12 2019-08-03 stsp return NULL;
3257 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3258 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3259 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3260 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3261 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3262 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3263 98eaaa12 2019-08-03 stsp return NULL;
3264 98eaaa12 2019-08-03 stsp }
3265 3fc0c068 2019-02-10 stsp
3266 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3267 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3268 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3269 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3270 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3271 3fc0c068 2019-02-10 stsp }
3272 b72f483a 2019-02-05 stsp
3273 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3274 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3275 98eaaa12 2019-08-03 stsp switch (staged_status) {
3276 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3277 98eaaa12 2019-08-03 stsp label1 = path;
3278 98eaaa12 2019-08-03 stsp label2 = path;
3279 98eaaa12 2019-08-03 stsp break;
3280 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3281 98eaaa12 2019-08-03 stsp label2 = path;
3282 98eaaa12 2019-08-03 stsp break;
3283 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3284 98eaaa12 2019-08-03 stsp label1 = path;
3285 98eaaa12 2019-08-03 stsp break;
3286 98eaaa12 2019-08-03 stsp default:
3287 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3288 98eaaa12 2019-08-03 stsp }
3289 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3290 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3291 63035f9f 2019-10-06 stsp a->repo, stdout);
3292 98eaaa12 2019-08-03 stsp }
3293 98eaaa12 2019-08-03 stsp
3294 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3295 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3296 4ce46740 2019-08-08 stsp char *id_str;
3297 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3298 408b4ebc 2019-08-03 stsp 8192);
3299 4ce46740 2019-08-08 stsp if (err)
3300 4ce46740 2019-08-08 stsp goto done;
3301 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3302 4ce46740 2019-08-08 stsp if (err)
3303 4ce46740 2019-08-08 stsp goto done;
3304 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3305 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3306 4ce46740 2019-08-08 stsp free(id_str);
3307 4ce46740 2019-08-08 stsp goto done;
3308 4ce46740 2019-08-08 stsp }
3309 4ce46740 2019-08-08 stsp free(id_str);
3310 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3311 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3312 4ce46740 2019-08-08 stsp if (err)
3313 4ce46740 2019-08-08 stsp goto done;
3314 4ce46740 2019-08-08 stsp }
3315 d00136be 2019-03-26 stsp
3316 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3317 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3318 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3319 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3320 2ec1f75b 2019-03-26 stsp goto done;
3321 2ec1f75b 2019-03-26 stsp }
3322 b72f483a 2019-02-05 stsp
3323 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3324 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3325 12463d8b 2019-12-13 stsp if (fd == -1) {
3326 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3327 12463d8b 2019-12-13 stsp goto done;
3328 12463d8b 2019-12-13 stsp }
3329 12463d8b 2019-12-13 stsp } else {
3330 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3331 12463d8b 2019-12-13 stsp if (fd == -1) {
3332 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3333 12463d8b 2019-12-13 stsp goto done;
3334 12463d8b 2019-12-13 stsp }
3335 12463d8b 2019-12-13 stsp }
3336 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3337 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3338 2ec1f75b 2019-03-26 stsp goto done;
3339 2ec1f75b 2019-03-26 stsp }
3340 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3341 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3342 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3343 2ec1f75b 2019-03-26 stsp goto done;
3344 2ec1f75b 2019-03-26 stsp }
3345 12463d8b 2019-12-13 stsp fd = -1;
3346 2ec1f75b 2019-03-26 stsp } else
3347 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3348 b72f483a 2019-02-05 stsp
3349 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3350 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3351 b72f483a 2019-02-05 stsp done:
3352 b72f483a 2019-02-05 stsp if (blob1)
3353 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3354 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3355 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3356 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3357 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3358 b72f483a 2019-02-05 stsp free(abspath);
3359 927df6b7 2019-02-10 stsp return err;
3360 927df6b7 2019-02-10 stsp }
3361 927df6b7 2019-02-10 stsp
3362 927df6b7 2019-02-10 stsp static const struct got_error *
3363 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3364 b00d56cd 2018-04-01 stsp {
3365 b00d56cd 2018-04-01 stsp const struct got_error *error;
3366 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3367 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3368 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3369 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3370 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3371 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3372 15a94983 2018-12-23 stsp int type1, type2;
3373 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3374 c0cc5c62 2018-10-18 stsp const char *errstr;
3375 927df6b7 2019-02-10 stsp char *path = NULL;
3376 b00d56cd 2018-04-01 stsp
3377 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3378 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3379 25eccc22 2019-01-04 stsp NULL) == -1)
3380 b00d56cd 2018-04-01 stsp err(1, "pledge");
3381 b00d56cd 2018-04-01 stsp #endif
3382 b00d56cd 2018-04-01 stsp
3383 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3384 b00d56cd 2018-04-01 stsp switch (ch) {
3385 c0cc5c62 2018-10-18 stsp case 'C':
3386 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3387 dfcab68b 2019-11-29 kn &errstr);
3388 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3389 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3390 c0cc5c62 2018-10-18 stsp break;
3391 b72f483a 2019-02-05 stsp case 'r':
3392 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3393 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3394 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3395 9ba1d308 2019-10-21 stsp optarg);
3396 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3397 b72f483a 2019-02-05 stsp break;
3398 98eaaa12 2019-08-03 stsp case 's':
3399 98eaaa12 2019-08-03 stsp diff_staged = 1;
3400 63035f9f 2019-10-06 stsp break;
3401 63035f9f 2019-10-06 stsp case 'w':
3402 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3403 98eaaa12 2019-08-03 stsp break;
3404 b00d56cd 2018-04-01 stsp default:
3405 2deda0b9 2019-03-07 stsp usage_diff();
3406 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3407 b00d56cd 2018-04-01 stsp }
3408 b00d56cd 2018-04-01 stsp }
3409 b00d56cd 2018-04-01 stsp
3410 b00d56cd 2018-04-01 stsp argc -= optind;
3411 b00d56cd 2018-04-01 stsp argv += optind;
3412 b00d56cd 2018-04-01 stsp
3413 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3414 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3415 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3416 b72f483a 2019-02-05 stsp goto done;
3417 b72f483a 2019-02-05 stsp }
3418 927df6b7 2019-02-10 stsp if (argc <= 1) {
3419 b72f483a 2019-02-05 stsp if (repo_path)
3420 b72f483a 2019-02-05 stsp errx(1,
3421 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3422 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3423 1f03b8da 2020-03-20 stsp if (error)
3424 1f03b8da 2020-03-20 stsp goto done;
3425 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3426 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3427 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3428 927df6b7 2019-02-10 stsp goto done;
3429 927df6b7 2019-02-10 stsp }
3430 927df6b7 2019-02-10 stsp if (argc == 1) {
3431 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3432 cbd1af7a 2019-03-18 stsp argv[0]);
3433 927df6b7 2019-02-10 stsp if (error)
3434 927df6b7 2019-02-10 stsp goto done;
3435 927df6b7 2019-02-10 stsp } else {
3436 927df6b7 2019-02-10 stsp path = strdup("");
3437 927df6b7 2019-02-10 stsp if (path == NULL) {
3438 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3439 927df6b7 2019-02-10 stsp goto done;
3440 927df6b7 2019-02-10 stsp }
3441 927df6b7 2019-02-10 stsp }
3442 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3443 98eaaa12 2019-08-03 stsp if (diff_staged)
3444 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3445 98eaaa12 2019-08-03 stsp "objects in repository");
3446 15a94983 2018-12-23 stsp id_str1 = argv[0];
3447 15a94983 2018-12-23 stsp id_str2 = argv[1];
3448 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3449 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3450 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3451 30db809c 2019-06-05 stsp goto done;
3452 1f03b8da 2020-03-20 stsp if (worktree) {
3453 1f03b8da 2020-03-20 stsp repo_path = strdup(
3454 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3455 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3456 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3457 1f03b8da 2020-03-20 stsp goto done;
3458 1f03b8da 2020-03-20 stsp }
3459 1f03b8da 2020-03-20 stsp } else {
3460 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3461 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3462 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3463 1f03b8da 2020-03-20 stsp goto done;
3464 1f03b8da 2020-03-20 stsp }
3465 30db809c 2019-06-05 stsp }
3466 30db809c 2019-06-05 stsp }
3467 b00d56cd 2018-04-01 stsp } else
3468 b00d56cd 2018-04-01 stsp usage_diff();
3469 b00d56cd 2018-04-01 stsp
3470 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3471 76089277 2018-04-01 stsp free(repo_path);
3472 b00d56cd 2018-04-01 stsp if (error != NULL)
3473 b00d56cd 2018-04-01 stsp goto done;
3474 b00d56cd 2018-04-01 stsp
3475 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3476 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3477 c02c541e 2019-03-29 stsp if (error)
3478 c02c541e 2019-03-29 stsp goto done;
3479 c02c541e 2019-03-29 stsp
3480 30db809c 2019-06-05 stsp if (argc <= 1) {
3481 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3482 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3483 b72f483a 2019-02-05 stsp char *id_str;
3484 72ea6654 2019-07-27 stsp
3485 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3486 72ea6654 2019-07-27 stsp
3487 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3488 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3489 b72f483a 2019-02-05 stsp if (error)
3490 b72f483a 2019-02-05 stsp goto done;
3491 b72f483a 2019-02-05 stsp arg.repo = repo;
3492 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3493 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3494 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3495 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3496 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3497 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3498 b72f483a 2019-02-05 stsp
3499 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3500 72ea6654 2019-07-27 stsp if (error)
3501 72ea6654 2019-07-27 stsp goto done;
3502 72ea6654 2019-07-27 stsp
3503 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3504 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3505 b72f483a 2019-02-05 stsp free(id_str);
3506 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3507 b72f483a 2019-02-05 stsp goto done;
3508 b72f483a 2019-02-05 stsp }
3509 b72f483a 2019-02-05 stsp
3510 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3511 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3512 0adb7196 2019-08-11 stsp if (error)
3513 0adb7196 2019-08-11 stsp goto done;
3514 b00d56cd 2018-04-01 stsp
3515 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3516 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3517 0adb7196 2019-08-11 stsp if (error)
3518 0adb7196 2019-08-11 stsp goto done;
3519 b00d56cd 2018-04-01 stsp
3520 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3521 15a94983 2018-12-23 stsp if (error)
3522 15a94983 2018-12-23 stsp goto done;
3523 15a94983 2018-12-23 stsp
3524 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3525 15a94983 2018-12-23 stsp if (error)
3526 15a94983 2018-12-23 stsp goto done;
3527 15a94983 2018-12-23 stsp
3528 15a94983 2018-12-23 stsp if (type1 != type2) {
3529 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3530 b00d56cd 2018-04-01 stsp goto done;
3531 b00d56cd 2018-04-01 stsp }
3532 b00d56cd 2018-04-01 stsp
3533 15a94983 2018-12-23 stsp switch (type1) {
3534 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3535 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3536 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3537 b00d56cd 2018-04-01 stsp break;
3538 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3539 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3540 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3541 b00d56cd 2018-04-01 stsp break;
3542 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3543 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3544 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3545 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3546 b00d56cd 2018-04-01 stsp break;
3547 b00d56cd 2018-04-01 stsp default:
3548 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3549 b00d56cd 2018-04-01 stsp }
3550 b00d56cd 2018-04-01 stsp done:
3551 5e70831e 2019-05-28 stsp free(label1);
3552 5e70831e 2019-05-28 stsp free(label2);
3553 15a94983 2018-12-23 stsp free(id1);
3554 15a94983 2018-12-23 stsp free(id2);
3555 927df6b7 2019-02-10 stsp free(path);
3556 b72f483a 2019-02-05 stsp if (worktree)
3557 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3558 ad242220 2018-09-08 stsp if (repo) {
3559 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3560 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3561 ad242220 2018-09-08 stsp if (error == NULL)
3562 ad242220 2018-09-08 stsp error = repo_error;
3563 ad242220 2018-09-08 stsp }
3564 b00d56cd 2018-04-01 stsp return error;
3565 404c43c4 2018-06-21 stsp }
3566 404c43c4 2018-06-21 stsp
3567 404c43c4 2018-06-21 stsp __dead static void
3568 404c43c4 2018-06-21 stsp usage_blame(void)
3569 404c43c4 2018-06-21 stsp {
3570 9270e621 2019-02-05 stsp fprintf(stderr,
3571 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3572 404c43c4 2018-06-21 stsp getprogname());
3573 404c43c4 2018-06-21 stsp exit(1);
3574 b00d56cd 2018-04-01 stsp }
3575 b00d56cd 2018-04-01 stsp
3576 28315671 2019-08-14 stsp struct blame_line {
3577 28315671 2019-08-14 stsp int annotated;
3578 28315671 2019-08-14 stsp char *id_str;
3579 82f6abb8 2019-08-14 stsp char *committer;
3580 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3581 28315671 2019-08-14 stsp };
3582 28315671 2019-08-14 stsp
3583 28315671 2019-08-14 stsp struct blame_cb_args {
3584 28315671 2019-08-14 stsp struct blame_line *lines;
3585 28315671 2019-08-14 stsp int nlines;
3586 7ef28ff8 2019-08-14 stsp int nlines_prec;
3587 28315671 2019-08-14 stsp int lineno_cur;
3588 28315671 2019-08-14 stsp off_t *line_offsets;
3589 28315671 2019-08-14 stsp FILE *f;
3590 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3591 28315671 2019-08-14 stsp };
3592 28315671 2019-08-14 stsp
3593 404c43c4 2018-06-21 stsp static const struct got_error *
3594 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3595 28315671 2019-08-14 stsp {
3596 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3597 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3598 28315671 2019-08-14 stsp struct blame_line *bline;
3599 82f6abb8 2019-08-14 stsp char *line = NULL;
3600 28315671 2019-08-14 stsp size_t linesize = 0;
3601 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3602 28315671 2019-08-14 stsp off_t offset;
3603 bcb49d15 2019-08-14 stsp struct tm tm;
3604 bcb49d15 2019-08-14 stsp time_t committer_time;
3605 28315671 2019-08-14 stsp
3606 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3607 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3608 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3609 28315671 2019-08-14 stsp
3610 28315671 2019-08-14 stsp if (sigint_received)
3611 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3612 28315671 2019-08-14 stsp
3613 2839f8b9 2019-08-18 stsp if (lineno == -1)
3614 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3615 2839f8b9 2019-08-18 stsp
3616 28315671 2019-08-14 stsp /* Annotate this line. */
3617 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3618 28315671 2019-08-14 stsp if (bline->annotated)
3619 28315671 2019-08-14 stsp return NULL;
3620 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3621 28315671 2019-08-14 stsp if (err)
3622 28315671 2019-08-14 stsp return err;
3623 82f6abb8 2019-08-14 stsp
3624 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3625 82f6abb8 2019-08-14 stsp if (err)
3626 82f6abb8 2019-08-14 stsp goto done;
3627 82f6abb8 2019-08-14 stsp
3628 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3629 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3630 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3631 bcb49d15 2019-08-14 stsp goto done;
3632 bcb49d15 2019-08-14 stsp }
3633 bcb49d15 2019-08-14 stsp
3634 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3635 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3636 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3637 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3638 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3639 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3640 82f6abb8 2019-08-14 stsp goto done;
3641 82f6abb8 2019-08-14 stsp }
3642 28315671 2019-08-14 stsp bline->annotated = 1;
3643 28315671 2019-08-14 stsp
3644 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3645 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3646 28315671 2019-08-14 stsp if (!bline->annotated)
3647 82f6abb8 2019-08-14 stsp goto done;
3648 28315671 2019-08-14 stsp
3649 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3650 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3651 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3652 82f6abb8 2019-08-14 stsp goto done;
3653 82f6abb8 2019-08-14 stsp }
3654 28315671 2019-08-14 stsp
3655 28315671 2019-08-14 stsp while (bline->annotated) {
3656 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3657 82f6abb8 2019-08-14 stsp size_t len;
3658 82f6abb8 2019-08-14 stsp
3659 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3660 28315671 2019-08-14 stsp if (ferror(a->f))
3661 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3662 28315671 2019-08-14 stsp break;
3663 28315671 2019-08-14 stsp }
3664 82f6abb8 2019-08-14 stsp
3665 82f6abb8 2019-08-14 stsp committer = bline->committer;
3666 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3667 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3668 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3669 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3670 82f6abb8 2019-08-14 stsp if (at)
3671 82f6abb8 2019-08-14 stsp *at = '\0';
3672 82f6abb8 2019-08-14 stsp len = strlen(committer);
3673 82f6abb8 2019-08-14 stsp if (len >= 9)
3674 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3675 28315671 2019-08-14 stsp
3676 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3677 28315671 2019-08-14 stsp if (nl)
3678 28315671 2019-08-14 stsp *nl = '\0';
3679 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3680 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3681 28315671 2019-08-14 stsp
3682 28315671 2019-08-14 stsp a->lineno_cur++;
3683 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3684 28315671 2019-08-14 stsp }
3685 82f6abb8 2019-08-14 stsp done:
3686 82f6abb8 2019-08-14 stsp if (commit)
3687 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3688 28315671 2019-08-14 stsp free(line);
3689 28315671 2019-08-14 stsp return err;
3690 28315671 2019-08-14 stsp }
3691 28315671 2019-08-14 stsp
3692 28315671 2019-08-14 stsp static const struct got_error *
3693 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3694 404c43c4 2018-06-21 stsp {
3695 404c43c4 2018-06-21 stsp const struct got_error *error;
3696 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3697 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3698 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3699 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3700 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3701 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3702 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3703 28315671 2019-08-14 stsp struct blame_cb_args bca;
3704 28315671 2019-08-14 stsp int ch, obj_type, i;
3705 b02560ec 2019-08-19 stsp size_t filesize;
3706 90f3c347 2019-08-19 stsp
3707 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3708 404c43c4 2018-06-21 stsp
3709 404c43c4 2018-06-21 stsp #ifndef PROFILE
3710 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3711 36e2fb66 2019-01-04 stsp NULL) == -1)
3712 404c43c4 2018-06-21 stsp err(1, "pledge");
3713 404c43c4 2018-06-21 stsp #endif
3714 404c43c4 2018-06-21 stsp
3715 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3716 404c43c4 2018-06-21 stsp switch (ch) {
3717 404c43c4 2018-06-21 stsp case 'c':
3718 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3719 404c43c4 2018-06-21 stsp break;
3720 66bea077 2018-08-02 stsp case 'r':
3721 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3722 66bea077 2018-08-02 stsp if (repo_path == NULL)
3723 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3724 9ba1d308 2019-10-21 stsp optarg);
3725 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3726 66bea077 2018-08-02 stsp break;
3727 404c43c4 2018-06-21 stsp default:
3728 2deda0b9 2019-03-07 stsp usage_blame();
3729 404c43c4 2018-06-21 stsp /* NOTREACHED */
3730 404c43c4 2018-06-21 stsp }
3731 404c43c4 2018-06-21 stsp }
3732 404c43c4 2018-06-21 stsp
3733 404c43c4 2018-06-21 stsp argc -= optind;
3734 404c43c4 2018-06-21 stsp argv += optind;
3735 404c43c4 2018-06-21 stsp
3736 a39318fd 2018-08-02 stsp if (argc == 1)
3737 404c43c4 2018-06-21 stsp path = argv[0];
3738 a39318fd 2018-08-02 stsp else
3739 404c43c4 2018-06-21 stsp usage_blame();
3740 404c43c4 2018-06-21 stsp
3741 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3742 66bea077 2018-08-02 stsp if (cwd == NULL) {
3743 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3744 66bea077 2018-08-02 stsp goto done;
3745 66bea077 2018-08-02 stsp }
3746 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3747 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3748 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3749 66bea077 2018-08-02 stsp goto done;
3750 0c06baac 2019-02-05 stsp else
3751 0c06baac 2019-02-05 stsp error = NULL;
3752 0c06baac 2019-02-05 stsp if (worktree) {
3753 0c06baac 2019-02-05 stsp repo_path =
3754 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3755 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3756 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3757 1f03b8da 2020-03-20 stsp if (error)
3758 1f03b8da 2020-03-20 stsp goto done;
3759 1f03b8da 2020-03-20 stsp }
3760 0c06baac 2019-02-05 stsp } else {
3761 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3762 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3763 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3764 0c06baac 2019-02-05 stsp goto done;
3765 0c06baac 2019-02-05 stsp }
3766 66bea077 2018-08-02 stsp }
3767 66bea077 2018-08-02 stsp }
3768 36e2fb66 2019-01-04 stsp
3769 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3770 c02c541e 2019-03-29 stsp if (error != NULL)
3771 36e2fb66 2019-01-04 stsp goto done;
3772 66bea077 2018-08-02 stsp
3773 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3774 c02c541e 2019-03-29 stsp if (error)
3775 404c43c4 2018-06-21 stsp goto done;
3776 404c43c4 2018-06-21 stsp
3777 0c06baac 2019-02-05 stsp if (worktree) {
3778 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3779 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3780 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3781 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3782 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3783 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3784 6efaaa2d 2019-02-05 stsp path) == -1) {
3785 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3786 0c06baac 2019-02-05 stsp goto done;
3787 0c06baac 2019-02-05 stsp }
3788 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3789 0c06baac 2019-02-05 stsp free(p);
3790 0c06baac 2019-02-05 stsp } else {
3791 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3792 0c06baac 2019-02-05 stsp }
3793 0c06baac 2019-02-05 stsp if (error)
3794 66bea077 2018-08-02 stsp goto done;
3795 66bea077 2018-08-02 stsp
3796 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3797 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3798 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3799 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3800 404c43c4 2018-06-21 stsp if (error != NULL)
3801 66bea077 2018-08-02 stsp goto done;
3802 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3803 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3804 404c43c4 2018-06-21 stsp if (error != NULL)
3805 66bea077 2018-08-02 stsp goto done;
3806 404c43c4 2018-06-21 stsp } else {
3807 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3808 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3809 30837e32 2019-07-25 stsp if (error)
3810 66bea077 2018-08-02 stsp goto done;
3811 404c43c4 2018-06-21 stsp }
3812 404c43c4 2018-06-21 stsp
3813 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3814 28315671 2019-08-14 stsp if (error)
3815 28315671 2019-08-14 stsp goto done;
3816 28315671 2019-08-14 stsp
3817 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3818 28315671 2019-08-14 stsp if (error)
3819 28315671 2019-08-14 stsp goto done;
3820 28315671 2019-08-14 stsp
3821 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3822 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3823 28315671 2019-08-14 stsp goto done;
3824 28315671 2019-08-14 stsp }
3825 28315671 2019-08-14 stsp
3826 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3827 28315671 2019-08-14 stsp if (error)
3828 28315671 2019-08-14 stsp goto done;
3829 28315671 2019-08-14 stsp bca.f = got_opentemp();
3830 28315671 2019-08-14 stsp if (bca.f == NULL) {
3831 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3832 28315671 2019-08-14 stsp goto done;
3833 28315671 2019-08-14 stsp }
3834 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3835 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3836 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3837 28315671 2019-08-14 stsp goto done;
3838 28315671 2019-08-14 stsp
3839 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3840 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3841 b02560ec 2019-08-19 stsp bca.nlines--;
3842 b02560ec 2019-08-19 stsp
3843 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3844 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3845 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3846 28315671 2019-08-14 stsp goto done;
3847 28315671 2019-08-14 stsp }
3848 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3849 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3850 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3851 7ef28ff8 2019-08-14 stsp while (i > 0) {
3852 7ef28ff8 2019-08-14 stsp i /= 10;
3853 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3854 7ef28ff8 2019-08-14 stsp }
3855 82f6abb8 2019-08-14 stsp bca.repo = repo;
3856 7ef28ff8 2019-08-14 stsp
3857 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3858 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3859 404c43c4 2018-06-21 stsp done:
3860 66bea077 2018-08-02 stsp free(in_repo_path);
3861 66bea077 2018-08-02 stsp free(repo_path);
3862 66bea077 2018-08-02 stsp free(cwd);
3863 404c43c4 2018-06-21 stsp free(commit_id);
3864 28315671 2019-08-14 stsp free(obj_id);
3865 28315671 2019-08-14 stsp if (blob)
3866 28315671 2019-08-14 stsp got_object_blob_close(blob);
3867 0c06baac 2019-02-05 stsp if (worktree)
3868 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3869 ad242220 2018-09-08 stsp if (repo) {
3870 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3871 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3872 ad242220 2018-09-08 stsp if (error == NULL)
3873 ad242220 2018-09-08 stsp error = repo_error;
3874 ad242220 2018-09-08 stsp }
3875 3affba96 2019-09-22 stsp if (bca.lines) {
3876 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3877 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3878 3affba96 2019-09-22 stsp free(bline->id_str);
3879 3affba96 2019-09-22 stsp free(bline->committer);
3880 3affba96 2019-09-22 stsp }
3881 3affba96 2019-09-22 stsp free(bca.lines);
3882 28315671 2019-08-14 stsp }
3883 28315671 2019-08-14 stsp free(bca.line_offsets);
3884 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3885 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3886 404c43c4 2018-06-21 stsp return error;
3887 5de5890b 2018-10-18 stsp }
3888 5de5890b 2018-10-18 stsp
3889 5de5890b 2018-10-18 stsp __dead static void
3890 5de5890b 2018-10-18 stsp usage_tree(void)
3891 5de5890b 2018-10-18 stsp {
3892 c1669e2e 2019-01-09 stsp fprintf(stderr,
3893 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3894 5de5890b 2018-10-18 stsp getprogname());
3895 5de5890b 2018-10-18 stsp exit(1);
3896 5de5890b 2018-10-18 stsp }
3897 5de5890b 2018-10-18 stsp
3898 c1669e2e 2019-01-09 stsp static void
3899 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3900 c1669e2e 2019-01-09 stsp const char *root_path)
3901 c1669e2e 2019-01-09 stsp {
3902 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3903 848d6979 2019-08-12 stsp const char *modestr = "";
3904 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3905 5de5890b 2018-10-18 stsp
3906 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3907 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3908 c1669e2e 2019-01-09 stsp path++;
3909 c1669e2e 2019-01-09 stsp
3910 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3911 63c5ca5d 2019-08-24 stsp modestr = "$";
3912 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3913 848d6979 2019-08-12 stsp modestr = "@";
3914 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3915 848d6979 2019-08-12 stsp modestr = "/";
3916 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3917 848d6979 2019-08-12 stsp modestr = "*";
3918 848d6979 2019-08-12 stsp
3919 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3920 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3921 c1669e2e 2019-01-09 stsp }
3922 c1669e2e 2019-01-09 stsp
3923 5de5890b 2018-10-18 stsp static const struct got_error *
3924 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3925 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3926 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3927 5de5890b 2018-10-18 stsp {
3928 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3929 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3930 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3931 56e0773d 2019-11-28 stsp int nentries, i;
3932 5de5890b 2018-10-18 stsp
3933 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3934 5de5890b 2018-10-18 stsp if (err)
3935 5de5890b 2018-10-18 stsp goto done;
3936 5de5890b 2018-10-18 stsp
3937 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3938 5de5890b 2018-10-18 stsp if (err)
3939 5de5890b 2018-10-18 stsp goto done;
3940 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3941 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3942 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3943 5de5890b 2018-10-18 stsp char *id = NULL;
3944 84453469 2018-11-11 stsp
3945 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3946 84453469 2018-11-11 stsp break;
3947 84453469 2018-11-11 stsp
3948 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3949 5de5890b 2018-10-18 stsp if (show_ids) {
3950 5de5890b 2018-10-18 stsp char *id_str;
3951 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3952 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3953 5de5890b 2018-10-18 stsp if (err)
3954 5de5890b 2018-10-18 stsp goto done;
3955 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3956 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3957 5de5890b 2018-10-18 stsp free(id_str);
3958 5de5890b 2018-10-18 stsp goto done;
3959 5de5890b 2018-10-18 stsp }
3960 5de5890b 2018-10-18 stsp free(id_str);
3961 5de5890b 2018-10-18 stsp }
3962 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3963 5de5890b 2018-10-18 stsp free(id);
3964 c1669e2e 2019-01-09 stsp
3965 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3966 c1669e2e 2019-01-09 stsp char *child_path;
3967 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3968 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3969 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3970 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3971 c1669e2e 2019-01-09 stsp goto done;
3972 c1669e2e 2019-01-09 stsp }
3973 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3974 c1669e2e 2019-01-09 stsp root_path, repo);
3975 c1669e2e 2019-01-09 stsp free(child_path);
3976 c1669e2e 2019-01-09 stsp if (err)
3977 c1669e2e 2019-01-09 stsp goto done;
3978 c1669e2e 2019-01-09 stsp }
3979 5de5890b 2018-10-18 stsp }
3980 5de5890b 2018-10-18 stsp done:
3981 5de5890b 2018-10-18 stsp if (tree)
3982 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3983 5de5890b 2018-10-18 stsp free(tree_id);
3984 5de5890b 2018-10-18 stsp return err;
3985 404c43c4 2018-06-21 stsp }
3986 404c43c4 2018-06-21 stsp
3987 5de5890b 2018-10-18 stsp static const struct got_error *
3988 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3989 5de5890b 2018-10-18 stsp {
3990 5de5890b 2018-10-18 stsp const struct got_error *error;
3991 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3992 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3993 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
3994 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3995 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3996 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3997 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3998 5de5890b 2018-10-18 stsp int ch;
3999 5de5890b 2018-10-18 stsp
4000 5de5890b 2018-10-18 stsp #ifndef PROFILE
4001 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4002 0f8d269b 2019-01-04 stsp NULL) == -1)
4003 5de5890b 2018-10-18 stsp err(1, "pledge");
4004 5de5890b 2018-10-18 stsp #endif
4005 5de5890b 2018-10-18 stsp
4006 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4007 5de5890b 2018-10-18 stsp switch (ch) {
4008 5de5890b 2018-10-18 stsp case 'c':
4009 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4010 5de5890b 2018-10-18 stsp break;
4011 5de5890b 2018-10-18 stsp case 'r':
4012 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4013 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4014 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4015 9ba1d308 2019-10-21 stsp optarg);
4016 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4017 5de5890b 2018-10-18 stsp break;
4018 5de5890b 2018-10-18 stsp case 'i':
4019 5de5890b 2018-10-18 stsp show_ids = 1;
4020 5de5890b 2018-10-18 stsp break;
4021 c1669e2e 2019-01-09 stsp case 'R':
4022 c1669e2e 2019-01-09 stsp recurse = 1;
4023 c1669e2e 2019-01-09 stsp break;
4024 5de5890b 2018-10-18 stsp default:
4025 2deda0b9 2019-03-07 stsp usage_tree();
4026 5de5890b 2018-10-18 stsp /* NOTREACHED */
4027 5de5890b 2018-10-18 stsp }
4028 5de5890b 2018-10-18 stsp }
4029 5de5890b 2018-10-18 stsp
4030 5de5890b 2018-10-18 stsp argc -= optind;
4031 5de5890b 2018-10-18 stsp argv += optind;
4032 5de5890b 2018-10-18 stsp
4033 5de5890b 2018-10-18 stsp if (argc == 1)
4034 5de5890b 2018-10-18 stsp path = argv[0];
4035 5de5890b 2018-10-18 stsp else if (argc > 1)
4036 5de5890b 2018-10-18 stsp usage_tree();
4037 5de5890b 2018-10-18 stsp else
4038 7a2c19d6 2019-02-05 stsp path = NULL;
4039 7a2c19d6 2019-02-05 stsp
4040 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4041 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4042 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4043 9bf7a39b 2019-02-05 stsp goto done;
4044 9bf7a39b 2019-02-05 stsp }
4045 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4046 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4047 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4048 7a2c19d6 2019-02-05 stsp goto done;
4049 7a2c19d6 2019-02-05 stsp else
4050 7a2c19d6 2019-02-05 stsp error = NULL;
4051 7a2c19d6 2019-02-05 stsp if (worktree) {
4052 7a2c19d6 2019-02-05 stsp repo_path =
4053 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4054 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4055 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4056 7a2c19d6 2019-02-05 stsp if (error)
4057 7a2c19d6 2019-02-05 stsp goto done;
4058 7a2c19d6 2019-02-05 stsp } else {
4059 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4060 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4061 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4062 7a2c19d6 2019-02-05 stsp goto done;
4063 7a2c19d6 2019-02-05 stsp }
4064 5de5890b 2018-10-18 stsp }
4065 5de5890b 2018-10-18 stsp }
4066 5de5890b 2018-10-18 stsp
4067 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4068 5de5890b 2018-10-18 stsp if (error != NULL)
4069 c02c541e 2019-03-29 stsp goto done;
4070 c02c541e 2019-03-29 stsp
4071 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4072 c02c541e 2019-03-29 stsp if (error)
4073 5de5890b 2018-10-18 stsp goto done;
4074 5de5890b 2018-10-18 stsp
4075 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4076 9bf7a39b 2019-02-05 stsp if (worktree) {
4077 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4078 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4079 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4080 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4081 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4082 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4083 9bf7a39b 2019-02-05 stsp goto done;
4084 9bf7a39b 2019-02-05 stsp }
4085 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4086 9bf7a39b 2019-02-05 stsp free(p);
4087 9bf7a39b 2019-02-05 stsp if (error)
4088 9bf7a39b 2019-02-05 stsp goto done;
4089 9bf7a39b 2019-02-05 stsp } else
4090 9bf7a39b 2019-02-05 stsp path = "/";
4091 9bf7a39b 2019-02-05 stsp }
4092 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4093 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4094 9bf7a39b 2019-02-05 stsp if (error != NULL)
4095 9bf7a39b 2019-02-05 stsp goto done;
4096 9bf7a39b 2019-02-05 stsp }
4097 5de5890b 2018-10-18 stsp
4098 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4099 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4100 4e0a20a4 2020-03-23 tracey if (worktree)
4101 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4102 4e0a20a4 2020-03-23 tracey else
4103 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4104 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4105 5de5890b 2018-10-18 stsp if (error != NULL)
4106 5de5890b 2018-10-18 stsp goto done;
4107 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4108 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4109 5de5890b 2018-10-18 stsp if (error != NULL)
4110 5de5890b 2018-10-18 stsp goto done;
4111 5de5890b 2018-10-18 stsp } else {
4112 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4113 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4114 30837e32 2019-07-25 stsp if (error)
4115 5de5890b 2018-10-18 stsp goto done;
4116 5de5890b 2018-10-18 stsp }
4117 5de5890b 2018-10-18 stsp
4118 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4119 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4120 5de5890b 2018-10-18 stsp done:
4121 5de5890b 2018-10-18 stsp free(in_repo_path);
4122 5de5890b 2018-10-18 stsp free(repo_path);
4123 5de5890b 2018-10-18 stsp free(cwd);
4124 5de5890b 2018-10-18 stsp free(commit_id);
4125 7a2c19d6 2019-02-05 stsp if (worktree)
4126 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4127 5de5890b 2018-10-18 stsp if (repo) {
4128 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4129 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4130 5de5890b 2018-10-18 stsp if (error == NULL)
4131 5de5890b 2018-10-18 stsp error = repo_error;
4132 5de5890b 2018-10-18 stsp }
4133 5de5890b 2018-10-18 stsp return error;
4134 5de5890b 2018-10-18 stsp }
4135 5de5890b 2018-10-18 stsp
4136 6bad629b 2019-02-04 stsp __dead static void
4137 6bad629b 2019-02-04 stsp usage_status(void)
4138 6bad629b 2019-02-04 stsp {
4139 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4140 6bad629b 2019-02-04 stsp exit(1);
4141 6bad629b 2019-02-04 stsp }
4142 5c860e29 2018-03-12 stsp
4143 b72f483a 2019-02-05 stsp static const struct got_error *
4144 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4145 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4146 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4147 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4148 6bad629b 2019-02-04 stsp {
4149 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4150 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4151 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4152 b72f483a 2019-02-05 stsp return NULL;
4153 6bad629b 2019-02-04 stsp }
4154 5c860e29 2018-03-12 stsp
4155 6bad629b 2019-02-04 stsp static const struct got_error *
4156 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4157 6bad629b 2019-02-04 stsp {
4158 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4159 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4160 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4161 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4162 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4163 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4164 a5edda0a 2019-07-27 stsp int ch;
4165 5c860e29 2018-03-12 stsp
4166 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4167 72ea6654 2019-07-27 stsp
4168 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4169 6bad629b 2019-02-04 stsp switch (ch) {
4170 5c860e29 2018-03-12 stsp default:
4171 2deda0b9 2019-03-07 stsp usage_status();
4172 6bad629b 2019-02-04 stsp /* NOTREACHED */
4173 5c860e29 2018-03-12 stsp }
4174 5c860e29 2018-03-12 stsp }
4175 5c860e29 2018-03-12 stsp
4176 6bad629b 2019-02-04 stsp argc -= optind;
4177 6bad629b 2019-02-04 stsp argv += optind;
4178 5c860e29 2018-03-12 stsp
4179 6bad629b 2019-02-04 stsp #ifndef PROFILE
4180 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4181 6bad629b 2019-02-04 stsp NULL) == -1)
4182 6bad629b 2019-02-04 stsp err(1, "pledge");
4183 f42b1b34 2018-03-12 stsp #endif
4184 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4185 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4186 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4187 927df6b7 2019-02-10 stsp goto done;
4188 927df6b7 2019-02-10 stsp }
4189 927df6b7 2019-02-10 stsp
4190 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4191 927df6b7 2019-02-10 stsp if (error != NULL)
4192 a5edda0a 2019-07-27 stsp goto done;
4193 6bad629b 2019-02-04 stsp
4194 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4195 c9956ddf 2019-09-08 stsp NULL);
4196 6bad629b 2019-02-04 stsp if (error != NULL)
4197 6bad629b 2019-02-04 stsp goto done;
4198 6bad629b 2019-02-04 stsp
4199 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4200 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4201 087fb88c 2019-08-04 stsp if (error)
4202 087fb88c 2019-08-04 stsp goto done;
4203 087fb88c 2019-08-04 stsp
4204 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4205 6bad629b 2019-02-04 stsp if (error)
4206 6bad629b 2019-02-04 stsp goto done;
4207 6bad629b 2019-02-04 stsp
4208 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4209 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4210 6bad629b 2019-02-04 stsp done:
4211 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4212 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4213 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4214 927df6b7 2019-02-10 stsp free(cwd);
4215 d0eebce4 2019-03-11 stsp return error;
4216 d0eebce4 2019-03-11 stsp }
4217 d0eebce4 2019-03-11 stsp
4218 d0eebce4 2019-03-11 stsp __dead static void
4219 d0eebce4 2019-03-11 stsp usage_ref(void)
4220 d0eebce4 2019-03-11 stsp {
4221 d0eebce4 2019-03-11 stsp fprintf(stderr,
4222 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4223 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4224 d0eebce4 2019-03-11 stsp getprogname());
4225 d0eebce4 2019-03-11 stsp exit(1);
4226 d0eebce4 2019-03-11 stsp }
4227 d0eebce4 2019-03-11 stsp
4228 d0eebce4 2019-03-11 stsp static const struct got_error *
4229 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4230 d0eebce4 2019-03-11 stsp {
4231 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4232 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4233 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4234 d0eebce4 2019-03-11 stsp
4235 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4236 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4237 d0eebce4 2019-03-11 stsp if (err)
4238 d0eebce4 2019-03-11 stsp return err;
4239 d0eebce4 2019-03-11 stsp
4240 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4241 d0eebce4 2019-03-11 stsp char *refstr;
4242 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4243 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4244 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4245 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4246 d0eebce4 2019-03-11 stsp free(refstr);
4247 d0eebce4 2019-03-11 stsp }
4248 d0eebce4 2019-03-11 stsp
4249 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4250 d0eebce4 2019-03-11 stsp return NULL;
4251 d0eebce4 2019-03-11 stsp }
4252 d0eebce4 2019-03-11 stsp
4253 d0eebce4 2019-03-11 stsp static const struct got_error *
4254 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4255 d0eebce4 2019-03-11 stsp {
4256 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4257 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4258 d0eebce4 2019-03-11 stsp
4259 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4260 d0eebce4 2019-03-11 stsp if (err)
4261 d0eebce4 2019-03-11 stsp return err;
4262 d0eebce4 2019-03-11 stsp
4263 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4264 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4265 d0eebce4 2019-03-11 stsp return err;
4266 d0eebce4 2019-03-11 stsp }
4267 d0eebce4 2019-03-11 stsp
4268 d0eebce4 2019-03-11 stsp static const struct got_error *
4269 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4270 d0eebce4 2019-03-11 stsp {
4271 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4272 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4273 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4274 d1644381 2019-07-14 stsp
4275 d1644381 2019-07-14 stsp /*
4276 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4277 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4278 d1644381 2019-07-14 stsp * an unintended typo.
4279 d1644381 2019-07-14 stsp */
4280 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4281 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4282 d0eebce4 2019-03-11 stsp
4283 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4284 dd88155e 2019-06-29 stsp repo);
4285 d83d9d5c 2019-05-13 stsp if (err) {
4286 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4287 d0eebce4 2019-03-11 stsp
4288 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4289 d83d9d5c 2019-05-13 stsp return err;
4290 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4291 d83d9d5c 2019-05-13 stsp if (err)
4292 d83d9d5c 2019-05-13 stsp return err;
4293 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4294 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4295 d83d9d5c 2019-05-13 stsp if (err)
4296 d83d9d5c 2019-05-13 stsp return err;
4297 d83d9d5c 2019-05-13 stsp }
4298 d83d9d5c 2019-05-13 stsp
4299 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4300 d0eebce4 2019-03-11 stsp if (err)
4301 d0eebce4 2019-03-11 stsp goto done;
4302 d0eebce4 2019-03-11 stsp
4303 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4304 d0eebce4 2019-03-11 stsp done:
4305 d0eebce4 2019-03-11 stsp if (ref)
4306 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4307 d0eebce4 2019-03-11 stsp free(id);
4308 d1c1ae5f 2019-08-12 stsp return err;
4309 d1c1ae5f 2019-08-12 stsp }
4310 d1c1ae5f 2019-08-12 stsp
4311 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4312 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4313 d1c1ae5f 2019-08-12 stsp {
4314 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4315 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4316 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4317 d1c1ae5f 2019-08-12 stsp
4318 d1c1ae5f 2019-08-12 stsp /*
4319 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4320 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4321 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4322 d1c1ae5f 2019-08-12 stsp */
4323 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4324 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4325 d1c1ae5f 2019-08-12 stsp
4326 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4327 d1c1ae5f 2019-08-12 stsp if (err)
4328 d1c1ae5f 2019-08-12 stsp return err;
4329 d1c1ae5f 2019-08-12 stsp
4330 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4331 d1c1ae5f 2019-08-12 stsp if (err)
4332 d1c1ae5f 2019-08-12 stsp goto done;
4333 d1c1ae5f 2019-08-12 stsp
4334 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4335 d1c1ae5f 2019-08-12 stsp done:
4336 d1c1ae5f 2019-08-12 stsp if (target_ref)
4337 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4338 d1c1ae5f 2019-08-12 stsp if (ref)
4339 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4340 d0eebce4 2019-03-11 stsp return err;
4341 d0eebce4 2019-03-11 stsp }
4342 d0eebce4 2019-03-11 stsp
4343 d0eebce4 2019-03-11 stsp static const struct got_error *
4344 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4345 d0eebce4 2019-03-11 stsp {
4346 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4347 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4348 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4349 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4350 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4351 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4352 b2070a3f 2020-03-22 stsp char *refname = NULL;
4353 d0eebce4 2019-03-11 stsp
4354 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4355 d0eebce4 2019-03-11 stsp switch (ch) {
4356 e31abbf2 2020-03-22 stsp case 'c':
4357 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4358 e31abbf2 2020-03-22 stsp break;
4359 d0eebce4 2019-03-11 stsp case 'd':
4360 e31abbf2 2020-03-22 stsp do_delete = 1;
4361 d0eebce4 2019-03-11 stsp break;
4362 d0eebce4 2019-03-11 stsp case 'r':
4363 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4364 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4365 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4366 9ba1d308 2019-10-21 stsp optarg);
4367 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4368 d0eebce4 2019-03-11 stsp break;
4369 d0eebce4 2019-03-11 stsp case 'l':
4370 d0eebce4 2019-03-11 stsp do_list = 1;
4371 d1c1ae5f 2019-08-12 stsp break;
4372 d1c1ae5f 2019-08-12 stsp case 's':
4373 e31abbf2 2020-03-22 stsp symref_target = optarg;
4374 d0eebce4 2019-03-11 stsp break;
4375 d0eebce4 2019-03-11 stsp default:
4376 d0eebce4 2019-03-11 stsp usage_ref();
4377 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4378 d0eebce4 2019-03-11 stsp }
4379 d0eebce4 2019-03-11 stsp }
4380 d0eebce4 2019-03-11 stsp
4381 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4382 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4383 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4384 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4385 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4386 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4387 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4388 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4389 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4390 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4391 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4392 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4393 d0eebce4 2019-03-11 stsp
4394 d0eebce4 2019-03-11 stsp argc -= optind;
4395 d0eebce4 2019-03-11 stsp argv += optind;
4396 d0eebce4 2019-03-11 stsp
4397 e31abbf2 2020-03-22 stsp if (do_list) {
4398 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4399 d0eebce4 2019-03-11 stsp usage_ref();
4400 b2070a3f 2020-03-22 stsp if (argc == 1) {
4401 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4402 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4403 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4404 b2070a3f 2020-03-22 stsp goto done;
4405 b2070a3f 2020-03-22 stsp }
4406 b2070a3f 2020-03-22 stsp }
4407 e31abbf2 2020-03-22 stsp } else {
4408 e31abbf2 2020-03-22 stsp if (argc != 1)
4409 e31abbf2 2020-03-22 stsp usage_ref();
4410 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4411 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4412 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4413 b2070a3f 2020-03-22 stsp goto done;
4414 b2070a3f 2020-03-22 stsp }
4415 e31abbf2 2020-03-22 stsp }
4416 b2070a3f 2020-03-22 stsp
4417 b2070a3f 2020-03-22 stsp if (refname)
4418 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4419 d0eebce4 2019-03-11 stsp
4420 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4421 e0b57350 2019-03-12 stsp if (do_list) {
4422 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4423 e0b57350 2019-03-12 stsp NULL) == -1)
4424 e0b57350 2019-03-12 stsp err(1, "pledge");
4425 e0b57350 2019-03-12 stsp } else {
4426 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4427 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4428 e0b57350 2019-03-12 stsp err(1, "pledge");
4429 e0b57350 2019-03-12 stsp }
4430 d0eebce4 2019-03-11 stsp #endif
4431 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4432 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4433 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4434 d0eebce4 2019-03-11 stsp goto done;
4435 d0eebce4 2019-03-11 stsp }
4436 d0eebce4 2019-03-11 stsp
4437 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4438 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4439 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4440 d0eebce4 2019-03-11 stsp goto done;
4441 d0eebce4 2019-03-11 stsp else
4442 d0eebce4 2019-03-11 stsp error = NULL;
4443 d0eebce4 2019-03-11 stsp if (worktree) {
4444 d0eebce4 2019-03-11 stsp repo_path =
4445 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4446 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4447 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4448 d0eebce4 2019-03-11 stsp if (error)
4449 d0eebce4 2019-03-11 stsp goto done;
4450 d0eebce4 2019-03-11 stsp } else {
4451 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4452 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4453 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4454 d0eebce4 2019-03-11 stsp goto done;
4455 d0eebce4 2019-03-11 stsp }
4456 d0eebce4 2019-03-11 stsp }
4457 d0eebce4 2019-03-11 stsp }
4458 d0eebce4 2019-03-11 stsp
4459 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4460 d0eebce4 2019-03-11 stsp if (error != NULL)
4461 d0eebce4 2019-03-11 stsp goto done;
4462 d0eebce4 2019-03-11 stsp
4463 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4464 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4465 c02c541e 2019-03-29 stsp if (error)
4466 c02c541e 2019-03-29 stsp goto done;
4467 c02c541e 2019-03-29 stsp
4468 d0eebce4 2019-03-11 stsp if (do_list)
4469 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4470 e31abbf2 2020-03-22 stsp else if (do_delete)
4471 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4472 e31abbf2 2020-03-22 stsp else if (symref_target)
4473 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4474 e31abbf2 2020-03-22 stsp else {
4475 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4476 e31abbf2 2020-03-22 stsp usage_ref();
4477 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4478 e31abbf2 2020-03-22 stsp }
4479 4e759de4 2019-06-26 stsp done:
4480 b2070a3f 2020-03-22 stsp free(refname);
4481 4e759de4 2019-06-26 stsp if (repo)
4482 4e759de4 2019-06-26 stsp got_repo_close(repo);
4483 4e759de4 2019-06-26 stsp if (worktree)
4484 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4485 4e759de4 2019-06-26 stsp free(cwd);
4486 4e759de4 2019-06-26 stsp free(repo_path);
4487 4e759de4 2019-06-26 stsp return error;
4488 4e759de4 2019-06-26 stsp }
4489 4e759de4 2019-06-26 stsp
4490 4e759de4 2019-06-26 stsp __dead static void
4491 4e759de4 2019-06-26 stsp usage_branch(void)
4492 4e759de4 2019-06-26 stsp {
4493 4e759de4 2019-06-26 stsp fprintf(stderr,
4494 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4495 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4496 4e759de4 2019-06-26 stsp exit(1);
4497 4e759de4 2019-06-26 stsp }
4498 4e759de4 2019-06-26 stsp
4499 4e759de4 2019-06-26 stsp static const struct got_error *
4500 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4501 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4502 4e99b47e 2019-10-04 stsp {
4503 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4504 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4505 4e99b47e 2019-10-04 stsp char *refstr;
4506 4e99b47e 2019-10-04 stsp
4507 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4508 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4509 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4510 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4511 4e99b47e 2019-10-04 stsp
4512 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4513 4e99b47e 2019-10-04 stsp if (err)
4514 4e99b47e 2019-10-04 stsp return err;
4515 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4516 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4517 4e99b47e 2019-10-04 stsp marker = "* ";
4518 4e99b47e 2019-10-04 stsp else
4519 4e99b47e 2019-10-04 stsp marker = "~ ";
4520 4e99b47e 2019-10-04 stsp free(id);
4521 4e99b47e 2019-10-04 stsp }
4522 4e99b47e 2019-10-04 stsp
4523 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4524 4e99b47e 2019-10-04 stsp refname += 11;
4525 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4526 4e99b47e 2019-10-04 stsp refname += 18;
4527 4e99b47e 2019-10-04 stsp
4528 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4529 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4530 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4531 4e99b47e 2019-10-04 stsp
4532 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4533 4e99b47e 2019-10-04 stsp free(refstr);
4534 4e99b47e 2019-10-04 stsp return NULL;
4535 4e99b47e 2019-10-04 stsp }
4536 4e99b47e 2019-10-04 stsp
4537 4e99b47e 2019-10-04 stsp static const struct got_error *
4538 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4539 ad89fa31 2019-10-04 stsp {
4540 ad89fa31 2019-10-04 stsp const char *refname;
4541 ad89fa31 2019-10-04 stsp
4542 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4543 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4544 ad89fa31 2019-10-04 stsp
4545 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4546 ad89fa31 2019-10-04 stsp
4547 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4548 ad89fa31 2019-10-04 stsp refname += 11;
4549 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4550 ad89fa31 2019-10-04 stsp refname += 18;
4551 ad89fa31 2019-10-04 stsp
4552 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4553 ad89fa31 2019-10-04 stsp
4554 ad89fa31 2019-10-04 stsp return NULL;
4555 ad89fa31 2019-10-04 stsp }
4556 ad89fa31 2019-10-04 stsp
4557 ad89fa31 2019-10-04 stsp static const struct got_error *
4558 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4559 4e759de4 2019-06-26 stsp {
4560 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4561 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4562 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4563 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4564 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4565 4e759de4 2019-06-26 stsp
4566 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4567 ba882ee3 2019-07-11 stsp
4568 4e99b47e 2019-10-04 stsp if (worktree) {
4569 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4570 4e99b47e 2019-10-04 stsp worktree);
4571 4e99b47e 2019-10-04 stsp if (err)
4572 4e99b47e 2019-10-04 stsp return err;
4573 4e99b47e 2019-10-04 stsp
4574 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4575 4e99b47e 2019-10-04 stsp worktree);
4576 4e99b47e 2019-10-04 stsp if (err)
4577 4e99b47e 2019-10-04 stsp return err;
4578 4e99b47e 2019-10-04 stsp
4579 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4580 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4581 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4582 4e99b47e 2019-10-04 stsp if (err)
4583 4e99b47e 2019-10-04 stsp return err;
4584 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4585 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4586 4e99b47e 2019-10-04 stsp }
4587 4e99b47e 2019-10-04 stsp }
4588 4e99b47e 2019-10-04 stsp
4589 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4590 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4591 4e759de4 2019-06-26 stsp if (err)
4592 4e759de4 2019-06-26 stsp return err;
4593 4e759de4 2019-06-26 stsp
4594 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4595 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4596 4e759de4 2019-06-26 stsp
4597 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4598 4e759de4 2019-06-26 stsp return NULL;
4599 4e759de4 2019-06-26 stsp }
4600 4e759de4 2019-06-26 stsp
4601 4e759de4 2019-06-26 stsp static const struct got_error *
4602 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4603 45cd4e47 2019-08-25 stsp const char *branch_name)
4604 4e759de4 2019-06-26 stsp {
4605 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4606 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4607 4e759de4 2019-06-26 stsp char *refname;
4608 4e759de4 2019-06-26 stsp
4609 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4610 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4611 4e759de4 2019-06-26 stsp
4612 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4613 4e759de4 2019-06-26 stsp if (err)
4614 4e759de4 2019-06-26 stsp goto done;
4615 4e759de4 2019-06-26 stsp
4616 45cd4e47 2019-08-25 stsp if (worktree &&
4617 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4618 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4619 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4620 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4621 45cd4e47 2019-08-25 stsp goto done;
4622 45cd4e47 2019-08-25 stsp }
4623 45cd4e47 2019-08-25 stsp
4624 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4625 4e759de4 2019-06-26 stsp done:
4626 45cd4e47 2019-08-25 stsp if (ref)
4627 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4628 4e759de4 2019-06-26 stsp free(refname);
4629 4e759de4 2019-06-26 stsp return err;
4630 4e759de4 2019-06-26 stsp }
4631 4e759de4 2019-06-26 stsp
4632 4e759de4 2019-06-26 stsp static const struct got_error *
4633 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4634 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4635 4e759de4 2019-06-26 stsp {
4636 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4637 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4638 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4639 d3f84d51 2019-07-11 stsp
4640 d3f84d51 2019-07-11 stsp /*
4641 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4642 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4643 d3f84d51 2019-07-11 stsp * an unintended typo.
4644 d3f84d51 2019-07-11 stsp */
4645 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4646 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4647 4e759de4 2019-06-26 stsp
4648 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4649 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4650 4e759de4 2019-06-26 stsp goto done;
4651 4e759de4 2019-06-26 stsp }
4652 4e759de4 2019-06-26 stsp
4653 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4654 4e759de4 2019-06-26 stsp if (err == NULL) {
4655 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4656 4e759de4 2019-06-26 stsp goto done;
4657 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4658 4e759de4 2019-06-26 stsp goto done;
4659 4e759de4 2019-06-26 stsp
4660 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4661 4e759de4 2019-06-26 stsp if (err)
4662 4e759de4 2019-06-26 stsp goto done;
4663 4e759de4 2019-06-26 stsp
4664 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4665 d0eebce4 2019-03-11 stsp done:
4666 4e759de4 2019-06-26 stsp if (ref)
4667 4e759de4 2019-06-26 stsp got_ref_close(ref);
4668 4e759de4 2019-06-26 stsp free(base_refname);
4669 4e759de4 2019-06-26 stsp free(refname);
4670 4e759de4 2019-06-26 stsp return err;
4671 4e759de4 2019-06-26 stsp }
4672 4e759de4 2019-06-26 stsp
4673 4e759de4 2019-06-26 stsp static const struct got_error *
4674 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4675 4e759de4 2019-06-26 stsp {
4676 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4677 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4678 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4679 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4680 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4681 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4682 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4683 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4684 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4685 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4686 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4687 4e759de4 2019-06-26 stsp
4688 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4689 da76fce2 2020-02-24 stsp
4690 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4691 4e759de4 2019-06-26 stsp switch (ch) {
4692 a74f7e83 2019-11-10 stsp case 'c':
4693 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4694 a74f7e83 2019-11-10 stsp break;
4695 4e759de4 2019-06-26 stsp case 'd':
4696 4e759de4 2019-06-26 stsp delref = optarg;
4697 4e759de4 2019-06-26 stsp break;
4698 4e759de4 2019-06-26 stsp case 'r':
4699 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4700 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4701 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4702 9ba1d308 2019-10-21 stsp optarg);
4703 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4704 4e759de4 2019-06-26 stsp break;
4705 4e759de4 2019-06-26 stsp case 'l':
4706 4e759de4 2019-06-26 stsp do_list = 1;
4707 da76fce2 2020-02-24 stsp break;
4708 da76fce2 2020-02-24 stsp case 'n':
4709 da76fce2 2020-02-24 stsp do_update = 0;
4710 4e759de4 2019-06-26 stsp break;
4711 4e759de4 2019-06-26 stsp default:
4712 4e759de4 2019-06-26 stsp usage_branch();
4713 4e759de4 2019-06-26 stsp /* NOTREACHED */
4714 4e759de4 2019-06-26 stsp }
4715 4e759de4 2019-06-26 stsp }
4716 4e759de4 2019-06-26 stsp
4717 4e759de4 2019-06-26 stsp if (do_list && delref)
4718 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
4719 4e759de4 2019-06-26 stsp
4720 4e759de4 2019-06-26 stsp argc -= optind;
4721 4e759de4 2019-06-26 stsp argv += optind;
4722 ad89fa31 2019-10-04 stsp
4723 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4724 ad89fa31 2019-10-04 stsp do_show = 1;
4725 4e759de4 2019-06-26 stsp
4726 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4727 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4728 a74f7e83 2019-11-10 stsp
4729 4e759de4 2019-06-26 stsp if (do_list || delref) {
4730 4e759de4 2019-06-26 stsp if (argc > 0)
4731 4e759de4 2019-06-26 stsp usage_branch();
4732 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4733 4e759de4 2019-06-26 stsp usage_branch();
4734 4e759de4 2019-06-26 stsp
4735 4e759de4 2019-06-26 stsp #ifndef PROFILE
4736 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4737 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4738 4e759de4 2019-06-26 stsp NULL) == -1)
4739 4e759de4 2019-06-26 stsp err(1, "pledge");
4740 4e759de4 2019-06-26 stsp } else {
4741 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4742 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4743 4e759de4 2019-06-26 stsp err(1, "pledge");
4744 4e759de4 2019-06-26 stsp }
4745 4e759de4 2019-06-26 stsp #endif
4746 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4747 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4748 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4749 4e759de4 2019-06-26 stsp goto done;
4750 4e759de4 2019-06-26 stsp }
4751 4e759de4 2019-06-26 stsp
4752 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4753 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4754 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4755 4e759de4 2019-06-26 stsp goto done;
4756 4e759de4 2019-06-26 stsp else
4757 4e759de4 2019-06-26 stsp error = NULL;
4758 4e759de4 2019-06-26 stsp if (worktree) {
4759 4e759de4 2019-06-26 stsp repo_path =
4760 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4761 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4762 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4763 4e759de4 2019-06-26 stsp if (error)
4764 4e759de4 2019-06-26 stsp goto done;
4765 4e759de4 2019-06-26 stsp } else {
4766 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4767 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4768 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4769 4e759de4 2019-06-26 stsp goto done;
4770 4e759de4 2019-06-26 stsp }
4771 4e759de4 2019-06-26 stsp }
4772 4e759de4 2019-06-26 stsp }
4773 4e759de4 2019-06-26 stsp
4774 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4775 4e759de4 2019-06-26 stsp if (error != NULL)
4776 4e759de4 2019-06-26 stsp goto done;
4777 4e759de4 2019-06-26 stsp
4778 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4779 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4780 4e759de4 2019-06-26 stsp if (error)
4781 4e759de4 2019-06-26 stsp goto done;
4782 4e759de4 2019-06-26 stsp
4783 ad89fa31 2019-10-04 stsp if (do_show)
4784 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4785 ad89fa31 2019-10-04 stsp else if (do_list)
4786 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4787 4e759de4 2019-06-26 stsp else if (delref)
4788 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4789 4e759de4 2019-06-26 stsp else {
4790 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4791 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4792 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4793 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4794 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4795 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4796 a74f7e83 2019-11-10 stsp if (error)
4797 a74f7e83 2019-11-10 stsp goto done;
4798 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4799 da76fce2 2020-02-24 stsp if (error)
4800 da76fce2 2020-02-24 stsp goto done;
4801 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4802 da76fce2 2020-02-24 stsp int did_something = 0;
4803 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4804 da76fce2 2020-02-24 stsp
4805 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4806 da76fce2 2020-02-24 stsp if (error)
4807 da76fce2 2020-02-24 stsp goto done;
4808 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4809 da76fce2 2020-02-24 stsp worktree);
4810 da76fce2 2020-02-24 stsp if (error)
4811 da76fce2 2020-02-24 stsp goto done;
4812 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4813 da76fce2 2020-02-24 stsp == -1) {
4814 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4815 da76fce2 2020-02-24 stsp goto done;
4816 da76fce2 2020-02-24 stsp }
4817 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4818 da76fce2 2020-02-24 stsp free(branch_refname);
4819 da76fce2 2020-02-24 stsp if (error)
4820 da76fce2 2020-02-24 stsp goto done;
4821 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4822 da76fce2 2020-02-24 stsp repo);
4823 da76fce2 2020-02-24 stsp if (error)
4824 da76fce2 2020-02-24 stsp goto done;
4825 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4826 da76fce2 2020-02-24 stsp commit_id);
4827 da76fce2 2020-02-24 stsp if (error)
4828 da76fce2 2020-02-24 stsp goto done;
4829 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4830 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4831 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4832 da76fce2 2020-02-24 stsp if (error)
4833 da76fce2 2020-02-24 stsp goto done;
4834 da76fce2 2020-02-24 stsp if (did_something)
4835 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4836 da76fce2 2020-02-24 stsp }
4837 4e759de4 2019-06-26 stsp }
4838 4e759de4 2019-06-26 stsp done:
4839 da76fce2 2020-02-24 stsp if (ref)
4840 da76fce2 2020-02-24 stsp got_ref_close(ref);
4841 d0eebce4 2019-03-11 stsp if (repo)
4842 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4843 d0eebce4 2019-03-11 stsp if (worktree)
4844 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4845 d0eebce4 2019-03-11 stsp free(cwd);
4846 d0eebce4 2019-03-11 stsp free(repo_path);
4847 da76fce2 2020-02-24 stsp free(commit_id);
4848 da76fce2 2020-02-24 stsp free(commit_id_str);
4849 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4850 da76fce2 2020-02-24 stsp free((char *)pe->path);
4851 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4852 d00136be 2019-03-26 stsp return error;
4853 d00136be 2019-03-26 stsp }
4854 d00136be 2019-03-26 stsp
4855 8e7bd50a 2019-08-22 stsp
4856 d00136be 2019-03-26 stsp __dead static void
4857 8e7bd50a 2019-08-22 stsp usage_tag(void)
4858 8e7bd50a 2019-08-22 stsp {
4859 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4860 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4861 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4862 8e7bd50a 2019-08-22 stsp exit(1);
4863 b8bad2ba 2019-08-23 stsp }
4864 b8bad2ba 2019-08-23 stsp
4865 b8bad2ba 2019-08-23 stsp #if 0
4866 b8bad2ba 2019-08-23 stsp static const struct got_error *
4867 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4868 b8bad2ba 2019-08-23 stsp {
4869 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4870 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4871 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4872 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4873 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4874 b8bad2ba 2019-08-23 stsp
4875 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4876 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4877 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4878 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4879 b8bad2ba 2019-08-23 stsp if (err)
4880 b8bad2ba 2019-08-23 stsp return err;
4881 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4882 b8bad2ba 2019-08-23 stsp continue;
4883 b8bad2ba 2019-08-23 stsp } else {
4884 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4885 b8bad2ba 2019-08-23 stsp if (err)
4886 b8bad2ba 2019-08-23 stsp break;
4887 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4888 b8bad2ba 2019-08-23 stsp free(re_id);
4889 b8bad2ba 2019-08-23 stsp if (err)
4890 b8bad2ba 2019-08-23 stsp break;
4891 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4892 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4893 b8bad2ba 2019-08-23 stsp }
4894 b8bad2ba 2019-08-23 stsp
4895 b8bad2ba 2019-08-23 stsp while (se) {
4896 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4897 b8bad2ba 2019-08-23 stsp if (err)
4898 b8bad2ba 2019-08-23 stsp break;
4899 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4900 b8bad2ba 2019-08-23 stsp free(se_id);
4901 b8bad2ba 2019-08-23 stsp if (err)
4902 b8bad2ba 2019-08-23 stsp break;
4903 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4904 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4905 b8bad2ba 2019-08-23 stsp
4906 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4907 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4908 b8bad2ba 2019-08-23 stsp if (err)
4909 b8bad2ba 2019-08-23 stsp return err;
4910 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4911 b8bad2ba 2019-08-23 stsp break;
4912 b8bad2ba 2019-08-23 stsp }
4913 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4914 b8bad2ba 2019-08-23 stsp continue;
4915 b8bad2ba 2019-08-23 stsp }
4916 b8bad2ba 2019-08-23 stsp }
4917 b8bad2ba 2019-08-23 stsp done:
4918 b8bad2ba 2019-08-23 stsp return err;
4919 8e7bd50a 2019-08-22 stsp }
4920 b8bad2ba 2019-08-23 stsp #endif
4921 b8bad2ba 2019-08-23 stsp
4922 b8bad2ba 2019-08-23 stsp static const struct got_error *
4923 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4924 8e7bd50a 2019-08-22 stsp {
4925 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4926 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4927 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4928 8e7bd50a 2019-08-22 stsp
4929 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4930 8e7bd50a 2019-08-22 stsp
4931 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4932 8e7bd50a 2019-08-22 stsp if (err)
4933 8e7bd50a 2019-08-22 stsp return err;
4934 8e7bd50a 2019-08-22 stsp
4935 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4936 8e7bd50a 2019-08-22 stsp const char *refname;
4937 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4938 8e7bd50a 2019-08-22 stsp char datebuf[26];
4939 d4efa91b 2020-01-14 stsp const char *tagger;
4940 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4941 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4942 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4943 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4944 8e7bd50a 2019-08-22 stsp
4945 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4946 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4947 8e7bd50a 2019-08-22 stsp continue;
4948 8e7bd50a 2019-08-22 stsp refname += 10;
4949 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4950 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4951 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4952 8e7bd50a 2019-08-22 stsp break;
4953 8e7bd50a 2019-08-22 stsp }
4954 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4955 8e7bd50a 2019-08-22 stsp free(refstr);
4956 8e7bd50a 2019-08-22 stsp
4957 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4958 8e7bd50a 2019-08-22 stsp if (err)
4959 8e7bd50a 2019-08-22 stsp break;
4960 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4961 d4efa91b 2020-01-14 stsp if (err) {
4962 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4963 d4efa91b 2020-01-14 stsp free(id);
4964 d4efa91b 2020-01-14 stsp break;
4965 d4efa91b 2020-01-14 stsp }
4966 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4967 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4968 d4efa91b 2020-01-14 stsp if (err) {
4969 d4efa91b 2020-01-14 stsp free(id);
4970 d4efa91b 2020-01-14 stsp break;
4971 d4efa91b 2020-01-14 stsp }
4972 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4973 d4efa91b 2020-01-14 stsp tagger_time =
4974 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4975 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4976 d4efa91b 2020-01-14 stsp free(id);
4977 d4efa91b 2020-01-14 stsp if (err)
4978 d4efa91b 2020-01-14 stsp break;
4979 d4efa91b 2020-01-14 stsp } else {
4980 d4efa91b 2020-01-14 stsp free(id);
4981 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4982 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4983 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4984 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4985 d4efa91b 2020-01-14 stsp if (err)
4986 d4efa91b 2020-01-14 stsp break;
4987 d4efa91b 2020-01-14 stsp }
4988 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4989 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4990 2417344c 2019-08-23 stsp if (datestr)
4991 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4992 d4efa91b 2020-01-14 stsp if (commit)
4993 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4994 d4efa91b 2020-01-14 stsp else {
4995 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4996 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4997 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4998 d4efa91b 2020-01-14 stsp id_str);
4999 d4efa91b 2020-01-14 stsp break;
5000 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5001 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5002 d4efa91b 2020-01-14 stsp id_str);
5003 d4efa91b 2020-01-14 stsp break;
5004 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5005 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5006 d4efa91b 2020-01-14 stsp id_str);
5007 d4efa91b 2020-01-14 stsp break;
5008 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5009 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5010 d4efa91b 2020-01-14 stsp id_str);
5011 d4efa91b 2020-01-14 stsp break;
5012 d4efa91b 2020-01-14 stsp default:
5013 d4efa91b 2020-01-14 stsp break;
5014 d4efa91b 2020-01-14 stsp }
5015 8e7bd50a 2019-08-22 stsp }
5016 8e7bd50a 2019-08-22 stsp free(id_str);
5017 d4efa91b 2020-01-14 stsp if (commit) {
5018 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5019 d4efa91b 2020-01-14 stsp if (err)
5020 d4efa91b 2020-01-14 stsp break;
5021 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5022 d4efa91b 2020-01-14 stsp } else {
5023 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5024 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5025 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5026 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5027 d4efa91b 2020-01-14 stsp break;
5028 d4efa91b 2020-01-14 stsp }
5029 8e7bd50a 2019-08-22 stsp }
5030 8e7bd50a 2019-08-22 stsp
5031 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5032 8e7bd50a 2019-08-22 stsp do {
5033 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5034 8e7bd50a 2019-08-22 stsp if (line)
5035 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5036 8e7bd50a 2019-08-22 stsp } while (line);
5037 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5038 8e7bd50a 2019-08-22 stsp }
5039 8e7bd50a 2019-08-22 stsp
5040 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5041 8e7bd50a 2019-08-22 stsp return NULL;
5042 8e7bd50a 2019-08-22 stsp }
5043 8e7bd50a 2019-08-22 stsp
5044 8e7bd50a 2019-08-22 stsp static const struct got_error *
5045 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5046 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5047 8e7bd50a 2019-08-22 stsp {
5048 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5049 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5050 f372d5cd 2019-10-21 stsp char *editor = NULL;
5051 8e7bd50a 2019-08-22 stsp int fd = -1;
5052 8e7bd50a 2019-08-22 stsp
5053 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5054 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5055 8e7bd50a 2019-08-22 stsp goto done;
5056 8e7bd50a 2019-08-22 stsp }
5057 8e7bd50a 2019-08-22 stsp
5058 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5059 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5060 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5061 8e7bd50a 2019-08-22 stsp goto done;
5062 8e7bd50a 2019-08-22 stsp }
5063 8e7bd50a 2019-08-22 stsp
5064 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5065 8e7bd50a 2019-08-22 stsp if (err)
5066 8e7bd50a 2019-08-22 stsp goto done;
5067 8e7bd50a 2019-08-22 stsp
5068 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5069 8e7bd50a 2019-08-22 stsp close(fd);
5070 8e7bd50a 2019-08-22 stsp
5071 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5072 8e7bd50a 2019-08-22 stsp if (err)
5073 8e7bd50a 2019-08-22 stsp goto done;
5074 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5075 8e7bd50a 2019-08-22 stsp done:
5076 8e7bd50a 2019-08-22 stsp free(initial_content);
5077 8e7bd50a 2019-08-22 stsp free(template);
5078 8e7bd50a 2019-08-22 stsp free(editor);
5079 8e7bd50a 2019-08-22 stsp
5080 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5081 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5082 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5083 8e7bd50a 2019-08-22 stsp if (err) {
5084 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5085 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5086 8e7bd50a 2019-08-22 stsp }
5087 8e7bd50a 2019-08-22 stsp }
5088 8e7bd50a 2019-08-22 stsp return err;
5089 8e7bd50a 2019-08-22 stsp }
5090 8e7bd50a 2019-08-22 stsp
5091 8e7bd50a 2019-08-22 stsp static const struct got_error *
5092 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5093 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5094 8e7bd50a 2019-08-22 stsp {
5095 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5096 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5097 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5098 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5099 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5100 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5101 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5102 8e7bd50a 2019-08-22 stsp
5103 8e7bd50a 2019-08-22 stsp /*
5104 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5105 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5106 8e7bd50a 2019-08-22 stsp * an unintended typo.
5107 8e7bd50a 2019-08-22 stsp */
5108 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5109 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5110 8e7bd50a 2019-08-22 stsp
5111 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5112 8e7bd50a 2019-08-22 stsp if (err)
5113 8e7bd50a 2019-08-22 stsp return err;
5114 8e7bd50a 2019-08-22 stsp
5115 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5116 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5117 8e7bd50a 2019-08-22 stsp if (err)
5118 8e7bd50a 2019-08-22 stsp goto done;
5119 8e7bd50a 2019-08-22 stsp
5120 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5121 8e7bd50a 2019-08-22 stsp if (err)
5122 8e7bd50a 2019-08-22 stsp goto done;
5123 8e7bd50a 2019-08-22 stsp
5124 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5125 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5126 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5127 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5128 8e7bd50a 2019-08-22 stsp goto done;
5129 8e7bd50a 2019-08-22 stsp }
5130 8e7bd50a 2019-08-22 stsp tag_name += 10;
5131 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5132 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5133 8e7bd50a 2019-08-22 stsp goto done;
5134 8e7bd50a 2019-08-22 stsp }
5135 8e7bd50a 2019-08-22 stsp
5136 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5137 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5138 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5139 8e7bd50a 2019-08-22 stsp goto done;
5140 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5141 8e7bd50a 2019-08-22 stsp goto done;
5142 8e7bd50a 2019-08-22 stsp
5143 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5144 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5145 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5146 f372d5cd 2019-10-21 stsp if (err) {
5147 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5148 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5149 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5150 8e7bd50a 2019-08-22 stsp goto done;
5151 f372d5cd 2019-10-21 stsp }
5152 8e7bd50a 2019-08-22 stsp }
5153 8e7bd50a 2019-08-22 stsp
5154 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5155 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5156 f372d5cd 2019-10-21 stsp if (err) {
5157 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5158 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5159 8e7bd50a 2019-08-22 stsp goto done;
5160 f372d5cd 2019-10-21 stsp }
5161 8e7bd50a 2019-08-22 stsp
5162 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5163 f372d5cd 2019-10-21 stsp if (err) {
5164 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5165 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5166 8e7bd50a 2019-08-22 stsp goto done;
5167 f372d5cd 2019-10-21 stsp }
5168 8e7bd50a 2019-08-22 stsp
5169 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5170 f372d5cd 2019-10-21 stsp if (err) {
5171 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5172 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5173 f372d5cd 2019-10-21 stsp goto done;
5174 f372d5cd 2019-10-21 stsp }
5175 8e7bd50a 2019-08-22 stsp
5176 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5177 f372d5cd 2019-10-21 stsp if (err) {
5178 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5179 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5180 f372d5cd 2019-10-21 stsp goto done;
5181 8e7bd50a 2019-08-22 stsp }
5182 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5183 8e7bd50a 2019-08-22 stsp done:
5184 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5185 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5186 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5187 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5188 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5189 f372d5cd 2019-10-21 stsp free(tag_id_str);
5190 8e7bd50a 2019-08-22 stsp if (ref)
5191 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5192 8e7bd50a 2019-08-22 stsp free(commit_id);
5193 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5194 8e7bd50a 2019-08-22 stsp free(refname);
5195 8e7bd50a 2019-08-22 stsp free(tagmsg);
5196 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5197 aba9c984 2019-09-08 stsp free(tagger);
5198 8e7bd50a 2019-08-22 stsp return err;
5199 8e7bd50a 2019-08-22 stsp }
5200 8e7bd50a 2019-08-22 stsp
5201 8e7bd50a 2019-08-22 stsp static const struct got_error *
5202 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5203 8e7bd50a 2019-08-22 stsp {
5204 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5205 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5206 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5207 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5208 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5209 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5210 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5211 8e7bd50a 2019-08-22 stsp
5212 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5213 8e7bd50a 2019-08-22 stsp switch (ch) {
5214 80106605 2020-02-24 stsp case 'c':
5215 80106605 2020-02-24 stsp commit_id_arg = optarg;
5216 80106605 2020-02-24 stsp break;
5217 8e7bd50a 2019-08-22 stsp case 'm':
5218 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5219 8e7bd50a 2019-08-22 stsp break;
5220 8e7bd50a 2019-08-22 stsp case 'r':
5221 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5222 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5223 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5224 9ba1d308 2019-10-21 stsp optarg);
5225 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5226 8e7bd50a 2019-08-22 stsp break;
5227 8e7bd50a 2019-08-22 stsp case 'l':
5228 8e7bd50a 2019-08-22 stsp do_list = 1;
5229 8e7bd50a 2019-08-22 stsp break;
5230 8e7bd50a 2019-08-22 stsp default:
5231 8e7bd50a 2019-08-22 stsp usage_tag();
5232 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5233 8e7bd50a 2019-08-22 stsp }
5234 8e7bd50a 2019-08-22 stsp }
5235 8e7bd50a 2019-08-22 stsp
5236 8e7bd50a 2019-08-22 stsp argc -= optind;
5237 8e7bd50a 2019-08-22 stsp argv += optind;
5238 8e7bd50a 2019-08-22 stsp
5239 8e7bd50a 2019-08-22 stsp if (do_list) {
5240 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5241 775ce909 2020-03-22 stsp errx(1,
5242 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5243 8e7bd50a 2019-08-22 stsp if (tagmsg)
5244 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5245 8e7bd50a 2019-08-22 stsp if (argc > 0)
5246 8e7bd50a 2019-08-22 stsp usage_tag();
5247 80106605 2020-02-24 stsp } else if (argc != 1)
5248 8e7bd50a 2019-08-22 stsp usage_tag();
5249 80106605 2020-02-24 stsp
5250 a2887370 2019-08-23 stsp tag_name = argv[0];
5251 8e7bd50a 2019-08-22 stsp
5252 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5253 8e7bd50a 2019-08-22 stsp if (do_list) {
5254 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5255 8e7bd50a 2019-08-22 stsp NULL) == -1)
5256 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5257 8e7bd50a 2019-08-22 stsp } else {
5258 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5259 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5260 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5261 8e7bd50a 2019-08-22 stsp }
5262 8e7bd50a 2019-08-22 stsp #endif
5263 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5264 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5265 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5266 8e7bd50a 2019-08-22 stsp goto done;
5267 8e7bd50a 2019-08-22 stsp }
5268 8e7bd50a 2019-08-22 stsp
5269 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5270 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5271 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5272 8e7bd50a 2019-08-22 stsp goto done;
5273 8e7bd50a 2019-08-22 stsp else
5274 8e7bd50a 2019-08-22 stsp error = NULL;
5275 8e7bd50a 2019-08-22 stsp if (worktree) {
5276 8e7bd50a 2019-08-22 stsp repo_path =
5277 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5278 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5279 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5280 8e7bd50a 2019-08-22 stsp if (error)
5281 8e7bd50a 2019-08-22 stsp goto done;
5282 8e7bd50a 2019-08-22 stsp } else {
5283 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5284 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5285 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5286 8e7bd50a 2019-08-22 stsp goto done;
5287 8e7bd50a 2019-08-22 stsp }
5288 8e7bd50a 2019-08-22 stsp }
5289 8e7bd50a 2019-08-22 stsp }
5290 8e7bd50a 2019-08-22 stsp
5291 8e7bd50a 2019-08-22 stsp if (do_list) {
5292 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5293 c9956ddf 2019-09-08 stsp if (error != NULL)
5294 c9956ddf 2019-09-08 stsp goto done;
5295 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5296 8e7bd50a 2019-08-22 stsp if (error)
5297 8e7bd50a 2019-08-22 stsp goto done;
5298 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5299 8e7bd50a 2019-08-22 stsp } else {
5300 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5301 c9956ddf 2019-09-08 stsp if (error)
5302 c9956ddf 2019-09-08 stsp goto done;
5303 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5304 c9956ddf 2019-09-08 stsp if (error != NULL)
5305 c9956ddf 2019-09-08 stsp goto done;
5306 c9956ddf 2019-09-08 stsp
5307 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5308 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5309 8e7bd50a 2019-08-22 stsp if (error)
5310 8e7bd50a 2019-08-22 stsp goto done;
5311 8e7bd50a 2019-08-22 stsp }
5312 8e7bd50a 2019-08-22 stsp
5313 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5314 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5315 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5316 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5317 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5318 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5319 8e7bd50a 2019-08-22 stsp if (error)
5320 8e7bd50a 2019-08-22 stsp goto done;
5321 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5322 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5323 8e7bd50a 2019-08-22 stsp if (error)
5324 8e7bd50a 2019-08-22 stsp goto done;
5325 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5326 8e7bd50a 2019-08-22 stsp free(commit_id);
5327 8e7bd50a 2019-08-22 stsp if (error)
5328 8e7bd50a 2019-08-22 stsp goto done;
5329 8e7bd50a 2019-08-22 stsp }
5330 8e7bd50a 2019-08-22 stsp
5331 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5332 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5333 8e7bd50a 2019-08-22 stsp }
5334 8e7bd50a 2019-08-22 stsp done:
5335 8e7bd50a 2019-08-22 stsp if (repo)
5336 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5337 8e7bd50a 2019-08-22 stsp if (worktree)
5338 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5339 8e7bd50a 2019-08-22 stsp free(cwd);
5340 8e7bd50a 2019-08-22 stsp free(repo_path);
5341 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5342 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5343 8e7bd50a 2019-08-22 stsp return error;
5344 8e7bd50a 2019-08-22 stsp }
5345 8e7bd50a 2019-08-22 stsp
5346 8e7bd50a 2019-08-22 stsp __dead static void
5347 d00136be 2019-03-26 stsp usage_add(void)
5348 d00136be 2019-03-26 stsp {
5349 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5350 022fae89 2019-12-06 tracey getprogname());
5351 d00136be 2019-03-26 stsp exit(1);
5352 d00136be 2019-03-26 stsp }
5353 d00136be 2019-03-26 stsp
5354 d00136be 2019-03-26 stsp static const struct got_error *
5355 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5356 4e68cba3 2019-11-23 stsp {
5357 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5358 4e68cba3 2019-11-23 stsp path++;
5359 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5360 4e68cba3 2019-11-23 stsp return NULL;
5361 4e68cba3 2019-11-23 stsp }
5362 4e68cba3 2019-11-23 stsp
5363 4e68cba3 2019-11-23 stsp static const struct got_error *
5364 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5365 d00136be 2019-03-26 stsp {
5366 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5367 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5368 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5369 1dd54920 2019-05-11 stsp char *cwd = NULL;
5370 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5371 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5372 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5373 1dd54920 2019-05-11 stsp
5374 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5375 d00136be 2019-03-26 stsp
5376 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5377 d00136be 2019-03-26 stsp switch (ch) {
5378 022fae89 2019-12-06 tracey case 'I':
5379 022fae89 2019-12-06 tracey no_ignores = 1;
5380 022fae89 2019-12-06 tracey break;
5381 4e68cba3 2019-11-23 stsp case 'R':
5382 4e68cba3 2019-11-23 stsp can_recurse = 1;
5383 4e68cba3 2019-11-23 stsp break;
5384 d00136be 2019-03-26 stsp default:
5385 d00136be 2019-03-26 stsp usage_add();
5386 d00136be 2019-03-26 stsp /* NOTREACHED */
5387 d00136be 2019-03-26 stsp }
5388 d00136be 2019-03-26 stsp }
5389 d00136be 2019-03-26 stsp
5390 d00136be 2019-03-26 stsp argc -= optind;
5391 d00136be 2019-03-26 stsp argv += optind;
5392 d00136be 2019-03-26 stsp
5393 43012d58 2019-07-14 stsp #ifndef PROFILE
5394 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5395 43012d58 2019-07-14 stsp NULL) == -1)
5396 43012d58 2019-07-14 stsp err(1, "pledge");
5397 43012d58 2019-07-14 stsp #endif
5398 723c305c 2019-05-11 jcs if (argc < 1)
5399 d00136be 2019-03-26 stsp usage_add();
5400 d00136be 2019-03-26 stsp
5401 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5402 d00136be 2019-03-26 stsp if (cwd == NULL) {
5403 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5404 d00136be 2019-03-26 stsp goto done;
5405 d00136be 2019-03-26 stsp }
5406 723c305c 2019-05-11 jcs
5407 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5408 d00136be 2019-03-26 stsp if (error)
5409 d00136be 2019-03-26 stsp goto done;
5410 d00136be 2019-03-26 stsp
5411 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5412 c9956ddf 2019-09-08 stsp NULL);
5413 031a5338 2019-03-26 stsp if (error != NULL)
5414 031a5338 2019-03-26 stsp goto done;
5415 031a5338 2019-03-26 stsp
5416 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5417 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5418 d00136be 2019-03-26 stsp if (error)
5419 d00136be 2019-03-26 stsp goto done;
5420 d00136be 2019-03-26 stsp
5421 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5422 6d022e97 2019-08-04 stsp if (error)
5423 022fae89 2019-12-06 tracey goto done;
5424 022fae89 2019-12-06 tracey
5425 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5426 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5427 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5428 6d022e97 2019-08-04 stsp goto done;
5429 723c305c 2019-05-11 jcs
5430 022fae89 2019-12-06 tracey }
5431 022fae89 2019-12-06 tracey
5432 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5433 4e68cba3 2019-11-23 stsp char *ondisk_path;
5434 4e68cba3 2019-11-23 stsp struct stat sb;
5435 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5436 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5437 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5438 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5439 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5440 4e68cba3 2019-11-23 stsp goto done;
5441 4e68cba3 2019-11-23 stsp }
5442 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5443 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5444 4e68cba3 2019-11-23 stsp free(ondisk_path);
5445 4e68cba3 2019-11-23 stsp continue;
5446 4e68cba3 2019-11-23 stsp }
5447 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5448 4e68cba3 2019-11-23 stsp ondisk_path);
5449 4e68cba3 2019-11-23 stsp free(ondisk_path);
5450 4e68cba3 2019-11-23 stsp goto done;
5451 4e68cba3 2019-11-23 stsp }
5452 4e68cba3 2019-11-23 stsp free(ondisk_path);
5453 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5454 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5455 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5456 4e68cba3 2019-11-23 stsp goto done;
5457 4e68cba3 2019-11-23 stsp }
5458 4e68cba3 2019-11-23 stsp }
5459 4e68cba3 2019-11-23 stsp }
5460 022fae89 2019-12-06 tracey
5461 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5462 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5463 d00136be 2019-03-26 stsp done:
5464 031a5338 2019-03-26 stsp if (repo)
5465 031a5338 2019-03-26 stsp got_repo_close(repo);
5466 d00136be 2019-03-26 stsp if (worktree)
5467 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5468 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5469 1dd54920 2019-05-11 stsp free((char *)pe->path);
5470 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5471 2ec1f75b 2019-03-26 stsp free(cwd);
5472 2ec1f75b 2019-03-26 stsp return error;
5473 2ec1f75b 2019-03-26 stsp }
5474 2ec1f75b 2019-03-26 stsp
5475 2ec1f75b 2019-03-26 stsp __dead static void
5476 648e4ef7 2019-07-09 stsp usage_remove(void)
5477 2ec1f75b 2019-03-26 stsp {
5478 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5479 f2a9dc41 2019-12-13 tracey getprogname());
5480 2ec1f75b 2019-03-26 stsp exit(1);
5481 2a06fe5f 2019-08-24 stsp }
5482 2a06fe5f 2019-08-24 stsp
5483 2a06fe5f 2019-08-24 stsp static const struct got_error *
5484 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5485 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5486 2a06fe5f 2019-08-24 stsp {
5487 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5488 f2a9dc41 2019-12-13 tracey path++;
5489 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5490 2a06fe5f 2019-08-24 stsp return NULL;
5491 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5492 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5493 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5494 2a06fe5f 2019-08-24 stsp return NULL;
5495 2ec1f75b 2019-03-26 stsp }
5496 2ec1f75b 2019-03-26 stsp
5497 2ec1f75b 2019-03-26 stsp static const struct got_error *
5498 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5499 2ec1f75b 2019-03-26 stsp {
5500 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5501 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5502 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5503 17ed4618 2019-06-02 stsp char *cwd = NULL;
5504 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5505 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5506 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5507 2ec1f75b 2019-03-26 stsp
5508 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5509 17ed4618 2019-06-02 stsp
5510 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5511 2ec1f75b 2019-03-26 stsp switch (ch) {
5512 2ec1f75b 2019-03-26 stsp case 'f':
5513 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5514 2ec1f75b 2019-03-26 stsp break;
5515 70e3e7f5 2019-12-13 tracey case 'k':
5516 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5517 70e3e7f5 2019-12-13 tracey break;
5518 f2a9dc41 2019-12-13 tracey case 'R':
5519 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5520 f2a9dc41 2019-12-13 tracey break;
5521 2ec1f75b 2019-03-26 stsp default:
5522 f2a9dc41 2019-12-13 tracey usage_remove();
5523 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5524 2ec1f75b 2019-03-26 stsp }
5525 2ec1f75b 2019-03-26 stsp }
5526 2ec1f75b 2019-03-26 stsp
5527 2ec1f75b 2019-03-26 stsp argc -= optind;
5528 2ec1f75b 2019-03-26 stsp argv += optind;
5529 2ec1f75b 2019-03-26 stsp
5530 43012d58 2019-07-14 stsp #ifndef PROFILE
5531 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5532 43012d58 2019-07-14 stsp NULL) == -1)
5533 43012d58 2019-07-14 stsp err(1, "pledge");
5534 43012d58 2019-07-14 stsp #endif
5535 17ed4618 2019-06-02 stsp if (argc < 1)
5536 648e4ef7 2019-07-09 stsp usage_remove();
5537 2ec1f75b 2019-03-26 stsp
5538 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5539 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5540 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5541 2ec1f75b 2019-03-26 stsp goto done;
5542 2ec1f75b 2019-03-26 stsp }
5543 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5544 2ec1f75b 2019-03-26 stsp if (error)
5545 2ec1f75b 2019-03-26 stsp goto done;
5546 2ec1f75b 2019-03-26 stsp
5547 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5548 c9956ddf 2019-09-08 stsp NULL);
5549 2af4a041 2019-05-11 jcs if (error)
5550 2ec1f75b 2019-03-26 stsp goto done;
5551 2ec1f75b 2019-03-26 stsp
5552 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5553 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5554 2ec1f75b 2019-03-26 stsp if (error)
5555 2ec1f75b 2019-03-26 stsp goto done;
5556 2ec1f75b 2019-03-26 stsp
5557 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5558 6d022e97 2019-08-04 stsp if (error)
5559 6d022e97 2019-08-04 stsp goto done;
5560 17ed4618 2019-06-02 stsp
5561 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5562 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5563 f2a9dc41 2019-12-13 tracey struct stat sb;
5564 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5565 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5566 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5567 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5568 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5569 f2a9dc41 2019-12-13 tracey goto done;
5570 f2a9dc41 2019-12-13 tracey }
5571 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5572 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5573 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5574 f2a9dc41 2019-12-13 tracey continue;
5575 f2a9dc41 2019-12-13 tracey }
5576 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5577 f2a9dc41 2019-12-13 tracey ondisk_path);
5578 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5579 f2a9dc41 2019-12-13 tracey goto done;
5580 f2a9dc41 2019-12-13 tracey }
5581 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5582 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5583 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5584 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5585 f2a9dc41 2019-12-13 tracey goto done;
5586 f2a9dc41 2019-12-13 tracey }
5587 f2a9dc41 2019-12-13 tracey }
5588 f2a9dc41 2019-12-13 tracey }
5589 f2a9dc41 2019-12-13 tracey
5590 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5591 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5592 a129376b 2019-03-28 stsp done:
5593 a129376b 2019-03-28 stsp if (repo)
5594 a129376b 2019-03-28 stsp got_repo_close(repo);
5595 a129376b 2019-03-28 stsp if (worktree)
5596 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5597 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5598 17ed4618 2019-06-02 stsp free((char *)pe->path);
5599 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5600 a129376b 2019-03-28 stsp free(cwd);
5601 a129376b 2019-03-28 stsp return error;
5602 a129376b 2019-03-28 stsp }
5603 a129376b 2019-03-28 stsp
5604 a129376b 2019-03-28 stsp __dead static void
5605 a129376b 2019-03-28 stsp usage_revert(void)
5606 a129376b 2019-03-28 stsp {
5607 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5608 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5609 a129376b 2019-03-28 stsp exit(1);
5610 a129376b 2019-03-28 stsp }
5611 a129376b 2019-03-28 stsp
5612 1ee397ad 2019-07-12 stsp static const struct got_error *
5613 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5614 a129376b 2019-03-28 stsp {
5615 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5616 fb9704af 2020-01-27 stsp return NULL;
5617 fb9704af 2020-01-27 stsp
5618 a129376b 2019-03-28 stsp while (path[0] == '/')
5619 a129376b 2019-03-28 stsp path++;
5620 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5621 33aa809d 2019-08-08 stsp return NULL;
5622 33aa809d 2019-08-08 stsp }
5623 33aa809d 2019-08-08 stsp
5624 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5625 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5626 33aa809d 2019-08-08 stsp const char *action;
5627 33aa809d 2019-08-08 stsp };
5628 33aa809d 2019-08-08 stsp
5629 33aa809d 2019-08-08 stsp static const struct got_error *
5630 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5631 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5632 33aa809d 2019-08-08 stsp {
5633 33aa809d 2019-08-08 stsp char *line = NULL;
5634 33aa809d 2019-08-08 stsp size_t linesize = 0;
5635 33aa809d 2019-08-08 stsp ssize_t linelen;
5636 33aa809d 2019-08-08 stsp
5637 33aa809d 2019-08-08 stsp switch (status) {
5638 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5639 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5640 33aa809d 2019-08-08 stsp break;
5641 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5642 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5643 33aa809d 2019-08-08 stsp break;
5644 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5645 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5646 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5647 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5648 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5649 33aa809d 2019-08-08 stsp printf("%s", line);
5650 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5651 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5652 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5653 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5654 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5655 33aa809d 2019-08-08 stsp break;
5656 33aa809d 2019-08-08 stsp default:
5657 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5658 33aa809d 2019-08-08 stsp }
5659 33aa809d 2019-08-08 stsp
5660 33aa809d 2019-08-08 stsp return NULL;
5661 33aa809d 2019-08-08 stsp }
5662 33aa809d 2019-08-08 stsp
5663 33aa809d 2019-08-08 stsp static const struct got_error *
5664 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5665 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5666 33aa809d 2019-08-08 stsp {
5667 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5668 33aa809d 2019-08-08 stsp char *line = NULL;
5669 33aa809d 2019-08-08 stsp size_t linesize = 0;
5670 33aa809d 2019-08-08 stsp ssize_t linelen;
5671 33aa809d 2019-08-08 stsp int resp = ' ';
5672 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5673 33aa809d 2019-08-08 stsp
5674 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5675 33aa809d 2019-08-08 stsp
5676 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5677 33aa809d 2019-08-08 stsp char *nl;
5678 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5679 33aa809d 2019-08-08 stsp a->action);
5680 33aa809d 2019-08-08 stsp if (err)
5681 33aa809d 2019-08-08 stsp return err;
5682 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5683 33aa809d 2019-08-08 stsp if (linelen == -1) {
5684 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5685 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5686 33aa809d 2019-08-08 stsp return NULL;
5687 33aa809d 2019-08-08 stsp }
5688 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5689 33aa809d 2019-08-08 stsp if (nl)
5690 33aa809d 2019-08-08 stsp *nl = '\0';
5691 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5692 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5693 33aa809d 2019-08-08 stsp printf("y\n");
5694 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5695 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5696 33aa809d 2019-08-08 stsp printf("n\n");
5697 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5698 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5699 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5700 33aa809d 2019-08-08 stsp printf("q\n");
5701 33aa809d 2019-08-08 stsp } else
5702 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5703 33aa809d 2019-08-08 stsp free(line);
5704 33aa809d 2019-08-08 stsp return NULL;
5705 33aa809d 2019-08-08 stsp }
5706 33aa809d 2019-08-08 stsp
5707 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5708 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5709 33aa809d 2019-08-08 stsp a->action);
5710 33aa809d 2019-08-08 stsp if (err)
5711 33aa809d 2019-08-08 stsp return err;
5712 33aa809d 2019-08-08 stsp resp = getchar();
5713 33aa809d 2019-08-08 stsp if (resp == '\n')
5714 33aa809d 2019-08-08 stsp resp = getchar();
5715 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5716 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5717 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5718 33aa809d 2019-08-08 stsp resp = ' ';
5719 33aa809d 2019-08-08 stsp }
5720 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5721 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5722 33aa809d 2019-08-08 stsp resp = ' ';
5723 33aa809d 2019-08-08 stsp }
5724 33aa809d 2019-08-08 stsp }
5725 33aa809d 2019-08-08 stsp
5726 33aa809d 2019-08-08 stsp if (resp == 'y')
5727 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5728 33aa809d 2019-08-08 stsp else if (resp == 'n')
5729 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5730 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5731 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5732 33aa809d 2019-08-08 stsp
5733 1ee397ad 2019-07-12 stsp return NULL;
5734 a129376b 2019-03-28 stsp }
5735 a129376b 2019-03-28 stsp
5736 33aa809d 2019-08-08 stsp
5737 a129376b 2019-03-28 stsp static const struct got_error *
5738 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5739 a129376b 2019-03-28 stsp {
5740 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5741 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5742 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5743 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5744 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5745 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5746 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5747 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5748 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5749 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5750 a129376b 2019-03-28 stsp
5751 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5752 e20a8b6f 2019-06-04 stsp
5753 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5754 a129376b 2019-03-28 stsp switch (ch) {
5755 33aa809d 2019-08-08 stsp case 'p':
5756 33aa809d 2019-08-08 stsp pflag = 1;
5757 33aa809d 2019-08-08 stsp break;
5758 33aa809d 2019-08-08 stsp case 'F':
5759 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5760 33aa809d 2019-08-08 stsp break;
5761 0f6d7415 2019-08-08 stsp case 'R':
5762 0f6d7415 2019-08-08 stsp can_recurse = 1;
5763 0f6d7415 2019-08-08 stsp break;
5764 a129376b 2019-03-28 stsp default:
5765 a129376b 2019-03-28 stsp usage_revert();
5766 a129376b 2019-03-28 stsp /* NOTREACHED */
5767 a129376b 2019-03-28 stsp }
5768 a129376b 2019-03-28 stsp }
5769 a129376b 2019-03-28 stsp
5770 a129376b 2019-03-28 stsp argc -= optind;
5771 a129376b 2019-03-28 stsp argv += optind;
5772 a129376b 2019-03-28 stsp
5773 43012d58 2019-07-14 stsp #ifndef PROFILE
5774 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5775 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5776 43012d58 2019-07-14 stsp err(1, "pledge");
5777 43012d58 2019-07-14 stsp #endif
5778 e20a8b6f 2019-06-04 stsp if (argc < 1)
5779 a129376b 2019-03-28 stsp usage_revert();
5780 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5781 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5782 a129376b 2019-03-28 stsp
5783 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5784 a129376b 2019-03-28 stsp if (cwd == NULL) {
5785 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5786 a129376b 2019-03-28 stsp goto done;
5787 a129376b 2019-03-28 stsp }
5788 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5789 2ec1f75b 2019-03-26 stsp if (error)
5790 2ec1f75b 2019-03-26 stsp goto done;
5791 a129376b 2019-03-28 stsp
5792 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5793 c9956ddf 2019-09-08 stsp NULL);
5794 a129376b 2019-03-28 stsp if (error != NULL)
5795 a129376b 2019-03-28 stsp goto done;
5796 a129376b 2019-03-28 stsp
5797 33aa809d 2019-08-08 stsp if (patch_script_path) {
5798 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5799 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5800 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5801 33aa809d 2019-08-08 stsp patch_script_path);
5802 33aa809d 2019-08-08 stsp goto done;
5803 33aa809d 2019-08-08 stsp }
5804 33aa809d 2019-08-08 stsp }
5805 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5806 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5807 a129376b 2019-03-28 stsp if (error)
5808 a129376b 2019-03-28 stsp goto done;
5809 a129376b 2019-03-28 stsp
5810 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5811 6d022e97 2019-08-04 stsp if (error)
5812 6d022e97 2019-08-04 stsp goto done;
5813 00db391e 2019-08-03 stsp
5814 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5815 0f6d7415 2019-08-08 stsp char *ondisk_path;
5816 0f6d7415 2019-08-08 stsp struct stat sb;
5817 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5818 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5819 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5820 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5821 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5822 0f6d7415 2019-08-08 stsp goto done;
5823 0f6d7415 2019-08-08 stsp }
5824 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5825 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5826 0f6d7415 2019-08-08 stsp free(ondisk_path);
5827 0f6d7415 2019-08-08 stsp continue;
5828 0f6d7415 2019-08-08 stsp }
5829 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5830 0f6d7415 2019-08-08 stsp ondisk_path);
5831 0f6d7415 2019-08-08 stsp free(ondisk_path);
5832 0f6d7415 2019-08-08 stsp goto done;
5833 0f6d7415 2019-08-08 stsp }
5834 0f6d7415 2019-08-08 stsp free(ondisk_path);
5835 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5836 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5837 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5838 0f6d7415 2019-08-08 stsp goto done;
5839 0f6d7415 2019-08-08 stsp }
5840 0f6d7415 2019-08-08 stsp }
5841 0f6d7415 2019-08-08 stsp }
5842 0f6d7415 2019-08-08 stsp
5843 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5844 33aa809d 2019-08-08 stsp cpa.action = "revert";
5845 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5846 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5847 2ec1f75b 2019-03-26 stsp done:
5848 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5849 33aa809d 2019-08-08 stsp error == NULL)
5850 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5851 2ec1f75b 2019-03-26 stsp if (repo)
5852 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5853 2ec1f75b 2019-03-26 stsp if (worktree)
5854 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5855 2ec1f75b 2019-03-26 stsp free(path);
5856 d00136be 2019-03-26 stsp free(cwd);
5857 6bad629b 2019-02-04 stsp return error;
5858 c4296144 2019-05-09 stsp }
5859 c4296144 2019-05-09 stsp
5860 c4296144 2019-05-09 stsp __dead static void
5861 c4296144 2019-05-09 stsp usage_commit(void)
5862 c4296144 2019-05-09 stsp {
5863 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5864 5c1e53bc 2019-07-28 stsp getprogname());
5865 c4296144 2019-05-09 stsp exit(1);
5866 33ad4cbe 2019-05-12 jcs }
5867 33ad4cbe 2019-05-12 jcs
5868 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5869 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5870 e2ba3d07 2019-05-13 stsp const char *editor;
5871 e0870e44 2019-05-13 stsp const char *worktree_path;
5872 76d98825 2019-06-03 stsp const char *branch_name;
5873 314a6357 2019-05-13 stsp const char *repo_path;
5874 e0870e44 2019-05-13 stsp char *logmsg_path;
5875 e2ba3d07 2019-05-13 stsp
5876 e2ba3d07 2019-05-13 stsp };
5877 e0870e44 2019-05-13 stsp
5878 33ad4cbe 2019-05-12 jcs static const struct got_error *
5879 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5880 33ad4cbe 2019-05-12 jcs void *arg)
5881 33ad4cbe 2019-05-12 jcs {
5882 76d98825 2019-06-03 stsp char *initial_content = NULL;
5883 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5884 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5885 e0870e44 2019-05-13 stsp char *template = NULL;
5886 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5887 3ce1b845 2019-07-15 stsp int fd;
5888 33ad4cbe 2019-05-12 jcs size_t len;
5889 33ad4cbe 2019-05-12 jcs
5890 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5891 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5892 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5893 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5894 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5895 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5896 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5897 33ad4cbe 2019-05-12 jcs return NULL;
5898 33ad4cbe 2019-05-12 jcs }
5899 33ad4cbe 2019-05-12 jcs
5900 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5901 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5902 76d98825 2019-06-03 stsp
5903 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5904 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5905 76d98825 2019-06-03 stsp a->branch_name) == -1)
5906 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5907 e0870e44 2019-05-13 stsp
5908 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5909 793c30b5 2019-05-13 stsp if (err)
5910 e0870e44 2019-05-13 stsp goto done;
5911 33ad4cbe 2019-05-12 jcs
5912 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5913 33ad4cbe 2019-05-12 jcs
5914 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5915 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5916 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5917 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5918 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5919 33ad4cbe 2019-05-12 jcs }
5920 33ad4cbe 2019-05-12 jcs close(fd);
5921 33ad4cbe 2019-05-12 jcs
5922 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5923 33ad4cbe 2019-05-12 jcs done:
5924 76d98825 2019-06-03 stsp free(initial_content);
5925 e0870e44 2019-05-13 stsp free(template);
5926 314a6357 2019-05-13 stsp
5927 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5928 3ce1b845 2019-07-15 stsp if (err == NULL) {
5929 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5930 3ce1b845 2019-07-15 stsp if (err) {
5931 3ce1b845 2019-07-15 stsp free(*logmsg);
5932 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5933 3ce1b845 2019-07-15 stsp }
5934 3ce1b845 2019-07-15 stsp }
5935 33ad4cbe 2019-05-12 jcs return err;
5936 6bad629b 2019-02-04 stsp }
5937 c4296144 2019-05-09 stsp
5938 c4296144 2019-05-09 stsp static const struct got_error *
5939 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5940 c4296144 2019-05-09 stsp {
5941 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5942 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5943 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5944 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5945 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5946 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5947 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5948 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5949 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5950 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5951 5c1e53bc 2019-07-28 stsp
5952 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5953 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5954 c4296144 2019-05-09 stsp
5955 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5956 c4296144 2019-05-09 stsp switch (ch) {
5957 c4296144 2019-05-09 stsp case 'm':
5958 c4296144 2019-05-09 stsp logmsg = optarg;
5959 c4296144 2019-05-09 stsp break;
5960 c4296144 2019-05-09 stsp default:
5961 c4296144 2019-05-09 stsp usage_commit();
5962 c4296144 2019-05-09 stsp /* NOTREACHED */
5963 c4296144 2019-05-09 stsp }
5964 c4296144 2019-05-09 stsp }
5965 c4296144 2019-05-09 stsp
5966 c4296144 2019-05-09 stsp argc -= optind;
5967 c4296144 2019-05-09 stsp argv += optind;
5968 c4296144 2019-05-09 stsp
5969 43012d58 2019-07-14 stsp #ifndef PROFILE
5970 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5971 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5972 43012d58 2019-07-14 stsp err(1, "pledge");
5973 43012d58 2019-07-14 stsp #endif
5974 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5975 c4296144 2019-05-09 stsp if (cwd == NULL) {
5976 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5977 c4296144 2019-05-09 stsp goto done;
5978 c4296144 2019-05-09 stsp }
5979 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5980 7d5807f4 2019-07-11 stsp if (error)
5981 7d5807f4 2019-07-11 stsp goto done;
5982 7d5807f4 2019-07-11 stsp
5983 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5984 c4296144 2019-05-09 stsp if (error)
5985 a698f62e 2019-07-25 stsp goto done;
5986 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5987 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5988 c4296144 2019-05-09 stsp goto done;
5989 a698f62e 2019-07-25 stsp }
5990 5c1e53bc 2019-07-28 stsp
5991 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5992 916f288c 2019-07-30 stsp worktree);
5993 5c1e53bc 2019-07-28 stsp if (error)
5994 5c1e53bc 2019-07-28 stsp goto done;
5995 c4296144 2019-05-09 stsp
5996 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5997 c9956ddf 2019-09-08 stsp if (error)
5998 c9956ddf 2019-09-08 stsp goto done;
5999 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6000 c9956ddf 2019-09-08 stsp gitconfig_path);
6001 c4296144 2019-05-09 stsp if (error != NULL)
6002 c4296144 2019-05-09 stsp goto done;
6003 c4296144 2019-05-09 stsp
6004 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6005 aba9c984 2019-09-08 stsp if (error)
6006 aba9c984 2019-09-08 stsp return error;
6007 aba9c984 2019-09-08 stsp
6008 314a6357 2019-05-13 stsp /*
6009 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6010 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6011 314a6357 2019-05-13 stsp */
6012 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6013 314a6357 2019-05-13 stsp error = get_editor(&editor);
6014 314a6357 2019-05-13 stsp else
6015 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6016 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6017 c4296144 2019-05-09 stsp if (error)
6018 c4296144 2019-05-09 stsp goto done;
6019 c4296144 2019-05-09 stsp
6020 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6021 087fb88c 2019-08-04 stsp if (error)
6022 087fb88c 2019-08-04 stsp goto done;
6023 087fb88c 2019-08-04 stsp
6024 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6025 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6026 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6027 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6028 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6029 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6030 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6031 916f288c 2019-07-30 stsp goto done;
6032 916f288c 2019-07-30 stsp }
6033 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6034 916f288c 2019-07-30 stsp }
6035 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6036 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6037 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6038 e0870e44 2019-05-13 stsp if (error) {
6039 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6040 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6041 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6042 c4296144 2019-05-09 stsp goto done;
6043 e0870e44 2019-05-13 stsp }
6044 c4296144 2019-05-09 stsp
6045 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6046 c4296144 2019-05-09 stsp if (error)
6047 c4296144 2019-05-09 stsp goto done;
6048 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6049 c4296144 2019-05-09 stsp done:
6050 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6051 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6052 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6053 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6054 7266f21f 2019-10-21 stsp error == NULL)
6055 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6056 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6057 c4296144 2019-05-09 stsp if (repo)
6058 c4296144 2019-05-09 stsp got_repo_close(repo);
6059 c4296144 2019-05-09 stsp if (worktree)
6060 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6061 c4296144 2019-05-09 stsp free(cwd);
6062 c4296144 2019-05-09 stsp free(id_str);
6063 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6064 e2ba3d07 2019-05-13 stsp free(editor);
6065 aba9c984 2019-09-08 stsp free(author);
6066 234035bc 2019-06-01 stsp return error;
6067 234035bc 2019-06-01 stsp }
6068 234035bc 2019-06-01 stsp
6069 234035bc 2019-06-01 stsp __dead static void
6070 234035bc 2019-06-01 stsp usage_cherrypick(void)
6071 234035bc 2019-06-01 stsp {
6072 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6073 234035bc 2019-06-01 stsp exit(1);
6074 234035bc 2019-06-01 stsp }
6075 234035bc 2019-06-01 stsp
6076 234035bc 2019-06-01 stsp static const struct got_error *
6077 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6078 234035bc 2019-06-01 stsp {
6079 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6080 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6081 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6082 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6083 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6084 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6085 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6086 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6087 234035bc 2019-06-01 stsp int ch, did_something = 0;
6088 234035bc 2019-06-01 stsp
6089 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6090 234035bc 2019-06-01 stsp switch (ch) {
6091 234035bc 2019-06-01 stsp default:
6092 234035bc 2019-06-01 stsp usage_cherrypick();
6093 234035bc 2019-06-01 stsp /* NOTREACHED */
6094 234035bc 2019-06-01 stsp }
6095 234035bc 2019-06-01 stsp }
6096 234035bc 2019-06-01 stsp
6097 234035bc 2019-06-01 stsp argc -= optind;
6098 234035bc 2019-06-01 stsp argv += optind;
6099 234035bc 2019-06-01 stsp
6100 43012d58 2019-07-14 stsp #ifndef PROFILE
6101 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6102 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6103 43012d58 2019-07-14 stsp err(1, "pledge");
6104 43012d58 2019-07-14 stsp #endif
6105 234035bc 2019-06-01 stsp if (argc != 1)
6106 234035bc 2019-06-01 stsp usage_cherrypick();
6107 234035bc 2019-06-01 stsp
6108 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6109 234035bc 2019-06-01 stsp if (cwd == NULL) {
6110 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6111 234035bc 2019-06-01 stsp goto done;
6112 234035bc 2019-06-01 stsp }
6113 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6114 234035bc 2019-06-01 stsp if (error)
6115 234035bc 2019-06-01 stsp goto done;
6116 234035bc 2019-06-01 stsp
6117 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6118 c9956ddf 2019-09-08 stsp NULL);
6119 234035bc 2019-06-01 stsp if (error != NULL)
6120 234035bc 2019-06-01 stsp goto done;
6121 234035bc 2019-06-01 stsp
6122 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6123 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6124 234035bc 2019-06-01 stsp if (error)
6125 234035bc 2019-06-01 stsp goto done;
6126 234035bc 2019-06-01 stsp
6127 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6128 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6129 234035bc 2019-06-01 stsp if (error != NULL) {
6130 234035bc 2019-06-01 stsp struct got_reference *ref;
6131 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6132 234035bc 2019-06-01 stsp goto done;
6133 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6134 234035bc 2019-06-01 stsp if (error != NULL)
6135 234035bc 2019-06-01 stsp goto done;
6136 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6137 234035bc 2019-06-01 stsp got_ref_close(ref);
6138 234035bc 2019-06-01 stsp if (error != NULL)
6139 234035bc 2019-06-01 stsp goto done;
6140 234035bc 2019-06-01 stsp }
6141 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6142 234035bc 2019-06-01 stsp if (error)
6143 234035bc 2019-06-01 stsp goto done;
6144 234035bc 2019-06-01 stsp
6145 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6146 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6147 234035bc 2019-06-01 stsp if (error != NULL)
6148 234035bc 2019-06-01 stsp goto done;
6149 234035bc 2019-06-01 stsp
6150 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6151 234035bc 2019-06-01 stsp if (error) {
6152 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6153 234035bc 2019-06-01 stsp goto done;
6154 234035bc 2019-06-01 stsp error = NULL;
6155 234035bc 2019-06-01 stsp } else {
6156 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6157 234035bc 2019-06-01 stsp goto done;
6158 234035bc 2019-06-01 stsp }
6159 234035bc 2019-06-01 stsp
6160 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6161 234035bc 2019-06-01 stsp if (error)
6162 234035bc 2019-06-01 stsp goto done;
6163 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6164 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6165 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6166 03415a1a 2019-06-02 stsp NULL);
6167 234035bc 2019-06-01 stsp if (error != NULL)
6168 234035bc 2019-06-01 stsp goto done;
6169 234035bc 2019-06-01 stsp
6170 234035bc 2019-06-01 stsp if (did_something)
6171 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6172 234035bc 2019-06-01 stsp done:
6173 234035bc 2019-06-01 stsp if (commit)
6174 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6175 234035bc 2019-06-01 stsp free(commit_id_str);
6176 234035bc 2019-06-01 stsp if (head_ref)
6177 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6178 234035bc 2019-06-01 stsp if (worktree)
6179 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6180 234035bc 2019-06-01 stsp if (repo)
6181 234035bc 2019-06-01 stsp got_repo_close(repo);
6182 c4296144 2019-05-09 stsp return error;
6183 c4296144 2019-05-09 stsp }
6184 5ef14e63 2019-06-02 stsp
6185 5ef14e63 2019-06-02 stsp __dead static void
6186 5ef14e63 2019-06-02 stsp usage_backout(void)
6187 5ef14e63 2019-06-02 stsp {
6188 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6189 5ef14e63 2019-06-02 stsp exit(1);
6190 5ef14e63 2019-06-02 stsp }
6191 5ef14e63 2019-06-02 stsp
6192 5ef14e63 2019-06-02 stsp static const struct got_error *
6193 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6194 5ef14e63 2019-06-02 stsp {
6195 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6196 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6197 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6198 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6199 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6200 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6201 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6202 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6203 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6204 5ef14e63 2019-06-02 stsp
6205 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6206 5ef14e63 2019-06-02 stsp switch (ch) {
6207 5ef14e63 2019-06-02 stsp default:
6208 5ef14e63 2019-06-02 stsp usage_backout();
6209 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6210 5ef14e63 2019-06-02 stsp }
6211 5ef14e63 2019-06-02 stsp }
6212 5ef14e63 2019-06-02 stsp
6213 5ef14e63 2019-06-02 stsp argc -= optind;
6214 5ef14e63 2019-06-02 stsp argv += optind;
6215 5ef14e63 2019-06-02 stsp
6216 43012d58 2019-07-14 stsp #ifndef PROFILE
6217 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6218 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6219 43012d58 2019-07-14 stsp err(1, "pledge");
6220 43012d58 2019-07-14 stsp #endif
6221 5ef14e63 2019-06-02 stsp if (argc != 1)
6222 5ef14e63 2019-06-02 stsp usage_backout();
6223 5ef14e63 2019-06-02 stsp
6224 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6225 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6226 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6227 5ef14e63 2019-06-02 stsp goto done;
6228 5ef14e63 2019-06-02 stsp }
6229 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6230 5ef14e63 2019-06-02 stsp if (error)
6231 5ef14e63 2019-06-02 stsp goto done;
6232 5ef14e63 2019-06-02 stsp
6233 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6234 c9956ddf 2019-09-08 stsp NULL);
6235 5ef14e63 2019-06-02 stsp if (error != NULL)
6236 5ef14e63 2019-06-02 stsp goto done;
6237 5ef14e63 2019-06-02 stsp
6238 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6239 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6240 5ef14e63 2019-06-02 stsp if (error)
6241 5ef14e63 2019-06-02 stsp goto done;
6242 5ef14e63 2019-06-02 stsp
6243 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6244 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6245 5ef14e63 2019-06-02 stsp if (error != NULL) {
6246 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6247 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6248 5ef14e63 2019-06-02 stsp goto done;
6249 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6250 5ef14e63 2019-06-02 stsp if (error != NULL)
6251 5ef14e63 2019-06-02 stsp goto done;
6252 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6253 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6254 5ef14e63 2019-06-02 stsp if (error != NULL)
6255 5ef14e63 2019-06-02 stsp goto done;
6256 5ef14e63 2019-06-02 stsp }
6257 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6258 5ef14e63 2019-06-02 stsp if (error)
6259 5ef14e63 2019-06-02 stsp goto done;
6260 5ef14e63 2019-06-02 stsp
6261 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6262 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6263 5ef14e63 2019-06-02 stsp if (error != NULL)
6264 5ef14e63 2019-06-02 stsp goto done;
6265 5ef14e63 2019-06-02 stsp
6266 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6267 5ef14e63 2019-06-02 stsp if (error)
6268 5ef14e63 2019-06-02 stsp goto done;
6269 5ef14e63 2019-06-02 stsp
6270 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6271 5ef14e63 2019-06-02 stsp if (error)
6272 5ef14e63 2019-06-02 stsp goto done;
6273 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6274 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6275 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6276 5ef14e63 2019-06-02 stsp goto done;
6277 5ef14e63 2019-06-02 stsp }
6278 5ef14e63 2019-06-02 stsp
6279 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6280 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6281 5ef14e63 2019-06-02 stsp if (error != NULL)
6282 5ef14e63 2019-06-02 stsp goto done;
6283 5ef14e63 2019-06-02 stsp
6284 5ef14e63 2019-06-02 stsp if (did_something)
6285 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6286 5ef14e63 2019-06-02 stsp done:
6287 5ef14e63 2019-06-02 stsp if (commit)
6288 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6289 5ef14e63 2019-06-02 stsp free(commit_id_str);
6290 5ef14e63 2019-06-02 stsp if (head_ref)
6291 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6292 818c7501 2019-07-11 stsp if (worktree)
6293 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6294 818c7501 2019-07-11 stsp if (repo)
6295 818c7501 2019-07-11 stsp got_repo_close(repo);
6296 818c7501 2019-07-11 stsp return error;
6297 818c7501 2019-07-11 stsp }
6298 818c7501 2019-07-11 stsp
6299 818c7501 2019-07-11 stsp __dead static void
6300 818c7501 2019-07-11 stsp usage_rebase(void)
6301 818c7501 2019-07-11 stsp {
6302 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6303 af54c8f8 2019-07-11 stsp getprogname());
6304 818c7501 2019-07-11 stsp exit(1);
6305 0ebf8283 2019-07-24 stsp }
6306 0ebf8283 2019-07-24 stsp
6307 0ebf8283 2019-07-24 stsp void
6308 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6309 0ebf8283 2019-07-24 stsp {
6310 0ebf8283 2019-07-24 stsp char *nl;
6311 0ebf8283 2019-07-24 stsp size_t len;
6312 0ebf8283 2019-07-24 stsp
6313 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6314 0ebf8283 2019-07-24 stsp if (len > limit)
6315 0ebf8283 2019-07-24 stsp len = limit;
6316 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6317 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6318 0ebf8283 2019-07-24 stsp if (nl)
6319 0ebf8283 2019-07-24 stsp *nl = '\0';
6320 0ebf8283 2019-07-24 stsp }
6321 0ebf8283 2019-07-24 stsp
6322 0ebf8283 2019-07-24 stsp static const struct got_error *
6323 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6324 0ebf8283 2019-07-24 stsp {
6325 5943eee2 2019-08-13 stsp const struct got_error *err;
6326 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6327 5943eee2 2019-08-13 stsp const char *s;
6328 0ebf8283 2019-07-24 stsp
6329 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6330 5943eee2 2019-08-13 stsp if (err)
6331 5943eee2 2019-08-13 stsp return err;
6332 0ebf8283 2019-07-24 stsp
6333 5943eee2 2019-08-13 stsp s = logmsg0;
6334 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6335 5943eee2 2019-08-13 stsp s++;
6336 5943eee2 2019-08-13 stsp
6337 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6338 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6339 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6340 5943eee2 2019-08-13 stsp goto done;
6341 5943eee2 2019-08-13 stsp }
6342 0ebf8283 2019-07-24 stsp
6343 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6344 5943eee2 2019-08-13 stsp done:
6345 5943eee2 2019-08-13 stsp free(logmsg0);
6346 a0ea4fc0 2020-02-28 stsp return err;
6347 a0ea4fc0 2020-02-28 stsp }
6348 a0ea4fc0 2020-02-28 stsp
6349 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6350 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6351 a0ea4fc0 2020-02-28 stsp {
6352 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6353 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6354 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6355 a0ea4fc0 2020-02-28 stsp
6356 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6357 a0ea4fc0 2020-02-28 stsp if (err)
6358 a0ea4fc0 2020-02-28 stsp return err;
6359 a0ea4fc0 2020-02-28 stsp
6360 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6361 a0ea4fc0 2020-02-28 stsp if (err)
6362 a0ea4fc0 2020-02-28 stsp goto done;
6363 a0ea4fc0 2020-02-28 stsp
6364 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6365 a0ea4fc0 2020-02-28 stsp
6366 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6367 a0ea4fc0 2020-02-28 stsp if (err)
6368 a0ea4fc0 2020-02-28 stsp goto done;
6369 a0ea4fc0 2020-02-28 stsp
6370 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6371 a0ea4fc0 2020-02-28 stsp done:
6372 a0ea4fc0 2020-02-28 stsp free(id_str);
6373 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6374 a0ea4fc0 2020-02-28 stsp free(logmsg);
6375 5943eee2 2019-08-13 stsp return err;
6376 818c7501 2019-07-11 stsp }
6377 818c7501 2019-07-11 stsp
6378 818c7501 2019-07-11 stsp static const struct got_error *
6379 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6380 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6381 818c7501 2019-07-11 stsp {
6382 818c7501 2019-07-11 stsp const struct got_error *err;
6383 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6384 818c7501 2019-07-11 stsp
6385 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6386 818c7501 2019-07-11 stsp if (err)
6387 818c7501 2019-07-11 stsp goto done;
6388 818c7501 2019-07-11 stsp
6389 ff0d2220 2019-07-11 stsp if (new_id) {
6390 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6391 ff0d2220 2019-07-11 stsp if (err)
6392 ff0d2220 2019-07-11 stsp goto done;
6393 ff0d2220 2019-07-11 stsp }
6394 818c7501 2019-07-11 stsp
6395 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6396 ff0d2220 2019-07-11 stsp if (new_id_str)
6397 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6398 0ebf8283 2019-07-24 stsp
6399 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6400 0ebf8283 2019-07-24 stsp if (err)
6401 0ebf8283 2019-07-24 stsp goto done;
6402 0ebf8283 2019-07-24 stsp
6403 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6404 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6405 818c7501 2019-07-11 stsp done:
6406 818c7501 2019-07-11 stsp free(old_id_str);
6407 818c7501 2019-07-11 stsp free(new_id_str);
6408 272a1371 2020-02-28 stsp free(logmsg);
6409 818c7501 2019-07-11 stsp return err;
6410 818c7501 2019-07-11 stsp }
6411 818c7501 2019-07-11 stsp
6412 1ee397ad 2019-07-12 stsp static const struct got_error *
6413 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6414 818c7501 2019-07-11 stsp {
6415 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6416 818c7501 2019-07-11 stsp
6417 818c7501 2019-07-11 stsp while (path[0] == '/')
6418 818c7501 2019-07-11 stsp path++;
6419 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6420 818c7501 2019-07-11 stsp
6421 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6422 1ee397ad 2019-07-12 stsp return NULL;
6423 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6424 818c7501 2019-07-11 stsp *rebase_status = status;
6425 1ee397ad 2019-07-12 stsp return NULL;
6426 818c7501 2019-07-11 stsp }
6427 818c7501 2019-07-11 stsp
6428 818c7501 2019-07-11 stsp static const struct got_error *
6429 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6430 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6431 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6432 818c7501 2019-07-11 stsp {
6433 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6434 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6435 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6436 818c7501 2019-07-11 stsp }
6437 818c7501 2019-07-11 stsp
6438 818c7501 2019-07-11 stsp static const struct got_error *
6439 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6440 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6441 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6442 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6443 ff0d2220 2019-07-11 stsp {
6444 ff0d2220 2019-07-11 stsp const struct got_error *error;
6445 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6446 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6447 ff0d2220 2019-07-11 stsp
6448 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6449 ff0d2220 2019-07-11 stsp if (error)
6450 ff0d2220 2019-07-11 stsp return error;
6451 ff0d2220 2019-07-11 stsp
6452 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6453 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6454 ff0d2220 2019-07-11 stsp if (error) {
6455 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6456 ff0d2220 2019-07-11 stsp goto done;
6457 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6458 ff0d2220 2019-07-11 stsp } else {
6459 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6460 ff0d2220 2019-07-11 stsp free(new_commit_id);
6461 ff0d2220 2019-07-11 stsp }
6462 ff0d2220 2019-07-11 stsp done:
6463 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6464 ff0d2220 2019-07-11 stsp return error;
6465 64c6d990 2019-07-11 stsp }
6466 64c6d990 2019-07-11 stsp
6467 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6468 64c6d990 2019-07-11 stsp const char *path_prefix;
6469 64c6d990 2019-07-11 stsp size_t len;
6470 8ca9bd68 2019-07-25 stsp int errcode;
6471 64c6d990 2019-07-11 stsp };
6472 64c6d990 2019-07-11 stsp
6473 64c6d990 2019-07-11 stsp static const struct got_error *
6474 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6475 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6476 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6477 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6478 64c6d990 2019-07-11 stsp {
6479 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6480 64c6d990 2019-07-11 stsp
6481 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6482 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6483 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6484 64c6d990 2019-07-11 stsp
6485 64c6d990 2019-07-11 stsp return NULL;
6486 64c6d990 2019-07-11 stsp }
6487 64c6d990 2019-07-11 stsp
6488 64c6d990 2019-07-11 stsp static const struct got_error *
6489 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6490 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6491 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6492 64c6d990 2019-07-11 stsp {
6493 64c6d990 2019-07-11 stsp const struct got_error *err;
6494 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6495 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6496 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6497 64c6d990 2019-07-11 stsp
6498 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6499 64c6d990 2019-07-11 stsp return NULL;
6500 64c6d990 2019-07-11 stsp
6501 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6502 64c6d990 2019-07-11 stsp if (err)
6503 64c6d990 2019-07-11 stsp goto done;
6504 64c6d990 2019-07-11 stsp
6505 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6506 64c6d990 2019-07-11 stsp if (err)
6507 64c6d990 2019-07-11 stsp goto done;
6508 64c6d990 2019-07-11 stsp
6509 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6510 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6511 64c6d990 2019-07-11 stsp if (err)
6512 64c6d990 2019-07-11 stsp goto done;
6513 64c6d990 2019-07-11 stsp
6514 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6515 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6516 64c6d990 2019-07-11 stsp if (err)
6517 64c6d990 2019-07-11 stsp goto done;
6518 64c6d990 2019-07-11 stsp
6519 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6520 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6521 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6522 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6523 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6524 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6525 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6526 64c6d990 2019-07-11 stsp done:
6527 64c6d990 2019-07-11 stsp if (tree1)
6528 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6529 64c6d990 2019-07-11 stsp if (tree2)
6530 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6531 64c6d990 2019-07-11 stsp if (commit)
6532 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6533 64c6d990 2019-07-11 stsp if (parent_commit)
6534 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6535 64c6d990 2019-07-11 stsp return err;
6536 ff0d2220 2019-07-11 stsp }
6537 ff0d2220 2019-07-11 stsp
6538 ff0d2220 2019-07-11 stsp static const struct got_error *
6539 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6540 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6541 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6542 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6543 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6544 af61c510 2019-07-19 stsp {
6545 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6546 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6547 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6548 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6549 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6550 af61c510 2019-07-19 stsp
6551 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6552 af61c510 2019-07-19 stsp if (err)
6553 af61c510 2019-07-19 stsp return err;
6554 af61c510 2019-07-19 stsp
6555 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6556 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6557 af61c510 2019-07-19 stsp if (err)
6558 af61c510 2019-07-19 stsp goto done;
6559 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6560 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6561 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6562 af61c510 2019-07-19 stsp if (err) {
6563 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6564 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6565 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6566 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6567 af61c510 2019-07-19 stsp "been reached?!?");
6568 ee780d5c 2020-01-04 stsp }
6569 ee780d5c 2020-01-04 stsp goto done;
6570 af61c510 2019-07-19 stsp } else {
6571 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6572 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6573 af61c510 2019-07-19 stsp if (err)
6574 af61c510 2019-07-19 stsp goto done;
6575 af61c510 2019-07-19 stsp
6576 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6577 af61c510 2019-07-19 stsp if (err)
6578 af61c510 2019-07-19 stsp goto done;
6579 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6580 af61c510 2019-07-19 stsp commit_id = parent_id;
6581 af61c510 2019-07-19 stsp }
6582 af61c510 2019-07-19 stsp }
6583 af61c510 2019-07-19 stsp done:
6584 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6585 af61c510 2019-07-19 stsp return err;
6586 af61c510 2019-07-19 stsp }
6587 af61c510 2019-07-19 stsp
6588 af61c510 2019-07-19 stsp static const struct got_error *
6589 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6590 818c7501 2019-07-11 stsp {
6591 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6592 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6593 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6594 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6595 818c7501 2019-07-11 stsp char *cwd = NULL;
6596 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6597 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6598 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6599 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6600 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6601 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6602 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6603 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6604 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6605 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6606 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6607 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6608 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6609 818c7501 2019-07-11 stsp
6610 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6611 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6612 818c7501 2019-07-11 stsp
6613 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6614 818c7501 2019-07-11 stsp switch (ch) {
6615 818c7501 2019-07-11 stsp case 'a':
6616 818c7501 2019-07-11 stsp abort_rebase = 1;
6617 818c7501 2019-07-11 stsp break;
6618 818c7501 2019-07-11 stsp case 'c':
6619 818c7501 2019-07-11 stsp continue_rebase = 1;
6620 818c7501 2019-07-11 stsp break;
6621 818c7501 2019-07-11 stsp default:
6622 818c7501 2019-07-11 stsp usage_rebase();
6623 818c7501 2019-07-11 stsp /* NOTREACHED */
6624 818c7501 2019-07-11 stsp }
6625 818c7501 2019-07-11 stsp }
6626 818c7501 2019-07-11 stsp
6627 818c7501 2019-07-11 stsp argc -= optind;
6628 818c7501 2019-07-11 stsp argv += optind;
6629 818c7501 2019-07-11 stsp
6630 43012d58 2019-07-14 stsp #ifndef PROFILE
6631 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6632 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6633 43012d58 2019-07-14 stsp err(1, "pledge");
6634 43012d58 2019-07-14 stsp #endif
6635 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6636 818c7501 2019-07-11 stsp usage_rebase();
6637 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6638 818c7501 2019-07-11 stsp if (argc != 0)
6639 818c7501 2019-07-11 stsp usage_rebase();
6640 818c7501 2019-07-11 stsp } else if (argc != 1)
6641 818c7501 2019-07-11 stsp usage_rebase();
6642 818c7501 2019-07-11 stsp
6643 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6644 818c7501 2019-07-11 stsp if (cwd == NULL) {
6645 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6646 818c7501 2019-07-11 stsp goto done;
6647 818c7501 2019-07-11 stsp }
6648 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6649 818c7501 2019-07-11 stsp if (error)
6650 818c7501 2019-07-11 stsp goto done;
6651 818c7501 2019-07-11 stsp
6652 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6653 c9956ddf 2019-09-08 stsp NULL);
6654 818c7501 2019-07-11 stsp if (error != NULL)
6655 818c7501 2019-07-11 stsp goto done;
6656 818c7501 2019-07-11 stsp
6657 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6658 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6659 7ef62c4e 2020-02-24 stsp if (error)
6660 7ef62c4e 2020-02-24 stsp goto done;
6661 7ef62c4e 2020-02-24 stsp
6662 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6663 7ef62c4e 2020-02-24 stsp worktree);
6664 818c7501 2019-07-11 stsp if (error)
6665 7ef62c4e 2020-02-24 stsp goto done;
6666 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6667 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6668 818c7501 2019-07-11 stsp goto done;
6669 7ef62c4e 2020-02-24 stsp }
6670 818c7501 2019-07-11 stsp
6671 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6672 818c7501 2019-07-11 stsp if (error)
6673 818c7501 2019-07-11 stsp goto done;
6674 818c7501 2019-07-11 stsp
6675 f6794adc 2019-07-23 stsp if (abort_rebase) {
6676 818c7501 2019-07-11 stsp int did_something;
6677 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6678 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6679 f6794adc 2019-07-23 stsp goto done;
6680 f6794adc 2019-07-23 stsp }
6681 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6682 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6683 3e3a69f1 2019-07-25 stsp worktree, repo);
6684 818c7501 2019-07-11 stsp if (error)
6685 818c7501 2019-07-11 stsp goto done;
6686 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6687 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6688 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6689 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6690 818c7501 2019-07-11 stsp if (error)
6691 818c7501 2019-07-11 stsp goto done;
6692 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6693 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6694 818c7501 2019-07-11 stsp }
6695 818c7501 2019-07-11 stsp
6696 818c7501 2019-07-11 stsp if (continue_rebase) {
6697 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6698 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6699 f6794adc 2019-07-23 stsp goto done;
6700 f6794adc 2019-07-23 stsp }
6701 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6702 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6703 3e3a69f1 2019-07-25 stsp worktree, repo);
6704 818c7501 2019-07-11 stsp if (error)
6705 818c7501 2019-07-11 stsp goto done;
6706 818c7501 2019-07-11 stsp
6707 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6708 01757395 2019-07-12 stsp resume_commit_id, repo);
6709 818c7501 2019-07-11 stsp if (error)
6710 818c7501 2019-07-11 stsp goto done;
6711 818c7501 2019-07-11 stsp
6712 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6713 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6714 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6715 818c7501 2019-07-11 stsp goto done;
6716 818c7501 2019-07-11 stsp }
6717 818c7501 2019-07-11 stsp } else {
6718 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6719 818c7501 2019-07-11 stsp if (error != NULL)
6720 818c7501 2019-07-11 stsp goto done;
6721 ff0d2220 2019-07-11 stsp }
6722 818c7501 2019-07-11 stsp
6723 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6724 ff0d2220 2019-07-11 stsp if (error)
6725 ff0d2220 2019-07-11 stsp goto done;
6726 ff0d2220 2019-07-11 stsp
6727 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6728 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6729 a51a74b3 2019-07-27 stsp
6730 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6731 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6732 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6733 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6734 818c7501 2019-07-11 stsp if (error)
6735 818c7501 2019-07-11 stsp goto done;
6736 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6737 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6738 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6739 818c7501 2019-07-11 stsp "with work tree's branch");
6740 818c7501 2019-07-11 stsp goto done;
6741 818c7501 2019-07-11 stsp }
6742 818c7501 2019-07-11 stsp
6743 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6744 a51a74b3 2019-07-27 stsp if (error) {
6745 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6746 a51a74b3 2019-07-27 stsp goto done;
6747 a51a74b3 2019-07-27 stsp error = NULL;
6748 a51a74b3 2019-07-27 stsp } else {
6749 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6750 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6751 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6752 a51a74b3 2019-07-27 stsp goto done;
6753 a51a74b3 2019-07-27 stsp }
6754 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6755 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6756 818c7501 2019-07-11 stsp if (error)
6757 818c7501 2019-07-11 stsp goto done;
6758 818c7501 2019-07-11 stsp }
6759 818c7501 2019-07-11 stsp
6760 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6761 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6762 818c7501 2019-07-11 stsp if (error)
6763 818c7501 2019-07-11 stsp goto done;
6764 818c7501 2019-07-11 stsp
6765 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6766 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6767 fc66b545 2019-08-12 stsp if (pid == NULL) {
6768 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6769 fc66b545 2019-08-12 stsp int did_something;
6770 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6771 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6772 fc66b545 2019-08-12 stsp &did_something);
6773 fc66b545 2019-08-12 stsp if (error)
6774 fc66b545 2019-08-12 stsp goto done;
6775 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6776 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6777 fc66b545 2019-08-12 stsp }
6778 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6779 fc66b545 2019-08-12 stsp goto done;
6780 fc66b545 2019-08-12 stsp }
6781 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6782 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6783 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6784 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6785 818c7501 2019-07-11 stsp commit = NULL;
6786 818c7501 2019-07-11 stsp if (error)
6787 818c7501 2019-07-11 stsp goto done;
6788 64c6d990 2019-07-11 stsp
6789 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6790 38b0338b 2019-11-29 stsp if (continue_rebase) {
6791 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6792 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6793 38b0338b 2019-11-29 stsp goto done;
6794 38b0338b 2019-11-29 stsp } else {
6795 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6796 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6797 38b0338b 2019-11-29 stsp char *id_str;
6798 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6799 38b0338b 2019-11-29 stsp new_base_branch);
6800 38b0338b 2019-11-29 stsp if (error)
6801 38b0338b 2019-11-29 stsp goto done;
6802 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6803 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6804 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6805 38b0338b 2019-11-29 stsp free(id_str);
6806 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6807 38b0338b 2019-11-29 stsp new_head_commit_id);
6808 38b0338b 2019-11-29 stsp if (error)
6809 38b0338b 2019-11-29 stsp goto done;
6810 38b0338b 2019-11-29 stsp }
6811 818c7501 2019-07-11 stsp }
6812 818c7501 2019-07-11 stsp
6813 818c7501 2019-07-11 stsp pid = NULL;
6814 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6815 818c7501 2019-07-11 stsp commit_id = qid->id;
6816 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6817 818c7501 2019-07-11 stsp pid = qid;
6818 818c7501 2019-07-11 stsp
6819 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6820 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6821 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6822 818c7501 2019-07-11 stsp if (error)
6823 818c7501 2019-07-11 stsp goto done;
6824 818c7501 2019-07-11 stsp
6825 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6826 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6827 a0ea4fc0 2020-02-28 stsp if (error)
6828 a0ea4fc0 2020-02-28 stsp goto done;
6829 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6830 818c7501 2019-07-11 stsp break;
6831 01757395 2019-07-12 stsp }
6832 818c7501 2019-07-11 stsp
6833 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6834 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6835 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6836 818c7501 2019-07-11 stsp if (error)
6837 818c7501 2019-07-11 stsp goto done;
6838 818c7501 2019-07-11 stsp }
6839 818c7501 2019-07-11 stsp
6840 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6841 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6842 818c7501 2019-07-11 stsp if (error)
6843 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6845 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6846 818c7501 2019-07-11 stsp } else
6847 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6848 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6849 818c7501 2019-07-11 stsp done:
6850 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6851 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6852 818c7501 2019-07-11 stsp free(resume_commit_id);
6853 818c7501 2019-07-11 stsp free(yca_id);
6854 818c7501 2019-07-11 stsp if (commit)
6855 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6856 818c7501 2019-07-11 stsp if (branch)
6857 818c7501 2019-07-11 stsp got_ref_close(branch);
6858 818c7501 2019-07-11 stsp if (new_base_branch)
6859 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6860 818c7501 2019-07-11 stsp if (tmp_branch)
6861 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6862 5ef14e63 2019-06-02 stsp if (worktree)
6863 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6864 5ef14e63 2019-06-02 stsp if (repo)
6865 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6866 5ef14e63 2019-06-02 stsp return error;
6867 0ebf8283 2019-07-24 stsp }
6868 0ebf8283 2019-07-24 stsp
6869 0ebf8283 2019-07-24 stsp __dead static void
6870 0ebf8283 2019-07-24 stsp usage_histedit(void)
6871 0ebf8283 2019-07-24 stsp {
6872 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6873 0ebf8283 2019-07-24 stsp getprogname());
6874 0ebf8283 2019-07-24 stsp exit(1);
6875 0ebf8283 2019-07-24 stsp }
6876 0ebf8283 2019-07-24 stsp
6877 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6878 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6879 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6880 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6881 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6882 0ebf8283 2019-07-24 stsp
6883 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6884 0ebf8283 2019-07-24 stsp unsigned char code;
6885 0ebf8283 2019-07-24 stsp const char *name;
6886 0ebf8283 2019-07-24 stsp const char *desc;
6887 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6888 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6889 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6890 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6891 82997472 2020-01-29 stsp "be used" },
6892 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6893 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6894 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6895 0ebf8283 2019-07-24 stsp };
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6898 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6899 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6900 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6901 0ebf8283 2019-07-24 stsp char *logmsg;
6902 0ebf8283 2019-07-24 stsp };
6903 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp static const struct got_error *
6906 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6907 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6908 0ebf8283 2019-07-24 stsp {
6909 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6910 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6911 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6912 8138f3e1 2019-08-11 stsp int n;
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6915 0ebf8283 2019-07-24 stsp if (err)
6916 0ebf8283 2019-07-24 stsp goto done;
6917 0ebf8283 2019-07-24 stsp
6918 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6919 0ebf8283 2019-07-24 stsp if (err)
6920 0ebf8283 2019-07-24 stsp goto done;
6921 0ebf8283 2019-07-24 stsp
6922 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6923 0ebf8283 2019-07-24 stsp if (err)
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp
6926 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6927 0ebf8283 2019-07-24 stsp if (n < 0)
6928 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6929 0ebf8283 2019-07-24 stsp done:
6930 0ebf8283 2019-07-24 stsp if (commit)
6931 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6932 0ebf8283 2019-07-24 stsp free(id_str);
6933 0ebf8283 2019-07-24 stsp free(logmsg);
6934 0ebf8283 2019-07-24 stsp return err;
6935 0ebf8283 2019-07-24 stsp }
6936 0ebf8283 2019-07-24 stsp
6937 0ebf8283 2019-07-24 stsp static const struct got_error *
6938 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6939 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6940 0ebf8283 2019-07-24 stsp {
6941 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6942 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6945 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6946 0ebf8283 2019-07-24 stsp
6947 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6948 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6949 0ebf8283 2019-07-24 stsp f, repo);
6950 0ebf8283 2019-07-24 stsp if (err)
6951 0ebf8283 2019-07-24 stsp break;
6952 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6953 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6954 083957f4 2020-02-24 stsp if (n < 0) {
6955 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6956 083957f4 2020-02-24 stsp break;
6957 083957f4 2020-02-24 stsp }
6958 083957f4 2020-02-24 stsp }
6959 0ebf8283 2019-07-24 stsp }
6960 0ebf8283 2019-07-24 stsp
6961 0ebf8283 2019-07-24 stsp return err;
6962 0ebf8283 2019-07-24 stsp }
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp static const struct got_error *
6965 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6966 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6967 0ebf8283 2019-07-24 stsp {
6968 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6969 0ebf8283 2019-07-24 stsp int n, i;
6970 514f2ffe 2020-01-29 stsp char *id_str;
6971 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6972 514f2ffe 2020-01-29 stsp
6973 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6974 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6975 514f2ffe 2020-01-29 stsp if (err)
6976 514f2ffe 2020-01-29 stsp return err;
6977 514f2ffe 2020-01-29 stsp
6978 514f2ffe 2020-01-29 stsp n = fprintf(f,
6979 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6980 514f2ffe 2020-01-29 stsp "# commit %s\n"
6981 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6982 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6983 514f2ffe 2020-01-29 stsp if (n < 0) {
6984 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6985 514f2ffe 2020-01-29 stsp goto done;
6986 514f2ffe 2020-01-29 stsp }
6987 0ebf8283 2019-07-24 stsp
6988 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6989 514f2ffe 2020-01-29 stsp if (n < 0) {
6990 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6991 514f2ffe 2020-01-29 stsp goto done;
6992 514f2ffe 2020-01-29 stsp }
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6995 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6996 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6997 0ebf8283 2019-07-24 stsp cmd->desc);
6998 0ebf8283 2019-07-24 stsp if (n < 0) {
6999 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7000 0ebf8283 2019-07-24 stsp break;
7001 0ebf8283 2019-07-24 stsp }
7002 0ebf8283 2019-07-24 stsp }
7003 514f2ffe 2020-01-29 stsp done:
7004 514f2ffe 2020-01-29 stsp free(id_str);
7005 0ebf8283 2019-07-24 stsp return err;
7006 0ebf8283 2019-07-24 stsp }
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp static const struct got_error *
7009 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7010 0ebf8283 2019-07-24 stsp {
7011 0ebf8283 2019-07-24 stsp static char msg[42];
7012 0ebf8283 2019-07-24 stsp int ret;
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7015 0ebf8283 2019-07-24 stsp lineno);
7016 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7017 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7018 0ebf8283 2019-07-24 stsp
7019 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7020 0ebf8283 2019-07-24 stsp }
7021 0ebf8283 2019-07-24 stsp
7022 0ebf8283 2019-07-24 stsp static const struct got_error *
7023 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7024 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7025 0ebf8283 2019-07-24 stsp {
7026 0ebf8283 2019-07-24 stsp const struct got_error *err;
7027 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7028 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7031 0ebf8283 2019-07-24 stsp if (err)
7032 0ebf8283 2019-07-24 stsp return err;
7033 0ebf8283 2019-07-24 stsp
7034 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7035 0ebf8283 2019-07-24 stsp if (err)
7036 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp
7038 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7039 5943eee2 2019-08-13 stsp if (err)
7040 5943eee2 2019-08-13 stsp goto done;
7041 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7042 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7043 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7044 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7045 0ebf8283 2019-07-24 stsp }
7046 0ebf8283 2019-07-24 stsp done:
7047 0ebf8283 2019-07-24 stsp if (folded_commit)
7048 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7049 0ebf8283 2019-07-24 stsp free(id_str);
7050 5943eee2 2019-08-13 stsp free(folded_logmsg);
7051 0ebf8283 2019-07-24 stsp return err;
7052 0ebf8283 2019-07-24 stsp }
7053 0ebf8283 2019-07-24 stsp
7054 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7055 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7056 0ebf8283 2019-07-24 stsp {
7057 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7058 0ebf8283 2019-07-24 stsp
7059 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7060 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7061 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7062 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7063 3f9de99f 2019-07-24 stsp folded = prev;
7064 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7065 0ebf8283 2019-07-24 stsp }
7066 0ebf8283 2019-07-24 stsp
7067 0ebf8283 2019-07-24 stsp return folded;
7068 0ebf8283 2019-07-24 stsp }
7069 0ebf8283 2019-07-24 stsp
7070 0ebf8283 2019-07-24 stsp static const struct got_error *
7071 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7072 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7073 0ebf8283 2019-07-24 stsp {
7074 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7075 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7076 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7077 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7078 0ebf8283 2019-07-24 stsp int fd;
7079 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7080 0ebf8283 2019-07-24 stsp
7081 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7082 0ebf8283 2019-07-24 stsp if (err)
7083 0ebf8283 2019-07-24 stsp return err;
7084 0ebf8283 2019-07-24 stsp
7085 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7086 0ebf8283 2019-07-24 stsp if (folded) {
7087 0ebf8283 2019-07-24 stsp while (folded != hle) {
7088 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7089 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7090 3f9de99f 2019-07-24 stsp continue;
7091 3f9de99f 2019-07-24 stsp }
7092 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7093 0ebf8283 2019-07-24 stsp logmsg, repo);
7094 0ebf8283 2019-07-24 stsp if (err)
7095 0ebf8283 2019-07-24 stsp goto done;
7096 0ebf8283 2019-07-24 stsp free(logmsg);
7097 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7098 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7099 0ebf8283 2019-07-24 stsp }
7100 0ebf8283 2019-07-24 stsp }
7101 0ebf8283 2019-07-24 stsp
7102 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7103 0ebf8283 2019-07-24 stsp if (err)
7104 0ebf8283 2019-07-24 stsp goto done;
7105 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7106 5943eee2 2019-08-13 stsp if (err)
7107 5943eee2 2019-08-13 stsp goto done;
7108 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7109 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7110 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7111 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7112 0ebf8283 2019-07-24 stsp goto done;
7113 0ebf8283 2019-07-24 stsp }
7114 0ebf8283 2019-07-24 stsp free(logmsg);
7115 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7116 0ebf8283 2019-07-24 stsp
7117 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7118 0ebf8283 2019-07-24 stsp if (err)
7119 0ebf8283 2019-07-24 stsp goto done;
7120 0ebf8283 2019-07-24 stsp
7121 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7122 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7123 0ebf8283 2019-07-24 stsp if (err)
7124 0ebf8283 2019-07-24 stsp goto done;
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7127 0ebf8283 2019-07-24 stsp close(fd);
7128 0ebf8283 2019-07-24 stsp
7129 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7130 0ebf8283 2019-07-24 stsp if (err)
7131 0ebf8283 2019-07-24 stsp goto done;
7132 0ebf8283 2019-07-24 stsp
7133 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7134 0ebf8283 2019-07-24 stsp if (err) {
7135 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7136 0ebf8283 2019-07-24 stsp goto done;
7137 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7138 0ebf8283 2019-07-24 stsp }
7139 0ebf8283 2019-07-24 stsp done:
7140 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7141 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7142 0ebf8283 2019-07-24 stsp free(logmsg_path);
7143 0ebf8283 2019-07-24 stsp free(logmsg);
7144 5943eee2 2019-08-13 stsp free(orig_logmsg);
7145 0ebf8283 2019-07-24 stsp free(editor);
7146 0ebf8283 2019-07-24 stsp if (commit)
7147 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7148 0ebf8283 2019-07-24 stsp return err;
7149 5ef14e63 2019-06-02 stsp }
7150 0ebf8283 2019-07-24 stsp
7151 0ebf8283 2019-07-24 stsp static const struct got_error *
7152 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7153 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7154 0ebf8283 2019-07-24 stsp {
7155 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7156 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7157 0ebf8283 2019-07-24 stsp size_t size;
7158 0ebf8283 2019-07-24 stsp ssize_t len;
7159 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7160 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7161 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7162 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7163 0ebf8283 2019-07-24 stsp
7164 0ebf8283 2019-07-24 stsp for (;;) {
7165 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7166 0ebf8283 2019-07-24 stsp if (len == -1) {
7167 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7168 0ebf8283 2019-07-24 stsp if (feof(f))
7169 0ebf8283 2019-07-24 stsp break;
7170 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7171 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7172 0ebf8283 2019-07-24 stsp break;
7173 0ebf8283 2019-07-24 stsp }
7174 0ebf8283 2019-07-24 stsp lineno++;
7175 0ebf8283 2019-07-24 stsp p = line;
7176 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7177 0ebf8283 2019-07-24 stsp p++;
7178 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7179 0ebf8283 2019-07-24 stsp free(line);
7180 0ebf8283 2019-07-24 stsp line = NULL;
7181 0ebf8283 2019-07-24 stsp continue;
7182 0ebf8283 2019-07-24 stsp }
7183 0ebf8283 2019-07-24 stsp cmd = NULL;
7184 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7185 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7186 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7187 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7188 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7189 0ebf8283 2019-07-24 stsp break;
7190 0ebf8283 2019-07-24 stsp }
7191 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7192 0ebf8283 2019-07-24 stsp p++;
7193 0ebf8283 2019-07-24 stsp break;
7194 0ebf8283 2019-07-24 stsp }
7195 0ebf8283 2019-07-24 stsp }
7196 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7197 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7198 0ebf8283 2019-07-24 stsp break;
7199 0ebf8283 2019-07-24 stsp }
7200 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7201 0ebf8283 2019-07-24 stsp p++;
7202 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7203 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7204 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7205 0ebf8283 2019-07-24 stsp break;
7206 0ebf8283 2019-07-24 stsp }
7207 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7208 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp break;
7211 0ebf8283 2019-07-24 stsp } else {
7212 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7213 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7214 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7215 0ebf8283 2019-07-24 stsp break;
7216 0ebf8283 2019-07-24 stsp }
7217 0ebf8283 2019-07-24 stsp }
7218 0ebf8283 2019-07-24 stsp free(line);
7219 0ebf8283 2019-07-24 stsp line = NULL;
7220 0ebf8283 2019-07-24 stsp continue;
7221 0ebf8283 2019-07-24 stsp } else {
7222 0ebf8283 2019-07-24 stsp end = p;
7223 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7224 0ebf8283 2019-07-24 stsp end++;
7225 0ebf8283 2019-07-24 stsp *end = '\0';
7226 0ebf8283 2019-07-24 stsp
7227 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7228 0ebf8283 2019-07-24 stsp if (err) {
7229 0ebf8283 2019-07-24 stsp /* override error code */
7230 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7231 0ebf8283 2019-07-24 stsp break;
7232 0ebf8283 2019-07-24 stsp }
7233 0ebf8283 2019-07-24 stsp }
7234 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7235 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7236 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7237 0ebf8283 2019-07-24 stsp break;
7238 0ebf8283 2019-07-24 stsp }
7239 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7240 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7241 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7242 0ebf8283 2019-07-24 stsp commit_id = NULL;
7243 0ebf8283 2019-07-24 stsp free(line);
7244 0ebf8283 2019-07-24 stsp line = NULL;
7245 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7246 0ebf8283 2019-07-24 stsp }
7247 0ebf8283 2019-07-24 stsp
7248 0ebf8283 2019-07-24 stsp free(line);
7249 0ebf8283 2019-07-24 stsp free(commit_id);
7250 0ebf8283 2019-07-24 stsp return err;
7251 0ebf8283 2019-07-24 stsp }
7252 0ebf8283 2019-07-24 stsp
7253 0ebf8283 2019-07-24 stsp static const struct got_error *
7254 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7255 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7256 bfce7f83 2019-07-27 stsp {
7257 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7258 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7259 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7260 5b87815e 2020-03-05 stsp static char msg[92];
7261 bfce7f83 2019-07-27 stsp char *id_str;
7262 bfce7f83 2019-07-27 stsp
7263 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7264 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7265 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7266 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7267 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7268 5b87815e 2020-03-05 stsp
7269 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7270 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7271 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7272 5b87815e 2020-03-05 stsp if (hle == hle2)
7273 5b87815e 2020-03-05 stsp continue;
7274 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7275 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7276 5b87815e 2020-03-05 stsp continue;
7277 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7278 5b87815e 2020-03-05 stsp if (err)
7279 5b87815e 2020-03-05 stsp return err;
7280 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7281 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7282 5b87815e 2020-03-05 stsp free(id_str);
7283 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7284 5b87815e 2020-03-05 stsp }
7285 5b87815e 2020-03-05 stsp }
7286 bfce7f83 2019-07-27 stsp
7287 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7288 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7289 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7290 bfce7f83 2019-07-27 stsp break;
7291 bfce7f83 2019-07-27 stsp }
7292 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7293 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7294 bfce7f83 2019-07-27 stsp if (err)
7295 bfce7f83 2019-07-27 stsp return err;
7296 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7297 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7298 bfce7f83 2019-07-27 stsp free(id_str);
7299 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7300 bfce7f83 2019-07-27 stsp }
7301 bfce7f83 2019-07-27 stsp }
7302 bfce7f83 2019-07-27 stsp
7303 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7304 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7305 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7306 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7307 bfce7f83 2019-07-27 stsp
7308 bfce7f83 2019-07-27 stsp return NULL;
7309 bfce7f83 2019-07-27 stsp }
7310 bfce7f83 2019-07-27 stsp
7311 bfce7f83 2019-07-27 stsp static const struct got_error *
7312 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7313 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7314 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7315 0ebf8283 2019-07-24 stsp {
7316 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7317 0ebf8283 2019-07-24 stsp char *editor;
7318 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7319 0ebf8283 2019-07-24 stsp
7320 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7321 0ebf8283 2019-07-24 stsp if (err)
7322 0ebf8283 2019-07-24 stsp return err;
7323 0ebf8283 2019-07-24 stsp
7324 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7325 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp }
7328 0ebf8283 2019-07-24 stsp
7329 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7330 0ebf8283 2019-07-24 stsp if (f == NULL) {
7331 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7332 0ebf8283 2019-07-24 stsp goto done;
7333 0ebf8283 2019-07-24 stsp }
7334 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7335 bfce7f83 2019-07-27 stsp if (err)
7336 bfce7f83 2019-07-27 stsp goto done;
7337 bfce7f83 2019-07-27 stsp
7338 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7339 0ebf8283 2019-07-24 stsp done:
7340 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7341 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7342 0ebf8283 2019-07-24 stsp free(editor);
7343 0ebf8283 2019-07-24 stsp return err;
7344 0ebf8283 2019-07-24 stsp }
7345 0ebf8283 2019-07-24 stsp
7346 0ebf8283 2019-07-24 stsp static const struct got_error *
7347 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7348 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7349 514f2ffe 2020-01-29 stsp struct got_repository *);
7350 0ebf8283 2019-07-24 stsp
7351 0ebf8283 2019-07-24 stsp static const struct got_error *
7352 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7353 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7354 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7355 0ebf8283 2019-07-24 stsp {
7356 0ebf8283 2019-07-24 stsp const struct got_error *err;
7357 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7358 0ebf8283 2019-07-24 stsp char *path = NULL;
7359 0ebf8283 2019-07-24 stsp
7360 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7361 0ebf8283 2019-07-24 stsp if (err)
7362 0ebf8283 2019-07-24 stsp return err;
7363 0ebf8283 2019-07-24 stsp
7364 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7365 0ebf8283 2019-07-24 stsp if (err)
7366 0ebf8283 2019-07-24 stsp goto done;
7367 0ebf8283 2019-07-24 stsp
7368 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7369 0ebf8283 2019-07-24 stsp if (err)
7370 0ebf8283 2019-07-24 stsp goto done;
7371 0ebf8283 2019-07-24 stsp
7372 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7373 083957f4 2020-02-24 stsp rewind(f);
7374 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7375 083957f4 2020-02-24 stsp } else {
7376 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7377 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7378 0ebf8283 2019-07-24 stsp goto done;
7379 083957f4 2020-02-24 stsp }
7380 083957f4 2020-02-24 stsp f = NULL;
7381 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7382 083957f4 2020-02-24 stsp if (err) {
7383 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7384 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7385 083957f4 2020-02-24 stsp goto done;
7386 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7387 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7388 083957f4 2020-02-24 stsp }
7389 0ebf8283 2019-07-24 stsp }
7390 0ebf8283 2019-07-24 stsp done:
7391 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7392 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7393 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7394 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7395 0ebf8283 2019-07-24 stsp free(path);
7396 0ebf8283 2019-07-24 stsp return err;
7397 0ebf8283 2019-07-24 stsp }
7398 0ebf8283 2019-07-24 stsp
7399 0ebf8283 2019-07-24 stsp static const struct got_error *
7400 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7401 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7402 0ebf8283 2019-07-24 stsp {
7403 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7404 0ebf8283 2019-07-24 stsp char *path = NULL;
7405 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7406 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7407 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7408 0ebf8283 2019-07-24 stsp
7409 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7410 0ebf8283 2019-07-24 stsp if (err)
7411 0ebf8283 2019-07-24 stsp return err;
7412 0ebf8283 2019-07-24 stsp
7413 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7414 0ebf8283 2019-07-24 stsp if (f == NULL) {
7415 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7416 0ebf8283 2019-07-24 stsp goto done;
7417 0ebf8283 2019-07-24 stsp }
7418 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7419 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7420 0ebf8283 2019-07-24 stsp repo);
7421 0ebf8283 2019-07-24 stsp if (err)
7422 0ebf8283 2019-07-24 stsp break;
7423 0ebf8283 2019-07-24 stsp
7424 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7425 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7426 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7427 0ebf8283 2019-07-24 stsp if (n < 0) {
7428 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7429 0ebf8283 2019-07-24 stsp break;
7430 0ebf8283 2019-07-24 stsp }
7431 0ebf8283 2019-07-24 stsp }
7432 0ebf8283 2019-07-24 stsp }
7433 0ebf8283 2019-07-24 stsp done:
7434 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7435 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7436 0ebf8283 2019-07-24 stsp free(path);
7437 0ebf8283 2019-07-24 stsp if (commit)
7438 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7439 0ebf8283 2019-07-24 stsp return err;
7440 0ebf8283 2019-07-24 stsp }
7441 0ebf8283 2019-07-24 stsp
7442 bfce7f83 2019-07-27 stsp void
7443 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7444 bfce7f83 2019-07-27 stsp {
7445 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7446 bfce7f83 2019-07-27 stsp
7447 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7448 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7449 bfce7f83 2019-07-27 stsp free(hle);
7450 bfce7f83 2019-07-27 stsp }
7451 bfce7f83 2019-07-27 stsp }
7452 bfce7f83 2019-07-27 stsp
7453 0ebf8283 2019-07-24 stsp static const struct got_error *
7454 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7455 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7456 0ebf8283 2019-07-24 stsp {
7457 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7458 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7459 0ebf8283 2019-07-24 stsp
7460 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7461 0ebf8283 2019-07-24 stsp if (f == NULL) {
7462 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7463 0ebf8283 2019-07-24 stsp goto done;
7464 0ebf8283 2019-07-24 stsp }
7465 0ebf8283 2019-07-24 stsp
7466 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7467 0ebf8283 2019-07-24 stsp done:
7468 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7469 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7470 0ebf8283 2019-07-24 stsp return err;
7471 0ebf8283 2019-07-24 stsp }
7472 0ebf8283 2019-07-24 stsp
7473 0ebf8283 2019-07-24 stsp static const struct got_error *
7474 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7475 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7476 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7477 0ebf8283 2019-07-24 stsp {
7478 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7479 0ebf8283 2019-07-24 stsp int resp = ' ';
7480 0ebf8283 2019-07-24 stsp
7481 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7482 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7483 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7484 0ebf8283 2019-07-24 stsp resp = getchar();
7485 a61a4414 2019-08-07 stsp if (resp == '\n')
7486 a61a4414 2019-08-07 stsp resp = getchar();
7487 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7488 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7489 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7490 bfce7f83 2019-07-27 stsp repo);
7491 426ebf2e 2019-07-25 stsp if (err) {
7492 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7493 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7494 426ebf2e 2019-07-25 stsp break;
7495 bfce7f83 2019-07-27 stsp prev_err = err;
7496 426ebf2e 2019-07-25 stsp resp = ' ';
7497 426ebf2e 2019-07-25 stsp continue;
7498 426ebf2e 2019-07-25 stsp }
7499 bfce7f83 2019-07-27 stsp break;
7500 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7501 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7502 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7503 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7504 426ebf2e 2019-07-25 stsp if (err) {
7505 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7506 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7507 426ebf2e 2019-07-25 stsp break;
7508 bfce7f83 2019-07-27 stsp prev_err = err;
7509 426ebf2e 2019-07-25 stsp resp = ' ';
7510 426ebf2e 2019-07-25 stsp continue;
7511 426ebf2e 2019-07-25 stsp }
7512 bfce7f83 2019-07-27 stsp break;
7513 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7514 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7515 0ebf8283 2019-07-24 stsp break;
7516 426ebf2e 2019-07-25 stsp } else
7517 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7518 0ebf8283 2019-07-24 stsp }
7519 0ebf8283 2019-07-24 stsp
7520 0ebf8283 2019-07-24 stsp return err;
7521 0ebf8283 2019-07-24 stsp }
7522 0ebf8283 2019-07-24 stsp
7523 0ebf8283 2019-07-24 stsp static const struct got_error *
7524 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7525 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7526 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7527 0ebf8283 2019-07-24 stsp {
7528 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7529 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7530 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7531 3e3a69f1 2019-07-25 stsp branch, repo);
7532 0ebf8283 2019-07-24 stsp }
7533 0ebf8283 2019-07-24 stsp
7534 0ebf8283 2019-07-24 stsp static const struct got_error *
7535 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7536 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7537 0ebf8283 2019-07-24 stsp {
7538 0ebf8283 2019-07-24 stsp const struct got_error *err;
7539 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7540 0ebf8283 2019-07-24 stsp
7541 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7542 0ebf8283 2019-07-24 stsp if (err)
7543 0ebf8283 2019-07-24 stsp goto done;
7544 0ebf8283 2019-07-24 stsp
7545 0ebf8283 2019-07-24 stsp if (new_id) {
7546 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7547 0ebf8283 2019-07-24 stsp if (err)
7548 0ebf8283 2019-07-24 stsp goto done;
7549 0ebf8283 2019-07-24 stsp }
7550 0ebf8283 2019-07-24 stsp
7551 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7552 0ebf8283 2019-07-24 stsp if (new_id_str)
7553 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7554 0ebf8283 2019-07-24 stsp
7555 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7556 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7557 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7558 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7559 0ebf8283 2019-07-24 stsp goto done;
7560 0ebf8283 2019-07-24 stsp }
7561 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7562 0ebf8283 2019-07-24 stsp } else {
7563 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7564 0ebf8283 2019-07-24 stsp if (err)
7565 0ebf8283 2019-07-24 stsp goto done;
7566 0ebf8283 2019-07-24 stsp }
7567 0ebf8283 2019-07-24 stsp
7568 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7569 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7570 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7571 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7572 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7573 0ebf8283 2019-07-24 stsp break;
7574 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7575 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7576 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7577 0ebf8283 2019-07-24 stsp logmsg);
7578 0ebf8283 2019-07-24 stsp break;
7579 0ebf8283 2019-07-24 stsp default:
7580 0ebf8283 2019-07-24 stsp break;
7581 0ebf8283 2019-07-24 stsp }
7582 0ebf8283 2019-07-24 stsp done:
7583 0ebf8283 2019-07-24 stsp free(old_id_str);
7584 0ebf8283 2019-07-24 stsp free(new_id_str);
7585 0ebf8283 2019-07-24 stsp return err;
7586 0ebf8283 2019-07-24 stsp }
7587 0ebf8283 2019-07-24 stsp
7588 0ebf8283 2019-07-24 stsp static const struct got_error *
7589 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7590 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7591 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7592 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7593 0ebf8283 2019-07-24 stsp {
7594 0ebf8283 2019-07-24 stsp const struct got_error *err;
7595 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7596 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7597 0ebf8283 2019-07-24 stsp
7598 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7599 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7600 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7601 0ebf8283 2019-07-24 stsp if (err)
7602 0ebf8283 2019-07-24 stsp return err;
7603 0ebf8283 2019-07-24 stsp }
7604 0ebf8283 2019-07-24 stsp
7605 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7606 0ebf8283 2019-07-24 stsp if (err)
7607 0ebf8283 2019-07-24 stsp return err;
7608 0ebf8283 2019-07-24 stsp
7609 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7610 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7611 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7612 0ebf8283 2019-07-24 stsp if (err) {
7613 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7614 0ebf8283 2019-07-24 stsp goto done;
7615 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7616 0ebf8283 2019-07-24 stsp } else {
7617 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7618 0ebf8283 2019-07-24 stsp free(new_commit_id);
7619 0ebf8283 2019-07-24 stsp }
7620 0ebf8283 2019-07-24 stsp done:
7621 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7622 0ebf8283 2019-07-24 stsp return err;
7623 0ebf8283 2019-07-24 stsp }
7624 0ebf8283 2019-07-24 stsp
7625 0ebf8283 2019-07-24 stsp static const struct got_error *
7626 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7627 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7628 0ebf8283 2019-07-24 stsp {
7629 0ebf8283 2019-07-24 stsp const struct got_error *error;
7630 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7631 0ebf8283 2019-07-24 stsp
7632 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7633 0ebf8283 2019-07-24 stsp repo);
7634 0ebf8283 2019-07-24 stsp if (error)
7635 0ebf8283 2019-07-24 stsp return error;
7636 0ebf8283 2019-07-24 stsp
7637 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7638 0ebf8283 2019-07-24 stsp if (error)
7639 0ebf8283 2019-07-24 stsp return error;
7640 0ebf8283 2019-07-24 stsp
7641 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7642 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7643 0ebf8283 2019-07-24 stsp return error;
7644 ab20a43a 2020-01-29 stsp }
7645 ab20a43a 2020-01-29 stsp
7646 ab20a43a 2020-01-29 stsp static const struct got_error *
7647 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7648 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7649 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7650 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7651 ab20a43a 2020-01-29 stsp {
7652 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7653 ab20a43a 2020-01-29 stsp
7654 ab20a43a 2020-01-29 stsp switch (status) {
7655 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7656 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7657 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7658 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7659 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7660 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7661 ab20a43a 2020-01-29 stsp default:
7662 ab20a43a 2020-01-29 stsp break;
7663 ab20a43a 2020-01-29 stsp }
7664 ab20a43a 2020-01-29 stsp
7665 ab20a43a 2020-01-29 stsp switch (staged_status) {
7666 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7667 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7668 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7669 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7670 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7671 ab20a43a 2020-01-29 stsp default:
7672 ab20a43a 2020-01-29 stsp break;
7673 ab20a43a 2020-01-29 stsp }
7674 ab20a43a 2020-01-29 stsp
7675 ab20a43a 2020-01-29 stsp return NULL;
7676 0ebf8283 2019-07-24 stsp }
7677 0ebf8283 2019-07-24 stsp
7678 0ebf8283 2019-07-24 stsp static const struct got_error *
7679 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7680 0ebf8283 2019-07-24 stsp {
7681 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7682 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7683 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7684 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7685 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7686 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7687 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7688 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7689 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7690 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7691 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7692 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7693 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7694 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7695 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7696 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7697 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7698 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7699 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7700 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7701 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7702 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7703 0ebf8283 2019-07-24 stsp
7704 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7705 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7706 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7707 0ebf8283 2019-07-24 stsp
7708 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7709 0ebf8283 2019-07-24 stsp switch (ch) {
7710 0ebf8283 2019-07-24 stsp case 'a':
7711 0ebf8283 2019-07-24 stsp abort_edit = 1;
7712 0ebf8283 2019-07-24 stsp break;
7713 0ebf8283 2019-07-24 stsp case 'c':
7714 0ebf8283 2019-07-24 stsp continue_edit = 1;
7715 0ebf8283 2019-07-24 stsp break;
7716 0ebf8283 2019-07-24 stsp case 'F':
7717 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7718 0ebf8283 2019-07-24 stsp break;
7719 083957f4 2020-02-24 stsp case 'm':
7720 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7721 083957f4 2020-02-24 stsp break;
7722 0ebf8283 2019-07-24 stsp default:
7723 0ebf8283 2019-07-24 stsp usage_histedit();
7724 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7725 0ebf8283 2019-07-24 stsp }
7726 0ebf8283 2019-07-24 stsp }
7727 0ebf8283 2019-07-24 stsp
7728 0ebf8283 2019-07-24 stsp argc -= optind;
7729 0ebf8283 2019-07-24 stsp argv += optind;
7730 0ebf8283 2019-07-24 stsp
7731 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7732 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7733 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7734 0ebf8283 2019-07-24 stsp err(1, "pledge");
7735 0ebf8283 2019-07-24 stsp #endif
7736 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7737 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7738 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7739 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7740 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7741 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7742 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7743 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7744 0ebf8283 2019-07-24 stsp if (argc != 0)
7745 0ebf8283 2019-07-24 stsp usage_histedit();
7746 0ebf8283 2019-07-24 stsp
7747 0ebf8283 2019-07-24 stsp /*
7748 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7749 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7750 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7751 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7752 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7753 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7754 0ebf8283 2019-07-24 stsp */
7755 0ebf8283 2019-07-24 stsp
7756 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7757 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7758 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7759 0ebf8283 2019-07-24 stsp goto done;
7760 0ebf8283 2019-07-24 stsp }
7761 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7762 0ebf8283 2019-07-24 stsp if (error)
7763 0ebf8283 2019-07-24 stsp goto done;
7764 0ebf8283 2019-07-24 stsp
7765 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7766 c9956ddf 2019-09-08 stsp NULL);
7767 0ebf8283 2019-07-24 stsp if (error != NULL)
7768 0ebf8283 2019-07-24 stsp goto done;
7769 0ebf8283 2019-07-24 stsp
7770 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7771 0ebf8283 2019-07-24 stsp if (error)
7772 0ebf8283 2019-07-24 stsp goto done;
7773 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7774 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7775 0ebf8283 2019-07-24 stsp goto done;
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp
7778 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7779 0ebf8283 2019-07-24 stsp if (error)
7780 0ebf8283 2019-07-24 stsp goto done;
7781 0ebf8283 2019-07-24 stsp
7782 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7783 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7784 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7785 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7786 083957f4 2020-02-24 stsp "before the -m option can be used");
7787 083957f4 2020-02-24 stsp goto done;
7788 083957f4 2020-02-24 stsp }
7789 083957f4 2020-02-24 stsp
7790 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7791 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7792 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7793 3e3a69f1 2019-07-25 stsp worktree, repo);
7794 0ebf8283 2019-07-24 stsp if (error)
7795 0ebf8283 2019-07-24 stsp goto done;
7796 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7797 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7798 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7799 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7800 0ebf8283 2019-07-24 stsp if (error)
7801 0ebf8283 2019-07-24 stsp goto done;
7802 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7803 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7804 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7805 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7806 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7807 0ebf8283 2019-07-24 stsp goto done;
7808 0ebf8283 2019-07-24 stsp }
7809 0ebf8283 2019-07-24 stsp
7810 0ebf8283 2019-07-24 stsp if (continue_edit) {
7811 0ebf8283 2019-07-24 stsp char *path;
7812 0ebf8283 2019-07-24 stsp
7813 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7814 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7815 0ebf8283 2019-07-24 stsp goto done;
7816 0ebf8283 2019-07-24 stsp }
7817 0ebf8283 2019-07-24 stsp
7818 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7819 0ebf8283 2019-07-24 stsp if (error)
7820 0ebf8283 2019-07-24 stsp goto done;
7821 0ebf8283 2019-07-24 stsp
7822 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7823 0ebf8283 2019-07-24 stsp free(path);
7824 0ebf8283 2019-07-24 stsp if (error)
7825 0ebf8283 2019-07-24 stsp goto done;
7826 0ebf8283 2019-07-24 stsp
7827 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7828 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7829 3e3a69f1 2019-07-25 stsp worktree, repo);
7830 0ebf8283 2019-07-24 stsp if (error)
7831 0ebf8283 2019-07-24 stsp goto done;
7832 0ebf8283 2019-07-24 stsp
7833 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7834 0ebf8283 2019-07-24 stsp if (error)
7835 0ebf8283 2019-07-24 stsp goto done;
7836 0ebf8283 2019-07-24 stsp
7837 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7838 0ebf8283 2019-07-24 stsp head_commit_id);
7839 0ebf8283 2019-07-24 stsp if (error)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7842 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7843 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7844 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7845 3aac7cf7 2019-07-25 stsp goto done;
7846 3aac7cf7 2019-07-25 stsp }
7847 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7848 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7849 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7850 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7851 0ebf8283 2019-07-24 stsp commit = NULL;
7852 0ebf8283 2019-07-24 stsp if (error)
7853 0ebf8283 2019-07-24 stsp goto done;
7854 0ebf8283 2019-07-24 stsp } else {
7855 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7856 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7857 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp }
7859 0ebf8283 2019-07-24 stsp
7860 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7861 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7862 0ebf8283 2019-07-24 stsp if (error != NULL)
7863 0ebf8283 2019-07-24 stsp goto done;
7864 0ebf8283 2019-07-24 stsp
7865 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7866 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7867 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7868 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7869 c7d20a3f 2019-07-30 stsp goto done;
7870 c7d20a3f 2019-07-30 stsp }
7871 c7d20a3f 2019-07-30 stsp
7872 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7873 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7874 bfce7f83 2019-07-27 stsp branch = NULL;
7875 0ebf8283 2019-07-24 stsp if (error)
7876 0ebf8283 2019-07-24 stsp goto done;
7877 0ebf8283 2019-07-24 stsp
7878 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7879 0ebf8283 2019-07-24 stsp head_commit_id);
7880 0ebf8283 2019-07-24 stsp if (error)
7881 0ebf8283 2019-07-24 stsp goto done;
7882 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7883 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7884 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7885 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7886 3aac7cf7 2019-07-25 stsp goto done;
7887 3aac7cf7 2019-07-25 stsp }
7888 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7889 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7890 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7891 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7892 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7893 0ebf8283 2019-07-24 stsp commit = NULL;
7894 0ebf8283 2019-07-24 stsp if (error)
7895 0ebf8283 2019-07-24 stsp goto done;
7896 0ebf8283 2019-07-24 stsp
7897 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7898 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7899 c5996fff 2020-01-29 stsp goto done;
7900 c5996fff 2020-01-29 stsp }
7901 c5996fff 2020-01-29 stsp
7902 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7903 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7904 bfce7f83 2019-07-27 stsp if (error)
7905 bfce7f83 2019-07-27 stsp goto done;
7906 bfce7f83 2019-07-27 stsp
7907 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7908 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7909 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7910 6ba2bea2 2019-07-27 stsp if (error) {
7911 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7912 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7913 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7914 0ebf8283 2019-07-24 stsp goto done;
7915 6ba2bea2 2019-07-27 stsp }
7916 0ebf8283 2019-07-24 stsp } else {
7917 514f2ffe 2020-01-29 stsp const char *branch_name;
7918 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7919 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7920 514f2ffe 2020-01-29 stsp branch_name += 11;
7921 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7922 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7923 6ba2bea2 2019-07-27 stsp if (error) {
7924 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7925 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7926 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7927 0ebf8283 2019-07-24 stsp goto done;
7928 6ba2bea2 2019-07-27 stsp }
7929 0ebf8283 2019-07-24 stsp
7930 0ebf8283 2019-07-24 stsp }
7931 0ebf8283 2019-07-24 stsp
7932 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7933 0ebf8283 2019-07-24 stsp repo);
7934 6ba2bea2 2019-07-27 stsp if (error) {
7935 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7936 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7937 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7938 0ebf8283 2019-07-24 stsp goto done;
7939 6ba2bea2 2019-07-27 stsp }
7940 0ebf8283 2019-07-24 stsp
7941 0ebf8283 2019-07-24 stsp }
7942 0ebf8283 2019-07-24 stsp
7943 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7944 4ec14e60 2019-08-28 hiltjo if (error)
7945 0ebf8283 2019-07-24 stsp goto done;
7946 0ebf8283 2019-07-24 stsp
7947 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7948 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7949 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7950 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7951 0ebf8283 2019-07-24 stsp continue;
7952 0ebf8283 2019-07-24 stsp
7953 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7954 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7955 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7956 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7957 0ebf8283 2019-07-24 stsp repo);
7958 ab20a43a 2020-01-29 stsp if (error)
7959 ab20a43a 2020-01-29 stsp goto done;
7960 0ebf8283 2019-07-24 stsp } else {
7961 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7962 ab20a43a 2020-01-29 stsp int have_changes = 0;
7963 ab20a43a 2020-01-29 stsp
7964 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7965 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7966 ab20a43a 2020-01-29 stsp if (error)
7967 ab20a43a 2020-01-29 stsp goto done;
7968 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7969 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7970 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7971 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7972 ab20a43a 2020-01-29 stsp if (error) {
7973 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7974 ab20a43a 2020-01-29 stsp goto done;
7975 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7976 ab20a43a 2020-01-29 stsp goto done;
7977 ab20a43a 2020-01-29 stsp }
7978 ab20a43a 2020-01-29 stsp if (have_changes) {
7979 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7980 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7981 ab20a43a 2020-01-29 stsp if (error)
7982 ab20a43a 2020-01-29 stsp goto done;
7983 ab20a43a 2020-01-29 stsp } else {
7984 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7985 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7986 ab20a43a 2020-01-29 stsp if (error)
7987 ab20a43a 2020-01-29 stsp goto done;
7988 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7989 ab20a43a 2020-01-29 stsp hle, NULL);
7990 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7991 ab20a43a 2020-01-29 stsp commit = NULL;
7992 ab20a43a 2020-01-29 stsp if (error)
7993 ab20a43a 2020-01-29 stsp goto done;
7994 ab20a43a 2020-01-29 stsp }
7995 0ebf8283 2019-07-24 stsp }
7996 0ebf8283 2019-07-24 stsp continue;
7997 0ebf8283 2019-07-24 stsp }
7998 0ebf8283 2019-07-24 stsp
7999 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8000 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8001 0ebf8283 2019-07-24 stsp if (error)
8002 0ebf8283 2019-07-24 stsp goto done;
8003 0ebf8283 2019-07-24 stsp continue;
8004 0ebf8283 2019-07-24 stsp }
8005 0ebf8283 2019-07-24 stsp
8006 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8007 0ebf8283 2019-07-24 stsp hle->commit_id);
8008 0ebf8283 2019-07-24 stsp if (error)
8009 0ebf8283 2019-07-24 stsp goto done;
8010 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8011 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8012 0ebf8283 2019-07-24 stsp
8013 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8014 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8015 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
8016 0ebf8283 2019-07-24 stsp if (error)
8017 0ebf8283 2019-07-24 stsp goto done;
8018 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8019 0ebf8283 2019-07-24 stsp commit = NULL;
8020 0ebf8283 2019-07-24 stsp
8021 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8022 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8023 a0ea4fc0 2020-02-28 stsp repo);
8024 a0ea4fc0 2020-02-28 stsp if (error)
8025 a0ea4fc0 2020-02-28 stsp goto done;
8026 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8027 0ebf8283 2019-07-24 stsp break;
8028 0ebf8283 2019-07-24 stsp }
8029 0ebf8283 2019-07-24 stsp
8030 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8031 0ebf8283 2019-07-24 stsp char *id_str;
8032 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8033 0ebf8283 2019-07-24 stsp if (error)
8034 0ebf8283 2019-07-24 stsp goto done;
8035 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8036 0ebf8283 2019-07-24 stsp id_str);
8037 0ebf8283 2019-07-24 stsp free(id_str);
8038 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8039 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8040 3e3a69f1 2019-07-25 stsp fileindex);
8041 0ebf8283 2019-07-24 stsp goto done;
8042 0ebf8283 2019-07-24 stsp }
8043 0ebf8283 2019-07-24 stsp
8044 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8045 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8046 0ebf8283 2019-07-24 stsp if (error)
8047 0ebf8283 2019-07-24 stsp goto done;
8048 0ebf8283 2019-07-24 stsp continue;
8049 0ebf8283 2019-07-24 stsp }
8050 0ebf8283 2019-07-24 stsp
8051 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8052 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8053 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8054 0ebf8283 2019-07-24 stsp if (error)
8055 0ebf8283 2019-07-24 stsp goto done;
8056 0ebf8283 2019-07-24 stsp }
8057 0ebf8283 2019-07-24 stsp
8058 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8059 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8060 0ebf8283 2019-07-24 stsp if (error)
8061 0ebf8283 2019-07-24 stsp goto done;
8062 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8063 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8064 0ebf8283 2019-07-24 stsp } else
8065 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8066 3e3a69f1 2019-07-25 stsp branch, repo);
8067 0ebf8283 2019-07-24 stsp done:
8068 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8069 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8070 0ebf8283 2019-07-24 stsp free(head_commit_id);
8071 0ebf8283 2019-07-24 stsp free(base_commit_id);
8072 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8073 0ebf8283 2019-07-24 stsp if (commit)
8074 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8075 0ebf8283 2019-07-24 stsp if (branch)
8076 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8077 0ebf8283 2019-07-24 stsp if (tmp_branch)
8078 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8079 0ebf8283 2019-07-24 stsp if (worktree)
8080 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8081 2822a352 2019-10-15 stsp if (repo)
8082 2822a352 2019-10-15 stsp got_repo_close(repo);
8083 2822a352 2019-10-15 stsp return error;
8084 2822a352 2019-10-15 stsp }
8085 2822a352 2019-10-15 stsp
8086 2822a352 2019-10-15 stsp __dead static void
8087 2822a352 2019-10-15 stsp usage_integrate(void)
8088 2822a352 2019-10-15 stsp {
8089 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8090 2822a352 2019-10-15 stsp exit(1);
8091 2822a352 2019-10-15 stsp }
8092 2822a352 2019-10-15 stsp
8093 2822a352 2019-10-15 stsp static const struct got_error *
8094 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8095 2822a352 2019-10-15 stsp {
8096 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8097 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8098 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8099 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8100 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8101 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8102 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8103 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8104 2822a352 2019-10-15 stsp int ch, did_something = 0;
8105 2822a352 2019-10-15 stsp
8106 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8107 2822a352 2019-10-15 stsp switch (ch) {
8108 2822a352 2019-10-15 stsp default:
8109 2822a352 2019-10-15 stsp usage_integrate();
8110 2822a352 2019-10-15 stsp /* NOTREACHED */
8111 2822a352 2019-10-15 stsp }
8112 2822a352 2019-10-15 stsp }
8113 2822a352 2019-10-15 stsp
8114 2822a352 2019-10-15 stsp argc -= optind;
8115 2822a352 2019-10-15 stsp argv += optind;
8116 2822a352 2019-10-15 stsp
8117 2822a352 2019-10-15 stsp if (argc != 1)
8118 2822a352 2019-10-15 stsp usage_integrate();
8119 2822a352 2019-10-15 stsp branch_arg = argv[0];
8120 2822a352 2019-10-15 stsp
8121 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8122 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8123 2822a352 2019-10-15 stsp err(1, "pledge");
8124 2822a352 2019-10-15 stsp
8125 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8126 2822a352 2019-10-15 stsp if (cwd == NULL) {
8127 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8128 2822a352 2019-10-15 stsp goto done;
8129 2822a352 2019-10-15 stsp }
8130 2822a352 2019-10-15 stsp
8131 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8132 2822a352 2019-10-15 stsp if (error)
8133 2822a352 2019-10-15 stsp goto done;
8134 2822a352 2019-10-15 stsp
8135 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8136 2822a352 2019-10-15 stsp if (error)
8137 2822a352 2019-10-15 stsp goto done;
8138 2822a352 2019-10-15 stsp
8139 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8140 2822a352 2019-10-15 stsp NULL);
8141 2822a352 2019-10-15 stsp if (error != NULL)
8142 2822a352 2019-10-15 stsp goto done;
8143 2822a352 2019-10-15 stsp
8144 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8145 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8146 2822a352 2019-10-15 stsp if (error)
8147 2822a352 2019-10-15 stsp goto done;
8148 2822a352 2019-10-15 stsp
8149 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8150 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8151 2822a352 2019-10-15 stsp goto done;
8152 2822a352 2019-10-15 stsp }
8153 2822a352 2019-10-15 stsp
8154 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8155 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8156 2822a352 2019-10-15 stsp if (error)
8157 2822a352 2019-10-15 stsp goto done;
8158 2822a352 2019-10-15 stsp
8159 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8160 2822a352 2019-10-15 stsp if (refname == NULL) {
8161 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8162 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8163 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8164 2822a352 2019-10-15 stsp goto done;
8165 2822a352 2019-10-15 stsp }
8166 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8167 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8168 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8169 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8170 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8171 2822a352 2019-10-15 stsp goto done;
8172 2822a352 2019-10-15 stsp }
8173 2822a352 2019-10-15 stsp
8174 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8175 2822a352 2019-10-15 stsp if (error)
8176 2822a352 2019-10-15 stsp goto done;
8177 2822a352 2019-10-15 stsp
8178 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8179 2822a352 2019-10-15 stsp if (error)
8180 2822a352 2019-10-15 stsp goto done;
8181 2822a352 2019-10-15 stsp
8182 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8183 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8184 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8185 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8186 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8187 2822a352 2019-10-15 stsp goto done;
8188 2822a352 2019-10-15 stsp }
8189 2822a352 2019-10-15 stsp
8190 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8191 2822a352 2019-10-15 stsp if (error) {
8192 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8193 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8194 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8195 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8196 2822a352 2019-10-15 stsp goto done;
8197 2822a352 2019-10-15 stsp }
8198 2822a352 2019-10-15 stsp
8199 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8200 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8201 2822a352 2019-10-15 stsp check_cancelled, NULL);
8202 2822a352 2019-10-15 stsp if (error)
8203 2822a352 2019-10-15 stsp goto done;
8204 2822a352 2019-10-15 stsp
8205 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8206 2822a352 2019-10-15 stsp done:
8207 715dc77e 2019-08-03 stsp if (repo)
8208 715dc77e 2019-08-03 stsp got_repo_close(repo);
8209 2822a352 2019-10-15 stsp if (worktree)
8210 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8211 2822a352 2019-10-15 stsp free(cwd);
8212 2822a352 2019-10-15 stsp free(base_commit_id);
8213 2822a352 2019-10-15 stsp free(commit_id);
8214 2822a352 2019-10-15 stsp free(refname);
8215 2822a352 2019-10-15 stsp free(base_refname);
8216 715dc77e 2019-08-03 stsp return error;
8217 715dc77e 2019-08-03 stsp }
8218 715dc77e 2019-08-03 stsp
8219 715dc77e 2019-08-03 stsp __dead static void
8220 715dc77e 2019-08-03 stsp usage_stage(void)
8221 715dc77e 2019-08-03 stsp {
8222 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8223 f3055044 2019-08-07 stsp "[file-path ...]\n",
8224 715dc77e 2019-08-03 stsp getprogname());
8225 715dc77e 2019-08-03 stsp exit(1);
8226 715dc77e 2019-08-03 stsp }
8227 715dc77e 2019-08-03 stsp
8228 715dc77e 2019-08-03 stsp static const struct got_error *
8229 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8230 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8231 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8232 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8233 408b4ebc 2019-08-03 stsp {
8234 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8235 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8236 408b4ebc 2019-08-03 stsp
8237 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8238 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8239 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8240 408b4ebc 2019-08-03 stsp return NULL;
8241 408b4ebc 2019-08-03 stsp
8242 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8243 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8244 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8245 408b4ebc 2019-08-03 stsp else
8246 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8247 408b4ebc 2019-08-03 stsp if (err)
8248 408b4ebc 2019-08-03 stsp return err;
8249 408b4ebc 2019-08-03 stsp
8250 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8251 408b4ebc 2019-08-03 stsp free(id_str);
8252 dc424a06 2019-08-07 stsp return NULL;
8253 dc424a06 2019-08-07 stsp }
8254 2e1f37b0 2019-08-08 stsp
8255 dc424a06 2019-08-07 stsp static const struct got_error *
8256 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8257 715dc77e 2019-08-03 stsp {
8258 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8259 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8260 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8261 715dc77e 2019-08-03 stsp char *cwd = NULL;
8262 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8263 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8264 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8265 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8266 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8267 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8268 715dc77e 2019-08-03 stsp
8269 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8270 715dc77e 2019-08-03 stsp
8271 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8272 715dc77e 2019-08-03 stsp switch (ch) {
8273 408b4ebc 2019-08-03 stsp case 'l':
8274 408b4ebc 2019-08-03 stsp list_stage = 1;
8275 408b4ebc 2019-08-03 stsp break;
8276 dc424a06 2019-08-07 stsp case 'p':
8277 dc424a06 2019-08-07 stsp pflag = 1;
8278 dc424a06 2019-08-07 stsp break;
8279 dc424a06 2019-08-07 stsp case 'F':
8280 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8281 dc424a06 2019-08-07 stsp break;
8282 715dc77e 2019-08-03 stsp default:
8283 715dc77e 2019-08-03 stsp usage_stage();
8284 715dc77e 2019-08-03 stsp /* NOTREACHED */
8285 715dc77e 2019-08-03 stsp }
8286 715dc77e 2019-08-03 stsp }
8287 715dc77e 2019-08-03 stsp
8288 715dc77e 2019-08-03 stsp argc -= optind;
8289 715dc77e 2019-08-03 stsp argv += optind;
8290 715dc77e 2019-08-03 stsp
8291 715dc77e 2019-08-03 stsp #ifndef PROFILE
8292 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8293 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8294 715dc77e 2019-08-03 stsp err(1, "pledge");
8295 715dc77e 2019-08-03 stsp #endif
8296 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8297 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8298 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8299 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8300 715dc77e 2019-08-03 stsp
8301 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8302 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8303 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8304 715dc77e 2019-08-03 stsp goto done;
8305 715dc77e 2019-08-03 stsp }
8306 715dc77e 2019-08-03 stsp
8307 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8308 715dc77e 2019-08-03 stsp if (error)
8309 715dc77e 2019-08-03 stsp goto done;
8310 715dc77e 2019-08-03 stsp
8311 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8312 c9956ddf 2019-09-08 stsp NULL);
8313 715dc77e 2019-08-03 stsp if (error != NULL)
8314 715dc77e 2019-08-03 stsp goto done;
8315 715dc77e 2019-08-03 stsp
8316 dc424a06 2019-08-07 stsp if (patch_script_path) {
8317 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8318 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8319 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8320 dc424a06 2019-08-07 stsp patch_script_path);
8321 dc424a06 2019-08-07 stsp goto done;
8322 dc424a06 2019-08-07 stsp }
8323 dc424a06 2019-08-07 stsp }
8324 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8325 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8326 715dc77e 2019-08-03 stsp if (error)
8327 715dc77e 2019-08-03 stsp goto done;
8328 715dc77e 2019-08-03 stsp
8329 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8330 6d022e97 2019-08-04 stsp if (error)
8331 6d022e97 2019-08-04 stsp goto done;
8332 715dc77e 2019-08-03 stsp
8333 6d022e97 2019-08-04 stsp if (list_stage)
8334 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8335 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8336 2e1f37b0 2019-08-08 stsp else {
8337 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8338 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8339 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8340 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8341 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8342 2e1f37b0 2019-08-08 stsp }
8343 ad493afc 2019-08-03 stsp done:
8344 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8345 2e1f37b0 2019-08-08 stsp error == NULL)
8346 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8347 ad493afc 2019-08-03 stsp if (repo)
8348 ad493afc 2019-08-03 stsp got_repo_close(repo);
8349 ad493afc 2019-08-03 stsp if (worktree)
8350 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8351 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8352 ad493afc 2019-08-03 stsp free((char *)pe->path);
8353 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8354 ad493afc 2019-08-03 stsp free(cwd);
8355 ad493afc 2019-08-03 stsp return error;
8356 ad493afc 2019-08-03 stsp }
8357 ad493afc 2019-08-03 stsp
8358 ad493afc 2019-08-03 stsp __dead static void
8359 ad493afc 2019-08-03 stsp usage_unstage(void)
8360 ad493afc 2019-08-03 stsp {
8361 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8362 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8363 ad493afc 2019-08-03 stsp getprogname());
8364 ad493afc 2019-08-03 stsp exit(1);
8365 ad493afc 2019-08-03 stsp }
8366 ad493afc 2019-08-03 stsp
8367 ad493afc 2019-08-03 stsp
8368 ad493afc 2019-08-03 stsp static const struct got_error *
8369 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8370 ad493afc 2019-08-03 stsp {
8371 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8372 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8373 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8374 ad493afc 2019-08-03 stsp char *cwd = NULL;
8375 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8376 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8377 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8378 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8379 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8380 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8381 ad493afc 2019-08-03 stsp
8382 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8383 ad493afc 2019-08-03 stsp
8384 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8385 ad493afc 2019-08-03 stsp switch (ch) {
8386 2e1f37b0 2019-08-08 stsp case 'p':
8387 2e1f37b0 2019-08-08 stsp pflag = 1;
8388 2e1f37b0 2019-08-08 stsp break;
8389 2e1f37b0 2019-08-08 stsp case 'F':
8390 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8391 2e1f37b0 2019-08-08 stsp break;
8392 ad493afc 2019-08-03 stsp default:
8393 ad493afc 2019-08-03 stsp usage_unstage();
8394 ad493afc 2019-08-03 stsp /* NOTREACHED */
8395 ad493afc 2019-08-03 stsp }
8396 ad493afc 2019-08-03 stsp }
8397 ad493afc 2019-08-03 stsp
8398 ad493afc 2019-08-03 stsp argc -= optind;
8399 ad493afc 2019-08-03 stsp argv += optind;
8400 ad493afc 2019-08-03 stsp
8401 ad493afc 2019-08-03 stsp #ifndef PROFILE
8402 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8403 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8404 ad493afc 2019-08-03 stsp err(1, "pledge");
8405 ad493afc 2019-08-03 stsp #endif
8406 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8407 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8408 2e1f37b0 2019-08-08 stsp
8409 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8410 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8411 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8412 ad493afc 2019-08-03 stsp goto done;
8413 ad493afc 2019-08-03 stsp }
8414 ad493afc 2019-08-03 stsp
8415 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8416 ad493afc 2019-08-03 stsp if (error)
8417 ad493afc 2019-08-03 stsp goto done;
8418 ad493afc 2019-08-03 stsp
8419 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8420 c9956ddf 2019-09-08 stsp NULL);
8421 ad493afc 2019-08-03 stsp if (error != NULL)
8422 ad493afc 2019-08-03 stsp goto done;
8423 ad493afc 2019-08-03 stsp
8424 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8425 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8426 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8427 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8428 2e1f37b0 2019-08-08 stsp patch_script_path);
8429 2e1f37b0 2019-08-08 stsp goto done;
8430 2e1f37b0 2019-08-08 stsp }
8431 2e1f37b0 2019-08-08 stsp }
8432 2e1f37b0 2019-08-08 stsp
8433 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8434 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8435 ad493afc 2019-08-03 stsp if (error)
8436 ad493afc 2019-08-03 stsp goto done;
8437 ad493afc 2019-08-03 stsp
8438 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8439 ad493afc 2019-08-03 stsp if (error)
8440 ad493afc 2019-08-03 stsp goto done;
8441 ad493afc 2019-08-03 stsp
8442 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8443 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8444 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8445 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8446 715dc77e 2019-08-03 stsp done:
8447 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8448 2e1f37b0 2019-08-08 stsp error == NULL)
8449 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8450 0ebf8283 2019-07-24 stsp if (repo)
8451 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8452 715dc77e 2019-08-03 stsp if (worktree)
8453 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8454 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8455 715dc77e 2019-08-03 stsp free((char *)pe->path);
8456 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8457 715dc77e 2019-08-03 stsp free(cwd);
8458 01073a5d 2019-08-22 stsp return error;
8459 01073a5d 2019-08-22 stsp }
8460 01073a5d 2019-08-22 stsp
8461 01073a5d 2019-08-22 stsp __dead static void
8462 01073a5d 2019-08-22 stsp usage_cat(void)
8463 01073a5d 2019-08-22 stsp {
8464 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8465 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8466 01073a5d 2019-08-22 stsp exit(1);
8467 01073a5d 2019-08-22 stsp }
8468 01073a5d 2019-08-22 stsp
8469 01073a5d 2019-08-22 stsp static const struct got_error *
8470 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8471 01073a5d 2019-08-22 stsp {
8472 01073a5d 2019-08-22 stsp const struct got_error *err;
8473 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8474 01073a5d 2019-08-22 stsp
8475 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8476 01073a5d 2019-08-22 stsp if (err)
8477 01073a5d 2019-08-22 stsp return err;
8478 01073a5d 2019-08-22 stsp
8479 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8480 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8481 01073a5d 2019-08-22 stsp return err;
8482 01073a5d 2019-08-22 stsp }
8483 01073a5d 2019-08-22 stsp
8484 01073a5d 2019-08-22 stsp static const struct got_error *
8485 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8486 01073a5d 2019-08-22 stsp {
8487 01073a5d 2019-08-22 stsp const struct got_error *err;
8488 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8489 56e0773d 2019-11-28 stsp int nentries, i;
8490 01073a5d 2019-08-22 stsp
8491 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8492 01073a5d 2019-08-22 stsp if (err)
8493 01073a5d 2019-08-22 stsp return err;
8494 01073a5d 2019-08-22 stsp
8495 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8496 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8497 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8498 01073a5d 2019-08-22 stsp char *id_str;
8499 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8500 01073a5d 2019-08-22 stsp break;
8501 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8502 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8503 01073a5d 2019-08-22 stsp if (err)
8504 01073a5d 2019-08-22 stsp break;
8505 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8506 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8507 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8508 01073a5d 2019-08-22 stsp free(id_str);
8509 01073a5d 2019-08-22 stsp }
8510 01073a5d 2019-08-22 stsp
8511 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8512 01073a5d 2019-08-22 stsp return err;
8513 01073a5d 2019-08-22 stsp }
8514 01073a5d 2019-08-22 stsp
8515 01073a5d 2019-08-22 stsp static const struct got_error *
8516 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8517 01073a5d 2019-08-22 stsp {
8518 01073a5d 2019-08-22 stsp const struct got_error *err;
8519 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8520 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8521 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8522 01073a5d 2019-08-22 stsp char *id_str = NULL;
8523 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8524 01073a5d 2019-08-22 stsp
8525 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8526 01073a5d 2019-08-22 stsp if (err)
8527 01073a5d 2019-08-22 stsp return err;
8528 01073a5d 2019-08-22 stsp
8529 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8530 01073a5d 2019-08-22 stsp if (err)
8531 01073a5d 2019-08-22 stsp goto done;
8532 01073a5d 2019-08-22 stsp
8533 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8534 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8535 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8536 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8537 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8538 01073a5d 2019-08-22 stsp char *pid_str;
8539 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8540 01073a5d 2019-08-22 stsp if (err)
8541 01073a5d 2019-08-22 stsp goto done;
8542 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8543 01073a5d 2019-08-22 stsp free(pid_str);
8544 01073a5d 2019-08-22 stsp }
8545 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8546 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8547 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8548 01073a5d 2019-08-22 stsp
8549 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8550 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8551 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8552 01073a5d 2019-08-22 stsp
8553 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8554 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8555 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8556 01073a5d 2019-08-22 stsp done:
8557 01073a5d 2019-08-22 stsp free(id_str);
8558 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8559 01073a5d 2019-08-22 stsp return err;
8560 01073a5d 2019-08-22 stsp }
8561 01073a5d 2019-08-22 stsp
8562 01073a5d 2019-08-22 stsp static const struct got_error *
8563 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8564 01073a5d 2019-08-22 stsp {
8565 01073a5d 2019-08-22 stsp const struct got_error *err;
8566 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8567 01073a5d 2019-08-22 stsp char *id_str = NULL;
8568 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8569 01073a5d 2019-08-22 stsp
8570 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8571 01073a5d 2019-08-22 stsp if (err)
8572 01073a5d 2019-08-22 stsp return err;
8573 01073a5d 2019-08-22 stsp
8574 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8575 01073a5d 2019-08-22 stsp if (err)
8576 01073a5d 2019-08-22 stsp goto done;
8577 01073a5d 2019-08-22 stsp
8578 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8579 e15d5241 2019-08-22 stsp
8580 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8581 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8582 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8583 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8584 01073a5d 2019-08-22 stsp break;
8585 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8586 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8587 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8588 01073a5d 2019-08-22 stsp break;
8589 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8590 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8591 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8592 01073a5d 2019-08-22 stsp break;
8593 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8594 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8595 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8596 01073a5d 2019-08-22 stsp break;
8597 01073a5d 2019-08-22 stsp default:
8598 01073a5d 2019-08-22 stsp break;
8599 01073a5d 2019-08-22 stsp }
8600 01073a5d 2019-08-22 stsp
8601 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8602 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8603 e15d5241 2019-08-22 stsp
8604 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8605 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8606 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8607 01073a5d 2019-08-22 stsp
8608 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8609 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8610 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8611 01073a5d 2019-08-22 stsp done:
8612 01073a5d 2019-08-22 stsp free(id_str);
8613 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8614 01073a5d 2019-08-22 stsp return err;
8615 01073a5d 2019-08-22 stsp }
8616 01073a5d 2019-08-22 stsp
8617 01073a5d 2019-08-22 stsp static const struct got_error *
8618 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8619 01073a5d 2019-08-22 stsp {
8620 01073a5d 2019-08-22 stsp const struct got_error *error;
8621 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8622 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8623 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8624 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8625 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8626 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8627 01073a5d 2019-08-22 stsp
8628 01073a5d 2019-08-22 stsp #ifndef PROFILE
8629 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8630 01073a5d 2019-08-22 stsp NULL) == -1)
8631 01073a5d 2019-08-22 stsp err(1, "pledge");
8632 01073a5d 2019-08-22 stsp #endif
8633 01073a5d 2019-08-22 stsp
8634 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8635 01073a5d 2019-08-22 stsp switch (ch) {
8636 896e9b6f 2019-08-26 stsp case 'c':
8637 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8638 896e9b6f 2019-08-26 stsp break;
8639 01073a5d 2019-08-22 stsp case 'r':
8640 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8641 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8642 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8643 9ba1d308 2019-10-21 stsp optarg);
8644 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8645 01073a5d 2019-08-22 stsp break;
8646 896e9b6f 2019-08-26 stsp case 'P':
8647 896e9b6f 2019-08-26 stsp force_path = 1;
8648 896e9b6f 2019-08-26 stsp break;
8649 01073a5d 2019-08-22 stsp default:
8650 01073a5d 2019-08-22 stsp usage_cat();
8651 01073a5d 2019-08-22 stsp /* NOTREACHED */
8652 01073a5d 2019-08-22 stsp }
8653 01073a5d 2019-08-22 stsp }
8654 01073a5d 2019-08-22 stsp
8655 01073a5d 2019-08-22 stsp argc -= optind;
8656 01073a5d 2019-08-22 stsp argv += optind;
8657 01073a5d 2019-08-22 stsp
8658 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8659 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8660 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8661 01073a5d 2019-08-22 stsp goto done;
8662 01073a5d 2019-08-22 stsp }
8663 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8664 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8665 01073a5d 2019-08-22 stsp goto done;
8666 01073a5d 2019-08-22 stsp if (worktree) {
8667 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8668 01073a5d 2019-08-22 stsp repo_path = strdup(
8669 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8670 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8671 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8672 01073a5d 2019-08-22 stsp goto done;
8673 01073a5d 2019-08-22 stsp }
8674 01073a5d 2019-08-22 stsp }
8675 01073a5d 2019-08-22 stsp }
8676 01073a5d 2019-08-22 stsp
8677 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8678 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8679 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8680 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8681 01073a5d 2019-08-22 stsp }
8682 01073a5d 2019-08-22 stsp
8683 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8684 01073a5d 2019-08-22 stsp free(repo_path);
8685 01073a5d 2019-08-22 stsp if (error != NULL)
8686 01073a5d 2019-08-22 stsp goto done;
8687 01073a5d 2019-08-22 stsp
8688 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8689 896e9b6f 2019-08-26 stsp if (error)
8690 896e9b6f 2019-08-26 stsp goto done;
8691 896e9b6f 2019-08-26 stsp
8692 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8693 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8694 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8695 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8696 01073a5d 2019-08-22 stsp if (error)
8697 01073a5d 2019-08-22 stsp goto done;
8698 01073a5d 2019-08-22 stsp
8699 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8700 896e9b6f 2019-08-26 stsp if (force_path) {
8701 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8702 896e9b6f 2019-08-26 stsp argv[i]);
8703 896e9b6f 2019-08-26 stsp if (error)
8704 896e9b6f 2019-08-26 stsp break;
8705 896e9b6f 2019-08-26 stsp } else {
8706 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8707 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8708 896e9b6f 2019-08-26 stsp if (error) {
8709 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8710 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8711 896e9b6f 2019-08-26 stsp break;
8712 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8713 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8714 896e9b6f 2019-08-26 stsp if (error)
8715 896e9b6f 2019-08-26 stsp break;
8716 896e9b6f 2019-08-26 stsp }
8717 896e9b6f 2019-08-26 stsp }
8718 01073a5d 2019-08-22 stsp
8719 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8720 01073a5d 2019-08-22 stsp if (error)
8721 01073a5d 2019-08-22 stsp break;
8722 01073a5d 2019-08-22 stsp
8723 01073a5d 2019-08-22 stsp switch (obj_type) {
8724 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8725 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8726 01073a5d 2019-08-22 stsp break;
8727 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8728 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8729 01073a5d 2019-08-22 stsp break;
8730 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8731 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8732 01073a5d 2019-08-22 stsp break;
8733 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8734 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8735 01073a5d 2019-08-22 stsp break;
8736 01073a5d 2019-08-22 stsp default:
8737 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8738 01073a5d 2019-08-22 stsp break;
8739 01073a5d 2019-08-22 stsp }
8740 01073a5d 2019-08-22 stsp if (error)
8741 01073a5d 2019-08-22 stsp break;
8742 01073a5d 2019-08-22 stsp free(label);
8743 01073a5d 2019-08-22 stsp label = NULL;
8744 01073a5d 2019-08-22 stsp free(id);
8745 01073a5d 2019-08-22 stsp id = NULL;
8746 01073a5d 2019-08-22 stsp }
8747 01073a5d 2019-08-22 stsp done:
8748 01073a5d 2019-08-22 stsp free(label);
8749 01073a5d 2019-08-22 stsp free(id);
8750 896e9b6f 2019-08-26 stsp free(commit_id);
8751 01073a5d 2019-08-22 stsp if (worktree)
8752 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8753 01073a5d 2019-08-22 stsp if (repo) {
8754 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8755 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8756 01073a5d 2019-08-22 stsp if (error == NULL)
8757 01073a5d 2019-08-22 stsp error = repo_error;
8758 01073a5d 2019-08-22 stsp }
8759 0ebf8283 2019-07-24 stsp return error;
8760 0ebf8283 2019-07-24 stsp }