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 4ba14133 2020-03-20 stsp "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 30718c93 2020-03-21 stsp create_head_ref(struct got_reference *target_ref, int verbosity,
889 30718c93 2020-03-21 stsp 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 4ba14133 2020-03-20 stsp err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, 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 41b0de12 2020-03-21 stsp }
961 41b0de12 2020-03-21 stsp
962 41b0de12 2020-03-21 stsp static const struct got_error *
963 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
964 93658fb9 2020-03-18 stsp {
965 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
966 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
967 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
968 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
969 bb64b798 2020-03-18 stsp const char *repo_path;
970 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
971 4ba14133 2020-03-20 stsp struct got_pathlist_head refs, symrefs, wanted_branches;
972 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
973 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
974 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
975 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
976 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
977 b46f3e71 2020-03-18 stsp char *git_url = NULL;
978 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
979 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
980 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
981 b46f3e71 2020-03-18 stsp ssize_t n;
982 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
983 41b0de12 2020-03-21 stsp int list_refs_only = 0;
984 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
985 93658fb9 2020-03-18 stsp
986 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
987 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
988 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
989 d9b4d0c0 2020-03-18 stsp
990 41b0de12 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvq")) != -1) {
991 93658fb9 2020-03-18 stsp switch (ch) {
992 659e7fbd 2020-03-20 stsp case 'a':
993 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
994 4ba14133 2020-03-20 stsp break;
995 4ba14133 2020-03-20 stsp case 'b':
996 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
997 4ba14133 2020-03-20 stsp optarg, NULL);
998 4ba14133 2020-03-20 stsp if (error)
999 4ba14133 2020-03-20 stsp return error;
1000 659e7fbd 2020-03-20 stsp break;
1001 41b0de12 2020-03-21 stsp case 'l':
1002 41b0de12 2020-03-21 stsp list_refs_only = 1;
1003 41b0de12 2020-03-21 stsp break;
1004 469dd726 2020-03-20 stsp case 'm':
1005 469dd726 2020-03-20 stsp mirror_references = 1;
1006 469dd726 2020-03-20 stsp break;
1007 68999b92 2020-03-18 stsp case 'v':
1008 68999b92 2020-03-18 stsp if (verbosity < 0)
1009 68999b92 2020-03-18 stsp verbosity = 0;
1010 68999b92 2020-03-18 stsp else if (verbosity < 3)
1011 68999b92 2020-03-18 stsp verbosity++;
1012 68999b92 2020-03-18 stsp break;
1013 68999b92 2020-03-18 stsp case 'q':
1014 68999b92 2020-03-18 stsp verbosity = -1;
1015 68999b92 2020-03-18 stsp break;
1016 93658fb9 2020-03-18 stsp default:
1017 93658fb9 2020-03-18 stsp usage_clone();
1018 93658fb9 2020-03-18 stsp break;
1019 93658fb9 2020-03-18 stsp }
1020 93658fb9 2020-03-18 stsp }
1021 93658fb9 2020-03-18 stsp argc -= optind;
1022 93658fb9 2020-03-18 stsp argv += optind;
1023 39c64a6a 2020-03-18 stsp
1024 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1025 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1026 41b0de12 2020-03-21 stsp if (list_refs_only) {
1027 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1028 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1029 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1030 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1031 41b0de12 2020-03-21 stsp if (mirror_references)
1032 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1033 41b0de12 2020-03-21 stsp if (verbosity == -1)
1034 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1035 41b0de12 2020-03-21 stsp }
1036 4ba14133 2020-03-20 stsp
1037 93658fb9 2020-03-18 stsp uri = argv[0];
1038 9df6f38b 2020-03-18 stsp
1039 9df6f38b 2020-03-18 stsp if (argc == 1)
1040 93658fb9 2020-03-18 stsp dirname = NULL;
1041 9df6f38b 2020-03-18 stsp else if (argc == 2)
1042 93658fb9 2020-03-18 stsp dirname = argv[1];
1043 93658fb9 2020-03-18 stsp else
1044 93658fb9 2020-03-18 stsp usage_clone();
1045 09838ffc 2020-03-18 stsp
1046 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1047 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1048 39c64a6a 2020-03-18 stsp if (error)
1049 09f63084 2020-03-20 stsp goto done;
1050 09f63084 2020-03-20 stsp
1051 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1052 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1053 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1054 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1055 09838ffc 2020-03-18 stsp goto done;
1056 09f63084 2020-03-20 stsp }
1057 09838ffc 2020-03-18 stsp
1058 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1059 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1060 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1061 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1062 39c64a6a 2020-03-18 stsp err(1, "pledge");
1063 b46f3e71 2020-03-18 stsp #endif
1064 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1065 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1066 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1067 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1068 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1069 39c64a6a 2020-03-18 stsp err(1, "pledge");
1070 b46f3e71 2020-03-18 stsp #endif
1071 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1072 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1073 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1074 39c64a6a 2020-03-18 stsp goto done;
1075 39c64a6a 2020-03-18 stsp } else {
1076 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1077 39c64a6a 2020-03-18 stsp goto done;
1078 39c64a6a 2020-03-18 stsp }
1079 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1080 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1081 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1082 bb64b798 2020-03-18 stsp goto done;
1083 bb64b798 2020-03-18 stsp }
1084 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1085 bb64b798 2020-03-18 stsp } else
1086 bb64b798 2020-03-18 stsp repo_path = dirname;
1087 bb64b798 2020-03-18 stsp
1088 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1089 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1090 41b0de12 2020-03-21 stsp if (error)
1091 41b0de12 2020-03-21 stsp goto done;
1092 bb64b798 2020-03-18 stsp
1093 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1094 41b0de12 2020-03-21 stsp if (error)
1095 41b0de12 2020-03-21 stsp goto done;
1096 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1097 41b0de12 2020-03-21 stsp if (error)
1098 41b0de12 2020-03-21 stsp goto done;
1099 41b0de12 2020-03-21 stsp }
1100 bb64b798 2020-03-18 stsp
1101 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1102 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1103 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1104 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1105 ee448f5f 2020-03-18 stsp goto done;
1106 ee448f5f 2020-03-18 stsp }
1107 ee448f5f 2020-03-18 stsp }
1108 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1109 ee448f5f 2020-03-18 stsp if (error)
1110 ee448f5f 2020-03-18 stsp goto done;
1111 ee448f5f 2020-03-18 stsp
1112 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1113 9c52365f 2020-03-21 stsp server_path, verbosity);
1114 39c64a6a 2020-03-18 stsp if (error)
1115 bb64b798 2020-03-18 stsp goto done;
1116 294dfefd 2020-03-18 stsp
1117 68999b92 2020-03-18 stsp if (verbosity >= 0)
1118 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1119 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1120 bb64b798 2020-03-18 stsp
1121 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1122 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1123 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1124 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1125 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1126 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1127 41b0de12 2020-03-21 stsp fetch_all_branches, &wanted_branches, list_refs_only,
1128 2690194b 2020-03-21 stsp verbosity, fetchfd, repo, fetch_progress, &fpa);
1129 39c64a6a 2020-03-18 stsp if (error)
1130 d9b4d0c0 2020-03-18 stsp goto done;
1131 d9b4d0c0 2020-03-18 stsp
1132 41b0de12 2020-03-21 stsp if (list_refs_only) {
1133 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1134 41b0de12 2020-03-21 stsp goto done;
1135 41b0de12 2020-03-21 stsp }
1136 41b0de12 2020-03-21 stsp
1137 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1138 39c64a6a 2020-03-18 stsp if (error)
1139 d9b4d0c0 2020-03-18 stsp goto done;
1140 68999b92 2020-03-18 stsp if (verbosity >= 0)
1141 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1142 d9b4d0c0 2020-03-18 stsp free(id_str);
1143 d9b4d0c0 2020-03-18 stsp
1144 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1145 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1146 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1147 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1148 7ebc0570 2020-03-18 stsp char *remote_refname;
1149 668a20f6 2020-03-18 stsp
1150 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1151 39c64a6a 2020-03-18 stsp if (error)
1152 d9b4d0c0 2020-03-18 stsp goto done;
1153 d9b4d0c0 2020-03-18 stsp
1154 469dd726 2020-03-20 stsp if (mirror_references)
1155 469dd726 2020-03-20 stsp continue;
1156 469dd726 2020-03-20 stsp
1157 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1158 7ebc0570 2020-03-18 stsp continue;
1159 7ebc0570 2020-03-18 stsp
1160 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1161 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1162 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1163 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1164 7ebc0570 2020-03-18 stsp goto done;
1165 7ebc0570 2020-03-18 stsp }
1166 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1167 39c64a6a 2020-03-18 stsp if (error)
1168 d9b4d0c0 2020-03-18 stsp goto done;
1169 d9b4d0c0 2020-03-18 stsp }
1170 d9b4d0c0 2020-03-18 stsp
1171 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1172 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1173 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1174 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1175 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1176 d9b4d0c0 2020-03-18 stsp
1177 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1178 d9b4d0c0 2020-03-18 stsp continue;
1179 d9b4d0c0 2020-03-18 stsp
1180 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1181 39c64a6a 2020-03-18 stsp if (error) {
1182 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1183 55330abe 2020-03-20 stsp error = NULL;
1184 d9b4d0c0 2020-03-18 stsp continue;
1185 55330abe 2020-03-20 stsp }
1186 d9b4d0c0 2020-03-18 stsp goto done;
1187 d9b4d0c0 2020-03-18 stsp }
1188 d9b4d0c0 2020-03-18 stsp
1189 30718c93 2020-03-21 stsp error = create_head_ref(target_ref, verbosity, repo);
1190 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1191 39c64a6a 2020-03-18 stsp if (error)
1192 d9b4d0c0 2020-03-18 stsp goto done;
1193 4ba14133 2020-03-20 stsp }
1194 4ba14133 2020-03-20 stsp if (pe == NULL) {
1195 4ba14133 2020-03-20 stsp /*
1196 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1197 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1198 4ba14133 2020-03-20 stsp * which could be fetched instead.
1199 4ba14133 2020-03-20 stsp */
1200 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1201 4ba14133 2020-03-20 stsp const char *target = pe->path;
1202 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1203 d9b4d0c0 2020-03-18 stsp
1204 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1205 4ba14133 2020-03-20 stsp if (error) {
1206 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1207 4ba14133 2020-03-20 stsp error = NULL;
1208 4ba14133 2020-03-20 stsp continue;
1209 4ba14133 2020-03-20 stsp }
1210 4ba14133 2020-03-20 stsp goto done;
1211 4ba14133 2020-03-20 stsp }
1212 4ba14133 2020-03-20 stsp
1213 30718c93 2020-03-21 stsp error = create_head_ref(target_ref, verbosity, repo);
1214 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1215 4ba14133 2020-03-20 stsp if (error)
1216 4ba14133 2020-03-20 stsp goto done;
1217 4ba14133 2020-03-20 stsp break;
1218 4ba14133 2020-03-20 stsp }
1219 659e7fbd 2020-03-20 stsp }
1220 659e7fbd 2020-03-20 stsp
1221 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1222 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1223 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1224 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1225 659e7fbd 2020-03-20 stsp goto done;
1226 659e7fbd 2020-03-20 stsp }
1227 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1228 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1229 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1230 659e7fbd 2020-03-20 stsp goto done;
1231 b46f3e71 2020-03-18 stsp }
1232 659e7fbd 2020-03-20 stsp if (mirror_references) {
1233 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1234 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1235 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1236 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1237 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1238 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1239 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1240 659e7fbd 2020-03-20 stsp goto done;
1241 659e7fbd 2020-03-20 stsp }
1242 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1243 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1244 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1245 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1246 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1247 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1248 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1249 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1250 659e7fbd 2020-03-20 stsp goto done;
1251 659e7fbd 2020-03-20 stsp }
1252 659e7fbd 2020-03-20 stsp } else {
1253 659e7fbd 2020-03-20 stsp const char *branchname;
1254 d715f13e 2020-03-19 stsp
1255 659e7fbd 2020-03-20 stsp /*
1256 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1257 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1258 659e7fbd 2020-03-20 stsp */
1259 659e7fbd 2020-03-20 stsp if (head_symref) {
1260 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1261 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1262 659e7fbd 2020-03-20 stsp branchname += 11;
1263 659e7fbd 2020-03-20 stsp } else
1264 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1265 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1266 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1267 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1268 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1269 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1270 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1271 659e7fbd 2020-03-20 stsp branchname) == -1) {
1272 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1273 659e7fbd 2020-03-20 stsp goto done;
1274 659e7fbd 2020-03-20 stsp }
1275 659e7fbd 2020-03-20 stsp }
1276 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1277 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1278 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1279 659e7fbd 2020-03-20 stsp goto done;
1280 659e7fbd 2020-03-20 stsp }
1281 659e7fbd 2020-03-20 stsp
1282 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1283 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1284 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1285 09838ffc 2020-03-18 stsp done:
1286 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1287 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1288 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1289 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1290 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1291 9c52365f 2020-03-21 stsp }
1292 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1293 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1294 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1295 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1296 bb64b798 2020-03-18 stsp if (repo)
1297 bb64b798 2020-03-18 stsp got_repo_close(repo);
1298 659e7fbd 2020-03-20 stsp if (head_symref)
1299 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1300 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1301 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1302 d9b4d0c0 2020-03-18 stsp free(pe->data);
1303 d9b4d0c0 2020-03-18 stsp }
1304 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1305 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1306 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1307 d9b4d0c0 2020-03-18 stsp free(pe->data);
1308 d9b4d0c0 2020-03-18 stsp }
1309 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1310 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1311 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1312 09838ffc 2020-03-18 stsp free(proto);
1313 09838ffc 2020-03-18 stsp free(host);
1314 09838ffc 2020-03-18 stsp free(port);
1315 09838ffc 2020-03-18 stsp free(server_path);
1316 09838ffc 2020-03-18 stsp free(repo_name);
1317 bb64b798 2020-03-18 stsp free(default_destdir);
1318 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1319 b46f3e71 2020-03-18 stsp free(git_url);
1320 39c64a6a 2020-03-18 stsp return error;
1321 7848a0e1 2020-03-19 stsp }
1322 7848a0e1 2020-03-19 stsp
1323 7848a0e1 2020-03-19 stsp static const struct got_error *
1324 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1325 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1326 7848a0e1 2020-03-19 stsp {
1327 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1328 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1329 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1330 7848a0e1 2020-03-19 stsp
1331 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1332 7848a0e1 2020-03-19 stsp if (err)
1333 7848a0e1 2020-03-19 stsp goto done;
1334 7848a0e1 2020-03-19 stsp
1335 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1336 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1337 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1338 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1339 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1340 db6d8ad8 2020-03-21 stsp }
1341 db6d8ad8 2020-03-21 stsp goto done;
1342 db6d8ad8 2020-03-21 stsp }
1343 db6d8ad8 2020-03-21 stsp
1344 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1345 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1346 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1347 7848a0e1 2020-03-19 stsp if (err)
1348 7848a0e1 2020-03-19 stsp goto done;
1349 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1350 7848a0e1 2020-03-19 stsp if (err)
1351 7848a0e1 2020-03-19 stsp goto done;
1352 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1353 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1354 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1355 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1356 688f11b3 2020-03-21 stsp }
1357 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1358 7848a0e1 2020-03-19 stsp if (err)
1359 7848a0e1 2020-03-19 stsp goto done;
1360 7848a0e1 2020-03-19 stsp } else {
1361 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1362 7848a0e1 2020-03-19 stsp if (err)
1363 7848a0e1 2020-03-19 stsp goto done;
1364 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1365 6338a6a1 2020-03-21 stsp goto done;
1366 6338a6a1 2020-03-21 stsp
1367 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1368 6338a6a1 2020-03-21 stsp if (err)
1369 6338a6a1 2020-03-21 stsp goto done;
1370 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1371 6338a6a1 2020-03-21 stsp if (err)
1372 6338a6a1 2020-03-21 stsp goto done;
1373 7848a0e1 2020-03-19 stsp }
1374 6338a6a1 2020-03-21 stsp
1375 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1376 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1377 6338a6a1 2020-03-21 stsp new_id_str);
1378 7848a0e1 2020-03-19 stsp done:
1379 7848a0e1 2020-03-19 stsp free(old_id);
1380 7848a0e1 2020-03-19 stsp free(new_id_str);
1381 7848a0e1 2020-03-19 stsp return err;
1382 2ab43947 2020-03-18 stsp }
1383 2ab43947 2020-03-18 stsp
1384 2ab43947 2020-03-18 stsp __dead static void
1385 7848a0e1 2020-03-19 stsp usage_fetch(void)
1386 7848a0e1 2020-03-19 stsp {
1387 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1388 db6d8ad8 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [remote-repository-name]\n",
1389 13f12b09 2020-03-21 stsp getprogname());
1390 7848a0e1 2020-03-19 stsp exit(1);
1391 7848a0e1 2020-03-19 stsp }
1392 7848a0e1 2020-03-19 stsp
1393 7848a0e1 2020-03-19 stsp static const struct got_error *
1394 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1395 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1396 f21ec2f0 2020-03-21 stsp {
1397 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1398 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1399 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1400 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1401 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1402 f21ec2f0 2020-03-21 stsp char *id_str;
1403 f21ec2f0 2020-03-21 stsp
1404 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1405 f21ec2f0 2020-03-21 stsp
1406 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1407 f21ec2f0 2020-03-21 stsp if (err)
1408 f21ec2f0 2020-03-21 stsp return err;
1409 f21ec2f0 2020-03-21 stsp
1410 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1411 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1412 f21ec2f0 2020-03-21 stsp
1413 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1414 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1415 f21ec2f0 2020-03-21 stsp continue;
1416 f21ec2f0 2020-03-21 stsp
1417 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1418 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1419 f21ec2f0 2020-03-21 stsp break;
1420 f21ec2f0 2020-03-21 stsp }
1421 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1422 f21ec2f0 2020-03-21 stsp continue;
1423 f21ec2f0 2020-03-21 stsp
1424 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1425 f21ec2f0 2020-03-21 stsp if (err)
1426 f21ec2f0 2020-03-21 stsp break;
1427 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1428 f21ec2f0 2020-03-21 stsp free(id);
1429 f21ec2f0 2020-03-21 stsp if (err)
1430 f21ec2f0 2020-03-21 stsp break;
1431 f21ec2f0 2020-03-21 stsp
1432 f21ec2f0 2020-03-21 stsp free(id_str);
1433 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1434 f21ec2f0 2020-03-21 stsp if (err)
1435 f21ec2f0 2020-03-21 stsp break;
1436 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1437 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1438 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1439 6338a6a1 2020-03-21 stsp }
1440 f21ec2f0 2020-03-21 stsp }
1441 f21ec2f0 2020-03-21 stsp
1442 f21ec2f0 2020-03-21 stsp return err;
1443 f21ec2f0 2020-03-21 stsp }
1444 f21ec2f0 2020-03-21 stsp
1445 f21ec2f0 2020-03-21 stsp static const struct got_error *
1446 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1447 7848a0e1 2020-03-19 stsp {
1448 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1449 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1450 7848a0e1 2020-03-19 stsp const char *remote_name;
1451 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1452 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1453 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1454 7848a0e1 2020-03-19 stsp int nremotes;
1455 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1456 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1457 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1458 4ba14133 2020-03-20 stsp struct got_pathlist_head refs, symrefs, wanted_branches;
1459 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1460 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1461 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1462 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1463 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1464 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1465 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1466 7848a0e1 2020-03-19 stsp
1467 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1468 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1469 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1470 7848a0e1 2020-03-19 stsp
1471 db6d8ad8 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvq")) != -1) {
1472 7848a0e1 2020-03-19 stsp switch (ch) {
1473 659e7fbd 2020-03-20 stsp case 'a':
1474 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1475 4ba14133 2020-03-20 stsp break;
1476 4ba14133 2020-03-20 stsp case 'b':
1477 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1478 4ba14133 2020-03-20 stsp optarg, NULL);
1479 4ba14133 2020-03-20 stsp if (error)
1480 4ba14133 2020-03-20 stsp return error;
1481 41b0de12 2020-03-21 stsp break;
1482 f21ec2f0 2020-03-21 stsp case 'd':
1483 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1484 f21ec2f0 2020-03-21 stsp break;
1485 41b0de12 2020-03-21 stsp case 'l':
1486 41b0de12 2020-03-21 stsp list_refs_only = 1;
1487 659e7fbd 2020-03-20 stsp break;
1488 7848a0e1 2020-03-19 stsp case 'r':
1489 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1490 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1491 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1492 7848a0e1 2020-03-19 stsp optarg);
1493 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1494 7848a0e1 2020-03-19 stsp break;
1495 db6d8ad8 2020-03-21 stsp case 't':
1496 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1497 db6d8ad8 2020-03-21 stsp break;
1498 7848a0e1 2020-03-19 stsp case 'v':
1499 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1500 7848a0e1 2020-03-19 stsp verbosity = 0;
1501 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1502 7848a0e1 2020-03-19 stsp verbosity++;
1503 7848a0e1 2020-03-19 stsp break;
1504 7848a0e1 2020-03-19 stsp case 'q':
1505 7848a0e1 2020-03-19 stsp verbosity = -1;
1506 7848a0e1 2020-03-19 stsp break;
1507 7848a0e1 2020-03-19 stsp default:
1508 7848a0e1 2020-03-19 stsp usage_fetch();
1509 7848a0e1 2020-03-19 stsp break;
1510 7848a0e1 2020-03-19 stsp }
1511 7848a0e1 2020-03-19 stsp }
1512 7848a0e1 2020-03-19 stsp argc -= optind;
1513 7848a0e1 2020-03-19 stsp argv += optind;
1514 7848a0e1 2020-03-19 stsp
1515 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1516 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1517 41b0de12 2020-03-21 stsp if (list_refs_only) {
1518 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1519 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1520 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1521 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1522 f21ec2f0 2020-03-21 stsp if (delete_refs)
1523 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1524 41b0de12 2020-03-21 stsp if (verbosity == -1)
1525 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1526 41b0de12 2020-03-21 stsp }
1527 4ba14133 2020-03-20 stsp
1528 7848a0e1 2020-03-19 stsp if (argc == 0)
1529 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1530 7848a0e1 2020-03-19 stsp else if (argc == 1)
1531 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1532 7848a0e1 2020-03-19 stsp else
1533 7848a0e1 2020-03-19 stsp usage_fetch();
1534 7848a0e1 2020-03-19 stsp
1535 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1536 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1537 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1538 7848a0e1 2020-03-19 stsp goto done;
1539 7848a0e1 2020-03-19 stsp }
1540 7848a0e1 2020-03-19 stsp
1541 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1542 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1543 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1544 7848a0e1 2020-03-19 stsp goto done;
1545 7848a0e1 2020-03-19 stsp else
1546 7848a0e1 2020-03-19 stsp error = NULL;
1547 7848a0e1 2020-03-19 stsp if (worktree) {
1548 7848a0e1 2020-03-19 stsp repo_path =
1549 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1550 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1551 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1552 7848a0e1 2020-03-19 stsp if (error)
1553 7848a0e1 2020-03-19 stsp goto done;
1554 7848a0e1 2020-03-19 stsp } else {
1555 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1556 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1557 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1558 7848a0e1 2020-03-19 stsp goto done;
1559 7848a0e1 2020-03-19 stsp }
1560 7848a0e1 2020-03-19 stsp }
1561 7848a0e1 2020-03-19 stsp }
1562 7848a0e1 2020-03-19 stsp
1563 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1564 7848a0e1 2020-03-19 stsp if (error)
1565 7848a0e1 2020-03-19 stsp goto done;
1566 7848a0e1 2020-03-19 stsp
1567 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1568 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1569 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1570 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1571 7848a0e1 2020-03-19 stsp break;
1572 7848a0e1 2020-03-19 stsp }
1573 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1574 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1575 7848a0e1 2020-03-19 stsp goto done;
1576 7848a0e1 2020-03-19 stsp }
1577 7848a0e1 2020-03-19 stsp
1578 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1579 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1580 7848a0e1 2020-03-19 stsp if (error)
1581 7848a0e1 2020-03-19 stsp goto done;
1582 7848a0e1 2020-03-19 stsp
1583 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1584 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1585 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1586 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1587 7848a0e1 2020-03-19 stsp err(1, "pledge");
1588 7848a0e1 2020-03-19 stsp #endif
1589 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1590 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1591 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1592 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1593 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1594 7848a0e1 2020-03-19 stsp err(1, "pledge");
1595 7848a0e1 2020-03-19 stsp #endif
1596 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1597 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1598 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1599 7848a0e1 2020-03-19 stsp goto done;
1600 7848a0e1 2020-03-19 stsp } else {
1601 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1602 7848a0e1 2020-03-19 stsp goto done;
1603 7848a0e1 2020-03-19 stsp }
1604 7848a0e1 2020-03-19 stsp
1605 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1606 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1607 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1608 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1609 7848a0e1 2020-03-19 stsp goto done;
1610 7848a0e1 2020-03-19 stsp }
1611 7848a0e1 2020-03-19 stsp }
1612 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1613 7848a0e1 2020-03-19 stsp if (error)
1614 7848a0e1 2020-03-19 stsp goto done;
1615 7848a0e1 2020-03-19 stsp
1616 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1617 9c52365f 2020-03-21 stsp server_path, verbosity);
1618 7848a0e1 2020-03-19 stsp if (error)
1619 7848a0e1 2020-03-19 stsp goto done;
1620 7848a0e1 2020-03-19 stsp
1621 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1622 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1623 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1624 7848a0e1 2020-03-19 stsp
1625 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1626 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1627 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1628 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1629 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1630 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1631 2690194b 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo, fetch_progress, &fpa);
1632 7848a0e1 2020-03-19 stsp if (error)
1633 7848a0e1 2020-03-19 stsp goto done;
1634 7848a0e1 2020-03-19 stsp
1635 41b0de12 2020-03-21 stsp if (list_refs_only) {
1636 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1637 41b0de12 2020-03-21 stsp goto done;
1638 41b0de12 2020-03-21 stsp }
1639 41b0de12 2020-03-21 stsp
1640 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1641 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1642 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1643 f21ec2f0 2020-03-21 stsp if (delete_refs)
1644 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1645 7848a0e1 2020-03-19 stsp goto done;
1646 7848a0e1 2020-03-19 stsp }
1647 7848a0e1 2020-03-19 stsp
1648 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1649 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1650 984065c8 2020-03-19 stsp if (error)
1651 984065c8 2020-03-19 stsp goto done;
1652 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1653 984065c8 2020-03-19 stsp free(id_str);
1654 984065c8 2020-03-19 stsp id_str = NULL;
1655 984065c8 2020-03-19 stsp }
1656 7848a0e1 2020-03-19 stsp
1657 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1658 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1659 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1660 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1661 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1662 7848a0e1 2020-03-19 stsp char *remote_refname;
1663 7848a0e1 2020-03-19 stsp
1664 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1665 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1666 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1667 7848a0e1 2020-03-19 stsp if (error) {
1668 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1669 7848a0e1 2020-03-19 stsp goto done;
1670 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1671 6338a6a1 2020-03-21 stsp repo);
1672 7848a0e1 2020-03-19 stsp if (error)
1673 7848a0e1 2020-03-19 stsp goto done;
1674 7848a0e1 2020-03-19 stsp } else {
1675 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1676 db6d8ad8 2020-03-21 stsp verbosity, repo);
1677 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1678 7848a0e1 2020-03-19 stsp if (error)
1679 7848a0e1 2020-03-19 stsp goto done;
1680 7848a0e1 2020-03-19 stsp }
1681 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1682 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1683 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1684 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1685 7848a0e1 2020-03-19 stsp goto done;
1686 7848a0e1 2020-03-19 stsp }
1687 7848a0e1 2020-03-19 stsp
1688 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1689 7848a0e1 2020-03-19 stsp if (error) {
1690 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1691 7848a0e1 2020-03-19 stsp goto done;
1692 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1693 688f11b3 2020-03-21 stsp verbosity, repo);
1694 7848a0e1 2020-03-19 stsp if (error)
1695 7848a0e1 2020-03-19 stsp goto done;
1696 7848a0e1 2020-03-19 stsp } else {
1697 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1698 db6d8ad8 2020-03-21 stsp verbosity, repo);
1699 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1700 7848a0e1 2020-03-19 stsp if (error)
1701 7848a0e1 2020-03-19 stsp goto done;
1702 7848a0e1 2020-03-19 stsp }
1703 2ec30c80 2020-03-20 stsp
1704 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1705 2ec30c80 2020-03-20 stsp error = got_ref_open(&ref, repo, refname, 0);
1706 2ec30c80 2020-03-20 stsp if (error) {
1707 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1708 2ec30c80 2020-03-20 stsp goto done;
1709 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1710 6338a6a1 2020-03-21 stsp repo);
1711 2ec30c80 2020-03-20 stsp if (error)
1712 2ec30c80 2020-03-20 stsp goto done;
1713 2ec30c80 2020-03-20 stsp } else
1714 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1715 7848a0e1 2020-03-19 stsp }
1716 7848a0e1 2020-03-19 stsp }
1717 f21ec2f0 2020-03-21 stsp if (delete_refs)
1718 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1719 7848a0e1 2020-03-19 stsp done:
1720 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1721 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1722 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1723 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1724 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1725 9c52365f 2020-03-21 stsp }
1726 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1727 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1728 7848a0e1 2020-03-19 stsp if (repo)
1729 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1730 7848a0e1 2020-03-19 stsp if (worktree)
1731 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1732 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1733 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1734 7848a0e1 2020-03-19 stsp free(pe->data);
1735 7848a0e1 2020-03-19 stsp }
1736 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1737 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1738 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1739 7848a0e1 2020-03-19 stsp free(pe->data);
1740 7848a0e1 2020-03-19 stsp }
1741 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1742 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1743 7848a0e1 2020-03-19 stsp free(id_str);
1744 7848a0e1 2020-03-19 stsp free(cwd);
1745 7848a0e1 2020-03-19 stsp free(repo_path);
1746 7848a0e1 2020-03-19 stsp free(pack_hash);
1747 7848a0e1 2020-03-19 stsp free(proto);
1748 7848a0e1 2020-03-19 stsp free(host);
1749 7848a0e1 2020-03-19 stsp free(port);
1750 7848a0e1 2020-03-19 stsp free(server_path);
1751 7848a0e1 2020-03-19 stsp free(repo_name);
1752 7848a0e1 2020-03-19 stsp return error;
1753 7848a0e1 2020-03-19 stsp }
1754 7848a0e1 2020-03-19 stsp
1755 7848a0e1 2020-03-19 stsp
1756 7848a0e1 2020-03-19 stsp __dead static void
1757 2ab43947 2020-03-18 stsp usage_checkout(void)
1758 2ab43947 2020-03-18 stsp {
1759 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1760 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1761 2ab43947 2020-03-18 stsp exit(1);
1762 2ab43947 2020-03-18 stsp }
1763 2ab43947 2020-03-18 stsp
1764 2ab43947 2020-03-18 stsp static void
1765 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1766 2ab43947 2020-03-18 stsp {
1767 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1768 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1769 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1770 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1771 2ab43947 2020-03-18 stsp getprogname());
1772 2ab43947 2020-03-18 stsp }
1773 2ab43947 2020-03-18 stsp
1774 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1775 2ab43947 2020-03-18 stsp const char *worktree_path;
1776 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1777 2ab43947 2020-03-18 stsp };
1778 2ab43947 2020-03-18 stsp
1779 2ab43947 2020-03-18 stsp static const struct got_error *
1780 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1781 2ab43947 2020-03-18 stsp {
1782 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1783 2ab43947 2020-03-18 stsp
1784 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1785 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1786 2ab43947 2020-03-18 stsp return NULL;
1787 2ab43947 2020-03-18 stsp
1788 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1789 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1790 2ab43947 2020-03-18 stsp return NULL;
1791 2ab43947 2020-03-18 stsp }
1792 2ab43947 2020-03-18 stsp
1793 2ab43947 2020-03-18 stsp while (path[0] == '/')
1794 2ab43947 2020-03-18 stsp path++;
1795 2ab43947 2020-03-18 stsp
1796 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1797 2ab43947 2020-03-18 stsp return NULL;
1798 2ab43947 2020-03-18 stsp }
1799 2ab43947 2020-03-18 stsp
1800 2ab43947 2020-03-18 stsp static const struct got_error *
1801 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1802 2ab43947 2020-03-18 stsp {
1803 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1804 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1805 2ab43947 2020-03-18 stsp return NULL;
1806 2ab43947 2020-03-18 stsp }
1807 2ab43947 2020-03-18 stsp
1808 2ab43947 2020-03-18 stsp static const struct got_error *
1809 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1810 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1811 2ab43947 2020-03-18 stsp struct got_repository *repo)
1812 2ab43947 2020-03-18 stsp {
1813 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1814 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1815 2ab43947 2020-03-18 stsp
1816 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1817 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1818 2ab43947 2020-03-18 stsp if (err)
1819 2ab43947 2020-03-18 stsp return err;
1820 2ab43947 2020-03-18 stsp
1821 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1822 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1823 2ab43947 2020-03-18 stsp
1824 2ab43947 2020-03-18 stsp /*
1825 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1826 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1827 2ab43947 2020-03-18 stsp *
1828 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1829 2ab43947 2020-03-18 stsp *
1830 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1831 2ab43947 2020-03-18 stsp * \ /
1832 2ab43947 2020-03-18 stsp * C E
1833 2ab43947 2020-03-18 stsp * \ /
1834 2ab43947 2020-03-18 stsp * B (yca)
1835 2ab43947 2020-03-18 stsp * |
1836 2ab43947 2020-03-18 stsp * A
1837 2ab43947 2020-03-18 stsp *
1838 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1839 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1840 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1841 2ab43947 2020-03-18 stsp */
1842 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1843 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1844 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1845 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1846 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1847 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1848 2ab43947 2020-03-18 stsp
1849 2ab43947 2020-03-18 stsp free(yca_id);
1850 2ab43947 2020-03-18 stsp return NULL;
1851 2ab43947 2020-03-18 stsp }
1852 2ab43947 2020-03-18 stsp
1853 2ab43947 2020-03-18 stsp static const struct got_error *
1854 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1855 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1856 2ab43947 2020-03-18 stsp struct got_repository *repo)
1857 2ab43947 2020-03-18 stsp {
1858 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1859 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1860 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1861 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1862 2ab43947 2020-03-18 stsp
1863 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1864 2ab43947 2020-03-18 stsp if (err)
1865 2ab43947 2020-03-18 stsp goto done;
1866 2ab43947 2020-03-18 stsp
1867 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1868 2ab43947 2020-03-18 stsp is_same_branch = 1;
1869 2ab43947 2020-03-18 stsp goto done;
1870 2ab43947 2020-03-18 stsp }
1871 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1872 2ab43947 2020-03-18 stsp is_same_branch = 1;
1873 2ab43947 2020-03-18 stsp goto done;
1874 2ab43947 2020-03-18 stsp }
1875 2ab43947 2020-03-18 stsp
1876 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1877 2ab43947 2020-03-18 stsp if (err)
1878 2ab43947 2020-03-18 stsp goto done;
1879 2ab43947 2020-03-18 stsp
1880 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1881 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1882 2ab43947 2020-03-18 stsp if (err)
1883 2ab43947 2020-03-18 stsp goto done;
1884 2ab43947 2020-03-18 stsp
1885 2ab43947 2020-03-18 stsp for (;;) {
1886 2ab43947 2020-03-18 stsp struct got_object_id *id;
1887 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1888 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1889 2ab43947 2020-03-18 stsp if (err) {
1890 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1891 2ab43947 2020-03-18 stsp err = NULL;
1892 2ab43947 2020-03-18 stsp break;
1893 2ab43947 2020-03-18 stsp }
1894 2ab43947 2020-03-18 stsp
1895 2ab43947 2020-03-18 stsp if (id) {
1896 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1897 2ab43947 2020-03-18 stsp break;
1898 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1899 2ab43947 2020-03-18 stsp is_same_branch = 1;
1900 2ab43947 2020-03-18 stsp break;
1901 2ab43947 2020-03-18 stsp }
1902 2ab43947 2020-03-18 stsp }
1903 2ab43947 2020-03-18 stsp }
1904 2ab43947 2020-03-18 stsp done:
1905 2ab43947 2020-03-18 stsp if (graph)
1906 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1907 2ab43947 2020-03-18 stsp free(head_commit_id);
1908 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1909 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1910 2ab43947 2020-03-18 stsp return err;
1911 a367ff0f 2019-05-14 stsp }
1912 8069f636 2019-01-12 stsp
1913 4ed7e80c 2018-05-20 stsp static const struct got_error *
1914 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1915 4b6c9460 2020-03-05 stsp {
1916 4b6c9460 2020-03-05 stsp static char msg[512];
1917 4b6c9460 2020-03-05 stsp const char *branch_name;
1918 4b6c9460 2020-03-05 stsp
1919 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1920 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1921 4b6c9460 2020-03-05 stsp else
1922 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1923 4b6c9460 2020-03-05 stsp
1924 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1925 4b6c9460 2020-03-05 stsp branch_name += 11;
1926 4b6c9460 2020-03-05 stsp
1927 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1928 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1929 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1930 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1931 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1932 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1933 4b6c9460 2020-03-05 stsp
1934 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1935 4b6c9460 2020-03-05 stsp }
1936 4b6c9460 2020-03-05 stsp
1937 4b6c9460 2020-03-05 stsp static const struct got_error *
1938 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1939 c09a553d 2018-03-12 stsp {
1940 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1941 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1942 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1943 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1944 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1945 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1946 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1947 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1948 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1949 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1950 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1951 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1952 f2ea84fa 2019-07-27 stsp
1953 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1954 c09a553d 2018-03-12 stsp
1955 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1956 0bb8a95e 2018-03-12 stsp switch (ch) {
1957 08573d5b 2019-05-14 stsp case 'b':
1958 08573d5b 2019-05-14 stsp branch_name = optarg;
1959 08573d5b 2019-05-14 stsp break;
1960 8069f636 2019-01-12 stsp case 'c':
1961 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1962 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1963 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1964 bb51a5b4 2020-01-13 stsp break;
1965 bb51a5b4 2020-01-13 stsp case 'E':
1966 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1967 8069f636 2019-01-12 stsp break;
1968 0bb8a95e 2018-03-12 stsp case 'p':
1969 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1970 0bb8a95e 2018-03-12 stsp break;
1971 0bb8a95e 2018-03-12 stsp default:
1972 2deda0b9 2019-03-07 stsp usage_checkout();
1973 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1974 0bb8a95e 2018-03-12 stsp }
1975 0bb8a95e 2018-03-12 stsp }
1976 0bb8a95e 2018-03-12 stsp
1977 0bb8a95e 2018-03-12 stsp argc -= optind;
1978 0bb8a95e 2018-03-12 stsp argv += optind;
1979 0bb8a95e 2018-03-12 stsp
1980 6715a751 2018-03-16 stsp #ifndef PROFILE
1981 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1982 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1983 c09a553d 2018-03-12 stsp err(1, "pledge");
1984 6715a751 2018-03-16 stsp #endif
1985 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1986 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1987 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1988 76089277 2018-04-01 stsp if (repo_path == NULL)
1989 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1990 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1991 76089277 2018-04-01 stsp if (cwd == NULL) {
1992 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1993 76089277 2018-04-01 stsp goto done;
1994 76089277 2018-04-01 stsp }
1995 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1996 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1997 230a42bd 2019-05-11 jcs if (base == NULL) {
1998 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1999 230a42bd 2019-05-11 jcs path_prefix);
2000 230a42bd 2019-05-11 jcs goto done;
2001 230a42bd 2019-05-11 jcs }
2002 230a42bd 2019-05-11 jcs } else {
2003 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2004 230a42bd 2019-05-11 jcs if (base == NULL) {
2005 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2006 230a42bd 2019-05-11 jcs repo_path);
2007 230a42bd 2019-05-11 jcs goto done;
2008 230a42bd 2019-05-11 jcs }
2009 76089277 2018-04-01 stsp }
2010 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2011 c09a553d 2018-03-12 stsp if (dotgit)
2012 c09a553d 2018-03-12 stsp *dotgit = '\0';
2013 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2014 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2015 c09a553d 2018-03-12 stsp free(cwd);
2016 76089277 2018-04-01 stsp goto done;
2017 c09a553d 2018-03-12 stsp }
2018 c09a553d 2018-03-12 stsp free(cwd);
2019 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2020 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2021 76089277 2018-04-01 stsp if (repo_path == NULL) {
2022 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2023 76089277 2018-04-01 stsp goto done;
2024 76089277 2018-04-01 stsp }
2025 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2026 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2027 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2028 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2029 b4b3a7dd 2019-07-22 stsp argv[1]);
2030 b4b3a7dd 2019-07-22 stsp goto done;
2031 b4b3a7dd 2019-07-22 stsp }
2032 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2033 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2034 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2035 b4b3a7dd 2019-07-22 stsp goto done;
2036 b4b3a7dd 2019-07-22 stsp }
2037 76089277 2018-04-01 stsp }
2038 c09a553d 2018-03-12 stsp } else
2039 c09a553d 2018-03-12 stsp usage_checkout();
2040 c09a553d 2018-03-12 stsp
2041 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2042 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2043 13bfb272 2019-05-10 jcs
2044 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2045 c09a553d 2018-03-12 stsp if (error != NULL)
2046 c02c541e 2019-03-29 stsp goto done;
2047 c02c541e 2019-03-29 stsp
2048 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2049 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2050 c530dc23 2019-07-23 stsp if (error) {
2051 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2052 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2053 c530dc23 2019-07-23 stsp goto done;
2054 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2055 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2056 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2057 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2058 c530dc23 2019-07-23 stsp goto done;
2059 c530dc23 2019-07-23 stsp }
2060 c530dc23 2019-07-23 stsp }
2061 c530dc23 2019-07-23 stsp
2062 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2063 c02c541e 2019-03-29 stsp if (error)
2064 c09a553d 2018-03-12 stsp goto done;
2065 8069f636 2019-01-12 stsp
2066 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2067 c09a553d 2018-03-12 stsp if (error != NULL)
2068 c09a553d 2018-03-12 stsp goto done;
2069 c09a553d 2018-03-12 stsp
2070 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2071 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2072 c09a553d 2018-03-12 stsp goto done;
2073 c09a553d 2018-03-12 stsp
2074 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2075 c09a553d 2018-03-12 stsp if (error != NULL)
2076 c09a553d 2018-03-12 stsp goto done;
2077 c09a553d 2018-03-12 stsp
2078 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2079 e5dc7198 2018-12-29 stsp path_prefix);
2080 e5dc7198 2018-12-29 stsp if (error != NULL)
2081 e5dc7198 2018-12-29 stsp goto done;
2082 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2083 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2084 49520a32 2018-12-29 stsp goto done;
2085 49520a32 2018-12-29 stsp }
2086 49520a32 2018-12-29 stsp
2087 8069f636 2019-01-12 stsp if (commit_id_str) {
2088 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2089 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2090 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2091 30837e32 2019-07-25 stsp if (error)
2092 8069f636 2019-01-12 stsp goto done;
2093 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2094 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2095 8069f636 2019-01-12 stsp if (error != NULL) {
2096 8069f636 2019-01-12 stsp free(commit_id);
2097 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2098 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2099 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2100 4b6c9460 2020-03-05 stsp }
2101 8069f636 2019-01-12 stsp goto done;
2102 8069f636 2019-01-12 stsp }
2103 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2104 4b6c9460 2020-03-05 stsp if (error) {
2105 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2106 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2107 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2108 4b6c9460 2020-03-05 stsp }
2109 45d344f6 2019-05-14 stsp goto done;
2110 4b6c9460 2020-03-05 stsp }
2111 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2112 8069f636 2019-01-12 stsp commit_id);
2113 8069f636 2019-01-12 stsp free(commit_id);
2114 8069f636 2019-01-12 stsp if (error)
2115 8069f636 2019-01-12 stsp goto done;
2116 8069f636 2019-01-12 stsp }
2117 8069f636 2019-01-12 stsp
2118 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2119 f2ea84fa 2019-07-27 stsp if (error)
2120 f2ea84fa 2019-07-27 stsp goto done;
2121 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2122 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2123 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2124 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2125 c09a553d 2018-03-12 stsp if (error != NULL)
2126 c09a553d 2018-03-12 stsp goto done;
2127 c09a553d 2018-03-12 stsp
2128 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2129 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2130 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2131 c09a553d 2018-03-12 stsp done:
2132 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2133 8069f636 2019-01-12 stsp free(commit_id_str);
2134 76089277 2018-04-01 stsp free(repo_path);
2135 507dc3bb 2018-12-29 stsp free(worktree_path);
2136 507dc3bb 2018-12-29 stsp return error;
2137 507dc3bb 2018-12-29 stsp }
2138 507dc3bb 2018-12-29 stsp
2139 507dc3bb 2018-12-29 stsp __dead static void
2140 507dc3bb 2018-12-29 stsp usage_update(void)
2141 507dc3bb 2018-12-29 stsp {
2142 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2143 507dc3bb 2018-12-29 stsp getprogname());
2144 507dc3bb 2018-12-29 stsp exit(1);
2145 507dc3bb 2018-12-29 stsp }
2146 507dc3bb 2018-12-29 stsp
2147 1ee397ad 2019-07-12 stsp static const struct got_error *
2148 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2149 507dc3bb 2018-12-29 stsp {
2150 784955db 2019-01-12 stsp int *did_something = arg;
2151 784955db 2019-01-12 stsp
2152 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2153 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2154 1ee397ad 2019-07-12 stsp return NULL;
2155 507dc3bb 2018-12-29 stsp
2156 1545c615 2019-02-10 stsp *did_something = 1;
2157 a484d721 2019-06-10 stsp
2158 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2159 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2160 1ee397ad 2019-07-12 stsp return NULL;
2161 a484d721 2019-06-10 stsp
2162 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2163 507dc3bb 2018-12-29 stsp path++;
2164 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2165 1ee397ad 2019-07-12 stsp return NULL;
2166 be7061eb 2018-12-30 stsp }
2167 be7061eb 2018-12-30 stsp
2168 be7061eb 2018-12-30 stsp static const struct got_error *
2169 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2170 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2171 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2172 a1fb16d8 2019-05-24 stsp {
2173 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2174 a1fb16d8 2019-05-24 stsp char *base_id_str;
2175 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2176 a1fb16d8 2019-05-24 stsp
2177 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2178 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2179 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2180 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2181 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2182 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2183 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2184 a1fb16d8 2019-05-24 stsp }
2185 a1fb16d8 2019-05-24 stsp
2186 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2187 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2188 a1fb16d8 2019-05-24 stsp if (err) {
2189 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2190 a1fb16d8 2019-05-24 stsp return err;
2191 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2192 a1fb16d8 2019-05-24 stsp }
2193 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2194 a1fb16d8 2019-05-24 stsp return NULL;
2195 a1fb16d8 2019-05-24 stsp
2196 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2197 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2198 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2199 a1fb16d8 2019-05-24 stsp if (err)
2200 a1fb16d8 2019-05-24 stsp return err;
2201 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2202 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2203 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2204 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2205 0ebf8283 2019-07-24 stsp return NULL;
2206 0ebf8283 2019-07-24 stsp }
2207 0ebf8283 2019-07-24 stsp
2208 0ebf8283 2019-07-24 stsp static const struct got_error *
2209 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2210 0ebf8283 2019-07-24 stsp {
2211 0ebf8283 2019-07-24 stsp const struct got_error *err;
2212 0ebf8283 2019-07-24 stsp int in_progress;
2213 0ebf8283 2019-07-24 stsp
2214 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2215 0ebf8283 2019-07-24 stsp if (err)
2216 0ebf8283 2019-07-24 stsp return err;
2217 0ebf8283 2019-07-24 stsp if (in_progress)
2218 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2219 0ebf8283 2019-07-24 stsp
2220 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2221 0ebf8283 2019-07-24 stsp if (err)
2222 0ebf8283 2019-07-24 stsp return err;
2223 0ebf8283 2019-07-24 stsp if (in_progress)
2224 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2225 0ebf8283 2019-07-24 stsp
2226 a1fb16d8 2019-05-24 stsp return NULL;
2227 a5edda0a 2019-07-27 stsp }
2228 a5edda0a 2019-07-27 stsp
2229 a5edda0a 2019-07-27 stsp static const struct got_error *
2230 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2231 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2232 a5edda0a 2019-07-27 stsp {
2233 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2234 a5edda0a 2019-07-27 stsp char *path;
2235 a5edda0a 2019-07-27 stsp int i;
2236 a5edda0a 2019-07-27 stsp
2237 a5edda0a 2019-07-27 stsp if (argc == 0) {
2238 a5edda0a 2019-07-27 stsp path = strdup("");
2239 a5edda0a 2019-07-27 stsp if (path == NULL)
2240 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2241 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2242 a5edda0a 2019-07-27 stsp }
2243 a5edda0a 2019-07-27 stsp
2244 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2245 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2246 a5edda0a 2019-07-27 stsp if (err)
2247 a5edda0a 2019-07-27 stsp break;
2248 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2249 a5edda0a 2019-07-27 stsp if (err) {
2250 a5edda0a 2019-07-27 stsp free(path);
2251 a5edda0a 2019-07-27 stsp break;
2252 a5edda0a 2019-07-27 stsp }
2253 a5edda0a 2019-07-27 stsp }
2254 a5edda0a 2019-07-27 stsp
2255 a5edda0a 2019-07-27 stsp return err;
2256 a1fb16d8 2019-05-24 stsp }
2257 a1fb16d8 2019-05-24 stsp
2258 a1fb16d8 2019-05-24 stsp static const struct got_error *
2259 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2260 507dc3bb 2018-12-29 stsp {
2261 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2262 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2263 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2264 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2265 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2266 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2267 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2268 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2269 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2270 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2271 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2272 507dc3bb 2018-12-29 stsp
2273 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2274 f2ea84fa 2019-07-27 stsp
2275 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2276 507dc3bb 2018-12-29 stsp switch (ch) {
2277 024e9686 2019-05-14 stsp case 'b':
2278 024e9686 2019-05-14 stsp branch_name = optarg;
2279 024e9686 2019-05-14 stsp break;
2280 507dc3bb 2018-12-29 stsp case 'c':
2281 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2282 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2283 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2284 507dc3bb 2018-12-29 stsp break;
2285 507dc3bb 2018-12-29 stsp default:
2286 2deda0b9 2019-03-07 stsp usage_update();
2287 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2288 507dc3bb 2018-12-29 stsp }
2289 507dc3bb 2018-12-29 stsp }
2290 507dc3bb 2018-12-29 stsp
2291 507dc3bb 2018-12-29 stsp argc -= optind;
2292 507dc3bb 2018-12-29 stsp argv += optind;
2293 507dc3bb 2018-12-29 stsp
2294 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2295 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2296 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2297 507dc3bb 2018-12-29 stsp err(1, "pledge");
2298 507dc3bb 2018-12-29 stsp #endif
2299 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2300 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2301 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2302 c4cdcb68 2019-04-03 stsp goto done;
2303 c4cdcb68 2019-04-03 stsp }
2304 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2305 7d5807f4 2019-07-11 stsp if (error)
2306 7d5807f4 2019-07-11 stsp goto done;
2307 7d5807f4 2019-07-11 stsp
2308 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2309 f2ea84fa 2019-07-27 stsp if (error)
2310 f2ea84fa 2019-07-27 stsp goto done;
2311 507dc3bb 2018-12-29 stsp
2312 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2313 c9956ddf 2019-09-08 stsp NULL);
2314 507dc3bb 2018-12-29 stsp if (error != NULL)
2315 507dc3bb 2018-12-29 stsp goto done;
2316 507dc3bb 2018-12-29 stsp
2317 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2318 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2319 087fb88c 2019-08-04 stsp if (error)
2320 087fb88c 2019-08-04 stsp goto done;
2321 087fb88c 2019-08-04 stsp
2322 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2323 0266afb7 2019-01-04 stsp if (error)
2324 0266afb7 2019-01-04 stsp goto done;
2325 0266afb7 2019-01-04 stsp
2326 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2327 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2328 024e9686 2019-05-14 stsp if (error != NULL)
2329 024e9686 2019-05-14 stsp goto done;
2330 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2331 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2332 9c4b8182 2019-01-02 stsp if (error != NULL)
2333 9c4b8182 2019-01-02 stsp goto done;
2334 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2335 507dc3bb 2018-12-29 stsp if (error != NULL)
2336 507dc3bb 2018-12-29 stsp goto done;
2337 507dc3bb 2018-12-29 stsp } else {
2338 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2339 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2340 04f57cb3 2019-07-25 stsp free(commit_id_str);
2341 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2342 30837e32 2019-07-25 stsp if (error)
2343 a297e751 2019-07-11 stsp goto done;
2344 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2345 a297e751 2019-07-11 stsp if (error)
2346 507dc3bb 2018-12-29 stsp goto done;
2347 507dc3bb 2018-12-29 stsp }
2348 35c965b2 2018-12-29 stsp
2349 a1fb16d8 2019-05-24 stsp if (branch_name) {
2350 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2351 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2352 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2353 f2ea84fa 2019-07-27 stsp continue;
2354 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2355 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2356 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2357 024e9686 2019-05-14 stsp goto done;
2358 024e9686 2019-05-14 stsp }
2359 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2360 024e9686 2019-05-14 stsp if (error)
2361 024e9686 2019-05-14 stsp goto done;
2362 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2363 3aef623b 2019-10-15 stsp repo);
2364 a367ff0f 2019-05-14 stsp free(head_commit_id);
2365 024e9686 2019-05-14 stsp if (error != NULL)
2366 024e9686 2019-05-14 stsp goto done;
2367 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2368 a367ff0f 2019-05-14 stsp if (error)
2369 a367ff0f 2019-05-14 stsp goto done;
2370 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2371 024e9686 2019-05-14 stsp if (error)
2372 024e9686 2019-05-14 stsp goto done;
2373 024e9686 2019-05-14 stsp } else {
2374 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2375 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2376 a367ff0f 2019-05-14 stsp if (error != NULL) {
2377 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2378 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2379 024e9686 2019-05-14 stsp goto done;
2380 a367ff0f 2019-05-14 stsp }
2381 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2382 a367ff0f 2019-05-14 stsp if (error)
2383 a367ff0f 2019-05-14 stsp goto done;
2384 024e9686 2019-05-14 stsp }
2385 507dc3bb 2018-12-29 stsp
2386 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2387 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2388 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2389 507dc3bb 2018-12-29 stsp commit_id);
2390 507dc3bb 2018-12-29 stsp if (error)
2391 507dc3bb 2018-12-29 stsp goto done;
2392 507dc3bb 2018-12-29 stsp }
2393 507dc3bb 2018-12-29 stsp
2394 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2395 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2396 507dc3bb 2018-12-29 stsp if (error != NULL)
2397 507dc3bb 2018-12-29 stsp goto done;
2398 9c4b8182 2019-01-02 stsp
2399 784955db 2019-01-12 stsp if (did_something)
2400 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2401 784955db 2019-01-12 stsp else
2402 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2403 507dc3bb 2018-12-29 stsp done:
2404 c09a553d 2018-03-12 stsp free(worktree_path);
2405 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2406 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2407 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2408 507dc3bb 2018-12-29 stsp free(commit_id);
2409 9c4b8182 2019-01-02 stsp free(commit_id_str);
2410 c09a553d 2018-03-12 stsp return error;
2411 c09a553d 2018-03-12 stsp }
2412 c09a553d 2018-03-12 stsp
2413 f42b1b34 2018-03-12 stsp static const struct got_error *
2414 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2415 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2416 63035f9f 2019-10-06 stsp struct got_repository *repo)
2417 5c860e29 2018-03-12 stsp {
2418 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2419 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2420 79109fed 2018-03-27 stsp
2421 44392932 2019-08-25 stsp if (blob_id1) {
2422 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2423 44392932 2019-08-25 stsp if (err)
2424 44392932 2019-08-25 stsp goto done;
2425 44392932 2019-08-25 stsp }
2426 79109fed 2018-03-27 stsp
2427 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2428 44392932 2019-08-25 stsp if (err)
2429 44392932 2019-08-25 stsp goto done;
2430 79109fed 2018-03-27 stsp
2431 44392932 2019-08-25 stsp while (path[0] == '/')
2432 44392932 2019-08-25 stsp path++;
2433 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2434 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2435 44392932 2019-08-25 stsp done:
2436 44392932 2019-08-25 stsp if (blob1)
2437 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2438 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2439 44392932 2019-08-25 stsp return err;
2440 44392932 2019-08-25 stsp }
2441 44392932 2019-08-25 stsp
2442 44392932 2019-08-25 stsp static const struct got_error *
2443 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2444 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2445 63035f9f 2019-10-06 stsp struct got_repository *repo)
2446 44392932 2019-08-25 stsp {
2447 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2448 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2449 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2450 44392932 2019-08-25 stsp
2451 44392932 2019-08-25 stsp if (tree_id1) {
2452 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2453 79109fed 2018-03-27 stsp if (err)
2454 44392932 2019-08-25 stsp goto done;
2455 79109fed 2018-03-27 stsp }
2456 79109fed 2018-03-27 stsp
2457 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2458 0f2b3dca 2018-12-22 stsp if (err)
2459 0f2b3dca 2018-12-22 stsp goto done;
2460 0f2b3dca 2018-12-22 stsp
2461 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2462 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2463 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2464 44392932 2019-08-25 stsp while (path[0] == '/')
2465 44392932 2019-08-25 stsp path++;
2466 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2467 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2468 0f2b3dca 2018-12-22 stsp done:
2469 79109fed 2018-03-27 stsp if (tree1)
2470 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2471 366e0a5f 2019-10-10 stsp if (tree2)
2472 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2473 44392932 2019-08-25 stsp return err;
2474 44392932 2019-08-25 stsp }
2475 44392932 2019-08-25 stsp
2476 44392932 2019-08-25 stsp static const struct got_error *
2477 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2478 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2479 44392932 2019-08-25 stsp {
2480 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2481 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2482 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2483 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2484 44392932 2019-08-25 stsp struct got_object_qid *qid;
2485 44392932 2019-08-25 stsp
2486 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2487 44392932 2019-08-25 stsp if (qid != NULL) {
2488 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2489 44392932 2019-08-25 stsp qid->id);
2490 44392932 2019-08-25 stsp if (err)
2491 44392932 2019-08-25 stsp return err;
2492 44392932 2019-08-25 stsp }
2493 44392932 2019-08-25 stsp
2494 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2495 44392932 2019-08-25 stsp int obj_type;
2496 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2497 44392932 2019-08-25 stsp if (err)
2498 44392932 2019-08-25 stsp goto done;
2499 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2500 44392932 2019-08-25 stsp if (err) {
2501 44392932 2019-08-25 stsp free(obj_id2);
2502 44392932 2019-08-25 stsp goto done;
2503 44392932 2019-08-25 stsp }
2504 44392932 2019-08-25 stsp if (pcommit) {
2505 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2506 44392932 2019-08-25 stsp qid->id, path);
2507 44392932 2019-08-25 stsp if (err) {
2508 44392932 2019-08-25 stsp free(obj_id2);
2509 44392932 2019-08-25 stsp goto done;
2510 44392932 2019-08-25 stsp }
2511 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2512 44392932 2019-08-25 stsp if (err) {
2513 44392932 2019-08-25 stsp free(obj_id2);
2514 44392932 2019-08-25 stsp goto done;
2515 44392932 2019-08-25 stsp }
2516 44392932 2019-08-25 stsp }
2517 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2518 44392932 2019-08-25 stsp if (err) {
2519 44392932 2019-08-25 stsp free(obj_id2);
2520 44392932 2019-08-25 stsp goto done;
2521 44392932 2019-08-25 stsp }
2522 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2523 44392932 2019-08-25 stsp switch (obj_type) {
2524 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2525 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2526 63035f9f 2019-10-06 stsp 0, repo);
2527 44392932 2019-08-25 stsp break;
2528 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2529 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2530 63035f9f 2019-10-06 stsp 0, repo);
2531 44392932 2019-08-25 stsp break;
2532 44392932 2019-08-25 stsp default:
2533 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2534 44392932 2019-08-25 stsp break;
2535 44392932 2019-08-25 stsp }
2536 44392932 2019-08-25 stsp free(obj_id1);
2537 44392932 2019-08-25 stsp free(obj_id2);
2538 44392932 2019-08-25 stsp } else {
2539 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2540 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2541 44392932 2019-08-25 stsp if (err)
2542 44392932 2019-08-25 stsp goto done;
2543 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2544 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2545 44392932 2019-08-25 stsp if (err)
2546 44392932 2019-08-25 stsp goto done;
2547 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2548 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2549 44392932 2019-08-25 stsp }
2550 44392932 2019-08-25 stsp done:
2551 0f2b3dca 2018-12-22 stsp free(id_str1);
2552 0f2b3dca 2018-12-22 stsp free(id_str2);
2553 44392932 2019-08-25 stsp if (pcommit)
2554 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2555 79109fed 2018-03-27 stsp return err;
2556 79109fed 2018-03-27 stsp }
2557 79109fed 2018-03-27 stsp
2558 4bb494d5 2018-06-16 stsp static char *
2559 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2560 6c281f94 2018-06-11 stsp {
2561 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2562 09867e48 2019-08-13 stsp char *p, *s;
2563 09867e48 2019-08-13 stsp
2564 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2565 09867e48 2019-08-13 stsp if (tm == NULL)
2566 09867e48 2019-08-13 stsp return NULL;
2567 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2568 09867e48 2019-08-13 stsp if (s == NULL)
2569 09867e48 2019-08-13 stsp return NULL;
2570 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2571 6c281f94 2018-06-11 stsp if (p)
2572 6c281f94 2018-06-11 stsp *p = '\0';
2573 6c281f94 2018-06-11 stsp return s;
2574 6c281f94 2018-06-11 stsp }
2575 dc424a06 2019-08-07 stsp
2576 6841bf13 2019-11-29 kn static const struct got_error *
2577 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2578 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2579 6841bf13 2019-11-29 kn {
2580 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2581 6841bf13 2019-11-29 kn regmatch_t regmatch;
2582 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2583 6841bf13 2019-11-29 kn
2584 6841bf13 2019-11-29 kn *have_match = 0;
2585 6841bf13 2019-11-29 kn
2586 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2587 6841bf13 2019-11-29 kn if (err)
2588 6841bf13 2019-11-29 kn return err;
2589 6841bf13 2019-11-29 kn
2590 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2591 6841bf13 2019-11-29 kn if (err)
2592 6841bf13 2019-11-29 kn goto done;
2593 6841bf13 2019-11-29 kn
2594 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2595 6841bf13 2019-11-29 kn *have_match = 1;
2596 6841bf13 2019-11-29 kn done:
2597 6841bf13 2019-11-29 kn free(id_str);
2598 6841bf13 2019-11-29 kn free(logmsg);
2599 6841bf13 2019-11-29 kn return err;
2600 6841bf13 2019-11-29 kn }
2601 6841bf13 2019-11-29 kn
2602 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2603 6c281f94 2018-06-11 stsp
2604 79109fed 2018-03-27 stsp static const struct got_error *
2605 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2606 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2607 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2608 79109fed 2018-03-27 stsp {
2609 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2610 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2611 6c281f94 2018-06-11 stsp char datebuf[26];
2612 45d799e2 2018-12-23 stsp time_t committer_time;
2613 45d799e2 2018-12-23 stsp const char *author, *committer;
2614 199a4027 2019-02-02 stsp char *refs_str = NULL;
2615 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2616 5c860e29 2018-03-12 stsp
2617 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2618 199a4027 2019-02-02 stsp char *s;
2619 199a4027 2019-02-02 stsp const char *name;
2620 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2621 a436ad14 2019-08-13 stsp int cmp;
2622 a436ad14 2019-08-13 stsp
2623 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2624 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2625 d9498b20 2019-02-05 stsp continue;
2626 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2627 199a4027 2019-02-02 stsp name += 5;
2628 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2629 7143d404 2019-03-12 stsp continue;
2630 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2631 e34f9ed6 2019-02-02 stsp name += 6;
2632 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2633 141c2bff 2019-02-04 stsp name += 8;
2634 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2635 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2636 5d844a1e 2019-08-13 stsp if (err) {
2637 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2638 5d844a1e 2019-08-13 stsp return err;
2639 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2640 5d844a1e 2019-08-13 stsp err = NULL;
2641 5d844a1e 2019-08-13 stsp tag = NULL;
2642 5d844a1e 2019-08-13 stsp }
2643 a436ad14 2019-08-13 stsp }
2644 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2645 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2646 a436ad14 2019-08-13 stsp if (tag)
2647 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2648 a436ad14 2019-08-13 stsp if (cmp != 0)
2649 a436ad14 2019-08-13 stsp continue;
2650 199a4027 2019-02-02 stsp s = refs_str;
2651 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2652 199a4027 2019-02-02 stsp name) == -1) {
2653 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2654 199a4027 2019-02-02 stsp free(s);
2655 34ca4898 2019-08-13 stsp return err;
2656 199a4027 2019-02-02 stsp }
2657 199a4027 2019-02-02 stsp free(s);
2658 199a4027 2019-02-02 stsp }
2659 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2660 8bf5b3c9 2018-03-17 stsp if (err)
2661 8bf5b3c9 2018-03-17 stsp return err;
2662 788c352e 2018-06-16 stsp
2663 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2664 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2665 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2666 832c249c 2018-06-10 stsp free(id_str);
2667 d3d493d7 2019-02-21 stsp id_str = NULL;
2668 d3d493d7 2019-02-21 stsp free(refs_str);
2669 d3d493d7 2019-02-21 stsp refs_str = NULL;
2670 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2671 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2672 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2673 09867e48 2019-08-13 stsp if (datestr)
2674 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2675 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2676 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2677 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2678 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2679 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2680 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2681 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2682 3fe1abad 2018-06-10 stsp int n = 1;
2683 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2684 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2685 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2686 a0603db2 2018-06-10 stsp if (err)
2687 a0603db2 2018-06-10 stsp return err;
2688 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2689 a0603db2 2018-06-10 stsp free(id_str);
2690 a0603db2 2018-06-10 stsp }
2691 a0603db2 2018-06-10 stsp }
2692 832c249c 2018-06-10 stsp
2693 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2694 5943eee2 2019-08-13 stsp if (err)
2695 5943eee2 2019-08-13 stsp return err;
2696 8bf5b3c9 2018-03-17 stsp
2697 621015ac 2018-07-23 stsp logmsg = logmsg0;
2698 832c249c 2018-06-10 stsp do {
2699 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2700 832c249c 2018-06-10 stsp if (line)
2701 832c249c 2018-06-10 stsp printf(" %s\n", line);
2702 832c249c 2018-06-10 stsp } while (line);
2703 621015ac 2018-07-23 stsp free(logmsg0);
2704 832c249c 2018-06-10 stsp
2705 971751ac 2018-03-27 stsp if (show_patch) {
2706 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2707 971751ac 2018-03-27 stsp if (err == 0)
2708 971751ac 2018-03-27 stsp printf("\n");
2709 971751ac 2018-03-27 stsp }
2710 07862c20 2018-09-15 stsp
2711 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2712 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2713 07862c20 2018-09-15 stsp return err;
2714 f42b1b34 2018-03-12 stsp }
2715 5c860e29 2018-03-12 stsp
2716 f42b1b34 2018-03-12 stsp static const struct got_error *
2717 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2718 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2719 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2720 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2721 f42b1b34 2018-03-12 stsp {
2722 f42b1b34 2018-03-12 stsp const struct got_error *err;
2723 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2724 6841bf13 2019-11-29 kn regex_t regex;
2725 6841bf13 2019-11-29 kn int have_match;
2726 372ccdbb 2018-06-10 stsp
2727 6841bf13 2019-11-29 kn if (search_pattern &&
2728 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2729 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2730 6841bf13 2019-11-29 kn
2731 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2732 f42b1b34 2018-03-12 stsp if (err)
2733 f42b1b34 2018-03-12 stsp return err;
2734 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2735 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2736 372ccdbb 2018-06-10 stsp if (err)
2737 fcc85cad 2018-07-22 stsp goto done;
2738 656b1f76 2019-05-11 jcs for (;;) {
2739 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2740 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2741 8bf5b3c9 2018-03-17 stsp
2742 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2743 84453469 2018-11-11 stsp break;
2744 84453469 2018-11-11 stsp
2745 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2746 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2747 372ccdbb 2018-06-10 stsp if (err) {
2748 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2749 9ba79e04 2018-06-11 stsp err = NULL;
2750 ee780d5c 2020-01-04 stsp break;
2751 7e665116 2018-04-02 stsp }
2752 b43fbaa0 2018-06-11 stsp if (id == NULL)
2753 7e665116 2018-04-02 stsp break;
2754 b43fbaa0 2018-06-11 stsp
2755 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2756 b43fbaa0 2018-06-11 stsp if (err)
2757 fcc85cad 2018-07-22 stsp break;
2758 6841bf13 2019-11-29 kn
2759 6841bf13 2019-11-29 kn if (search_pattern) {
2760 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2761 6841bf13 2019-11-29 kn if (err) {
2762 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2763 6841bf13 2019-11-29 kn break;
2764 6841bf13 2019-11-29 kn }
2765 6841bf13 2019-11-29 kn if (have_match == 0) {
2766 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2767 6841bf13 2019-11-29 kn continue;
2768 6841bf13 2019-11-29 kn }
2769 6841bf13 2019-11-29 kn }
2770 6841bf13 2019-11-29 kn
2771 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2772 44392932 2019-08-25 stsp diff_context, refs);
2773 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2774 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2775 7e665116 2018-04-02 stsp break;
2776 31cedeaf 2018-09-15 stsp }
2777 fcc85cad 2018-07-22 stsp done:
2778 6841bf13 2019-11-29 kn if (search_pattern)
2779 6841bf13 2019-11-29 kn regfree(&regex);
2780 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2781 f42b1b34 2018-03-12 stsp return err;
2782 f42b1b34 2018-03-12 stsp }
2783 5c860e29 2018-03-12 stsp
2784 4ed7e80c 2018-05-20 stsp __dead static void
2785 6f3d1eb0 2018-03-12 stsp usage_log(void)
2786 6f3d1eb0 2018-03-12 stsp {
2787 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2788 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2789 6f3d1eb0 2018-03-12 stsp exit(1);
2790 b1ebc001 2019-08-13 stsp }
2791 b1ebc001 2019-08-13 stsp
2792 b1ebc001 2019-08-13 stsp static int
2793 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2794 b1ebc001 2019-08-13 stsp {
2795 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2796 b1ebc001 2019-08-13 stsp long long n;
2797 b1ebc001 2019-08-13 stsp const char *errstr;
2798 b1ebc001 2019-08-13 stsp
2799 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2800 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2801 b1ebc001 2019-08-13 stsp return 0;
2802 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2803 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2804 b1ebc001 2019-08-13 stsp return 0;
2805 b1ebc001 2019-08-13 stsp return n;
2806 6f3d1eb0 2018-03-12 stsp }
2807 6f3d1eb0 2018-03-12 stsp
2808 4ed7e80c 2018-05-20 stsp static const struct got_error *
2809 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2810 f42b1b34 2018-03-12 stsp {
2811 f42b1b34 2018-03-12 stsp const struct got_error *error;
2812 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2813 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2814 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2815 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2816 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2817 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2818 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2819 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2820 64a96a6d 2018-04-01 stsp const char *errstr;
2821 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2822 1b3893a2 2019-03-18 stsp
2823 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2824 5c860e29 2018-03-12 stsp
2825 6715a751 2018-03-16 stsp #ifndef PROFILE
2826 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2827 6098196c 2019-01-04 stsp NULL)
2828 ad242220 2018-09-08 stsp == -1)
2829 f42b1b34 2018-03-12 stsp err(1, "pledge");
2830 6715a751 2018-03-16 stsp #endif
2831 79109fed 2018-03-27 stsp
2832 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2833 b1ebc001 2019-08-13 stsp
2834 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2835 79109fed 2018-03-27 stsp switch (ch) {
2836 79109fed 2018-03-27 stsp case 'p':
2837 79109fed 2018-03-27 stsp show_patch = 1;
2838 d142fc45 2018-04-01 stsp break;
2839 d142fc45 2018-04-01 stsp case 'c':
2840 d142fc45 2018-04-01 stsp start_commit = optarg;
2841 64a96a6d 2018-04-01 stsp break;
2842 c0cc5c62 2018-10-18 stsp case 'C':
2843 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2844 4a8520aa 2018-10-18 stsp &errstr);
2845 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2846 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2847 c0cc5c62 2018-10-18 stsp break;
2848 64a96a6d 2018-04-01 stsp case 'l':
2849 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2850 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2851 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2852 cc54c501 2019-07-15 stsp break;
2853 48c8c60d 2020-01-27 stsp case 'b':
2854 48c8c60d 2020-01-27 stsp log_branches = 1;
2855 a0603db2 2018-06-10 stsp break;
2856 04ca23f4 2018-07-16 stsp case 'r':
2857 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2858 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2859 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2860 9ba1d308 2019-10-21 stsp optarg);
2861 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2862 04ca23f4 2018-07-16 stsp break;
2863 6841bf13 2019-11-29 kn case 's':
2864 6841bf13 2019-11-29 kn search_pattern = optarg;
2865 6841bf13 2019-11-29 kn break;
2866 79109fed 2018-03-27 stsp default:
2867 2deda0b9 2019-03-07 stsp usage_log();
2868 79109fed 2018-03-27 stsp /* NOTREACHED */
2869 79109fed 2018-03-27 stsp }
2870 79109fed 2018-03-27 stsp }
2871 79109fed 2018-03-27 stsp
2872 79109fed 2018-03-27 stsp argc -= optind;
2873 79109fed 2018-03-27 stsp argv += optind;
2874 f42b1b34 2018-03-12 stsp
2875 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2876 dc1edbfa 2019-11-29 kn diff_context = 3;
2877 dc1edbfa 2019-11-29 kn else if (!show_patch)
2878 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2879 dc1edbfa 2019-11-29 kn
2880 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2881 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2882 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2883 04ca23f4 2018-07-16 stsp goto done;
2884 04ca23f4 2018-07-16 stsp }
2885 cffc0aa4 2019-02-05 stsp
2886 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2887 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2888 cffc0aa4 2019-02-05 stsp goto done;
2889 cffc0aa4 2019-02-05 stsp error = NULL;
2890 cffc0aa4 2019-02-05 stsp
2891 e7301579 2019-03-18 stsp if (argc == 0) {
2892 cbd1af7a 2019-03-18 stsp path = strdup("");
2893 e7301579 2019-03-18 stsp if (path == NULL) {
2894 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2895 cbd1af7a 2019-03-18 stsp goto done;
2896 e7301579 2019-03-18 stsp }
2897 e7301579 2019-03-18 stsp } else if (argc == 1) {
2898 e7301579 2019-03-18 stsp if (worktree) {
2899 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2900 e7301579 2019-03-18 stsp argv[0]);
2901 e7301579 2019-03-18 stsp if (error)
2902 e7301579 2019-03-18 stsp goto done;
2903 e7301579 2019-03-18 stsp } else {
2904 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2905 e7301579 2019-03-18 stsp if (path == NULL) {
2906 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2907 e7301579 2019-03-18 stsp goto done;
2908 e7301579 2019-03-18 stsp }
2909 e7301579 2019-03-18 stsp }
2910 cbd1af7a 2019-03-18 stsp } else
2911 cbd1af7a 2019-03-18 stsp usage_log();
2912 cbd1af7a 2019-03-18 stsp
2913 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2914 5486daa2 2019-05-11 stsp repo_path = worktree ?
2915 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2916 5486daa2 2019-05-11 stsp }
2917 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2918 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2919 cffc0aa4 2019-02-05 stsp goto done;
2920 04ca23f4 2018-07-16 stsp }
2921 6098196c 2019-01-04 stsp
2922 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2923 d7d4f210 2018-03-12 stsp if (error != NULL)
2924 04ca23f4 2018-07-16 stsp goto done;
2925 f42b1b34 2018-03-12 stsp
2926 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2927 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2928 c02c541e 2019-03-29 stsp if (error)
2929 c02c541e 2019-03-29 stsp goto done;
2930 c02c541e 2019-03-29 stsp
2931 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2932 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2933 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2934 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2935 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2936 3235492e 2018-04-01 stsp if (error != NULL)
2937 3235492e 2018-04-01 stsp return error;
2938 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2939 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2940 3235492e 2018-04-01 stsp if (error != NULL)
2941 3235492e 2018-04-01 stsp return error;
2942 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2943 3235492e 2018-04-01 stsp } else {
2944 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2945 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2946 3235492e 2018-04-01 stsp if (error == NULL) {
2947 1caad61b 2019-02-01 stsp int obj_type;
2948 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2949 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2950 d1f2edc9 2018-06-13 stsp if (error != NULL)
2951 1caad61b 2019-02-01 stsp goto done;
2952 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2953 1caad61b 2019-02-01 stsp if (error != NULL)
2954 1caad61b 2019-02-01 stsp goto done;
2955 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2956 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2957 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2958 1caad61b 2019-02-01 stsp if (error != NULL)
2959 1caad61b 2019-02-01 stsp goto done;
2960 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2961 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2962 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2963 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2964 1caad61b 2019-02-01 stsp goto done;
2965 1caad61b 2019-02-01 stsp }
2966 1caad61b 2019-02-01 stsp free(id);
2967 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2968 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2969 1caad61b 2019-02-01 stsp if (id == NULL)
2970 638f9024 2019-05-13 stsp error = got_error_from_errno(
2971 230a42bd 2019-05-11 jcs "got_object_id_dup");
2972 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2973 1caad61b 2019-02-01 stsp if (error)
2974 1caad61b 2019-02-01 stsp goto done;
2975 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2976 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2977 1caad61b 2019-02-01 stsp goto done;
2978 1caad61b 2019-02-01 stsp }
2979 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2980 d1f2edc9 2018-06-13 stsp if (error != NULL)
2981 1caad61b 2019-02-01 stsp goto done;
2982 d1f2edc9 2018-06-13 stsp }
2983 15a94983 2018-12-23 stsp if (commit == NULL) {
2984 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2985 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2986 d1f2edc9 2018-06-13 stsp if (error != NULL)
2987 d1f2edc9 2018-06-13 stsp return error;
2988 3235492e 2018-04-01 stsp }
2989 3235492e 2018-04-01 stsp }
2990 d7d4f210 2018-03-12 stsp if (error != NULL)
2991 04ca23f4 2018-07-16 stsp goto done;
2992 04ca23f4 2018-07-16 stsp
2993 deeabeae 2019-08-27 stsp if (worktree) {
2994 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2995 deeabeae 2019-08-27 stsp char *p;
2996 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2997 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2998 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2999 deeabeae 2019-08-27 stsp goto done;
3000 deeabeae 2019-08-27 stsp }
3001 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3002 deeabeae 2019-08-27 stsp free(p);
3003 deeabeae 2019-08-27 stsp } else
3004 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3005 04ca23f4 2018-07-16 stsp if (error != NULL)
3006 04ca23f4 2018-07-16 stsp goto done;
3007 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3008 04ca23f4 2018-07-16 stsp free(path);
3009 04ca23f4 2018-07-16 stsp path = in_repo_path;
3010 04ca23f4 2018-07-16 stsp }
3011 199a4027 2019-02-02 stsp
3012 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3013 199a4027 2019-02-02 stsp if (error)
3014 199a4027 2019-02-02 stsp goto done;
3015 04ca23f4 2018-07-16 stsp
3016 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3017 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3018 04ca23f4 2018-07-16 stsp done:
3019 04ca23f4 2018-07-16 stsp free(path);
3020 04ca23f4 2018-07-16 stsp free(repo_path);
3021 04ca23f4 2018-07-16 stsp free(cwd);
3022 f42b1b34 2018-03-12 stsp free(id);
3023 cffc0aa4 2019-02-05 stsp if (worktree)
3024 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3025 ad242220 2018-09-08 stsp if (repo) {
3026 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3027 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3028 ad242220 2018-09-08 stsp if (error == NULL)
3029 ad242220 2018-09-08 stsp error = repo_error;
3030 ad242220 2018-09-08 stsp }
3031 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3032 8bf5b3c9 2018-03-17 stsp return error;
3033 5c860e29 2018-03-12 stsp }
3034 5c860e29 2018-03-12 stsp
3035 4ed7e80c 2018-05-20 stsp __dead static void
3036 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3037 3f8b7d6a 2018-04-01 stsp {
3038 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3039 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3040 3f8b7d6a 2018-04-01 stsp exit(1);
3041 b00d56cd 2018-04-01 stsp }
3042 b00d56cd 2018-04-01 stsp
3043 b72f483a 2019-02-05 stsp struct print_diff_arg {
3044 b72f483a 2019-02-05 stsp struct got_repository *repo;
3045 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3046 b72f483a 2019-02-05 stsp int diff_context;
3047 3fc0c068 2019-02-10 stsp const char *id_str;
3048 3fc0c068 2019-02-10 stsp int header_shown;
3049 98eaaa12 2019-08-03 stsp int diff_staged;
3050 63035f9f 2019-10-06 stsp int ignore_whitespace;
3051 b72f483a 2019-02-05 stsp };
3052 b72f483a 2019-02-05 stsp
3053 4ed7e80c 2018-05-20 stsp static const struct got_error *
3054 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3055 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3056 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3057 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3058 b72f483a 2019-02-05 stsp {
3059 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3060 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3061 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3062 12463d8b 2019-12-13 stsp int fd = -1;
3063 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3064 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3065 b72f483a 2019-02-05 stsp struct stat sb;
3066 b72f483a 2019-02-05 stsp
3067 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3068 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3069 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3070 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3071 98eaaa12 2019-08-03 stsp return NULL;
3072 98eaaa12 2019-08-03 stsp } else {
3073 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3074 98eaaa12 2019-08-03 stsp return NULL;
3075 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3076 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3077 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3078 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3079 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3080 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3081 98eaaa12 2019-08-03 stsp return NULL;
3082 98eaaa12 2019-08-03 stsp }
3083 3fc0c068 2019-02-10 stsp
3084 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3085 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3086 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3087 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3088 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3089 3fc0c068 2019-02-10 stsp }
3090 b72f483a 2019-02-05 stsp
3091 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3092 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3093 98eaaa12 2019-08-03 stsp switch (staged_status) {
3094 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3095 98eaaa12 2019-08-03 stsp label1 = path;
3096 98eaaa12 2019-08-03 stsp label2 = path;
3097 98eaaa12 2019-08-03 stsp break;
3098 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3099 98eaaa12 2019-08-03 stsp label2 = path;
3100 98eaaa12 2019-08-03 stsp break;
3101 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3102 98eaaa12 2019-08-03 stsp label1 = path;
3103 98eaaa12 2019-08-03 stsp break;
3104 98eaaa12 2019-08-03 stsp default:
3105 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3106 98eaaa12 2019-08-03 stsp }
3107 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3108 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3109 63035f9f 2019-10-06 stsp a->repo, stdout);
3110 98eaaa12 2019-08-03 stsp }
3111 98eaaa12 2019-08-03 stsp
3112 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3113 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3114 4ce46740 2019-08-08 stsp char *id_str;
3115 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3116 408b4ebc 2019-08-03 stsp 8192);
3117 4ce46740 2019-08-08 stsp if (err)
3118 4ce46740 2019-08-08 stsp goto done;
3119 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3120 4ce46740 2019-08-08 stsp if (err)
3121 4ce46740 2019-08-08 stsp goto done;
3122 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3123 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3124 4ce46740 2019-08-08 stsp free(id_str);
3125 4ce46740 2019-08-08 stsp goto done;
3126 4ce46740 2019-08-08 stsp }
3127 4ce46740 2019-08-08 stsp free(id_str);
3128 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3129 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3130 4ce46740 2019-08-08 stsp if (err)
3131 4ce46740 2019-08-08 stsp goto done;
3132 4ce46740 2019-08-08 stsp }
3133 d00136be 2019-03-26 stsp
3134 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3135 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3136 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3137 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3138 2ec1f75b 2019-03-26 stsp goto done;
3139 2ec1f75b 2019-03-26 stsp }
3140 b72f483a 2019-02-05 stsp
3141 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3142 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3143 12463d8b 2019-12-13 stsp if (fd == -1) {
3144 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3145 12463d8b 2019-12-13 stsp goto done;
3146 12463d8b 2019-12-13 stsp }
3147 12463d8b 2019-12-13 stsp } else {
3148 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3149 12463d8b 2019-12-13 stsp if (fd == -1) {
3150 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3151 12463d8b 2019-12-13 stsp goto done;
3152 12463d8b 2019-12-13 stsp }
3153 12463d8b 2019-12-13 stsp }
3154 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3155 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3156 2ec1f75b 2019-03-26 stsp goto done;
3157 2ec1f75b 2019-03-26 stsp }
3158 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3159 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3160 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3161 2ec1f75b 2019-03-26 stsp goto done;
3162 2ec1f75b 2019-03-26 stsp }
3163 12463d8b 2019-12-13 stsp fd = -1;
3164 2ec1f75b 2019-03-26 stsp } else
3165 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3166 b72f483a 2019-02-05 stsp
3167 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3168 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3169 b72f483a 2019-02-05 stsp done:
3170 b72f483a 2019-02-05 stsp if (blob1)
3171 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3172 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3173 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3174 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3175 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3176 b72f483a 2019-02-05 stsp free(abspath);
3177 927df6b7 2019-02-10 stsp return err;
3178 927df6b7 2019-02-10 stsp }
3179 927df6b7 2019-02-10 stsp
3180 927df6b7 2019-02-10 stsp static const struct got_error *
3181 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3182 b00d56cd 2018-04-01 stsp {
3183 b00d56cd 2018-04-01 stsp const struct got_error *error;
3184 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3185 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3186 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3187 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3188 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3189 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3190 15a94983 2018-12-23 stsp int type1, type2;
3191 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3192 c0cc5c62 2018-10-18 stsp const char *errstr;
3193 927df6b7 2019-02-10 stsp char *path = NULL;
3194 b00d56cd 2018-04-01 stsp
3195 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3196 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3197 25eccc22 2019-01-04 stsp NULL) == -1)
3198 b00d56cd 2018-04-01 stsp err(1, "pledge");
3199 b00d56cd 2018-04-01 stsp #endif
3200 b00d56cd 2018-04-01 stsp
3201 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3202 b00d56cd 2018-04-01 stsp switch (ch) {
3203 c0cc5c62 2018-10-18 stsp case 'C':
3204 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3205 dfcab68b 2019-11-29 kn &errstr);
3206 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3207 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3208 c0cc5c62 2018-10-18 stsp break;
3209 b72f483a 2019-02-05 stsp case 'r':
3210 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3211 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3212 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3213 9ba1d308 2019-10-21 stsp optarg);
3214 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3215 b72f483a 2019-02-05 stsp break;
3216 98eaaa12 2019-08-03 stsp case 's':
3217 98eaaa12 2019-08-03 stsp diff_staged = 1;
3218 63035f9f 2019-10-06 stsp break;
3219 63035f9f 2019-10-06 stsp case 'w':
3220 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3221 98eaaa12 2019-08-03 stsp break;
3222 b00d56cd 2018-04-01 stsp default:
3223 2deda0b9 2019-03-07 stsp usage_diff();
3224 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3225 b00d56cd 2018-04-01 stsp }
3226 b00d56cd 2018-04-01 stsp }
3227 b00d56cd 2018-04-01 stsp
3228 b00d56cd 2018-04-01 stsp argc -= optind;
3229 b00d56cd 2018-04-01 stsp argv += optind;
3230 b00d56cd 2018-04-01 stsp
3231 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3232 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3233 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3234 b72f483a 2019-02-05 stsp goto done;
3235 b72f483a 2019-02-05 stsp }
3236 927df6b7 2019-02-10 stsp if (argc <= 1) {
3237 b72f483a 2019-02-05 stsp if (repo_path)
3238 b72f483a 2019-02-05 stsp errx(1,
3239 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3240 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3241 1f03b8da 2020-03-20 stsp if (error)
3242 1f03b8da 2020-03-20 stsp goto done;
3243 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3244 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3245 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3246 927df6b7 2019-02-10 stsp goto done;
3247 927df6b7 2019-02-10 stsp }
3248 927df6b7 2019-02-10 stsp if (argc == 1) {
3249 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3250 cbd1af7a 2019-03-18 stsp argv[0]);
3251 927df6b7 2019-02-10 stsp if (error)
3252 927df6b7 2019-02-10 stsp goto done;
3253 927df6b7 2019-02-10 stsp } else {
3254 927df6b7 2019-02-10 stsp path = strdup("");
3255 927df6b7 2019-02-10 stsp if (path == NULL) {
3256 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3257 927df6b7 2019-02-10 stsp goto done;
3258 927df6b7 2019-02-10 stsp }
3259 927df6b7 2019-02-10 stsp }
3260 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3261 98eaaa12 2019-08-03 stsp if (diff_staged)
3262 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3263 98eaaa12 2019-08-03 stsp "objects in repository");
3264 15a94983 2018-12-23 stsp id_str1 = argv[0];
3265 15a94983 2018-12-23 stsp id_str2 = argv[1];
3266 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3267 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3268 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3269 30db809c 2019-06-05 stsp goto done;
3270 1f03b8da 2020-03-20 stsp if (worktree) {
3271 1f03b8da 2020-03-20 stsp repo_path = strdup(
3272 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3273 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3274 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3275 1f03b8da 2020-03-20 stsp goto done;
3276 1f03b8da 2020-03-20 stsp }
3277 1f03b8da 2020-03-20 stsp } else {
3278 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3279 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3280 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3281 1f03b8da 2020-03-20 stsp goto done;
3282 1f03b8da 2020-03-20 stsp }
3283 30db809c 2019-06-05 stsp }
3284 30db809c 2019-06-05 stsp }
3285 b00d56cd 2018-04-01 stsp } else
3286 b00d56cd 2018-04-01 stsp usage_diff();
3287 b00d56cd 2018-04-01 stsp
3288 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3289 76089277 2018-04-01 stsp free(repo_path);
3290 b00d56cd 2018-04-01 stsp if (error != NULL)
3291 b00d56cd 2018-04-01 stsp goto done;
3292 b00d56cd 2018-04-01 stsp
3293 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3294 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3295 c02c541e 2019-03-29 stsp if (error)
3296 c02c541e 2019-03-29 stsp goto done;
3297 c02c541e 2019-03-29 stsp
3298 30db809c 2019-06-05 stsp if (argc <= 1) {
3299 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3300 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3301 b72f483a 2019-02-05 stsp char *id_str;
3302 72ea6654 2019-07-27 stsp
3303 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3304 72ea6654 2019-07-27 stsp
3305 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3306 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3307 b72f483a 2019-02-05 stsp if (error)
3308 b72f483a 2019-02-05 stsp goto done;
3309 b72f483a 2019-02-05 stsp arg.repo = repo;
3310 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3311 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3312 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3313 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3314 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3315 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3316 b72f483a 2019-02-05 stsp
3317 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3318 72ea6654 2019-07-27 stsp if (error)
3319 72ea6654 2019-07-27 stsp goto done;
3320 72ea6654 2019-07-27 stsp
3321 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3322 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3323 b72f483a 2019-02-05 stsp free(id_str);
3324 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3325 b72f483a 2019-02-05 stsp goto done;
3326 b72f483a 2019-02-05 stsp }
3327 b72f483a 2019-02-05 stsp
3328 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3329 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3330 0adb7196 2019-08-11 stsp if (error)
3331 0adb7196 2019-08-11 stsp goto done;
3332 b00d56cd 2018-04-01 stsp
3333 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3334 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3335 0adb7196 2019-08-11 stsp if (error)
3336 0adb7196 2019-08-11 stsp goto done;
3337 b00d56cd 2018-04-01 stsp
3338 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3339 15a94983 2018-12-23 stsp if (error)
3340 15a94983 2018-12-23 stsp goto done;
3341 15a94983 2018-12-23 stsp
3342 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3343 15a94983 2018-12-23 stsp if (error)
3344 15a94983 2018-12-23 stsp goto done;
3345 15a94983 2018-12-23 stsp
3346 15a94983 2018-12-23 stsp if (type1 != type2) {
3347 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3348 b00d56cd 2018-04-01 stsp goto done;
3349 b00d56cd 2018-04-01 stsp }
3350 b00d56cd 2018-04-01 stsp
3351 15a94983 2018-12-23 stsp switch (type1) {
3352 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3353 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3354 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3355 b00d56cd 2018-04-01 stsp break;
3356 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3357 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3358 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3359 b00d56cd 2018-04-01 stsp break;
3360 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3361 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3362 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3363 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3364 b00d56cd 2018-04-01 stsp break;
3365 b00d56cd 2018-04-01 stsp default:
3366 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3367 b00d56cd 2018-04-01 stsp }
3368 b00d56cd 2018-04-01 stsp done:
3369 5e70831e 2019-05-28 stsp free(label1);
3370 5e70831e 2019-05-28 stsp free(label2);
3371 15a94983 2018-12-23 stsp free(id1);
3372 15a94983 2018-12-23 stsp free(id2);
3373 927df6b7 2019-02-10 stsp free(path);
3374 b72f483a 2019-02-05 stsp if (worktree)
3375 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3376 ad242220 2018-09-08 stsp if (repo) {
3377 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3378 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3379 ad242220 2018-09-08 stsp if (error == NULL)
3380 ad242220 2018-09-08 stsp error = repo_error;
3381 ad242220 2018-09-08 stsp }
3382 b00d56cd 2018-04-01 stsp return error;
3383 404c43c4 2018-06-21 stsp }
3384 404c43c4 2018-06-21 stsp
3385 404c43c4 2018-06-21 stsp __dead static void
3386 404c43c4 2018-06-21 stsp usage_blame(void)
3387 404c43c4 2018-06-21 stsp {
3388 9270e621 2019-02-05 stsp fprintf(stderr,
3389 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3390 404c43c4 2018-06-21 stsp getprogname());
3391 404c43c4 2018-06-21 stsp exit(1);
3392 b00d56cd 2018-04-01 stsp }
3393 b00d56cd 2018-04-01 stsp
3394 28315671 2019-08-14 stsp struct blame_line {
3395 28315671 2019-08-14 stsp int annotated;
3396 28315671 2019-08-14 stsp char *id_str;
3397 82f6abb8 2019-08-14 stsp char *committer;
3398 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3399 28315671 2019-08-14 stsp };
3400 28315671 2019-08-14 stsp
3401 28315671 2019-08-14 stsp struct blame_cb_args {
3402 28315671 2019-08-14 stsp struct blame_line *lines;
3403 28315671 2019-08-14 stsp int nlines;
3404 7ef28ff8 2019-08-14 stsp int nlines_prec;
3405 28315671 2019-08-14 stsp int lineno_cur;
3406 28315671 2019-08-14 stsp off_t *line_offsets;
3407 28315671 2019-08-14 stsp FILE *f;
3408 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3409 28315671 2019-08-14 stsp };
3410 28315671 2019-08-14 stsp
3411 404c43c4 2018-06-21 stsp static const struct got_error *
3412 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3413 28315671 2019-08-14 stsp {
3414 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3415 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3416 28315671 2019-08-14 stsp struct blame_line *bline;
3417 82f6abb8 2019-08-14 stsp char *line = NULL;
3418 28315671 2019-08-14 stsp size_t linesize = 0;
3419 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3420 28315671 2019-08-14 stsp off_t offset;
3421 bcb49d15 2019-08-14 stsp struct tm tm;
3422 bcb49d15 2019-08-14 stsp time_t committer_time;
3423 28315671 2019-08-14 stsp
3424 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3425 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3426 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3427 28315671 2019-08-14 stsp
3428 28315671 2019-08-14 stsp if (sigint_received)
3429 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3430 28315671 2019-08-14 stsp
3431 2839f8b9 2019-08-18 stsp if (lineno == -1)
3432 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3433 2839f8b9 2019-08-18 stsp
3434 28315671 2019-08-14 stsp /* Annotate this line. */
3435 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3436 28315671 2019-08-14 stsp if (bline->annotated)
3437 28315671 2019-08-14 stsp return NULL;
3438 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3439 28315671 2019-08-14 stsp if (err)
3440 28315671 2019-08-14 stsp return err;
3441 82f6abb8 2019-08-14 stsp
3442 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3443 82f6abb8 2019-08-14 stsp if (err)
3444 82f6abb8 2019-08-14 stsp goto done;
3445 82f6abb8 2019-08-14 stsp
3446 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3447 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3448 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3449 bcb49d15 2019-08-14 stsp goto done;
3450 bcb49d15 2019-08-14 stsp }
3451 bcb49d15 2019-08-14 stsp
3452 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3453 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3454 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3455 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3456 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3457 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3458 82f6abb8 2019-08-14 stsp goto done;
3459 82f6abb8 2019-08-14 stsp }
3460 28315671 2019-08-14 stsp bline->annotated = 1;
3461 28315671 2019-08-14 stsp
3462 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3463 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3464 28315671 2019-08-14 stsp if (!bline->annotated)
3465 82f6abb8 2019-08-14 stsp goto done;
3466 28315671 2019-08-14 stsp
3467 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3468 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3469 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3470 82f6abb8 2019-08-14 stsp goto done;
3471 82f6abb8 2019-08-14 stsp }
3472 28315671 2019-08-14 stsp
3473 28315671 2019-08-14 stsp while (bline->annotated) {
3474 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3475 82f6abb8 2019-08-14 stsp size_t len;
3476 82f6abb8 2019-08-14 stsp
3477 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3478 28315671 2019-08-14 stsp if (ferror(a->f))
3479 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3480 28315671 2019-08-14 stsp break;
3481 28315671 2019-08-14 stsp }
3482 82f6abb8 2019-08-14 stsp
3483 82f6abb8 2019-08-14 stsp committer = bline->committer;
3484 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3485 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3486 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3487 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3488 82f6abb8 2019-08-14 stsp if (at)
3489 82f6abb8 2019-08-14 stsp *at = '\0';
3490 82f6abb8 2019-08-14 stsp len = strlen(committer);
3491 82f6abb8 2019-08-14 stsp if (len >= 9)
3492 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3493 28315671 2019-08-14 stsp
3494 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3495 28315671 2019-08-14 stsp if (nl)
3496 28315671 2019-08-14 stsp *nl = '\0';
3497 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3498 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3499 28315671 2019-08-14 stsp
3500 28315671 2019-08-14 stsp a->lineno_cur++;
3501 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3502 28315671 2019-08-14 stsp }
3503 82f6abb8 2019-08-14 stsp done:
3504 82f6abb8 2019-08-14 stsp if (commit)
3505 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3506 28315671 2019-08-14 stsp free(line);
3507 28315671 2019-08-14 stsp return err;
3508 28315671 2019-08-14 stsp }
3509 28315671 2019-08-14 stsp
3510 28315671 2019-08-14 stsp static const struct got_error *
3511 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3512 404c43c4 2018-06-21 stsp {
3513 404c43c4 2018-06-21 stsp const struct got_error *error;
3514 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3515 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3516 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3517 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3518 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3519 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3520 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3521 28315671 2019-08-14 stsp struct blame_cb_args bca;
3522 28315671 2019-08-14 stsp int ch, obj_type, i;
3523 b02560ec 2019-08-19 stsp size_t filesize;
3524 90f3c347 2019-08-19 stsp
3525 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3526 404c43c4 2018-06-21 stsp
3527 404c43c4 2018-06-21 stsp #ifndef PROFILE
3528 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3529 36e2fb66 2019-01-04 stsp NULL) == -1)
3530 404c43c4 2018-06-21 stsp err(1, "pledge");
3531 404c43c4 2018-06-21 stsp #endif
3532 404c43c4 2018-06-21 stsp
3533 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3534 404c43c4 2018-06-21 stsp switch (ch) {
3535 404c43c4 2018-06-21 stsp case 'c':
3536 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3537 404c43c4 2018-06-21 stsp break;
3538 66bea077 2018-08-02 stsp case 'r':
3539 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3540 66bea077 2018-08-02 stsp if (repo_path == NULL)
3541 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3542 9ba1d308 2019-10-21 stsp optarg);
3543 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3544 66bea077 2018-08-02 stsp break;
3545 404c43c4 2018-06-21 stsp default:
3546 2deda0b9 2019-03-07 stsp usage_blame();
3547 404c43c4 2018-06-21 stsp /* NOTREACHED */
3548 404c43c4 2018-06-21 stsp }
3549 404c43c4 2018-06-21 stsp }
3550 404c43c4 2018-06-21 stsp
3551 404c43c4 2018-06-21 stsp argc -= optind;
3552 404c43c4 2018-06-21 stsp argv += optind;
3553 404c43c4 2018-06-21 stsp
3554 a39318fd 2018-08-02 stsp if (argc == 1)
3555 404c43c4 2018-06-21 stsp path = argv[0];
3556 a39318fd 2018-08-02 stsp else
3557 404c43c4 2018-06-21 stsp usage_blame();
3558 404c43c4 2018-06-21 stsp
3559 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3560 66bea077 2018-08-02 stsp if (cwd == NULL) {
3561 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3562 66bea077 2018-08-02 stsp goto done;
3563 66bea077 2018-08-02 stsp }
3564 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3565 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3566 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3567 66bea077 2018-08-02 stsp goto done;
3568 0c06baac 2019-02-05 stsp else
3569 0c06baac 2019-02-05 stsp error = NULL;
3570 0c06baac 2019-02-05 stsp if (worktree) {
3571 0c06baac 2019-02-05 stsp repo_path =
3572 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3573 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3574 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3575 1f03b8da 2020-03-20 stsp if (error)
3576 1f03b8da 2020-03-20 stsp goto done;
3577 1f03b8da 2020-03-20 stsp }
3578 0c06baac 2019-02-05 stsp } else {
3579 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3580 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3581 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3582 0c06baac 2019-02-05 stsp goto done;
3583 0c06baac 2019-02-05 stsp }
3584 66bea077 2018-08-02 stsp }
3585 66bea077 2018-08-02 stsp }
3586 36e2fb66 2019-01-04 stsp
3587 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3588 c02c541e 2019-03-29 stsp if (error != NULL)
3589 36e2fb66 2019-01-04 stsp goto done;
3590 66bea077 2018-08-02 stsp
3591 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3592 c02c541e 2019-03-29 stsp if (error)
3593 404c43c4 2018-06-21 stsp goto done;
3594 404c43c4 2018-06-21 stsp
3595 0c06baac 2019-02-05 stsp if (worktree) {
3596 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3597 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3598 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3599 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3600 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3601 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3602 6efaaa2d 2019-02-05 stsp path) == -1) {
3603 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3604 0c06baac 2019-02-05 stsp goto done;
3605 0c06baac 2019-02-05 stsp }
3606 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3607 0c06baac 2019-02-05 stsp free(p);
3608 0c06baac 2019-02-05 stsp } else {
3609 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3610 0c06baac 2019-02-05 stsp }
3611 0c06baac 2019-02-05 stsp if (error)
3612 66bea077 2018-08-02 stsp goto done;
3613 66bea077 2018-08-02 stsp
3614 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3615 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3616 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3617 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3618 404c43c4 2018-06-21 stsp if (error != NULL)
3619 66bea077 2018-08-02 stsp goto done;
3620 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3621 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3622 404c43c4 2018-06-21 stsp if (error != NULL)
3623 66bea077 2018-08-02 stsp goto done;
3624 404c43c4 2018-06-21 stsp } else {
3625 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3626 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3627 30837e32 2019-07-25 stsp if (error)
3628 66bea077 2018-08-02 stsp goto done;
3629 404c43c4 2018-06-21 stsp }
3630 404c43c4 2018-06-21 stsp
3631 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3632 28315671 2019-08-14 stsp if (error)
3633 28315671 2019-08-14 stsp goto done;
3634 28315671 2019-08-14 stsp
3635 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3636 28315671 2019-08-14 stsp if (error)
3637 28315671 2019-08-14 stsp goto done;
3638 28315671 2019-08-14 stsp
3639 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3640 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3641 28315671 2019-08-14 stsp goto done;
3642 28315671 2019-08-14 stsp }
3643 28315671 2019-08-14 stsp
3644 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3645 28315671 2019-08-14 stsp if (error)
3646 28315671 2019-08-14 stsp goto done;
3647 28315671 2019-08-14 stsp bca.f = got_opentemp();
3648 28315671 2019-08-14 stsp if (bca.f == NULL) {
3649 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3650 28315671 2019-08-14 stsp goto done;
3651 28315671 2019-08-14 stsp }
3652 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3653 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3654 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3655 28315671 2019-08-14 stsp goto done;
3656 28315671 2019-08-14 stsp
3657 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3658 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3659 b02560ec 2019-08-19 stsp bca.nlines--;
3660 b02560ec 2019-08-19 stsp
3661 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3662 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3663 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3664 28315671 2019-08-14 stsp goto done;
3665 28315671 2019-08-14 stsp }
3666 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3667 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3668 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3669 7ef28ff8 2019-08-14 stsp while (i > 0) {
3670 7ef28ff8 2019-08-14 stsp i /= 10;
3671 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3672 7ef28ff8 2019-08-14 stsp }
3673 82f6abb8 2019-08-14 stsp bca.repo = repo;
3674 7ef28ff8 2019-08-14 stsp
3675 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3676 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3677 404c43c4 2018-06-21 stsp done:
3678 66bea077 2018-08-02 stsp free(in_repo_path);
3679 66bea077 2018-08-02 stsp free(repo_path);
3680 66bea077 2018-08-02 stsp free(cwd);
3681 404c43c4 2018-06-21 stsp free(commit_id);
3682 28315671 2019-08-14 stsp free(obj_id);
3683 28315671 2019-08-14 stsp if (blob)
3684 28315671 2019-08-14 stsp got_object_blob_close(blob);
3685 0c06baac 2019-02-05 stsp if (worktree)
3686 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3687 ad242220 2018-09-08 stsp if (repo) {
3688 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3689 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3690 ad242220 2018-09-08 stsp if (error == NULL)
3691 ad242220 2018-09-08 stsp error = repo_error;
3692 ad242220 2018-09-08 stsp }
3693 3affba96 2019-09-22 stsp if (bca.lines) {
3694 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3695 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3696 3affba96 2019-09-22 stsp free(bline->id_str);
3697 3affba96 2019-09-22 stsp free(bline->committer);
3698 3affba96 2019-09-22 stsp }
3699 3affba96 2019-09-22 stsp free(bca.lines);
3700 28315671 2019-08-14 stsp }
3701 28315671 2019-08-14 stsp free(bca.line_offsets);
3702 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3703 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3704 404c43c4 2018-06-21 stsp return error;
3705 5de5890b 2018-10-18 stsp }
3706 5de5890b 2018-10-18 stsp
3707 5de5890b 2018-10-18 stsp __dead static void
3708 5de5890b 2018-10-18 stsp usage_tree(void)
3709 5de5890b 2018-10-18 stsp {
3710 c1669e2e 2019-01-09 stsp fprintf(stderr,
3711 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3712 5de5890b 2018-10-18 stsp getprogname());
3713 5de5890b 2018-10-18 stsp exit(1);
3714 5de5890b 2018-10-18 stsp }
3715 5de5890b 2018-10-18 stsp
3716 c1669e2e 2019-01-09 stsp static void
3717 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3718 c1669e2e 2019-01-09 stsp const char *root_path)
3719 c1669e2e 2019-01-09 stsp {
3720 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3721 848d6979 2019-08-12 stsp const char *modestr = "";
3722 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3723 5de5890b 2018-10-18 stsp
3724 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3725 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3726 c1669e2e 2019-01-09 stsp path++;
3727 c1669e2e 2019-01-09 stsp
3728 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3729 63c5ca5d 2019-08-24 stsp modestr = "$";
3730 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3731 848d6979 2019-08-12 stsp modestr = "@";
3732 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3733 848d6979 2019-08-12 stsp modestr = "/";
3734 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3735 848d6979 2019-08-12 stsp modestr = "*";
3736 848d6979 2019-08-12 stsp
3737 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3738 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3739 c1669e2e 2019-01-09 stsp }
3740 c1669e2e 2019-01-09 stsp
3741 5de5890b 2018-10-18 stsp static const struct got_error *
3742 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3743 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3744 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3745 5de5890b 2018-10-18 stsp {
3746 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3747 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3748 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3749 56e0773d 2019-11-28 stsp int nentries, i;
3750 5de5890b 2018-10-18 stsp
3751 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3752 5de5890b 2018-10-18 stsp if (err)
3753 5de5890b 2018-10-18 stsp goto done;
3754 5de5890b 2018-10-18 stsp
3755 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3756 5de5890b 2018-10-18 stsp if (err)
3757 5de5890b 2018-10-18 stsp goto done;
3758 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3759 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3760 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3761 5de5890b 2018-10-18 stsp char *id = NULL;
3762 84453469 2018-11-11 stsp
3763 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3764 84453469 2018-11-11 stsp break;
3765 84453469 2018-11-11 stsp
3766 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3767 5de5890b 2018-10-18 stsp if (show_ids) {
3768 5de5890b 2018-10-18 stsp char *id_str;
3769 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3770 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3771 5de5890b 2018-10-18 stsp if (err)
3772 5de5890b 2018-10-18 stsp goto done;
3773 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3774 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3775 5de5890b 2018-10-18 stsp free(id_str);
3776 5de5890b 2018-10-18 stsp goto done;
3777 5de5890b 2018-10-18 stsp }
3778 5de5890b 2018-10-18 stsp free(id_str);
3779 5de5890b 2018-10-18 stsp }
3780 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3781 5de5890b 2018-10-18 stsp free(id);
3782 c1669e2e 2019-01-09 stsp
3783 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3784 c1669e2e 2019-01-09 stsp char *child_path;
3785 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3786 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3787 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3788 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3789 c1669e2e 2019-01-09 stsp goto done;
3790 c1669e2e 2019-01-09 stsp }
3791 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3792 c1669e2e 2019-01-09 stsp root_path, repo);
3793 c1669e2e 2019-01-09 stsp free(child_path);
3794 c1669e2e 2019-01-09 stsp if (err)
3795 c1669e2e 2019-01-09 stsp goto done;
3796 c1669e2e 2019-01-09 stsp }
3797 5de5890b 2018-10-18 stsp }
3798 5de5890b 2018-10-18 stsp done:
3799 5de5890b 2018-10-18 stsp if (tree)
3800 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3801 5de5890b 2018-10-18 stsp free(tree_id);
3802 5de5890b 2018-10-18 stsp return err;
3803 404c43c4 2018-06-21 stsp }
3804 404c43c4 2018-06-21 stsp
3805 5de5890b 2018-10-18 stsp static const struct got_error *
3806 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3807 5de5890b 2018-10-18 stsp {
3808 5de5890b 2018-10-18 stsp const struct got_error *error;
3809 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3810 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3811 7a2c19d6 2019-02-05 stsp const char *path;
3812 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3813 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3814 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3815 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3816 5de5890b 2018-10-18 stsp int ch;
3817 5de5890b 2018-10-18 stsp
3818 5de5890b 2018-10-18 stsp #ifndef PROFILE
3819 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3820 0f8d269b 2019-01-04 stsp NULL) == -1)
3821 5de5890b 2018-10-18 stsp err(1, "pledge");
3822 5de5890b 2018-10-18 stsp #endif
3823 5de5890b 2018-10-18 stsp
3824 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3825 5de5890b 2018-10-18 stsp switch (ch) {
3826 5de5890b 2018-10-18 stsp case 'c':
3827 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3828 5de5890b 2018-10-18 stsp break;
3829 5de5890b 2018-10-18 stsp case 'r':
3830 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3831 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3833 9ba1d308 2019-10-21 stsp optarg);
3834 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3835 5de5890b 2018-10-18 stsp break;
3836 5de5890b 2018-10-18 stsp case 'i':
3837 5de5890b 2018-10-18 stsp show_ids = 1;
3838 5de5890b 2018-10-18 stsp break;
3839 c1669e2e 2019-01-09 stsp case 'R':
3840 c1669e2e 2019-01-09 stsp recurse = 1;
3841 c1669e2e 2019-01-09 stsp break;
3842 5de5890b 2018-10-18 stsp default:
3843 2deda0b9 2019-03-07 stsp usage_tree();
3844 5de5890b 2018-10-18 stsp /* NOTREACHED */
3845 5de5890b 2018-10-18 stsp }
3846 5de5890b 2018-10-18 stsp }
3847 5de5890b 2018-10-18 stsp
3848 5de5890b 2018-10-18 stsp argc -= optind;
3849 5de5890b 2018-10-18 stsp argv += optind;
3850 5de5890b 2018-10-18 stsp
3851 5de5890b 2018-10-18 stsp if (argc == 1)
3852 5de5890b 2018-10-18 stsp path = argv[0];
3853 5de5890b 2018-10-18 stsp else if (argc > 1)
3854 5de5890b 2018-10-18 stsp usage_tree();
3855 5de5890b 2018-10-18 stsp else
3856 7a2c19d6 2019-02-05 stsp path = NULL;
3857 7a2c19d6 2019-02-05 stsp
3858 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3859 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3860 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3861 9bf7a39b 2019-02-05 stsp goto done;
3862 9bf7a39b 2019-02-05 stsp }
3863 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3864 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3865 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3866 7a2c19d6 2019-02-05 stsp goto done;
3867 7a2c19d6 2019-02-05 stsp else
3868 7a2c19d6 2019-02-05 stsp error = NULL;
3869 7a2c19d6 2019-02-05 stsp if (worktree) {
3870 7a2c19d6 2019-02-05 stsp repo_path =
3871 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3872 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3873 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3874 7a2c19d6 2019-02-05 stsp if (error)
3875 7a2c19d6 2019-02-05 stsp goto done;
3876 7a2c19d6 2019-02-05 stsp } else {
3877 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3878 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3879 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3880 7a2c19d6 2019-02-05 stsp goto done;
3881 7a2c19d6 2019-02-05 stsp }
3882 5de5890b 2018-10-18 stsp }
3883 5de5890b 2018-10-18 stsp }
3884 5de5890b 2018-10-18 stsp
3885 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3886 5de5890b 2018-10-18 stsp if (error != NULL)
3887 c02c541e 2019-03-29 stsp goto done;
3888 c02c541e 2019-03-29 stsp
3889 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3890 c02c541e 2019-03-29 stsp if (error)
3891 5de5890b 2018-10-18 stsp goto done;
3892 5de5890b 2018-10-18 stsp
3893 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3894 9bf7a39b 2019-02-05 stsp if (worktree) {
3895 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3896 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3897 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3898 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3899 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3900 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3901 9bf7a39b 2019-02-05 stsp goto done;
3902 9bf7a39b 2019-02-05 stsp }
3903 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3904 9bf7a39b 2019-02-05 stsp free(p);
3905 9bf7a39b 2019-02-05 stsp if (error)
3906 9bf7a39b 2019-02-05 stsp goto done;
3907 9bf7a39b 2019-02-05 stsp } else
3908 9bf7a39b 2019-02-05 stsp path = "/";
3909 9bf7a39b 2019-02-05 stsp }
3910 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3911 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3912 9bf7a39b 2019-02-05 stsp if (error != NULL)
3913 9bf7a39b 2019-02-05 stsp goto done;
3914 9bf7a39b 2019-02-05 stsp }
3915 5de5890b 2018-10-18 stsp
3916 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3917 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3918 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3919 5de5890b 2018-10-18 stsp if (error != NULL)
3920 5de5890b 2018-10-18 stsp goto done;
3921 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3922 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3923 5de5890b 2018-10-18 stsp if (error != NULL)
3924 5de5890b 2018-10-18 stsp goto done;
3925 5de5890b 2018-10-18 stsp } else {
3926 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3927 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3928 30837e32 2019-07-25 stsp if (error)
3929 5de5890b 2018-10-18 stsp goto done;
3930 5de5890b 2018-10-18 stsp }
3931 5de5890b 2018-10-18 stsp
3932 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3933 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3934 5de5890b 2018-10-18 stsp done:
3935 5de5890b 2018-10-18 stsp free(in_repo_path);
3936 5de5890b 2018-10-18 stsp free(repo_path);
3937 5de5890b 2018-10-18 stsp free(cwd);
3938 5de5890b 2018-10-18 stsp free(commit_id);
3939 7a2c19d6 2019-02-05 stsp if (worktree)
3940 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3941 5de5890b 2018-10-18 stsp if (repo) {
3942 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3943 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3944 5de5890b 2018-10-18 stsp if (error == NULL)
3945 5de5890b 2018-10-18 stsp error = repo_error;
3946 5de5890b 2018-10-18 stsp }
3947 5de5890b 2018-10-18 stsp return error;
3948 5de5890b 2018-10-18 stsp }
3949 5de5890b 2018-10-18 stsp
3950 6bad629b 2019-02-04 stsp __dead static void
3951 6bad629b 2019-02-04 stsp usage_status(void)
3952 6bad629b 2019-02-04 stsp {
3953 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3954 6bad629b 2019-02-04 stsp exit(1);
3955 6bad629b 2019-02-04 stsp }
3956 5c860e29 2018-03-12 stsp
3957 b72f483a 2019-02-05 stsp static const struct got_error *
3958 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3959 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3960 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3961 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3962 6bad629b 2019-02-04 stsp {
3963 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3964 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3965 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3966 b72f483a 2019-02-05 stsp return NULL;
3967 6bad629b 2019-02-04 stsp }
3968 5c860e29 2018-03-12 stsp
3969 6bad629b 2019-02-04 stsp static const struct got_error *
3970 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3971 6bad629b 2019-02-04 stsp {
3972 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3973 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3974 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3975 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3976 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3977 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3978 a5edda0a 2019-07-27 stsp int ch;
3979 5c860e29 2018-03-12 stsp
3980 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3981 72ea6654 2019-07-27 stsp
3982 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3983 6bad629b 2019-02-04 stsp switch (ch) {
3984 5c860e29 2018-03-12 stsp default:
3985 2deda0b9 2019-03-07 stsp usage_status();
3986 6bad629b 2019-02-04 stsp /* NOTREACHED */
3987 5c860e29 2018-03-12 stsp }
3988 5c860e29 2018-03-12 stsp }
3989 5c860e29 2018-03-12 stsp
3990 6bad629b 2019-02-04 stsp argc -= optind;
3991 6bad629b 2019-02-04 stsp argv += optind;
3992 5c860e29 2018-03-12 stsp
3993 6bad629b 2019-02-04 stsp #ifndef PROFILE
3994 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3995 6bad629b 2019-02-04 stsp NULL) == -1)
3996 6bad629b 2019-02-04 stsp err(1, "pledge");
3997 f42b1b34 2018-03-12 stsp #endif
3998 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3999 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4000 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4001 927df6b7 2019-02-10 stsp goto done;
4002 927df6b7 2019-02-10 stsp }
4003 927df6b7 2019-02-10 stsp
4004 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4005 927df6b7 2019-02-10 stsp if (error != NULL)
4006 a5edda0a 2019-07-27 stsp goto done;
4007 6bad629b 2019-02-04 stsp
4008 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4009 c9956ddf 2019-09-08 stsp NULL);
4010 6bad629b 2019-02-04 stsp if (error != NULL)
4011 6bad629b 2019-02-04 stsp goto done;
4012 6bad629b 2019-02-04 stsp
4013 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4014 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4015 087fb88c 2019-08-04 stsp if (error)
4016 087fb88c 2019-08-04 stsp goto done;
4017 087fb88c 2019-08-04 stsp
4018 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4019 6bad629b 2019-02-04 stsp if (error)
4020 6bad629b 2019-02-04 stsp goto done;
4021 6bad629b 2019-02-04 stsp
4022 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4023 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4024 6bad629b 2019-02-04 stsp done:
4025 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4026 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4027 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4028 927df6b7 2019-02-10 stsp free(cwd);
4029 d0eebce4 2019-03-11 stsp return error;
4030 d0eebce4 2019-03-11 stsp }
4031 d0eebce4 2019-03-11 stsp
4032 d0eebce4 2019-03-11 stsp __dead static void
4033 d0eebce4 2019-03-11 stsp usage_ref(void)
4034 d0eebce4 2019-03-11 stsp {
4035 d0eebce4 2019-03-11 stsp fprintf(stderr,
4036 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
4037 d0eebce4 2019-03-11 stsp getprogname());
4038 d0eebce4 2019-03-11 stsp exit(1);
4039 d0eebce4 2019-03-11 stsp }
4040 d0eebce4 2019-03-11 stsp
4041 d0eebce4 2019-03-11 stsp static const struct got_error *
4042 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
4043 d0eebce4 2019-03-11 stsp {
4044 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4045 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4046 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4047 d0eebce4 2019-03-11 stsp
4048 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4049 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4050 d0eebce4 2019-03-11 stsp if (err)
4051 d0eebce4 2019-03-11 stsp return err;
4052 d0eebce4 2019-03-11 stsp
4053 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4054 d0eebce4 2019-03-11 stsp char *refstr;
4055 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4056 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4057 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4058 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4059 d0eebce4 2019-03-11 stsp free(refstr);
4060 d0eebce4 2019-03-11 stsp }
4061 d0eebce4 2019-03-11 stsp
4062 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4063 d0eebce4 2019-03-11 stsp return NULL;
4064 d0eebce4 2019-03-11 stsp }
4065 d0eebce4 2019-03-11 stsp
4066 d0eebce4 2019-03-11 stsp static const struct got_error *
4067 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4068 d0eebce4 2019-03-11 stsp {
4069 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4070 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4071 d0eebce4 2019-03-11 stsp
4072 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4073 d0eebce4 2019-03-11 stsp if (err)
4074 d0eebce4 2019-03-11 stsp return err;
4075 d0eebce4 2019-03-11 stsp
4076 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4077 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4078 d0eebce4 2019-03-11 stsp return err;
4079 d0eebce4 2019-03-11 stsp }
4080 d0eebce4 2019-03-11 stsp
4081 d0eebce4 2019-03-11 stsp static const struct got_error *
4082 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4083 d0eebce4 2019-03-11 stsp {
4084 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4085 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4086 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4087 d1644381 2019-07-14 stsp
4088 d1644381 2019-07-14 stsp /*
4089 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4090 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4091 d1644381 2019-07-14 stsp * an unintended typo.
4092 d1644381 2019-07-14 stsp */
4093 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4094 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4095 d0eebce4 2019-03-11 stsp
4096 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4097 dd88155e 2019-06-29 stsp repo);
4098 d83d9d5c 2019-05-13 stsp if (err) {
4099 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4100 d0eebce4 2019-03-11 stsp
4101 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4102 d83d9d5c 2019-05-13 stsp return err;
4103 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4104 d83d9d5c 2019-05-13 stsp if (err)
4105 d83d9d5c 2019-05-13 stsp return err;
4106 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4107 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4108 d83d9d5c 2019-05-13 stsp if (err)
4109 d83d9d5c 2019-05-13 stsp return err;
4110 d83d9d5c 2019-05-13 stsp }
4111 d83d9d5c 2019-05-13 stsp
4112 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4113 d0eebce4 2019-03-11 stsp if (err)
4114 d0eebce4 2019-03-11 stsp goto done;
4115 d0eebce4 2019-03-11 stsp
4116 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4117 d0eebce4 2019-03-11 stsp done:
4118 d0eebce4 2019-03-11 stsp if (ref)
4119 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4120 d0eebce4 2019-03-11 stsp free(id);
4121 d1c1ae5f 2019-08-12 stsp return err;
4122 d1c1ae5f 2019-08-12 stsp }
4123 d1c1ae5f 2019-08-12 stsp
4124 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4125 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4126 d1c1ae5f 2019-08-12 stsp {
4127 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4128 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4129 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4130 d1c1ae5f 2019-08-12 stsp
4131 d1c1ae5f 2019-08-12 stsp /*
4132 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4133 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4134 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4135 d1c1ae5f 2019-08-12 stsp */
4136 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4137 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4138 d1c1ae5f 2019-08-12 stsp
4139 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4140 d1c1ae5f 2019-08-12 stsp if (err)
4141 d1c1ae5f 2019-08-12 stsp return err;
4142 d1c1ae5f 2019-08-12 stsp
4143 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4144 d1c1ae5f 2019-08-12 stsp if (err)
4145 d1c1ae5f 2019-08-12 stsp goto done;
4146 d1c1ae5f 2019-08-12 stsp
4147 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4148 d1c1ae5f 2019-08-12 stsp done:
4149 d1c1ae5f 2019-08-12 stsp if (target_ref)
4150 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4151 d1c1ae5f 2019-08-12 stsp if (ref)
4152 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4153 d0eebce4 2019-03-11 stsp return err;
4154 d0eebce4 2019-03-11 stsp }
4155 d0eebce4 2019-03-11 stsp
4156 d0eebce4 2019-03-11 stsp static const struct got_error *
4157 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4158 d0eebce4 2019-03-11 stsp {
4159 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4160 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4161 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4162 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4163 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
4164 d0eebce4 2019-03-11 stsp const char *delref = NULL;
4165 d0eebce4 2019-03-11 stsp
4166 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
4167 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
4168 d0eebce4 2019-03-11 stsp switch (ch) {
4169 d0eebce4 2019-03-11 stsp case 'd':
4170 d0eebce4 2019-03-11 stsp delref = optarg;
4171 d0eebce4 2019-03-11 stsp break;
4172 d0eebce4 2019-03-11 stsp case 'r':
4173 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4174 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4175 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4176 9ba1d308 2019-10-21 stsp optarg);
4177 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4178 d0eebce4 2019-03-11 stsp break;
4179 d0eebce4 2019-03-11 stsp case 'l':
4180 d0eebce4 2019-03-11 stsp do_list = 1;
4181 d1c1ae5f 2019-08-12 stsp break;
4182 d1c1ae5f 2019-08-12 stsp case 's':
4183 d1c1ae5f 2019-08-12 stsp create_symref = 1;
4184 d0eebce4 2019-03-11 stsp break;
4185 d0eebce4 2019-03-11 stsp default:
4186 d0eebce4 2019-03-11 stsp usage_ref();
4187 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4188 d0eebce4 2019-03-11 stsp }
4189 d0eebce4 2019-03-11 stsp }
4190 d0eebce4 2019-03-11 stsp
4191 d0eebce4 2019-03-11 stsp if (do_list && delref)
4192 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
4193 d0eebce4 2019-03-11 stsp
4194 d0eebce4 2019-03-11 stsp argc -= optind;
4195 d0eebce4 2019-03-11 stsp argv += optind;
4196 d0eebce4 2019-03-11 stsp
4197 d0eebce4 2019-03-11 stsp if (do_list || delref) {
4198 d1c1ae5f 2019-08-12 stsp if (create_symref)
4199 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
4200 d1c1ae5f 2019-08-12 stsp "-l or -d options");
4201 d0eebce4 2019-03-11 stsp if (argc > 0)
4202 d0eebce4 2019-03-11 stsp usage_ref();
4203 d0eebce4 2019-03-11 stsp } else if (argc != 2)
4204 d0eebce4 2019-03-11 stsp usage_ref();
4205 d0eebce4 2019-03-11 stsp
4206 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4207 e0b57350 2019-03-12 stsp if (do_list) {
4208 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4209 e0b57350 2019-03-12 stsp NULL) == -1)
4210 e0b57350 2019-03-12 stsp err(1, "pledge");
4211 e0b57350 2019-03-12 stsp } else {
4212 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4213 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4214 e0b57350 2019-03-12 stsp err(1, "pledge");
4215 e0b57350 2019-03-12 stsp }
4216 d0eebce4 2019-03-11 stsp #endif
4217 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4218 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4219 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4220 d0eebce4 2019-03-11 stsp goto done;
4221 d0eebce4 2019-03-11 stsp }
4222 d0eebce4 2019-03-11 stsp
4223 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4224 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4225 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4226 d0eebce4 2019-03-11 stsp goto done;
4227 d0eebce4 2019-03-11 stsp else
4228 d0eebce4 2019-03-11 stsp error = NULL;
4229 d0eebce4 2019-03-11 stsp if (worktree) {
4230 d0eebce4 2019-03-11 stsp repo_path =
4231 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4232 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4233 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4234 d0eebce4 2019-03-11 stsp if (error)
4235 d0eebce4 2019-03-11 stsp goto done;
4236 d0eebce4 2019-03-11 stsp } else {
4237 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4238 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4239 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4240 d0eebce4 2019-03-11 stsp goto done;
4241 d0eebce4 2019-03-11 stsp }
4242 d0eebce4 2019-03-11 stsp }
4243 d0eebce4 2019-03-11 stsp }
4244 d0eebce4 2019-03-11 stsp
4245 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4246 d0eebce4 2019-03-11 stsp if (error != NULL)
4247 d0eebce4 2019-03-11 stsp goto done;
4248 d0eebce4 2019-03-11 stsp
4249 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4250 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4251 c02c541e 2019-03-29 stsp if (error)
4252 c02c541e 2019-03-29 stsp goto done;
4253 c02c541e 2019-03-29 stsp
4254 d0eebce4 2019-03-11 stsp if (do_list)
4255 d0eebce4 2019-03-11 stsp error = list_refs(repo);
4256 d0eebce4 2019-03-11 stsp else if (delref)
4257 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
4258 d1c1ae5f 2019-08-12 stsp else if (create_symref)
4259 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
4260 d0eebce4 2019-03-11 stsp else
4261 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
4262 4e759de4 2019-06-26 stsp done:
4263 4e759de4 2019-06-26 stsp if (repo)
4264 4e759de4 2019-06-26 stsp got_repo_close(repo);
4265 4e759de4 2019-06-26 stsp if (worktree)
4266 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4267 4e759de4 2019-06-26 stsp free(cwd);
4268 4e759de4 2019-06-26 stsp free(repo_path);
4269 4e759de4 2019-06-26 stsp return error;
4270 4e759de4 2019-06-26 stsp }
4271 4e759de4 2019-06-26 stsp
4272 4e759de4 2019-06-26 stsp __dead static void
4273 4e759de4 2019-06-26 stsp usage_branch(void)
4274 4e759de4 2019-06-26 stsp {
4275 4e759de4 2019-06-26 stsp fprintf(stderr,
4276 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4277 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4278 4e759de4 2019-06-26 stsp exit(1);
4279 4e759de4 2019-06-26 stsp }
4280 4e759de4 2019-06-26 stsp
4281 4e759de4 2019-06-26 stsp static const struct got_error *
4282 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4283 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4284 4e99b47e 2019-10-04 stsp {
4285 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4286 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4287 4e99b47e 2019-10-04 stsp char *refstr;
4288 4e99b47e 2019-10-04 stsp
4289 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4290 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4291 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4292 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4293 4e99b47e 2019-10-04 stsp
4294 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4295 4e99b47e 2019-10-04 stsp if (err)
4296 4e99b47e 2019-10-04 stsp return err;
4297 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4298 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4299 4e99b47e 2019-10-04 stsp marker = "* ";
4300 4e99b47e 2019-10-04 stsp else
4301 4e99b47e 2019-10-04 stsp marker = "~ ";
4302 4e99b47e 2019-10-04 stsp free(id);
4303 4e99b47e 2019-10-04 stsp }
4304 4e99b47e 2019-10-04 stsp
4305 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4306 4e99b47e 2019-10-04 stsp refname += 11;
4307 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4308 4e99b47e 2019-10-04 stsp refname += 18;
4309 4e99b47e 2019-10-04 stsp
4310 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4311 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4312 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4313 4e99b47e 2019-10-04 stsp
4314 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4315 4e99b47e 2019-10-04 stsp free(refstr);
4316 4e99b47e 2019-10-04 stsp return NULL;
4317 4e99b47e 2019-10-04 stsp }
4318 4e99b47e 2019-10-04 stsp
4319 4e99b47e 2019-10-04 stsp static const struct got_error *
4320 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4321 ad89fa31 2019-10-04 stsp {
4322 ad89fa31 2019-10-04 stsp const char *refname;
4323 ad89fa31 2019-10-04 stsp
4324 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4325 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4326 ad89fa31 2019-10-04 stsp
4327 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4328 ad89fa31 2019-10-04 stsp
4329 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4330 ad89fa31 2019-10-04 stsp refname += 11;
4331 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4332 ad89fa31 2019-10-04 stsp refname += 18;
4333 ad89fa31 2019-10-04 stsp
4334 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4335 ad89fa31 2019-10-04 stsp
4336 ad89fa31 2019-10-04 stsp return NULL;
4337 ad89fa31 2019-10-04 stsp }
4338 ad89fa31 2019-10-04 stsp
4339 ad89fa31 2019-10-04 stsp static const struct got_error *
4340 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4341 4e759de4 2019-06-26 stsp {
4342 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4343 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4344 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4345 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4346 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4347 4e759de4 2019-06-26 stsp
4348 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4349 ba882ee3 2019-07-11 stsp
4350 4e99b47e 2019-10-04 stsp if (worktree) {
4351 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4352 4e99b47e 2019-10-04 stsp worktree);
4353 4e99b47e 2019-10-04 stsp if (err)
4354 4e99b47e 2019-10-04 stsp return err;
4355 4e99b47e 2019-10-04 stsp
4356 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4357 4e99b47e 2019-10-04 stsp worktree);
4358 4e99b47e 2019-10-04 stsp if (err)
4359 4e99b47e 2019-10-04 stsp return err;
4360 4e99b47e 2019-10-04 stsp
4361 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4362 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4363 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4364 4e99b47e 2019-10-04 stsp if (err)
4365 4e99b47e 2019-10-04 stsp return err;
4366 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4367 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4368 4e99b47e 2019-10-04 stsp }
4369 4e99b47e 2019-10-04 stsp }
4370 4e99b47e 2019-10-04 stsp
4371 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4372 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4373 4e759de4 2019-06-26 stsp if (err)
4374 4e759de4 2019-06-26 stsp return err;
4375 4e759de4 2019-06-26 stsp
4376 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4377 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4378 4e759de4 2019-06-26 stsp
4379 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4380 4e759de4 2019-06-26 stsp return NULL;
4381 4e759de4 2019-06-26 stsp }
4382 4e759de4 2019-06-26 stsp
4383 4e759de4 2019-06-26 stsp static const struct got_error *
4384 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4385 45cd4e47 2019-08-25 stsp const char *branch_name)
4386 4e759de4 2019-06-26 stsp {
4387 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4388 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4389 4e759de4 2019-06-26 stsp char *refname;
4390 4e759de4 2019-06-26 stsp
4391 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4392 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4393 4e759de4 2019-06-26 stsp
4394 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4395 4e759de4 2019-06-26 stsp if (err)
4396 4e759de4 2019-06-26 stsp goto done;
4397 4e759de4 2019-06-26 stsp
4398 45cd4e47 2019-08-25 stsp if (worktree &&
4399 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4400 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4401 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4402 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4403 45cd4e47 2019-08-25 stsp goto done;
4404 45cd4e47 2019-08-25 stsp }
4405 45cd4e47 2019-08-25 stsp
4406 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4407 4e759de4 2019-06-26 stsp done:
4408 45cd4e47 2019-08-25 stsp if (ref)
4409 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4410 4e759de4 2019-06-26 stsp free(refname);
4411 4e759de4 2019-06-26 stsp return err;
4412 4e759de4 2019-06-26 stsp }
4413 4e759de4 2019-06-26 stsp
4414 4e759de4 2019-06-26 stsp static const struct got_error *
4415 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4416 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4417 4e759de4 2019-06-26 stsp {
4418 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4419 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4420 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4421 d3f84d51 2019-07-11 stsp
4422 d3f84d51 2019-07-11 stsp /*
4423 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4424 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4425 d3f84d51 2019-07-11 stsp * an unintended typo.
4426 d3f84d51 2019-07-11 stsp */
4427 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4428 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4429 4e759de4 2019-06-26 stsp
4430 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4431 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4432 4e759de4 2019-06-26 stsp goto done;
4433 4e759de4 2019-06-26 stsp }
4434 4e759de4 2019-06-26 stsp
4435 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4436 4e759de4 2019-06-26 stsp if (err == NULL) {
4437 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4438 4e759de4 2019-06-26 stsp goto done;
4439 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4440 4e759de4 2019-06-26 stsp goto done;
4441 4e759de4 2019-06-26 stsp
4442 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4443 4e759de4 2019-06-26 stsp if (err)
4444 4e759de4 2019-06-26 stsp goto done;
4445 4e759de4 2019-06-26 stsp
4446 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4447 d0eebce4 2019-03-11 stsp done:
4448 4e759de4 2019-06-26 stsp if (ref)
4449 4e759de4 2019-06-26 stsp got_ref_close(ref);
4450 4e759de4 2019-06-26 stsp free(base_refname);
4451 4e759de4 2019-06-26 stsp free(refname);
4452 4e759de4 2019-06-26 stsp return err;
4453 4e759de4 2019-06-26 stsp }
4454 4e759de4 2019-06-26 stsp
4455 4e759de4 2019-06-26 stsp static const struct got_error *
4456 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4457 4e759de4 2019-06-26 stsp {
4458 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4459 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4460 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4461 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4462 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4463 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4464 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4465 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4466 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4467 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4468 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4469 4e759de4 2019-06-26 stsp
4470 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4471 da76fce2 2020-02-24 stsp
4472 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4473 4e759de4 2019-06-26 stsp switch (ch) {
4474 a74f7e83 2019-11-10 stsp case 'c':
4475 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4476 a74f7e83 2019-11-10 stsp break;
4477 4e759de4 2019-06-26 stsp case 'd':
4478 4e759de4 2019-06-26 stsp delref = optarg;
4479 4e759de4 2019-06-26 stsp break;
4480 4e759de4 2019-06-26 stsp case 'r':
4481 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4482 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4483 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4484 9ba1d308 2019-10-21 stsp optarg);
4485 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4486 4e759de4 2019-06-26 stsp break;
4487 4e759de4 2019-06-26 stsp case 'l':
4488 4e759de4 2019-06-26 stsp do_list = 1;
4489 da76fce2 2020-02-24 stsp break;
4490 da76fce2 2020-02-24 stsp case 'n':
4491 da76fce2 2020-02-24 stsp do_update = 0;
4492 4e759de4 2019-06-26 stsp break;
4493 4e759de4 2019-06-26 stsp default:
4494 4e759de4 2019-06-26 stsp usage_branch();
4495 4e759de4 2019-06-26 stsp /* NOTREACHED */
4496 4e759de4 2019-06-26 stsp }
4497 4e759de4 2019-06-26 stsp }
4498 4e759de4 2019-06-26 stsp
4499 4e759de4 2019-06-26 stsp if (do_list && delref)
4500 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4501 4e759de4 2019-06-26 stsp
4502 4e759de4 2019-06-26 stsp argc -= optind;
4503 4e759de4 2019-06-26 stsp argv += optind;
4504 ad89fa31 2019-10-04 stsp
4505 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4506 ad89fa31 2019-10-04 stsp do_show = 1;
4507 4e759de4 2019-06-26 stsp
4508 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4509 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4510 a74f7e83 2019-11-10 stsp
4511 4e759de4 2019-06-26 stsp if (do_list || delref) {
4512 4e759de4 2019-06-26 stsp if (argc > 0)
4513 4e759de4 2019-06-26 stsp usage_branch();
4514 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4515 4e759de4 2019-06-26 stsp usage_branch();
4516 4e759de4 2019-06-26 stsp
4517 4e759de4 2019-06-26 stsp #ifndef PROFILE
4518 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4519 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4520 4e759de4 2019-06-26 stsp NULL) == -1)
4521 4e759de4 2019-06-26 stsp err(1, "pledge");
4522 4e759de4 2019-06-26 stsp } else {
4523 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4524 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4525 4e759de4 2019-06-26 stsp err(1, "pledge");
4526 4e759de4 2019-06-26 stsp }
4527 4e759de4 2019-06-26 stsp #endif
4528 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4529 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4530 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4531 4e759de4 2019-06-26 stsp goto done;
4532 4e759de4 2019-06-26 stsp }
4533 4e759de4 2019-06-26 stsp
4534 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4535 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4536 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4537 4e759de4 2019-06-26 stsp goto done;
4538 4e759de4 2019-06-26 stsp else
4539 4e759de4 2019-06-26 stsp error = NULL;
4540 4e759de4 2019-06-26 stsp if (worktree) {
4541 4e759de4 2019-06-26 stsp repo_path =
4542 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4543 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4544 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4545 4e759de4 2019-06-26 stsp if (error)
4546 4e759de4 2019-06-26 stsp goto done;
4547 4e759de4 2019-06-26 stsp } else {
4548 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4549 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4550 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4551 4e759de4 2019-06-26 stsp goto done;
4552 4e759de4 2019-06-26 stsp }
4553 4e759de4 2019-06-26 stsp }
4554 4e759de4 2019-06-26 stsp }
4555 4e759de4 2019-06-26 stsp
4556 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4557 4e759de4 2019-06-26 stsp if (error != NULL)
4558 4e759de4 2019-06-26 stsp goto done;
4559 4e759de4 2019-06-26 stsp
4560 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4561 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4562 4e759de4 2019-06-26 stsp if (error)
4563 4e759de4 2019-06-26 stsp goto done;
4564 4e759de4 2019-06-26 stsp
4565 ad89fa31 2019-10-04 stsp if (do_show)
4566 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4567 ad89fa31 2019-10-04 stsp else if (do_list)
4568 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4569 4e759de4 2019-06-26 stsp else if (delref)
4570 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4571 4e759de4 2019-06-26 stsp else {
4572 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4573 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4574 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4575 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4576 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4577 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4578 a74f7e83 2019-11-10 stsp if (error)
4579 a74f7e83 2019-11-10 stsp goto done;
4580 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4581 da76fce2 2020-02-24 stsp if (error)
4582 da76fce2 2020-02-24 stsp goto done;
4583 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4584 da76fce2 2020-02-24 stsp int did_something = 0;
4585 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4586 da76fce2 2020-02-24 stsp
4587 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4588 da76fce2 2020-02-24 stsp if (error)
4589 da76fce2 2020-02-24 stsp goto done;
4590 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4591 da76fce2 2020-02-24 stsp worktree);
4592 da76fce2 2020-02-24 stsp if (error)
4593 da76fce2 2020-02-24 stsp goto done;
4594 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4595 da76fce2 2020-02-24 stsp == -1) {
4596 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4597 da76fce2 2020-02-24 stsp goto done;
4598 da76fce2 2020-02-24 stsp }
4599 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4600 da76fce2 2020-02-24 stsp free(branch_refname);
4601 da76fce2 2020-02-24 stsp if (error)
4602 da76fce2 2020-02-24 stsp goto done;
4603 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4604 da76fce2 2020-02-24 stsp repo);
4605 da76fce2 2020-02-24 stsp if (error)
4606 da76fce2 2020-02-24 stsp goto done;
4607 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4608 da76fce2 2020-02-24 stsp commit_id);
4609 da76fce2 2020-02-24 stsp if (error)
4610 da76fce2 2020-02-24 stsp goto done;
4611 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4612 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4613 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4614 da76fce2 2020-02-24 stsp if (error)
4615 da76fce2 2020-02-24 stsp goto done;
4616 da76fce2 2020-02-24 stsp if (did_something)
4617 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4618 da76fce2 2020-02-24 stsp }
4619 4e759de4 2019-06-26 stsp }
4620 4e759de4 2019-06-26 stsp done:
4621 da76fce2 2020-02-24 stsp if (ref)
4622 da76fce2 2020-02-24 stsp got_ref_close(ref);
4623 d0eebce4 2019-03-11 stsp if (repo)
4624 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4625 d0eebce4 2019-03-11 stsp if (worktree)
4626 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4627 d0eebce4 2019-03-11 stsp free(cwd);
4628 d0eebce4 2019-03-11 stsp free(repo_path);
4629 da76fce2 2020-02-24 stsp free(commit_id);
4630 da76fce2 2020-02-24 stsp free(commit_id_str);
4631 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4632 da76fce2 2020-02-24 stsp free((char *)pe->path);
4633 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4634 d00136be 2019-03-26 stsp return error;
4635 d00136be 2019-03-26 stsp }
4636 d00136be 2019-03-26 stsp
4637 8e7bd50a 2019-08-22 stsp
4638 d00136be 2019-03-26 stsp __dead static void
4639 8e7bd50a 2019-08-22 stsp usage_tag(void)
4640 8e7bd50a 2019-08-22 stsp {
4641 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4642 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4643 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4644 8e7bd50a 2019-08-22 stsp exit(1);
4645 b8bad2ba 2019-08-23 stsp }
4646 b8bad2ba 2019-08-23 stsp
4647 b8bad2ba 2019-08-23 stsp #if 0
4648 b8bad2ba 2019-08-23 stsp static const struct got_error *
4649 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4650 b8bad2ba 2019-08-23 stsp {
4651 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4652 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4653 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4654 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4655 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4656 b8bad2ba 2019-08-23 stsp
4657 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4658 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4659 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4660 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4661 b8bad2ba 2019-08-23 stsp if (err)
4662 b8bad2ba 2019-08-23 stsp return err;
4663 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4664 b8bad2ba 2019-08-23 stsp continue;
4665 b8bad2ba 2019-08-23 stsp } else {
4666 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4667 b8bad2ba 2019-08-23 stsp if (err)
4668 b8bad2ba 2019-08-23 stsp break;
4669 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4670 b8bad2ba 2019-08-23 stsp free(re_id);
4671 b8bad2ba 2019-08-23 stsp if (err)
4672 b8bad2ba 2019-08-23 stsp break;
4673 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4674 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4675 b8bad2ba 2019-08-23 stsp }
4676 b8bad2ba 2019-08-23 stsp
4677 b8bad2ba 2019-08-23 stsp while (se) {
4678 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4679 b8bad2ba 2019-08-23 stsp if (err)
4680 b8bad2ba 2019-08-23 stsp break;
4681 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4682 b8bad2ba 2019-08-23 stsp free(se_id);
4683 b8bad2ba 2019-08-23 stsp if (err)
4684 b8bad2ba 2019-08-23 stsp break;
4685 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4686 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4687 b8bad2ba 2019-08-23 stsp
4688 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4689 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4690 b8bad2ba 2019-08-23 stsp if (err)
4691 b8bad2ba 2019-08-23 stsp return err;
4692 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4693 b8bad2ba 2019-08-23 stsp break;
4694 b8bad2ba 2019-08-23 stsp }
4695 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4696 b8bad2ba 2019-08-23 stsp continue;
4697 b8bad2ba 2019-08-23 stsp }
4698 b8bad2ba 2019-08-23 stsp }
4699 b8bad2ba 2019-08-23 stsp done:
4700 b8bad2ba 2019-08-23 stsp return err;
4701 8e7bd50a 2019-08-22 stsp }
4702 b8bad2ba 2019-08-23 stsp #endif
4703 b8bad2ba 2019-08-23 stsp
4704 b8bad2ba 2019-08-23 stsp static const struct got_error *
4705 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4706 8e7bd50a 2019-08-22 stsp {
4707 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4708 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4709 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4710 8e7bd50a 2019-08-22 stsp
4711 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4712 8e7bd50a 2019-08-22 stsp
4713 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4714 8e7bd50a 2019-08-22 stsp if (err)
4715 8e7bd50a 2019-08-22 stsp return err;
4716 8e7bd50a 2019-08-22 stsp
4717 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4718 8e7bd50a 2019-08-22 stsp const char *refname;
4719 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4720 8e7bd50a 2019-08-22 stsp char datebuf[26];
4721 d4efa91b 2020-01-14 stsp const char *tagger;
4722 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4723 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4724 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4725 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4726 8e7bd50a 2019-08-22 stsp
4727 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4728 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4729 8e7bd50a 2019-08-22 stsp continue;
4730 8e7bd50a 2019-08-22 stsp refname += 10;
4731 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4732 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4733 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4734 8e7bd50a 2019-08-22 stsp break;
4735 8e7bd50a 2019-08-22 stsp }
4736 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4737 8e7bd50a 2019-08-22 stsp free(refstr);
4738 8e7bd50a 2019-08-22 stsp
4739 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4740 8e7bd50a 2019-08-22 stsp if (err)
4741 8e7bd50a 2019-08-22 stsp break;
4742 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4743 d4efa91b 2020-01-14 stsp if (err) {
4744 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4745 d4efa91b 2020-01-14 stsp free(id);
4746 d4efa91b 2020-01-14 stsp break;
4747 d4efa91b 2020-01-14 stsp }
4748 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4749 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4750 d4efa91b 2020-01-14 stsp if (err) {
4751 d4efa91b 2020-01-14 stsp free(id);
4752 d4efa91b 2020-01-14 stsp break;
4753 d4efa91b 2020-01-14 stsp }
4754 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4755 d4efa91b 2020-01-14 stsp tagger_time =
4756 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4757 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4758 d4efa91b 2020-01-14 stsp free(id);
4759 d4efa91b 2020-01-14 stsp if (err)
4760 d4efa91b 2020-01-14 stsp break;
4761 d4efa91b 2020-01-14 stsp } else {
4762 d4efa91b 2020-01-14 stsp free(id);
4763 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4764 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4765 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4766 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4767 d4efa91b 2020-01-14 stsp if (err)
4768 d4efa91b 2020-01-14 stsp break;
4769 d4efa91b 2020-01-14 stsp }
4770 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4771 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4772 2417344c 2019-08-23 stsp if (datestr)
4773 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4774 d4efa91b 2020-01-14 stsp if (commit)
4775 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4776 d4efa91b 2020-01-14 stsp else {
4777 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4778 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4779 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4780 d4efa91b 2020-01-14 stsp id_str);
4781 d4efa91b 2020-01-14 stsp break;
4782 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4783 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4784 d4efa91b 2020-01-14 stsp id_str);
4785 d4efa91b 2020-01-14 stsp break;
4786 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4787 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4788 d4efa91b 2020-01-14 stsp id_str);
4789 d4efa91b 2020-01-14 stsp break;
4790 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4791 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4792 d4efa91b 2020-01-14 stsp id_str);
4793 d4efa91b 2020-01-14 stsp break;
4794 d4efa91b 2020-01-14 stsp default:
4795 d4efa91b 2020-01-14 stsp break;
4796 d4efa91b 2020-01-14 stsp }
4797 8e7bd50a 2019-08-22 stsp }
4798 8e7bd50a 2019-08-22 stsp free(id_str);
4799 d4efa91b 2020-01-14 stsp if (commit) {
4800 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4801 d4efa91b 2020-01-14 stsp if (err)
4802 d4efa91b 2020-01-14 stsp break;
4803 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4804 d4efa91b 2020-01-14 stsp } else {
4805 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4806 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4807 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4808 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4809 d4efa91b 2020-01-14 stsp break;
4810 d4efa91b 2020-01-14 stsp }
4811 8e7bd50a 2019-08-22 stsp }
4812 8e7bd50a 2019-08-22 stsp
4813 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4814 8e7bd50a 2019-08-22 stsp do {
4815 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4816 8e7bd50a 2019-08-22 stsp if (line)
4817 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4818 8e7bd50a 2019-08-22 stsp } while (line);
4819 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4820 8e7bd50a 2019-08-22 stsp }
4821 8e7bd50a 2019-08-22 stsp
4822 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4823 8e7bd50a 2019-08-22 stsp return NULL;
4824 8e7bd50a 2019-08-22 stsp }
4825 8e7bd50a 2019-08-22 stsp
4826 8e7bd50a 2019-08-22 stsp static const struct got_error *
4827 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4828 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4829 8e7bd50a 2019-08-22 stsp {
4830 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4831 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4832 f372d5cd 2019-10-21 stsp char *editor = NULL;
4833 8e7bd50a 2019-08-22 stsp int fd = -1;
4834 8e7bd50a 2019-08-22 stsp
4835 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4836 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4837 8e7bd50a 2019-08-22 stsp goto done;
4838 8e7bd50a 2019-08-22 stsp }
4839 8e7bd50a 2019-08-22 stsp
4840 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4841 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4842 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4843 8e7bd50a 2019-08-22 stsp goto done;
4844 8e7bd50a 2019-08-22 stsp }
4845 8e7bd50a 2019-08-22 stsp
4846 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4847 8e7bd50a 2019-08-22 stsp if (err)
4848 8e7bd50a 2019-08-22 stsp goto done;
4849 8e7bd50a 2019-08-22 stsp
4850 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4851 8e7bd50a 2019-08-22 stsp close(fd);
4852 8e7bd50a 2019-08-22 stsp
4853 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4854 8e7bd50a 2019-08-22 stsp if (err)
4855 8e7bd50a 2019-08-22 stsp goto done;
4856 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4857 8e7bd50a 2019-08-22 stsp done:
4858 8e7bd50a 2019-08-22 stsp free(initial_content);
4859 8e7bd50a 2019-08-22 stsp free(template);
4860 8e7bd50a 2019-08-22 stsp free(editor);
4861 8e7bd50a 2019-08-22 stsp
4862 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4863 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4864 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4865 8e7bd50a 2019-08-22 stsp if (err) {
4866 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4867 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4868 8e7bd50a 2019-08-22 stsp }
4869 8e7bd50a 2019-08-22 stsp }
4870 8e7bd50a 2019-08-22 stsp return err;
4871 8e7bd50a 2019-08-22 stsp }
4872 8e7bd50a 2019-08-22 stsp
4873 8e7bd50a 2019-08-22 stsp static const struct got_error *
4874 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4875 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4876 8e7bd50a 2019-08-22 stsp {
4877 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4878 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4879 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4880 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4881 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4882 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4883 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4884 8e7bd50a 2019-08-22 stsp
4885 8e7bd50a 2019-08-22 stsp /*
4886 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4887 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4888 8e7bd50a 2019-08-22 stsp * an unintended typo.
4889 8e7bd50a 2019-08-22 stsp */
4890 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4891 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4892 8e7bd50a 2019-08-22 stsp
4893 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4894 8e7bd50a 2019-08-22 stsp if (err)
4895 8e7bd50a 2019-08-22 stsp return err;
4896 8e7bd50a 2019-08-22 stsp
4897 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4898 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4899 8e7bd50a 2019-08-22 stsp if (err)
4900 8e7bd50a 2019-08-22 stsp goto done;
4901 8e7bd50a 2019-08-22 stsp
4902 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4903 8e7bd50a 2019-08-22 stsp if (err)
4904 8e7bd50a 2019-08-22 stsp goto done;
4905 8e7bd50a 2019-08-22 stsp
4906 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4907 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4908 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4909 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4910 8e7bd50a 2019-08-22 stsp goto done;
4911 8e7bd50a 2019-08-22 stsp }
4912 8e7bd50a 2019-08-22 stsp tag_name += 10;
4913 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4914 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4915 8e7bd50a 2019-08-22 stsp goto done;
4916 8e7bd50a 2019-08-22 stsp }
4917 8e7bd50a 2019-08-22 stsp
4918 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4919 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4920 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4921 8e7bd50a 2019-08-22 stsp goto done;
4922 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4923 8e7bd50a 2019-08-22 stsp goto done;
4924 8e7bd50a 2019-08-22 stsp
4925 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4926 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4927 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4928 f372d5cd 2019-10-21 stsp if (err) {
4929 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4930 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4931 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4932 8e7bd50a 2019-08-22 stsp goto done;
4933 f372d5cd 2019-10-21 stsp }
4934 8e7bd50a 2019-08-22 stsp }
4935 8e7bd50a 2019-08-22 stsp
4936 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4937 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4938 f372d5cd 2019-10-21 stsp if (err) {
4939 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4940 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4941 8e7bd50a 2019-08-22 stsp goto done;
4942 f372d5cd 2019-10-21 stsp }
4943 8e7bd50a 2019-08-22 stsp
4944 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4945 f372d5cd 2019-10-21 stsp if (err) {
4946 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4947 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4948 8e7bd50a 2019-08-22 stsp goto done;
4949 f372d5cd 2019-10-21 stsp }
4950 8e7bd50a 2019-08-22 stsp
4951 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4952 f372d5cd 2019-10-21 stsp if (err) {
4953 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4954 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4955 f372d5cd 2019-10-21 stsp goto done;
4956 f372d5cd 2019-10-21 stsp }
4957 8e7bd50a 2019-08-22 stsp
4958 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4959 f372d5cd 2019-10-21 stsp if (err) {
4960 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4961 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4962 f372d5cd 2019-10-21 stsp goto done;
4963 8e7bd50a 2019-08-22 stsp }
4964 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4965 8e7bd50a 2019-08-22 stsp done:
4966 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4967 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4968 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4969 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4970 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4971 f372d5cd 2019-10-21 stsp free(tag_id_str);
4972 8e7bd50a 2019-08-22 stsp if (ref)
4973 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4974 8e7bd50a 2019-08-22 stsp free(commit_id);
4975 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4976 8e7bd50a 2019-08-22 stsp free(refname);
4977 8e7bd50a 2019-08-22 stsp free(tagmsg);
4978 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4979 aba9c984 2019-09-08 stsp free(tagger);
4980 8e7bd50a 2019-08-22 stsp return err;
4981 8e7bd50a 2019-08-22 stsp }
4982 8e7bd50a 2019-08-22 stsp
4983 8e7bd50a 2019-08-22 stsp static const struct got_error *
4984 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4985 8e7bd50a 2019-08-22 stsp {
4986 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4987 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4988 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4989 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4990 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4991 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4992 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4993 8e7bd50a 2019-08-22 stsp
4994 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4995 8e7bd50a 2019-08-22 stsp switch (ch) {
4996 80106605 2020-02-24 stsp case 'c':
4997 80106605 2020-02-24 stsp commit_id_arg = optarg;
4998 80106605 2020-02-24 stsp break;
4999 8e7bd50a 2019-08-22 stsp case 'm':
5000 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5001 8e7bd50a 2019-08-22 stsp break;
5002 8e7bd50a 2019-08-22 stsp case 'r':
5003 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5004 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5005 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5006 9ba1d308 2019-10-21 stsp optarg);
5007 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5008 8e7bd50a 2019-08-22 stsp break;
5009 8e7bd50a 2019-08-22 stsp case 'l':
5010 8e7bd50a 2019-08-22 stsp do_list = 1;
5011 8e7bd50a 2019-08-22 stsp break;
5012 8e7bd50a 2019-08-22 stsp default:
5013 8e7bd50a 2019-08-22 stsp usage_tag();
5014 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5015 8e7bd50a 2019-08-22 stsp }
5016 8e7bd50a 2019-08-22 stsp }
5017 8e7bd50a 2019-08-22 stsp
5018 8e7bd50a 2019-08-22 stsp argc -= optind;
5019 8e7bd50a 2019-08-22 stsp argv += optind;
5020 8e7bd50a 2019-08-22 stsp
5021 8e7bd50a 2019-08-22 stsp if (do_list) {
5022 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5023 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
5024 8e7bd50a 2019-08-22 stsp if (tagmsg)
5025 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5026 8e7bd50a 2019-08-22 stsp if (argc > 0)
5027 8e7bd50a 2019-08-22 stsp usage_tag();
5028 80106605 2020-02-24 stsp } else if (argc != 1)
5029 8e7bd50a 2019-08-22 stsp usage_tag();
5030 80106605 2020-02-24 stsp
5031 a2887370 2019-08-23 stsp tag_name = argv[0];
5032 8e7bd50a 2019-08-22 stsp
5033 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5034 8e7bd50a 2019-08-22 stsp if (do_list) {
5035 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5036 8e7bd50a 2019-08-22 stsp NULL) == -1)
5037 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5038 8e7bd50a 2019-08-22 stsp } else {
5039 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5040 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5041 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5042 8e7bd50a 2019-08-22 stsp }
5043 8e7bd50a 2019-08-22 stsp #endif
5044 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5045 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5046 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5047 8e7bd50a 2019-08-22 stsp goto done;
5048 8e7bd50a 2019-08-22 stsp }
5049 8e7bd50a 2019-08-22 stsp
5050 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5051 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5052 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5053 8e7bd50a 2019-08-22 stsp goto done;
5054 8e7bd50a 2019-08-22 stsp else
5055 8e7bd50a 2019-08-22 stsp error = NULL;
5056 8e7bd50a 2019-08-22 stsp if (worktree) {
5057 8e7bd50a 2019-08-22 stsp repo_path =
5058 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5059 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5060 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5061 8e7bd50a 2019-08-22 stsp if (error)
5062 8e7bd50a 2019-08-22 stsp goto done;
5063 8e7bd50a 2019-08-22 stsp } else {
5064 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5065 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5066 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5067 8e7bd50a 2019-08-22 stsp goto done;
5068 8e7bd50a 2019-08-22 stsp }
5069 8e7bd50a 2019-08-22 stsp }
5070 8e7bd50a 2019-08-22 stsp }
5071 8e7bd50a 2019-08-22 stsp
5072 8e7bd50a 2019-08-22 stsp if (do_list) {
5073 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5074 c9956ddf 2019-09-08 stsp if (error != NULL)
5075 c9956ddf 2019-09-08 stsp goto done;
5076 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5077 8e7bd50a 2019-08-22 stsp if (error)
5078 8e7bd50a 2019-08-22 stsp goto done;
5079 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5080 8e7bd50a 2019-08-22 stsp } else {
5081 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5082 c9956ddf 2019-09-08 stsp if (error)
5083 c9956ddf 2019-09-08 stsp goto done;
5084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5085 c9956ddf 2019-09-08 stsp if (error != NULL)
5086 c9956ddf 2019-09-08 stsp goto done;
5087 c9956ddf 2019-09-08 stsp
5088 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5089 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5090 8e7bd50a 2019-08-22 stsp if (error)
5091 8e7bd50a 2019-08-22 stsp goto done;
5092 8e7bd50a 2019-08-22 stsp }
5093 8e7bd50a 2019-08-22 stsp
5094 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5095 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5096 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5097 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5098 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5099 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5100 8e7bd50a 2019-08-22 stsp if (error)
5101 8e7bd50a 2019-08-22 stsp goto done;
5102 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5103 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5104 8e7bd50a 2019-08-22 stsp if (error)
5105 8e7bd50a 2019-08-22 stsp goto done;
5106 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5107 8e7bd50a 2019-08-22 stsp free(commit_id);
5108 8e7bd50a 2019-08-22 stsp if (error)
5109 8e7bd50a 2019-08-22 stsp goto done;
5110 8e7bd50a 2019-08-22 stsp }
5111 8e7bd50a 2019-08-22 stsp
5112 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5113 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5114 8e7bd50a 2019-08-22 stsp }
5115 8e7bd50a 2019-08-22 stsp done:
5116 8e7bd50a 2019-08-22 stsp if (repo)
5117 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5118 8e7bd50a 2019-08-22 stsp if (worktree)
5119 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5120 8e7bd50a 2019-08-22 stsp free(cwd);
5121 8e7bd50a 2019-08-22 stsp free(repo_path);
5122 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5123 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5124 8e7bd50a 2019-08-22 stsp return error;
5125 8e7bd50a 2019-08-22 stsp }
5126 8e7bd50a 2019-08-22 stsp
5127 8e7bd50a 2019-08-22 stsp __dead static void
5128 d00136be 2019-03-26 stsp usage_add(void)
5129 d00136be 2019-03-26 stsp {
5130 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5131 022fae89 2019-12-06 tracey getprogname());
5132 d00136be 2019-03-26 stsp exit(1);
5133 d00136be 2019-03-26 stsp }
5134 d00136be 2019-03-26 stsp
5135 d00136be 2019-03-26 stsp static const struct got_error *
5136 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5137 4e68cba3 2019-11-23 stsp {
5138 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5139 4e68cba3 2019-11-23 stsp path++;
5140 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5141 4e68cba3 2019-11-23 stsp return NULL;
5142 4e68cba3 2019-11-23 stsp }
5143 4e68cba3 2019-11-23 stsp
5144 4e68cba3 2019-11-23 stsp static const struct got_error *
5145 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5146 d00136be 2019-03-26 stsp {
5147 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5148 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5149 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5150 1dd54920 2019-05-11 stsp char *cwd = NULL;
5151 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5152 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5153 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5154 1dd54920 2019-05-11 stsp
5155 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5156 d00136be 2019-03-26 stsp
5157 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5158 d00136be 2019-03-26 stsp switch (ch) {
5159 022fae89 2019-12-06 tracey case 'I':
5160 022fae89 2019-12-06 tracey no_ignores = 1;
5161 022fae89 2019-12-06 tracey break;
5162 4e68cba3 2019-11-23 stsp case 'R':
5163 4e68cba3 2019-11-23 stsp can_recurse = 1;
5164 4e68cba3 2019-11-23 stsp break;
5165 d00136be 2019-03-26 stsp default:
5166 d00136be 2019-03-26 stsp usage_add();
5167 d00136be 2019-03-26 stsp /* NOTREACHED */
5168 d00136be 2019-03-26 stsp }
5169 d00136be 2019-03-26 stsp }
5170 d00136be 2019-03-26 stsp
5171 d00136be 2019-03-26 stsp argc -= optind;
5172 d00136be 2019-03-26 stsp argv += optind;
5173 d00136be 2019-03-26 stsp
5174 43012d58 2019-07-14 stsp #ifndef PROFILE
5175 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5176 43012d58 2019-07-14 stsp NULL) == -1)
5177 43012d58 2019-07-14 stsp err(1, "pledge");
5178 43012d58 2019-07-14 stsp #endif
5179 723c305c 2019-05-11 jcs if (argc < 1)
5180 d00136be 2019-03-26 stsp usage_add();
5181 d00136be 2019-03-26 stsp
5182 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5183 d00136be 2019-03-26 stsp if (cwd == NULL) {
5184 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5185 d00136be 2019-03-26 stsp goto done;
5186 d00136be 2019-03-26 stsp }
5187 723c305c 2019-05-11 jcs
5188 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5189 d00136be 2019-03-26 stsp if (error)
5190 d00136be 2019-03-26 stsp goto done;
5191 d00136be 2019-03-26 stsp
5192 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5193 c9956ddf 2019-09-08 stsp NULL);
5194 031a5338 2019-03-26 stsp if (error != NULL)
5195 031a5338 2019-03-26 stsp goto done;
5196 031a5338 2019-03-26 stsp
5197 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5198 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5199 d00136be 2019-03-26 stsp if (error)
5200 d00136be 2019-03-26 stsp goto done;
5201 d00136be 2019-03-26 stsp
5202 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5203 6d022e97 2019-08-04 stsp if (error)
5204 022fae89 2019-12-06 tracey goto done;
5205 022fae89 2019-12-06 tracey
5206 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5207 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5208 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5209 6d022e97 2019-08-04 stsp goto done;
5210 723c305c 2019-05-11 jcs
5211 022fae89 2019-12-06 tracey }
5212 022fae89 2019-12-06 tracey
5213 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5214 4e68cba3 2019-11-23 stsp char *ondisk_path;
5215 4e68cba3 2019-11-23 stsp struct stat sb;
5216 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5217 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5218 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5219 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5220 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5221 4e68cba3 2019-11-23 stsp goto done;
5222 4e68cba3 2019-11-23 stsp }
5223 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5224 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5225 4e68cba3 2019-11-23 stsp free(ondisk_path);
5226 4e68cba3 2019-11-23 stsp continue;
5227 4e68cba3 2019-11-23 stsp }
5228 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5229 4e68cba3 2019-11-23 stsp ondisk_path);
5230 4e68cba3 2019-11-23 stsp free(ondisk_path);
5231 4e68cba3 2019-11-23 stsp goto done;
5232 4e68cba3 2019-11-23 stsp }
5233 4e68cba3 2019-11-23 stsp free(ondisk_path);
5234 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5235 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5236 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5237 4e68cba3 2019-11-23 stsp goto done;
5238 4e68cba3 2019-11-23 stsp }
5239 4e68cba3 2019-11-23 stsp }
5240 4e68cba3 2019-11-23 stsp }
5241 022fae89 2019-12-06 tracey
5242 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5243 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5244 d00136be 2019-03-26 stsp done:
5245 031a5338 2019-03-26 stsp if (repo)
5246 031a5338 2019-03-26 stsp got_repo_close(repo);
5247 d00136be 2019-03-26 stsp if (worktree)
5248 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5249 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5250 1dd54920 2019-05-11 stsp free((char *)pe->path);
5251 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5252 2ec1f75b 2019-03-26 stsp free(cwd);
5253 2ec1f75b 2019-03-26 stsp return error;
5254 2ec1f75b 2019-03-26 stsp }
5255 2ec1f75b 2019-03-26 stsp
5256 2ec1f75b 2019-03-26 stsp __dead static void
5257 648e4ef7 2019-07-09 stsp usage_remove(void)
5258 2ec1f75b 2019-03-26 stsp {
5259 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5260 f2a9dc41 2019-12-13 tracey getprogname());
5261 2ec1f75b 2019-03-26 stsp exit(1);
5262 2a06fe5f 2019-08-24 stsp }
5263 2a06fe5f 2019-08-24 stsp
5264 2a06fe5f 2019-08-24 stsp static const struct got_error *
5265 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5266 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5267 2a06fe5f 2019-08-24 stsp {
5268 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5269 f2a9dc41 2019-12-13 tracey path++;
5270 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5271 2a06fe5f 2019-08-24 stsp return NULL;
5272 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5273 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5274 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5275 2a06fe5f 2019-08-24 stsp return NULL;
5276 2ec1f75b 2019-03-26 stsp }
5277 2ec1f75b 2019-03-26 stsp
5278 2ec1f75b 2019-03-26 stsp static const struct got_error *
5279 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5280 2ec1f75b 2019-03-26 stsp {
5281 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5282 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5283 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5284 17ed4618 2019-06-02 stsp char *cwd = NULL;
5285 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5286 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5287 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5288 2ec1f75b 2019-03-26 stsp
5289 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5290 17ed4618 2019-06-02 stsp
5291 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5292 2ec1f75b 2019-03-26 stsp switch (ch) {
5293 2ec1f75b 2019-03-26 stsp case 'f':
5294 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5295 2ec1f75b 2019-03-26 stsp break;
5296 70e3e7f5 2019-12-13 tracey case 'k':
5297 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5298 70e3e7f5 2019-12-13 tracey break;
5299 f2a9dc41 2019-12-13 tracey case 'R':
5300 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5301 f2a9dc41 2019-12-13 tracey break;
5302 2ec1f75b 2019-03-26 stsp default:
5303 f2a9dc41 2019-12-13 tracey usage_remove();
5304 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5305 2ec1f75b 2019-03-26 stsp }
5306 2ec1f75b 2019-03-26 stsp }
5307 2ec1f75b 2019-03-26 stsp
5308 2ec1f75b 2019-03-26 stsp argc -= optind;
5309 2ec1f75b 2019-03-26 stsp argv += optind;
5310 2ec1f75b 2019-03-26 stsp
5311 43012d58 2019-07-14 stsp #ifndef PROFILE
5312 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5313 43012d58 2019-07-14 stsp NULL) == -1)
5314 43012d58 2019-07-14 stsp err(1, "pledge");
5315 43012d58 2019-07-14 stsp #endif
5316 17ed4618 2019-06-02 stsp if (argc < 1)
5317 648e4ef7 2019-07-09 stsp usage_remove();
5318 2ec1f75b 2019-03-26 stsp
5319 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5320 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5321 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5322 2ec1f75b 2019-03-26 stsp goto done;
5323 2ec1f75b 2019-03-26 stsp }
5324 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5325 2ec1f75b 2019-03-26 stsp if (error)
5326 2ec1f75b 2019-03-26 stsp goto done;
5327 2ec1f75b 2019-03-26 stsp
5328 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5329 c9956ddf 2019-09-08 stsp NULL);
5330 2af4a041 2019-05-11 jcs if (error)
5331 2ec1f75b 2019-03-26 stsp goto done;
5332 2ec1f75b 2019-03-26 stsp
5333 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5334 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5335 2ec1f75b 2019-03-26 stsp if (error)
5336 2ec1f75b 2019-03-26 stsp goto done;
5337 2ec1f75b 2019-03-26 stsp
5338 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5339 6d022e97 2019-08-04 stsp if (error)
5340 6d022e97 2019-08-04 stsp goto done;
5341 17ed4618 2019-06-02 stsp
5342 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5343 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5344 f2a9dc41 2019-12-13 tracey struct stat sb;
5345 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5346 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5347 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5348 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5349 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5350 f2a9dc41 2019-12-13 tracey goto done;
5351 f2a9dc41 2019-12-13 tracey }
5352 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5353 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5354 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5355 f2a9dc41 2019-12-13 tracey continue;
5356 f2a9dc41 2019-12-13 tracey }
5357 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5358 f2a9dc41 2019-12-13 tracey ondisk_path);
5359 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5360 f2a9dc41 2019-12-13 tracey goto done;
5361 f2a9dc41 2019-12-13 tracey }
5362 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5363 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5364 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5365 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5366 f2a9dc41 2019-12-13 tracey goto done;
5367 f2a9dc41 2019-12-13 tracey }
5368 f2a9dc41 2019-12-13 tracey }
5369 f2a9dc41 2019-12-13 tracey }
5370 f2a9dc41 2019-12-13 tracey
5371 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5372 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5373 a129376b 2019-03-28 stsp done:
5374 a129376b 2019-03-28 stsp if (repo)
5375 a129376b 2019-03-28 stsp got_repo_close(repo);
5376 a129376b 2019-03-28 stsp if (worktree)
5377 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5378 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5379 17ed4618 2019-06-02 stsp free((char *)pe->path);
5380 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5381 a129376b 2019-03-28 stsp free(cwd);
5382 a129376b 2019-03-28 stsp return error;
5383 a129376b 2019-03-28 stsp }
5384 a129376b 2019-03-28 stsp
5385 a129376b 2019-03-28 stsp __dead static void
5386 a129376b 2019-03-28 stsp usage_revert(void)
5387 a129376b 2019-03-28 stsp {
5388 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5389 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5390 a129376b 2019-03-28 stsp exit(1);
5391 a129376b 2019-03-28 stsp }
5392 a129376b 2019-03-28 stsp
5393 1ee397ad 2019-07-12 stsp static const struct got_error *
5394 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5395 a129376b 2019-03-28 stsp {
5396 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5397 fb9704af 2020-01-27 stsp return NULL;
5398 fb9704af 2020-01-27 stsp
5399 a129376b 2019-03-28 stsp while (path[0] == '/')
5400 a129376b 2019-03-28 stsp path++;
5401 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5402 33aa809d 2019-08-08 stsp return NULL;
5403 33aa809d 2019-08-08 stsp }
5404 33aa809d 2019-08-08 stsp
5405 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5406 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5407 33aa809d 2019-08-08 stsp const char *action;
5408 33aa809d 2019-08-08 stsp };
5409 33aa809d 2019-08-08 stsp
5410 33aa809d 2019-08-08 stsp static const struct got_error *
5411 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5412 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5413 33aa809d 2019-08-08 stsp {
5414 33aa809d 2019-08-08 stsp char *line = NULL;
5415 33aa809d 2019-08-08 stsp size_t linesize = 0;
5416 33aa809d 2019-08-08 stsp ssize_t linelen;
5417 33aa809d 2019-08-08 stsp
5418 33aa809d 2019-08-08 stsp switch (status) {
5419 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5420 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5421 33aa809d 2019-08-08 stsp break;
5422 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5423 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5424 33aa809d 2019-08-08 stsp break;
5425 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5426 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5427 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5428 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5429 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5430 33aa809d 2019-08-08 stsp printf("%s", line);
5431 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5432 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5433 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5434 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5435 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5436 33aa809d 2019-08-08 stsp break;
5437 33aa809d 2019-08-08 stsp default:
5438 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5439 33aa809d 2019-08-08 stsp }
5440 33aa809d 2019-08-08 stsp
5441 33aa809d 2019-08-08 stsp return NULL;
5442 33aa809d 2019-08-08 stsp }
5443 33aa809d 2019-08-08 stsp
5444 33aa809d 2019-08-08 stsp static const struct got_error *
5445 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5446 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5447 33aa809d 2019-08-08 stsp {
5448 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5449 33aa809d 2019-08-08 stsp char *line = NULL;
5450 33aa809d 2019-08-08 stsp size_t linesize = 0;
5451 33aa809d 2019-08-08 stsp ssize_t linelen;
5452 33aa809d 2019-08-08 stsp int resp = ' ';
5453 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5454 33aa809d 2019-08-08 stsp
5455 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5456 33aa809d 2019-08-08 stsp
5457 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5458 33aa809d 2019-08-08 stsp char *nl;
5459 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5460 33aa809d 2019-08-08 stsp a->action);
5461 33aa809d 2019-08-08 stsp if (err)
5462 33aa809d 2019-08-08 stsp return err;
5463 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5464 33aa809d 2019-08-08 stsp if (linelen == -1) {
5465 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5466 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5467 33aa809d 2019-08-08 stsp return NULL;
5468 33aa809d 2019-08-08 stsp }
5469 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5470 33aa809d 2019-08-08 stsp if (nl)
5471 33aa809d 2019-08-08 stsp *nl = '\0';
5472 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5473 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5474 33aa809d 2019-08-08 stsp printf("y\n");
5475 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5476 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5477 33aa809d 2019-08-08 stsp printf("n\n");
5478 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5479 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5480 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5481 33aa809d 2019-08-08 stsp printf("q\n");
5482 33aa809d 2019-08-08 stsp } else
5483 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5484 33aa809d 2019-08-08 stsp free(line);
5485 33aa809d 2019-08-08 stsp return NULL;
5486 33aa809d 2019-08-08 stsp }
5487 33aa809d 2019-08-08 stsp
5488 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5489 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5490 33aa809d 2019-08-08 stsp a->action);
5491 33aa809d 2019-08-08 stsp if (err)
5492 33aa809d 2019-08-08 stsp return err;
5493 33aa809d 2019-08-08 stsp resp = getchar();
5494 33aa809d 2019-08-08 stsp if (resp == '\n')
5495 33aa809d 2019-08-08 stsp resp = getchar();
5496 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5497 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5498 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5499 33aa809d 2019-08-08 stsp resp = ' ';
5500 33aa809d 2019-08-08 stsp }
5501 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5502 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5503 33aa809d 2019-08-08 stsp resp = ' ';
5504 33aa809d 2019-08-08 stsp }
5505 33aa809d 2019-08-08 stsp }
5506 33aa809d 2019-08-08 stsp
5507 33aa809d 2019-08-08 stsp if (resp == 'y')
5508 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5509 33aa809d 2019-08-08 stsp else if (resp == 'n')
5510 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5511 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5512 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5513 33aa809d 2019-08-08 stsp
5514 1ee397ad 2019-07-12 stsp return NULL;
5515 a129376b 2019-03-28 stsp }
5516 a129376b 2019-03-28 stsp
5517 33aa809d 2019-08-08 stsp
5518 a129376b 2019-03-28 stsp static const struct got_error *
5519 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5520 a129376b 2019-03-28 stsp {
5521 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5522 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5523 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5524 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5525 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5526 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5527 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5528 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5529 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5530 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5531 a129376b 2019-03-28 stsp
5532 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5533 e20a8b6f 2019-06-04 stsp
5534 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5535 a129376b 2019-03-28 stsp switch (ch) {
5536 33aa809d 2019-08-08 stsp case 'p':
5537 33aa809d 2019-08-08 stsp pflag = 1;
5538 33aa809d 2019-08-08 stsp break;
5539 33aa809d 2019-08-08 stsp case 'F':
5540 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5541 33aa809d 2019-08-08 stsp break;
5542 0f6d7415 2019-08-08 stsp case 'R':
5543 0f6d7415 2019-08-08 stsp can_recurse = 1;
5544 0f6d7415 2019-08-08 stsp break;
5545 a129376b 2019-03-28 stsp default:
5546 a129376b 2019-03-28 stsp usage_revert();
5547 a129376b 2019-03-28 stsp /* NOTREACHED */
5548 a129376b 2019-03-28 stsp }
5549 a129376b 2019-03-28 stsp }
5550 a129376b 2019-03-28 stsp
5551 a129376b 2019-03-28 stsp argc -= optind;
5552 a129376b 2019-03-28 stsp argv += optind;
5553 a129376b 2019-03-28 stsp
5554 43012d58 2019-07-14 stsp #ifndef PROFILE
5555 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5556 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5557 43012d58 2019-07-14 stsp err(1, "pledge");
5558 43012d58 2019-07-14 stsp #endif
5559 e20a8b6f 2019-06-04 stsp if (argc < 1)
5560 a129376b 2019-03-28 stsp usage_revert();
5561 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5562 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5563 a129376b 2019-03-28 stsp
5564 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5565 a129376b 2019-03-28 stsp if (cwd == NULL) {
5566 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5567 a129376b 2019-03-28 stsp goto done;
5568 a129376b 2019-03-28 stsp }
5569 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5570 2ec1f75b 2019-03-26 stsp if (error)
5571 2ec1f75b 2019-03-26 stsp goto done;
5572 a129376b 2019-03-28 stsp
5573 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5574 c9956ddf 2019-09-08 stsp NULL);
5575 a129376b 2019-03-28 stsp if (error != NULL)
5576 a129376b 2019-03-28 stsp goto done;
5577 a129376b 2019-03-28 stsp
5578 33aa809d 2019-08-08 stsp if (patch_script_path) {
5579 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5580 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5581 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5582 33aa809d 2019-08-08 stsp patch_script_path);
5583 33aa809d 2019-08-08 stsp goto done;
5584 33aa809d 2019-08-08 stsp }
5585 33aa809d 2019-08-08 stsp }
5586 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5587 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5588 a129376b 2019-03-28 stsp if (error)
5589 a129376b 2019-03-28 stsp goto done;
5590 a129376b 2019-03-28 stsp
5591 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5592 6d022e97 2019-08-04 stsp if (error)
5593 6d022e97 2019-08-04 stsp goto done;
5594 00db391e 2019-08-03 stsp
5595 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5596 0f6d7415 2019-08-08 stsp char *ondisk_path;
5597 0f6d7415 2019-08-08 stsp struct stat sb;
5598 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5599 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5600 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5601 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5602 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5603 0f6d7415 2019-08-08 stsp goto done;
5604 0f6d7415 2019-08-08 stsp }
5605 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5606 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5607 0f6d7415 2019-08-08 stsp free(ondisk_path);
5608 0f6d7415 2019-08-08 stsp continue;
5609 0f6d7415 2019-08-08 stsp }
5610 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5611 0f6d7415 2019-08-08 stsp ondisk_path);
5612 0f6d7415 2019-08-08 stsp free(ondisk_path);
5613 0f6d7415 2019-08-08 stsp goto done;
5614 0f6d7415 2019-08-08 stsp }
5615 0f6d7415 2019-08-08 stsp free(ondisk_path);
5616 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5617 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5618 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5619 0f6d7415 2019-08-08 stsp goto done;
5620 0f6d7415 2019-08-08 stsp }
5621 0f6d7415 2019-08-08 stsp }
5622 0f6d7415 2019-08-08 stsp }
5623 0f6d7415 2019-08-08 stsp
5624 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5625 33aa809d 2019-08-08 stsp cpa.action = "revert";
5626 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5627 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5628 2ec1f75b 2019-03-26 stsp done:
5629 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5630 33aa809d 2019-08-08 stsp error == NULL)
5631 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5632 2ec1f75b 2019-03-26 stsp if (repo)
5633 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5634 2ec1f75b 2019-03-26 stsp if (worktree)
5635 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5636 2ec1f75b 2019-03-26 stsp free(path);
5637 d00136be 2019-03-26 stsp free(cwd);
5638 6bad629b 2019-02-04 stsp return error;
5639 c4296144 2019-05-09 stsp }
5640 c4296144 2019-05-09 stsp
5641 c4296144 2019-05-09 stsp __dead static void
5642 c4296144 2019-05-09 stsp usage_commit(void)
5643 c4296144 2019-05-09 stsp {
5644 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5645 5c1e53bc 2019-07-28 stsp getprogname());
5646 c4296144 2019-05-09 stsp exit(1);
5647 33ad4cbe 2019-05-12 jcs }
5648 33ad4cbe 2019-05-12 jcs
5649 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5650 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5651 e2ba3d07 2019-05-13 stsp const char *editor;
5652 e0870e44 2019-05-13 stsp const char *worktree_path;
5653 76d98825 2019-06-03 stsp const char *branch_name;
5654 314a6357 2019-05-13 stsp const char *repo_path;
5655 e0870e44 2019-05-13 stsp char *logmsg_path;
5656 e2ba3d07 2019-05-13 stsp
5657 e2ba3d07 2019-05-13 stsp };
5658 e0870e44 2019-05-13 stsp
5659 33ad4cbe 2019-05-12 jcs static const struct got_error *
5660 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5661 33ad4cbe 2019-05-12 jcs void *arg)
5662 33ad4cbe 2019-05-12 jcs {
5663 76d98825 2019-06-03 stsp char *initial_content = NULL;
5664 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5665 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5666 e0870e44 2019-05-13 stsp char *template = NULL;
5667 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5668 3ce1b845 2019-07-15 stsp int fd;
5669 33ad4cbe 2019-05-12 jcs size_t len;
5670 33ad4cbe 2019-05-12 jcs
5671 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5672 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5673 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5674 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5675 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5676 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5677 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5678 33ad4cbe 2019-05-12 jcs return NULL;
5679 33ad4cbe 2019-05-12 jcs }
5680 33ad4cbe 2019-05-12 jcs
5681 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5682 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5683 76d98825 2019-06-03 stsp
5684 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5685 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5686 76d98825 2019-06-03 stsp a->branch_name) == -1)
5687 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5688 e0870e44 2019-05-13 stsp
5689 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5690 793c30b5 2019-05-13 stsp if (err)
5691 e0870e44 2019-05-13 stsp goto done;
5692 33ad4cbe 2019-05-12 jcs
5693 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5694 33ad4cbe 2019-05-12 jcs
5695 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5696 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5697 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5698 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5699 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5700 33ad4cbe 2019-05-12 jcs }
5701 33ad4cbe 2019-05-12 jcs close(fd);
5702 33ad4cbe 2019-05-12 jcs
5703 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5704 33ad4cbe 2019-05-12 jcs done:
5705 76d98825 2019-06-03 stsp free(initial_content);
5706 e0870e44 2019-05-13 stsp free(template);
5707 314a6357 2019-05-13 stsp
5708 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5709 3ce1b845 2019-07-15 stsp if (err == NULL) {
5710 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5711 3ce1b845 2019-07-15 stsp if (err) {
5712 3ce1b845 2019-07-15 stsp free(*logmsg);
5713 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5714 3ce1b845 2019-07-15 stsp }
5715 3ce1b845 2019-07-15 stsp }
5716 33ad4cbe 2019-05-12 jcs return err;
5717 6bad629b 2019-02-04 stsp }
5718 c4296144 2019-05-09 stsp
5719 c4296144 2019-05-09 stsp static const struct got_error *
5720 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5721 c4296144 2019-05-09 stsp {
5722 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5723 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5724 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5725 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5726 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5727 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5728 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5729 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5730 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5731 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5732 5c1e53bc 2019-07-28 stsp
5733 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5734 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5735 c4296144 2019-05-09 stsp
5736 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5737 c4296144 2019-05-09 stsp switch (ch) {
5738 c4296144 2019-05-09 stsp case 'm':
5739 c4296144 2019-05-09 stsp logmsg = optarg;
5740 c4296144 2019-05-09 stsp break;
5741 c4296144 2019-05-09 stsp default:
5742 c4296144 2019-05-09 stsp usage_commit();
5743 c4296144 2019-05-09 stsp /* NOTREACHED */
5744 c4296144 2019-05-09 stsp }
5745 c4296144 2019-05-09 stsp }
5746 c4296144 2019-05-09 stsp
5747 c4296144 2019-05-09 stsp argc -= optind;
5748 c4296144 2019-05-09 stsp argv += optind;
5749 c4296144 2019-05-09 stsp
5750 43012d58 2019-07-14 stsp #ifndef PROFILE
5751 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5752 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5753 43012d58 2019-07-14 stsp err(1, "pledge");
5754 43012d58 2019-07-14 stsp #endif
5755 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5756 c4296144 2019-05-09 stsp if (cwd == NULL) {
5757 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5758 c4296144 2019-05-09 stsp goto done;
5759 c4296144 2019-05-09 stsp }
5760 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5761 7d5807f4 2019-07-11 stsp if (error)
5762 7d5807f4 2019-07-11 stsp goto done;
5763 7d5807f4 2019-07-11 stsp
5764 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5765 c4296144 2019-05-09 stsp if (error)
5766 a698f62e 2019-07-25 stsp goto done;
5767 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5768 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5769 c4296144 2019-05-09 stsp goto done;
5770 a698f62e 2019-07-25 stsp }
5771 5c1e53bc 2019-07-28 stsp
5772 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5773 916f288c 2019-07-30 stsp worktree);
5774 5c1e53bc 2019-07-28 stsp if (error)
5775 5c1e53bc 2019-07-28 stsp goto done;
5776 c4296144 2019-05-09 stsp
5777 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5778 c9956ddf 2019-09-08 stsp if (error)
5779 c9956ddf 2019-09-08 stsp goto done;
5780 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5781 c9956ddf 2019-09-08 stsp gitconfig_path);
5782 c4296144 2019-05-09 stsp if (error != NULL)
5783 c4296144 2019-05-09 stsp goto done;
5784 c4296144 2019-05-09 stsp
5785 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5786 aba9c984 2019-09-08 stsp if (error)
5787 aba9c984 2019-09-08 stsp return error;
5788 aba9c984 2019-09-08 stsp
5789 314a6357 2019-05-13 stsp /*
5790 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5791 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5792 314a6357 2019-05-13 stsp */
5793 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5794 314a6357 2019-05-13 stsp error = get_editor(&editor);
5795 314a6357 2019-05-13 stsp else
5796 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5797 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5798 c4296144 2019-05-09 stsp if (error)
5799 c4296144 2019-05-09 stsp goto done;
5800 c4296144 2019-05-09 stsp
5801 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5802 087fb88c 2019-08-04 stsp if (error)
5803 087fb88c 2019-08-04 stsp goto done;
5804 087fb88c 2019-08-04 stsp
5805 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5806 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5807 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5808 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5809 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5810 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5811 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5812 916f288c 2019-07-30 stsp goto done;
5813 916f288c 2019-07-30 stsp }
5814 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5815 916f288c 2019-07-30 stsp }
5816 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5817 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5818 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5819 e0870e44 2019-05-13 stsp if (error) {
5820 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5821 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5822 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5823 c4296144 2019-05-09 stsp goto done;
5824 e0870e44 2019-05-13 stsp }
5825 c4296144 2019-05-09 stsp
5826 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5827 c4296144 2019-05-09 stsp if (error)
5828 c4296144 2019-05-09 stsp goto done;
5829 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5830 c4296144 2019-05-09 stsp done:
5831 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5832 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5833 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5834 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5835 7266f21f 2019-10-21 stsp error == NULL)
5836 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5837 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5838 c4296144 2019-05-09 stsp if (repo)
5839 c4296144 2019-05-09 stsp got_repo_close(repo);
5840 c4296144 2019-05-09 stsp if (worktree)
5841 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5842 c4296144 2019-05-09 stsp free(cwd);
5843 c4296144 2019-05-09 stsp free(id_str);
5844 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5845 e2ba3d07 2019-05-13 stsp free(editor);
5846 aba9c984 2019-09-08 stsp free(author);
5847 234035bc 2019-06-01 stsp return error;
5848 234035bc 2019-06-01 stsp }
5849 234035bc 2019-06-01 stsp
5850 234035bc 2019-06-01 stsp __dead static void
5851 234035bc 2019-06-01 stsp usage_cherrypick(void)
5852 234035bc 2019-06-01 stsp {
5853 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5854 234035bc 2019-06-01 stsp exit(1);
5855 234035bc 2019-06-01 stsp }
5856 234035bc 2019-06-01 stsp
5857 234035bc 2019-06-01 stsp static const struct got_error *
5858 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5859 234035bc 2019-06-01 stsp {
5860 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5861 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5862 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5863 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5864 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5865 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5866 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5867 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5868 234035bc 2019-06-01 stsp int ch, did_something = 0;
5869 234035bc 2019-06-01 stsp
5870 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5871 234035bc 2019-06-01 stsp switch (ch) {
5872 234035bc 2019-06-01 stsp default:
5873 234035bc 2019-06-01 stsp usage_cherrypick();
5874 234035bc 2019-06-01 stsp /* NOTREACHED */
5875 234035bc 2019-06-01 stsp }
5876 234035bc 2019-06-01 stsp }
5877 234035bc 2019-06-01 stsp
5878 234035bc 2019-06-01 stsp argc -= optind;
5879 234035bc 2019-06-01 stsp argv += optind;
5880 234035bc 2019-06-01 stsp
5881 43012d58 2019-07-14 stsp #ifndef PROFILE
5882 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5883 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5884 43012d58 2019-07-14 stsp err(1, "pledge");
5885 43012d58 2019-07-14 stsp #endif
5886 234035bc 2019-06-01 stsp if (argc != 1)
5887 234035bc 2019-06-01 stsp usage_cherrypick();
5888 234035bc 2019-06-01 stsp
5889 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5890 234035bc 2019-06-01 stsp if (cwd == NULL) {
5891 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5892 234035bc 2019-06-01 stsp goto done;
5893 234035bc 2019-06-01 stsp }
5894 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5895 234035bc 2019-06-01 stsp if (error)
5896 234035bc 2019-06-01 stsp goto done;
5897 234035bc 2019-06-01 stsp
5898 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5899 c9956ddf 2019-09-08 stsp NULL);
5900 234035bc 2019-06-01 stsp if (error != NULL)
5901 234035bc 2019-06-01 stsp goto done;
5902 234035bc 2019-06-01 stsp
5903 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5904 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5905 234035bc 2019-06-01 stsp if (error)
5906 234035bc 2019-06-01 stsp goto done;
5907 234035bc 2019-06-01 stsp
5908 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5909 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5910 234035bc 2019-06-01 stsp if (error != NULL) {
5911 234035bc 2019-06-01 stsp struct got_reference *ref;
5912 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5913 234035bc 2019-06-01 stsp goto done;
5914 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5915 234035bc 2019-06-01 stsp if (error != NULL)
5916 234035bc 2019-06-01 stsp goto done;
5917 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5918 234035bc 2019-06-01 stsp got_ref_close(ref);
5919 234035bc 2019-06-01 stsp if (error != NULL)
5920 234035bc 2019-06-01 stsp goto done;
5921 234035bc 2019-06-01 stsp }
5922 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5923 234035bc 2019-06-01 stsp if (error)
5924 234035bc 2019-06-01 stsp goto done;
5925 234035bc 2019-06-01 stsp
5926 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5927 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5928 234035bc 2019-06-01 stsp if (error != NULL)
5929 234035bc 2019-06-01 stsp goto done;
5930 234035bc 2019-06-01 stsp
5931 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5932 234035bc 2019-06-01 stsp if (error) {
5933 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5934 234035bc 2019-06-01 stsp goto done;
5935 234035bc 2019-06-01 stsp error = NULL;
5936 234035bc 2019-06-01 stsp } else {
5937 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5938 234035bc 2019-06-01 stsp goto done;
5939 234035bc 2019-06-01 stsp }
5940 234035bc 2019-06-01 stsp
5941 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5942 234035bc 2019-06-01 stsp if (error)
5943 234035bc 2019-06-01 stsp goto done;
5944 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5945 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5946 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5947 03415a1a 2019-06-02 stsp NULL);
5948 234035bc 2019-06-01 stsp if (error != NULL)
5949 234035bc 2019-06-01 stsp goto done;
5950 234035bc 2019-06-01 stsp
5951 234035bc 2019-06-01 stsp if (did_something)
5952 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5953 234035bc 2019-06-01 stsp done:
5954 234035bc 2019-06-01 stsp if (commit)
5955 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5956 234035bc 2019-06-01 stsp free(commit_id_str);
5957 234035bc 2019-06-01 stsp if (head_ref)
5958 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5959 234035bc 2019-06-01 stsp if (worktree)
5960 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5961 234035bc 2019-06-01 stsp if (repo)
5962 234035bc 2019-06-01 stsp got_repo_close(repo);
5963 c4296144 2019-05-09 stsp return error;
5964 c4296144 2019-05-09 stsp }
5965 5ef14e63 2019-06-02 stsp
5966 5ef14e63 2019-06-02 stsp __dead static void
5967 5ef14e63 2019-06-02 stsp usage_backout(void)
5968 5ef14e63 2019-06-02 stsp {
5969 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5970 5ef14e63 2019-06-02 stsp exit(1);
5971 5ef14e63 2019-06-02 stsp }
5972 5ef14e63 2019-06-02 stsp
5973 5ef14e63 2019-06-02 stsp static const struct got_error *
5974 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5975 5ef14e63 2019-06-02 stsp {
5976 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5977 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5978 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5979 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5980 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5981 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5982 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5983 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5984 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5985 5ef14e63 2019-06-02 stsp
5986 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5987 5ef14e63 2019-06-02 stsp switch (ch) {
5988 5ef14e63 2019-06-02 stsp default:
5989 5ef14e63 2019-06-02 stsp usage_backout();
5990 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5991 5ef14e63 2019-06-02 stsp }
5992 5ef14e63 2019-06-02 stsp }
5993 5ef14e63 2019-06-02 stsp
5994 5ef14e63 2019-06-02 stsp argc -= optind;
5995 5ef14e63 2019-06-02 stsp argv += optind;
5996 5ef14e63 2019-06-02 stsp
5997 43012d58 2019-07-14 stsp #ifndef PROFILE
5998 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5999 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6000 43012d58 2019-07-14 stsp err(1, "pledge");
6001 43012d58 2019-07-14 stsp #endif
6002 5ef14e63 2019-06-02 stsp if (argc != 1)
6003 5ef14e63 2019-06-02 stsp usage_backout();
6004 5ef14e63 2019-06-02 stsp
6005 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6006 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6007 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6008 5ef14e63 2019-06-02 stsp goto done;
6009 5ef14e63 2019-06-02 stsp }
6010 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6011 5ef14e63 2019-06-02 stsp if (error)
6012 5ef14e63 2019-06-02 stsp goto done;
6013 5ef14e63 2019-06-02 stsp
6014 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6015 c9956ddf 2019-09-08 stsp NULL);
6016 5ef14e63 2019-06-02 stsp if (error != NULL)
6017 5ef14e63 2019-06-02 stsp goto done;
6018 5ef14e63 2019-06-02 stsp
6019 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6020 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6021 5ef14e63 2019-06-02 stsp if (error)
6022 5ef14e63 2019-06-02 stsp goto done;
6023 5ef14e63 2019-06-02 stsp
6024 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6025 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6026 5ef14e63 2019-06-02 stsp if (error != NULL) {
6027 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6028 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6029 5ef14e63 2019-06-02 stsp goto done;
6030 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6031 5ef14e63 2019-06-02 stsp if (error != NULL)
6032 5ef14e63 2019-06-02 stsp goto done;
6033 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6034 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6035 5ef14e63 2019-06-02 stsp if (error != NULL)
6036 5ef14e63 2019-06-02 stsp goto done;
6037 5ef14e63 2019-06-02 stsp }
6038 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6039 5ef14e63 2019-06-02 stsp if (error)
6040 5ef14e63 2019-06-02 stsp goto done;
6041 5ef14e63 2019-06-02 stsp
6042 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6043 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6044 5ef14e63 2019-06-02 stsp if (error != NULL)
6045 5ef14e63 2019-06-02 stsp goto done;
6046 5ef14e63 2019-06-02 stsp
6047 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6048 5ef14e63 2019-06-02 stsp if (error)
6049 5ef14e63 2019-06-02 stsp goto done;
6050 5ef14e63 2019-06-02 stsp
6051 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6052 5ef14e63 2019-06-02 stsp if (error)
6053 5ef14e63 2019-06-02 stsp goto done;
6054 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6055 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6056 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6057 5ef14e63 2019-06-02 stsp goto done;
6058 5ef14e63 2019-06-02 stsp }
6059 5ef14e63 2019-06-02 stsp
6060 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6061 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6062 5ef14e63 2019-06-02 stsp if (error != NULL)
6063 5ef14e63 2019-06-02 stsp goto done;
6064 5ef14e63 2019-06-02 stsp
6065 5ef14e63 2019-06-02 stsp if (did_something)
6066 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6067 5ef14e63 2019-06-02 stsp done:
6068 5ef14e63 2019-06-02 stsp if (commit)
6069 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6070 5ef14e63 2019-06-02 stsp free(commit_id_str);
6071 5ef14e63 2019-06-02 stsp if (head_ref)
6072 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6073 818c7501 2019-07-11 stsp if (worktree)
6074 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6075 818c7501 2019-07-11 stsp if (repo)
6076 818c7501 2019-07-11 stsp got_repo_close(repo);
6077 818c7501 2019-07-11 stsp return error;
6078 818c7501 2019-07-11 stsp }
6079 818c7501 2019-07-11 stsp
6080 818c7501 2019-07-11 stsp __dead static void
6081 818c7501 2019-07-11 stsp usage_rebase(void)
6082 818c7501 2019-07-11 stsp {
6083 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6084 af54c8f8 2019-07-11 stsp getprogname());
6085 818c7501 2019-07-11 stsp exit(1);
6086 0ebf8283 2019-07-24 stsp }
6087 0ebf8283 2019-07-24 stsp
6088 0ebf8283 2019-07-24 stsp void
6089 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6090 0ebf8283 2019-07-24 stsp {
6091 0ebf8283 2019-07-24 stsp char *nl;
6092 0ebf8283 2019-07-24 stsp size_t len;
6093 0ebf8283 2019-07-24 stsp
6094 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6095 0ebf8283 2019-07-24 stsp if (len > limit)
6096 0ebf8283 2019-07-24 stsp len = limit;
6097 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6098 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6099 0ebf8283 2019-07-24 stsp if (nl)
6100 0ebf8283 2019-07-24 stsp *nl = '\0';
6101 0ebf8283 2019-07-24 stsp }
6102 0ebf8283 2019-07-24 stsp
6103 0ebf8283 2019-07-24 stsp static const struct got_error *
6104 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6105 0ebf8283 2019-07-24 stsp {
6106 5943eee2 2019-08-13 stsp const struct got_error *err;
6107 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6108 5943eee2 2019-08-13 stsp const char *s;
6109 0ebf8283 2019-07-24 stsp
6110 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6111 5943eee2 2019-08-13 stsp if (err)
6112 5943eee2 2019-08-13 stsp return err;
6113 0ebf8283 2019-07-24 stsp
6114 5943eee2 2019-08-13 stsp s = logmsg0;
6115 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6116 5943eee2 2019-08-13 stsp s++;
6117 5943eee2 2019-08-13 stsp
6118 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6119 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6120 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6121 5943eee2 2019-08-13 stsp goto done;
6122 5943eee2 2019-08-13 stsp }
6123 0ebf8283 2019-07-24 stsp
6124 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6125 5943eee2 2019-08-13 stsp done:
6126 5943eee2 2019-08-13 stsp free(logmsg0);
6127 a0ea4fc0 2020-02-28 stsp return err;
6128 a0ea4fc0 2020-02-28 stsp }
6129 a0ea4fc0 2020-02-28 stsp
6130 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6131 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6132 a0ea4fc0 2020-02-28 stsp {
6133 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6134 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6135 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6136 a0ea4fc0 2020-02-28 stsp
6137 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6138 a0ea4fc0 2020-02-28 stsp if (err)
6139 a0ea4fc0 2020-02-28 stsp return err;
6140 a0ea4fc0 2020-02-28 stsp
6141 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6142 a0ea4fc0 2020-02-28 stsp if (err)
6143 a0ea4fc0 2020-02-28 stsp goto done;
6144 a0ea4fc0 2020-02-28 stsp
6145 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6146 a0ea4fc0 2020-02-28 stsp
6147 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6148 a0ea4fc0 2020-02-28 stsp if (err)
6149 a0ea4fc0 2020-02-28 stsp goto done;
6150 a0ea4fc0 2020-02-28 stsp
6151 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6152 a0ea4fc0 2020-02-28 stsp done:
6153 a0ea4fc0 2020-02-28 stsp free(id_str);
6154 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6155 a0ea4fc0 2020-02-28 stsp free(logmsg);
6156 5943eee2 2019-08-13 stsp return err;
6157 818c7501 2019-07-11 stsp }
6158 818c7501 2019-07-11 stsp
6159 818c7501 2019-07-11 stsp static const struct got_error *
6160 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6161 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6162 818c7501 2019-07-11 stsp {
6163 818c7501 2019-07-11 stsp const struct got_error *err;
6164 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6165 818c7501 2019-07-11 stsp
6166 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6167 818c7501 2019-07-11 stsp if (err)
6168 818c7501 2019-07-11 stsp goto done;
6169 818c7501 2019-07-11 stsp
6170 ff0d2220 2019-07-11 stsp if (new_id) {
6171 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6172 ff0d2220 2019-07-11 stsp if (err)
6173 ff0d2220 2019-07-11 stsp goto done;
6174 ff0d2220 2019-07-11 stsp }
6175 818c7501 2019-07-11 stsp
6176 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6177 ff0d2220 2019-07-11 stsp if (new_id_str)
6178 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6179 0ebf8283 2019-07-24 stsp
6180 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6181 0ebf8283 2019-07-24 stsp if (err)
6182 0ebf8283 2019-07-24 stsp goto done;
6183 0ebf8283 2019-07-24 stsp
6184 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6185 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6186 818c7501 2019-07-11 stsp done:
6187 818c7501 2019-07-11 stsp free(old_id_str);
6188 818c7501 2019-07-11 stsp free(new_id_str);
6189 272a1371 2020-02-28 stsp free(logmsg);
6190 818c7501 2019-07-11 stsp return err;
6191 818c7501 2019-07-11 stsp }
6192 818c7501 2019-07-11 stsp
6193 1ee397ad 2019-07-12 stsp static const struct got_error *
6194 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6195 818c7501 2019-07-11 stsp {
6196 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6197 818c7501 2019-07-11 stsp
6198 818c7501 2019-07-11 stsp while (path[0] == '/')
6199 818c7501 2019-07-11 stsp path++;
6200 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6201 818c7501 2019-07-11 stsp
6202 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6203 1ee397ad 2019-07-12 stsp return NULL;
6204 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6205 818c7501 2019-07-11 stsp *rebase_status = status;
6206 1ee397ad 2019-07-12 stsp return NULL;
6207 818c7501 2019-07-11 stsp }
6208 818c7501 2019-07-11 stsp
6209 818c7501 2019-07-11 stsp static const struct got_error *
6210 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6211 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6212 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6213 818c7501 2019-07-11 stsp {
6214 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6215 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6216 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6217 818c7501 2019-07-11 stsp }
6218 818c7501 2019-07-11 stsp
6219 818c7501 2019-07-11 stsp static const struct got_error *
6220 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6221 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6222 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6223 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6224 ff0d2220 2019-07-11 stsp {
6225 ff0d2220 2019-07-11 stsp const struct got_error *error;
6226 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6227 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6228 ff0d2220 2019-07-11 stsp
6229 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6230 ff0d2220 2019-07-11 stsp if (error)
6231 ff0d2220 2019-07-11 stsp return error;
6232 ff0d2220 2019-07-11 stsp
6233 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6234 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6235 ff0d2220 2019-07-11 stsp if (error) {
6236 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6237 ff0d2220 2019-07-11 stsp goto done;
6238 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6239 ff0d2220 2019-07-11 stsp } else {
6240 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6241 ff0d2220 2019-07-11 stsp free(new_commit_id);
6242 ff0d2220 2019-07-11 stsp }
6243 ff0d2220 2019-07-11 stsp done:
6244 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6245 ff0d2220 2019-07-11 stsp return error;
6246 64c6d990 2019-07-11 stsp }
6247 64c6d990 2019-07-11 stsp
6248 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6249 64c6d990 2019-07-11 stsp const char *path_prefix;
6250 64c6d990 2019-07-11 stsp size_t len;
6251 8ca9bd68 2019-07-25 stsp int errcode;
6252 64c6d990 2019-07-11 stsp };
6253 64c6d990 2019-07-11 stsp
6254 64c6d990 2019-07-11 stsp static const struct got_error *
6255 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6256 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6257 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6258 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6259 64c6d990 2019-07-11 stsp {
6260 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6261 64c6d990 2019-07-11 stsp
6262 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6263 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6264 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6265 64c6d990 2019-07-11 stsp
6266 64c6d990 2019-07-11 stsp return NULL;
6267 64c6d990 2019-07-11 stsp }
6268 64c6d990 2019-07-11 stsp
6269 64c6d990 2019-07-11 stsp static const struct got_error *
6270 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6271 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6272 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6273 64c6d990 2019-07-11 stsp {
6274 64c6d990 2019-07-11 stsp const struct got_error *err;
6275 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6276 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6277 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6278 64c6d990 2019-07-11 stsp
6279 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6280 64c6d990 2019-07-11 stsp return NULL;
6281 64c6d990 2019-07-11 stsp
6282 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6283 64c6d990 2019-07-11 stsp if (err)
6284 64c6d990 2019-07-11 stsp goto done;
6285 64c6d990 2019-07-11 stsp
6286 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6287 64c6d990 2019-07-11 stsp if (err)
6288 64c6d990 2019-07-11 stsp goto done;
6289 64c6d990 2019-07-11 stsp
6290 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6291 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6292 64c6d990 2019-07-11 stsp if (err)
6293 64c6d990 2019-07-11 stsp goto done;
6294 64c6d990 2019-07-11 stsp
6295 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6296 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6297 64c6d990 2019-07-11 stsp if (err)
6298 64c6d990 2019-07-11 stsp goto done;
6299 64c6d990 2019-07-11 stsp
6300 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6301 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6302 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6303 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6304 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6305 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6306 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6307 64c6d990 2019-07-11 stsp done:
6308 64c6d990 2019-07-11 stsp if (tree1)
6309 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6310 64c6d990 2019-07-11 stsp if (tree2)
6311 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6312 64c6d990 2019-07-11 stsp if (commit)
6313 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6314 64c6d990 2019-07-11 stsp if (parent_commit)
6315 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6316 64c6d990 2019-07-11 stsp return err;
6317 ff0d2220 2019-07-11 stsp }
6318 ff0d2220 2019-07-11 stsp
6319 ff0d2220 2019-07-11 stsp static const struct got_error *
6320 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6321 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6322 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6323 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6324 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6325 af61c510 2019-07-19 stsp {
6326 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6327 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6328 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6329 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6330 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6331 af61c510 2019-07-19 stsp
6332 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6333 af61c510 2019-07-19 stsp if (err)
6334 af61c510 2019-07-19 stsp return err;
6335 af61c510 2019-07-19 stsp
6336 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6337 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6338 af61c510 2019-07-19 stsp if (err)
6339 af61c510 2019-07-19 stsp goto done;
6340 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6341 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6342 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6343 af61c510 2019-07-19 stsp if (err) {
6344 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6345 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6346 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6347 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6348 af61c510 2019-07-19 stsp "been reached?!?");
6349 ee780d5c 2020-01-04 stsp }
6350 ee780d5c 2020-01-04 stsp goto done;
6351 af61c510 2019-07-19 stsp } else {
6352 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6353 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6354 af61c510 2019-07-19 stsp if (err)
6355 af61c510 2019-07-19 stsp goto done;
6356 af61c510 2019-07-19 stsp
6357 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6358 af61c510 2019-07-19 stsp if (err)
6359 af61c510 2019-07-19 stsp goto done;
6360 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6361 af61c510 2019-07-19 stsp commit_id = parent_id;
6362 af61c510 2019-07-19 stsp }
6363 af61c510 2019-07-19 stsp }
6364 af61c510 2019-07-19 stsp done:
6365 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6366 af61c510 2019-07-19 stsp return err;
6367 af61c510 2019-07-19 stsp }
6368 af61c510 2019-07-19 stsp
6369 af61c510 2019-07-19 stsp static const struct got_error *
6370 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6371 818c7501 2019-07-11 stsp {
6372 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6373 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6374 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6375 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6376 818c7501 2019-07-11 stsp char *cwd = NULL;
6377 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6378 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6379 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6380 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6381 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6382 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6383 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6384 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6385 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6386 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6387 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6388 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6389 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6390 818c7501 2019-07-11 stsp
6391 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6392 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6393 818c7501 2019-07-11 stsp
6394 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6395 818c7501 2019-07-11 stsp switch (ch) {
6396 818c7501 2019-07-11 stsp case 'a':
6397 818c7501 2019-07-11 stsp abort_rebase = 1;
6398 818c7501 2019-07-11 stsp break;
6399 818c7501 2019-07-11 stsp case 'c':
6400 818c7501 2019-07-11 stsp continue_rebase = 1;
6401 818c7501 2019-07-11 stsp break;
6402 818c7501 2019-07-11 stsp default:
6403 818c7501 2019-07-11 stsp usage_rebase();
6404 818c7501 2019-07-11 stsp /* NOTREACHED */
6405 818c7501 2019-07-11 stsp }
6406 818c7501 2019-07-11 stsp }
6407 818c7501 2019-07-11 stsp
6408 818c7501 2019-07-11 stsp argc -= optind;
6409 818c7501 2019-07-11 stsp argv += optind;
6410 818c7501 2019-07-11 stsp
6411 43012d58 2019-07-14 stsp #ifndef PROFILE
6412 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6413 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6414 43012d58 2019-07-14 stsp err(1, "pledge");
6415 43012d58 2019-07-14 stsp #endif
6416 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6417 818c7501 2019-07-11 stsp usage_rebase();
6418 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6419 818c7501 2019-07-11 stsp if (argc != 0)
6420 818c7501 2019-07-11 stsp usage_rebase();
6421 818c7501 2019-07-11 stsp } else if (argc != 1)
6422 818c7501 2019-07-11 stsp usage_rebase();
6423 818c7501 2019-07-11 stsp
6424 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6425 818c7501 2019-07-11 stsp if (cwd == NULL) {
6426 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6427 818c7501 2019-07-11 stsp goto done;
6428 818c7501 2019-07-11 stsp }
6429 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6430 818c7501 2019-07-11 stsp if (error)
6431 818c7501 2019-07-11 stsp goto done;
6432 818c7501 2019-07-11 stsp
6433 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6434 c9956ddf 2019-09-08 stsp NULL);
6435 818c7501 2019-07-11 stsp if (error != NULL)
6436 818c7501 2019-07-11 stsp goto done;
6437 818c7501 2019-07-11 stsp
6438 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6439 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6440 7ef62c4e 2020-02-24 stsp if (error)
6441 7ef62c4e 2020-02-24 stsp goto done;
6442 7ef62c4e 2020-02-24 stsp
6443 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6444 7ef62c4e 2020-02-24 stsp worktree);
6445 818c7501 2019-07-11 stsp if (error)
6446 7ef62c4e 2020-02-24 stsp goto done;
6447 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6448 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6449 818c7501 2019-07-11 stsp goto done;
6450 7ef62c4e 2020-02-24 stsp }
6451 818c7501 2019-07-11 stsp
6452 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6453 818c7501 2019-07-11 stsp if (error)
6454 818c7501 2019-07-11 stsp goto done;
6455 818c7501 2019-07-11 stsp
6456 f6794adc 2019-07-23 stsp if (abort_rebase) {
6457 818c7501 2019-07-11 stsp int did_something;
6458 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6459 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6460 f6794adc 2019-07-23 stsp goto done;
6461 f6794adc 2019-07-23 stsp }
6462 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6463 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6464 3e3a69f1 2019-07-25 stsp worktree, repo);
6465 818c7501 2019-07-11 stsp if (error)
6466 818c7501 2019-07-11 stsp goto done;
6467 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6468 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6469 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6470 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6471 818c7501 2019-07-11 stsp if (error)
6472 818c7501 2019-07-11 stsp goto done;
6473 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6474 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6475 818c7501 2019-07-11 stsp }
6476 818c7501 2019-07-11 stsp
6477 818c7501 2019-07-11 stsp if (continue_rebase) {
6478 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6479 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6480 f6794adc 2019-07-23 stsp goto done;
6481 f6794adc 2019-07-23 stsp }
6482 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6483 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6484 3e3a69f1 2019-07-25 stsp worktree, repo);
6485 818c7501 2019-07-11 stsp if (error)
6486 818c7501 2019-07-11 stsp goto done;
6487 818c7501 2019-07-11 stsp
6488 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6489 01757395 2019-07-12 stsp resume_commit_id, repo);
6490 818c7501 2019-07-11 stsp if (error)
6491 818c7501 2019-07-11 stsp goto done;
6492 818c7501 2019-07-11 stsp
6493 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6494 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6495 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6496 818c7501 2019-07-11 stsp goto done;
6497 818c7501 2019-07-11 stsp }
6498 818c7501 2019-07-11 stsp } else {
6499 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6500 818c7501 2019-07-11 stsp if (error != NULL)
6501 818c7501 2019-07-11 stsp goto done;
6502 ff0d2220 2019-07-11 stsp }
6503 818c7501 2019-07-11 stsp
6504 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6505 ff0d2220 2019-07-11 stsp if (error)
6506 ff0d2220 2019-07-11 stsp goto done;
6507 ff0d2220 2019-07-11 stsp
6508 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6509 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6510 a51a74b3 2019-07-27 stsp
6511 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6512 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6513 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6514 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6515 818c7501 2019-07-11 stsp if (error)
6516 818c7501 2019-07-11 stsp goto done;
6517 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6518 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6519 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6520 818c7501 2019-07-11 stsp "with work tree's branch");
6521 818c7501 2019-07-11 stsp goto done;
6522 818c7501 2019-07-11 stsp }
6523 818c7501 2019-07-11 stsp
6524 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6525 a51a74b3 2019-07-27 stsp if (error) {
6526 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6527 a51a74b3 2019-07-27 stsp goto done;
6528 a51a74b3 2019-07-27 stsp error = NULL;
6529 a51a74b3 2019-07-27 stsp } else {
6530 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6531 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6532 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6533 a51a74b3 2019-07-27 stsp goto done;
6534 a51a74b3 2019-07-27 stsp }
6535 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6536 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6537 818c7501 2019-07-11 stsp if (error)
6538 818c7501 2019-07-11 stsp goto done;
6539 818c7501 2019-07-11 stsp }
6540 818c7501 2019-07-11 stsp
6541 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6542 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6543 818c7501 2019-07-11 stsp if (error)
6544 818c7501 2019-07-11 stsp goto done;
6545 818c7501 2019-07-11 stsp
6546 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6547 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6548 fc66b545 2019-08-12 stsp if (pid == NULL) {
6549 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6550 fc66b545 2019-08-12 stsp int did_something;
6551 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6552 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6553 fc66b545 2019-08-12 stsp &did_something);
6554 fc66b545 2019-08-12 stsp if (error)
6555 fc66b545 2019-08-12 stsp goto done;
6556 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6557 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6558 fc66b545 2019-08-12 stsp }
6559 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6560 fc66b545 2019-08-12 stsp goto done;
6561 fc66b545 2019-08-12 stsp }
6562 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6563 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6564 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6565 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6566 818c7501 2019-07-11 stsp commit = NULL;
6567 818c7501 2019-07-11 stsp if (error)
6568 818c7501 2019-07-11 stsp goto done;
6569 64c6d990 2019-07-11 stsp
6570 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6571 38b0338b 2019-11-29 stsp if (continue_rebase) {
6572 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6573 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6574 38b0338b 2019-11-29 stsp goto done;
6575 38b0338b 2019-11-29 stsp } else {
6576 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6577 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6578 38b0338b 2019-11-29 stsp char *id_str;
6579 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6580 38b0338b 2019-11-29 stsp new_base_branch);
6581 38b0338b 2019-11-29 stsp if (error)
6582 38b0338b 2019-11-29 stsp goto done;
6583 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6584 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6585 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6586 38b0338b 2019-11-29 stsp free(id_str);
6587 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6588 38b0338b 2019-11-29 stsp new_head_commit_id);
6589 38b0338b 2019-11-29 stsp if (error)
6590 38b0338b 2019-11-29 stsp goto done;
6591 38b0338b 2019-11-29 stsp }
6592 818c7501 2019-07-11 stsp }
6593 818c7501 2019-07-11 stsp
6594 818c7501 2019-07-11 stsp pid = NULL;
6595 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6596 818c7501 2019-07-11 stsp commit_id = qid->id;
6597 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6598 818c7501 2019-07-11 stsp pid = qid;
6599 818c7501 2019-07-11 stsp
6600 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6601 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6602 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6603 818c7501 2019-07-11 stsp if (error)
6604 818c7501 2019-07-11 stsp goto done;
6605 818c7501 2019-07-11 stsp
6606 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6607 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6608 a0ea4fc0 2020-02-28 stsp if (error)
6609 a0ea4fc0 2020-02-28 stsp goto done;
6610 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6611 818c7501 2019-07-11 stsp break;
6612 01757395 2019-07-12 stsp }
6613 818c7501 2019-07-11 stsp
6614 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6615 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6616 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6617 818c7501 2019-07-11 stsp if (error)
6618 818c7501 2019-07-11 stsp goto done;
6619 818c7501 2019-07-11 stsp }
6620 818c7501 2019-07-11 stsp
6621 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6622 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6623 818c7501 2019-07-11 stsp if (error)
6624 818c7501 2019-07-11 stsp goto done;
6625 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6626 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6627 818c7501 2019-07-11 stsp } else
6628 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6629 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6630 818c7501 2019-07-11 stsp done:
6631 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6632 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6633 818c7501 2019-07-11 stsp free(resume_commit_id);
6634 818c7501 2019-07-11 stsp free(yca_id);
6635 818c7501 2019-07-11 stsp if (commit)
6636 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6637 818c7501 2019-07-11 stsp if (branch)
6638 818c7501 2019-07-11 stsp got_ref_close(branch);
6639 818c7501 2019-07-11 stsp if (new_base_branch)
6640 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6641 818c7501 2019-07-11 stsp if (tmp_branch)
6642 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6643 5ef14e63 2019-06-02 stsp if (worktree)
6644 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6645 5ef14e63 2019-06-02 stsp if (repo)
6646 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6647 5ef14e63 2019-06-02 stsp return error;
6648 0ebf8283 2019-07-24 stsp }
6649 0ebf8283 2019-07-24 stsp
6650 0ebf8283 2019-07-24 stsp __dead static void
6651 0ebf8283 2019-07-24 stsp usage_histedit(void)
6652 0ebf8283 2019-07-24 stsp {
6653 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6654 0ebf8283 2019-07-24 stsp getprogname());
6655 0ebf8283 2019-07-24 stsp exit(1);
6656 0ebf8283 2019-07-24 stsp }
6657 0ebf8283 2019-07-24 stsp
6658 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6659 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6660 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6661 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6662 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6663 0ebf8283 2019-07-24 stsp
6664 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6665 0ebf8283 2019-07-24 stsp unsigned char code;
6666 0ebf8283 2019-07-24 stsp const char *name;
6667 0ebf8283 2019-07-24 stsp const char *desc;
6668 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6669 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6670 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6671 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6672 82997472 2020-01-29 stsp "be used" },
6673 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6674 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6675 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6676 0ebf8283 2019-07-24 stsp };
6677 0ebf8283 2019-07-24 stsp
6678 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6679 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6680 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6681 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6682 0ebf8283 2019-07-24 stsp char *logmsg;
6683 0ebf8283 2019-07-24 stsp };
6684 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6685 0ebf8283 2019-07-24 stsp
6686 0ebf8283 2019-07-24 stsp static const struct got_error *
6687 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6688 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6689 0ebf8283 2019-07-24 stsp {
6690 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6691 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6692 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6693 8138f3e1 2019-08-11 stsp int n;
6694 0ebf8283 2019-07-24 stsp
6695 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6696 0ebf8283 2019-07-24 stsp if (err)
6697 0ebf8283 2019-07-24 stsp goto done;
6698 0ebf8283 2019-07-24 stsp
6699 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6700 0ebf8283 2019-07-24 stsp if (err)
6701 0ebf8283 2019-07-24 stsp goto done;
6702 0ebf8283 2019-07-24 stsp
6703 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6704 0ebf8283 2019-07-24 stsp if (err)
6705 0ebf8283 2019-07-24 stsp goto done;
6706 0ebf8283 2019-07-24 stsp
6707 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6708 0ebf8283 2019-07-24 stsp if (n < 0)
6709 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6710 0ebf8283 2019-07-24 stsp done:
6711 0ebf8283 2019-07-24 stsp if (commit)
6712 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6713 0ebf8283 2019-07-24 stsp free(id_str);
6714 0ebf8283 2019-07-24 stsp free(logmsg);
6715 0ebf8283 2019-07-24 stsp return err;
6716 0ebf8283 2019-07-24 stsp }
6717 0ebf8283 2019-07-24 stsp
6718 0ebf8283 2019-07-24 stsp static const struct got_error *
6719 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6720 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6721 0ebf8283 2019-07-24 stsp {
6722 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6723 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6724 0ebf8283 2019-07-24 stsp
6725 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6726 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6727 0ebf8283 2019-07-24 stsp
6728 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6729 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6730 0ebf8283 2019-07-24 stsp f, repo);
6731 0ebf8283 2019-07-24 stsp if (err)
6732 0ebf8283 2019-07-24 stsp break;
6733 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6734 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6735 083957f4 2020-02-24 stsp if (n < 0) {
6736 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6737 083957f4 2020-02-24 stsp break;
6738 083957f4 2020-02-24 stsp }
6739 083957f4 2020-02-24 stsp }
6740 0ebf8283 2019-07-24 stsp }
6741 0ebf8283 2019-07-24 stsp
6742 0ebf8283 2019-07-24 stsp return err;
6743 0ebf8283 2019-07-24 stsp }
6744 0ebf8283 2019-07-24 stsp
6745 0ebf8283 2019-07-24 stsp static const struct got_error *
6746 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6747 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6748 0ebf8283 2019-07-24 stsp {
6749 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6750 0ebf8283 2019-07-24 stsp int n, i;
6751 514f2ffe 2020-01-29 stsp char *id_str;
6752 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6753 514f2ffe 2020-01-29 stsp
6754 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6755 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6756 514f2ffe 2020-01-29 stsp if (err)
6757 514f2ffe 2020-01-29 stsp return err;
6758 514f2ffe 2020-01-29 stsp
6759 514f2ffe 2020-01-29 stsp n = fprintf(f,
6760 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6761 514f2ffe 2020-01-29 stsp "# commit %s\n"
6762 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6763 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6764 514f2ffe 2020-01-29 stsp if (n < 0) {
6765 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6766 514f2ffe 2020-01-29 stsp goto done;
6767 514f2ffe 2020-01-29 stsp }
6768 0ebf8283 2019-07-24 stsp
6769 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6770 514f2ffe 2020-01-29 stsp if (n < 0) {
6771 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6772 514f2ffe 2020-01-29 stsp goto done;
6773 514f2ffe 2020-01-29 stsp }
6774 0ebf8283 2019-07-24 stsp
6775 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6776 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6777 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6778 0ebf8283 2019-07-24 stsp cmd->desc);
6779 0ebf8283 2019-07-24 stsp if (n < 0) {
6780 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6781 0ebf8283 2019-07-24 stsp break;
6782 0ebf8283 2019-07-24 stsp }
6783 0ebf8283 2019-07-24 stsp }
6784 514f2ffe 2020-01-29 stsp done:
6785 514f2ffe 2020-01-29 stsp free(id_str);
6786 0ebf8283 2019-07-24 stsp return err;
6787 0ebf8283 2019-07-24 stsp }
6788 0ebf8283 2019-07-24 stsp
6789 0ebf8283 2019-07-24 stsp static const struct got_error *
6790 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6791 0ebf8283 2019-07-24 stsp {
6792 0ebf8283 2019-07-24 stsp static char msg[42];
6793 0ebf8283 2019-07-24 stsp int ret;
6794 0ebf8283 2019-07-24 stsp
6795 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6796 0ebf8283 2019-07-24 stsp lineno);
6797 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6798 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6799 0ebf8283 2019-07-24 stsp
6800 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6801 0ebf8283 2019-07-24 stsp }
6802 0ebf8283 2019-07-24 stsp
6803 0ebf8283 2019-07-24 stsp static const struct got_error *
6804 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6805 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6806 0ebf8283 2019-07-24 stsp {
6807 0ebf8283 2019-07-24 stsp const struct got_error *err;
6808 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6809 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6810 0ebf8283 2019-07-24 stsp
6811 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6812 0ebf8283 2019-07-24 stsp if (err)
6813 0ebf8283 2019-07-24 stsp return err;
6814 0ebf8283 2019-07-24 stsp
6815 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6816 0ebf8283 2019-07-24 stsp if (err)
6817 0ebf8283 2019-07-24 stsp goto done;
6818 0ebf8283 2019-07-24 stsp
6819 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6820 5943eee2 2019-08-13 stsp if (err)
6821 5943eee2 2019-08-13 stsp goto done;
6822 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6823 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6824 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6825 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6826 0ebf8283 2019-07-24 stsp }
6827 0ebf8283 2019-07-24 stsp done:
6828 0ebf8283 2019-07-24 stsp if (folded_commit)
6829 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6830 0ebf8283 2019-07-24 stsp free(id_str);
6831 5943eee2 2019-08-13 stsp free(folded_logmsg);
6832 0ebf8283 2019-07-24 stsp return err;
6833 0ebf8283 2019-07-24 stsp }
6834 0ebf8283 2019-07-24 stsp
6835 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6836 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6837 0ebf8283 2019-07-24 stsp {
6838 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6839 0ebf8283 2019-07-24 stsp
6840 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6841 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6842 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6843 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6844 3f9de99f 2019-07-24 stsp folded = prev;
6845 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6846 0ebf8283 2019-07-24 stsp }
6847 0ebf8283 2019-07-24 stsp
6848 0ebf8283 2019-07-24 stsp return folded;
6849 0ebf8283 2019-07-24 stsp }
6850 0ebf8283 2019-07-24 stsp
6851 0ebf8283 2019-07-24 stsp static const struct got_error *
6852 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6853 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6854 0ebf8283 2019-07-24 stsp {
6855 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6856 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6857 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6858 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6859 0ebf8283 2019-07-24 stsp int fd;
6860 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6863 0ebf8283 2019-07-24 stsp if (err)
6864 0ebf8283 2019-07-24 stsp return err;
6865 0ebf8283 2019-07-24 stsp
6866 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6867 0ebf8283 2019-07-24 stsp if (folded) {
6868 0ebf8283 2019-07-24 stsp while (folded != hle) {
6869 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6870 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6871 3f9de99f 2019-07-24 stsp continue;
6872 3f9de99f 2019-07-24 stsp }
6873 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6874 0ebf8283 2019-07-24 stsp logmsg, repo);
6875 0ebf8283 2019-07-24 stsp if (err)
6876 0ebf8283 2019-07-24 stsp goto done;
6877 0ebf8283 2019-07-24 stsp free(logmsg);
6878 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6879 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6880 0ebf8283 2019-07-24 stsp }
6881 0ebf8283 2019-07-24 stsp }
6882 0ebf8283 2019-07-24 stsp
6883 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6884 0ebf8283 2019-07-24 stsp if (err)
6885 0ebf8283 2019-07-24 stsp goto done;
6886 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6887 5943eee2 2019-08-13 stsp if (err)
6888 5943eee2 2019-08-13 stsp goto done;
6889 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6890 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6891 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6892 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6893 0ebf8283 2019-07-24 stsp goto done;
6894 0ebf8283 2019-07-24 stsp }
6895 0ebf8283 2019-07-24 stsp free(logmsg);
6896 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6897 0ebf8283 2019-07-24 stsp
6898 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6899 0ebf8283 2019-07-24 stsp if (err)
6900 0ebf8283 2019-07-24 stsp goto done;
6901 0ebf8283 2019-07-24 stsp
6902 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6903 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6904 0ebf8283 2019-07-24 stsp if (err)
6905 0ebf8283 2019-07-24 stsp goto done;
6906 0ebf8283 2019-07-24 stsp
6907 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6908 0ebf8283 2019-07-24 stsp close(fd);
6909 0ebf8283 2019-07-24 stsp
6910 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6911 0ebf8283 2019-07-24 stsp if (err)
6912 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6915 0ebf8283 2019-07-24 stsp if (err) {
6916 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6917 0ebf8283 2019-07-24 stsp goto done;
6918 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6919 0ebf8283 2019-07-24 stsp }
6920 0ebf8283 2019-07-24 stsp done:
6921 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6922 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6923 0ebf8283 2019-07-24 stsp free(logmsg_path);
6924 0ebf8283 2019-07-24 stsp free(logmsg);
6925 5943eee2 2019-08-13 stsp free(orig_logmsg);
6926 0ebf8283 2019-07-24 stsp free(editor);
6927 0ebf8283 2019-07-24 stsp if (commit)
6928 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6929 0ebf8283 2019-07-24 stsp return err;
6930 5ef14e63 2019-06-02 stsp }
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp static const struct got_error *
6933 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6934 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6935 0ebf8283 2019-07-24 stsp {
6936 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6937 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6938 0ebf8283 2019-07-24 stsp size_t size;
6939 0ebf8283 2019-07-24 stsp ssize_t len;
6940 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6941 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6942 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6943 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6944 0ebf8283 2019-07-24 stsp
6945 0ebf8283 2019-07-24 stsp for (;;) {
6946 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6947 0ebf8283 2019-07-24 stsp if (len == -1) {
6948 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6949 0ebf8283 2019-07-24 stsp if (feof(f))
6950 0ebf8283 2019-07-24 stsp break;
6951 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6952 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6953 0ebf8283 2019-07-24 stsp break;
6954 0ebf8283 2019-07-24 stsp }
6955 0ebf8283 2019-07-24 stsp lineno++;
6956 0ebf8283 2019-07-24 stsp p = line;
6957 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6958 0ebf8283 2019-07-24 stsp p++;
6959 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6960 0ebf8283 2019-07-24 stsp free(line);
6961 0ebf8283 2019-07-24 stsp line = NULL;
6962 0ebf8283 2019-07-24 stsp continue;
6963 0ebf8283 2019-07-24 stsp }
6964 0ebf8283 2019-07-24 stsp cmd = NULL;
6965 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6966 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6967 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6968 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6969 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6970 0ebf8283 2019-07-24 stsp break;
6971 0ebf8283 2019-07-24 stsp }
6972 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6973 0ebf8283 2019-07-24 stsp p++;
6974 0ebf8283 2019-07-24 stsp break;
6975 0ebf8283 2019-07-24 stsp }
6976 0ebf8283 2019-07-24 stsp }
6977 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6978 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6979 0ebf8283 2019-07-24 stsp break;
6980 0ebf8283 2019-07-24 stsp }
6981 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6982 0ebf8283 2019-07-24 stsp p++;
6983 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6984 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6985 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6986 0ebf8283 2019-07-24 stsp break;
6987 0ebf8283 2019-07-24 stsp }
6988 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6989 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6990 0ebf8283 2019-07-24 stsp if (err)
6991 0ebf8283 2019-07-24 stsp break;
6992 0ebf8283 2019-07-24 stsp } else {
6993 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6994 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6995 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6996 0ebf8283 2019-07-24 stsp break;
6997 0ebf8283 2019-07-24 stsp }
6998 0ebf8283 2019-07-24 stsp }
6999 0ebf8283 2019-07-24 stsp free(line);
7000 0ebf8283 2019-07-24 stsp line = NULL;
7001 0ebf8283 2019-07-24 stsp continue;
7002 0ebf8283 2019-07-24 stsp } else {
7003 0ebf8283 2019-07-24 stsp end = p;
7004 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7005 0ebf8283 2019-07-24 stsp end++;
7006 0ebf8283 2019-07-24 stsp *end = '\0';
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7009 0ebf8283 2019-07-24 stsp if (err) {
7010 0ebf8283 2019-07-24 stsp /* override error code */
7011 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7012 0ebf8283 2019-07-24 stsp break;
7013 0ebf8283 2019-07-24 stsp }
7014 0ebf8283 2019-07-24 stsp }
7015 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7016 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7017 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7018 0ebf8283 2019-07-24 stsp break;
7019 0ebf8283 2019-07-24 stsp }
7020 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7021 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7022 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7023 0ebf8283 2019-07-24 stsp commit_id = NULL;
7024 0ebf8283 2019-07-24 stsp free(line);
7025 0ebf8283 2019-07-24 stsp line = NULL;
7026 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7027 0ebf8283 2019-07-24 stsp }
7028 0ebf8283 2019-07-24 stsp
7029 0ebf8283 2019-07-24 stsp free(line);
7030 0ebf8283 2019-07-24 stsp free(commit_id);
7031 0ebf8283 2019-07-24 stsp return err;
7032 0ebf8283 2019-07-24 stsp }
7033 0ebf8283 2019-07-24 stsp
7034 0ebf8283 2019-07-24 stsp static const struct got_error *
7035 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7036 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7037 bfce7f83 2019-07-27 stsp {
7038 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7039 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7040 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7041 5b87815e 2020-03-05 stsp static char msg[92];
7042 bfce7f83 2019-07-27 stsp char *id_str;
7043 bfce7f83 2019-07-27 stsp
7044 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7045 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7046 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7047 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7048 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7049 5b87815e 2020-03-05 stsp
7050 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7051 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7052 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7053 5b87815e 2020-03-05 stsp if (hle == hle2)
7054 5b87815e 2020-03-05 stsp continue;
7055 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7056 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7057 5b87815e 2020-03-05 stsp continue;
7058 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7059 5b87815e 2020-03-05 stsp if (err)
7060 5b87815e 2020-03-05 stsp return err;
7061 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7062 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7063 5b87815e 2020-03-05 stsp free(id_str);
7064 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7065 5b87815e 2020-03-05 stsp }
7066 5b87815e 2020-03-05 stsp }
7067 bfce7f83 2019-07-27 stsp
7068 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7069 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7070 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7071 bfce7f83 2019-07-27 stsp break;
7072 bfce7f83 2019-07-27 stsp }
7073 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7074 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7075 bfce7f83 2019-07-27 stsp if (err)
7076 bfce7f83 2019-07-27 stsp return err;
7077 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7078 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7079 bfce7f83 2019-07-27 stsp free(id_str);
7080 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7081 bfce7f83 2019-07-27 stsp }
7082 bfce7f83 2019-07-27 stsp }
7083 bfce7f83 2019-07-27 stsp
7084 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7085 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7086 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7087 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7088 bfce7f83 2019-07-27 stsp
7089 bfce7f83 2019-07-27 stsp return NULL;
7090 bfce7f83 2019-07-27 stsp }
7091 bfce7f83 2019-07-27 stsp
7092 bfce7f83 2019-07-27 stsp static const struct got_error *
7093 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7094 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7095 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7096 0ebf8283 2019-07-24 stsp {
7097 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7098 0ebf8283 2019-07-24 stsp char *editor;
7099 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7100 0ebf8283 2019-07-24 stsp
7101 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7102 0ebf8283 2019-07-24 stsp if (err)
7103 0ebf8283 2019-07-24 stsp return err;
7104 0ebf8283 2019-07-24 stsp
7105 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7106 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7107 0ebf8283 2019-07-24 stsp goto done;
7108 0ebf8283 2019-07-24 stsp }
7109 0ebf8283 2019-07-24 stsp
7110 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7111 0ebf8283 2019-07-24 stsp if (f == NULL) {
7112 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7113 0ebf8283 2019-07-24 stsp goto done;
7114 0ebf8283 2019-07-24 stsp }
7115 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7116 bfce7f83 2019-07-27 stsp if (err)
7117 bfce7f83 2019-07-27 stsp goto done;
7118 bfce7f83 2019-07-27 stsp
7119 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7120 0ebf8283 2019-07-24 stsp done:
7121 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7122 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7123 0ebf8283 2019-07-24 stsp free(editor);
7124 0ebf8283 2019-07-24 stsp return err;
7125 0ebf8283 2019-07-24 stsp }
7126 0ebf8283 2019-07-24 stsp
7127 0ebf8283 2019-07-24 stsp static const struct got_error *
7128 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7129 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7130 514f2ffe 2020-01-29 stsp struct got_repository *);
7131 0ebf8283 2019-07-24 stsp
7132 0ebf8283 2019-07-24 stsp static const struct got_error *
7133 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7134 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7135 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7136 0ebf8283 2019-07-24 stsp {
7137 0ebf8283 2019-07-24 stsp const struct got_error *err;
7138 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7139 0ebf8283 2019-07-24 stsp char *path = NULL;
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp return err;
7144 0ebf8283 2019-07-24 stsp
7145 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7146 0ebf8283 2019-07-24 stsp if (err)
7147 0ebf8283 2019-07-24 stsp goto done;
7148 0ebf8283 2019-07-24 stsp
7149 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7150 0ebf8283 2019-07-24 stsp if (err)
7151 0ebf8283 2019-07-24 stsp goto done;
7152 0ebf8283 2019-07-24 stsp
7153 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7154 083957f4 2020-02-24 stsp rewind(f);
7155 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7156 083957f4 2020-02-24 stsp } else {
7157 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7158 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7159 0ebf8283 2019-07-24 stsp goto done;
7160 083957f4 2020-02-24 stsp }
7161 083957f4 2020-02-24 stsp f = NULL;
7162 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7163 083957f4 2020-02-24 stsp if (err) {
7164 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7165 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7166 083957f4 2020-02-24 stsp goto done;
7167 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7168 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7169 083957f4 2020-02-24 stsp }
7170 0ebf8283 2019-07-24 stsp }
7171 0ebf8283 2019-07-24 stsp done:
7172 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7173 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7174 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7175 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7176 0ebf8283 2019-07-24 stsp free(path);
7177 0ebf8283 2019-07-24 stsp return err;
7178 0ebf8283 2019-07-24 stsp }
7179 0ebf8283 2019-07-24 stsp
7180 0ebf8283 2019-07-24 stsp static const struct got_error *
7181 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7182 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7183 0ebf8283 2019-07-24 stsp {
7184 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7185 0ebf8283 2019-07-24 stsp char *path = NULL;
7186 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7187 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7188 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7189 0ebf8283 2019-07-24 stsp
7190 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7191 0ebf8283 2019-07-24 stsp if (err)
7192 0ebf8283 2019-07-24 stsp return err;
7193 0ebf8283 2019-07-24 stsp
7194 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7195 0ebf8283 2019-07-24 stsp if (f == NULL) {
7196 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7197 0ebf8283 2019-07-24 stsp goto done;
7198 0ebf8283 2019-07-24 stsp }
7199 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7200 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7201 0ebf8283 2019-07-24 stsp repo);
7202 0ebf8283 2019-07-24 stsp if (err)
7203 0ebf8283 2019-07-24 stsp break;
7204 0ebf8283 2019-07-24 stsp
7205 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7206 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7207 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7208 0ebf8283 2019-07-24 stsp if (n < 0) {
7209 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7210 0ebf8283 2019-07-24 stsp break;
7211 0ebf8283 2019-07-24 stsp }
7212 0ebf8283 2019-07-24 stsp }
7213 0ebf8283 2019-07-24 stsp }
7214 0ebf8283 2019-07-24 stsp done:
7215 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7216 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7217 0ebf8283 2019-07-24 stsp free(path);
7218 0ebf8283 2019-07-24 stsp if (commit)
7219 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7220 0ebf8283 2019-07-24 stsp return err;
7221 0ebf8283 2019-07-24 stsp }
7222 0ebf8283 2019-07-24 stsp
7223 bfce7f83 2019-07-27 stsp void
7224 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7225 bfce7f83 2019-07-27 stsp {
7226 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7227 bfce7f83 2019-07-27 stsp
7228 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7229 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7230 bfce7f83 2019-07-27 stsp free(hle);
7231 bfce7f83 2019-07-27 stsp }
7232 bfce7f83 2019-07-27 stsp }
7233 bfce7f83 2019-07-27 stsp
7234 0ebf8283 2019-07-24 stsp static const struct got_error *
7235 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7236 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7237 0ebf8283 2019-07-24 stsp {
7238 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7239 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7240 0ebf8283 2019-07-24 stsp
7241 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7242 0ebf8283 2019-07-24 stsp if (f == NULL) {
7243 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7244 0ebf8283 2019-07-24 stsp goto done;
7245 0ebf8283 2019-07-24 stsp }
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7248 0ebf8283 2019-07-24 stsp done:
7249 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7250 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7251 0ebf8283 2019-07-24 stsp return err;
7252 0ebf8283 2019-07-24 stsp }
7253 0ebf8283 2019-07-24 stsp
7254 0ebf8283 2019-07-24 stsp static const struct got_error *
7255 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7256 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7257 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7258 0ebf8283 2019-07-24 stsp {
7259 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7260 0ebf8283 2019-07-24 stsp int resp = ' ';
7261 0ebf8283 2019-07-24 stsp
7262 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7263 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7264 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7265 0ebf8283 2019-07-24 stsp resp = getchar();
7266 a61a4414 2019-08-07 stsp if (resp == '\n')
7267 a61a4414 2019-08-07 stsp resp = getchar();
7268 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7269 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7270 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7271 bfce7f83 2019-07-27 stsp repo);
7272 426ebf2e 2019-07-25 stsp if (err) {
7273 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7274 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7275 426ebf2e 2019-07-25 stsp break;
7276 bfce7f83 2019-07-27 stsp prev_err = err;
7277 426ebf2e 2019-07-25 stsp resp = ' ';
7278 426ebf2e 2019-07-25 stsp continue;
7279 426ebf2e 2019-07-25 stsp }
7280 bfce7f83 2019-07-27 stsp break;
7281 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7282 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7283 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7284 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7285 426ebf2e 2019-07-25 stsp if (err) {
7286 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7287 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7288 426ebf2e 2019-07-25 stsp break;
7289 bfce7f83 2019-07-27 stsp prev_err = err;
7290 426ebf2e 2019-07-25 stsp resp = ' ';
7291 426ebf2e 2019-07-25 stsp continue;
7292 426ebf2e 2019-07-25 stsp }
7293 bfce7f83 2019-07-27 stsp break;
7294 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7295 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7296 0ebf8283 2019-07-24 stsp break;
7297 426ebf2e 2019-07-25 stsp } else
7298 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7299 0ebf8283 2019-07-24 stsp }
7300 0ebf8283 2019-07-24 stsp
7301 0ebf8283 2019-07-24 stsp return err;
7302 0ebf8283 2019-07-24 stsp }
7303 0ebf8283 2019-07-24 stsp
7304 0ebf8283 2019-07-24 stsp static const struct got_error *
7305 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7306 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7307 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7308 0ebf8283 2019-07-24 stsp {
7309 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7310 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7311 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7312 3e3a69f1 2019-07-25 stsp branch, repo);
7313 0ebf8283 2019-07-24 stsp }
7314 0ebf8283 2019-07-24 stsp
7315 0ebf8283 2019-07-24 stsp static const struct got_error *
7316 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7317 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7318 0ebf8283 2019-07-24 stsp {
7319 0ebf8283 2019-07-24 stsp const struct got_error *err;
7320 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7321 0ebf8283 2019-07-24 stsp
7322 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7323 0ebf8283 2019-07-24 stsp if (err)
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp
7326 0ebf8283 2019-07-24 stsp if (new_id) {
7327 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7328 0ebf8283 2019-07-24 stsp if (err)
7329 0ebf8283 2019-07-24 stsp goto done;
7330 0ebf8283 2019-07-24 stsp }
7331 0ebf8283 2019-07-24 stsp
7332 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7333 0ebf8283 2019-07-24 stsp if (new_id_str)
7334 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7335 0ebf8283 2019-07-24 stsp
7336 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7337 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7338 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7339 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7340 0ebf8283 2019-07-24 stsp goto done;
7341 0ebf8283 2019-07-24 stsp }
7342 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7343 0ebf8283 2019-07-24 stsp } else {
7344 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7345 0ebf8283 2019-07-24 stsp if (err)
7346 0ebf8283 2019-07-24 stsp goto done;
7347 0ebf8283 2019-07-24 stsp }
7348 0ebf8283 2019-07-24 stsp
7349 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7350 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7351 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7352 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7353 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7354 0ebf8283 2019-07-24 stsp break;
7355 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7356 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7357 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7358 0ebf8283 2019-07-24 stsp logmsg);
7359 0ebf8283 2019-07-24 stsp break;
7360 0ebf8283 2019-07-24 stsp default:
7361 0ebf8283 2019-07-24 stsp break;
7362 0ebf8283 2019-07-24 stsp }
7363 0ebf8283 2019-07-24 stsp done:
7364 0ebf8283 2019-07-24 stsp free(old_id_str);
7365 0ebf8283 2019-07-24 stsp free(new_id_str);
7366 0ebf8283 2019-07-24 stsp return err;
7367 0ebf8283 2019-07-24 stsp }
7368 0ebf8283 2019-07-24 stsp
7369 0ebf8283 2019-07-24 stsp static const struct got_error *
7370 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7371 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7372 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7373 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7374 0ebf8283 2019-07-24 stsp {
7375 0ebf8283 2019-07-24 stsp const struct got_error *err;
7376 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7377 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7378 0ebf8283 2019-07-24 stsp
7379 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7380 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7381 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7382 0ebf8283 2019-07-24 stsp if (err)
7383 0ebf8283 2019-07-24 stsp return err;
7384 0ebf8283 2019-07-24 stsp }
7385 0ebf8283 2019-07-24 stsp
7386 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7387 0ebf8283 2019-07-24 stsp if (err)
7388 0ebf8283 2019-07-24 stsp return err;
7389 0ebf8283 2019-07-24 stsp
7390 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7391 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7392 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7393 0ebf8283 2019-07-24 stsp if (err) {
7394 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7395 0ebf8283 2019-07-24 stsp goto done;
7396 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7397 0ebf8283 2019-07-24 stsp } else {
7398 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7399 0ebf8283 2019-07-24 stsp free(new_commit_id);
7400 0ebf8283 2019-07-24 stsp }
7401 0ebf8283 2019-07-24 stsp done:
7402 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7403 0ebf8283 2019-07-24 stsp return err;
7404 0ebf8283 2019-07-24 stsp }
7405 0ebf8283 2019-07-24 stsp
7406 0ebf8283 2019-07-24 stsp static const struct got_error *
7407 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7408 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7409 0ebf8283 2019-07-24 stsp {
7410 0ebf8283 2019-07-24 stsp const struct got_error *error;
7411 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7412 0ebf8283 2019-07-24 stsp
7413 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7414 0ebf8283 2019-07-24 stsp repo);
7415 0ebf8283 2019-07-24 stsp if (error)
7416 0ebf8283 2019-07-24 stsp return error;
7417 0ebf8283 2019-07-24 stsp
7418 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7419 0ebf8283 2019-07-24 stsp if (error)
7420 0ebf8283 2019-07-24 stsp return error;
7421 0ebf8283 2019-07-24 stsp
7422 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7423 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7424 0ebf8283 2019-07-24 stsp return error;
7425 ab20a43a 2020-01-29 stsp }
7426 ab20a43a 2020-01-29 stsp
7427 ab20a43a 2020-01-29 stsp static const struct got_error *
7428 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7429 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7430 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7431 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7432 ab20a43a 2020-01-29 stsp {
7433 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7434 ab20a43a 2020-01-29 stsp
7435 ab20a43a 2020-01-29 stsp switch (status) {
7436 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7437 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7438 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7439 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7440 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7441 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7442 ab20a43a 2020-01-29 stsp default:
7443 ab20a43a 2020-01-29 stsp break;
7444 ab20a43a 2020-01-29 stsp }
7445 ab20a43a 2020-01-29 stsp
7446 ab20a43a 2020-01-29 stsp switch (staged_status) {
7447 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7448 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7449 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7450 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7451 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7452 ab20a43a 2020-01-29 stsp default:
7453 ab20a43a 2020-01-29 stsp break;
7454 ab20a43a 2020-01-29 stsp }
7455 ab20a43a 2020-01-29 stsp
7456 ab20a43a 2020-01-29 stsp return NULL;
7457 0ebf8283 2019-07-24 stsp }
7458 0ebf8283 2019-07-24 stsp
7459 0ebf8283 2019-07-24 stsp static const struct got_error *
7460 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7461 0ebf8283 2019-07-24 stsp {
7462 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7463 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7464 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7465 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7466 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7467 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7468 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7469 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7470 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7471 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7472 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7473 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7474 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7475 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7476 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7477 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7478 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7479 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7480 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7481 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7482 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7483 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7484 0ebf8283 2019-07-24 stsp
7485 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7486 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7487 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7488 0ebf8283 2019-07-24 stsp
7489 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7490 0ebf8283 2019-07-24 stsp switch (ch) {
7491 0ebf8283 2019-07-24 stsp case 'a':
7492 0ebf8283 2019-07-24 stsp abort_edit = 1;
7493 0ebf8283 2019-07-24 stsp break;
7494 0ebf8283 2019-07-24 stsp case 'c':
7495 0ebf8283 2019-07-24 stsp continue_edit = 1;
7496 0ebf8283 2019-07-24 stsp break;
7497 0ebf8283 2019-07-24 stsp case 'F':
7498 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7499 0ebf8283 2019-07-24 stsp break;
7500 083957f4 2020-02-24 stsp case 'm':
7501 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7502 083957f4 2020-02-24 stsp break;
7503 0ebf8283 2019-07-24 stsp default:
7504 0ebf8283 2019-07-24 stsp usage_histedit();
7505 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7506 0ebf8283 2019-07-24 stsp }
7507 0ebf8283 2019-07-24 stsp }
7508 0ebf8283 2019-07-24 stsp
7509 0ebf8283 2019-07-24 stsp argc -= optind;
7510 0ebf8283 2019-07-24 stsp argv += optind;
7511 0ebf8283 2019-07-24 stsp
7512 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7513 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7514 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7515 0ebf8283 2019-07-24 stsp err(1, "pledge");
7516 0ebf8283 2019-07-24 stsp #endif
7517 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7518 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7519 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7520 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7521 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7522 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7523 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7524 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7525 0ebf8283 2019-07-24 stsp if (argc != 0)
7526 0ebf8283 2019-07-24 stsp usage_histedit();
7527 0ebf8283 2019-07-24 stsp
7528 0ebf8283 2019-07-24 stsp /*
7529 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7530 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7531 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7532 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7533 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7534 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7535 0ebf8283 2019-07-24 stsp */
7536 0ebf8283 2019-07-24 stsp
7537 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7538 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7539 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7540 0ebf8283 2019-07-24 stsp goto done;
7541 0ebf8283 2019-07-24 stsp }
7542 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7543 0ebf8283 2019-07-24 stsp if (error)
7544 0ebf8283 2019-07-24 stsp goto done;
7545 0ebf8283 2019-07-24 stsp
7546 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7547 c9956ddf 2019-09-08 stsp NULL);
7548 0ebf8283 2019-07-24 stsp if (error != NULL)
7549 0ebf8283 2019-07-24 stsp goto done;
7550 0ebf8283 2019-07-24 stsp
7551 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7552 0ebf8283 2019-07-24 stsp if (error)
7553 0ebf8283 2019-07-24 stsp goto done;
7554 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7555 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7556 0ebf8283 2019-07-24 stsp goto done;
7557 0ebf8283 2019-07-24 stsp }
7558 0ebf8283 2019-07-24 stsp
7559 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7560 0ebf8283 2019-07-24 stsp if (error)
7561 0ebf8283 2019-07-24 stsp goto done;
7562 0ebf8283 2019-07-24 stsp
7563 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7564 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7565 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7566 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7567 083957f4 2020-02-24 stsp "before the -m option can be used");
7568 083957f4 2020-02-24 stsp goto done;
7569 083957f4 2020-02-24 stsp }
7570 083957f4 2020-02-24 stsp
7571 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7572 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7573 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7574 3e3a69f1 2019-07-25 stsp worktree, repo);
7575 0ebf8283 2019-07-24 stsp if (error)
7576 0ebf8283 2019-07-24 stsp goto done;
7577 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7578 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7579 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7580 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7581 0ebf8283 2019-07-24 stsp if (error)
7582 0ebf8283 2019-07-24 stsp goto done;
7583 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7584 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7585 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7586 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7587 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7588 0ebf8283 2019-07-24 stsp goto done;
7589 0ebf8283 2019-07-24 stsp }
7590 0ebf8283 2019-07-24 stsp
7591 0ebf8283 2019-07-24 stsp if (continue_edit) {
7592 0ebf8283 2019-07-24 stsp char *path;
7593 0ebf8283 2019-07-24 stsp
7594 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7595 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7596 0ebf8283 2019-07-24 stsp goto done;
7597 0ebf8283 2019-07-24 stsp }
7598 0ebf8283 2019-07-24 stsp
7599 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7600 0ebf8283 2019-07-24 stsp if (error)
7601 0ebf8283 2019-07-24 stsp goto done;
7602 0ebf8283 2019-07-24 stsp
7603 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7604 0ebf8283 2019-07-24 stsp free(path);
7605 0ebf8283 2019-07-24 stsp if (error)
7606 0ebf8283 2019-07-24 stsp goto done;
7607 0ebf8283 2019-07-24 stsp
7608 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7609 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7610 3e3a69f1 2019-07-25 stsp worktree, repo);
7611 0ebf8283 2019-07-24 stsp if (error)
7612 0ebf8283 2019-07-24 stsp goto done;
7613 0ebf8283 2019-07-24 stsp
7614 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7615 0ebf8283 2019-07-24 stsp if (error)
7616 0ebf8283 2019-07-24 stsp goto done;
7617 0ebf8283 2019-07-24 stsp
7618 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7619 0ebf8283 2019-07-24 stsp head_commit_id);
7620 0ebf8283 2019-07-24 stsp if (error)
7621 0ebf8283 2019-07-24 stsp goto done;
7622 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7623 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7624 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7625 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7626 3aac7cf7 2019-07-25 stsp goto done;
7627 3aac7cf7 2019-07-25 stsp }
7628 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7629 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7630 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7631 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7632 0ebf8283 2019-07-24 stsp commit = NULL;
7633 0ebf8283 2019-07-24 stsp if (error)
7634 0ebf8283 2019-07-24 stsp goto done;
7635 0ebf8283 2019-07-24 stsp } else {
7636 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7637 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7638 0ebf8283 2019-07-24 stsp goto done;
7639 0ebf8283 2019-07-24 stsp }
7640 0ebf8283 2019-07-24 stsp
7641 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7642 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7643 0ebf8283 2019-07-24 stsp if (error != NULL)
7644 0ebf8283 2019-07-24 stsp goto done;
7645 0ebf8283 2019-07-24 stsp
7646 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7647 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7648 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7649 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7650 c7d20a3f 2019-07-30 stsp goto done;
7651 c7d20a3f 2019-07-30 stsp }
7652 c7d20a3f 2019-07-30 stsp
7653 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7654 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7655 bfce7f83 2019-07-27 stsp branch = NULL;
7656 0ebf8283 2019-07-24 stsp if (error)
7657 0ebf8283 2019-07-24 stsp goto done;
7658 0ebf8283 2019-07-24 stsp
7659 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7660 0ebf8283 2019-07-24 stsp head_commit_id);
7661 0ebf8283 2019-07-24 stsp if (error)
7662 0ebf8283 2019-07-24 stsp goto done;
7663 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7664 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7665 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7666 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7667 3aac7cf7 2019-07-25 stsp goto done;
7668 3aac7cf7 2019-07-25 stsp }
7669 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7670 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7671 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7672 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7673 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7674 0ebf8283 2019-07-24 stsp commit = NULL;
7675 0ebf8283 2019-07-24 stsp if (error)
7676 0ebf8283 2019-07-24 stsp goto done;
7677 0ebf8283 2019-07-24 stsp
7678 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7679 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7680 c5996fff 2020-01-29 stsp goto done;
7681 c5996fff 2020-01-29 stsp }
7682 c5996fff 2020-01-29 stsp
7683 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7684 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7685 bfce7f83 2019-07-27 stsp if (error)
7686 bfce7f83 2019-07-27 stsp goto done;
7687 bfce7f83 2019-07-27 stsp
7688 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7689 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7690 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7691 6ba2bea2 2019-07-27 stsp if (error) {
7692 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7693 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7694 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7695 0ebf8283 2019-07-24 stsp goto done;
7696 6ba2bea2 2019-07-27 stsp }
7697 0ebf8283 2019-07-24 stsp } else {
7698 514f2ffe 2020-01-29 stsp const char *branch_name;
7699 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7700 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7701 514f2ffe 2020-01-29 stsp branch_name += 11;
7702 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7703 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7704 6ba2bea2 2019-07-27 stsp if (error) {
7705 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7706 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7707 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7708 0ebf8283 2019-07-24 stsp goto done;
7709 6ba2bea2 2019-07-27 stsp }
7710 0ebf8283 2019-07-24 stsp
7711 0ebf8283 2019-07-24 stsp }
7712 0ebf8283 2019-07-24 stsp
7713 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7714 0ebf8283 2019-07-24 stsp repo);
7715 6ba2bea2 2019-07-27 stsp if (error) {
7716 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7717 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7718 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7719 0ebf8283 2019-07-24 stsp goto done;
7720 6ba2bea2 2019-07-27 stsp }
7721 0ebf8283 2019-07-24 stsp
7722 0ebf8283 2019-07-24 stsp }
7723 0ebf8283 2019-07-24 stsp
7724 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7725 4ec14e60 2019-08-28 hiltjo if (error)
7726 0ebf8283 2019-07-24 stsp goto done;
7727 0ebf8283 2019-07-24 stsp
7728 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7729 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7730 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7731 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7732 0ebf8283 2019-07-24 stsp continue;
7733 0ebf8283 2019-07-24 stsp
7734 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7735 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7736 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7737 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7738 0ebf8283 2019-07-24 stsp repo);
7739 ab20a43a 2020-01-29 stsp if (error)
7740 ab20a43a 2020-01-29 stsp goto done;
7741 0ebf8283 2019-07-24 stsp } else {
7742 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7743 ab20a43a 2020-01-29 stsp int have_changes = 0;
7744 ab20a43a 2020-01-29 stsp
7745 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7746 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7747 ab20a43a 2020-01-29 stsp if (error)
7748 ab20a43a 2020-01-29 stsp goto done;
7749 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7750 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7751 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7752 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7753 ab20a43a 2020-01-29 stsp if (error) {
7754 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7755 ab20a43a 2020-01-29 stsp goto done;
7756 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7757 ab20a43a 2020-01-29 stsp goto done;
7758 ab20a43a 2020-01-29 stsp }
7759 ab20a43a 2020-01-29 stsp if (have_changes) {
7760 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7761 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7762 ab20a43a 2020-01-29 stsp if (error)
7763 ab20a43a 2020-01-29 stsp goto done;
7764 ab20a43a 2020-01-29 stsp } else {
7765 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7766 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7767 ab20a43a 2020-01-29 stsp if (error)
7768 ab20a43a 2020-01-29 stsp goto done;
7769 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7770 ab20a43a 2020-01-29 stsp hle, NULL);
7771 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7772 ab20a43a 2020-01-29 stsp commit = NULL;
7773 ab20a43a 2020-01-29 stsp if (error)
7774 ab20a43a 2020-01-29 stsp goto done;
7775 ab20a43a 2020-01-29 stsp }
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp continue;
7778 0ebf8283 2019-07-24 stsp }
7779 0ebf8283 2019-07-24 stsp
7780 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7781 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7782 0ebf8283 2019-07-24 stsp if (error)
7783 0ebf8283 2019-07-24 stsp goto done;
7784 0ebf8283 2019-07-24 stsp continue;
7785 0ebf8283 2019-07-24 stsp }
7786 0ebf8283 2019-07-24 stsp
7787 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7788 0ebf8283 2019-07-24 stsp hle->commit_id);
7789 0ebf8283 2019-07-24 stsp if (error)
7790 0ebf8283 2019-07-24 stsp goto done;
7791 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7792 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7793 0ebf8283 2019-07-24 stsp
7794 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7795 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7796 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7797 0ebf8283 2019-07-24 stsp if (error)
7798 0ebf8283 2019-07-24 stsp goto done;
7799 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7800 0ebf8283 2019-07-24 stsp commit = NULL;
7801 0ebf8283 2019-07-24 stsp
7802 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7803 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7804 a0ea4fc0 2020-02-28 stsp repo);
7805 a0ea4fc0 2020-02-28 stsp if (error)
7806 a0ea4fc0 2020-02-28 stsp goto done;
7807 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7808 0ebf8283 2019-07-24 stsp break;
7809 0ebf8283 2019-07-24 stsp }
7810 0ebf8283 2019-07-24 stsp
7811 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7812 0ebf8283 2019-07-24 stsp char *id_str;
7813 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7814 0ebf8283 2019-07-24 stsp if (error)
7815 0ebf8283 2019-07-24 stsp goto done;
7816 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7817 0ebf8283 2019-07-24 stsp id_str);
7818 0ebf8283 2019-07-24 stsp free(id_str);
7819 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7820 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7821 3e3a69f1 2019-07-25 stsp fileindex);
7822 0ebf8283 2019-07-24 stsp goto done;
7823 0ebf8283 2019-07-24 stsp }
7824 0ebf8283 2019-07-24 stsp
7825 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7826 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7827 0ebf8283 2019-07-24 stsp if (error)
7828 0ebf8283 2019-07-24 stsp goto done;
7829 0ebf8283 2019-07-24 stsp continue;
7830 0ebf8283 2019-07-24 stsp }
7831 0ebf8283 2019-07-24 stsp
7832 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7833 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7834 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7835 0ebf8283 2019-07-24 stsp if (error)
7836 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp }
7838 0ebf8283 2019-07-24 stsp
7839 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7840 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7841 0ebf8283 2019-07-24 stsp if (error)
7842 0ebf8283 2019-07-24 stsp goto done;
7843 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7844 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7845 0ebf8283 2019-07-24 stsp } else
7846 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7847 3e3a69f1 2019-07-25 stsp branch, repo);
7848 0ebf8283 2019-07-24 stsp done:
7849 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7850 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7851 0ebf8283 2019-07-24 stsp free(head_commit_id);
7852 0ebf8283 2019-07-24 stsp free(base_commit_id);
7853 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7854 0ebf8283 2019-07-24 stsp if (commit)
7855 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7856 0ebf8283 2019-07-24 stsp if (branch)
7857 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7858 0ebf8283 2019-07-24 stsp if (tmp_branch)
7859 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7860 0ebf8283 2019-07-24 stsp if (worktree)
7861 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7862 2822a352 2019-10-15 stsp if (repo)
7863 2822a352 2019-10-15 stsp got_repo_close(repo);
7864 2822a352 2019-10-15 stsp return error;
7865 2822a352 2019-10-15 stsp }
7866 2822a352 2019-10-15 stsp
7867 2822a352 2019-10-15 stsp __dead static void
7868 2822a352 2019-10-15 stsp usage_integrate(void)
7869 2822a352 2019-10-15 stsp {
7870 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7871 2822a352 2019-10-15 stsp exit(1);
7872 2822a352 2019-10-15 stsp }
7873 2822a352 2019-10-15 stsp
7874 2822a352 2019-10-15 stsp static const struct got_error *
7875 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7876 2822a352 2019-10-15 stsp {
7877 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7878 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7879 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7880 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7881 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7882 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7883 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7884 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7885 2822a352 2019-10-15 stsp int ch, did_something = 0;
7886 2822a352 2019-10-15 stsp
7887 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7888 2822a352 2019-10-15 stsp switch (ch) {
7889 2822a352 2019-10-15 stsp default:
7890 2822a352 2019-10-15 stsp usage_integrate();
7891 2822a352 2019-10-15 stsp /* NOTREACHED */
7892 2822a352 2019-10-15 stsp }
7893 2822a352 2019-10-15 stsp }
7894 2822a352 2019-10-15 stsp
7895 2822a352 2019-10-15 stsp argc -= optind;
7896 2822a352 2019-10-15 stsp argv += optind;
7897 2822a352 2019-10-15 stsp
7898 2822a352 2019-10-15 stsp if (argc != 1)
7899 2822a352 2019-10-15 stsp usage_integrate();
7900 2822a352 2019-10-15 stsp branch_arg = argv[0];
7901 2822a352 2019-10-15 stsp
7902 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7903 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7904 2822a352 2019-10-15 stsp err(1, "pledge");
7905 2822a352 2019-10-15 stsp
7906 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7907 2822a352 2019-10-15 stsp if (cwd == NULL) {
7908 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7909 2822a352 2019-10-15 stsp goto done;
7910 2822a352 2019-10-15 stsp }
7911 2822a352 2019-10-15 stsp
7912 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7913 2822a352 2019-10-15 stsp if (error)
7914 2822a352 2019-10-15 stsp goto done;
7915 2822a352 2019-10-15 stsp
7916 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7917 2822a352 2019-10-15 stsp if (error)
7918 2822a352 2019-10-15 stsp goto done;
7919 2822a352 2019-10-15 stsp
7920 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7921 2822a352 2019-10-15 stsp NULL);
7922 2822a352 2019-10-15 stsp if (error != NULL)
7923 2822a352 2019-10-15 stsp goto done;
7924 2822a352 2019-10-15 stsp
7925 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7926 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7927 2822a352 2019-10-15 stsp if (error)
7928 2822a352 2019-10-15 stsp goto done;
7929 2822a352 2019-10-15 stsp
7930 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7931 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7932 2822a352 2019-10-15 stsp goto done;
7933 2822a352 2019-10-15 stsp }
7934 2822a352 2019-10-15 stsp
7935 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7936 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7937 2822a352 2019-10-15 stsp if (error)
7938 2822a352 2019-10-15 stsp goto done;
7939 2822a352 2019-10-15 stsp
7940 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7941 2822a352 2019-10-15 stsp if (refname == NULL) {
7942 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7943 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7944 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7945 2822a352 2019-10-15 stsp goto done;
7946 2822a352 2019-10-15 stsp }
7947 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7948 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7949 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7950 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7951 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7952 2822a352 2019-10-15 stsp goto done;
7953 2822a352 2019-10-15 stsp }
7954 2822a352 2019-10-15 stsp
7955 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7956 2822a352 2019-10-15 stsp if (error)
7957 2822a352 2019-10-15 stsp goto done;
7958 2822a352 2019-10-15 stsp
7959 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7960 2822a352 2019-10-15 stsp if (error)
7961 2822a352 2019-10-15 stsp goto done;
7962 2822a352 2019-10-15 stsp
7963 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7964 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7965 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7966 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7967 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7968 2822a352 2019-10-15 stsp goto done;
7969 2822a352 2019-10-15 stsp }
7970 2822a352 2019-10-15 stsp
7971 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7972 2822a352 2019-10-15 stsp if (error) {
7973 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7974 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7975 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7976 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7977 2822a352 2019-10-15 stsp goto done;
7978 2822a352 2019-10-15 stsp }
7979 2822a352 2019-10-15 stsp
7980 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7981 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7982 2822a352 2019-10-15 stsp check_cancelled, NULL);
7983 2822a352 2019-10-15 stsp if (error)
7984 2822a352 2019-10-15 stsp goto done;
7985 2822a352 2019-10-15 stsp
7986 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7987 2822a352 2019-10-15 stsp done:
7988 715dc77e 2019-08-03 stsp if (repo)
7989 715dc77e 2019-08-03 stsp got_repo_close(repo);
7990 2822a352 2019-10-15 stsp if (worktree)
7991 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7992 2822a352 2019-10-15 stsp free(cwd);
7993 2822a352 2019-10-15 stsp free(base_commit_id);
7994 2822a352 2019-10-15 stsp free(commit_id);
7995 2822a352 2019-10-15 stsp free(refname);
7996 2822a352 2019-10-15 stsp free(base_refname);
7997 715dc77e 2019-08-03 stsp return error;
7998 715dc77e 2019-08-03 stsp }
7999 715dc77e 2019-08-03 stsp
8000 715dc77e 2019-08-03 stsp __dead static void
8001 715dc77e 2019-08-03 stsp usage_stage(void)
8002 715dc77e 2019-08-03 stsp {
8003 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8004 f3055044 2019-08-07 stsp "[file-path ...]\n",
8005 715dc77e 2019-08-03 stsp getprogname());
8006 715dc77e 2019-08-03 stsp exit(1);
8007 715dc77e 2019-08-03 stsp }
8008 715dc77e 2019-08-03 stsp
8009 715dc77e 2019-08-03 stsp static const struct got_error *
8010 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8011 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8012 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8013 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8014 408b4ebc 2019-08-03 stsp {
8015 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8016 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8017 408b4ebc 2019-08-03 stsp
8018 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8019 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8020 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8021 408b4ebc 2019-08-03 stsp return NULL;
8022 408b4ebc 2019-08-03 stsp
8023 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8024 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8025 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8026 408b4ebc 2019-08-03 stsp else
8027 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8028 408b4ebc 2019-08-03 stsp if (err)
8029 408b4ebc 2019-08-03 stsp return err;
8030 408b4ebc 2019-08-03 stsp
8031 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8032 408b4ebc 2019-08-03 stsp free(id_str);
8033 dc424a06 2019-08-07 stsp return NULL;
8034 dc424a06 2019-08-07 stsp }
8035 2e1f37b0 2019-08-08 stsp
8036 dc424a06 2019-08-07 stsp static const struct got_error *
8037 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8038 715dc77e 2019-08-03 stsp {
8039 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8040 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8041 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8042 715dc77e 2019-08-03 stsp char *cwd = NULL;
8043 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8044 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8045 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8046 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8047 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8048 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8049 715dc77e 2019-08-03 stsp
8050 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8051 715dc77e 2019-08-03 stsp
8052 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8053 715dc77e 2019-08-03 stsp switch (ch) {
8054 408b4ebc 2019-08-03 stsp case 'l':
8055 408b4ebc 2019-08-03 stsp list_stage = 1;
8056 408b4ebc 2019-08-03 stsp break;
8057 dc424a06 2019-08-07 stsp case 'p':
8058 dc424a06 2019-08-07 stsp pflag = 1;
8059 dc424a06 2019-08-07 stsp break;
8060 dc424a06 2019-08-07 stsp case 'F':
8061 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8062 dc424a06 2019-08-07 stsp break;
8063 715dc77e 2019-08-03 stsp default:
8064 715dc77e 2019-08-03 stsp usage_stage();
8065 715dc77e 2019-08-03 stsp /* NOTREACHED */
8066 715dc77e 2019-08-03 stsp }
8067 715dc77e 2019-08-03 stsp }
8068 715dc77e 2019-08-03 stsp
8069 715dc77e 2019-08-03 stsp argc -= optind;
8070 715dc77e 2019-08-03 stsp argv += optind;
8071 715dc77e 2019-08-03 stsp
8072 715dc77e 2019-08-03 stsp #ifndef PROFILE
8073 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8074 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8075 715dc77e 2019-08-03 stsp err(1, "pledge");
8076 715dc77e 2019-08-03 stsp #endif
8077 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8078 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8079 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8080 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8081 715dc77e 2019-08-03 stsp
8082 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8083 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8084 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8085 715dc77e 2019-08-03 stsp goto done;
8086 715dc77e 2019-08-03 stsp }
8087 715dc77e 2019-08-03 stsp
8088 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8089 715dc77e 2019-08-03 stsp if (error)
8090 715dc77e 2019-08-03 stsp goto done;
8091 715dc77e 2019-08-03 stsp
8092 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8093 c9956ddf 2019-09-08 stsp NULL);
8094 715dc77e 2019-08-03 stsp if (error != NULL)
8095 715dc77e 2019-08-03 stsp goto done;
8096 715dc77e 2019-08-03 stsp
8097 dc424a06 2019-08-07 stsp if (patch_script_path) {
8098 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8099 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8100 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8101 dc424a06 2019-08-07 stsp patch_script_path);
8102 dc424a06 2019-08-07 stsp goto done;
8103 dc424a06 2019-08-07 stsp }
8104 dc424a06 2019-08-07 stsp }
8105 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8106 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8107 715dc77e 2019-08-03 stsp if (error)
8108 715dc77e 2019-08-03 stsp goto done;
8109 715dc77e 2019-08-03 stsp
8110 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8111 6d022e97 2019-08-04 stsp if (error)
8112 6d022e97 2019-08-04 stsp goto done;
8113 715dc77e 2019-08-03 stsp
8114 6d022e97 2019-08-04 stsp if (list_stage)
8115 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8116 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8117 2e1f37b0 2019-08-08 stsp else {
8118 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8119 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8120 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8121 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8122 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8123 2e1f37b0 2019-08-08 stsp }
8124 ad493afc 2019-08-03 stsp done:
8125 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8126 2e1f37b0 2019-08-08 stsp error == NULL)
8127 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8128 ad493afc 2019-08-03 stsp if (repo)
8129 ad493afc 2019-08-03 stsp got_repo_close(repo);
8130 ad493afc 2019-08-03 stsp if (worktree)
8131 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8132 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8133 ad493afc 2019-08-03 stsp free((char *)pe->path);
8134 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8135 ad493afc 2019-08-03 stsp free(cwd);
8136 ad493afc 2019-08-03 stsp return error;
8137 ad493afc 2019-08-03 stsp }
8138 ad493afc 2019-08-03 stsp
8139 ad493afc 2019-08-03 stsp __dead static void
8140 ad493afc 2019-08-03 stsp usage_unstage(void)
8141 ad493afc 2019-08-03 stsp {
8142 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8143 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8144 ad493afc 2019-08-03 stsp getprogname());
8145 ad493afc 2019-08-03 stsp exit(1);
8146 ad493afc 2019-08-03 stsp }
8147 ad493afc 2019-08-03 stsp
8148 ad493afc 2019-08-03 stsp
8149 ad493afc 2019-08-03 stsp static const struct got_error *
8150 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8151 ad493afc 2019-08-03 stsp {
8152 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8153 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8154 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8155 ad493afc 2019-08-03 stsp char *cwd = NULL;
8156 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8157 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8158 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8159 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8160 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8161 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8162 ad493afc 2019-08-03 stsp
8163 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8164 ad493afc 2019-08-03 stsp
8165 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8166 ad493afc 2019-08-03 stsp switch (ch) {
8167 2e1f37b0 2019-08-08 stsp case 'p':
8168 2e1f37b0 2019-08-08 stsp pflag = 1;
8169 2e1f37b0 2019-08-08 stsp break;
8170 2e1f37b0 2019-08-08 stsp case 'F':
8171 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8172 2e1f37b0 2019-08-08 stsp break;
8173 ad493afc 2019-08-03 stsp default:
8174 ad493afc 2019-08-03 stsp usage_unstage();
8175 ad493afc 2019-08-03 stsp /* NOTREACHED */
8176 ad493afc 2019-08-03 stsp }
8177 ad493afc 2019-08-03 stsp }
8178 ad493afc 2019-08-03 stsp
8179 ad493afc 2019-08-03 stsp argc -= optind;
8180 ad493afc 2019-08-03 stsp argv += optind;
8181 ad493afc 2019-08-03 stsp
8182 ad493afc 2019-08-03 stsp #ifndef PROFILE
8183 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8184 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8185 ad493afc 2019-08-03 stsp err(1, "pledge");
8186 ad493afc 2019-08-03 stsp #endif
8187 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8188 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8189 2e1f37b0 2019-08-08 stsp
8190 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8191 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8192 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8193 ad493afc 2019-08-03 stsp goto done;
8194 ad493afc 2019-08-03 stsp }
8195 ad493afc 2019-08-03 stsp
8196 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8197 ad493afc 2019-08-03 stsp if (error)
8198 ad493afc 2019-08-03 stsp goto done;
8199 ad493afc 2019-08-03 stsp
8200 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8201 c9956ddf 2019-09-08 stsp NULL);
8202 ad493afc 2019-08-03 stsp if (error != NULL)
8203 ad493afc 2019-08-03 stsp goto done;
8204 ad493afc 2019-08-03 stsp
8205 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8206 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8207 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8208 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8209 2e1f37b0 2019-08-08 stsp patch_script_path);
8210 2e1f37b0 2019-08-08 stsp goto done;
8211 2e1f37b0 2019-08-08 stsp }
8212 2e1f37b0 2019-08-08 stsp }
8213 2e1f37b0 2019-08-08 stsp
8214 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8215 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8216 ad493afc 2019-08-03 stsp if (error)
8217 ad493afc 2019-08-03 stsp goto done;
8218 ad493afc 2019-08-03 stsp
8219 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8220 ad493afc 2019-08-03 stsp if (error)
8221 ad493afc 2019-08-03 stsp goto done;
8222 ad493afc 2019-08-03 stsp
8223 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8224 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8225 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8226 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8227 715dc77e 2019-08-03 stsp done:
8228 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8229 2e1f37b0 2019-08-08 stsp error == NULL)
8230 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8231 0ebf8283 2019-07-24 stsp if (repo)
8232 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8233 715dc77e 2019-08-03 stsp if (worktree)
8234 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8235 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8236 715dc77e 2019-08-03 stsp free((char *)pe->path);
8237 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8238 715dc77e 2019-08-03 stsp free(cwd);
8239 01073a5d 2019-08-22 stsp return error;
8240 01073a5d 2019-08-22 stsp }
8241 01073a5d 2019-08-22 stsp
8242 01073a5d 2019-08-22 stsp __dead static void
8243 01073a5d 2019-08-22 stsp usage_cat(void)
8244 01073a5d 2019-08-22 stsp {
8245 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8246 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8247 01073a5d 2019-08-22 stsp exit(1);
8248 01073a5d 2019-08-22 stsp }
8249 01073a5d 2019-08-22 stsp
8250 01073a5d 2019-08-22 stsp static const struct got_error *
8251 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8252 01073a5d 2019-08-22 stsp {
8253 01073a5d 2019-08-22 stsp const struct got_error *err;
8254 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8255 01073a5d 2019-08-22 stsp
8256 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8257 01073a5d 2019-08-22 stsp if (err)
8258 01073a5d 2019-08-22 stsp return err;
8259 01073a5d 2019-08-22 stsp
8260 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8261 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8262 01073a5d 2019-08-22 stsp return err;
8263 01073a5d 2019-08-22 stsp }
8264 01073a5d 2019-08-22 stsp
8265 01073a5d 2019-08-22 stsp static const struct got_error *
8266 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8267 01073a5d 2019-08-22 stsp {
8268 01073a5d 2019-08-22 stsp const struct got_error *err;
8269 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8270 56e0773d 2019-11-28 stsp int nentries, i;
8271 01073a5d 2019-08-22 stsp
8272 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8273 01073a5d 2019-08-22 stsp if (err)
8274 01073a5d 2019-08-22 stsp return err;
8275 01073a5d 2019-08-22 stsp
8276 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8277 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8278 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8279 01073a5d 2019-08-22 stsp char *id_str;
8280 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8281 01073a5d 2019-08-22 stsp break;
8282 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8283 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8284 01073a5d 2019-08-22 stsp if (err)
8285 01073a5d 2019-08-22 stsp break;
8286 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8287 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8288 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8289 01073a5d 2019-08-22 stsp free(id_str);
8290 01073a5d 2019-08-22 stsp }
8291 01073a5d 2019-08-22 stsp
8292 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8293 01073a5d 2019-08-22 stsp return err;
8294 01073a5d 2019-08-22 stsp }
8295 01073a5d 2019-08-22 stsp
8296 01073a5d 2019-08-22 stsp static const struct got_error *
8297 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8298 01073a5d 2019-08-22 stsp {
8299 01073a5d 2019-08-22 stsp const struct got_error *err;
8300 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8301 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8302 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8303 01073a5d 2019-08-22 stsp char *id_str = NULL;
8304 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8305 01073a5d 2019-08-22 stsp
8306 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8307 01073a5d 2019-08-22 stsp if (err)
8308 01073a5d 2019-08-22 stsp return err;
8309 01073a5d 2019-08-22 stsp
8310 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8311 01073a5d 2019-08-22 stsp if (err)
8312 01073a5d 2019-08-22 stsp goto done;
8313 01073a5d 2019-08-22 stsp
8314 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8315 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8316 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8317 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8318 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8319 01073a5d 2019-08-22 stsp char *pid_str;
8320 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8321 01073a5d 2019-08-22 stsp if (err)
8322 01073a5d 2019-08-22 stsp goto done;
8323 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8324 01073a5d 2019-08-22 stsp free(pid_str);
8325 01073a5d 2019-08-22 stsp }
8326 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8327 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8328 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8329 01073a5d 2019-08-22 stsp
8330 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8331 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8332 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8333 01073a5d 2019-08-22 stsp
8334 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8335 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8336 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8337 01073a5d 2019-08-22 stsp done:
8338 01073a5d 2019-08-22 stsp free(id_str);
8339 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8340 01073a5d 2019-08-22 stsp return err;
8341 01073a5d 2019-08-22 stsp }
8342 01073a5d 2019-08-22 stsp
8343 01073a5d 2019-08-22 stsp static const struct got_error *
8344 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8345 01073a5d 2019-08-22 stsp {
8346 01073a5d 2019-08-22 stsp const struct got_error *err;
8347 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8348 01073a5d 2019-08-22 stsp char *id_str = NULL;
8349 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8350 01073a5d 2019-08-22 stsp
8351 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8352 01073a5d 2019-08-22 stsp if (err)
8353 01073a5d 2019-08-22 stsp return err;
8354 01073a5d 2019-08-22 stsp
8355 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8356 01073a5d 2019-08-22 stsp if (err)
8357 01073a5d 2019-08-22 stsp goto done;
8358 01073a5d 2019-08-22 stsp
8359 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8360 e15d5241 2019-08-22 stsp
8361 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8362 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8363 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8364 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8365 01073a5d 2019-08-22 stsp break;
8366 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8367 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8368 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8369 01073a5d 2019-08-22 stsp break;
8370 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8371 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8372 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8373 01073a5d 2019-08-22 stsp break;
8374 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8375 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8376 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8377 01073a5d 2019-08-22 stsp break;
8378 01073a5d 2019-08-22 stsp default:
8379 01073a5d 2019-08-22 stsp break;
8380 01073a5d 2019-08-22 stsp }
8381 01073a5d 2019-08-22 stsp
8382 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8383 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8384 e15d5241 2019-08-22 stsp
8385 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8386 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8387 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8388 01073a5d 2019-08-22 stsp
8389 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8390 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8391 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8392 01073a5d 2019-08-22 stsp done:
8393 01073a5d 2019-08-22 stsp free(id_str);
8394 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8395 01073a5d 2019-08-22 stsp return err;
8396 01073a5d 2019-08-22 stsp }
8397 01073a5d 2019-08-22 stsp
8398 01073a5d 2019-08-22 stsp static const struct got_error *
8399 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8400 01073a5d 2019-08-22 stsp {
8401 01073a5d 2019-08-22 stsp const struct got_error *error;
8402 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8403 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8404 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8405 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8406 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8407 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8408 01073a5d 2019-08-22 stsp
8409 01073a5d 2019-08-22 stsp #ifndef PROFILE
8410 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8411 01073a5d 2019-08-22 stsp NULL) == -1)
8412 01073a5d 2019-08-22 stsp err(1, "pledge");
8413 01073a5d 2019-08-22 stsp #endif
8414 01073a5d 2019-08-22 stsp
8415 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8416 01073a5d 2019-08-22 stsp switch (ch) {
8417 896e9b6f 2019-08-26 stsp case 'c':
8418 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8419 896e9b6f 2019-08-26 stsp break;
8420 01073a5d 2019-08-22 stsp case 'r':
8421 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8422 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8423 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8424 9ba1d308 2019-10-21 stsp optarg);
8425 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8426 01073a5d 2019-08-22 stsp break;
8427 896e9b6f 2019-08-26 stsp case 'P':
8428 896e9b6f 2019-08-26 stsp force_path = 1;
8429 896e9b6f 2019-08-26 stsp break;
8430 01073a5d 2019-08-22 stsp default:
8431 01073a5d 2019-08-22 stsp usage_cat();
8432 01073a5d 2019-08-22 stsp /* NOTREACHED */
8433 01073a5d 2019-08-22 stsp }
8434 01073a5d 2019-08-22 stsp }
8435 01073a5d 2019-08-22 stsp
8436 01073a5d 2019-08-22 stsp argc -= optind;
8437 01073a5d 2019-08-22 stsp argv += optind;
8438 01073a5d 2019-08-22 stsp
8439 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8440 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8441 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8442 01073a5d 2019-08-22 stsp goto done;
8443 01073a5d 2019-08-22 stsp }
8444 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8445 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8446 01073a5d 2019-08-22 stsp goto done;
8447 01073a5d 2019-08-22 stsp if (worktree) {
8448 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8449 01073a5d 2019-08-22 stsp repo_path = strdup(
8450 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8451 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8452 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8453 01073a5d 2019-08-22 stsp goto done;
8454 01073a5d 2019-08-22 stsp }
8455 01073a5d 2019-08-22 stsp }
8456 01073a5d 2019-08-22 stsp }
8457 01073a5d 2019-08-22 stsp
8458 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8459 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8460 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8461 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8462 01073a5d 2019-08-22 stsp }
8463 01073a5d 2019-08-22 stsp
8464 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8465 01073a5d 2019-08-22 stsp free(repo_path);
8466 01073a5d 2019-08-22 stsp if (error != NULL)
8467 01073a5d 2019-08-22 stsp goto done;
8468 01073a5d 2019-08-22 stsp
8469 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8470 896e9b6f 2019-08-26 stsp if (error)
8471 896e9b6f 2019-08-26 stsp goto done;
8472 896e9b6f 2019-08-26 stsp
8473 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8474 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8475 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8476 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8477 01073a5d 2019-08-22 stsp if (error)
8478 01073a5d 2019-08-22 stsp goto done;
8479 01073a5d 2019-08-22 stsp
8480 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8481 896e9b6f 2019-08-26 stsp if (force_path) {
8482 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8483 896e9b6f 2019-08-26 stsp argv[i]);
8484 896e9b6f 2019-08-26 stsp if (error)
8485 896e9b6f 2019-08-26 stsp break;
8486 896e9b6f 2019-08-26 stsp } else {
8487 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8488 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8489 896e9b6f 2019-08-26 stsp if (error) {
8490 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8491 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8492 896e9b6f 2019-08-26 stsp break;
8493 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8494 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8495 896e9b6f 2019-08-26 stsp if (error)
8496 896e9b6f 2019-08-26 stsp break;
8497 896e9b6f 2019-08-26 stsp }
8498 896e9b6f 2019-08-26 stsp }
8499 01073a5d 2019-08-22 stsp
8500 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8501 01073a5d 2019-08-22 stsp if (error)
8502 01073a5d 2019-08-22 stsp break;
8503 01073a5d 2019-08-22 stsp
8504 01073a5d 2019-08-22 stsp switch (obj_type) {
8505 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8506 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8507 01073a5d 2019-08-22 stsp break;
8508 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8509 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8510 01073a5d 2019-08-22 stsp break;
8511 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8512 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8513 01073a5d 2019-08-22 stsp break;
8514 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8515 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8516 01073a5d 2019-08-22 stsp break;
8517 01073a5d 2019-08-22 stsp default:
8518 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8519 01073a5d 2019-08-22 stsp break;
8520 01073a5d 2019-08-22 stsp }
8521 01073a5d 2019-08-22 stsp if (error)
8522 01073a5d 2019-08-22 stsp break;
8523 01073a5d 2019-08-22 stsp free(label);
8524 01073a5d 2019-08-22 stsp label = NULL;
8525 01073a5d 2019-08-22 stsp free(id);
8526 01073a5d 2019-08-22 stsp id = NULL;
8527 01073a5d 2019-08-22 stsp }
8528 01073a5d 2019-08-22 stsp done:
8529 01073a5d 2019-08-22 stsp free(label);
8530 01073a5d 2019-08-22 stsp free(id);
8531 896e9b6f 2019-08-26 stsp free(commit_id);
8532 01073a5d 2019-08-22 stsp if (worktree)
8533 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8534 01073a5d 2019-08-22 stsp if (repo) {
8535 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8536 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8537 01073a5d 2019-08-22 stsp if (error == NULL)
8538 01073a5d 2019-08-22 stsp error = repo_error;
8539 01073a5d 2019-08-22 stsp }
8540 0ebf8283 2019-07-24 stsp return error;
8541 0ebf8283 2019-07-24 stsp }