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 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 404c43c4 2018-06-21 stsp #include "got_blame.h"
54 63219cd2 2019-01-04 stsp #include "got_privsep.h"
55 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
56 5c860e29 2018-03-12 stsp
57 5c860e29 2018-03-12 stsp #ifndef nitems
58 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 5c860e29 2018-03-12 stsp #endif
60 99437157 2018-11-11 stsp
61 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static void
65 99437157 2018-11-11 stsp catch_sigint(int signo)
66 99437157 2018-11-11 stsp {
67 99437157 2018-11-11 stsp sigint_received = 1;
68 99437157 2018-11-11 stsp }
69 99437157 2018-11-11 stsp
70 99437157 2018-11-11 stsp static void
71 99437157 2018-11-11 stsp catch_sigpipe(int signo)
72 99437157 2018-11-11 stsp {
73 99437157 2018-11-11 stsp sigpipe_received = 1;
74 99437157 2018-11-11 stsp }
75 5c860e29 2018-03-12 stsp
76 99437157 2018-11-11 stsp
77 8cfb4057 2019-07-09 stsp struct got_cmd {
78 5c860e29 2018-03-12 stsp const char *cmd_name;
79 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
81 97b3a7be 2019-07-09 stsp const char *cmd_alias;
82 5c860e29 2018-03-12 stsp };
83 5c860e29 2018-03-12 stsp
84 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
85 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
86 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
90 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
92 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
93 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
94 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
95 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
96 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
97 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
98 d00136be 2019-03-26 stsp __dead static void usage_add(void);
99 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
100 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
101 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
102 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
103 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
104 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
105 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
106 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
107 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
108 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
109 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
110 5c860e29 2018-03-12 stsp
111 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
112 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
113 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
114 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
115 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
118 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
119 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
121 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
122 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
123 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
124 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
125 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
126 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
127 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
128 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
129 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
130 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
131 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
132 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
133 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
134 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
135 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
136 5c860e29 2018-03-12 stsp
137 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
138 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
139 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
140 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
143 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
144 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
145 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
146 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
147 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
148 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
149 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
150 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
151 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
152 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
153 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
154 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
155 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
157 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
158 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
159 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
160 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
161 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
162 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
163 5c860e29 2018-03-12 stsp };
164 ce5b7c56 2019-07-09 stsp
165 ce5b7c56 2019-07-09 stsp static void
166 ce5b7c56 2019-07-09 stsp list_commands(void)
167 ce5b7c56 2019-07-09 stsp {
168 ce5b7c56 2019-07-09 stsp int i;
169 ce5b7c56 2019-07-09 stsp
170 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
171 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
172 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
173 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
174 ce5b7c56 2019-07-09 stsp }
175 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
176 ce5b7c56 2019-07-09 stsp }
177 5c860e29 2018-03-12 stsp
178 5c860e29 2018-03-12 stsp int
179 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
180 5c860e29 2018-03-12 stsp {
181 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
182 5c860e29 2018-03-12 stsp unsigned int i;
183 5c860e29 2018-03-12 stsp int ch;
184 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
185 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
186 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
187 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
188 83cd27f8 2020-01-13 stsp };
189 5c860e29 2018-03-12 stsp
190 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
191 5c860e29 2018-03-12 stsp
192 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 5c860e29 2018-03-12 stsp switch (ch) {
194 1b6b95a8 2018-03-12 stsp case 'h':
195 1b6b95a8 2018-03-12 stsp hflag = 1;
196 53ccebc2 2019-07-30 stsp break;
197 53ccebc2 2019-07-30 stsp case 'V':
198 53ccebc2 2019-07-30 stsp Vflag = 1;
199 1b6b95a8 2018-03-12 stsp break;
200 5c860e29 2018-03-12 stsp default:
201 ce5b7c56 2019-07-09 stsp usage(hflag);
202 5c860e29 2018-03-12 stsp /* NOTREACHED */
203 5c860e29 2018-03-12 stsp }
204 5c860e29 2018-03-12 stsp }
205 5c860e29 2018-03-12 stsp
206 5c860e29 2018-03-12 stsp argc -= optind;
207 5c860e29 2018-03-12 stsp argv += optind;
208 1e70621d 2018-03-27 stsp optind = 0;
209 53ccebc2 2019-07-30 stsp
210 53ccebc2 2019-07-30 stsp if (Vflag) {
211 53ccebc2 2019-07-30 stsp got_version_print_str();
212 53ccebc2 2019-07-30 stsp return 1;
213 53ccebc2 2019-07-30 stsp }
214 5c860e29 2018-03-12 stsp
215 5c860e29 2018-03-12 stsp if (argc <= 0)
216 ce5b7c56 2019-07-09 stsp usage(hflag);
217 5c860e29 2018-03-12 stsp
218 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
219 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
220 99437157 2018-11-11 stsp
221 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
222 d7d4f210 2018-03-12 stsp const struct got_error *error;
223 d7d4f210 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
225 5c860e29 2018-03-12 stsp
226 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
228 5c860e29 2018-03-12 stsp continue;
229 5c860e29 2018-03-12 stsp
230 1b6b95a8 2018-03-12 stsp if (hflag)
231 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
232 1b6b95a8 2018-03-12 stsp
233 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
234 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
235 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
236 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
237 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 70015d7a 2019-11-08 stsp !(sigint_received &&
239 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
241 d7d4f210 2018-03-12 stsp return 1;
242 d7d4f210 2018-03-12 stsp }
243 d7d4f210 2018-03-12 stsp
244 d7d4f210 2018-03-12 stsp return 0;
245 5c860e29 2018-03-12 stsp }
246 5c860e29 2018-03-12 stsp
247 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 ce5b7c56 2019-07-09 stsp list_commands();
249 5c860e29 2018-03-12 stsp return 1;
250 5c860e29 2018-03-12 stsp }
251 5c860e29 2018-03-12 stsp
252 4ed7e80c 2018-05-20 stsp __dead static void
253 ce5b7c56 2019-07-09 stsp usage(int hflag)
254 5c860e29 2018-03-12 stsp {
255 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 53ccebc2 2019-07-30 stsp getprogname());
257 ce5b7c56 2019-07-09 stsp if (hflag)
258 ce5b7c56 2019-07-09 stsp list_commands();
259 5c860e29 2018-03-12 stsp exit(1);
260 5c860e29 2018-03-12 stsp }
261 5c860e29 2018-03-12 stsp
262 0266afb7 2019-01-04 stsp static const struct got_error *
263 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
264 e2ba3d07 2019-05-13 stsp {
265 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
266 e2ba3d07 2019-05-13 stsp const char *editor;
267 8920fa04 2019-08-18 stsp
268 8920fa04 2019-08-18 stsp *abspath = NULL;
269 e2ba3d07 2019-05-13 stsp
270 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
271 e2ba3d07 2019-05-13 stsp if (editor == NULL)
272 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
273 e2ba3d07 2019-05-13 stsp
274 0ee7065d 2019-05-13 stsp if (editor) {
275 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
276 0ee7065d 2019-05-13 stsp if (err)
277 0ee7065d 2019-05-13 stsp return err;
278 0ee7065d 2019-05-13 stsp }
279 e2ba3d07 2019-05-13 stsp
280 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
281 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
282 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
283 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
284 0ee7065d 2019-05-13 stsp }
285 0ee7065d 2019-05-13 stsp
286 e2ba3d07 2019-05-13 stsp return NULL;
287 e2ba3d07 2019-05-13 stsp }
288 e2ba3d07 2019-05-13 stsp
289 e2ba3d07 2019-05-13 stsp static const struct got_error *
290 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
291 c530dc23 2019-07-23 stsp const char *worktree_path)
292 0266afb7 2019-01-04 stsp {
293 163ce85a 2019-05-13 stsp const struct got_error *err;
294 0266afb7 2019-01-04 stsp
295 37c06ea4 2019-07-15 stsp #ifdef PROFILE
296 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
297 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
298 37c06ea4 2019-07-15 stsp #endif
299 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
301 0266afb7 2019-01-04 stsp
302 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
304 0266afb7 2019-01-04 stsp
305 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
307 0266afb7 2019-01-04 stsp
308 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
309 163ce85a 2019-05-13 stsp if (err != NULL)
310 163ce85a 2019-05-13 stsp return err;
311 0266afb7 2019-01-04 stsp
312 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
313 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
314 0266afb7 2019-01-04 stsp
315 0266afb7 2019-01-04 stsp return NULL;
316 2c7829a4 2019-06-17 stsp }
317 2c7829a4 2019-06-17 stsp
318 2c7829a4 2019-06-17 stsp __dead static void
319 2c7829a4 2019-06-17 stsp usage_init(void)
320 2c7829a4 2019-06-17 stsp {
321 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
322 2c7829a4 2019-06-17 stsp exit(1);
323 2c7829a4 2019-06-17 stsp }
324 2c7829a4 2019-06-17 stsp
325 2c7829a4 2019-06-17 stsp static const struct got_error *
326 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
327 2c7829a4 2019-06-17 stsp {
328 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
329 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
330 2c7829a4 2019-06-17 stsp int ch;
331 2c7829a4 2019-06-17 stsp
332 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
333 2c7829a4 2019-06-17 stsp switch (ch) {
334 2c7829a4 2019-06-17 stsp default:
335 2c7829a4 2019-06-17 stsp usage_init();
336 2c7829a4 2019-06-17 stsp /* NOTREACHED */
337 2c7829a4 2019-06-17 stsp }
338 2c7829a4 2019-06-17 stsp }
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp argc -= optind;
341 2c7829a4 2019-06-17 stsp argv += optind;
342 2c7829a4 2019-06-17 stsp
343 2c7829a4 2019-06-17 stsp #ifndef PROFILE
344 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 2c7829a4 2019-06-17 stsp err(1, "pledge");
346 2c7829a4 2019-06-17 stsp #endif
347 2c7829a4 2019-06-17 stsp if (argc != 1)
348 bc20e173 2019-06-17 stsp usage_init();
349 2c7829a4 2019-06-17 stsp
350 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
351 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
352 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
355 2c7829a4 2019-06-17 stsp
356 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
357 2c7829a4 2019-06-17 stsp if (error &&
358 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
359 2c7829a4 2019-06-17 stsp goto done;
360 2c7829a4 2019-06-17 stsp
361 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
362 2c7829a4 2019-06-17 stsp if (error)
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
366 3ce1b845 2019-07-15 stsp done:
367 3ce1b845 2019-07-15 stsp free(repo_path);
368 3ce1b845 2019-07-15 stsp return error;
369 3ce1b845 2019-07-15 stsp }
370 3ce1b845 2019-07-15 stsp
371 3ce1b845 2019-07-15 stsp __dead static void
372 3ce1b845 2019-07-15 stsp usage_import(void)
373 3ce1b845 2019-07-15 stsp {
374 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
376 3ce1b845 2019-07-15 stsp exit(1);
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp int
380 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
381 3ce1b845 2019-07-15 stsp {
382 3ce1b845 2019-07-15 stsp pid_t pid;
383 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
384 3ce1b845 2019-07-15 stsp int st = -1;
385 3ce1b845 2019-07-15 stsp
386 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
387 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
391 3ce1b845 2019-07-15 stsp case -1:
392 3ce1b845 2019-07-15 stsp goto doneediting;
393 3ce1b845 2019-07-15 stsp case 0:
394 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
395 3ce1b845 2019-07-15 stsp _exit(127);
396 3ce1b845 2019-07-15 stsp }
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
399 3ce1b845 2019-07-15 stsp if (errno != EINTR)
400 3ce1b845 2019-07-15 stsp break;
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp doneediting:
403 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
404 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
408 3ce1b845 2019-07-15 stsp errno = EINTR;
409 3ce1b845 2019-07-15 stsp return -1;
410 3ce1b845 2019-07-15 stsp }
411 3ce1b845 2019-07-15 stsp
412 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
413 3ce1b845 2019-07-15 stsp }
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp static const struct got_error *
416 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 3ce1b845 2019-07-15 stsp const char *initial_content)
418 3ce1b845 2019-07-15 stsp {
419 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
420 3ce1b845 2019-07-15 stsp char buf[1024];
421 3ce1b845 2019-07-15 stsp struct stat st, st2;
422 3ce1b845 2019-07-15 stsp FILE *fp;
423 3ce1b845 2019-07-15 stsp int content_changed = 0;
424 3ce1b845 2019-07-15 stsp size_t len;
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp *logmsg = NULL;
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
430 3ce1b845 2019-07-15 stsp
431 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
432 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
435 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
436 3ce1b845 2019-07-15 stsp
437 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
442 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
444 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
445 3ce1b845 2019-07-15 stsp len = 0;
446 3ce1b845 2019-07-15 stsp
447 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
448 3ce1b845 2019-07-15 stsp if (fp == NULL) {
449 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
450 3ce1b845 2019-07-15 stsp goto done;
451 3ce1b845 2019-07-15 stsp }
452 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
453 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
454 3ce1b845 2019-07-15 stsp content_changed = 1;
455 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
457 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
458 3ce1b845 2019-07-15 stsp }
459 3ce1b845 2019-07-15 stsp fclose(fp);
460 3ce1b845 2019-07-15 stsp
461 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
463 3ce1b845 2019-07-15 stsp len--;
464 3ce1b845 2019-07-15 stsp }
465 3ce1b845 2019-07-15 stsp
466 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
467 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
469 3ce1b845 2019-07-15 stsp done:
470 3ce1b845 2019-07-15 stsp if (err) {
471 3ce1b845 2019-07-15 stsp free(*logmsg);
472 3ce1b845 2019-07-15 stsp *logmsg = NULL;
473 3ce1b845 2019-07-15 stsp }
474 3ce1b845 2019-07-15 stsp return err;
475 3ce1b845 2019-07-15 stsp }
476 3ce1b845 2019-07-15 stsp
477 3ce1b845 2019-07-15 stsp static const struct got_error *
478 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
480 3ce1b845 2019-07-15 stsp {
481 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
482 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
483 3ce1b845 2019-07-15 stsp int fd;
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
486 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
487 3ce1b845 2019-07-15 stsp branch_name) == -1)
488 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
489 3ce1b845 2019-07-15 stsp
490 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
491 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
492 3ce1b845 2019-07-15 stsp if (err)
493 3ce1b845 2019-07-15 stsp goto done;
494 3ce1b845 2019-07-15 stsp
495 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
496 3ce1b845 2019-07-15 stsp close(fd);
497 3ce1b845 2019-07-15 stsp
498 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
499 3ce1b845 2019-07-15 stsp done:
500 3ce1b845 2019-07-15 stsp free(initial_content);
501 3ce1b845 2019-07-15 stsp return err;
502 3ce1b845 2019-07-15 stsp }
503 3ce1b845 2019-07-15 stsp
504 3ce1b845 2019-07-15 stsp static const struct got_error *
505 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
506 3ce1b845 2019-07-15 stsp {
507 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
508 3ce1b845 2019-07-15 stsp return NULL;
509 3ce1b845 2019-07-15 stsp }
510 3ce1b845 2019-07-15 stsp
511 3ce1b845 2019-07-15 stsp static const struct got_error *
512 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
513 84792843 2019-08-09 stsp {
514 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
515 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
516 84792843 2019-08-09 stsp
517 84792843 2019-08-09 stsp *author = NULL;
518 aba9c984 2019-09-08 stsp
519 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
521 c9956ddf 2019-09-08 stsp if (name && email) {
522 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
523 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
524 aba9c984 2019-09-08 stsp return NULL;
525 aba9c984 2019-09-08 stsp }
526 84792843 2019-08-09 stsp
527 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
528 84792843 2019-08-09 stsp if (got_author == NULL) {
529 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
530 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
531 c9956ddf 2019-09-08 stsp if (name && email) {
532 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
533 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
534 c9956ddf 2019-09-08 stsp return NULL;
535 c9956ddf 2019-09-08 stsp }
536 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
537 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
538 84792843 2019-08-09 stsp }
539 84792843 2019-08-09 stsp
540 aba9c984 2019-09-08 stsp *author = strdup(got_author);
541 aba9c984 2019-09-08 stsp if (*author == NULL)
542 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
543 84792843 2019-08-09 stsp
544 84792843 2019-08-09 stsp /*
545 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
546 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
547 84792843 2019-08-09 stsp */
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '<') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 aba9c984 2019-09-08 stsp goto done;
553 aba9c984 2019-09-08 stsp }
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
555 84792843 2019-08-09 stsp got_author++;
556 aba9c984 2019-09-08 stsp if (*got_author != '@') {
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 aba9c984 2019-09-08 stsp goto done;
559 aba9c984 2019-09-08 stsp }
560 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
561 84792843 2019-08-09 stsp got_author++;
562 84792843 2019-08-09 stsp if (*got_author != '>')
563 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 aba9c984 2019-09-08 stsp done:
565 aba9c984 2019-09-08 stsp if (err) {
566 aba9c984 2019-09-08 stsp free(*author);
567 aba9c984 2019-09-08 stsp *author = NULL;
568 aba9c984 2019-09-08 stsp }
569 aba9c984 2019-09-08 stsp return err;
570 c9956ddf 2019-09-08 stsp }
571 c9956ddf 2019-09-08 stsp
572 c9956ddf 2019-09-08 stsp static const struct got_error *
573 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
574 c9956ddf 2019-09-08 stsp {
575 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
576 c9956ddf 2019-09-08 stsp
577 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
578 c9956ddf 2019-09-08 stsp if (homedir) {
579 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
581 c9956ddf 2019-09-08 stsp
582 c9956ddf 2019-09-08 stsp }
583 c9956ddf 2019-09-08 stsp return NULL;
584 84792843 2019-08-09 stsp }
585 84792843 2019-08-09 stsp
586 84792843 2019-08-09 stsp static const struct got_error *
587 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
588 3ce1b845 2019-07-15 stsp {
589 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
590 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
593 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
595 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
597 3ce1b845 2019-07-15 stsp int ch;
598 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
600 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
601 3ce1b845 2019-07-15 stsp
602 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
603 3ce1b845 2019-07-15 stsp
604 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 3ce1b845 2019-07-15 stsp switch (ch) {
606 3ce1b845 2019-07-15 stsp case 'b':
607 3ce1b845 2019-07-15 stsp branch_name = optarg;
608 3ce1b845 2019-07-15 stsp break;
609 3ce1b845 2019-07-15 stsp case 'm':
610 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
611 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
612 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
613 3ce1b845 2019-07-15 stsp goto done;
614 3ce1b845 2019-07-15 stsp }
615 3ce1b845 2019-07-15 stsp break;
616 3ce1b845 2019-07-15 stsp case 'r':
617 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
618 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
619 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
620 9ba1d308 2019-10-21 stsp optarg);
621 3ce1b845 2019-07-15 stsp goto done;
622 3ce1b845 2019-07-15 stsp }
623 3ce1b845 2019-07-15 stsp break;
624 3ce1b845 2019-07-15 stsp case 'I':
625 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
626 3ce1b845 2019-07-15 stsp break;
627 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
628 3ce1b845 2019-07-15 stsp NULL);
629 3ce1b845 2019-07-15 stsp if (error)
630 3ce1b845 2019-07-15 stsp goto done;
631 3ce1b845 2019-07-15 stsp break;
632 3ce1b845 2019-07-15 stsp default:
633 b2b65d18 2019-08-22 stsp usage_import();
634 3ce1b845 2019-07-15 stsp /* NOTREACHED */
635 3ce1b845 2019-07-15 stsp }
636 3ce1b845 2019-07-15 stsp }
637 3ce1b845 2019-07-15 stsp
638 3ce1b845 2019-07-15 stsp argc -= optind;
639 3ce1b845 2019-07-15 stsp argv += optind;
640 3ce1b845 2019-07-15 stsp
641 3ce1b845 2019-07-15 stsp #ifndef PROFILE
642 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
643 aba9c984 2019-09-08 stsp "unveil",
644 3ce1b845 2019-07-15 stsp NULL) == -1)
645 3ce1b845 2019-07-15 stsp err(1, "pledge");
646 3ce1b845 2019-07-15 stsp #endif
647 3ce1b845 2019-07-15 stsp if (argc != 1)
648 3ce1b845 2019-07-15 stsp usage_import();
649 2c7829a4 2019-06-17 stsp
650 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
651 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
652 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
653 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
654 3ce1b845 2019-07-15 stsp }
655 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
656 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
657 c9956ddf 2019-09-08 stsp if (error)
658 c9956ddf 2019-09-08 stsp goto done;
659 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
660 3ce1b845 2019-07-15 stsp if (error)
661 3ce1b845 2019-07-15 stsp goto done;
662 aba9c984 2019-09-08 stsp
663 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
664 aba9c984 2019-09-08 stsp if (error)
665 aba9c984 2019-09-08 stsp return error;
666 e560b7e0 2019-11-28 stsp
667 e560b7e0 2019-11-28 stsp /*
668 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
669 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
670 e560b7e0 2019-11-28 stsp * an unintended typo.
671 e560b7e0 2019-11-28 stsp */
672 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
673 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
674 3ce1b845 2019-07-15 stsp
675 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
677 3ce1b845 2019-07-15 stsp goto done;
678 3ce1b845 2019-07-15 stsp }
679 3ce1b845 2019-07-15 stsp
680 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
681 3ce1b845 2019-07-15 stsp if (error) {
682 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
683 3ce1b845 2019-07-15 stsp goto done;
684 3ce1b845 2019-07-15 stsp } else {
685 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 3ce1b845 2019-07-15 stsp "import target branch already exists");
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp }
689 3ce1b845 2019-07-15 stsp
690 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
691 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
692 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
693 3ce1b845 2019-07-15 stsp goto done;
694 3ce1b845 2019-07-15 stsp }
695 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
696 3ce1b845 2019-07-15 stsp
697 3ce1b845 2019-07-15 stsp /*
698 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
699 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
700 3ce1b845 2019-07-15 stsp */
701 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
702 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
703 3ce1b845 2019-07-15 stsp if (error)
704 3ce1b845 2019-07-15 stsp goto done;
705 8e158b01 2019-09-22 stsp free(logmsg);
706 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 ef293bdd 2019-10-21 stsp path_dir, refname);
708 ef293bdd 2019-10-21 stsp if (error) {
709 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
712 3ce1b845 2019-07-15 stsp goto done;
713 ef293bdd 2019-10-21 stsp }
714 3ce1b845 2019-07-15 stsp }
715 3ce1b845 2019-07-15 stsp
716 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
717 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
718 ef293bdd 2019-10-21 stsp if (logmsg_path)
719 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
720 3ce1b845 2019-07-15 stsp goto done;
721 ef293bdd 2019-10-21 stsp }
722 3ce1b845 2019-07-15 stsp
723 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
727 ef293bdd 2019-10-21 stsp goto done;
728 ef293bdd 2019-10-21 stsp }
729 ef293bdd 2019-10-21 stsp
730 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
732 ef293bdd 2019-10-21 stsp if (error) {
733 ef293bdd 2019-10-21 stsp if (logmsg_path)
734 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
735 3ce1b845 2019-07-15 stsp goto done;
736 ef293bdd 2019-10-21 stsp }
737 3ce1b845 2019-07-15 stsp
738 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 ef293bdd 2019-10-21 stsp if (error) {
740 ef293bdd 2019-10-21 stsp if (logmsg_path)
741 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
742 3ce1b845 2019-07-15 stsp goto done;
743 ef293bdd 2019-10-21 stsp }
744 3ce1b845 2019-07-15 stsp
745 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (logmsg_path)
748 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
749 3ce1b845 2019-07-15 stsp goto done;
750 ef293bdd 2019-10-21 stsp }
751 3ce1b845 2019-07-15 stsp
752 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
753 ef293bdd 2019-10-21 stsp if (error) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
756 3ce1b845 2019-07-15 stsp goto done;
757 ef293bdd 2019-10-21 stsp }
758 3ce1b845 2019-07-15 stsp
759 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 3ce1b845 2019-07-15 stsp if (error) {
761 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
764 3ce1b845 2019-07-15 stsp goto done;
765 ef293bdd 2019-10-21 stsp }
766 3ce1b845 2019-07-15 stsp
767 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 3ce1b845 2019-07-15 stsp branch_ref);
769 ef293bdd 2019-10-21 stsp if (error) {
770 ef293bdd 2019-10-21 stsp if (logmsg_path)
771 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
772 3ce1b845 2019-07-15 stsp goto done;
773 ef293bdd 2019-10-21 stsp }
774 3ce1b845 2019-07-15 stsp
775 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
776 ef293bdd 2019-10-21 stsp if (error) {
777 ef293bdd 2019-10-21 stsp if (logmsg_path)
778 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
779 3ce1b845 2019-07-15 stsp goto done;
780 ef293bdd 2019-10-21 stsp }
781 3ce1b845 2019-07-15 stsp }
782 3ce1b845 2019-07-15 stsp
783 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
784 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
785 2c7829a4 2019-06-17 stsp done:
786 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
787 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
788 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
789 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
791 8e158b01 2019-09-22 stsp free(logmsg);
792 ef293bdd 2019-10-21 stsp free(logmsg_path);
793 2c7829a4 2019-06-17 stsp free(repo_path);
794 3ce1b845 2019-07-15 stsp free(editor);
795 3ce1b845 2019-07-15 stsp free(refname);
796 3ce1b845 2019-07-15 stsp free(new_commit_id);
797 3ce1b845 2019-07-15 stsp free(id_str);
798 aba9c984 2019-09-08 stsp free(author);
799 c9956ddf 2019-09-08 stsp free(gitconfig_path);
800 3ce1b845 2019-07-15 stsp if (branch_ref)
801 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
802 3ce1b845 2019-07-15 stsp if (head_ref)
803 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
804 2c7829a4 2019-06-17 stsp return error;
805 93658fb9 2020-03-18 stsp }
806 93658fb9 2020-03-18 stsp
807 93658fb9 2020-03-18 stsp __dead static void
808 93658fb9 2020-03-18 stsp usage_clone(void)
809 93658fb9 2020-03-18 stsp {
810 93658fb9 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
811 93658fb9 2020-03-18 stsp exit(1);
812 0266afb7 2019-01-04 stsp }
813 0266afb7 2019-01-04 stsp
814 4ed7e80c 2018-05-20 stsp __dead static void
815 c09a553d 2018-03-12 stsp usage_checkout(void)
816 c09a553d 2018-03-12 stsp {
817 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
819 c09a553d 2018-03-12 stsp exit(1);
820 92a684f4 2018-03-12 stsp }
821 92a684f4 2018-03-12 stsp
822 7f47418f 2019-12-20 stsp static void
823 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
824 7f47418f 2019-12-20 stsp {
825 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
826 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
827 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
828 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
829 7f47418f 2019-12-20 stsp getprogname());
830 7f47418f 2019-12-20 stsp }
831 7f47418f 2019-12-20 stsp
832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
833 7f47418f 2019-12-20 stsp const char *worktree_path;
834 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
835 7f47418f 2019-12-20 stsp };
836 7f47418f 2019-12-20 stsp
837 1ee397ad 2019-07-12 stsp static const struct got_error *
838 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
839 92a684f4 2018-03-12 stsp {
840 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
841 a484d721 2019-06-10 stsp
842 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
843 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
844 7f47418f 2019-12-20 stsp return NULL;
845 7f47418f 2019-12-20 stsp
846 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
847 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
848 1ee397ad 2019-07-12 stsp return NULL;
849 7f47418f 2019-12-20 stsp }
850 92a684f4 2018-03-12 stsp
851 92a684f4 2018-03-12 stsp while (path[0] == '/')
852 92a684f4 2018-03-12 stsp path++;
853 92a684f4 2018-03-12 stsp
854 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
855 1ee397ad 2019-07-12 stsp return NULL;
856 99437157 2018-11-11 stsp }
857 99437157 2018-11-11 stsp
858 99437157 2018-11-11 stsp static const struct got_error *
859 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
860 99437157 2018-11-11 stsp {
861 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
862 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
863 99437157 2018-11-11 stsp return NULL;
864 8069f636 2019-01-12 stsp }
865 8069f636 2019-01-12 stsp
866 8069f636 2019-01-12 stsp static const struct got_error *
867 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
868 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 3aef623b 2019-10-15 stsp struct got_repository *repo)
870 8069f636 2019-01-12 stsp {
871 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
872 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
873 8069f636 2019-01-12 stsp
874 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
876 36a38700 2019-05-10 stsp if (err)
877 36a38700 2019-05-10 stsp return err;
878 8069f636 2019-01-12 stsp
879 d5bea539 2019-05-13 stsp if (yca_id == NULL)
880 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
881 8069f636 2019-01-12 stsp
882 d5bea539 2019-05-13 stsp /*
883 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
884 d5bea539 2019-05-13 stsp * and the work tree's base commit.
885 d5bea539 2019-05-13 stsp *
886 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
887 d5bea539 2019-05-13 stsp *
888 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
889 d5bea539 2019-05-13 stsp * \ /
890 d5bea539 2019-05-13 stsp * C E
891 d5bea539 2019-05-13 stsp * \ /
892 d5bea539 2019-05-13 stsp * B (yca)
893 d5bea539 2019-05-13 stsp * |
894 d5bea539 2019-05-13 stsp * A
895 d5bea539 2019-05-13 stsp *
896 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
897 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
898 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
899 d5bea539 2019-05-13 stsp */
900 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
901 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
903 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
905 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
906 8069f636 2019-01-12 stsp
907 d5bea539 2019-05-13 stsp free(yca_id);
908 d5bea539 2019-05-13 stsp return NULL;
909 c09a553d 2018-03-12 stsp }
910 a367ff0f 2019-05-14 stsp
911 a367ff0f 2019-05-14 stsp static const struct got_error *
912 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
913 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
914 a51a74b3 2019-07-27 stsp struct got_repository *repo)
915 a367ff0f 2019-05-14 stsp {
916 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
917 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
918 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
919 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
920 a367ff0f 2019-05-14 stsp
921 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
922 a367ff0f 2019-05-14 stsp if (err)
923 a51a74b3 2019-07-27 stsp goto done;
924 a51a74b3 2019-07-27 stsp
925 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 a51a74b3 2019-07-27 stsp is_same_branch = 1;
927 a51a74b3 2019-07-27 stsp goto done;
928 a51a74b3 2019-07-27 stsp }
929 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 a51a74b3 2019-07-27 stsp is_same_branch = 1;
931 a367ff0f 2019-05-14 stsp goto done;
932 a51a74b3 2019-07-27 stsp }
933 a367ff0f 2019-05-14 stsp
934 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
935 a367ff0f 2019-05-14 stsp if (err)
936 a367ff0f 2019-05-14 stsp goto done;
937 a367ff0f 2019-05-14 stsp
938 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
940 a367ff0f 2019-05-14 stsp if (err)
941 a367ff0f 2019-05-14 stsp goto done;
942 a367ff0f 2019-05-14 stsp
943 a367ff0f 2019-05-14 stsp for (;;) {
944 a367ff0f 2019-05-14 stsp struct got_object_id *id;
945 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
946 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
947 a367ff0f 2019-05-14 stsp if (err) {
948 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
949 a367ff0f 2019-05-14 stsp err = NULL;
950 ee780d5c 2020-01-04 stsp break;
951 a367ff0f 2019-05-14 stsp }
952 c09a553d 2018-03-12 stsp
953 a367ff0f 2019-05-14 stsp if (id) {
954 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
955 a51a74b3 2019-07-27 stsp break;
956 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
957 a367ff0f 2019-05-14 stsp is_same_branch = 1;
958 a367ff0f 2019-05-14 stsp break;
959 a367ff0f 2019-05-14 stsp }
960 a367ff0f 2019-05-14 stsp }
961 a367ff0f 2019-05-14 stsp }
962 a367ff0f 2019-05-14 stsp done:
963 a367ff0f 2019-05-14 stsp if (graph)
964 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
965 a367ff0f 2019-05-14 stsp free(head_commit_id);
966 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
967 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
968 a367ff0f 2019-05-14 stsp return err;
969 93658fb9 2020-03-18 stsp }
970 93658fb9 2020-03-18 stsp
971 93658fb9 2020-03-18 stsp static const struct got_error *
972 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
973 93658fb9 2020-03-18 stsp {
974 09838ffc 2020-03-18 stsp const struct got_error *err = NULL;
975 09838ffc 2020-03-18 stsp const char *uri, *branch_filter, *dirname;
976 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
977 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
978 bb64b798 2020-03-18 stsp const char *repo_path;
979 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
980 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
981 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
982 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
983 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
984 93658fb9 2020-03-18 stsp
985 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
986 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
987 d9b4d0c0 2020-03-18 stsp
988 93658fb9 2020-03-18 stsp while ((ch = getopt(argc, argv, "b:")) != -1) {
989 93658fb9 2020-03-18 stsp switch (ch) {
990 93658fb9 2020-03-18 stsp case 'b':
991 93658fb9 2020-03-18 stsp branch_filter = optarg;
992 93658fb9 2020-03-18 stsp break;
993 93658fb9 2020-03-18 stsp default:
994 93658fb9 2020-03-18 stsp usage_clone();
995 93658fb9 2020-03-18 stsp break;
996 93658fb9 2020-03-18 stsp }
997 93658fb9 2020-03-18 stsp }
998 93658fb9 2020-03-18 stsp argc -= optind;
999 93658fb9 2020-03-18 stsp argv += optind;
1000 93658fb9 2020-03-18 stsp uri = argv[0];
1001 93658fb9 2020-03-18 stsp if(argc == 1)
1002 93658fb9 2020-03-18 stsp dirname = NULL;
1003 93658fb9 2020-03-18 stsp else if(argc == 2)
1004 93658fb9 2020-03-18 stsp dirname = argv[1];
1005 93658fb9 2020-03-18 stsp else
1006 93658fb9 2020-03-18 stsp usage_clone();
1007 09838ffc 2020-03-18 stsp
1008 09838ffc 2020-03-18 stsp err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1009 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1010 20eb36d0 2020-03-18 stsp if (err)
1011 20eb36d0 2020-03-18 stsp goto done;
1012 20eb36d0 2020-03-18 stsp
1013 20eb36d0 2020-03-18 stsp err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1014 09838ffc 2020-03-18 stsp if (err)
1015 09838ffc 2020-03-18 stsp goto done;
1016 09838ffc 2020-03-18 stsp
1017 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1018 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1019 bb64b798 2020-03-18 stsp err = got_error_from_errno("asprintf");
1020 bb64b798 2020-03-18 stsp goto done;
1021 bb64b798 2020-03-18 stsp }
1022 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1023 bb64b798 2020-03-18 stsp } else
1024 bb64b798 2020-03-18 stsp repo_path = dirname;
1025 bb64b798 2020-03-18 stsp
1026 bb64b798 2020-03-18 stsp err = got_path_mkdir(repo_path);
1027 bb64b798 2020-03-18 stsp if (err)
1028 bb64b798 2020-03-18 stsp goto done;
1029 bb64b798 2020-03-18 stsp
1030 bb64b798 2020-03-18 stsp err = got_repo_init(repo_path);
1031 bb64b798 2020-03-18 stsp if (err != NULL)
1032 bb64b798 2020-03-18 stsp goto done;
1033 bb64b798 2020-03-18 stsp
1034 bb64b798 2020-03-18 stsp err = got_repo_open(&repo, repo_path, NULL);
1035 bb64b798 2020-03-18 stsp if (err)
1036 bb64b798 2020-03-18 stsp goto done;
1037 bb64b798 2020-03-18 stsp
1038 07e52fce 2020-03-18 stsp err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1039 ceee4c0f 2020-03-18 stsp repo);
1040 d9b4d0c0 2020-03-18 stsp if (err)
1041 d9b4d0c0 2020-03-18 stsp goto done;
1042 d9b4d0c0 2020-03-18 stsp
1043 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, pack_hash);
1044 d9b4d0c0 2020-03-18 stsp if (err)
1045 d9b4d0c0 2020-03-18 stsp goto done;
1046 d9b4d0c0 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1047 d9b4d0c0 2020-03-18 stsp free(id_str);
1048 d9b4d0c0 2020-03-18 stsp
1049 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1050 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1051 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1052 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1053 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1054 d9b4d0c0 2020-03-18 stsp
1055 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, id);
1056 d9b4d0c0 2020-03-18 stsp if (err)
1057 d9b4d0c0 2020-03-18 stsp goto done;
1058 d9b4d0c0 2020-03-18 stsp
1059 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc(&ref, refname, id);
1060 d9b4d0c0 2020-03-18 stsp if (err) {
1061 d9b4d0c0 2020-03-18 stsp free(id_str);
1062 d9b4d0c0 2020-03-18 stsp goto done;
1063 d9b4d0c0 2020-03-18 stsp }
1064 d9b4d0c0 2020-03-18 stsp
1065 d9b4d0c0 2020-03-18 stsp printf("%s: %s\n", got_ref_get_name(ref), id_str);
1066 d9b4d0c0 2020-03-18 stsp free(id_str);
1067 d9b4d0c0 2020-03-18 stsp err = got_ref_write(ref, repo);
1068 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1069 d9b4d0c0 2020-03-18 stsp if (err)
1070 d9b4d0c0 2020-03-18 stsp goto done;
1071 d9b4d0c0 2020-03-18 stsp }
1072 d9b4d0c0 2020-03-18 stsp
1073 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1074 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1075 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1076 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1077 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1078 d9b4d0c0 2020-03-18 stsp
1079 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1080 d9b4d0c0 2020-03-18 stsp continue;
1081 d9b4d0c0 2020-03-18 stsp
1082 d9b4d0c0 2020-03-18 stsp err = got_ref_open(&target_ref, repo, target, 0);
1083 d9b4d0c0 2020-03-18 stsp if (err) {
1084 d9b4d0c0 2020-03-18 stsp if (err->code == GOT_ERR_NOT_REF)
1085 d9b4d0c0 2020-03-18 stsp continue;
1086 d9b4d0c0 2020-03-18 stsp goto done;
1087 d9b4d0c0 2020-03-18 stsp }
1088 d9b4d0c0 2020-03-18 stsp
1089 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1090 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1091 d9b4d0c0 2020-03-18 stsp if (err)
1092 d9b4d0c0 2020-03-18 stsp goto done;
1093 d9b4d0c0 2020-03-18 stsp
1094 d9b4d0c0 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1095 d9b4d0c0 2020-03-18 stsp got_ref_get_symref_target(symref));
1096 d9b4d0c0 2020-03-18 stsp
1097 d9b4d0c0 2020-03-18 stsp err = got_ref_write(symref, repo);
1098 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1099 d9b4d0c0 2020-03-18 stsp break;
1100 d9b4d0c0 2020-03-18 stsp }
1101 d9b4d0c0 2020-03-18 stsp
1102 09838ffc 2020-03-18 stsp done:
1103 20eb36d0 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1104 20eb36d0 2020-03-18 stsp err = got_error_from_errno("close");
1105 bb64b798 2020-03-18 stsp if (repo)
1106 bb64b798 2020-03-18 stsp got_repo_close(repo);
1107 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1108 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1109 d9b4d0c0 2020-03-18 stsp free(pe->data);
1110 d9b4d0c0 2020-03-18 stsp }
1111 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1112 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1113 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1114 d9b4d0c0 2020-03-18 stsp free(pe->data);
1115 d9b4d0c0 2020-03-18 stsp }
1116 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1117 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1118 09838ffc 2020-03-18 stsp free(proto);
1119 09838ffc 2020-03-18 stsp free(host);
1120 09838ffc 2020-03-18 stsp free(port);
1121 09838ffc 2020-03-18 stsp free(server_path);
1122 09838ffc 2020-03-18 stsp free(repo_name);
1123 bb64b798 2020-03-18 stsp free(default_destdir);
1124 09838ffc 2020-03-18 stsp return err;
1125 a367ff0f 2019-05-14 stsp }
1126 8069f636 2019-01-12 stsp
1127 4ed7e80c 2018-05-20 stsp static const struct got_error *
1128 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1129 4b6c9460 2020-03-05 stsp {
1130 4b6c9460 2020-03-05 stsp static char msg[512];
1131 4b6c9460 2020-03-05 stsp const char *branch_name;
1132 4b6c9460 2020-03-05 stsp
1133 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1134 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1135 4b6c9460 2020-03-05 stsp else
1136 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1137 4b6c9460 2020-03-05 stsp
1138 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1139 4b6c9460 2020-03-05 stsp branch_name += 11;
1140 4b6c9460 2020-03-05 stsp
1141 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1142 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1143 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1144 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1145 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1146 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1147 4b6c9460 2020-03-05 stsp
1148 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1149 4b6c9460 2020-03-05 stsp }
1150 4b6c9460 2020-03-05 stsp
1151 4b6c9460 2020-03-05 stsp static const struct got_error *
1152 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1153 c09a553d 2018-03-12 stsp {
1154 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1155 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1156 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1157 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1158 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1159 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1160 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1161 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1162 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1163 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1164 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1165 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1166 f2ea84fa 2019-07-27 stsp
1167 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1168 c09a553d 2018-03-12 stsp
1169 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1170 0bb8a95e 2018-03-12 stsp switch (ch) {
1171 08573d5b 2019-05-14 stsp case 'b':
1172 08573d5b 2019-05-14 stsp branch_name = optarg;
1173 08573d5b 2019-05-14 stsp break;
1174 8069f636 2019-01-12 stsp case 'c':
1175 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1176 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1177 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1178 bb51a5b4 2020-01-13 stsp break;
1179 bb51a5b4 2020-01-13 stsp case 'E':
1180 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1181 8069f636 2019-01-12 stsp break;
1182 0bb8a95e 2018-03-12 stsp case 'p':
1183 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1184 0bb8a95e 2018-03-12 stsp break;
1185 0bb8a95e 2018-03-12 stsp default:
1186 2deda0b9 2019-03-07 stsp usage_checkout();
1187 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1188 0bb8a95e 2018-03-12 stsp }
1189 0bb8a95e 2018-03-12 stsp }
1190 0bb8a95e 2018-03-12 stsp
1191 0bb8a95e 2018-03-12 stsp argc -= optind;
1192 0bb8a95e 2018-03-12 stsp argv += optind;
1193 0bb8a95e 2018-03-12 stsp
1194 6715a751 2018-03-16 stsp #ifndef PROFILE
1195 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1196 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1197 c09a553d 2018-03-12 stsp err(1, "pledge");
1198 6715a751 2018-03-16 stsp #endif
1199 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1200 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1201 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1202 76089277 2018-04-01 stsp if (repo_path == NULL)
1203 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1204 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1205 76089277 2018-04-01 stsp if (cwd == NULL) {
1206 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1207 76089277 2018-04-01 stsp goto done;
1208 76089277 2018-04-01 stsp }
1209 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1210 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1211 230a42bd 2019-05-11 jcs if (base == NULL) {
1212 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1213 230a42bd 2019-05-11 jcs path_prefix);
1214 230a42bd 2019-05-11 jcs goto done;
1215 230a42bd 2019-05-11 jcs }
1216 230a42bd 2019-05-11 jcs } else {
1217 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1218 230a42bd 2019-05-11 jcs if (base == NULL) {
1219 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1220 230a42bd 2019-05-11 jcs repo_path);
1221 230a42bd 2019-05-11 jcs goto done;
1222 230a42bd 2019-05-11 jcs }
1223 76089277 2018-04-01 stsp }
1224 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1225 c09a553d 2018-03-12 stsp if (dotgit)
1226 c09a553d 2018-03-12 stsp *dotgit = '\0';
1227 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1228 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1229 c09a553d 2018-03-12 stsp free(cwd);
1230 76089277 2018-04-01 stsp goto done;
1231 c09a553d 2018-03-12 stsp }
1232 c09a553d 2018-03-12 stsp free(cwd);
1233 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1234 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1235 76089277 2018-04-01 stsp if (repo_path == NULL) {
1236 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1237 76089277 2018-04-01 stsp goto done;
1238 76089277 2018-04-01 stsp }
1239 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1240 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1241 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1242 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1243 b4b3a7dd 2019-07-22 stsp argv[1]);
1244 b4b3a7dd 2019-07-22 stsp goto done;
1245 b4b3a7dd 2019-07-22 stsp }
1246 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1247 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1248 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1249 b4b3a7dd 2019-07-22 stsp goto done;
1250 b4b3a7dd 2019-07-22 stsp }
1251 76089277 2018-04-01 stsp }
1252 c09a553d 2018-03-12 stsp } else
1253 c09a553d 2018-03-12 stsp usage_checkout();
1254 c09a553d 2018-03-12 stsp
1255 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1256 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1257 13bfb272 2019-05-10 jcs
1258 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1259 c09a553d 2018-03-12 stsp if (error != NULL)
1260 c02c541e 2019-03-29 stsp goto done;
1261 c02c541e 2019-03-29 stsp
1262 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1263 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1264 c530dc23 2019-07-23 stsp if (error) {
1265 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1266 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1267 c530dc23 2019-07-23 stsp goto done;
1268 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1269 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1270 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1271 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1272 c530dc23 2019-07-23 stsp goto done;
1273 c530dc23 2019-07-23 stsp }
1274 c530dc23 2019-07-23 stsp }
1275 c530dc23 2019-07-23 stsp
1276 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1277 c02c541e 2019-03-29 stsp if (error)
1278 c09a553d 2018-03-12 stsp goto done;
1279 8069f636 2019-01-12 stsp
1280 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1281 c09a553d 2018-03-12 stsp if (error != NULL)
1282 c09a553d 2018-03-12 stsp goto done;
1283 c09a553d 2018-03-12 stsp
1284 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1285 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1286 c09a553d 2018-03-12 stsp goto done;
1287 c09a553d 2018-03-12 stsp
1288 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1289 c09a553d 2018-03-12 stsp if (error != NULL)
1290 c09a553d 2018-03-12 stsp goto done;
1291 c09a553d 2018-03-12 stsp
1292 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1293 e5dc7198 2018-12-29 stsp path_prefix);
1294 e5dc7198 2018-12-29 stsp if (error != NULL)
1295 e5dc7198 2018-12-29 stsp goto done;
1296 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1297 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1298 49520a32 2018-12-29 stsp goto done;
1299 49520a32 2018-12-29 stsp }
1300 49520a32 2018-12-29 stsp
1301 8069f636 2019-01-12 stsp if (commit_id_str) {
1302 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1303 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1304 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1305 30837e32 2019-07-25 stsp if (error)
1306 8069f636 2019-01-12 stsp goto done;
1307 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1308 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1309 8069f636 2019-01-12 stsp if (error != NULL) {
1310 8069f636 2019-01-12 stsp free(commit_id);
1311 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1312 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1313 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1314 4b6c9460 2020-03-05 stsp }
1315 8069f636 2019-01-12 stsp goto done;
1316 8069f636 2019-01-12 stsp }
1317 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1318 4b6c9460 2020-03-05 stsp if (error) {
1319 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1320 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1321 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1322 4b6c9460 2020-03-05 stsp }
1323 45d344f6 2019-05-14 stsp goto done;
1324 4b6c9460 2020-03-05 stsp }
1325 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1326 8069f636 2019-01-12 stsp commit_id);
1327 8069f636 2019-01-12 stsp free(commit_id);
1328 8069f636 2019-01-12 stsp if (error)
1329 8069f636 2019-01-12 stsp goto done;
1330 8069f636 2019-01-12 stsp }
1331 8069f636 2019-01-12 stsp
1332 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1333 f2ea84fa 2019-07-27 stsp if (error)
1334 f2ea84fa 2019-07-27 stsp goto done;
1335 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1336 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1337 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1338 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1339 c09a553d 2018-03-12 stsp if (error != NULL)
1340 c09a553d 2018-03-12 stsp goto done;
1341 c09a553d 2018-03-12 stsp
1342 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1343 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1344 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1345 c09a553d 2018-03-12 stsp done:
1346 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1347 8069f636 2019-01-12 stsp free(commit_id_str);
1348 76089277 2018-04-01 stsp free(repo_path);
1349 507dc3bb 2018-12-29 stsp free(worktree_path);
1350 507dc3bb 2018-12-29 stsp return error;
1351 507dc3bb 2018-12-29 stsp }
1352 507dc3bb 2018-12-29 stsp
1353 507dc3bb 2018-12-29 stsp __dead static void
1354 507dc3bb 2018-12-29 stsp usage_update(void)
1355 507dc3bb 2018-12-29 stsp {
1356 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1357 507dc3bb 2018-12-29 stsp getprogname());
1358 507dc3bb 2018-12-29 stsp exit(1);
1359 507dc3bb 2018-12-29 stsp }
1360 507dc3bb 2018-12-29 stsp
1361 1ee397ad 2019-07-12 stsp static const struct got_error *
1362 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1363 507dc3bb 2018-12-29 stsp {
1364 784955db 2019-01-12 stsp int *did_something = arg;
1365 784955db 2019-01-12 stsp
1366 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1367 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1368 1ee397ad 2019-07-12 stsp return NULL;
1369 507dc3bb 2018-12-29 stsp
1370 1545c615 2019-02-10 stsp *did_something = 1;
1371 a484d721 2019-06-10 stsp
1372 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1373 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1374 1ee397ad 2019-07-12 stsp return NULL;
1375 a484d721 2019-06-10 stsp
1376 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1377 507dc3bb 2018-12-29 stsp path++;
1378 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1379 1ee397ad 2019-07-12 stsp return NULL;
1380 be7061eb 2018-12-30 stsp }
1381 be7061eb 2018-12-30 stsp
1382 be7061eb 2018-12-30 stsp static const struct got_error *
1383 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1384 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1385 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1386 a1fb16d8 2019-05-24 stsp {
1387 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1388 a1fb16d8 2019-05-24 stsp char *base_id_str;
1389 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1390 a1fb16d8 2019-05-24 stsp
1391 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1392 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1393 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1394 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1395 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1396 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1397 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1398 a1fb16d8 2019-05-24 stsp }
1399 a1fb16d8 2019-05-24 stsp
1400 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1401 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1402 a1fb16d8 2019-05-24 stsp if (err) {
1403 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1404 a1fb16d8 2019-05-24 stsp return err;
1405 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1406 a1fb16d8 2019-05-24 stsp }
1407 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1408 a1fb16d8 2019-05-24 stsp return NULL;
1409 a1fb16d8 2019-05-24 stsp
1410 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1411 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1412 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1413 a1fb16d8 2019-05-24 stsp if (err)
1414 a1fb16d8 2019-05-24 stsp return err;
1415 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1416 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1417 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1418 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1419 0ebf8283 2019-07-24 stsp return NULL;
1420 0ebf8283 2019-07-24 stsp }
1421 0ebf8283 2019-07-24 stsp
1422 0ebf8283 2019-07-24 stsp static const struct got_error *
1423 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1424 0ebf8283 2019-07-24 stsp {
1425 0ebf8283 2019-07-24 stsp const struct got_error *err;
1426 0ebf8283 2019-07-24 stsp int in_progress;
1427 0ebf8283 2019-07-24 stsp
1428 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1429 0ebf8283 2019-07-24 stsp if (err)
1430 0ebf8283 2019-07-24 stsp return err;
1431 0ebf8283 2019-07-24 stsp if (in_progress)
1432 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1433 0ebf8283 2019-07-24 stsp
1434 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1435 0ebf8283 2019-07-24 stsp if (err)
1436 0ebf8283 2019-07-24 stsp return err;
1437 0ebf8283 2019-07-24 stsp if (in_progress)
1438 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1439 0ebf8283 2019-07-24 stsp
1440 a1fb16d8 2019-05-24 stsp return NULL;
1441 a5edda0a 2019-07-27 stsp }
1442 a5edda0a 2019-07-27 stsp
1443 a5edda0a 2019-07-27 stsp static const struct got_error *
1444 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1445 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1446 a5edda0a 2019-07-27 stsp {
1447 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1448 a5edda0a 2019-07-27 stsp char *path;
1449 a5edda0a 2019-07-27 stsp int i;
1450 a5edda0a 2019-07-27 stsp
1451 a5edda0a 2019-07-27 stsp if (argc == 0) {
1452 a5edda0a 2019-07-27 stsp path = strdup("");
1453 a5edda0a 2019-07-27 stsp if (path == NULL)
1454 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1455 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1456 a5edda0a 2019-07-27 stsp }
1457 a5edda0a 2019-07-27 stsp
1458 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1459 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1460 a5edda0a 2019-07-27 stsp if (err)
1461 a5edda0a 2019-07-27 stsp break;
1462 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1463 a5edda0a 2019-07-27 stsp if (err) {
1464 a5edda0a 2019-07-27 stsp free(path);
1465 a5edda0a 2019-07-27 stsp break;
1466 a5edda0a 2019-07-27 stsp }
1467 a5edda0a 2019-07-27 stsp }
1468 a5edda0a 2019-07-27 stsp
1469 a5edda0a 2019-07-27 stsp return err;
1470 a1fb16d8 2019-05-24 stsp }
1471 a1fb16d8 2019-05-24 stsp
1472 a1fb16d8 2019-05-24 stsp static const struct got_error *
1473 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1474 507dc3bb 2018-12-29 stsp {
1475 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1476 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1477 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1478 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1479 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1480 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1481 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1482 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1483 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1484 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1485 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1486 507dc3bb 2018-12-29 stsp
1487 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1488 f2ea84fa 2019-07-27 stsp
1489 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1490 507dc3bb 2018-12-29 stsp switch (ch) {
1491 024e9686 2019-05-14 stsp case 'b':
1492 024e9686 2019-05-14 stsp branch_name = optarg;
1493 024e9686 2019-05-14 stsp break;
1494 507dc3bb 2018-12-29 stsp case 'c':
1495 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1496 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1497 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1498 507dc3bb 2018-12-29 stsp break;
1499 507dc3bb 2018-12-29 stsp default:
1500 2deda0b9 2019-03-07 stsp usage_update();
1501 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1502 507dc3bb 2018-12-29 stsp }
1503 507dc3bb 2018-12-29 stsp }
1504 507dc3bb 2018-12-29 stsp
1505 507dc3bb 2018-12-29 stsp argc -= optind;
1506 507dc3bb 2018-12-29 stsp argv += optind;
1507 507dc3bb 2018-12-29 stsp
1508 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1509 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1510 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1511 507dc3bb 2018-12-29 stsp err(1, "pledge");
1512 507dc3bb 2018-12-29 stsp #endif
1513 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1514 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1515 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1516 c4cdcb68 2019-04-03 stsp goto done;
1517 c4cdcb68 2019-04-03 stsp }
1518 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1519 7d5807f4 2019-07-11 stsp if (error)
1520 7d5807f4 2019-07-11 stsp goto done;
1521 7d5807f4 2019-07-11 stsp
1522 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1523 f2ea84fa 2019-07-27 stsp if (error)
1524 f2ea84fa 2019-07-27 stsp goto done;
1525 507dc3bb 2018-12-29 stsp
1526 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1527 c9956ddf 2019-09-08 stsp NULL);
1528 507dc3bb 2018-12-29 stsp if (error != NULL)
1529 507dc3bb 2018-12-29 stsp goto done;
1530 507dc3bb 2018-12-29 stsp
1531 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1532 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1533 087fb88c 2019-08-04 stsp if (error)
1534 087fb88c 2019-08-04 stsp goto done;
1535 087fb88c 2019-08-04 stsp
1536 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1537 0266afb7 2019-01-04 stsp if (error)
1538 0266afb7 2019-01-04 stsp goto done;
1539 0266afb7 2019-01-04 stsp
1540 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1541 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1542 024e9686 2019-05-14 stsp if (error != NULL)
1543 024e9686 2019-05-14 stsp goto done;
1544 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1545 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1546 9c4b8182 2019-01-02 stsp if (error != NULL)
1547 9c4b8182 2019-01-02 stsp goto done;
1548 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1549 507dc3bb 2018-12-29 stsp if (error != NULL)
1550 507dc3bb 2018-12-29 stsp goto done;
1551 507dc3bb 2018-12-29 stsp } else {
1552 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1553 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1554 04f57cb3 2019-07-25 stsp free(commit_id_str);
1555 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1556 30837e32 2019-07-25 stsp if (error)
1557 a297e751 2019-07-11 stsp goto done;
1558 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1559 a297e751 2019-07-11 stsp if (error)
1560 507dc3bb 2018-12-29 stsp goto done;
1561 507dc3bb 2018-12-29 stsp }
1562 35c965b2 2018-12-29 stsp
1563 a1fb16d8 2019-05-24 stsp if (branch_name) {
1564 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1565 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1566 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1567 f2ea84fa 2019-07-27 stsp continue;
1568 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1569 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1570 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1571 024e9686 2019-05-14 stsp goto done;
1572 024e9686 2019-05-14 stsp }
1573 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1574 024e9686 2019-05-14 stsp if (error)
1575 024e9686 2019-05-14 stsp goto done;
1576 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1577 3aef623b 2019-10-15 stsp repo);
1578 a367ff0f 2019-05-14 stsp free(head_commit_id);
1579 024e9686 2019-05-14 stsp if (error != NULL)
1580 024e9686 2019-05-14 stsp goto done;
1581 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1582 a367ff0f 2019-05-14 stsp if (error)
1583 a367ff0f 2019-05-14 stsp goto done;
1584 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1585 024e9686 2019-05-14 stsp if (error)
1586 024e9686 2019-05-14 stsp goto done;
1587 024e9686 2019-05-14 stsp } else {
1588 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1589 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1590 a367ff0f 2019-05-14 stsp if (error != NULL) {
1591 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1592 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1593 024e9686 2019-05-14 stsp goto done;
1594 a367ff0f 2019-05-14 stsp }
1595 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1596 a367ff0f 2019-05-14 stsp if (error)
1597 a367ff0f 2019-05-14 stsp goto done;
1598 024e9686 2019-05-14 stsp }
1599 507dc3bb 2018-12-29 stsp
1600 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1601 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1602 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1603 507dc3bb 2018-12-29 stsp commit_id);
1604 507dc3bb 2018-12-29 stsp if (error)
1605 507dc3bb 2018-12-29 stsp goto done;
1606 507dc3bb 2018-12-29 stsp }
1607 507dc3bb 2018-12-29 stsp
1608 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1609 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1610 507dc3bb 2018-12-29 stsp if (error != NULL)
1611 507dc3bb 2018-12-29 stsp goto done;
1612 9c4b8182 2019-01-02 stsp
1613 784955db 2019-01-12 stsp if (did_something)
1614 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1615 784955db 2019-01-12 stsp else
1616 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1617 507dc3bb 2018-12-29 stsp done:
1618 c09a553d 2018-03-12 stsp free(worktree_path);
1619 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1620 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1621 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1622 507dc3bb 2018-12-29 stsp free(commit_id);
1623 9c4b8182 2019-01-02 stsp free(commit_id_str);
1624 c09a553d 2018-03-12 stsp return error;
1625 c09a553d 2018-03-12 stsp }
1626 c09a553d 2018-03-12 stsp
1627 f42b1b34 2018-03-12 stsp static const struct got_error *
1628 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1629 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1630 63035f9f 2019-10-06 stsp struct got_repository *repo)
1631 5c860e29 2018-03-12 stsp {
1632 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1633 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1634 79109fed 2018-03-27 stsp
1635 44392932 2019-08-25 stsp if (blob_id1) {
1636 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1637 44392932 2019-08-25 stsp if (err)
1638 44392932 2019-08-25 stsp goto done;
1639 44392932 2019-08-25 stsp }
1640 79109fed 2018-03-27 stsp
1641 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1642 44392932 2019-08-25 stsp if (err)
1643 44392932 2019-08-25 stsp goto done;
1644 79109fed 2018-03-27 stsp
1645 44392932 2019-08-25 stsp while (path[0] == '/')
1646 44392932 2019-08-25 stsp path++;
1647 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1648 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1649 44392932 2019-08-25 stsp done:
1650 44392932 2019-08-25 stsp if (blob1)
1651 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1652 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1653 44392932 2019-08-25 stsp return err;
1654 44392932 2019-08-25 stsp }
1655 44392932 2019-08-25 stsp
1656 44392932 2019-08-25 stsp static const struct got_error *
1657 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1658 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1659 63035f9f 2019-10-06 stsp struct got_repository *repo)
1660 44392932 2019-08-25 stsp {
1661 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1662 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1663 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1664 44392932 2019-08-25 stsp
1665 44392932 2019-08-25 stsp if (tree_id1) {
1666 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1667 79109fed 2018-03-27 stsp if (err)
1668 44392932 2019-08-25 stsp goto done;
1669 79109fed 2018-03-27 stsp }
1670 79109fed 2018-03-27 stsp
1671 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1672 0f2b3dca 2018-12-22 stsp if (err)
1673 0f2b3dca 2018-12-22 stsp goto done;
1674 0f2b3dca 2018-12-22 stsp
1675 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1676 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1677 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1678 44392932 2019-08-25 stsp while (path[0] == '/')
1679 44392932 2019-08-25 stsp path++;
1680 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1681 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1682 0f2b3dca 2018-12-22 stsp done:
1683 79109fed 2018-03-27 stsp if (tree1)
1684 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1685 366e0a5f 2019-10-10 stsp if (tree2)
1686 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1687 44392932 2019-08-25 stsp return err;
1688 44392932 2019-08-25 stsp }
1689 44392932 2019-08-25 stsp
1690 44392932 2019-08-25 stsp static const struct got_error *
1691 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1692 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1693 44392932 2019-08-25 stsp {
1694 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1695 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1696 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1697 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1698 44392932 2019-08-25 stsp struct got_object_qid *qid;
1699 44392932 2019-08-25 stsp
1700 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1701 44392932 2019-08-25 stsp if (qid != NULL) {
1702 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1703 44392932 2019-08-25 stsp qid->id);
1704 44392932 2019-08-25 stsp if (err)
1705 44392932 2019-08-25 stsp return err;
1706 44392932 2019-08-25 stsp }
1707 44392932 2019-08-25 stsp
1708 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1709 44392932 2019-08-25 stsp int obj_type;
1710 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1711 44392932 2019-08-25 stsp if (err)
1712 44392932 2019-08-25 stsp goto done;
1713 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1714 44392932 2019-08-25 stsp if (err) {
1715 44392932 2019-08-25 stsp free(obj_id2);
1716 44392932 2019-08-25 stsp goto done;
1717 44392932 2019-08-25 stsp }
1718 44392932 2019-08-25 stsp if (pcommit) {
1719 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1720 44392932 2019-08-25 stsp qid->id, path);
1721 44392932 2019-08-25 stsp if (err) {
1722 44392932 2019-08-25 stsp free(obj_id2);
1723 44392932 2019-08-25 stsp goto done;
1724 44392932 2019-08-25 stsp }
1725 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1726 44392932 2019-08-25 stsp if (err) {
1727 44392932 2019-08-25 stsp free(obj_id2);
1728 44392932 2019-08-25 stsp goto done;
1729 44392932 2019-08-25 stsp }
1730 44392932 2019-08-25 stsp }
1731 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1732 44392932 2019-08-25 stsp if (err) {
1733 44392932 2019-08-25 stsp free(obj_id2);
1734 44392932 2019-08-25 stsp goto done;
1735 44392932 2019-08-25 stsp }
1736 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1737 44392932 2019-08-25 stsp switch (obj_type) {
1738 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1739 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1740 63035f9f 2019-10-06 stsp 0, repo);
1741 44392932 2019-08-25 stsp break;
1742 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1743 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1744 63035f9f 2019-10-06 stsp 0, repo);
1745 44392932 2019-08-25 stsp break;
1746 44392932 2019-08-25 stsp default:
1747 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1748 44392932 2019-08-25 stsp break;
1749 44392932 2019-08-25 stsp }
1750 44392932 2019-08-25 stsp free(obj_id1);
1751 44392932 2019-08-25 stsp free(obj_id2);
1752 44392932 2019-08-25 stsp } else {
1753 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1754 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1755 44392932 2019-08-25 stsp if (err)
1756 44392932 2019-08-25 stsp goto done;
1757 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1758 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1759 44392932 2019-08-25 stsp if (err)
1760 44392932 2019-08-25 stsp goto done;
1761 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1762 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1763 44392932 2019-08-25 stsp }
1764 44392932 2019-08-25 stsp done:
1765 0f2b3dca 2018-12-22 stsp free(id_str1);
1766 0f2b3dca 2018-12-22 stsp free(id_str2);
1767 44392932 2019-08-25 stsp if (pcommit)
1768 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1769 79109fed 2018-03-27 stsp return err;
1770 79109fed 2018-03-27 stsp }
1771 79109fed 2018-03-27 stsp
1772 4bb494d5 2018-06-16 stsp static char *
1773 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1774 6c281f94 2018-06-11 stsp {
1775 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1776 09867e48 2019-08-13 stsp char *p, *s;
1777 09867e48 2019-08-13 stsp
1778 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1779 09867e48 2019-08-13 stsp if (tm == NULL)
1780 09867e48 2019-08-13 stsp return NULL;
1781 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1782 09867e48 2019-08-13 stsp if (s == NULL)
1783 09867e48 2019-08-13 stsp return NULL;
1784 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1785 6c281f94 2018-06-11 stsp if (p)
1786 6c281f94 2018-06-11 stsp *p = '\0';
1787 6c281f94 2018-06-11 stsp return s;
1788 6c281f94 2018-06-11 stsp }
1789 dc424a06 2019-08-07 stsp
1790 6841bf13 2019-11-29 kn static const struct got_error *
1791 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1792 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1793 6841bf13 2019-11-29 kn {
1794 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1795 6841bf13 2019-11-29 kn regmatch_t regmatch;
1796 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1797 6841bf13 2019-11-29 kn
1798 6841bf13 2019-11-29 kn *have_match = 0;
1799 6841bf13 2019-11-29 kn
1800 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1801 6841bf13 2019-11-29 kn if (err)
1802 6841bf13 2019-11-29 kn return err;
1803 6841bf13 2019-11-29 kn
1804 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1805 6841bf13 2019-11-29 kn if (err)
1806 6841bf13 2019-11-29 kn goto done;
1807 6841bf13 2019-11-29 kn
1808 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1809 6841bf13 2019-11-29 kn *have_match = 1;
1810 6841bf13 2019-11-29 kn done:
1811 6841bf13 2019-11-29 kn free(id_str);
1812 6841bf13 2019-11-29 kn free(logmsg);
1813 6841bf13 2019-11-29 kn return err;
1814 6841bf13 2019-11-29 kn }
1815 6841bf13 2019-11-29 kn
1816 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1817 6c281f94 2018-06-11 stsp
1818 79109fed 2018-03-27 stsp static const struct got_error *
1819 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1820 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1821 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1822 79109fed 2018-03-27 stsp {
1823 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1824 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1825 6c281f94 2018-06-11 stsp char datebuf[26];
1826 45d799e2 2018-12-23 stsp time_t committer_time;
1827 45d799e2 2018-12-23 stsp const char *author, *committer;
1828 199a4027 2019-02-02 stsp char *refs_str = NULL;
1829 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1830 5c860e29 2018-03-12 stsp
1831 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1832 199a4027 2019-02-02 stsp char *s;
1833 199a4027 2019-02-02 stsp const char *name;
1834 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1835 a436ad14 2019-08-13 stsp int cmp;
1836 a436ad14 2019-08-13 stsp
1837 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1838 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1839 d9498b20 2019-02-05 stsp continue;
1840 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1841 199a4027 2019-02-02 stsp name += 5;
1842 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1843 7143d404 2019-03-12 stsp continue;
1844 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1845 e34f9ed6 2019-02-02 stsp name += 6;
1846 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1847 141c2bff 2019-02-04 stsp name += 8;
1848 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1849 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1850 5d844a1e 2019-08-13 stsp if (err) {
1851 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1852 5d844a1e 2019-08-13 stsp return err;
1853 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1854 5d844a1e 2019-08-13 stsp err = NULL;
1855 5d844a1e 2019-08-13 stsp tag = NULL;
1856 5d844a1e 2019-08-13 stsp }
1857 a436ad14 2019-08-13 stsp }
1858 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1859 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1860 a436ad14 2019-08-13 stsp if (tag)
1861 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1862 a436ad14 2019-08-13 stsp if (cmp != 0)
1863 a436ad14 2019-08-13 stsp continue;
1864 199a4027 2019-02-02 stsp s = refs_str;
1865 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1866 199a4027 2019-02-02 stsp name) == -1) {
1867 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1868 199a4027 2019-02-02 stsp free(s);
1869 34ca4898 2019-08-13 stsp return err;
1870 199a4027 2019-02-02 stsp }
1871 199a4027 2019-02-02 stsp free(s);
1872 199a4027 2019-02-02 stsp }
1873 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1874 8bf5b3c9 2018-03-17 stsp if (err)
1875 8bf5b3c9 2018-03-17 stsp return err;
1876 788c352e 2018-06-16 stsp
1877 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1878 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1879 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1880 832c249c 2018-06-10 stsp free(id_str);
1881 d3d493d7 2019-02-21 stsp id_str = NULL;
1882 d3d493d7 2019-02-21 stsp free(refs_str);
1883 d3d493d7 2019-02-21 stsp refs_str = NULL;
1884 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1885 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1886 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1887 09867e48 2019-08-13 stsp if (datestr)
1888 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1889 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1890 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1891 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1892 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1893 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1894 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1895 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1896 3fe1abad 2018-06-10 stsp int n = 1;
1897 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1898 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1899 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1900 a0603db2 2018-06-10 stsp if (err)
1901 a0603db2 2018-06-10 stsp return err;
1902 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1903 a0603db2 2018-06-10 stsp free(id_str);
1904 a0603db2 2018-06-10 stsp }
1905 a0603db2 2018-06-10 stsp }
1906 832c249c 2018-06-10 stsp
1907 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1908 5943eee2 2019-08-13 stsp if (err)
1909 5943eee2 2019-08-13 stsp return err;
1910 8bf5b3c9 2018-03-17 stsp
1911 621015ac 2018-07-23 stsp logmsg = logmsg0;
1912 832c249c 2018-06-10 stsp do {
1913 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1914 832c249c 2018-06-10 stsp if (line)
1915 832c249c 2018-06-10 stsp printf(" %s\n", line);
1916 832c249c 2018-06-10 stsp } while (line);
1917 621015ac 2018-07-23 stsp free(logmsg0);
1918 832c249c 2018-06-10 stsp
1919 971751ac 2018-03-27 stsp if (show_patch) {
1920 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1921 971751ac 2018-03-27 stsp if (err == 0)
1922 971751ac 2018-03-27 stsp printf("\n");
1923 971751ac 2018-03-27 stsp }
1924 07862c20 2018-09-15 stsp
1925 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1926 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1927 07862c20 2018-09-15 stsp return err;
1928 f42b1b34 2018-03-12 stsp }
1929 5c860e29 2018-03-12 stsp
1930 f42b1b34 2018-03-12 stsp static const struct got_error *
1931 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1932 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1933 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1934 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1935 f42b1b34 2018-03-12 stsp {
1936 f42b1b34 2018-03-12 stsp const struct got_error *err;
1937 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1938 6841bf13 2019-11-29 kn regex_t regex;
1939 6841bf13 2019-11-29 kn int have_match;
1940 372ccdbb 2018-06-10 stsp
1941 6841bf13 2019-11-29 kn if (search_pattern &&
1942 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1943 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1944 6841bf13 2019-11-29 kn
1945 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1946 f42b1b34 2018-03-12 stsp if (err)
1947 f42b1b34 2018-03-12 stsp return err;
1948 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1949 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1950 372ccdbb 2018-06-10 stsp if (err)
1951 fcc85cad 2018-07-22 stsp goto done;
1952 656b1f76 2019-05-11 jcs for (;;) {
1953 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1954 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1955 8bf5b3c9 2018-03-17 stsp
1956 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1957 84453469 2018-11-11 stsp break;
1958 84453469 2018-11-11 stsp
1959 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1960 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1961 372ccdbb 2018-06-10 stsp if (err) {
1962 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1963 9ba79e04 2018-06-11 stsp err = NULL;
1964 ee780d5c 2020-01-04 stsp break;
1965 7e665116 2018-04-02 stsp }
1966 b43fbaa0 2018-06-11 stsp if (id == NULL)
1967 7e665116 2018-04-02 stsp break;
1968 b43fbaa0 2018-06-11 stsp
1969 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1970 b43fbaa0 2018-06-11 stsp if (err)
1971 fcc85cad 2018-07-22 stsp break;
1972 6841bf13 2019-11-29 kn
1973 6841bf13 2019-11-29 kn if (search_pattern) {
1974 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1975 6841bf13 2019-11-29 kn if (err) {
1976 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1977 6841bf13 2019-11-29 kn break;
1978 6841bf13 2019-11-29 kn }
1979 6841bf13 2019-11-29 kn if (have_match == 0) {
1980 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1981 6841bf13 2019-11-29 kn continue;
1982 6841bf13 2019-11-29 kn }
1983 6841bf13 2019-11-29 kn }
1984 6841bf13 2019-11-29 kn
1985 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1986 44392932 2019-08-25 stsp diff_context, refs);
1987 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1988 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1989 7e665116 2018-04-02 stsp break;
1990 31cedeaf 2018-09-15 stsp }
1991 fcc85cad 2018-07-22 stsp done:
1992 6841bf13 2019-11-29 kn if (search_pattern)
1993 6841bf13 2019-11-29 kn regfree(&regex);
1994 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1995 f42b1b34 2018-03-12 stsp return err;
1996 f42b1b34 2018-03-12 stsp }
1997 5c860e29 2018-03-12 stsp
1998 4ed7e80c 2018-05-20 stsp __dead static void
1999 6f3d1eb0 2018-03-12 stsp usage_log(void)
2000 6f3d1eb0 2018-03-12 stsp {
2001 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2002 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2003 6f3d1eb0 2018-03-12 stsp exit(1);
2004 b1ebc001 2019-08-13 stsp }
2005 b1ebc001 2019-08-13 stsp
2006 b1ebc001 2019-08-13 stsp static int
2007 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2008 b1ebc001 2019-08-13 stsp {
2009 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2010 b1ebc001 2019-08-13 stsp long long n;
2011 b1ebc001 2019-08-13 stsp const char *errstr;
2012 b1ebc001 2019-08-13 stsp
2013 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2014 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2015 b1ebc001 2019-08-13 stsp return 0;
2016 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2017 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2018 b1ebc001 2019-08-13 stsp return 0;
2019 b1ebc001 2019-08-13 stsp return n;
2020 6f3d1eb0 2018-03-12 stsp }
2021 6f3d1eb0 2018-03-12 stsp
2022 4ed7e80c 2018-05-20 stsp static const struct got_error *
2023 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2024 f42b1b34 2018-03-12 stsp {
2025 f42b1b34 2018-03-12 stsp const struct got_error *error;
2026 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2027 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2028 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2029 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2030 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2031 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2032 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2033 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2034 64a96a6d 2018-04-01 stsp const char *errstr;
2035 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2036 1b3893a2 2019-03-18 stsp
2037 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2038 5c860e29 2018-03-12 stsp
2039 6715a751 2018-03-16 stsp #ifndef PROFILE
2040 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2041 6098196c 2019-01-04 stsp NULL)
2042 ad242220 2018-09-08 stsp == -1)
2043 f42b1b34 2018-03-12 stsp err(1, "pledge");
2044 6715a751 2018-03-16 stsp #endif
2045 79109fed 2018-03-27 stsp
2046 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2047 b1ebc001 2019-08-13 stsp
2048 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2049 79109fed 2018-03-27 stsp switch (ch) {
2050 79109fed 2018-03-27 stsp case 'p':
2051 79109fed 2018-03-27 stsp show_patch = 1;
2052 d142fc45 2018-04-01 stsp break;
2053 d142fc45 2018-04-01 stsp case 'c':
2054 d142fc45 2018-04-01 stsp start_commit = optarg;
2055 64a96a6d 2018-04-01 stsp break;
2056 c0cc5c62 2018-10-18 stsp case 'C':
2057 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2058 4a8520aa 2018-10-18 stsp &errstr);
2059 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2060 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2061 c0cc5c62 2018-10-18 stsp break;
2062 64a96a6d 2018-04-01 stsp case 'l':
2063 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2064 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2065 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2066 cc54c501 2019-07-15 stsp break;
2067 48c8c60d 2020-01-27 stsp case 'b':
2068 48c8c60d 2020-01-27 stsp log_branches = 1;
2069 a0603db2 2018-06-10 stsp break;
2070 04ca23f4 2018-07-16 stsp case 'r':
2071 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2072 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2073 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2074 9ba1d308 2019-10-21 stsp optarg);
2075 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2076 04ca23f4 2018-07-16 stsp break;
2077 6841bf13 2019-11-29 kn case 's':
2078 6841bf13 2019-11-29 kn search_pattern = optarg;
2079 6841bf13 2019-11-29 kn break;
2080 79109fed 2018-03-27 stsp default:
2081 2deda0b9 2019-03-07 stsp usage_log();
2082 79109fed 2018-03-27 stsp /* NOTREACHED */
2083 79109fed 2018-03-27 stsp }
2084 79109fed 2018-03-27 stsp }
2085 79109fed 2018-03-27 stsp
2086 79109fed 2018-03-27 stsp argc -= optind;
2087 79109fed 2018-03-27 stsp argv += optind;
2088 f42b1b34 2018-03-12 stsp
2089 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2090 dc1edbfa 2019-11-29 kn diff_context = 3;
2091 dc1edbfa 2019-11-29 kn else if (!show_patch)
2092 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2093 dc1edbfa 2019-11-29 kn
2094 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2095 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2096 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2097 04ca23f4 2018-07-16 stsp goto done;
2098 04ca23f4 2018-07-16 stsp }
2099 cffc0aa4 2019-02-05 stsp
2100 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2101 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2102 cffc0aa4 2019-02-05 stsp goto done;
2103 cffc0aa4 2019-02-05 stsp error = NULL;
2104 cffc0aa4 2019-02-05 stsp
2105 e7301579 2019-03-18 stsp if (argc == 0) {
2106 cbd1af7a 2019-03-18 stsp path = strdup("");
2107 e7301579 2019-03-18 stsp if (path == NULL) {
2108 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2109 cbd1af7a 2019-03-18 stsp goto done;
2110 e7301579 2019-03-18 stsp }
2111 e7301579 2019-03-18 stsp } else if (argc == 1) {
2112 e7301579 2019-03-18 stsp if (worktree) {
2113 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2114 e7301579 2019-03-18 stsp argv[0]);
2115 e7301579 2019-03-18 stsp if (error)
2116 e7301579 2019-03-18 stsp goto done;
2117 e7301579 2019-03-18 stsp } else {
2118 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2119 e7301579 2019-03-18 stsp if (path == NULL) {
2120 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2121 e7301579 2019-03-18 stsp goto done;
2122 e7301579 2019-03-18 stsp }
2123 e7301579 2019-03-18 stsp }
2124 cbd1af7a 2019-03-18 stsp } else
2125 cbd1af7a 2019-03-18 stsp usage_log();
2126 cbd1af7a 2019-03-18 stsp
2127 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2128 5486daa2 2019-05-11 stsp repo_path = worktree ?
2129 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2130 5486daa2 2019-05-11 stsp }
2131 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2132 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2133 cffc0aa4 2019-02-05 stsp goto done;
2134 04ca23f4 2018-07-16 stsp }
2135 6098196c 2019-01-04 stsp
2136 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2137 d7d4f210 2018-03-12 stsp if (error != NULL)
2138 04ca23f4 2018-07-16 stsp goto done;
2139 f42b1b34 2018-03-12 stsp
2140 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2141 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2142 c02c541e 2019-03-29 stsp if (error)
2143 c02c541e 2019-03-29 stsp goto done;
2144 c02c541e 2019-03-29 stsp
2145 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2146 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2147 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2148 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2149 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2150 3235492e 2018-04-01 stsp if (error != NULL)
2151 3235492e 2018-04-01 stsp return error;
2152 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2153 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2154 3235492e 2018-04-01 stsp if (error != NULL)
2155 3235492e 2018-04-01 stsp return error;
2156 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2157 3235492e 2018-04-01 stsp } else {
2158 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2159 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2160 3235492e 2018-04-01 stsp if (error == NULL) {
2161 1caad61b 2019-02-01 stsp int obj_type;
2162 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2163 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2164 d1f2edc9 2018-06-13 stsp if (error != NULL)
2165 1caad61b 2019-02-01 stsp goto done;
2166 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2167 1caad61b 2019-02-01 stsp if (error != NULL)
2168 1caad61b 2019-02-01 stsp goto done;
2169 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2170 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2171 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2172 1caad61b 2019-02-01 stsp if (error != NULL)
2173 1caad61b 2019-02-01 stsp goto done;
2174 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2175 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2176 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2177 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2178 1caad61b 2019-02-01 stsp goto done;
2179 1caad61b 2019-02-01 stsp }
2180 1caad61b 2019-02-01 stsp free(id);
2181 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2182 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2183 1caad61b 2019-02-01 stsp if (id == NULL)
2184 638f9024 2019-05-13 stsp error = got_error_from_errno(
2185 230a42bd 2019-05-11 jcs "got_object_id_dup");
2186 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2187 1caad61b 2019-02-01 stsp if (error)
2188 1caad61b 2019-02-01 stsp goto done;
2189 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2190 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2191 1caad61b 2019-02-01 stsp goto done;
2192 1caad61b 2019-02-01 stsp }
2193 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2194 d1f2edc9 2018-06-13 stsp if (error != NULL)
2195 1caad61b 2019-02-01 stsp goto done;
2196 d1f2edc9 2018-06-13 stsp }
2197 15a94983 2018-12-23 stsp if (commit == NULL) {
2198 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2199 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2200 d1f2edc9 2018-06-13 stsp if (error != NULL)
2201 d1f2edc9 2018-06-13 stsp return error;
2202 3235492e 2018-04-01 stsp }
2203 3235492e 2018-04-01 stsp }
2204 d7d4f210 2018-03-12 stsp if (error != NULL)
2205 04ca23f4 2018-07-16 stsp goto done;
2206 04ca23f4 2018-07-16 stsp
2207 deeabeae 2019-08-27 stsp if (worktree) {
2208 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2209 deeabeae 2019-08-27 stsp char *p;
2210 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2211 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2212 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2213 deeabeae 2019-08-27 stsp goto done;
2214 deeabeae 2019-08-27 stsp }
2215 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2216 deeabeae 2019-08-27 stsp free(p);
2217 deeabeae 2019-08-27 stsp } else
2218 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2219 04ca23f4 2018-07-16 stsp if (error != NULL)
2220 04ca23f4 2018-07-16 stsp goto done;
2221 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2222 04ca23f4 2018-07-16 stsp free(path);
2223 04ca23f4 2018-07-16 stsp path = in_repo_path;
2224 04ca23f4 2018-07-16 stsp }
2225 199a4027 2019-02-02 stsp
2226 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2227 199a4027 2019-02-02 stsp if (error)
2228 199a4027 2019-02-02 stsp goto done;
2229 04ca23f4 2018-07-16 stsp
2230 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2231 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2232 04ca23f4 2018-07-16 stsp done:
2233 04ca23f4 2018-07-16 stsp free(path);
2234 04ca23f4 2018-07-16 stsp free(repo_path);
2235 04ca23f4 2018-07-16 stsp free(cwd);
2236 f42b1b34 2018-03-12 stsp free(id);
2237 cffc0aa4 2019-02-05 stsp if (worktree)
2238 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2239 ad242220 2018-09-08 stsp if (repo) {
2240 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2241 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2242 ad242220 2018-09-08 stsp if (error == NULL)
2243 ad242220 2018-09-08 stsp error = repo_error;
2244 ad242220 2018-09-08 stsp }
2245 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2246 8bf5b3c9 2018-03-17 stsp return error;
2247 5c860e29 2018-03-12 stsp }
2248 5c860e29 2018-03-12 stsp
2249 4ed7e80c 2018-05-20 stsp __dead static void
2250 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2251 3f8b7d6a 2018-04-01 stsp {
2252 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2253 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2254 3f8b7d6a 2018-04-01 stsp exit(1);
2255 b00d56cd 2018-04-01 stsp }
2256 b00d56cd 2018-04-01 stsp
2257 b72f483a 2019-02-05 stsp struct print_diff_arg {
2258 b72f483a 2019-02-05 stsp struct got_repository *repo;
2259 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2260 b72f483a 2019-02-05 stsp int diff_context;
2261 3fc0c068 2019-02-10 stsp const char *id_str;
2262 3fc0c068 2019-02-10 stsp int header_shown;
2263 98eaaa12 2019-08-03 stsp int diff_staged;
2264 63035f9f 2019-10-06 stsp int ignore_whitespace;
2265 b72f483a 2019-02-05 stsp };
2266 b72f483a 2019-02-05 stsp
2267 4ed7e80c 2018-05-20 stsp static const struct got_error *
2268 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2269 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2270 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2271 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2272 b72f483a 2019-02-05 stsp {
2273 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2274 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2275 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2276 12463d8b 2019-12-13 stsp int fd = -1;
2277 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2278 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2279 b72f483a 2019-02-05 stsp struct stat sb;
2280 b72f483a 2019-02-05 stsp
2281 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2282 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2283 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2284 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2285 98eaaa12 2019-08-03 stsp return NULL;
2286 98eaaa12 2019-08-03 stsp } else {
2287 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2288 98eaaa12 2019-08-03 stsp return NULL;
2289 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2290 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2291 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2292 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2293 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2294 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2295 98eaaa12 2019-08-03 stsp return NULL;
2296 98eaaa12 2019-08-03 stsp }
2297 3fc0c068 2019-02-10 stsp
2298 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2299 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2300 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2301 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2302 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2303 3fc0c068 2019-02-10 stsp }
2304 b72f483a 2019-02-05 stsp
2305 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2306 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2307 98eaaa12 2019-08-03 stsp switch (staged_status) {
2308 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2309 98eaaa12 2019-08-03 stsp label1 = path;
2310 98eaaa12 2019-08-03 stsp label2 = path;
2311 98eaaa12 2019-08-03 stsp break;
2312 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2313 98eaaa12 2019-08-03 stsp label2 = path;
2314 98eaaa12 2019-08-03 stsp break;
2315 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2316 98eaaa12 2019-08-03 stsp label1 = path;
2317 98eaaa12 2019-08-03 stsp break;
2318 98eaaa12 2019-08-03 stsp default:
2319 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2320 98eaaa12 2019-08-03 stsp }
2321 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2322 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2323 63035f9f 2019-10-06 stsp a->repo, stdout);
2324 98eaaa12 2019-08-03 stsp }
2325 98eaaa12 2019-08-03 stsp
2326 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2327 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2328 4ce46740 2019-08-08 stsp char *id_str;
2329 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2330 408b4ebc 2019-08-03 stsp 8192);
2331 4ce46740 2019-08-08 stsp if (err)
2332 4ce46740 2019-08-08 stsp goto done;
2333 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2334 4ce46740 2019-08-08 stsp if (err)
2335 4ce46740 2019-08-08 stsp goto done;
2336 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2337 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2338 4ce46740 2019-08-08 stsp free(id_str);
2339 4ce46740 2019-08-08 stsp goto done;
2340 4ce46740 2019-08-08 stsp }
2341 4ce46740 2019-08-08 stsp free(id_str);
2342 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2343 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2344 4ce46740 2019-08-08 stsp if (err)
2345 4ce46740 2019-08-08 stsp goto done;
2346 4ce46740 2019-08-08 stsp }
2347 d00136be 2019-03-26 stsp
2348 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2349 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2350 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2352 2ec1f75b 2019-03-26 stsp goto done;
2353 2ec1f75b 2019-03-26 stsp }
2354 b72f483a 2019-02-05 stsp
2355 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2356 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2357 12463d8b 2019-12-13 stsp if (fd == -1) {
2358 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2359 12463d8b 2019-12-13 stsp goto done;
2360 12463d8b 2019-12-13 stsp }
2361 12463d8b 2019-12-13 stsp } else {
2362 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2363 12463d8b 2019-12-13 stsp if (fd == -1) {
2364 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2365 12463d8b 2019-12-13 stsp goto done;
2366 12463d8b 2019-12-13 stsp }
2367 12463d8b 2019-12-13 stsp }
2368 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2369 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2370 2ec1f75b 2019-03-26 stsp goto done;
2371 2ec1f75b 2019-03-26 stsp }
2372 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2373 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2374 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2375 2ec1f75b 2019-03-26 stsp goto done;
2376 2ec1f75b 2019-03-26 stsp }
2377 12463d8b 2019-12-13 stsp fd = -1;
2378 2ec1f75b 2019-03-26 stsp } else
2379 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2380 b72f483a 2019-02-05 stsp
2381 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2382 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2383 b72f483a 2019-02-05 stsp done:
2384 b72f483a 2019-02-05 stsp if (blob1)
2385 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2386 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2387 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2388 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2389 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2390 b72f483a 2019-02-05 stsp free(abspath);
2391 927df6b7 2019-02-10 stsp return err;
2392 927df6b7 2019-02-10 stsp }
2393 927df6b7 2019-02-10 stsp
2394 927df6b7 2019-02-10 stsp static const struct got_error *
2395 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2396 b00d56cd 2018-04-01 stsp {
2397 b00d56cd 2018-04-01 stsp const struct got_error *error;
2398 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2399 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2400 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2401 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2402 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2403 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2404 15a94983 2018-12-23 stsp int type1, type2;
2405 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2406 c0cc5c62 2018-10-18 stsp const char *errstr;
2407 927df6b7 2019-02-10 stsp char *path = NULL;
2408 b00d56cd 2018-04-01 stsp
2409 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2410 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2411 25eccc22 2019-01-04 stsp NULL) == -1)
2412 b00d56cd 2018-04-01 stsp err(1, "pledge");
2413 b00d56cd 2018-04-01 stsp #endif
2414 b00d56cd 2018-04-01 stsp
2415 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2416 b00d56cd 2018-04-01 stsp switch (ch) {
2417 c0cc5c62 2018-10-18 stsp case 'C':
2418 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2419 dfcab68b 2019-11-29 kn &errstr);
2420 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2421 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2422 c0cc5c62 2018-10-18 stsp break;
2423 b72f483a 2019-02-05 stsp case 'r':
2424 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2425 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2426 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2427 9ba1d308 2019-10-21 stsp optarg);
2428 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2429 b72f483a 2019-02-05 stsp break;
2430 98eaaa12 2019-08-03 stsp case 's':
2431 98eaaa12 2019-08-03 stsp diff_staged = 1;
2432 63035f9f 2019-10-06 stsp break;
2433 63035f9f 2019-10-06 stsp case 'w':
2434 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2435 98eaaa12 2019-08-03 stsp break;
2436 b00d56cd 2018-04-01 stsp default:
2437 2deda0b9 2019-03-07 stsp usage_diff();
2438 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2439 b00d56cd 2018-04-01 stsp }
2440 b00d56cd 2018-04-01 stsp }
2441 b00d56cd 2018-04-01 stsp
2442 b00d56cd 2018-04-01 stsp argc -= optind;
2443 b00d56cd 2018-04-01 stsp argv += optind;
2444 b00d56cd 2018-04-01 stsp
2445 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2446 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2447 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2448 b72f483a 2019-02-05 stsp goto done;
2449 b72f483a 2019-02-05 stsp }
2450 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2451 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2452 927df6b7 2019-02-10 stsp goto done;
2453 927df6b7 2019-02-10 stsp if (argc <= 1) {
2454 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2455 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2456 927df6b7 2019-02-10 stsp goto done;
2457 927df6b7 2019-02-10 stsp }
2458 b72f483a 2019-02-05 stsp if (repo_path)
2459 b72f483a 2019-02-05 stsp errx(1,
2460 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2461 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2462 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2463 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2464 927df6b7 2019-02-10 stsp goto done;
2465 927df6b7 2019-02-10 stsp }
2466 927df6b7 2019-02-10 stsp if (argc == 1) {
2467 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2468 cbd1af7a 2019-03-18 stsp argv[0]);
2469 927df6b7 2019-02-10 stsp if (error)
2470 927df6b7 2019-02-10 stsp goto done;
2471 927df6b7 2019-02-10 stsp } else {
2472 927df6b7 2019-02-10 stsp path = strdup("");
2473 927df6b7 2019-02-10 stsp if (path == NULL) {
2474 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2475 927df6b7 2019-02-10 stsp goto done;
2476 927df6b7 2019-02-10 stsp }
2477 927df6b7 2019-02-10 stsp }
2478 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2479 98eaaa12 2019-08-03 stsp if (diff_staged)
2480 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2481 98eaaa12 2019-08-03 stsp "objects in repository");
2482 15a94983 2018-12-23 stsp id_str1 = argv[0];
2483 15a94983 2018-12-23 stsp id_str2 = argv[1];
2484 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2485 30db809c 2019-06-05 stsp repo_path =
2486 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2487 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2488 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2489 30db809c 2019-06-05 stsp goto done;
2490 30db809c 2019-06-05 stsp }
2491 30db809c 2019-06-05 stsp }
2492 b00d56cd 2018-04-01 stsp } else
2493 b00d56cd 2018-04-01 stsp usage_diff();
2494 25eccc22 2019-01-04 stsp
2495 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2496 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2497 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2498 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2499 b72f483a 2019-02-05 stsp }
2500 b00d56cd 2018-04-01 stsp
2501 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2502 76089277 2018-04-01 stsp free(repo_path);
2503 b00d56cd 2018-04-01 stsp if (error != NULL)
2504 b00d56cd 2018-04-01 stsp goto done;
2505 b00d56cd 2018-04-01 stsp
2506 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2507 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2508 c02c541e 2019-03-29 stsp if (error)
2509 c02c541e 2019-03-29 stsp goto done;
2510 c02c541e 2019-03-29 stsp
2511 30db809c 2019-06-05 stsp if (argc <= 1) {
2512 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2513 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2514 b72f483a 2019-02-05 stsp char *id_str;
2515 72ea6654 2019-07-27 stsp
2516 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2517 72ea6654 2019-07-27 stsp
2518 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2519 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2520 b72f483a 2019-02-05 stsp if (error)
2521 b72f483a 2019-02-05 stsp goto done;
2522 b72f483a 2019-02-05 stsp arg.repo = repo;
2523 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2524 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2525 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2526 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2527 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2528 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2529 b72f483a 2019-02-05 stsp
2530 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2531 72ea6654 2019-07-27 stsp if (error)
2532 72ea6654 2019-07-27 stsp goto done;
2533 72ea6654 2019-07-27 stsp
2534 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2535 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2536 b72f483a 2019-02-05 stsp free(id_str);
2537 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2538 b72f483a 2019-02-05 stsp goto done;
2539 b72f483a 2019-02-05 stsp }
2540 b72f483a 2019-02-05 stsp
2541 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2542 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2543 0adb7196 2019-08-11 stsp if (error)
2544 0adb7196 2019-08-11 stsp goto done;
2545 b00d56cd 2018-04-01 stsp
2546 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2547 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2548 0adb7196 2019-08-11 stsp if (error)
2549 0adb7196 2019-08-11 stsp goto done;
2550 b00d56cd 2018-04-01 stsp
2551 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2552 15a94983 2018-12-23 stsp if (error)
2553 15a94983 2018-12-23 stsp goto done;
2554 15a94983 2018-12-23 stsp
2555 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2556 15a94983 2018-12-23 stsp if (error)
2557 15a94983 2018-12-23 stsp goto done;
2558 15a94983 2018-12-23 stsp
2559 15a94983 2018-12-23 stsp if (type1 != type2) {
2560 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2561 b00d56cd 2018-04-01 stsp goto done;
2562 b00d56cd 2018-04-01 stsp }
2563 b00d56cd 2018-04-01 stsp
2564 15a94983 2018-12-23 stsp switch (type1) {
2565 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2566 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2567 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2568 b00d56cd 2018-04-01 stsp break;
2569 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2570 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2571 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2572 b00d56cd 2018-04-01 stsp break;
2573 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2574 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2575 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2576 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2577 b00d56cd 2018-04-01 stsp break;
2578 b00d56cd 2018-04-01 stsp default:
2579 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2580 b00d56cd 2018-04-01 stsp }
2581 b00d56cd 2018-04-01 stsp done:
2582 5e70831e 2019-05-28 stsp free(label1);
2583 5e70831e 2019-05-28 stsp free(label2);
2584 15a94983 2018-12-23 stsp free(id1);
2585 15a94983 2018-12-23 stsp free(id2);
2586 927df6b7 2019-02-10 stsp free(path);
2587 b72f483a 2019-02-05 stsp if (worktree)
2588 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2589 ad242220 2018-09-08 stsp if (repo) {
2590 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2591 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2592 ad242220 2018-09-08 stsp if (error == NULL)
2593 ad242220 2018-09-08 stsp error = repo_error;
2594 ad242220 2018-09-08 stsp }
2595 b00d56cd 2018-04-01 stsp return error;
2596 404c43c4 2018-06-21 stsp }
2597 404c43c4 2018-06-21 stsp
2598 404c43c4 2018-06-21 stsp __dead static void
2599 404c43c4 2018-06-21 stsp usage_blame(void)
2600 404c43c4 2018-06-21 stsp {
2601 9270e621 2019-02-05 stsp fprintf(stderr,
2602 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2603 404c43c4 2018-06-21 stsp getprogname());
2604 404c43c4 2018-06-21 stsp exit(1);
2605 b00d56cd 2018-04-01 stsp }
2606 b00d56cd 2018-04-01 stsp
2607 28315671 2019-08-14 stsp struct blame_line {
2608 28315671 2019-08-14 stsp int annotated;
2609 28315671 2019-08-14 stsp char *id_str;
2610 82f6abb8 2019-08-14 stsp char *committer;
2611 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2612 28315671 2019-08-14 stsp };
2613 28315671 2019-08-14 stsp
2614 28315671 2019-08-14 stsp struct blame_cb_args {
2615 28315671 2019-08-14 stsp struct blame_line *lines;
2616 28315671 2019-08-14 stsp int nlines;
2617 7ef28ff8 2019-08-14 stsp int nlines_prec;
2618 28315671 2019-08-14 stsp int lineno_cur;
2619 28315671 2019-08-14 stsp off_t *line_offsets;
2620 28315671 2019-08-14 stsp FILE *f;
2621 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2622 28315671 2019-08-14 stsp };
2623 28315671 2019-08-14 stsp
2624 404c43c4 2018-06-21 stsp static const struct got_error *
2625 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2626 28315671 2019-08-14 stsp {
2627 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2628 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2629 28315671 2019-08-14 stsp struct blame_line *bline;
2630 82f6abb8 2019-08-14 stsp char *line = NULL;
2631 28315671 2019-08-14 stsp size_t linesize = 0;
2632 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2633 28315671 2019-08-14 stsp off_t offset;
2634 bcb49d15 2019-08-14 stsp struct tm tm;
2635 bcb49d15 2019-08-14 stsp time_t committer_time;
2636 28315671 2019-08-14 stsp
2637 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2638 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2639 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2640 28315671 2019-08-14 stsp
2641 28315671 2019-08-14 stsp if (sigint_received)
2642 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2643 28315671 2019-08-14 stsp
2644 2839f8b9 2019-08-18 stsp if (lineno == -1)
2645 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2646 2839f8b9 2019-08-18 stsp
2647 28315671 2019-08-14 stsp /* Annotate this line. */
2648 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2649 28315671 2019-08-14 stsp if (bline->annotated)
2650 28315671 2019-08-14 stsp return NULL;
2651 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2652 28315671 2019-08-14 stsp if (err)
2653 28315671 2019-08-14 stsp return err;
2654 82f6abb8 2019-08-14 stsp
2655 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2656 82f6abb8 2019-08-14 stsp if (err)
2657 82f6abb8 2019-08-14 stsp goto done;
2658 82f6abb8 2019-08-14 stsp
2659 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2660 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2661 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2662 bcb49d15 2019-08-14 stsp goto done;
2663 bcb49d15 2019-08-14 stsp }
2664 bcb49d15 2019-08-14 stsp
2665 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2666 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2667 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2668 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2669 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2670 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2671 82f6abb8 2019-08-14 stsp goto done;
2672 82f6abb8 2019-08-14 stsp }
2673 28315671 2019-08-14 stsp bline->annotated = 1;
2674 28315671 2019-08-14 stsp
2675 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2676 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2677 28315671 2019-08-14 stsp if (!bline->annotated)
2678 82f6abb8 2019-08-14 stsp goto done;
2679 28315671 2019-08-14 stsp
2680 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2681 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2682 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2683 82f6abb8 2019-08-14 stsp goto done;
2684 82f6abb8 2019-08-14 stsp }
2685 28315671 2019-08-14 stsp
2686 28315671 2019-08-14 stsp while (bline->annotated) {
2687 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2688 82f6abb8 2019-08-14 stsp size_t len;
2689 82f6abb8 2019-08-14 stsp
2690 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2691 28315671 2019-08-14 stsp if (ferror(a->f))
2692 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2693 28315671 2019-08-14 stsp break;
2694 28315671 2019-08-14 stsp }
2695 82f6abb8 2019-08-14 stsp
2696 82f6abb8 2019-08-14 stsp committer = bline->committer;
2697 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2698 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2699 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2700 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2701 82f6abb8 2019-08-14 stsp if (at)
2702 82f6abb8 2019-08-14 stsp *at = '\0';
2703 82f6abb8 2019-08-14 stsp len = strlen(committer);
2704 82f6abb8 2019-08-14 stsp if (len >= 9)
2705 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2706 28315671 2019-08-14 stsp
2707 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2708 28315671 2019-08-14 stsp if (nl)
2709 28315671 2019-08-14 stsp *nl = '\0';
2710 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2711 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2712 28315671 2019-08-14 stsp
2713 28315671 2019-08-14 stsp a->lineno_cur++;
2714 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2715 28315671 2019-08-14 stsp }
2716 82f6abb8 2019-08-14 stsp done:
2717 82f6abb8 2019-08-14 stsp if (commit)
2718 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2719 28315671 2019-08-14 stsp free(line);
2720 28315671 2019-08-14 stsp return err;
2721 28315671 2019-08-14 stsp }
2722 28315671 2019-08-14 stsp
2723 28315671 2019-08-14 stsp static const struct got_error *
2724 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2725 404c43c4 2018-06-21 stsp {
2726 404c43c4 2018-06-21 stsp const struct got_error *error;
2727 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2728 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2729 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2730 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2731 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2732 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2733 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2734 28315671 2019-08-14 stsp struct blame_cb_args bca;
2735 28315671 2019-08-14 stsp int ch, obj_type, i;
2736 b02560ec 2019-08-19 stsp size_t filesize;
2737 90f3c347 2019-08-19 stsp
2738 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2739 404c43c4 2018-06-21 stsp
2740 404c43c4 2018-06-21 stsp #ifndef PROFILE
2741 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2742 36e2fb66 2019-01-04 stsp NULL) == -1)
2743 404c43c4 2018-06-21 stsp err(1, "pledge");
2744 404c43c4 2018-06-21 stsp #endif
2745 404c43c4 2018-06-21 stsp
2746 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2747 404c43c4 2018-06-21 stsp switch (ch) {
2748 404c43c4 2018-06-21 stsp case 'c':
2749 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2750 404c43c4 2018-06-21 stsp break;
2751 66bea077 2018-08-02 stsp case 'r':
2752 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2753 66bea077 2018-08-02 stsp if (repo_path == NULL)
2754 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2755 9ba1d308 2019-10-21 stsp optarg);
2756 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2757 66bea077 2018-08-02 stsp break;
2758 404c43c4 2018-06-21 stsp default:
2759 2deda0b9 2019-03-07 stsp usage_blame();
2760 404c43c4 2018-06-21 stsp /* NOTREACHED */
2761 404c43c4 2018-06-21 stsp }
2762 404c43c4 2018-06-21 stsp }
2763 404c43c4 2018-06-21 stsp
2764 404c43c4 2018-06-21 stsp argc -= optind;
2765 404c43c4 2018-06-21 stsp argv += optind;
2766 404c43c4 2018-06-21 stsp
2767 a39318fd 2018-08-02 stsp if (argc == 1)
2768 404c43c4 2018-06-21 stsp path = argv[0];
2769 a39318fd 2018-08-02 stsp else
2770 404c43c4 2018-06-21 stsp usage_blame();
2771 404c43c4 2018-06-21 stsp
2772 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2773 66bea077 2018-08-02 stsp if (cwd == NULL) {
2774 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2775 66bea077 2018-08-02 stsp goto done;
2776 66bea077 2018-08-02 stsp }
2777 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2778 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2779 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2780 66bea077 2018-08-02 stsp goto done;
2781 0c06baac 2019-02-05 stsp else
2782 0c06baac 2019-02-05 stsp error = NULL;
2783 0c06baac 2019-02-05 stsp if (worktree) {
2784 0c06baac 2019-02-05 stsp repo_path =
2785 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2786 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2787 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2788 0c06baac 2019-02-05 stsp if (error)
2789 0c06baac 2019-02-05 stsp goto done;
2790 0c06baac 2019-02-05 stsp } else {
2791 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2792 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2793 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2794 0c06baac 2019-02-05 stsp goto done;
2795 0c06baac 2019-02-05 stsp }
2796 66bea077 2018-08-02 stsp }
2797 66bea077 2018-08-02 stsp }
2798 36e2fb66 2019-01-04 stsp
2799 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2800 c02c541e 2019-03-29 stsp if (error != NULL)
2801 36e2fb66 2019-01-04 stsp goto done;
2802 66bea077 2018-08-02 stsp
2803 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2804 c02c541e 2019-03-29 stsp if (error)
2805 404c43c4 2018-06-21 stsp goto done;
2806 404c43c4 2018-06-21 stsp
2807 0c06baac 2019-02-05 stsp if (worktree) {
2808 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2809 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2810 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2811 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2812 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2813 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2814 6efaaa2d 2019-02-05 stsp path) == -1) {
2815 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2816 0c06baac 2019-02-05 stsp goto done;
2817 0c06baac 2019-02-05 stsp }
2818 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2819 0c06baac 2019-02-05 stsp free(p);
2820 0c06baac 2019-02-05 stsp } else {
2821 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2822 0c06baac 2019-02-05 stsp }
2823 0c06baac 2019-02-05 stsp if (error)
2824 66bea077 2018-08-02 stsp goto done;
2825 66bea077 2018-08-02 stsp
2826 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2827 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2828 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2829 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2830 404c43c4 2018-06-21 stsp if (error != NULL)
2831 66bea077 2018-08-02 stsp goto done;
2832 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2833 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2834 404c43c4 2018-06-21 stsp if (error != NULL)
2835 66bea077 2018-08-02 stsp goto done;
2836 404c43c4 2018-06-21 stsp } else {
2837 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2838 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2839 30837e32 2019-07-25 stsp if (error)
2840 66bea077 2018-08-02 stsp goto done;
2841 404c43c4 2018-06-21 stsp }
2842 404c43c4 2018-06-21 stsp
2843 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2844 28315671 2019-08-14 stsp if (error)
2845 28315671 2019-08-14 stsp goto done;
2846 28315671 2019-08-14 stsp
2847 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2848 28315671 2019-08-14 stsp if (error)
2849 28315671 2019-08-14 stsp goto done;
2850 28315671 2019-08-14 stsp
2851 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2852 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2853 28315671 2019-08-14 stsp goto done;
2854 28315671 2019-08-14 stsp }
2855 28315671 2019-08-14 stsp
2856 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2857 28315671 2019-08-14 stsp if (error)
2858 28315671 2019-08-14 stsp goto done;
2859 28315671 2019-08-14 stsp bca.f = got_opentemp();
2860 28315671 2019-08-14 stsp if (bca.f == NULL) {
2861 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2862 28315671 2019-08-14 stsp goto done;
2863 28315671 2019-08-14 stsp }
2864 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2865 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2866 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2867 28315671 2019-08-14 stsp goto done;
2868 28315671 2019-08-14 stsp
2869 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2870 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2871 b02560ec 2019-08-19 stsp bca.nlines--;
2872 b02560ec 2019-08-19 stsp
2873 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2874 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2875 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2876 28315671 2019-08-14 stsp goto done;
2877 28315671 2019-08-14 stsp }
2878 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2879 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2880 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2881 7ef28ff8 2019-08-14 stsp while (i > 0) {
2882 7ef28ff8 2019-08-14 stsp i /= 10;
2883 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2884 7ef28ff8 2019-08-14 stsp }
2885 82f6abb8 2019-08-14 stsp bca.repo = repo;
2886 7ef28ff8 2019-08-14 stsp
2887 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2888 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2889 404c43c4 2018-06-21 stsp done:
2890 66bea077 2018-08-02 stsp free(in_repo_path);
2891 66bea077 2018-08-02 stsp free(repo_path);
2892 66bea077 2018-08-02 stsp free(cwd);
2893 404c43c4 2018-06-21 stsp free(commit_id);
2894 28315671 2019-08-14 stsp free(obj_id);
2895 28315671 2019-08-14 stsp if (blob)
2896 28315671 2019-08-14 stsp got_object_blob_close(blob);
2897 0c06baac 2019-02-05 stsp if (worktree)
2898 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2899 ad242220 2018-09-08 stsp if (repo) {
2900 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2901 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2902 ad242220 2018-09-08 stsp if (error == NULL)
2903 ad242220 2018-09-08 stsp error = repo_error;
2904 ad242220 2018-09-08 stsp }
2905 3affba96 2019-09-22 stsp if (bca.lines) {
2906 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2907 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2908 3affba96 2019-09-22 stsp free(bline->id_str);
2909 3affba96 2019-09-22 stsp free(bline->committer);
2910 3affba96 2019-09-22 stsp }
2911 3affba96 2019-09-22 stsp free(bca.lines);
2912 28315671 2019-08-14 stsp }
2913 28315671 2019-08-14 stsp free(bca.line_offsets);
2914 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2915 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2916 404c43c4 2018-06-21 stsp return error;
2917 5de5890b 2018-10-18 stsp }
2918 5de5890b 2018-10-18 stsp
2919 5de5890b 2018-10-18 stsp __dead static void
2920 5de5890b 2018-10-18 stsp usage_tree(void)
2921 5de5890b 2018-10-18 stsp {
2922 c1669e2e 2019-01-09 stsp fprintf(stderr,
2923 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2924 5de5890b 2018-10-18 stsp getprogname());
2925 5de5890b 2018-10-18 stsp exit(1);
2926 5de5890b 2018-10-18 stsp }
2927 5de5890b 2018-10-18 stsp
2928 c1669e2e 2019-01-09 stsp static void
2929 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2930 c1669e2e 2019-01-09 stsp const char *root_path)
2931 c1669e2e 2019-01-09 stsp {
2932 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2933 848d6979 2019-08-12 stsp const char *modestr = "";
2934 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2935 5de5890b 2018-10-18 stsp
2936 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2937 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2938 c1669e2e 2019-01-09 stsp path++;
2939 c1669e2e 2019-01-09 stsp
2940 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2941 63c5ca5d 2019-08-24 stsp modestr = "$";
2942 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2943 848d6979 2019-08-12 stsp modestr = "@";
2944 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2945 848d6979 2019-08-12 stsp modestr = "/";
2946 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2947 848d6979 2019-08-12 stsp modestr = "*";
2948 848d6979 2019-08-12 stsp
2949 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2950 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2951 c1669e2e 2019-01-09 stsp }
2952 c1669e2e 2019-01-09 stsp
2953 5de5890b 2018-10-18 stsp static const struct got_error *
2954 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2955 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2956 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2957 5de5890b 2018-10-18 stsp {
2958 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2959 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2960 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2961 56e0773d 2019-11-28 stsp int nentries, i;
2962 5de5890b 2018-10-18 stsp
2963 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2964 5de5890b 2018-10-18 stsp if (err)
2965 5de5890b 2018-10-18 stsp goto done;
2966 5de5890b 2018-10-18 stsp
2967 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2968 5de5890b 2018-10-18 stsp if (err)
2969 5de5890b 2018-10-18 stsp goto done;
2970 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2971 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2972 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2973 5de5890b 2018-10-18 stsp char *id = NULL;
2974 84453469 2018-11-11 stsp
2975 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2976 84453469 2018-11-11 stsp break;
2977 84453469 2018-11-11 stsp
2978 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2979 5de5890b 2018-10-18 stsp if (show_ids) {
2980 5de5890b 2018-10-18 stsp char *id_str;
2981 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2982 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2983 5de5890b 2018-10-18 stsp if (err)
2984 5de5890b 2018-10-18 stsp goto done;
2985 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2986 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2987 5de5890b 2018-10-18 stsp free(id_str);
2988 5de5890b 2018-10-18 stsp goto done;
2989 5de5890b 2018-10-18 stsp }
2990 5de5890b 2018-10-18 stsp free(id_str);
2991 5de5890b 2018-10-18 stsp }
2992 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2993 5de5890b 2018-10-18 stsp free(id);
2994 c1669e2e 2019-01-09 stsp
2995 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2996 c1669e2e 2019-01-09 stsp char *child_path;
2997 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2998 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2999 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3000 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3001 c1669e2e 2019-01-09 stsp goto done;
3002 c1669e2e 2019-01-09 stsp }
3003 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3004 c1669e2e 2019-01-09 stsp root_path, repo);
3005 c1669e2e 2019-01-09 stsp free(child_path);
3006 c1669e2e 2019-01-09 stsp if (err)
3007 c1669e2e 2019-01-09 stsp goto done;
3008 c1669e2e 2019-01-09 stsp }
3009 5de5890b 2018-10-18 stsp }
3010 5de5890b 2018-10-18 stsp done:
3011 5de5890b 2018-10-18 stsp if (tree)
3012 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3013 5de5890b 2018-10-18 stsp free(tree_id);
3014 5de5890b 2018-10-18 stsp return err;
3015 404c43c4 2018-06-21 stsp }
3016 404c43c4 2018-06-21 stsp
3017 5de5890b 2018-10-18 stsp static const struct got_error *
3018 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3019 5de5890b 2018-10-18 stsp {
3020 5de5890b 2018-10-18 stsp const struct got_error *error;
3021 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3022 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3023 7a2c19d6 2019-02-05 stsp const char *path;
3024 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3025 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3026 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3027 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3028 5de5890b 2018-10-18 stsp int ch;
3029 5de5890b 2018-10-18 stsp
3030 5de5890b 2018-10-18 stsp #ifndef PROFILE
3031 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3032 0f8d269b 2019-01-04 stsp NULL) == -1)
3033 5de5890b 2018-10-18 stsp err(1, "pledge");
3034 5de5890b 2018-10-18 stsp #endif
3035 5de5890b 2018-10-18 stsp
3036 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3037 5de5890b 2018-10-18 stsp switch (ch) {
3038 5de5890b 2018-10-18 stsp case 'c':
3039 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3040 5de5890b 2018-10-18 stsp break;
3041 5de5890b 2018-10-18 stsp case 'r':
3042 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3043 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3044 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3045 9ba1d308 2019-10-21 stsp optarg);
3046 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3047 5de5890b 2018-10-18 stsp break;
3048 5de5890b 2018-10-18 stsp case 'i':
3049 5de5890b 2018-10-18 stsp show_ids = 1;
3050 5de5890b 2018-10-18 stsp break;
3051 c1669e2e 2019-01-09 stsp case 'R':
3052 c1669e2e 2019-01-09 stsp recurse = 1;
3053 c1669e2e 2019-01-09 stsp break;
3054 5de5890b 2018-10-18 stsp default:
3055 2deda0b9 2019-03-07 stsp usage_tree();
3056 5de5890b 2018-10-18 stsp /* NOTREACHED */
3057 5de5890b 2018-10-18 stsp }
3058 5de5890b 2018-10-18 stsp }
3059 5de5890b 2018-10-18 stsp
3060 5de5890b 2018-10-18 stsp argc -= optind;
3061 5de5890b 2018-10-18 stsp argv += optind;
3062 5de5890b 2018-10-18 stsp
3063 5de5890b 2018-10-18 stsp if (argc == 1)
3064 5de5890b 2018-10-18 stsp path = argv[0];
3065 5de5890b 2018-10-18 stsp else if (argc > 1)
3066 5de5890b 2018-10-18 stsp usage_tree();
3067 5de5890b 2018-10-18 stsp else
3068 7a2c19d6 2019-02-05 stsp path = NULL;
3069 7a2c19d6 2019-02-05 stsp
3070 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3071 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3072 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3073 9bf7a39b 2019-02-05 stsp goto done;
3074 9bf7a39b 2019-02-05 stsp }
3075 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3076 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3077 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3078 7a2c19d6 2019-02-05 stsp goto done;
3079 7a2c19d6 2019-02-05 stsp else
3080 7a2c19d6 2019-02-05 stsp error = NULL;
3081 7a2c19d6 2019-02-05 stsp if (worktree) {
3082 7a2c19d6 2019-02-05 stsp repo_path =
3083 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3084 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3085 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3086 7a2c19d6 2019-02-05 stsp if (error)
3087 7a2c19d6 2019-02-05 stsp goto done;
3088 7a2c19d6 2019-02-05 stsp } else {
3089 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3090 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3091 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3092 7a2c19d6 2019-02-05 stsp goto done;
3093 7a2c19d6 2019-02-05 stsp }
3094 5de5890b 2018-10-18 stsp }
3095 5de5890b 2018-10-18 stsp }
3096 5de5890b 2018-10-18 stsp
3097 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3098 5de5890b 2018-10-18 stsp if (error != NULL)
3099 c02c541e 2019-03-29 stsp goto done;
3100 c02c541e 2019-03-29 stsp
3101 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3102 c02c541e 2019-03-29 stsp if (error)
3103 5de5890b 2018-10-18 stsp goto done;
3104 5de5890b 2018-10-18 stsp
3105 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3106 9bf7a39b 2019-02-05 stsp if (worktree) {
3107 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3108 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3109 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3110 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3111 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3112 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3113 9bf7a39b 2019-02-05 stsp goto done;
3114 9bf7a39b 2019-02-05 stsp }
3115 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3116 9bf7a39b 2019-02-05 stsp free(p);
3117 9bf7a39b 2019-02-05 stsp if (error)
3118 9bf7a39b 2019-02-05 stsp goto done;
3119 9bf7a39b 2019-02-05 stsp } else
3120 9bf7a39b 2019-02-05 stsp path = "/";
3121 9bf7a39b 2019-02-05 stsp }
3122 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3123 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3124 9bf7a39b 2019-02-05 stsp if (error != NULL)
3125 9bf7a39b 2019-02-05 stsp goto done;
3126 9bf7a39b 2019-02-05 stsp }
3127 5de5890b 2018-10-18 stsp
3128 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3129 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3130 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3131 5de5890b 2018-10-18 stsp if (error != NULL)
3132 5de5890b 2018-10-18 stsp goto done;
3133 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3134 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3135 5de5890b 2018-10-18 stsp if (error != NULL)
3136 5de5890b 2018-10-18 stsp goto done;
3137 5de5890b 2018-10-18 stsp } else {
3138 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3139 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3140 30837e32 2019-07-25 stsp if (error)
3141 5de5890b 2018-10-18 stsp goto done;
3142 5de5890b 2018-10-18 stsp }
3143 5de5890b 2018-10-18 stsp
3144 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3145 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3146 5de5890b 2018-10-18 stsp done:
3147 5de5890b 2018-10-18 stsp free(in_repo_path);
3148 5de5890b 2018-10-18 stsp free(repo_path);
3149 5de5890b 2018-10-18 stsp free(cwd);
3150 5de5890b 2018-10-18 stsp free(commit_id);
3151 7a2c19d6 2019-02-05 stsp if (worktree)
3152 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3153 5de5890b 2018-10-18 stsp if (repo) {
3154 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3155 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3156 5de5890b 2018-10-18 stsp if (error == NULL)
3157 5de5890b 2018-10-18 stsp error = repo_error;
3158 5de5890b 2018-10-18 stsp }
3159 5de5890b 2018-10-18 stsp return error;
3160 5de5890b 2018-10-18 stsp }
3161 5de5890b 2018-10-18 stsp
3162 6bad629b 2019-02-04 stsp __dead static void
3163 6bad629b 2019-02-04 stsp usage_status(void)
3164 6bad629b 2019-02-04 stsp {
3165 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3166 6bad629b 2019-02-04 stsp exit(1);
3167 6bad629b 2019-02-04 stsp }
3168 5c860e29 2018-03-12 stsp
3169 b72f483a 2019-02-05 stsp static const struct got_error *
3170 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3171 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3172 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3173 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3174 6bad629b 2019-02-04 stsp {
3175 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3176 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3177 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3178 b72f483a 2019-02-05 stsp return NULL;
3179 6bad629b 2019-02-04 stsp }
3180 5c860e29 2018-03-12 stsp
3181 6bad629b 2019-02-04 stsp static const struct got_error *
3182 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3183 6bad629b 2019-02-04 stsp {
3184 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3185 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3186 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3187 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3188 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3189 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3190 a5edda0a 2019-07-27 stsp int ch;
3191 5c860e29 2018-03-12 stsp
3192 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3193 72ea6654 2019-07-27 stsp
3194 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3195 6bad629b 2019-02-04 stsp switch (ch) {
3196 5c860e29 2018-03-12 stsp default:
3197 2deda0b9 2019-03-07 stsp usage_status();
3198 6bad629b 2019-02-04 stsp /* NOTREACHED */
3199 5c860e29 2018-03-12 stsp }
3200 5c860e29 2018-03-12 stsp }
3201 5c860e29 2018-03-12 stsp
3202 6bad629b 2019-02-04 stsp argc -= optind;
3203 6bad629b 2019-02-04 stsp argv += optind;
3204 5c860e29 2018-03-12 stsp
3205 6bad629b 2019-02-04 stsp #ifndef PROFILE
3206 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3207 6bad629b 2019-02-04 stsp NULL) == -1)
3208 6bad629b 2019-02-04 stsp err(1, "pledge");
3209 f42b1b34 2018-03-12 stsp #endif
3210 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3211 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3212 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3213 927df6b7 2019-02-10 stsp goto done;
3214 927df6b7 2019-02-10 stsp }
3215 927df6b7 2019-02-10 stsp
3216 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3217 927df6b7 2019-02-10 stsp if (error != NULL)
3218 a5edda0a 2019-07-27 stsp goto done;
3219 6bad629b 2019-02-04 stsp
3220 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3221 c9956ddf 2019-09-08 stsp NULL);
3222 6bad629b 2019-02-04 stsp if (error != NULL)
3223 6bad629b 2019-02-04 stsp goto done;
3224 6bad629b 2019-02-04 stsp
3225 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3227 087fb88c 2019-08-04 stsp if (error)
3228 087fb88c 2019-08-04 stsp goto done;
3229 087fb88c 2019-08-04 stsp
3230 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3231 6bad629b 2019-02-04 stsp if (error)
3232 6bad629b 2019-02-04 stsp goto done;
3233 6bad629b 2019-02-04 stsp
3234 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3235 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3236 6bad629b 2019-02-04 stsp done:
3237 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3238 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3239 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3240 927df6b7 2019-02-10 stsp free(cwd);
3241 d0eebce4 2019-03-11 stsp return error;
3242 d0eebce4 2019-03-11 stsp }
3243 d0eebce4 2019-03-11 stsp
3244 d0eebce4 2019-03-11 stsp __dead static void
3245 d0eebce4 2019-03-11 stsp usage_ref(void)
3246 d0eebce4 2019-03-11 stsp {
3247 d0eebce4 2019-03-11 stsp fprintf(stderr,
3248 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3249 d0eebce4 2019-03-11 stsp getprogname());
3250 d0eebce4 2019-03-11 stsp exit(1);
3251 d0eebce4 2019-03-11 stsp }
3252 d0eebce4 2019-03-11 stsp
3253 d0eebce4 2019-03-11 stsp static const struct got_error *
3254 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3255 d0eebce4 2019-03-11 stsp {
3256 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3257 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3258 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3259 d0eebce4 2019-03-11 stsp
3260 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3261 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3262 d0eebce4 2019-03-11 stsp if (err)
3263 d0eebce4 2019-03-11 stsp return err;
3264 d0eebce4 2019-03-11 stsp
3265 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3266 d0eebce4 2019-03-11 stsp char *refstr;
3267 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3268 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3269 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3270 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3271 d0eebce4 2019-03-11 stsp free(refstr);
3272 d0eebce4 2019-03-11 stsp }
3273 d0eebce4 2019-03-11 stsp
3274 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3275 d0eebce4 2019-03-11 stsp return NULL;
3276 d0eebce4 2019-03-11 stsp }
3277 d0eebce4 2019-03-11 stsp
3278 d0eebce4 2019-03-11 stsp static const struct got_error *
3279 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3280 d0eebce4 2019-03-11 stsp {
3281 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3282 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3283 d0eebce4 2019-03-11 stsp
3284 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3285 d0eebce4 2019-03-11 stsp if (err)
3286 d0eebce4 2019-03-11 stsp return err;
3287 d0eebce4 2019-03-11 stsp
3288 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3289 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3290 d0eebce4 2019-03-11 stsp return err;
3291 d0eebce4 2019-03-11 stsp }
3292 d0eebce4 2019-03-11 stsp
3293 d0eebce4 2019-03-11 stsp static const struct got_error *
3294 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3295 d0eebce4 2019-03-11 stsp {
3296 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3297 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3298 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3299 d1644381 2019-07-14 stsp
3300 d1644381 2019-07-14 stsp /*
3301 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3302 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3303 d1644381 2019-07-14 stsp * an unintended typo.
3304 d1644381 2019-07-14 stsp */
3305 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3306 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3307 d0eebce4 2019-03-11 stsp
3308 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3309 dd88155e 2019-06-29 stsp repo);
3310 d83d9d5c 2019-05-13 stsp if (err) {
3311 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3312 d0eebce4 2019-03-11 stsp
3313 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3314 d83d9d5c 2019-05-13 stsp return err;
3315 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3316 d83d9d5c 2019-05-13 stsp if (err)
3317 d83d9d5c 2019-05-13 stsp return err;
3318 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3319 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3320 d83d9d5c 2019-05-13 stsp if (err)
3321 d83d9d5c 2019-05-13 stsp return err;
3322 d83d9d5c 2019-05-13 stsp }
3323 d83d9d5c 2019-05-13 stsp
3324 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3325 d0eebce4 2019-03-11 stsp if (err)
3326 d0eebce4 2019-03-11 stsp goto done;
3327 d0eebce4 2019-03-11 stsp
3328 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3329 d0eebce4 2019-03-11 stsp done:
3330 d0eebce4 2019-03-11 stsp if (ref)
3331 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3332 d0eebce4 2019-03-11 stsp free(id);
3333 d1c1ae5f 2019-08-12 stsp return err;
3334 d1c1ae5f 2019-08-12 stsp }
3335 d1c1ae5f 2019-08-12 stsp
3336 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3337 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3338 d1c1ae5f 2019-08-12 stsp {
3339 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3340 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3341 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3342 d1c1ae5f 2019-08-12 stsp
3343 d1c1ae5f 2019-08-12 stsp /*
3344 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3345 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3346 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3347 d1c1ae5f 2019-08-12 stsp */
3348 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3349 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3350 d1c1ae5f 2019-08-12 stsp
3351 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3352 d1c1ae5f 2019-08-12 stsp if (err)
3353 d1c1ae5f 2019-08-12 stsp return err;
3354 d1c1ae5f 2019-08-12 stsp
3355 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3356 d1c1ae5f 2019-08-12 stsp if (err)
3357 d1c1ae5f 2019-08-12 stsp goto done;
3358 d1c1ae5f 2019-08-12 stsp
3359 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3360 d1c1ae5f 2019-08-12 stsp done:
3361 d1c1ae5f 2019-08-12 stsp if (target_ref)
3362 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3363 d1c1ae5f 2019-08-12 stsp if (ref)
3364 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3365 d0eebce4 2019-03-11 stsp return err;
3366 d0eebce4 2019-03-11 stsp }
3367 d0eebce4 2019-03-11 stsp
3368 d0eebce4 2019-03-11 stsp static const struct got_error *
3369 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3370 d0eebce4 2019-03-11 stsp {
3371 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3372 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3373 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3374 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3375 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3376 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3377 d0eebce4 2019-03-11 stsp
3378 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3379 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3380 d0eebce4 2019-03-11 stsp switch (ch) {
3381 d0eebce4 2019-03-11 stsp case 'd':
3382 d0eebce4 2019-03-11 stsp delref = optarg;
3383 d0eebce4 2019-03-11 stsp break;
3384 d0eebce4 2019-03-11 stsp case 'r':
3385 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3386 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3387 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3388 9ba1d308 2019-10-21 stsp optarg);
3389 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3390 d0eebce4 2019-03-11 stsp break;
3391 d0eebce4 2019-03-11 stsp case 'l':
3392 d0eebce4 2019-03-11 stsp do_list = 1;
3393 d1c1ae5f 2019-08-12 stsp break;
3394 d1c1ae5f 2019-08-12 stsp case 's':
3395 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3396 d0eebce4 2019-03-11 stsp break;
3397 d0eebce4 2019-03-11 stsp default:
3398 d0eebce4 2019-03-11 stsp usage_ref();
3399 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3400 d0eebce4 2019-03-11 stsp }
3401 d0eebce4 2019-03-11 stsp }
3402 d0eebce4 2019-03-11 stsp
3403 d0eebce4 2019-03-11 stsp if (do_list && delref)
3404 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3405 d0eebce4 2019-03-11 stsp
3406 d0eebce4 2019-03-11 stsp argc -= optind;
3407 d0eebce4 2019-03-11 stsp argv += optind;
3408 d0eebce4 2019-03-11 stsp
3409 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3410 d1c1ae5f 2019-08-12 stsp if (create_symref)
3411 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3412 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3413 d0eebce4 2019-03-11 stsp if (argc > 0)
3414 d0eebce4 2019-03-11 stsp usage_ref();
3415 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3416 d0eebce4 2019-03-11 stsp usage_ref();
3417 d0eebce4 2019-03-11 stsp
3418 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3419 e0b57350 2019-03-12 stsp if (do_list) {
3420 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3421 e0b57350 2019-03-12 stsp NULL) == -1)
3422 e0b57350 2019-03-12 stsp err(1, "pledge");
3423 e0b57350 2019-03-12 stsp } else {
3424 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3425 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3426 e0b57350 2019-03-12 stsp err(1, "pledge");
3427 e0b57350 2019-03-12 stsp }
3428 d0eebce4 2019-03-11 stsp #endif
3429 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3430 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3431 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3432 d0eebce4 2019-03-11 stsp goto done;
3433 d0eebce4 2019-03-11 stsp }
3434 d0eebce4 2019-03-11 stsp
3435 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3436 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3437 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3438 d0eebce4 2019-03-11 stsp goto done;
3439 d0eebce4 2019-03-11 stsp else
3440 d0eebce4 2019-03-11 stsp error = NULL;
3441 d0eebce4 2019-03-11 stsp if (worktree) {
3442 d0eebce4 2019-03-11 stsp repo_path =
3443 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3444 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3445 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3446 d0eebce4 2019-03-11 stsp if (error)
3447 d0eebce4 2019-03-11 stsp goto done;
3448 d0eebce4 2019-03-11 stsp } else {
3449 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3450 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3451 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3452 d0eebce4 2019-03-11 stsp goto done;
3453 d0eebce4 2019-03-11 stsp }
3454 d0eebce4 2019-03-11 stsp }
3455 d0eebce4 2019-03-11 stsp }
3456 d0eebce4 2019-03-11 stsp
3457 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3458 d0eebce4 2019-03-11 stsp if (error != NULL)
3459 d0eebce4 2019-03-11 stsp goto done;
3460 d0eebce4 2019-03-11 stsp
3461 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3462 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3463 c02c541e 2019-03-29 stsp if (error)
3464 c02c541e 2019-03-29 stsp goto done;
3465 c02c541e 2019-03-29 stsp
3466 d0eebce4 2019-03-11 stsp if (do_list)
3467 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3468 d0eebce4 2019-03-11 stsp else if (delref)
3469 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3470 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3471 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3472 d0eebce4 2019-03-11 stsp else
3473 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3474 4e759de4 2019-06-26 stsp done:
3475 4e759de4 2019-06-26 stsp if (repo)
3476 4e759de4 2019-06-26 stsp got_repo_close(repo);
3477 4e759de4 2019-06-26 stsp if (worktree)
3478 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3479 4e759de4 2019-06-26 stsp free(cwd);
3480 4e759de4 2019-06-26 stsp free(repo_path);
3481 4e759de4 2019-06-26 stsp return error;
3482 4e759de4 2019-06-26 stsp }
3483 4e759de4 2019-06-26 stsp
3484 4e759de4 2019-06-26 stsp __dead static void
3485 4e759de4 2019-06-26 stsp usage_branch(void)
3486 4e759de4 2019-06-26 stsp {
3487 4e759de4 2019-06-26 stsp fprintf(stderr,
3488 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3489 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3490 4e759de4 2019-06-26 stsp exit(1);
3491 4e759de4 2019-06-26 stsp }
3492 4e759de4 2019-06-26 stsp
3493 4e759de4 2019-06-26 stsp static const struct got_error *
3494 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3496 4e99b47e 2019-10-04 stsp {
3497 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3498 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3499 4e99b47e 2019-10-04 stsp char *refstr;
3500 4e99b47e 2019-10-04 stsp
3501 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3502 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3503 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3504 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3505 4e99b47e 2019-10-04 stsp
3506 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3507 4e99b47e 2019-10-04 stsp if (err)
3508 4e99b47e 2019-10-04 stsp return err;
3509 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3510 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3511 4e99b47e 2019-10-04 stsp marker = "* ";
3512 4e99b47e 2019-10-04 stsp else
3513 4e99b47e 2019-10-04 stsp marker = "~ ";
3514 4e99b47e 2019-10-04 stsp free(id);
3515 4e99b47e 2019-10-04 stsp }
3516 4e99b47e 2019-10-04 stsp
3517 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3518 4e99b47e 2019-10-04 stsp refname += 11;
3519 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3520 4e99b47e 2019-10-04 stsp refname += 18;
3521 4e99b47e 2019-10-04 stsp
3522 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3523 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3524 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3525 4e99b47e 2019-10-04 stsp
3526 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3527 4e99b47e 2019-10-04 stsp free(refstr);
3528 4e99b47e 2019-10-04 stsp return NULL;
3529 4e99b47e 2019-10-04 stsp }
3530 4e99b47e 2019-10-04 stsp
3531 4e99b47e 2019-10-04 stsp static const struct got_error *
3532 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3533 ad89fa31 2019-10-04 stsp {
3534 ad89fa31 2019-10-04 stsp const char *refname;
3535 ad89fa31 2019-10-04 stsp
3536 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3537 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3538 ad89fa31 2019-10-04 stsp
3539 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3540 ad89fa31 2019-10-04 stsp
3541 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3542 ad89fa31 2019-10-04 stsp refname += 11;
3543 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3544 ad89fa31 2019-10-04 stsp refname += 18;
3545 ad89fa31 2019-10-04 stsp
3546 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3547 ad89fa31 2019-10-04 stsp
3548 ad89fa31 2019-10-04 stsp return NULL;
3549 ad89fa31 2019-10-04 stsp }
3550 ad89fa31 2019-10-04 stsp
3551 ad89fa31 2019-10-04 stsp static const struct got_error *
3552 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3553 4e759de4 2019-06-26 stsp {
3554 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3555 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3556 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3557 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3558 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3559 4e759de4 2019-06-26 stsp
3560 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3561 ba882ee3 2019-07-11 stsp
3562 4e99b47e 2019-10-04 stsp if (worktree) {
3563 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3564 4e99b47e 2019-10-04 stsp worktree);
3565 4e99b47e 2019-10-04 stsp if (err)
3566 4e99b47e 2019-10-04 stsp return err;
3567 4e99b47e 2019-10-04 stsp
3568 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3569 4e99b47e 2019-10-04 stsp worktree);
3570 4e99b47e 2019-10-04 stsp if (err)
3571 4e99b47e 2019-10-04 stsp return err;
3572 4e99b47e 2019-10-04 stsp
3573 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3574 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3575 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3576 4e99b47e 2019-10-04 stsp if (err)
3577 4e99b47e 2019-10-04 stsp return err;
3578 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3579 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3580 4e99b47e 2019-10-04 stsp }
3581 4e99b47e 2019-10-04 stsp }
3582 4e99b47e 2019-10-04 stsp
3583 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3584 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3585 4e759de4 2019-06-26 stsp if (err)
3586 4e759de4 2019-06-26 stsp return err;
3587 4e759de4 2019-06-26 stsp
3588 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3589 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3590 4e759de4 2019-06-26 stsp
3591 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3592 4e759de4 2019-06-26 stsp return NULL;
3593 4e759de4 2019-06-26 stsp }
3594 4e759de4 2019-06-26 stsp
3595 4e759de4 2019-06-26 stsp static const struct got_error *
3596 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3597 45cd4e47 2019-08-25 stsp const char *branch_name)
3598 4e759de4 2019-06-26 stsp {
3599 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3600 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3601 4e759de4 2019-06-26 stsp char *refname;
3602 4e759de4 2019-06-26 stsp
3603 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3604 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3605 4e759de4 2019-06-26 stsp
3606 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3607 4e759de4 2019-06-26 stsp if (err)
3608 4e759de4 2019-06-26 stsp goto done;
3609 4e759de4 2019-06-26 stsp
3610 45cd4e47 2019-08-25 stsp if (worktree &&
3611 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3612 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3613 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3614 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3615 45cd4e47 2019-08-25 stsp goto done;
3616 45cd4e47 2019-08-25 stsp }
3617 45cd4e47 2019-08-25 stsp
3618 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3619 4e759de4 2019-06-26 stsp done:
3620 45cd4e47 2019-08-25 stsp if (ref)
3621 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3622 4e759de4 2019-06-26 stsp free(refname);
3623 4e759de4 2019-06-26 stsp return err;
3624 4e759de4 2019-06-26 stsp }
3625 4e759de4 2019-06-26 stsp
3626 4e759de4 2019-06-26 stsp static const struct got_error *
3627 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3628 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3629 4e759de4 2019-06-26 stsp {
3630 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3631 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3632 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3633 d3f84d51 2019-07-11 stsp
3634 d3f84d51 2019-07-11 stsp /*
3635 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3636 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3637 d3f84d51 2019-07-11 stsp * an unintended typo.
3638 d3f84d51 2019-07-11 stsp */
3639 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3640 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3641 4e759de4 2019-06-26 stsp
3642 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3643 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3644 4e759de4 2019-06-26 stsp goto done;
3645 4e759de4 2019-06-26 stsp }
3646 4e759de4 2019-06-26 stsp
3647 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3648 4e759de4 2019-06-26 stsp if (err == NULL) {
3649 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3650 4e759de4 2019-06-26 stsp goto done;
3651 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3652 4e759de4 2019-06-26 stsp goto done;
3653 4e759de4 2019-06-26 stsp
3654 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3655 4e759de4 2019-06-26 stsp if (err)
3656 4e759de4 2019-06-26 stsp goto done;
3657 4e759de4 2019-06-26 stsp
3658 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3659 d0eebce4 2019-03-11 stsp done:
3660 4e759de4 2019-06-26 stsp if (ref)
3661 4e759de4 2019-06-26 stsp got_ref_close(ref);
3662 4e759de4 2019-06-26 stsp free(base_refname);
3663 4e759de4 2019-06-26 stsp free(refname);
3664 4e759de4 2019-06-26 stsp return err;
3665 4e759de4 2019-06-26 stsp }
3666 4e759de4 2019-06-26 stsp
3667 4e759de4 2019-06-26 stsp static const struct got_error *
3668 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3669 4e759de4 2019-06-26 stsp {
3670 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3671 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3672 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3673 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3674 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3675 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3676 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3677 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3678 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3679 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3680 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3681 4e759de4 2019-06-26 stsp
3682 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3683 da76fce2 2020-02-24 stsp
3684 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3685 4e759de4 2019-06-26 stsp switch (ch) {
3686 a74f7e83 2019-11-10 stsp case 'c':
3687 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3688 a74f7e83 2019-11-10 stsp break;
3689 4e759de4 2019-06-26 stsp case 'd':
3690 4e759de4 2019-06-26 stsp delref = optarg;
3691 4e759de4 2019-06-26 stsp break;
3692 4e759de4 2019-06-26 stsp case 'r':
3693 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3694 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3695 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3696 9ba1d308 2019-10-21 stsp optarg);
3697 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3698 4e759de4 2019-06-26 stsp break;
3699 4e759de4 2019-06-26 stsp case 'l':
3700 4e759de4 2019-06-26 stsp do_list = 1;
3701 da76fce2 2020-02-24 stsp break;
3702 da76fce2 2020-02-24 stsp case 'n':
3703 da76fce2 2020-02-24 stsp do_update = 0;
3704 4e759de4 2019-06-26 stsp break;
3705 4e759de4 2019-06-26 stsp default:
3706 4e759de4 2019-06-26 stsp usage_branch();
3707 4e759de4 2019-06-26 stsp /* NOTREACHED */
3708 4e759de4 2019-06-26 stsp }
3709 4e759de4 2019-06-26 stsp }
3710 4e759de4 2019-06-26 stsp
3711 4e759de4 2019-06-26 stsp if (do_list && delref)
3712 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3713 4e759de4 2019-06-26 stsp
3714 4e759de4 2019-06-26 stsp argc -= optind;
3715 4e759de4 2019-06-26 stsp argv += optind;
3716 ad89fa31 2019-10-04 stsp
3717 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3718 ad89fa31 2019-10-04 stsp do_show = 1;
3719 4e759de4 2019-06-26 stsp
3720 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3721 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3722 a74f7e83 2019-11-10 stsp
3723 4e759de4 2019-06-26 stsp if (do_list || delref) {
3724 4e759de4 2019-06-26 stsp if (argc > 0)
3725 4e759de4 2019-06-26 stsp usage_branch();
3726 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3727 4e759de4 2019-06-26 stsp usage_branch();
3728 4e759de4 2019-06-26 stsp
3729 4e759de4 2019-06-26 stsp #ifndef PROFILE
3730 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3731 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3732 4e759de4 2019-06-26 stsp NULL) == -1)
3733 4e759de4 2019-06-26 stsp err(1, "pledge");
3734 4e759de4 2019-06-26 stsp } else {
3735 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3736 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3737 4e759de4 2019-06-26 stsp err(1, "pledge");
3738 4e759de4 2019-06-26 stsp }
3739 4e759de4 2019-06-26 stsp #endif
3740 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3741 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3742 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3743 4e759de4 2019-06-26 stsp goto done;
3744 4e759de4 2019-06-26 stsp }
3745 4e759de4 2019-06-26 stsp
3746 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3747 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3748 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3749 4e759de4 2019-06-26 stsp goto done;
3750 4e759de4 2019-06-26 stsp else
3751 4e759de4 2019-06-26 stsp error = NULL;
3752 4e759de4 2019-06-26 stsp if (worktree) {
3753 4e759de4 2019-06-26 stsp repo_path =
3754 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3755 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3756 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3757 4e759de4 2019-06-26 stsp if (error)
3758 4e759de4 2019-06-26 stsp goto done;
3759 4e759de4 2019-06-26 stsp } else {
3760 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3761 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3762 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3763 4e759de4 2019-06-26 stsp goto done;
3764 4e759de4 2019-06-26 stsp }
3765 4e759de4 2019-06-26 stsp }
3766 4e759de4 2019-06-26 stsp }
3767 4e759de4 2019-06-26 stsp
3768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3769 4e759de4 2019-06-26 stsp if (error != NULL)
3770 4e759de4 2019-06-26 stsp goto done;
3771 4e759de4 2019-06-26 stsp
3772 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3773 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3774 4e759de4 2019-06-26 stsp if (error)
3775 4e759de4 2019-06-26 stsp goto done;
3776 4e759de4 2019-06-26 stsp
3777 ad89fa31 2019-10-04 stsp if (do_show)
3778 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3779 ad89fa31 2019-10-04 stsp else if (do_list)
3780 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3781 4e759de4 2019-06-26 stsp else if (delref)
3782 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3783 4e759de4 2019-06-26 stsp else {
3784 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3785 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3786 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3787 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3788 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3789 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3790 a74f7e83 2019-11-10 stsp if (error)
3791 a74f7e83 2019-11-10 stsp goto done;
3792 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3793 da76fce2 2020-02-24 stsp if (error)
3794 da76fce2 2020-02-24 stsp goto done;
3795 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3796 da76fce2 2020-02-24 stsp int did_something = 0;
3797 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3798 da76fce2 2020-02-24 stsp
3799 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3800 da76fce2 2020-02-24 stsp if (error)
3801 da76fce2 2020-02-24 stsp goto done;
3802 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3803 da76fce2 2020-02-24 stsp worktree);
3804 da76fce2 2020-02-24 stsp if (error)
3805 da76fce2 2020-02-24 stsp goto done;
3806 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3807 da76fce2 2020-02-24 stsp == -1) {
3808 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3809 da76fce2 2020-02-24 stsp goto done;
3810 da76fce2 2020-02-24 stsp }
3811 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3812 da76fce2 2020-02-24 stsp free(branch_refname);
3813 da76fce2 2020-02-24 stsp if (error)
3814 da76fce2 2020-02-24 stsp goto done;
3815 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3816 da76fce2 2020-02-24 stsp repo);
3817 da76fce2 2020-02-24 stsp if (error)
3818 da76fce2 2020-02-24 stsp goto done;
3819 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3820 da76fce2 2020-02-24 stsp commit_id);
3821 da76fce2 2020-02-24 stsp if (error)
3822 da76fce2 2020-02-24 stsp goto done;
3823 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3824 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3825 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3826 da76fce2 2020-02-24 stsp if (error)
3827 da76fce2 2020-02-24 stsp goto done;
3828 da76fce2 2020-02-24 stsp if (did_something)
3829 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3830 da76fce2 2020-02-24 stsp }
3831 4e759de4 2019-06-26 stsp }
3832 4e759de4 2019-06-26 stsp done:
3833 da76fce2 2020-02-24 stsp if (ref)
3834 da76fce2 2020-02-24 stsp got_ref_close(ref);
3835 d0eebce4 2019-03-11 stsp if (repo)
3836 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3837 d0eebce4 2019-03-11 stsp if (worktree)
3838 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3839 d0eebce4 2019-03-11 stsp free(cwd);
3840 d0eebce4 2019-03-11 stsp free(repo_path);
3841 da76fce2 2020-02-24 stsp free(commit_id);
3842 da76fce2 2020-02-24 stsp free(commit_id_str);
3843 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3844 da76fce2 2020-02-24 stsp free((char *)pe->path);
3845 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3846 d00136be 2019-03-26 stsp return error;
3847 d00136be 2019-03-26 stsp }
3848 d00136be 2019-03-26 stsp
3849 8e7bd50a 2019-08-22 stsp
3850 d00136be 2019-03-26 stsp __dead static void
3851 8e7bd50a 2019-08-22 stsp usage_tag(void)
3852 8e7bd50a 2019-08-22 stsp {
3853 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3854 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3855 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3856 8e7bd50a 2019-08-22 stsp exit(1);
3857 b8bad2ba 2019-08-23 stsp }
3858 b8bad2ba 2019-08-23 stsp
3859 b8bad2ba 2019-08-23 stsp #if 0
3860 b8bad2ba 2019-08-23 stsp static const struct got_error *
3861 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3862 b8bad2ba 2019-08-23 stsp {
3863 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3864 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3865 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3866 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3867 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3868 b8bad2ba 2019-08-23 stsp
3869 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3870 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3871 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3872 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3873 b8bad2ba 2019-08-23 stsp if (err)
3874 b8bad2ba 2019-08-23 stsp return err;
3875 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3876 b8bad2ba 2019-08-23 stsp continue;
3877 b8bad2ba 2019-08-23 stsp } else {
3878 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3879 b8bad2ba 2019-08-23 stsp if (err)
3880 b8bad2ba 2019-08-23 stsp break;
3881 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3882 b8bad2ba 2019-08-23 stsp free(re_id);
3883 b8bad2ba 2019-08-23 stsp if (err)
3884 b8bad2ba 2019-08-23 stsp break;
3885 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3886 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3887 b8bad2ba 2019-08-23 stsp }
3888 b8bad2ba 2019-08-23 stsp
3889 b8bad2ba 2019-08-23 stsp while (se) {
3890 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3891 b8bad2ba 2019-08-23 stsp if (err)
3892 b8bad2ba 2019-08-23 stsp break;
3893 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3894 b8bad2ba 2019-08-23 stsp free(se_id);
3895 b8bad2ba 2019-08-23 stsp if (err)
3896 b8bad2ba 2019-08-23 stsp break;
3897 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3898 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3899 b8bad2ba 2019-08-23 stsp
3900 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3901 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3902 b8bad2ba 2019-08-23 stsp if (err)
3903 b8bad2ba 2019-08-23 stsp return err;
3904 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3905 b8bad2ba 2019-08-23 stsp break;
3906 b8bad2ba 2019-08-23 stsp }
3907 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3908 b8bad2ba 2019-08-23 stsp continue;
3909 b8bad2ba 2019-08-23 stsp }
3910 b8bad2ba 2019-08-23 stsp }
3911 b8bad2ba 2019-08-23 stsp done:
3912 b8bad2ba 2019-08-23 stsp return err;
3913 8e7bd50a 2019-08-22 stsp }
3914 b8bad2ba 2019-08-23 stsp #endif
3915 b8bad2ba 2019-08-23 stsp
3916 b8bad2ba 2019-08-23 stsp static const struct got_error *
3917 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3918 8e7bd50a 2019-08-22 stsp {
3919 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3920 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3921 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3922 8e7bd50a 2019-08-22 stsp
3923 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3924 8e7bd50a 2019-08-22 stsp
3925 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3926 8e7bd50a 2019-08-22 stsp if (err)
3927 8e7bd50a 2019-08-22 stsp return err;
3928 8e7bd50a 2019-08-22 stsp
3929 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3930 8e7bd50a 2019-08-22 stsp const char *refname;
3931 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3932 8e7bd50a 2019-08-22 stsp char datebuf[26];
3933 d4efa91b 2020-01-14 stsp const char *tagger;
3934 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3935 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3936 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3937 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3938 8e7bd50a 2019-08-22 stsp
3939 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3940 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3941 8e7bd50a 2019-08-22 stsp continue;
3942 8e7bd50a 2019-08-22 stsp refname += 10;
3943 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3944 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3945 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3946 8e7bd50a 2019-08-22 stsp break;
3947 8e7bd50a 2019-08-22 stsp }
3948 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3949 8e7bd50a 2019-08-22 stsp free(refstr);
3950 8e7bd50a 2019-08-22 stsp
3951 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3952 8e7bd50a 2019-08-22 stsp if (err)
3953 8e7bd50a 2019-08-22 stsp break;
3954 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3955 d4efa91b 2020-01-14 stsp if (err) {
3956 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3957 d4efa91b 2020-01-14 stsp free(id);
3958 d4efa91b 2020-01-14 stsp break;
3959 d4efa91b 2020-01-14 stsp }
3960 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3961 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3962 d4efa91b 2020-01-14 stsp if (err) {
3963 d4efa91b 2020-01-14 stsp free(id);
3964 d4efa91b 2020-01-14 stsp break;
3965 d4efa91b 2020-01-14 stsp }
3966 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3967 d4efa91b 2020-01-14 stsp tagger_time =
3968 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3969 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3970 d4efa91b 2020-01-14 stsp free(id);
3971 d4efa91b 2020-01-14 stsp if (err)
3972 d4efa91b 2020-01-14 stsp break;
3973 d4efa91b 2020-01-14 stsp } else {
3974 d4efa91b 2020-01-14 stsp free(id);
3975 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3976 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3977 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3978 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3979 d4efa91b 2020-01-14 stsp if (err)
3980 d4efa91b 2020-01-14 stsp break;
3981 d4efa91b 2020-01-14 stsp }
3982 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3983 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3984 2417344c 2019-08-23 stsp if (datestr)
3985 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3986 d4efa91b 2020-01-14 stsp if (commit)
3987 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3988 d4efa91b 2020-01-14 stsp else {
3989 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3990 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3991 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3992 d4efa91b 2020-01-14 stsp id_str);
3993 d4efa91b 2020-01-14 stsp break;
3994 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3995 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3996 d4efa91b 2020-01-14 stsp id_str);
3997 d4efa91b 2020-01-14 stsp break;
3998 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3999 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4000 d4efa91b 2020-01-14 stsp id_str);
4001 d4efa91b 2020-01-14 stsp break;
4002 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4003 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4004 d4efa91b 2020-01-14 stsp id_str);
4005 d4efa91b 2020-01-14 stsp break;
4006 d4efa91b 2020-01-14 stsp default:
4007 d4efa91b 2020-01-14 stsp break;
4008 d4efa91b 2020-01-14 stsp }
4009 8e7bd50a 2019-08-22 stsp }
4010 8e7bd50a 2019-08-22 stsp free(id_str);
4011 d4efa91b 2020-01-14 stsp if (commit) {
4012 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4013 d4efa91b 2020-01-14 stsp if (err)
4014 d4efa91b 2020-01-14 stsp break;
4015 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4016 d4efa91b 2020-01-14 stsp } else {
4017 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4018 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4019 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4020 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4021 d4efa91b 2020-01-14 stsp break;
4022 d4efa91b 2020-01-14 stsp }
4023 8e7bd50a 2019-08-22 stsp }
4024 8e7bd50a 2019-08-22 stsp
4025 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4026 8e7bd50a 2019-08-22 stsp do {
4027 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4028 8e7bd50a 2019-08-22 stsp if (line)
4029 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4030 8e7bd50a 2019-08-22 stsp } while (line);
4031 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4032 8e7bd50a 2019-08-22 stsp }
4033 8e7bd50a 2019-08-22 stsp
4034 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4035 8e7bd50a 2019-08-22 stsp return NULL;
4036 8e7bd50a 2019-08-22 stsp }
4037 8e7bd50a 2019-08-22 stsp
4038 8e7bd50a 2019-08-22 stsp static const struct got_error *
4039 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4040 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4041 8e7bd50a 2019-08-22 stsp {
4042 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4043 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4044 f372d5cd 2019-10-21 stsp char *editor = NULL;
4045 8e7bd50a 2019-08-22 stsp int fd = -1;
4046 8e7bd50a 2019-08-22 stsp
4047 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4048 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4049 8e7bd50a 2019-08-22 stsp goto done;
4050 8e7bd50a 2019-08-22 stsp }
4051 8e7bd50a 2019-08-22 stsp
4052 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4053 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4054 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4055 8e7bd50a 2019-08-22 stsp goto done;
4056 8e7bd50a 2019-08-22 stsp }
4057 8e7bd50a 2019-08-22 stsp
4058 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4059 8e7bd50a 2019-08-22 stsp if (err)
4060 8e7bd50a 2019-08-22 stsp goto done;
4061 8e7bd50a 2019-08-22 stsp
4062 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4063 8e7bd50a 2019-08-22 stsp close(fd);
4064 8e7bd50a 2019-08-22 stsp
4065 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4066 8e7bd50a 2019-08-22 stsp if (err)
4067 8e7bd50a 2019-08-22 stsp goto done;
4068 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4069 8e7bd50a 2019-08-22 stsp done:
4070 8e7bd50a 2019-08-22 stsp free(initial_content);
4071 8e7bd50a 2019-08-22 stsp free(template);
4072 8e7bd50a 2019-08-22 stsp free(editor);
4073 8e7bd50a 2019-08-22 stsp
4074 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4075 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4076 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4077 8e7bd50a 2019-08-22 stsp if (err) {
4078 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4079 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4080 8e7bd50a 2019-08-22 stsp }
4081 8e7bd50a 2019-08-22 stsp }
4082 8e7bd50a 2019-08-22 stsp return err;
4083 8e7bd50a 2019-08-22 stsp }
4084 8e7bd50a 2019-08-22 stsp
4085 8e7bd50a 2019-08-22 stsp static const struct got_error *
4086 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4087 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4088 8e7bd50a 2019-08-22 stsp {
4089 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4090 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4091 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4092 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4093 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4094 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4095 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4096 8e7bd50a 2019-08-22 stsp
4097 8e7bd50a 2019-08-22 stsp /*
4098 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4099 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4100 8e7bd50a 2019-08-22 stsp * an unintended typo.
4101 8e7bd50a 2019-08-22 stsp */
4102 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4103 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4104 8e7bd50a 2019-08-22 stsp
4105 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4106 8e7bd50a 2019-08-22 stsp if (err)
4107 8e7bd50a 2019-08-22 stsp return err;
4108 8e7bd50a 2019-08-22 stsp
4109 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4110 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4111 8e7bd50a 2019-08-22 stsp if (err)
4112 8e7bd50a 2019-08-22 stsp goto done;
4113 8e7bd50a 2019-08-22 stsp
4114 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4115 8e7bd50a 2019-08-22 stsp if (err)
4116 8e7bd50a 2019-08-22 stsp goto done;
4117 8e7bd50a 2019-08-22 stsp
4118 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4119 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4120 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4121 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4122 8e7bd50a 2019-08-22 stsp goto done;
4123 8e7bd50a 2019-08-22 stsp }
4124 8e7bd50a 2019-08-22 stsp tag_name += 10;
4125 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4126 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4127 8e7bd50a 2019-08-22 stsp goto done;
4128 8e7bd50a 2019-08-22 stsp }
4129 8e7bd50a 2019-08-22 stsp
4130 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4131 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4132 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4133 8e7bd50a 2019-08-22 stsp goto done;
4134 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4135 8e7bd50a 2019-08-22 stsp goto done;
4136 8e7bd50a 2019-08-22 stsp
4137 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4138 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4139 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4140 f372d5cd 2019-10-21 stsp if (err) {
4141 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4142 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4143 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4144 8e7bd50a 2019-08-22 stsp goto done;
4145 f372d5cd 2019-10-21 stsp }
4146 8e7bd50a 2019-08-22 stsp }
4147 8e7bd50a 2019-08-22 stsp
4148 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4149 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4150 f372d5cd 2019-10-21 stsp if (err) {
4151 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4152 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4153 8e7bd50a 2019-08-22 stsp goto done;
4154 f372d5cd 2019-10-21 stsp }
4155 8e7bd50a 2019-08-22 stsp
4156 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4157 f372d5cd 2019-10-21 stsp if (err) {
4158 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4159 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4160 8e7bd50a 2019-08-22 stsp goto done;
4161 f372d5cd 2019-10-21 stsp }
4162 8e7bd50a 2019-08-22 stsp
4163 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4164 f372d5cd 2019-10-21 stsp if (err) {
4165 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4166 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4167 f372d5cd 2019-10-21 stsp goto done;
4168 f372d5cd 2019-10-21 stsp }
4169 8e7bd50a 2019-08-22 stsp
4170 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4171 f372d5cd 2019-10-21 stsp if (err) {
4172 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4173 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4174 f372d5cd 2019-10-21 stsp goto done;
4175 8e7bd50a 2019-08-22 stsp }
4176 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4177 8e7bd50a 2019-08-22 stsp done:
4178 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4179 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4180 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4181 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4182 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4183 f372d5cd 2019-10-21 stsp free(tag_id_str);
4184 8e7bd50a 2019-08-22 stsp if (ref)
4185 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4186 8e7bd50a 2019-08-22 stsp free(commit_id);
4187 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4188 8e7bd50a 2019-08-22 stsp free(refname);
4189 8e7bd50a 2019-08-22 stsp free(tagmsg);
4190 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4191 aba9c984 2019-09-08 stsp free(tagger);
4192 8e7bd50a 2019-08-22 stsp return err;
4193 8e7bd50a 2019-08-22 stsp }
4194 8e7bd50a 2019-08-22 stsp
4195 8e7bd50a 2019-08-22 stsp static const struct got_error *
4196 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4197 8e7bd50a 2019-08-22 stsp {
4198 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4199 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4200 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4201 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4202 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4203 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4204 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4205 8e7bd50a 2019-08-22 stsp
4206 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4207 8e7bd50a 2019-08-22 stsp switch (ch) {
4208 80106605 2020-02-24 stsp case 'c':
4209 80106605 2020-02-24 stsp commit_id_arg = optarg;
4210 80106605 2020-02-24 stsp break;
4211 8e7bd50a 2019-08-22 stsp case 'm':
4212 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4213 8e7bd50a 2019-08-22 stsp break;
4214 8e7bd50a 2019-08-22 stsp case 'r':
4215 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4216 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4217 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4218 9ba1d308 2019-10-21 stsp optarg);
4219 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4220 8e7bd50a 2019-08-22 stsp break;
4221 8e7bd50a 2019-08-22 stsp case 'l':
4222 8e7bd50a 2019-08-22 stsp do_list = 1;
4223 8e7bd50a 2019-08-22 stsp break;
4224 8e7bd50a 2019-08-22 stsp default:
4225 8e7bd50a 2019-08-22 stsp usage_tag();
4226 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4227 8e7bd50a 2019-08-22 stsp }
4228 8e7bd50a 2019-08-22 stsp }
4229 8e7bd50a 2019-08-22 stsp
4230 8e7bd50a 2019-08-22 stsp argc -= optind;
4231 8e7bd50a 2019-08-22 stsp argv += optind;
4232 8e7bd50a 2019-08-22 stsp
4233 8e7bd50a 2019-08-22 stsp if (do_list) {
4234 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4235 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4236 8e7bd50a 2019-08-22 stsp if (tagmsg)
4237 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4238 8e7bd50a 2019-08-22 stsp if (argc > 0)
4239 8e7bd50a 2019-08-22 stsp usage_tag();
4240 80106605 2020-02-24 stsp } else if (argc != 1)
4241 8e7bd50a 2019-08-22 stsp usage_tag();
4242 80106605 2020-02-24 stsp
4243 a2887370 2019-08-23 stsp tag_name = argv[0];
4244 8e7bd50a 2019-08-22 stsp
4245 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4246 8e7bd50a 2019-08-22 stsp if (do_list) {
4247 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4248 8e7bd50a 2019-08-22 stsp NULL) == -1)
4249 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4250 8e7bd50a 2019-08-22 stsp } else {
4251 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4252 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4253 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4254 8e7bd50a 2019-08-22 stsp }
4255 8e7bd50a 2019-08-22 stsp #endif
4256 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4257 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4258 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4259 8e7bd50a 2019-08-22 stsp goto done;
4260 8e7bd50a 2019-08-22 stsp }
4261 8e7bd50a 2019-08-22 stsp
4262 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4263 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4264 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4265 8e7bd50a 2019-08-22 stsp goto done;
4266 8e7bd50a 2019-08-22 stsp else
4267 8e7bd50a 2019-08-22 stsp error = NULL;
4268 8e7bd50a 2019-08-22 stsp if (worktree) {
4269 8e7bd50a 2019-08-22 stsp repo_path =
4270 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4271 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4272 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4273 8e7bd50a 2019-08-22 stsp if (error)
4274 8e7bd50a 2019-08-22 stsp goto done;
4275 8e7bd50a 2019-08-22 stsp } else {
4276 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4277 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4278 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4279 8e7bd50a 2019-08-22 stsp goto done;
4280 8e7bd50a 2019-08-22 stsp }
4281 8e7bd50a 2019-08-22 stsp }
4282 8e7bd50a 2019-08-22 stsp }
4283 8e7bd50a 2019-08-22 stsp
4284 8e7bd50a 2019-08-22 stsp if (do_list) {
4285 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4286 c9956ddf 2019-09-08 stsp if (error != NULL)
4287 c9956ddf 2019-09-08 stsp goto done;
4288 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4289 8e7bd50a 2019-08-22 stsp if (error)
4290 8e7bd50a 2019-08-22 stsp goto done;
4291 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4292 8e7bd50a 2019-08-22 stsp } else {
4293 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4294 c9956ddf 2019-09-08 stsp if (error)
4295 c9956ddf 2019-09-08 stsp goto done;
4296 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4297 c9956ddf 2019-09-08 stsp if (error != NULL)
4298 c9956ddf 2019-09-08 stsp goto done;
4299 c9956ddf 2019-09-08 stsp
4300 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4301 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4302 8e7bd50a 2019-08-22 stsp if (error)
4303 8e7bd50a 2019-08-22 stsp goto done;
4304 8e7bd50a 2019-08-22 stsp }
4305 8e7bd50a 2019-08-22 stsp
4306 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4307 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4308 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4309 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4310 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4311 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4312 8e7bd50a 2019-08-22 stsp if (error)
4313 8e7bd50a 2019-08-22 stsp goto done;
4314 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4315 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4316 8e7bd50a 2019-08-22 stsp if (error)
4317 8e7bd50a 2019-08-22 stsp goto done;
4318 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4319 8e7bd50a 2019-08-22 stsp free(commit_id);
4320 8e7bd50a 2019-08-22 stsp if (error)
4321 8e7bd50a 2019-08-22 stsp goto done;
4322 8e7bd50a 2019-08-22 stsp }
4323 8e7bd50a 2019-08-22 stsp
4324 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4325 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4326 8e7bd50a 2019-08-22 stsp }
4327 8e7bd50a 2019-08-22 stsp done:
4328 8e7bd50a 2019-08-22 stsp if (repo)
4329 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4330 8e7bd50a 2019-08-22 stsp if (worktree)
4331 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4332 8e7bd50a 2019-08-22 stsp free(cwd);
4333 8e7bd50a 2019-08-22 stsp free(repo_path);
4334 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4335 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4336 8e7bd50a 2019-08-22 stsp return error;
4337 8e7bd50a 2019-08-22 stsp }
4338 8e7bd50a 2019-08-22 stsp
4339 8e7bd50a 2019-08-22 stsp __dead static void
4340 d00136be 2019-03-26 stsp usage_add(void)
4341 d00136be 2019-03-26 stsp {
4342 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4343 022fae89 2019-12-06 tracey getprogname());
4344 d00136be 2019-03-26 stsp exit(1);
4345 d00136be 2019-03-26 stsp }
4346 d00136be 2019-03-26 stsp
4347 d00136be 2019-03-26 stsp static const struct got_error *
4348 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4349 4e68cba3 2019-11-23 stsp {
4350 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4351 4e68cba3 2019-11-23 stsp path++;
4352 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4353 4e68cba3 2019-11-23 stsp return NULL;
4354 4e68cba3 2019-11-23 stsp }
4355 4e68cba3 2019-11-23 stsp
4356 4e68cba3 2019-11-23 stsp static const struct got_error *
4357 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4358 d00136be 2019-03-26 stsp {
4359 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4360 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4361 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4362 1dd54920 2019-05-11 stsp char *cwd = NULL;
4363 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4364 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4365 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4366 1dd54920 2019-05-11 stsp
4367 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4368 d00136be 2019-03-26 stsp
4369 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4370 d00136be 2019-03-26 stsp switch (ch) {
4371 022fae89 2019-12-06 tracey case 'I':
4372 022fae89 2019-12-06 tracey no_ignores = 1;
4373 022fae89 2019-12-06 tracey break;
4374 4e68cba3 2019-11-23 stsp case 'R':
4375 4e68cba3 2019-11-23 stsp can_recurse = 1;
4376 4e68cba3 2019-11-23 stsp break;
4377 d00136be 2019-03-26 stsp default:
4378 d00136be 2019-03-26 stsp usage_add();
4379 d00136be 2019-03-26 stsp /* NOTREACHED */
4380 d00136be 2019-03-26 stsp }
4381 d00136be 2019-03-26 stsp }
4382 d00136be 2019-03-26 stsp
4383 d00136be 2019-03-26 stsp argc -= optind;
4384 d00136be 2019-03-26 stsp argv += optind;
4385 d00136be 2019-03-26 stsp
4386 43012d58 2019-07-14 stsp #ifndef PROFILE
4387 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4388 43012d58 2019-07-14 stsp NULL) == -1)
4389 43012d58 2019-07-14 stsp err(1, "pledge");
4390 43012d58 2019-07-14 stsp #endif
4391 723c305c 2019-05-11 jcs if (argc < 1)
4392 d00136be 2019-03-26 stsp usage_add();
4393 d00136be 2019-03-26 stsp
4394 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4395 d00136be 2019-03-26 stsp if (cwd == NULL) {
4396 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4397 d00136be 2019-03-26 stsp goto done;
4398 d00136be 2019-03-26 stsp }
4399 723c305c 2019-05-11 jcs
4400 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4401 d00136be 2019-03-26 stsp if (error)
4402 d00136be 2019-03-26 stsp goto done;
4403 d00136be 2019-03-26 stsp
4404 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4405 c9956ddf 2019-09-08 stsp NULL);
4406 031a5338 2019-03-26 stsp if (error != NULL)
4407 031a5338 2019-03-26 stsp goto done;
4408 031a5338 2019-03-26 stsp
4409 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4410 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4411 d00136be 2019-03-26 stsp if (error)
4412 d00136be 2019-03-26 stsp goto done;
4413 d00136be 2019-03-26 stsp
4414 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4415 6d022e97 2019-08-04 stsp if (error)
4416 022fae89 2019-12-06 tracey goto done;
4417 022fae89 2019-12-06 tracey
4418 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4419 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4420 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4421 6d022e97 2019-08-04 stsp goto done;
4422 723c305c 2019-05-11 jcs
4423 022fae89 2019-12-06 tracey }
4424 022fae89 2019-12-06 tracey
4425 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4426 4e68cba3 2019-11-23 stsp char *ondisk_path;
4427 4e68cba3 2019-11-23 stsp struct stat sb;
4428 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4429 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4430 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4431 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4432 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4433 4e68cba3 2019-11-23 stsp goto done;
4434 4e68cba3 2019-11-23 stsp }
4435 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4436 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4437 4e68cba3 2019-11-23 stsp free(ondisk_path);
4438 4e68cba3 2019-11-23 stsp continue;
4439 4e68cba3 2019-11-23 stsp }
4440 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4441 4e68cba3 2019-11-23 stsp ondisk_path);
4442 4e68cba3 2019-11-23 stsp free(ondisk_path);
4443 4e68cba3 2019-11-23 stsp goto done;
4444 4e68cba3 2019-11-23 stsp }
4445 4e68cba3 2019-11-23 stsp free(ondisk_path);
4446 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4447 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4448 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4449 4e68cba3 2019-11-23 stsp goto done;
4450 4e68cba3 2019-11-23 stsp }
4451 4e68cba3 2019-11-23 stsp }
4452 4e68cba3 2019-11-23 stsp }
4453 022fae89 2019-12-06 tracey
4454 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4455 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4456 d00136be 2019-03-26 stsp done:
4457 031a5338 2019-03-26 stsp if (repo)
4458 031a5338 2019-03-26 stsp got_repo_close(repo);
4459 d00136be 2019-03-26 stsp if (worktree)
4460 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4461 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4462 1dd54920 2019-05-11 stsp free((char *)pe->path);
4463 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4464 2ec1f75b 2019-03-26 stsp free(cwd);
4465 2ec1f75b 2019-03-26 stsp return error;
4466 2ec1f75b 2019-03-26 stsp }
4467 2ec1f75b 2019-03-26 stsp
4468 2ec1f75b 2019-03-26 stsp __dead static void
4469 648e4ef7 2019-07-09 stsp usage_remove(void)
4470 2ec1f75b 2019-03-26 stsp {
4471 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4472 f2a9dc41 2019-12-13 tracey getprogname());
4473 2ec1f75b 2019-03-26 stsp exit(1);
4474 2a06fe5f 2019-08-24 stsp }
4475 2a06fe5f 2019-08-24 stsp
4476 2a06fe5f 2019-08-24 stsp static const struct got_error *
4477 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4478 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4479 2a06fe5f 2019-08-24 stsp {
4480 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4481 f2a9dc41 2019-12-13 tracey path++;
4482 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4483 2a06fe5f 2019-08-24 stsp return NULL;
4484 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4485 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4486 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4487 2a06fe5f 2019-08-24 stsp return NULL;
4488 2ec1f75b 2019-03-26 stsp }
4489 2ec1f75b 2019-03-26 stsp
4490 2ec1f75b 2019-03-26 stsp static const struct got_error *
4491 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4492 2ec1f75b 2019-03-26 stsp {
4493 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4494 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4495 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4496 17ed4618 2019-06-02 stsp char *cwd = NULL;
4497 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4498 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4499 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4500 2ec1f75b 2019-03-26 stsp
4501 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4502 17ed4618 2019-06-02 stsp
4503 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4504 2ec1f75b 2019-03-26 stsp switch (ch) {
4505 2ec1f75b 2019-03-26 stsp case 'f':
4506 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4507 2ec1f75b 2019-03-26 stsp break;
4508 70e3e7f5 2019-12-13 tracey case 'k':
4509 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4510 70e3e7f5 2019-12-13 tracey break;
4511 f2a9dc41 2019-12-13 tracey case 'R':
4512 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4513 f2a9dc41 2019-12-13 tracey break;
4514 2ec1f75b 2019-03-26 stsp default:
4515 f2a9dc41 2019-12-13 tracey usage_remove();
4516 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4517 2ec1f75b 2019-03-26 stsp }
4518 2ec1f75b 2019-03-26 stsp }
4519 2ec1f75b 2019-03-26 stsp
4520 2ec1f75b 2019-03-26 stsp argc -= optind;
4521 2ec1f75b 2019-03-26 stsp argv += optind;
4522 2ec1f75b 2019-03-26 stsp
4523 43012d58 2019-07-14 stsp #ifndef PROFILE
4524 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4525 43012d58 2019-07-14 stsp NULL) == -1)
4526 43012d58 2019-07-14 stsp err(1, "pledge");
4527 43012d58 2019-07-14 stsp #endif
4528 17ed4618 2019-06-02 stsp if (argc < 1)
4529 648e4ef7 2019-07-09 stsp usage_remove();
4530 2ec1f75b 2019-03-26 stsp
4531 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4532 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4533 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4534 2ec1f75b 2019-03-26 stsp goto done;
4535 2ec1f75b 2019-03-26 stsp }
4536 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4537 2ec1f75b 2019-03-26 stsp if (error)
4538 2ec1f75b 2019-03-26 stsp goto done;
4539 2ec1f75b 2019-03-26 stsp
4540 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4541 c9956ddf 2019-09-08 stsp NULL);
4542 2af4a041 2019-05-11 jcs if (error)
4543 2ec1f75b 2019-03-26 stsp goto done;
4544 2ec1f75b 2019-03-26 stsp
4545 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4546 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4547 2ec1f75b 2019-03-26 stsp if (error)
4548 2ec1f75b 2019-03-26 stsp goto done;
4549 2ec1f75b 2019-03-26 stsp
4550 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4551 6d022e97 2019-08-04 stsp if (error)
4552 6d022e97 2019-08-04 stsp goto done;
4553 17ed4618 2019-06-02 stsp
4554 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4555 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4556 f2a9dc41 2019-12-13 tracey struct stat sb;
4557 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4558 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4559 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4560 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4561 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4562 f2a9dc41 2019-12-13 tracey goto done;
4563 f2a9dc41 2019-12-13 tracey }
4564 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4565 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4566 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4567 f2a9dc41 2019-12-13 tracey continue;
4568 f2a9dc41 2019-12-13 tracey }
4569 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4570 f2a9dc41 2019-12-13 tracey ondisk_path);
4571 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4572 f2a9dc41 2019-12-13 tracey goto done;
4573 f2a9dc41 2019-12-13 tracey }
4574 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4575 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4576 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4577 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4578 f2a9dc41 2019-12-13 tracey goto done;
4579 f2a9dc41 2019-12-13 tracey }
4580 f2a9dc41 2019-12-13 tracey }
4581 f2a9dc41 2019-12-13 tracey }
4582 f2a9dc41 2019-12-13 tracey
4583 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4584 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4585 a129376b 2019-03-28 stsp done:
4586 a129376b 2019-03-28 stsp if (repo)
4587 a129376b 2019-03-28 stsp got_repo_close(repo);
4588 a129376b 2019-03-28 stsp if (worktree)
4589 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4590 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4591 17ed4618 2019-06-02 stsp free((char *)pe->path);
4592 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4593 a129376b 2019-03-28 stsp free(cwd);
4594 a129376b 2019-03-28 stsp return error;
4595 a129376b 2019-03-28 stsp }
4596 a129376b 2019-03-28 stsp
4597 a129376b 2019-03-28 stsp __dead static void
4598 a129376b 2019-03-28 stsp usage_revert(void)
4599 a129376b 2019-03-28 stsp {
4600 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4601 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4602 a129376b 2019-03-28 stsp exit(1);
4603 a129376b 2019-03-28 stsp }
4604 a129376b 2019-03-28 stsp
4605 1ee397ad 2019-07-12 stsp static const struct got_error *
4606 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4607 a129376b 2019-03-28 stsp {
4608 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4609 fb9704af 2020-01-27 stsp return NULL;
4610 fb9704af 2020-01-27 stsp
4611 a129376b 2019-03-28 stsp while (path[0] == '/')
4612 a129376b 2019-03-28 stsp path++;
4613 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4614 33aa809d 2019-08-08 stsp return NULL;
4615 33aa809d 2019-08-08 stsp }
4616 33aa809d 2019-08-08 stsp
4617 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4618 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4619 33aa809d 2019-08-08 stsp const char *action;
4620 33aa809d 2019-08-08 stsp };
4621 33aa809d 2019-08-08 stsp
4622 33aa809d 2019-08-08 stsp static const struct got_error *
4623 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4624 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4625 33aa809d 2019-08-08 stsp {
4626 33aa809d 2019-08-08 stsp char *line = NULL;
4627 33aa809d 2019-08-08 stsp size_t linesize = 0;
4628 33aa809d 2019-08-08 stsp ssize_t linelen;
4629 33aa809d 2019-08-08 stsp
4630 33aa809d 2019-08-08 stsp switch (status) {
4631 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4632 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4633 33aa809d 2019-08-08 stsp break;
4634 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4635 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4636 33aa809d 2019-08-08 stsp break;
4637 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4638 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4639 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4640 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4641 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4642 33aa809d 2019-08-08 stsp printf("%s", line);
4643 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4644 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4645 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4646 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4647 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4648 33aa809d 2019-08-08 stsp break;
4649 33aa809d 2019-08-08 stsp default:
4650 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4651 33aa809d 2019-08-08 stsp }
4652 33aa809d 2019-08-08 stsp
4653 33aa809d 2019-08-08 stsp return NULL;
4654 33aa809d 2019-08-08 stsp }
4655 33aa809d 2019-08-08 stsp
4656 33aa809d 2019-08-08 stsp static const struct got_error *
4657 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4658 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4659 33aa809d 2019-08-08 stsp {
4660 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4661 33aa809d 2019-08-08 stsp char *line = NULL;
4662 33aa809d 2019-08-08 stsp size_t linesize = 0;
4663 33aa809d 2019-08-08 stsp ssize_t linelen;
4664 33aa809d 2019-08-08 stsp int resp = ' ';
4665 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4666 33aa809d 2019-08-08 stsp
4667 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4668 33aa809d 2019-08-08 stsp
4669 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4670 33aa809d 2019-08-08 stsp char *nl;
4671 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4672 33aa809d 2019-08-08 stsp a->action);
4673 33aa809d 2019-08-08 stsp if (err)
4674 33aa809d 2019-08-08 stsp return err;
4675 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4676 33aa809d 2019-08-08 stsp if (linelen == -1) {
4677 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4678 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4679 33aa809d 2019-08-08 stsp return NULL;
4680 33aa809d 2019-08-08 stsp }
4681 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4682 33aa809d 2019-08-08 stsp if (nl)
4683 33aa809d 2019-08-08 stsp *nl = '\0';
4684 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4685 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4686 33aa809d 2019-08-08 stsp printf("y\n");
4687 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4688 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4689 33aa809d 2019-08-08 stsp printf("n\n");
4690 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4691 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4692 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4693 33aa809d 2019-08-08 stsp printf("q\n");
4694 33aa809d 2019-08-08 stsp } else
4695 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4696 33aa809d 2019-08-08 stsp free(line);
4697 33aa809d 2019-08-08 stsp return NULL;
4698 33aa809d 2019-08-08 stsp }
4699 33aa809d 2019-08-08 stsp
4700 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4701 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4702 33aa809d 2019-08-08 stsp a->action);
4703 33aa809d 2019-08-08 stsp if (err)
4704 33aa809d 2019-08-08 stsp return err;
4705 33aa809d 2019-08-08 stsp resp = getchar();
4706 33aa809d 2019-08-08 stsp if (resp == '\n')
4707 33aa809d 2019-08-08 stsp resp = getchar();
4708 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4709 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4710 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4711 33aa809d 2019-08-08 stsp resp = ' ';
4712 33aa809d 2019-08-08 stsp }
4713 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4714 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4715 33aa809d 2019-08-08 stsp resp = ' ';
4716 33aa809d 2019-08-08 stsp }
4717 33aa809d 2019-08-08 stsp }
4718 33aa809d 2019-08-08 stsp
4719 33aa809d 2019-08-08 stsp if (resp == 'y')
4720 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4721 33aa809d 2019-08-08 stsp else if (resp == 'n')
4722 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4723 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4724 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4725 33aa809d 2019-08-08 stsp
4726 1ee397ad 2019-07-12 stsp return NULL;
4727 a129376b 2019-03-28 stsp }
4728 a129376b 2019-03-28 stsp
4729 33aa809d 2019-08-08 stsp
4730 a129376b 2019-03-28 stsp static const struct got_error *
4731 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4732 a129376b 2019-03-28 stsp {
4733 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4734 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4735 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4736 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4737 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4738 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4739 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4740 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4741 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4742 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4743 a129376b 2019-03-28 stsp
4744 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4745 e20a8b6f 2019-06-04 stsp
4746 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4747 a129376b 2019-03-28 stsp switch (ch) {
4748 33aa809d 2019-08-08 stsp case 'p':
4749 33aa809d 2019-08-08 stsp pflag = 1;
4750 33aa809d 2019-08-08 stsp break;
4751 33aa809d 2019-08-08 stsp case 'F':
4752 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4753 33aa809d 2019-08-08 stsp break;
4754 0f6d7415 2019-08-08 stsp case 'R':
4755 0f6d7415 2019-08-08 stsp can_recurse = 1;
4756 0f6d7415 2019-08-08 stsp break;
4757 a129376b 2019-03-28 stsp default:
4758 a129376b 2019-03-28 stsp usage_revert();
4759 a129376b 2019-03-28 stsp /* NOTREACHED */
4760 a129376b 2019-03-28 stsp }
4761 a129376b 2019-03-28 stsp }
4762 a129376b 2019-03-28 stsp
4763 a129376b 2019-03-28 stsp argc -= optind;
4764 a129376b 2019-03-28 stsp argv += optind;
4765 a129376b 2019-03-28 stsp
4766 43012d58 2019-07-14 stsp #ifndef PROFILE
4767 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4768 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4769 43012d58 2019-07-14 stsp err(1, "pledge");
4770 43012d58 2019-07-14 stsp #endif
4771 e20a8b6f 2019-06-04 stsp if (argc < 1)
4772 a129376b 2019-03-28 stsp usage_revert();
4773 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4774 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4775 a129376b 2019-03-28 stsp
4776 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4777 a129376b 2019-03-28 stsp if (cwd == NULL) {
4778 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4779 a129376b 2019-03-28 stsp goto done;
4780 a129376b 2019-03-28 stsp }
4781 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4782 2ec1f75b 2019-03-26 stsp if (error)
4783 2ec1f75b 2019-03-26 stsp goto done;
4784 a129376b 2019-03-28 stsp
4785 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4786 c9956ddf 2019-09-08 stsp NULL);
4787 a129376b 2019-03-28 stsp if (error != NULL)
4788 a129376b 2019-03-28 stsp goto done;
4789 a129376b 2019-03-28 stsp
4790 33aa809d 2019-08-08 stsp if (patch_script_path) {
4791 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4792 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4793 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4794 33aa809d 2019-08-08 stsp patch_script_path);
4795 33aa809d 2019-08-08 stsp goto done;
4796 33aa809d 2019-08-08 stsp }
4797 33aa809d 2019-08-08 stsp }
4798 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4799 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4800 a129376b 2019-03-28 stsp if (error)
4801 a129376b 2019-03-28 stsp goto done;
4802 a129376b 2019-03-28 stsp
4803 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4804 6d022e97 2019-08-04 stsp if (error)
4805 6d022e97 2019-08-04 stsp goto done;
4806 00db391e 2019-08-03 stsp
4807 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4808 0f6d7415 2019-08-08 stsp char *ondisk_path;
4809 0f6d7415 2019-08-08 stsp struct stat sb;
4810 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4811 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4812 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4813 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4814 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4815 0f6d7415 2019-08-08 stsp goto done;
4816 0f6d7415 2019-08-08 stsp }
4817 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4818 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4819 0f6d7415 2019-08-08 stsp free(ondisk_path);
4820 0f6d7415 2019-08-08 stsp continue;
4821 0f6d7415 2019-08-08 stsp }
4822 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4823 0f6d7415 2019-08-08 stsp ondisk_path);
4824 0f6d7415 2019-08-08 stsp free(ondisk_path);
4825 0f6d7415 2019-08-08 stsp goto done;
4826 0f6d7415 2019-08-08 stsp }
4827 0f6d7415 2019-08-08 stsp free(ondisk_path);
4828 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4829 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4830 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4831 0f6d7415 2019-08-08 stsp goto done;
4832 0f6d7415 2019-08-08 stsp }
4833 0f6d7415 2019-08-08 stsp }
4834 0f6d7415 2019-08-08 stsp }
4835 0f6d7415 2019-08-08 stsp
4836 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4837 33aa809d 2019-08-08 stsp cpa.action = "revert";
4838 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4839 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4840 2ec1f75b 2019-03-26 stsp done:
4841 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4842 33aa809d 2019-08-08 stsp error == NULL)
4843 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4844 2ec1f75b 2019-03-26 stsp if (repo)
4845 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4846 2ec1f75b 2019-03-26 stsp if (worktree)
4847 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4848 2ec1f75b 2019-03-26 stsp free(path);
4849 d00136be 2019-03-26 stsp free(cwd);
4850 6bad629b 2019-02-04 stsp return error;
4851 c4296144 2019-05-09 stsp }
4852 c4296144 2019-05-09 stsp
4853 c4296144 2019-05-09 stsp __dead static void
4854 c4296144 2019-05-09 stsp usage_commit(void)
4855 c4296144 2019-05-09 stsp {
4856 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4857 5c1e53bc 2019-07-28 stsp getprogname());
4858 c4296144 2019-05-09 stsp exit(1);
4859 33ad4cbe 2019-05-12 jcs }
4860 33ad4cbe 2019-05-12 jcs
4861 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4862 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4863 e2ba3d07 2019-05-13 stsp const char *editor;
4864 e0870e44 2019-05-13 stsp const char *worktree_path;
4865 76d98825 2019-06-03 stsp const char *branch_name;
4866 314a6357 2019-05-13 stsp const char *repo_path;
4867 e0870e44 2019-05-13 stsp char *logmsg_path;
4868 e2ba3d07 2019-05-13 stsp
4869 e2ba3d07 2019-05-13 stsp };
4870 e0870e44 2019-05-13 stsp
4871 33ad4cbe 2019-05-12 jcs static const struct got_error *
4872 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4873 33ad4cbe 2019-05-12 jcs void *arg)
4874 33ad4cbe 2019-05-12 jcs {
4875 76d98825 2019-06-03 stsp char *initial_content = NULL;
4876 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4877 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4878 e0870e44 2019-05-13 stsp char *template = NULL;
4879 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4880 3ce1b845 2019-07-15 stsp int fd;
4881 33ad4cbe 2019-05-12 jcs size_t len;
4882 33ad4cbe 2019-05-12 jcs
4883 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4884 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4885 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4886 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4887 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4888 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4889 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4890 33ad4cbe 2019-05-12 jcs return NULL;
4891 33ad4cbe 2019-05-12 jcs }
4892 33ad4cbe 2019-05-12 jcs
4893 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4894 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4895 76d98825 2019-06-03 stsp
4896 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4897 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4898 76d98825 2019-06-03 stsp a->branch_name) == -1)
4899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4900 e0870e44 2019-05-13 stsp
4901 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4902 793c30b5 2019-05-13 stsp if (err)
4903 e0870e44 2019-05-13 stsp goto done;
4904 33ad4cbe 2019-05-12 jcs
4905 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4906 33ad4cbe 2019-05-12 jcs
4907 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4908 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4909 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4910 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4911 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4912 33ad4cbe 2019-05-12 jcs }
4913 33ad4cbe 2019-05-12 jcs close(fd);
4914 33ad4cbe 2019-05-12 jcs
4915 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4916 33ad4cbe 2019-05-12 jcs done:
4917 76d98825 2019-06-03 stsp free(initial_content);
4918 e0870e44 2019-05-13 stsp free(template);
4919 314a6357 2019-05-13 stsp
4920 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4921 3ce1b845 2019-07-15 stsp if (err == NULL) {
4922 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4923 3ce1b845 2019-07-15 stsp if (err) {
4924 3ce1b845 2019-07-15 stsp free(*logmsg);
4925 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4926 3ce1b845 2019-07-15 stsp }
4927 3ce1b845 2019-07-15 stsp }
4928 33ad4cbe 2019-05-12 jcs return err;
4929 6bad629b 2019-02-04 stsp }
4930 c4296144 2019-05-09 stsp
4931 c4296144 2019-05-09 stsp static const struct got_error *
4932 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4933 c4296144 2019-05-09 stsp {
4934 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4935 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4936 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4937 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4938 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4939 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4940 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4941 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4942 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4943 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4944 5c1e53bc 2019-07-28 stsp
4945 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4946 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4947 c4296144 2019-05-09 stsp
4948 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4949 c4296144 2019-05-09 stsp switch (ch) {
4950 c4296144 2019-05-09 stsp case 'm':
4951 c4296144 2019-05-09 stsp logmsg = optarg;
4952 c4296144 2019-05-09 stsp break;
4953 c4296144 2019-05-09 stsp default:
4954 c4296144 2019-05-09 stsp usage_commit();
4955 c4296144 2019-05-09 stsp /* NOTREACHED */
4956 c4296144 2019-05-09 stsp }
4957 c4296144 2019-05-09 stsp }
4958 c4296144 2019-05-09 stsp
4959 c4296144 2019-05-09 stsp argc -= optind;
4960 c4296144 2019-05-09 stsp argv += optind;
4961 c4296144 2019-05-09 stsp
4962 43012d58 2019-07-14 stsp #ifndef PROFILE
4963 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4964 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4965 43012d58 2019-07-14 stsp err(1, "pledge");
4966 43012d58 2019-07-14 stsp #endif
4967 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4968 c4296144 2019-05-09 stsp if (cwd == NULL) {
4969 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4970 c4296144 2019-05-09 stsp goto done;
4971 c4296144 2019-05-09 stsp }
4972 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4973 7d5807f4 2019-07-11 stsp if (error)
4974 7d5807f4 2019-07-11 stsp goto done;
4975 7d5807f4 2019-07-11 stsp
4976 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4977 c4296144 2019-05-09 stsp if (error)
4978 a698f62e 2019-07-25 stsp goto done;
4979 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4980 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4981 c4296144 2019-05-09 stsp goto done;
4982 a698f62e 2019-07-25 stsp }
4983 5c1e53bc 2019-07-28 stsp
4984 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4985 916f288c 2019-07-30 stsp worktree);
4986 5c1e53bc 2019-07-28 stsp if (error)
4987 5c1e53bc 2019-07-28 stsp goto done;
4988 c4296144 2019-05-09 stsp
4989 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4990 c9956ddf 2019-09-08 stsp if (error)
4991 c9956ddf 2019-09-08 stsp goto done;
4992 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4993 c9956ddf 2019-09-08 stsp gitconfig_path);
4994 c4296144 2019-05-09 stsp if (error != NULL)
4995 c4296144 2019-05-09 stsp goto done;
4996 c4296144 2019-05-09 stsp
4997 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4998 aba9c984 2019-09-08 stsp if (error)
4999 aba9c984 2019-09-08 stsp return error;
5000 aba9c984 2019-09-08 stsp
5001 314a6357 2019-05-13 stsp /*
5002 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5003 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5004 314a6357 2019-05-13 stsp */
5005 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5006 314a6357 2019-05-13 stsp error = get_editor(&editor);
5007 314a6357 2019-05-13 stsp else
5008 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5009 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5010 c4296144 2019-05-09 stsp if (error)
5011 c4296144 2019-05-09 stsp goto done;
5012 c4296144 2019-05-09 stsp
5013 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5014 087fb88c 2019-08-04 stsp if (error)
5015 087fb88c 2019-08-04 stsp goto done;
5016 087fb88c 2019-08-04 stsp
5017 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5018 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5019 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5020 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5021 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5022 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5023 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5024 916f288c 2019-07-30 stsp goto done;
5025 916f288c 2019-07-30 stsp }
5026 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5027 916f288c 2019-07-30 stsp }
5028 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5029 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5030 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5031 e0870e44 2019-05-13 stsp if (error) {
5032 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5033 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5034 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5035 c4296144 2019-05-09 stsp goto done;
5036 e0870e44 2019-05-13 stsp }
5037 c4296144 2019-05-09 stsp
5038 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5039 c4296144 2019-05-09 stsp if (error)
5040 c4296144 2019-05-09 stsp goto done;
5041 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5042 c4296144 2019-05-09 stsp done:
5043 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5044 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5045 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5046 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5047 7266f21f 2019-10-21 stsp error == NULL)
5048 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5049 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5050 c4296144 2019-05-09 stsp if (repo)
5051 c4296144 2019-05-09 stsp got_repo_close(repo);
5052 c4296144 2019-05-09 stsp if (worktree)
5053 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5054 c4296144 2019-05-09 stsp free(cwd);
5055 c4296144 2019-05-09 stsp free(id_str);
5056 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5057 e2ba3d07 2019-05-13 stsp free(editor);
5058 aba9c984 2019-09-08 stsp free(author);
5059 234035bc 2019-06-01 stsp return error;
5060 234035bc 2019-06-01 stsp }
5061 234035bc 2019-06-01 stsp
5062 234035bc 2019-06-01 stsp __dead static void
5063 234035bc 2019-06-01 stsp usage_cherrypick(void)
5064 234035bc 2019-06-01 stsp {
5065 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5066 234035bc 2019-06-01 stsp exit(1);
5067 234035bc 2019-06-01 stsp }
5068 234035bc 2019-06-01 stsp
5069 234035bc 2019-06-01 stsp static const struct got_error *
5070 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5071 234035bc 2019-06-01 stsp {
5072 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5073 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5074 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5075 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5076 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5077 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5078 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5079 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5080 234035bc 2019-06-01 stsp int ch, did_something = 0;
5081 234035bc 2019-06-01 stsp
5082 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5083 234035bc 2019-06-01 stsp switch (ch) {
5084 234035bc 2019-06-01 stsp default:
5085 234035bc 2019-06-01 stsp usage_cherrypick();
5086 234035bc 2019-06-01 stsp /* NOTREACHED */
5087 234035bc 2019-06-01 stsp }
5088 234035bc 2019-06-01 stsp }
5089 234035bc 2019-06-01 stsp
5090 234035bc 2019-06-01 stsp argc -= optind;
5091 234035bc 2019-06-01 stsp argv += optind;
5092 234035bc 2019-06-01 stsp
5093 43012d58 2019-07-14 stsp #ifndef PROFILE
5094 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5095 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5096 43012d58 2019-07-14 stsp err(1, "pledge");
5097 43012d58 2019-07-14 stsp #endif
5098 234035bc 2019-06-01 stsp if (argc != 1)
5099 234035bc 2019-06-01 stsp usage_cherrypick();
5100 234035bc 2019-06-01 stsp
5101 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5102 234035bc 2019-06-01 stsp if (cwd == NULL) {
5103 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5104 234035bc 2019-06-01 stsp goto done;
5105 234035bc 2019-06-01 stsp }
5106 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5107 234035bc 2019-06-01 stsp if (error)
5108 234035bc 2019-06-01 stsp goto done;
5109 234035bc 2019-06-01 stsp
5110 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5111 c9956ddf 2019-09-08 stsp NULL);
5112 234035bc 2019-06-01 stsp if (error != NULL)
5113 234035bc 2019-06-01 stsp goto done;
5114 234035bc 2019-06-01 stsp
5115 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5116 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5117 234035bc 2019-06-01 stsp if (error)
5118 234035bc 2019-06-01 stsp goto done;
5119 234035bc 2019-06-01 stsp
5120 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5121 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5122 234035bc 2019-06-01 stsp if (error != NULL) {
5123 234035bc 2019-06-01 stsp struct got_reference *ref;
5124 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5125 234035bc 2019-06-01 stsp goto done;
5126 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5127 234035bc 2019-06-01 stsp if (error != NULL)
5128 234035bc 2019-06-01 stsp goto done;
5129 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5130 234035bc 2019-06-01 stsp got_ref_close(ref);
5131 234035bc 2019-06-01 stsp if (error != NULL)
5132 234035bc 2019-06-01 stsp goto done;
5133 234035bc 2019-06-01 stsp }
5134 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5135 234035bc 2019-06-01 stsp if (error)
5136 234035bc 2019-06-01 stsp goto done;
5137 234035bc 2019-06-01 stsp
5138 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5139 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5140 234035bc 2019-06-01 stsp if (error != NULL)
5141 234035bc 2019-06-01 stsp goto done;
5142 234035bc 2019-06-01 stsp
5143 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5144 234035bc 2019-06-01 stsp if (error) {
5145 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5146 234035bc 2019-06-01 stsp goto done;
5147 234035bc 2019-06-01 stsp error = NULL;
5148 234035bc 2019-06-01 stsp } else {
5149 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5150 234035bc 2019-06-01 stsp goto done;
5151 234035bc 2019-06-01 stsp }
5152 234035bc 2019-06-01 stsp
5153 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5154 234035bc 2019-06-01 stsp if (error)
5155 234035bc 2019-06-01 stsp goto done;
5156 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5157 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5158 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5159 03415a1a 2019-06-02 stsp NULL);
5160 234035bc 2019-06-01 stsp if (error != NULL)
5161 234035bc 2019-06-01 stsp goto done;
5162 234035bc 2019-06-01 stsp
5163 234035bc 2019-06-01 stsp if (did_something)
5164 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5165 234035bc 2019-06-01 stsp done:
5166 234035bc 2019-06-01 stsp if (commit)
5167 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5168 234035bc 2019-06-01 stsp free(commit_id_str);
5169 234035bc 2019-06-01 stsp if (head_ref)
5170 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5171 234035bc 2019-06-01 stsp if (worktree)
5172 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5173 234035bc 2019-06-01 stsp if (repo)
5174 234035bc 2019-06-01 stsp got_repo_close(repo);
5175 c4296144 2019-05-09 stsp return error;
5176 c4296144 2019-05-09 stsp }
5177 5ef14e63 2019-06-02 stsp
5178 5ef14e63 2019-06-02 stsp __dead static void
5179 5ef14e63 2019-06-02 stsp usage_backout(void)
5180 5ef14e63 2019-06-02 stsp {
5181 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5182 5ef14e63 2019-06-02 stsp exit(1);
5183 5ef14e63 2019-06-02 stsp }
5184 5ef14e63 2019-06-02 stsp
5185 5ef14e63 2019-06-02 stsp static const struct got_error *
5186 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5187 5ef14e63 2019-06-02 stsp {
5188 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5189 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5190 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5191 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5192 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5193 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5194 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5195 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5196 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5197 5ef14e63 2019-06-02 stsp
5198 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5199 5ef14e63 2019-06-02 stsp switch (ch) {
5200 5ef14e63 2019-06-02 stsp default:
5201 5ef14e63 2019-06-02 stsp usage_backout();
5202 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5203 5ef14e63 2019-06-02 stsp }
5204 5ef14e63 2019-06-02 stsp }
5205 5ef14e63 2019-06-02 stsp
5206 5ef14e63 2019-06-02 stsp argc -= optind;
5207 5ef14e63 2019-06-02 stsp argv += optind;
5208 5ef14e63 2019-06-02 stsp
5209 43012d58 2019-07-14 stsp #ifndef PROFILE
5210 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5211 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5212 43012d58 2019-07-14 stsp err(1, "pledge");
5213 43012d58 2019-07-14 stsp #endif
5214 5ef14e63 2019-06-02 stsp if (argc != 1)
5215 5ef14e63 2019-06-02 stsp usage_backout();
5216 5ef14e63 2019-06-02 stsp
5217 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5218 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5219 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5220 5ef14e63 2019-06-02 stsp goto done;
5221 5ef14e63 2019-06-02 stsp }
5222 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5223 5ef14e63 2019-06-02 stsp if (error)
5224 5ef14e63 2019-06-02 stsp goto done;
5225 5ef14e63 2019-06-02 stsp
5226 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5227 c9956ddf 2019-09-08 stsp NULL);
5228 5ef14e63 2019-06-02 stsp if (error != NULL)
5229 5ef14e63 2019-06-02 stsp goto done;
5230 5ef14e63 2019-06-02 stsp
5231 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5232 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5233 5ef14e63 2019-06-02 stsp if (error)
5234 5ef14e63 2019-06-02 stsp goto done;
5235 5ef14e63 2019-06-02 stsp
5236 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5237 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5238 5ef14e63 2019-06-02 stsp if (error != NULL) {
5239 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5240 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5241 5ef14e63 2019-06-02 stsp goto done;
5242 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5243 5ef14e63 2019-06-02 stsp if (error != NULL)
5244 5ef14e63 2019-06-02 stsp goto done;
5245 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5246 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5247 5ef14e63 2019-06-02 stsp if (error != NULL)
5248 5ef14e63 2019-06-02 stsp goto done;
5249 5ef14e63 2019-06-02 stsp }
5250 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5251 5ef14e63 2019-06-02 stsp if (error)
5252 5ef14e63 2019-06-02 stsp goto done;
5253 5ef14e63 2019-06-02 stsp
5254 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5255 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5256 5ef14e63 2019-06-02 stsp if (error != NULL)
5257 5ef14e63 2019-06-02 stsp goto done;
5258 5ef14e63 2019-06-02 stsp
5259 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5260 5ef14e63 2019-06-02 stsp if (error)
5261 5ef14e63 2019-06-02 stsp goto done;
5262 5ef14e63 2019-06-02 stsp
5263 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5264 5ef14e63 2019-06-02 stsp if (error)
5265 5ef14e63 2019-06-02 stsp goto done;
5266 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5267 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5268 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5269 5ef14e63 2019-06-02 stsp goto done;
5270 5ef14e63 2019-06-02 stsp }
5271 5ef14e63 2019-06-02 stsp
5272 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5273 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5274 5ef14e63 2019-06-02 stsp if (error != NULL)
5275 5ef14e63 2019-06-02 stsp goto done;
5276 5ef14e63 2019-06-02 stsp
5277 5ef14e63 2019-06-02 stsp if (did_something)
5278 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5279 5ef14e63 2019-06-02 stsp done:
5280 5ef14e63 2019-06-02 stsp if (commit)
5281 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5282 5ef14e63 2019-06-02 stsp free(commit_id_str);
5283 5ef14e63 2019-06-02 stsp if (head_ref)
5284 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5285 818c7501 2019-07-11 stsp if (worktree)
5286 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5287 818c7501 2019-07-11 stsp if (repo)
5288 818c7501 2019-07-11 stsp got_repo_close(repo);
5289 818c7501 2019-07-11 stsp return error;
5290 818c7501 2019-07-11 stsp }
5291 818c7501 2019-07-11 stsp
5292 818c7501 2019-07-11 stsp __dead static void
5293 818c7501 2019-07-11 stsp usage_rebase(void)
5294 818c7501 2019-07-11 stsp {
5295 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5296 af54c8f8 2019-07-11 stsp getprogname());
5297 818c7501 2019-07-11 stsp exit(1);
5298 0ebf8283 2019-07-24 stsp }
5299 0ebf8283 2019-07-24 stsp
5300 0ebf8283 2019-07-24 stsp void
5301 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5302 0ebf8283 2019-07-24 stsp {
5303 0ebf8283 2019-07-24 stsp char *nl;
5304 0ebf8283 2019-07-24 stsp size_t len;
5305 0ebf8283 2019-07-24 stsp
5306 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5307 0ebf8283 2019-07-24 stsp if (len > limit)
5308 0ebf8283 2019-07-24 stsp len = limit;
5309 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5310 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5311 0ebf8283 2019-07-24 stsp if (nl)
5312 0ebf8283 2019-07-24 stsp *nl = '\0';
5313 0ebf8283 2019-07-24 stsp }
5314 0ebf8283 2019-07-24 stsp
5315 0ebf8283 2019-07-24 stsp static const struct got_error *
5316 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5317 0ebf8283 2019-07-24 stsp {
5318 5943eee2 2019-08-13 stsp const struct got_error *err;
5319 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5320 5943eee2 2019-08-13 stsp const char *s;
5321 0ebf8283 2019-07-24 stsp
5322 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5323 5943eee2 2019-08-13 stsp if (err)
5324 5943eee2 2019-08-13 stsp return err;
5325 0ebf8283 2019-07-24 stsp
5326 5943eee2 2019-08-13 stsp s = logmsg0;
5327 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5328 5943eee2 2019-08-13 stsp s++;
5329 5943eee2 2019-08-13 stsp
5330 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5331 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5332 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5333 5943eee2 2019-08-13 stsp goto done;
5334 5943eee2 2019-08-13 stsp }
5335 0ebf8283 2019-07-24 stsp
5336 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5337 5943eee2 2019-08-13 stsp done:
5338 5943eee2 2019-08-13 stsp free(logmsg0);
5339 a0ea4fc0 2020-02-28 stsp return err;
5340 a0ea4fc0 2020-02-28 stsp }
5341 a0ea4fc0 2020-02-28 stsp
5342 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5343 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5344 a0ea4fc0 2020-02-28 stsp {
5345 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5346 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5347 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5348 a0ea4fc0 2020-02-28 stsp
5349 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5350 a0ea4fc0 2020-02-28 stsp if (err)
5351 a0ea4fc0 2020-02-28 stsp return err;
5352 a0ea4fc0 2020-02-28 stsp
5353 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5354 a0ea4fc0 2020-02-28 stsp if (err)
5355 a0ea4fc0 2020-02-28 stsp goto done;
5356 a0ea4fc0 2020-02-28 stsp
5357 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5358 a0ea4fc0 2020-02-28 stsp
5359 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5360 a0ea4fc0 2020-02-28 stsp if (err)
5361 a0ea4fc0 2020-02-28 stsp goto done;
5362 a0ea4fc0 2020-02-28 stsp
5363 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5364 a0ea4fc0 2020-02-28 stsp done:
5365 a0ea4fc0 2020-02-28 stsp free(id_str);
5366 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5367 a0ea4fc0 2020-02-28 stsp free(logmsg);
5368 5943eee2 2019-08-13 stsp return err;
5369 818c7501 2019-07-11 stsp }
5370 818c7501 2019-07-11 stsp
5371 818c7501 2019-07-11 stsp static const struct got_error *
5372 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5373 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5374 818c7501 2019-07-11 stsp {
5375 818c7501 2019-07-11 stsp const struct got_error *err;
5376 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5377 818c7501 2019-07-11 stsp
5378 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5379 818c7501 2019-07-11 stsp if (err)
5380 818c7501 2019-07-11 stsp goto done;
5381 818c7501 2019-07-11 stsp
5382 ff0d2220 2019-07-11 stsp if (new_id) {
5383 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5384 ff0d2220 2019-07-11 stsp if (err)
5385 ff0d2220 2019-07-11 stsp goto done;
5386 ff0d2220 2019-07-11 stsp }
5387 818c7501 2019-07-11 stsp
5388 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5389 ff0d2220 2019-07-11 stsp if (new_id_str)
5390 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5391 0ebf8283 2019-07-24 stsp
5392 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5393 0ebf8283 2019-07-24 stsp if (err)
5394 0ebf8283 2019-07-24 stsp goto done;
5395 0ebf8283 2019-07-24 stsp
5396 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5397 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5398 818c7501 2019-07-11 stsp done:
5399 818c7501 2019-07-11 stsp free(old_id_str);
5400 818c7501 2019-07-11 stsp free(new_id_str);
5401 272a1371 2020-02-28 stsp free(logmsg);
5402 818c7501 2019-07-11 stsp return err;
5403 818c7501 2019-07-11 stsp }
5404 818c7501 2019-07-11 stsp
5405 1ee397ad 2019-07-12 stsp static const struct got_error *
5406 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5407 818c7501 2019-07-11 stsp {
5408 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5409 818c7501 2019-07-11 stsp
5410 818c7501 2019-07-11 stsp while (path[0] == '/')
5411 818c7501 2019-07-11 stsp path++;
5412 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5413 818c7501 2019-07-11 stsp
5414 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5415 1ee397ad 2019-07-12 stsp return NULL;
5416 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5417 818c7501 2019-07-11 stsp *rebase_status = status;
5418 1ee397ad 2019-07-12 stsp return NULL;
5419 818c7501 2019-07-11 stsp }
5420 818c7501 2019-07-11 stsp
5421 818c7501 2019-07-11 stsp static const struct got_error *
5422 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5423 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5424 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5425 818c7501 2019-07-11 stsp {
5426 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5427 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5428 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5429 818c7501 2019-07-11 stsp }
5430 818c7501 2019-07-11 stsp
5431 818c7501 2019-07-11 stsp static const struct got_error *
5432 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5433 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5434 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5435 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5436 ff0d2220 2019-07-11 stsp {
5437 ff0d2220 2019-07-11 stsp const struct got_error *error;
5438 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5439 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5440 ff0d2220 2019-07-11 stsp
5441 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5442 ff0d2220 2019-07-11 stsp if (error)
5443 ff0d2220 2019-07-11 stsp return error;
5444 ff0d2220 2019-07-11 stsp
5445 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5446 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5447 ff0d2220 2019-07-11 stsp if (error) {
5448 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5449 ff0d2220 2019-07-11 stsp goto done;
5450 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5451 ff0d2220 2019-07-11 stsp } else {
5452 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5453 ff0d2220 2019-07-11 stsp free(new_commit_id);
5454 ff0d2220 2019-07-11 stsp }
5455 ff0d2220 2019-07-11 stsp done:
5456 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5457 ff0d2220 2019-07-11 stsp return error;
5458 64c6d990 2019-07-11 stsp }
5459 64c6d990 2019-07-11 stsp
5460 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5461 64c6d990 2019-07-11 stsp const char *path_prefix;
5462 64c6d990 2019-07-11 stsp size_t len;
5463 8ca9bd68 2019-07-25 stsp int errcode;
5464 64c6d990 2019-07-11 stsp };
5465 64c6d990 2019-07-11 stsp
5466 64c6d990 2019-07-11 stsp static const struct got_error *
5467 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5468 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5469 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5470 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5471 64c6d990 2019-07-11 stsp {
5472 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5473 64c6d990 2019-07-11 stsp
5474 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5475 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5476 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5477 64c6d990 2019-07-11 stsp
5478 64c6d990 2019-07-11 stsp return NULL;
5479 64c6d990 2019-07-11 stsp }
5480 64c6d990 2019-07-11 stsp
5481 64c6d990 2019-07-11 stsp static const struct got_error *
5482 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5483 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5484 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5485 64c6d990 2019-07-11 stsp {
5486 64c6d990 2019-07-11 stsp const struct got_error *err;
5487 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5488 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5489 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5490 64c6d990 2019-07-11 stsp
5491 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5492 64c6d990 2019-07-11 stsp return NULL;
5493 64c6d990 2019-07-11 stsp
5494 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5495 64c6d990 2019-07-11 stsp if (err)
5496 64c6d990 2019-07-11 stsp goto done;
5497 64c6d990 2019-07-11 stsp
5498 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5499 64c6d990 2019-07-11 stsp if (err)
5500 64c6d990 2019-07-11 stsp goto done;
5501 64c6d990 2019-07-11 stsp
5502 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5503 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5504 64c6d990 2019-07-11 stsp if (err)
5505 64c6d990 2019-07-11 stsp goto done;
5506 64c6d990 2019-07-11 stsp
5507 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5508 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5509 64c6d990 2019-07-11 stsp if (err)
5510 64c6d990 2019-07-11 stsp goto done;
5511 64c6d990 2019-07-11 stsp
5512 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5513 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5514 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5515 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5516 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5517 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5518 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5519 64c6d990 2019-07-11 stsp done:
5520 64c6d990 2019-07-11 stsp if (tree1)
5521 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5522 64c6d990 2019-07-11 stsp if (tree2)
5523 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5524 64c6d990 2019-07-11 stsp if (commit)
5525 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5526 64c6d990 2019-07-11 stsp if (parent_commit)
5527 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5528 64c6d990 2019-07-11 stsp return err;
5529 ff0d2220 2019-07-11 stsp }
5530 ff0d2220 2019-07-11 stsp
5531 ff0d2220 2019-07-11 stsp static const struct got_error *
5532 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5533 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5534 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5535 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5536 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5537 af61c510 2019-07-19 stsp {
5538 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5539 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5540 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5541 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5542 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5543 af61c510 2019-07-19 stsp
5544 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5545 af61c510 2019-07-19 stsp if (err)
5546 af61c510 2019-07-19 stsp return err;
5547 af61c510 2019-07-19 stsp
5548 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5549 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5550 af61c510 2019-07-19 stsp if (err)
5551 af61c510 2019-07-19 stsp goto done;
5552 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5553 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5554 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5555 af61c510 2019-07-19 stsp if (err) {
5556 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5557 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5558 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5559 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5560 af61c510 2019-07-19 stsp "been reached?!?");
5561 ee780d5c 2020-01-04 stsp }
5562 ee780d5c 2020-01-04 stsp goto done;
5563 af61c510 2019-07-19 stsp } else {
5564 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5565 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5566 af61c510 2019-07-19 stsp if (err)
5567 af61c510 2019-07-19 stsp goto done;
5568 af61c510 2019-07-19 stsp
5569 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5570 af61c510 2019-07-19 stsp if (err)
5571 af61c510 2019-07-19 stsp goto done;
5572 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5573 af61c510 2019-07-19 stsp commit_id = parent_id;
5574 af61c510 2019-07-19 stsp }
5575 af61c510 2019-07-19 stsp }
5576 af61c510 2019-07-19 stsp done:
5577 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5578 af61c510 2019-07-19 stsp return err;
5579 af61c510 2019-07-19 stsp }
5580 af61c510 2019-07-19 stsp
5581 af61c510 2019-07-19 stsp static const struct got_error *
5582 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5583 818c7501 2019-07-11 stsp {
5584 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5585 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5586 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5587 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5588 818c7501 2019-07-11 stsp char *cwd = NULL;
5589 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5590 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5591 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5592 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5593 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5594 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5595 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5596 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5597 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5598 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5599 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5600 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5601 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5602 818c7501 2019-07-11 stsp
5603 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5604 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5605 818c7501 2019-07-11 stsp
5606 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5607 818c7501 2019-07-11 stsp switch (ch) {
5608 818c7501 2019-07-11 stsp case 'a':
5609 818c7501 2019-07-11 stsp abort_rebase = 1;
5610 818c7501 2019-07-11 stsp break;
5611 818c7501 2019-07-11 stsp case 'c':
5612 818c7501 2019-07-11 stsp continue_rebase = 1;
5613 818c7501 2019-07-11 stsp break;
5614 818c7501 2019-07-11 stsp default:
5615 818c7501 2019-07-11 stsp usage_rebase();
5616 818c7501 2019-07-11 stsp /* NOTREACHED */
5617 818c7501 2019-07-11 stsp }
5618 818c7501 2019-07-11 stsp }
5619 818c7501 2019-07-11 stsp
5620 818c7501 2019-07-11 stsp argc -= optind;
5621 818c7501 2019-07-11 stsp argv += optind;
5622 818c7501 2019-07-11 stsp
5623 43012d58 2019-07-14 stsp #ifndef PROFILE
5624 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5625 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5626 43012d58 2019-07-14 stsp err(1, "pledge");
5627 43012d58 2019-07-14 stsp #endif
5628 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5629 818c7501 2019-07-11 stsp usage_rebase();
5630 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5631 818c7501 2019-07-11 stsp if (argc != 0)
5632 818c7501 2019-07-11 stsp usage_rebase();
5633 818c7501 2019-07-11 stsp } else if (argc != 1)
5634 818c7501 2019-07-11 stsp usage_rebase();
5635 818c7501 2019-07-11 stsp
5636 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5637 818c7501 2019-07-11 stsp if (cwd == NULL) {
5638 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5639 818c7501 2019-07-11 stsp goto done;
5640 818c7501 2019-07-11 stsp }
5641 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5642 818c7501 2019-07-11 stsp if (error)
5643 818c7501 2019-07-11 stsp goto done;
5644 818c7501 2019-07-11 stsp
5645 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5646 c9956ddf 2019-09-08 stsp NULL);
5647 818c7501 2019-07-11 stsp if (error != NULL)
5648 818c7501 2019-07-11 stsp goto done;
5649 818c7501 2019-07-11 stsp
5650 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5651 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5652 7ef62c4e 2020-02-24 stsp if (error)
5653 7ef62c4e 2020-02-24 stsp goto done;
5654 7ef62c4e 2020-02-24 stsp
5655 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5656 7ef62c4e 2020-02-24 stsp worktree);
5657 818c7501 2019-07-11 stsp if (error)
5658 7ef62c4e 2020-02-24 stsp goto done;
5659 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5660 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5661 818c7501 2019-07-11 stsp goto done;
5662 7ef62c4e 2020-02-24 stsp }
5663 818c7501 2019-07-11 stsp
5664 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5665 818c7501 2019-07-11 stsp if (error)
5666 818c7501 2019-07-11 stsp goto done;
5667 818c7501 2019-07-11 stsp
5668 f6794adc 2019-07-23 stsp if (abort_rebase) {
5669 818c7501 2019-07-11 stsp int did_something;
5670 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5671 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5672 f6794adc 2019-07-23 stsp goto done;
5673 f6794adc 2019-07-23 stsp }
5674 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5675 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5676 3e3a69f1 2019-07-25 stsp worktree, repo);
5677 818c7501 2019-07-11 stsp if (error)
5678 818c7501 2019-07-11 stsp goto done;
5679 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5680 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5681 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5682 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5683 818c7501 2019-07-11 stsp if (error)
5684 818c7501 2019-07-11 stsp goto done;
5685 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5686 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5687 818c7501 2019-07-11 stsp }
5688 818c7501 2019-07-11 stsp
5689 818c7501 2019-07-11 stsp if (continue_rebase) {
5690 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5691 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5692 f6794adc 2019-07-23 stsp goto done;
5693 f6794adc 2019-07-23 stsp }
5694 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5695 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5696 3e3a69f1 2019-07-25 stsp worktree, repo);
5697 818c7501 2019-07-11 stsp if (error)
5698 818c7501 2019-07-11 stsp goto done;
5699 818c7501 2019-07-11 stsp
5700 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5701 01757395 2019-07-12 stsp resume_commit_id, repo);
5702 818c7501 2019-07-11 stsp if (error)
5703 818c7501 2019-07-11 stsp goto done;
5704 818c7501 2019-07-11 stsp
5705 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5706 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5707 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5708 818c7501 2019-07-11 stsp goto done;
5709 818c7501 2019-07-11 stsp }
5710 818c7501 2019-07-11 stsp } else {
5711 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5712 818c7501 2019-07-11 stsp if (error != NULL)
5713 818c7501 2019-07-11 stsp goto done;
5714 ff0d2220 2019-07-11 stsp }
5715 818c7501 2019-07-11 stsp
5716 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5717 ff0d2220 2019-07-11 stsp if (error)
5718 ff0d2220 2019-07-11 stsp goto done;
5719 ff0d2220 2019-07-11 stsp
5720 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5721 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5722 a51a74b3 2019-07-27 stsp
5723 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5724 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5725 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5726 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5727 818c7501 2019-07-11 stsp if (error)
5728 818c7501 2019-07-11 stsp goto done;
5729 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5730 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5731 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5732 818c7501 2019-07-11 stsp "with work tree's branch");
5733 818c7501 2019-07-11 stsp goto done;
5734 818c7501 2019-07-11 stsp }
5735 818c7501 2019-07-11 stsp
5736 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5737 a51a74b3 2019-07-27 stsp if (error) {
5738 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5739 a51a74b3 2019-07-27 stsp goto done;
5740 a51a74b3 2019-07-27 stsp error = NULL;
5741 a51a74b3 2019-07-27 stsp } else {
5742 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5743 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5744 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5745 a51a74b3 2019-07-27 stsp goto done;
5746 a51a74b3 2019-07-27 stsp }
5747 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5748 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5749 818c7501 2019-07-11 stsp if (error)
5750 818c7501 2019-07-11 stsp goto done;
5751 818c7501 2019-07-11 stsp }
5752 818c7501 2019-07-11 stsp
5753 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5754 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5755 818c7501 2019-07-11 stsp if (error)
5756 818c7501 2019-07-11 stsp goto done;
5757 818c7501 2019-07-11 stsp
5758 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5759 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5760 fc66b545 2019-08-12 stsp if (pid == NULL) {
5761 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5762 fc66b545 2019-08-12 stsp int did_something;
5763 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5764 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5765 fc66b545 2019-08-12 stsp &did_something);
5766 fc66b545 2019-08-12 stsp if (error)
5767 fc66b545 2019-08-12 stsp goto done;
5768 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5769 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5770 fc66b545 2019-08-12 stsp }
5771 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5772 fc66b545 2019-08-12 stsp goto done;
5773 fc66b545 2019-08-12 stsp }
5774 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5775 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5776 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5777 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5778 818c7501 2019-07-11 stsp commit = NULL;
5779 818c7501 2019-07-11 stsp if (error)
5780 818c7501 2019-07-11 stsp goto done;
5781 64c6d990 2019-07-11 stsp
5782 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5783 38b0338b 2019-11-29 stsp if (continue_rebase) {
5784 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5785 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5786 38b0338b 2019-11-29 stsp goto done;
5787 38b0338b 2019-11-29 stsp } else {
5788 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5789 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5790 38b0338b 2019-11-29 stsp char *id_str;
5791 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5792 38b0338b 2019-11-29 stsp new_base_branch);
5793 38b0338b 2019-11-29 stsp if (error)
5794 38b0338b 2019-11-29 stsp goto done;
5795 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5796 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5797 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5798 38b0338b 2019-11-29 stsp free(id_str);
5799 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5800 38b0338b 2019-11-29 stsp new_head_commit_id);
5801 38b0338b 2019-11-29 stsp if (error)
5802 38b0338b 2019-11-29 stsp goto done;
5803 38b0338b 2019-11-29 stsp }
5804 818c7501 2019-07-11 stsp }
5805 818c7501 2019-07-11 stsp
5806 818c7501 2019-07-11 stsp pid = NULL;
5807 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5808 818c7501 2019-07-11 stsp commit_id = qid->id;
5809 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5810 818c7501 2019-07-11 stsp pid = qid;
5811 818c7501 2019-07-11 stsp
5812 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5813 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5814 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5815 818c7501 2019-07-11 stsp if (error)
5816 818c7501 2019-07-11 stsp goto done;
5817 818c7501 2019-07-11 stsp
5818 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5819 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5820 a0ea4fc0 2020-02-28 stsp if (error)
5821 a0ea4fc0 2020-02-28 stsp goto done;
5822 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5823 818c7501 2019-07-11 stsp break;
5824 01757395 2019-07-12 stsp }
5825 818c7501 2019-07-11 stsp
5826 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5827 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5828 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5829 818c7501 2019-07-11 stsp if (error)
5830 818c7501 2019-07-11 stsp goto done;
5831 818c7501 2019-07-11 stsp }
5832 818c7501 2019-07-11 stsp
5833 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5834 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5835 818c7501 2019-07-11 stsp if (error)
5836 818c7501 2019-07-11 stsp goto done;
5837 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5838 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5839 818c7501 2019-07-11 stsp } else
5840 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5841 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5842 818c7501 2019-07-11 stsp done:
5843 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5844 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5845 818c7501 2019-07-11 stsp free(resume_commit_id);
5846 818c7501 2019-07-11 stsp free(yca_id);
5847 818c7501 2019-07-11 stsp if (commit)
5848 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5849 818c7501 2019-07-11 stsp if (branch)
5850 818c7501 2019-07-11 stsp got_ref_close(branch);
5851 818c7501 2019-07-11 stsp if (new_base_branch)
5852 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5853 818c7501 2019-07-11 stsp if (tmp_branch)
5854 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5855 5ef14e63 2019-06-02 stsp if (worktree)
5856 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5857 5ef14e63 2019-06-02 stsp if (repo)
5858 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5859 5ef14e63 2019-06-02 stsp return error;
5860 0ebf8283 2019-07-24 stsp }
5861 0ebf8283 2019-07-24 stsp
5862 0ebf8283 2019-07-24 stsp __dead static void
5863 0ebf8283 2019-07-24 stsp usage_histedit(void)
5864 0ebf8283 2019-07-24 stsp {
5865 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5866 0ebf8283 2019-07-24 stsp getprogname());
5867 0ebf8283 2019-07-24 stsp exit(1);
5868 0ebf8283 2019-07-24 stsp }
5869 0ebf8283 2019-07-24 stsp
5870 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5871 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5872 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5873 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5874 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5875 0ebf8283 2019-07-24 stsp
5876 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5877 0ebf8283 2019-07-24 stsp unsigned char code;
5878 0ebf8283 2019-07-24 stsp const char *name;
5879 0ebf8283 2019-07-24 stsp const char *desc;
5880 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5881 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5882 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5883 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5884 82997472 2020-01-29 stsp "be used" },
5885 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5886 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5887 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5888 0ebf8283 2019-07-24 stsp };
5889 0ebf8283 2019-07-24 stsp
5890 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5891 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5892 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5893 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5894 0ebf8283 2019-07-24 stsp char *logmsg;
5895 0ebf8283 2019-07-24 stsp };
5896 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5897 0ebf8283 2019-07-24 stsp
5898 0ebf8283 2019-07-24 stsp static const struct got_error *
5899 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5900 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5901 0ebf8283 2019-07-24 stsp {
5902 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5903 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5904 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5905 8138f3e1 2019-08-11 stsp int n;
5906 0ebf8283 2019-07-24 stsp
5907 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5908 0ebf8283 2019-07-24 stsp if (err)
5909 0ebf8283 2019-07-24 stsp goto done;
5910 0ebf8283 2019-07-24 stsp
5911 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5912 0ebf8283 2019-07-24 stsp if (err)
5913 0ebf8283 2019-07-24 stsp goto done;
5914 0ebf8283 2019-07-24 stsp
5915 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5916 0ebf8283 2019-07-24 stsp if (err)
5917 0ebf8283 2019-07-24 stsp goto done;
5918 0ebf8283 2019-07-24 stsp
5919 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5920 0ebf8283 2019-07-24 stsp if (n < 0)
5921 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5922 0ebf8283 2019-07-24 stsp done:
5923 0ebf8283 2019-07-24 stsp if (commit)
5924 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5925 0ebf8283 2019-07-24 stsp free(id_str);
5926 0ebf8283 2019-07-24 stsp free(logmsg);
5927 0ebf8283 2019-07-24 stsp return err;
5928 0ebf8283 2019-07-24 stsp }
5929 0ebf8283 2019-07-24 stsp
5930 0ebf8283 2019-07-24 stsp static const struct got_error *
5931 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5932 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5933 0ebf8283 2019-07-24 stsp {
5934 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5935 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5936 0ebf8283 2019-07-24 stsp
5937 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5938 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5939 0ebf8283 2019-07-24 stsp
5940 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5941 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5942 0ebf8283 2019-07-24 stsp f, repo);
5943 0ebf8283 2019-07-24 stsp if (err)
5944 0ebf8283 2019-07-24 stsp break;
5945 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5946 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5947 083957f4 2020-02-24 stsp if (n < 0) {
5948 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5949 083957f4 2020-02-24 stsp break;
5950 083957f4 2020-02-24 stsp }
5951 083957f4 2020-02-24 stsp }
5952 0ebf8283 2019-07-24 stsp }
5953 0ebf8283 2019-07-24 stsp
5954 0ebf8283 2019-07-24 stsp return err;
5955 0ebf8283 2019-07-24 stsp }
5956 0ebf8283 2019-07-24 stsp
5957 0ebf8283 2019-07-24 stsp static const struct got_error *
5958 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5959 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5960 0ebf8283 2019-07-24 stsp {
5961 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5962 0ebf8283 2019-07-24 stsp int n, i;
5963 514f2ffe 2020-01-29 stsp char *id_str;
5964 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5965 514f2ffe 2020-01-29 stsp
5966 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5967 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5968 514f2ffe 2020-01-29 stsp if (err)
5969 514f2ffe 2020-01-29 stsp return err;
5970 514f2ffe 2020-01-29 stsp
5971 514f2ffe 2020-01-29 stsp n = fprintf(f,
5972 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5973 514f2ffe 2020-01-29 stsp "# commit %s\n"
5974 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5975 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5976 514f2ffe 2020-01-29 stsp if (n < 0) {
5977 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5978 514f2ffe 2020-01-29 stsp goto done;
5979 514f2ffe 2020-01-29 stsp }
5980 0ebf8283 2019-07-24 stsp
5981 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5982 514f2ffe 2020-01-29 stsp if (n < 0) {
5983 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5984 514f2ffe 2020-01-29 stsp goto done;
5985 514f2ffe 2020-01-29 stsp }
5986 0ebf8283 2019-07-24 stsp
5987 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5988 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5989 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5990 0ebf8283 2019-07-24 stsp cmd->desc);
5991 0ebf8283 2019-07-24 stsp if (n < 0) {
5992 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5993 0ebf8283 2019-07-24 stsp break;
5994 0ebf8283 2019-07-24 stsp }
5995 0ebf8283 2019-07-24 stsp }
5996 514f2ffe 2020-01-29 stsp done:
5997 514f2ffe 2020-01-29 stsp free(id_str);
5998 0ebf8283 2019-07-24 stsp return err;
5999 0ebf8283 2019-07-24 stsp }
6000 0ebf8283 2019-07-24 stsp
6001 0ebf8283 2019-07-24 stsp static const struct got_error *
6002 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6003 0ebf8283 2019-07-24 stsp {
6004 0ebf8283 2019-07-24 stsp static char msg[42];
6005 0ebf8283 2019-07-24 stsp int ret;
6006 0ebf8283 2019-07-24 stsp
6007 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6008 0ebf8283 2019-07-24 stsp lineno);
6009 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6010 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6011 0ebf8283 2019-07-24 stsp
6012 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6013 0ebf8283 2019-07-24 stsp }
6014 0ebf8283 2019-07-24 stsp
6015 0ebf8283 2019-07-24 stsp static const struct got_error *
6016 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6017 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6018 0ebf8283 2019-07-24 stsp {
6019 0ebf8283 2019-07-24 stsp const struct got_error *err;
6020 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6021 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6022 0ebf8283 2019-07-24 stsp
6023 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6024 0ebf8283 2019-07-24 stsp if (err)
6025 0ebf8283 2019-07-24 stsp return err;
6026 0ebf8283 2019-07-24 stsp
6027 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6028 0ebf8283 2019-07-24 stsp if (err)
6029 0ebf8283 2019-07-24 stsp goto done;
6030 0ebf8283 2019-07-24 stsp
6031 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6032 5943eee2 2019-08-13 stsp if (err)
6033 5943eee2 2019-08-13 stsp goto done;
6034 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6035 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6036 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6037 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6038 0ebf8283 2019-07-24 stsp }
6039 0ebf8283 2019-07-24 stsp done:
6040 0ebf8283 2019-07-24 stsp if (folded_commit)
6041 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6042 0ebf8283 2019-07-24 stsp free(id_str);
6043 5943eee2 2019-08-13 stsp free(folded_logmsg);
6044 0ebf8283 2019-07-24 stsp return err;
6045 0ebf8283 2019-07-24 stsp }
6046 0ebf8283 2019-07-24 stsp
6047 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6048 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6049 0ebf8283 2019-07-24 stsp {
6050 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6051 0ebf8283 2019-07-24 stsp
6052 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6053 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6054 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6055 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6056 3f9de99f 2019-07-24 stsp folded = prev;
6057 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6058 0ebf8283 2019-07-24 stsp }
6059 0ebf8283 2019-07-24 stsp
6060 0ebf8283 2019-07-24 stsp return folded;
6061 0ebf8283 2019-07-24 stsp }
6062 0ebf8283 2019-07-24 stsp
6063 0ebf8283 2019-07-24 stsp static const struct got_error *
6064 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6065 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6066 0ebf8283 2019-07-24 stsp {
6067 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6068 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6069 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6070 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6071 0ebf8283 2019-07-24 stsp int fd;
6072 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6073 0ebf8283 2019-07-24 stsp
6074 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6075 0ebf8283 2019-07-24 stsp if (err)
6076 0ebf8283 2019-07-24 stsp return err;
6077 0ebf8283 2019-07-24 stsp
6078 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6079 0ebf8283 2019-07-24 stsp if (folded) {
6080 0ebf8283 2019-07-24 stsp while (folded != hle) {
6081 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6082 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6083 3f9de99f 2019-07-24 stsp continue;
6084 3f9de99f 2019-07-24 stsp }
6085 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6086 0ebf8283 2019-07-24 stsp logmsg, repo);
6087 0ebf8283 2019-07-24 stsp if (err)
6088 0ebf8283 2019-07-24 stsp goto done;
6089 0ebf8283 2019-07-24 stsp free(logmsg);
6090 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6091 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6092 0ebf8283 2019-07-24 stsp }
6093 0ebf8283 2019-07-24 stsp }
6094 0ebf8283 2019-07-24 stsp
6095 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6096 0ebf8283 2019-07-24 stsp if (err)
6097 0ebf8283 2019-07-24 stsp goto done;
6098 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6099 5943eee2 2019-08-13 stsp if (err)
6100 5943eee2 2019-08-13 stsp goto done;
6101 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6102 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6103 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6104 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6105 0ebf8283 2019-07-24 stsp goto done;
6106 0ebf8283 2019-07-24 stsp }
6107 0ebf8283 2019-07-24 stsp free(logmsg);
6108 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6109 0ebf8283 2019-07-24 stsp
6110 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6111 0ebf8283 2019-07-24 stsp if (err)
6112 0ebf8283 2019-07-24 stsp goto done;
6113 0ebf8283 2019-07-24 stsp
6114 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6115 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6116 0ebf8283 2019-07-24 stsp if (err)
6117 0ebf8283 2019-07-24 stsp goto done;
6118 0ebf8283 2019-07-24 stsp
6119 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6120 0ebf8283 2019-07-24 stsp close(fd);
6121 0ebf8283 2019-07-24 stsp
6122 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6123 0ebf8283 2019-07-24 stsp if (err)
6124 0ebf8283 2019-07-24 stsp goto done;
6125 0ebf8283 2019-07-24 stsp
6126 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6127 0ebf8283 2019-07-24 stsp if (err) {
6128 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6129 0ebf8283 2019-07-24 stsp goto done;
6130 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6131 0ebf8283 2019-07-24 stsp }
6132 0ebf8283 2019-07-24 stsp done:
6133 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6134 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6135 0ebf8283 2019-07-24 stsp free(logmsg_path);
6136 0ebf8283 2019-07-24 stsp free(logmsg);
6137 5943eee2 2019-08-13 stsp free(orig_logmsg);
6138 0ebf8283 2019-07-24 stsp free(editor);
6139 0ebf8283 2019-07-24 stsp if (commit)
6140 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6141 0ebf8283 2019-07-24 stsp return err;
6142 5ef14e63 2019-06-02 stsp }
6143 0ebf8283 2019-07-24 stsp
6144 0ebf8283 2019-07-24 stsp static const struct got_error *
6145 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6146 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6147 0ebf8283 2019-07-24 stsp {
6148 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6149 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6150 0ebf8283 2019-07-24 stsp size_t size;
6151 0ebf8283 2019-07-24 stsp ssize_t len;
6152 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6153 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6154 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6155 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6156 0ebf8283 2019-07-24 stsp
6157 0ebf8283 2019-07-24 stsp for (;;) {
6158 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6159 0ebf8283 2019-07-24 stsp if (len == -1) {
6160 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6161 0ebf8283 2019-07-24 stsp if (feof(f))
6162 0ebf8283 2019-07-24 stsp break;
6163 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6164 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6165 0ebf8283 2019-07-24 stsp break;
6166 0ebf8283 2019-07-24 stsp }
6167 0ebf8283 2019-07-24 stsp lineno++;
6168 0ebf8283 2019-07-24 stsp p = line;
6169 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6170 0ebf8283 2019-07-24 stsp p++;
6171 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6172 0ebf8283 2019-07-24 stsp free(line);
6173 0ebf8283 2019-07-24 stsp line = NULL;
6174 0ebf8283 2019-07-24 stsp continue;
6175 0ebf8283 2019-07-24 stsp }
6176 0ebf8283 2019-07-24 stsp cmd = NULL;
6177 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6178 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6179 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6180 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6181 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6182 0ebf8283 2019-07-24 stsp break;
6183 0ebf8283 2019-07-24 stsp }
6184 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6185 0ebf8283 2019-07-24 stsp p++;
6186 0ebf8283 2019-07-24 stsp break;
6187 0ebf8283 2019-07-24 stsp }
6188 0ebf8283 2019-07-24 stsp }
6189 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6190 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6191 0ebf8283 2019-07-24 stsp break;
6192 0ebf8283 2019-07-24 stsp }
6193 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6194 0ebf8283 2019-07-24 stsp p++;
6195 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6196 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6197 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6198 0ebf8283 2019-07-24 stsp break;
6199 0ebf8283 2019-07-24 stsp }
6200 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6201 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6202 0ebf8283 2019-07-24 stsp if (err)
6203 0ebf8283 2019-07-24 stsp break;
6204 0ebf8283 2019-07-24 stsp } else {
6205 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6206 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6207 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6208 0ebf8283 2019-07-24 stsp break;
6209 0ebf8283 2019-07-24 stsp }
6210 0ebf8283 2019-07-24 stsp }
6211 0ebf8283 2019-07-24 stsp free(line);
6212 0ebf8283 2019-07-24 stsp line = NULL;
6213 0ebf8283 2019-07-24 stsp continue;
6214 0ebf8283 2019-07-24 stsp } else {
6215 0ebf8283 2019-07-24 stsp end = p;
6216 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6217 0ebf8283 2019-07-24 stsp end++;
6218 0ebf8283 2019-07-24 stsp *end = '\0';
6219 0ebf8283 2019-07-24 stsp
6220 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6221 0ebf8283 2019-07-24 stsp if (err) {
6222 0ebf8283 2019-07-24 stsp /* override error code */
6223 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6224 0ebf8283 2019-07-24 stsp break;
6225 0ebf8283 2019-07-24 stsp }
6226 0ebf8283 2019-07-24 stsp }
6227 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6228 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6229 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6230 0ebf8283 2019-07-24 stsp break;
6231 0ebf8283 2019-07-24 stsp }
6232 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6233 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6234 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6235 0ebf8283 2019-07-24 stsp commit_id = NULL;
6236 0ebf8283 2019-07-24 stsp free(line);
6237 0ebf8283 2019-07-24 stsp line = NULL;
6238 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6239 0ebf8283 2019-07-24 stsp }
6240 0ebf8283 2019-07-24 stsp
6241 0ebf8283 2019-07-24 stsp free(line);
6242 0ebf8283 2019-07-24 stsp free(commit_id);
6243 0ebf8283 2019-07-24 stsp return err;
6244 0ebf8283 2019-07-24 stsp }
6245 0ebf8283 2019-07-24 stsp
6246 0ebf8283 2019-07-24 stsp static const struct got_error *
6247 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6248 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6249 bfce7f83 2019-07-27 stsp {
6250 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6251 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6252 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6253 5b87815e 2020-03-05 stsp static char msg[92];
6254 bfce7f83 2019-07-27 stsp char *id_str;
6255 bfce7f83 2019-07-27 stsp
6256 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6257 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6258 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6259 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6260 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6261 5b87815e 2020-03-05 stsp
6262 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6263 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6264 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6265 5b87815e 2020-03-05 stsp if (hle == hle2)
6266 5b87815e 2020-03-05 stsp continue;
6267 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6268 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6269 5b87815e 2020-03-05 stsp continue;
6270 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6271 5b87815e 2020-03-05 stsp if (err)
6272 5b87815e 2020-03-05 stsp return err;
6273 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6274 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6275 5b87815e 2020-03-05 stsp free(id_str);
6276 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6277 5b87815e 2020-03-05 stsp }
6278 5b87815e 2020-03-05 stsp }
6279 bfce7f83 2019-07-27 stsp
6280 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6281 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6282 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6283 bfce7f83 2019-07-27 stsp break;
6284 bfce7f83 2019-07-27 stsp }
6285 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6286 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6287 bfce7f83 2019-07-27 stsp if (err)
6288 bfce7f83 2019-07-27 stsp return err;
6289 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6290 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6291 bfce7f83 2019-07-27 stsp free(id_str);
6292 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6293 bfce7f83 2019-07-27 stsp }
6294 bfce7f83 2019-07-27 stsp }
6295 bfce7f83 2019-07-27 stsp
6296 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6297 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6298 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6299 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6300 bfce7f83 2019-07-27 stsp
6301 bfce7f83 2019-07-27 stsp return NULL;
6302 bfce7f83 2019-07-27 stsp }
6303 bfce7f83 2019-07-27 stsp
6304 bfce7f83 2019-07-27 stsp static const struct got_error *
6305 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6306 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6307 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6308 0ebf8283 2019-07-24 stsp {
6309 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6310 0ebf8283 2019-07-24 stsp char *editor;
6311 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6312 0ebf8283 2019-07-24 stsp
6313 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6314 0ebf8283 2019-07-24 stsp if (err)
6315 0ebf8283 2019-07-24 stsp return err;
6316 0ebf8283 2019-07-24 stsp
6317 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6318 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6319 0ebf8283 2019-07-24 stsp goto done;
6320 0ebf8283 2019-07-24 stsp }
6321 0ebf8283 2019-07-24 stsp
6322 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6323 0ebf8283 2019-07-24 stsp if (f == NULL) {
6324 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6325 0ebf8283 2019-07-24 stsp goto done;
6326 0ebf8283 2019-07-24 stsp }
6327 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6328 bfce7f83 2019-07-27 stsp if (err)
6329 bfce7f83 2019-07-27 stsp goto done;
6330 bfce7f83 2019-07-27 stsp
6331 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6332 0ebf8283 2019-07-24 stsp done:
6333 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6334 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6335 0ebf8283 2019-07-24 stsp free(editor);
6336 0ebf8283 2019-07-24 stsp return err;
6337 0ebf8283 2019-07-24 stsp }
6338 0ebf8283 2019-07-24 stsp
6339 0ebf8283 2019-07-24 stsp static const struct got_error *
6340 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6341 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6342 514f2ffe 2020-01-29 stsp struct got_repository *);
6343 0ebf8283 2019-07-24 stsp
6344 0ebf8283 2019-07-24 stsp static const struct got_error *
6345 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6346 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6347 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6348 0ebf8283 2019-07-24 stsp {
6349 0ebf8283 2019-07-24 stsp const struct got_error *err;
6350 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6351 0ebf8283 2019-07-24 stsp char *path = NULL;
6352 0ebf8283 2019-07-24 stsp
6353 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6354 0ebf8283 2019-07-24 stsp if (err)
6355 0ebf8283 2019-07-24 stsp return err;
6356 0ebf8283 2019-07-24 stsp
6357 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6358 0ebf8283 2019-07-24 stsp if (err)
6359 0ebf8283 2019-07-24 stsp goto done;
6360 0ebf8283 2019-07-24 stsp
6361 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6362 0ebf8283 2019-07-24 stsp if (err)
6363 0ebf8283 2019-07-24 stsp goto done;
6364 0ebf8283 2019-07-24 stsp
6365 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6366 083957f4 2020-02-24 stsp rewind(f);
6367 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6368 083957f4 2020-02-24 stsp } else {
6369 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6370 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6371 0ebf8283 2019-07-24 stsp goto done;
6372 083957f4 2020-02-24 stsp }
6373 083957f4 2020-02-24 stsp f = NULL;
6374 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6375 083957f4 2020-02-24 stsp if (err) {
6376 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6377 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6378 083957f4 2020-02-24 stsp goto done;
6379 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6380 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6381 083957f4 2020-02-24 stsp }
6382 0ebf8283 2019-07-24 stsp }
6383 0ebf8283 2019-07-24 stsp done:
6384 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6385 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6386 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6387 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6388 0ebf8283 2019-07-24 stsp free(path);
6389 0ebf8283 2019-07-24 stsp return err;
6390 0ebf8283 2019-07-24 stsp }
6391 0ebf8283 2019-07-24 stsp
6392 0ebf8283 2019-07-24 stsp static const struct got_error *
6393 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6394 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6395 0ebf8283 2019-07-24 stsp {
6396 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6397 0ebf8283 2019-07-24 stsp char *path = NULL;
6398 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6399 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6400 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6401 0ebf8283 2019-07-24 stsp
6402 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6403 0ebf8283 2019-07-24 stsp if (err)
6404 0ebf8283 2019-07-24 stsp return err;
6405 0ebf8283 2019-07-24 stsp
6406 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6407 0ebf8283 2019-07-24 stsp if (f == NULL) {
6408 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6409 0ebf8283 2019-07-24 stsp goto done;
6410 0ebf8283 2019-07-24 stsp }
6411 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6412 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6413 0ebf8283 2019-07-24 stsp repo);
6414 0ebf8283 2019-07-24 stsp if (err)
6415 0ebf8283 2019-07-24 stsp break;
6416 0ebf8283 2019-07-24 stsp
6417 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6418 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6419 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6420 0ebf8283 2019-07-24 stsp if (n < 0) {
6421 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6422 0ebf8283 2019-07-24 stsp break;
6423 0ebf8283 2019-07-24 stsp }
6424 0ebf8283 2019-07-24 stsp }
6425 0ebf8283 2019-07-24 stsp }
6426 0ebf8283 2019-07-24 stsp done:
6427 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6428 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6429 0ebf8283 2019-07-24 stsp free(path);
6430 0ebf8283 2019-07-24 stsp if (commit)
6431 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6432 0ebf8283 2019-07-24 stsp return err;
6433 0ebf8283 2019-07-24 stsp }
6434 0ebf8283 2019-07-24 stsp
6435 bfce7f83 2019-07-27 stsp void
6436 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6437 bfce7f83 2019-07-27 stsp {
6438 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6439 bfce7f83 2019-07-27 stsp
6440 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6441 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6442 bfce7f83 2019-07-27 stsp free(hle);
6443 bfce7f83 2019-07-27 stsp }
6444 bfce7f83 2019-07-27 stsp }
6445 bfce7f83 2019-07-27 stsp
6446 0ebf8283 2019-07-24 stsp static const struct got_error *
6447 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6448 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6449 0ebf8283 2019-07-24 stsp {
6450 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6451 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6452 0ebf8283 2019-07-24 stsp
6453 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6454 0ebf8283 2019-07-24 stsp if (f == NULL) {
6455 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6456 0ebf8283 2019-07-24 stsp goto done;
6457 0ebf8283 2019-07-24 stsp }
6458 0ebf8283 2019-07-24 stsp
6459 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6460 0ebf8283 2019-07-24 stsp done:
6461 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6462 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6463 0ebf8283 2019-07-24 stsp return err;
6464 0ebf8283 2019-07-24 stsp }
6465 0ebf8283 2019-07-24 stsp
6466 0ebf8283 2019-07-24 stsp static const struct got_error *
6467 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6468 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6469 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6470 0ebf8283 2019-07-24 stsp {
6471 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6472 0ebf8283 2019-07-24 stsp int resp = ' ';
6473 0ebf8283 2019-07-24 stsp
6474 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6475 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6476 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6477 0ebf8283 2019-07-24 stsp resp = getchar();
6478 a61a4414 2019-08-07 stsp if (resp == '\n')
6479 a61a4414 2019-08-07 stsp resp = getchar();
6480 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6481 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6482 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6483 bfce7f83 2019-07-27 stsp repo);
6484 426ebf2e 2019-07-25 stsp if (err) {
6485 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6486 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6487 426ebf2e 2019-07-25 stsp break;
6488 bfce7f83 2019-07-27 stsp prev_err = err;
6489 426ebf2e 2019-07-25 stsp resp = ' ';
6490 426ebf2e 2019-07-25 stsp continue;
6491 426ebf2e 2019-07-25 stsp }
6492 bfce7f83 2019-07-27 stsp break;
6493 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6494 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6495 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6496 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6497 426ebf2e 2019-07-25 stsp if (err) {
6498 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6499 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6500 426ebf2e 2019-07-25 stsp break;
6501 bfce7f83 2019-07-27 stsp prev_err = err;
6502 426ebf2e 2019-07-25 stsp resp = ' ';
6503 426ebf2e 2019-07-25 stsp continue;
6504 426ebf2e 2019-07-25 stsp }
6505 bfce7f83 2019-07-27 stsp break;
6506 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6507 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6508 0ebf8283 2019-07-24 stsp break;
6509 426ebf2e 2019-07-25 stsp } else
6510 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6511 0ebf8283 2019-07-24 stsp }
6512 0ebf8283 2019-07-24 stsp
6513 0ebf8283 2019-07-24 stsp return err;
6514 0ebf8283 2019-07-24 stsp }
6515 0ebf8283 2019-07-24 stsp
6516 0ebf8283 2019-07-24 stsp static const struct got_error *
6517 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6518 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6519 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6520 0ebf8283 2019-07-24 stsp {
6521 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6522 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6523 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6524 3e3a69f1 2019-07-25 stsp branch, repo);
6525 0ebf8283 2019-07-24 stsp }
6526 0ebf8283 2019-07-24 stsp
6527 0ebf8283 2019-07-24 stsp static const struct got_error *
6528 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6529 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6530 0ebf8283 2019-07-24 stsp {
6531 0ebf8283 2019-07-24 stsp const struct got_error *err;
6532 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6533 0ebf8283 2019-07-24 stsp
6534 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6535 0ebf8283 2019-07-24 stsp if (err)
6536 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp
6538 0ebf8283 2019-07-24 stsp if (new_id) {
6539 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6540 0ebf8283 2019-07-24 stsp if (err)
6541 0ebf8283 2019-07-24 stsp goto done;
6542 0ebf8283 2019-07-24 stsp }
6543 0ebf8283 2019-07-24 stsp
6544 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6545 0ebf8283 2019-07-24 stsp if (new_id_str)
6546 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6547 0ebf8283 2019-07-24 stsp
6548 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6549 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6550 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6551 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6552 0ebf8283 2019-07-24 stsp goto done;
6553 0ebf8283 2019-07-24 stsp }
6554 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6555 0ebf8283 2019-07-24 stsp } else {
6556 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6557 0ebf8283 2019-07-24 stsp if (err)
6558 0ebf8283 2019-07-24 stsp goto done;
6559 0ebf8283 2019-07-24 stsp }
6560 0ebf8283 2019-07-24 stsp
6561 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6562 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6563 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6564 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6565 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6566 0ebf8283 2019-07-24 stsp break;
6567 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6568 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6569 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6570 0ebf8283 2019-07-24 stsp logmsg);
6571 0ebf8283 2019-07-24 stsp break;
6572 0ebf8283 2019-07-24 stsp default:
6573 0ebf8283 2019-07-24 stsp break;
6574 0ebf8283 2019-07-24 stsp }
6575 0ebf8283 2019-07-24 stsp done:
6576 0ebf8283 2019-07-24 stsp free(old_id_str);
6577 0ebf8283 2019-07-24 stsp free(new_id_str);
6578 0ebf8283 2019-07-24 stsp return err;
6579 0ebf8283 2019-07-24 stsp }
6580 0ebf8283 2019-07-24 stsp
6581 0ebf8283 2019-07-24 stsp static const struct got_error *
6582 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6583 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6584 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6585 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6586 0ebf8283 2019-07-24 stsp {
6587 0ebf8283 2019-07-24 stsp const struct got_error *err;
6588 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6589 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6590 0ebf8283 2019-07-24 stsp
6591 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6592 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6593 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6594 0ebf8283 2019-07-24 stsp if (err)
6595 0ebf8283 2019-07-24 stsp return err;
6596 0ebf8283 2019-07-24 stsp }
6597 0ebf8283 2019-07-24 stsp
6598 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6599 0ebf8283 2019-07-24 stsp if (err)
6600 0ebf8283 2019-07-24 stsp return err;
6601 0ebf8283 2019-07-24 stsp
6602 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6603 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6604 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6605 0ebf8283 2019-07-24 stsp if (err) {
6606 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6607 0ebf8283 2019-07-24 stsp goto done;
6608 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6609 0ebf8283 2019-07-24 stsp } else {
6610 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6611 0ebf8283 2019-07-24 stsp free(new_commit_id);
6612 0ebf8283 2019-07-24 stsp }
6613 0ebf8283 2019-07-24 stsp done:
6614 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6615 0ebf8283 2019-07-24 stsp return err;
6616 0ebf8283 2019-07-24 stsp }
6617 0ebf8283 2019-07-24 stsp
6618 0ebf8283 2019-07-24 stsp static const struct got_error *
6619 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6620 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6621 0ebf8283 2019-07-24 stsp {
6622 0ebf8283 2019-07-24 stsp const struct got_error *error;
6623 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6624 0ebf8283 2019-07-24 stsp
6625 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6626 0ebf8283 2019-07-24 stsp repo);
6627 0ebf8283 2019-07-24 stsp if (error)
6628 0ebf8283 2019-07-24 stsp return error;
6629 0ebf8283 2019-07-24 stsp
6630 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6631 0ebf8283 2019-07-24 stsp if (error)
6632 0ebf8283 2019-07-24 stsp return error;
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6635 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6636 0ebf8283 2019-07-24 stsp return error;
6637 ab20a43a 2020-01-29 stsp }
6638 ab20a43a 2020-01-29 stsp
6639 ab20a43a 2020-01-29 stsp static const struct got_error *
6640 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6641 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6642 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6643 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6644 ab20a43a 2020-01-29 stsp {
6645 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6646 ab20a43a 2020-01-29 stsp
6647 ab20a43a 2020-01-29 stsp switch (status) {
6648 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6649 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6650 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6651 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6652 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6653 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6654 ab20a43a 2020-01-29 stsp default:
6655 ab20a43a 2020-01-29 stsp break;
6656 ab20a43a 2020-01-29 stsp }
6657 ab20a43a 2020-01-29 stsp
6658 ab20a43a 2020-01-29 stsp switch (staged_status) {
6659 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6660 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6661 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6662 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6663 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6664 ab20a43a 2020-01-29 stsp default:
6665 ab20a43a 2020-01-29 stsp break;
6666 ab20a43a 2020-01-29 stsp }
6667 ab20a43a 2020-01-29 stsp
6668 ab20a43a 2020-01-29 stsp return NULL;
6669 0ebf8283 2019-07-24 stsp }
6670 0ebf8283 2019-07-24 stsp
6671 0ebf8283 2019-07-24 stsp static const struct got_error *
6672 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6673 0ebf8283 2019-07-24 stsp {
6674 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6675 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6676 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6677 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6678 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6679 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6680 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6681 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6682 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6683 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6684 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6685 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6686 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6687 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6688 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6689 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6691 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6692 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6693 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6694 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6695 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6696 0ebf8283 2019-07-24 stsp
6697 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6698 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6699 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6700 0ebf8283 2019-07-24 stsp
6701 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6702 0ebf8283 2019-07-24 stsp switch (ch) {
6703 0ebf8283 2019-07-24 stsp case 'a':
6704 0ebf8283 2019-07-24 stsp abort_edit = 1;
6705 0ebf8283 2019-07-24 stsp break;
6706 0ebf8283 2019-07-24 stsp case 'c':
6707 0ebf8283 2019-07-24 stsp continue_edit = 1;
6708 0ebf8283 2019-07-24 stsp break;
6709 0ebf8283 2019-07-24 stsp case 'F':
6710 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6711 0ebf8283 2019-07-24 stsp break;
6712 083957f4 2020-02-24 stsp case 'm':
6713 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6714 083957f4 2020-02-24 stsp break;
6715 0ebf8283 2019-07-24 stsp default:
6716 0ebf8283 2019-07-24 stsp usage_histedit();
6717 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6718 0ebf8283 2019-07-24 stsp }
6719 0ebf8283 2019-07-24 stsp }
6720 0ebf8283 2019-07-24 stsp
6721 0ebf8283 2019-07-24 stsp argc -= optind;
6722 0ebf8283 2019-07-24 stsp argv += optind;
6723 0ebf8283 2019-07-24 stsp
6724 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6725 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6726 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6727 0ebf8283 2019-07-24 stsp err(1, "pledge");
6728 0ebf8283 2019-07-24 stsp #endif
6729 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6730 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6731 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6732 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6733 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6734 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6735 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6736 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6737 0ebf8283 2019-07-24 stsp if (argc != 0)
6738 0ebf8283 2019-07-24 stsp usage_histedit();
6739 0ebf8283 2019-07-24 stsp
6740 0ebf8283 2019-07-24 stsp /*
6741 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6742 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6743 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6744 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6745 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6746 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6747 0ebf8283 2019-07-24 stsp */
6748 0ebf8283 2019-07-24 stsp
6749 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6750 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6751 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6752 0ebf8283 2019-07-24 stsp goto done;
6753 0ebf8283 2019-07-24 stsp }
6754 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6755 0ebf8283 2019-07-24 stsp if (error)
6756 0ebf8283 2019-07-24 stsp goto done;
6757 0ebf8283 2019-07-24 stsp
6758 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6759 c9956ddf 2019-09-08 stsp NULL);
6760 0ebf8283 2019-07-24 stsp if (error != NULL)
6761 0ebf8283 2019-07-24 stsp goto done;
6762 0ebf8283 2019-07-24 stsp
6763 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6764 0ebf8283 2019-07-24 stsp if (error)
6765 0ebf8283 2019-07-24 stsp goto done;
6766 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6767 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6768 0ebf8283 2019-07-24 stsp goto done;
6769 0ebf8283 2019-07-24 stsp }
6770 0ebf8283 2019-07-24 stsp
6771 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6772 0ebf8283 2019-07-24 stsp if (error)
6773 0ebf8283 2019-07-24 stsp goto done;
6774 0ebf8283 2019-07-24 stsp
6775 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6776 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6777 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6778 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6779 083957f4 2020-02-24 stsp "before the -m option can be used");
6780 083957f4 2020-02-24 stsp goto done;
6781 083957f4 2020-02-24 stsp }
6782 083957f4 2020-02-24 stsp
6783 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6784 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6785 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6786 3e3a69f1 2019-07-25 stsp worktree, repo);
6787 0ebf8283 2019-07-24 stsp if (error)
6788 0ebf8283 2019-07-24 stsp goto done;
6789 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6790 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6791 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6792 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6793 0ebf8283 2019-07-24 stsp if (error)
6794 0ebf8283 2019-07-24 stsp goto done;
6795 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6796 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6797 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6798 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6799 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6800 0ebf8283 2019-07-24 stsp goto done;
6801 0ebf8283 2019-07-24 stsp }
6802 0ebf8283 2019-07-24 stsp
6803 0ebf8283 2019-07-24 stsp if (continue_edit) {
6804 0ebf8283 2019-07-24 stsp char *path;
6805 0ebf8283 2019-07-24 stsp
6806 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6807 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6808 0ebf8283 2019-07-24 stsp goto done;
6809 0ebf8283 2019-07-24 stsp }
6810 0ebf8283 2019-07-24 stsp
6811 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6812 0ebf8283 2019-07-24 stsp if (error)
6813 0ebf8283 2019-07-24 stsp goto done;
6814 0ebf8283 2019-07-24 stsp
6815 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6816 0ebf8283 2019-07-24 stsp free(path);
6817 0ebf8283 2019-07-24 stsp if (error)
6818 0ebf8283 2019-07-24 stsp goto done;
6819 0ebf8283 2019-07-24 stsp
6820 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6821 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6822 3e3a69f1 2019-07-25 stsp worktree, repo);
6823 0ebf8283 2019-07-24 stsp if (error)
6824 0ebf8283 2019-07-24 stsp goto done;
6825 0ebf8283 2019-07-24 stsp
6826 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6827 0ebf8283 2019-07-24 stsp if (error)
6828 0ebf8283 2019-07-24 stsp goto done;
6829 0ebf8283 2019-07-24 stsp
6830 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6831 0ebf8283 2019-07-24 stsp head_commit_id);
6832 0ebf8283 2019-07-24 stsp if (error)
6833 0ebf8283 2019-07-24 stsp goto done;
6834 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6835 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6836 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6837 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6838 3aac7cf7 2019-07-25 stsp goto done;
6839 3aac7cf7 2019-07-25 stsp }
6840 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6841 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6842 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6843 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6844 0ebf8283 2019-07-24 stsp commit = NULL;
6845 0ebf8283 2019-07-24 stsp if (error)
6846 0ebf8283 2019-07-24 stsp goto done;
6847 0ebf8283 2019-07-24 stsp } else {
6848 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6849 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6850 0ebf8283 2019-07-24 stsp goto done;
6851 0ebf8283 2019-07-24 stsp }
6852 0ebf8283 2019-07-24 stsp
6853 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6854 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6855 0ebf8283 2019-07-24 stsp if (error != NULL)
6856 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp
6858 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6859 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6860 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6861 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6862 c7d20a3f 2019-07-30 stsp goto done;
6863 c7d20a3f 2019-07-30 stsp }
6864 c7d20a3f 2019-07-30 stsp
6865 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6866 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6867 bfce7f83 2019-07-27 stsp branch = NULL;
6868 0ebf8283 2019-07-24 stsp if (error)
6869 0ebf8283 2019-07-24 stsp goto done;
6870 0ebf8283 2019-07-24 stsp
6871 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6872 0ebf8283 2019-07-24 stsp head_commit_id);
6873 0ebf8283 2019-07-24 stsp if (error)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6876 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6877 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6878 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6879 3aac7cf7 2019-07-25 stsp goto done;
6880 3aac7cf7 2019-07-25 stsp }
6881 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6882 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6883 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6884 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6885 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6886 0ebf8283 2019-07-24 stsp commit = NULL;
6887 0ebf8283 2019-07-24 stsp if (error)
6888 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp
6890 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6891 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6892 c5996fff 2020-01-29 stsp goto done;
6893 c5996fff 2020-01-29 stsp }
6894 c5996fff 2020-01-29 stsp
6895 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6896 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6897 bfce7f83 2019-07-27 stsp if (error)
6898 bfce7f83 2019-07-27 stsp goto done;
6899 bfce7f83 2019-07-27 stsp
6900 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6901 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6902 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6903 6ba2bea2 2019-07-27 stsp if (error) {
6904 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6905 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6906 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6907 0ebf8283 2019-07-24 stsp goto done;
6908 6ba2bea2 2019-07-27 stsp }
6909 0ebf8283 2019-07-24 stsp } else {
6910 514f2ffe 2020-01-29 stsp const char *branch_name;
6911 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6912 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6913 514f2ffe 2020-01-29 stsp branch_name += 11;
6914 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6915 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6916 6ba2bea2 2019-07-27 stsp if (error) {
6917 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6918 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6919 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6920 0ebf8283 2019-07-24 stsp goto done;
6921 6ba2bea2 2019-07-27 stsp }
6922 0ebf8283 2019-07-24 stsp
6923 0ebf8283 2019-07-24 stsp }
6924 0ebf8283 2019-07-24 stsp
6925 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6926 0ebf8283 2019-07-24 stsp repo);
6927 6ba2bea2 2019-07-27 stsp if (error) {
6928 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6929 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6930 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6931 0ebf8283 2019-07-24 stsp goto done;
6932 6ba2bea2 2019-07-27 stsp }
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp }
6935 0ebf8283 2019-07-24 stsp
6936 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6937 4ec14e60 2019-08-28 hiltjo if (error)
6938 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp
6940 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6941 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6942 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6943 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6944 0ebf8283 2019-07-24 stsp continue;
6945 0ebf8283 2019-07-24 stsp
6946 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6947 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6948 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6949 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6950 0ebf8283 2019-07-24 stsp repo);
6951 ab20a43a 2020-01-29 stsp if (error)
6952 ab20a43a 2020-01-29 stsp goto done;
6953 0ebf8283 2019-07-24 stsp } else {
6954 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6955 ab20a43a 2020-01-29 stsp int have_changes = 0;
6956 ab20a43a 2020-01-29 stsp
6957 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6958 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6959 ab20a43a 2020-01-29 stsp if (error)
6960 ab20a43a 2020-01-29 stsp goto done;
6961 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6962 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6963 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6964 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6965 ab20a43a 2020-01-29 stsp if (error) {
6966 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6967 ab20a43a 2020-01-29 stsp goto done;
6968 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6969 ab20a43a 2020-01-29 stsp goto done;
6970 ab20a43a 2020-01-29 stsp }
6971 ab20a43a 2020-01-29 stsp if (have_changes) {
6972 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6973 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6974 ab20a43a 2020-01-29 stsp if (error)
6975 ab20a43a 2020-01-29 stsp goto done;
6976 ab20a43a 2020-01-29 stsp } else {
6977 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6978 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6979 ab20a43a 2020-01-29 stsp if (error)
6980 ab20a43a 2020-01-29 stsp goto done;
6981 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6982 ab20a43a 2020-01-29 stsp hle, NULL);
6983 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6984 ab20a43a 2020-01-29 stsp commit = NULL;
6985 ab20a43a 2020-01-29 stsp if (error)
6986 ab20a43a 2020-01-29 stsp goto done;
6987 ab20a43a 2020-01-29 stsp }
6988 0ebf8283 2019-07-24 stsp }
6989 0ebf8283 2019-07-24 stsp continue;
6990 0ebf8283 2019-07-24 stsp }
6991 0ebf8283 2019-07-24 stsp
6992 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6993 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6994 0ebf8283 2019-07-24 stsp if (error)
6995 0ebf8283 2019-07-24 stsp goto done;
6996 0ebf8283 2019-07-24 stsp continue;
6997 0ebf8283 2019-07-24 stsp }
6998 0ebf8283 2019-07-24 stsp
6999 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7000 0ebf8283 2019-07-24 stsp hle->commit_id);
7001 0ebf8283 2019-07-24 stsp if (error)
7002 0ebf8283 2019-07-24 stsp goto done;
7003 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7004 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7005 0ebf8283 2019-07-24 stsp
7006 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7007 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7008 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7009 0ebf8283 2019-07-24 stsp if (error)
7010 0ebf8283 2019-07-24 stsp goto done;
7011 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7012 0ebf8283 2019-07-24 stsp commit = NULL;
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7015 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7016 a0ea4fc0 2020-02-28 stsp repo);
7017 a0ea4fc0 2020-02-28 stsp if (error)
7018 a0ea4fc0 2020-02-28 stsp goto done;
7019 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7020 0ebf8283 2019-07-24 stsp break;
7021 0ebf8283 2019-07-24 stsp }
7022 0ebf8283 2019-07-24 stsp
7023 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7024 0ebf8283 2019-07-24 stsp char *id_str;
7025 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7026 0ebf8283 2019-07-24 stsp if (error)
7027 0ebf8283 2019-07-24 stsp goto done;
7028 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7029 0ebf8283 2019-07-24 stsp id_str);
7030 0ebf8283 2019-07-24 stsp free(id_str);
7031 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7032 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7033 3e3a69f1 2019-07-25 stsp fileindex);
7034 0ebf8283 2019-07-24 stsp goto done;
7035 0ebf8283 2019-07-24 stsp }
7036 0ebf8283 2019-07-24 stsp
7037 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7038 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7039 0ebf8283 2019-07-24 stsp if (error)
7040 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp continue;
7042 0ebf8283 2019-07-24 stsp }
7043 0ebf8283 2019-07-24 stsp
7044 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7045 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7046 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7047 0ebf8283 2019-07-24 stsp if (error)
7048 0ebf8283 2019-07-24 stsp goto done;
7049 0ebf8283 2019-07-24 stsp }
7050 0ebf8283 2019-07-24 stsp
7051 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7052 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7053 0ebf8283 2019-07-24 stsp if (error)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7056 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7057 0ebf8283 2019-07-24 stsp } else
7058 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7059 3e3a69f1 2019-07-25 stsp branch, repo);
7060 0ebf8283 2019-07-24 stsp done:
7061 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7062 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7063 0ebf8283 2019-07-24 stsp free(head_commit_id);
7064 0ebf8283 2019-07-24 stsp free(base_commit_id);
7065 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7066 0ebf8283 2019-07-24 stsp if (commit)
7067 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7068 0ebf8283 2019-07-24 stsp if (branch)
7069 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7070 0ebf8283 2019-07-24 stsp if (tmp_branch)
7071 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7072 0ebf8283 2019-07-24 stsp if (worktree)
7073 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7074 2822a352 2019-10-15 stsp if (repo)
7075 2822a352 2019-10-15 stsp got_repo_close(repo);
7076 2822a352 2019-10-15 stsp return error;
7077 2822a352 2019-10-15 stsp }
7078 2822a352 2019-10-15 stsp
7079 2822a352 2019-10-15 stsp __dead static void
7080 2822a352 2019-10-15 stsp usage_integrate(void)
7081 2822a352 2019-10-15 stsp {
7082 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7083 2822a352 2019-10-15 stsp exit(1);
7084 2822a352 2019-10-15 stsp }
7085 2822a352 2019-10-15 stsp
7086 2822a352 2019-10-15 stsp static const struct got_error *
7087 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7088 2822a352 2019-10-15 stsp {
7089 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7090 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7091 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7092 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7093 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7094 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7095 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7096 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7097 2822a352 2019-10-15 stsp int ch, did_something = 0;
7098 2822a352 2019-10-15 stsp
7099 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7100 2822a352 2019-10-15 stsp switch (ch) {
7101 2822a352 2019-10-15 stsp default:
7102 2822a352 2019-10-15 stsp usage_integrate();
7103 2822a352 2019-10-15 stsp /* NOTREACHED */
7104 2822a352 2019-10-15 stsp }
7105 2822a352 2019-10-15 stsp }
7106 2822a352 2019-10-15 stsp
7107 2822a352 2019-10-15 stsp argc -= optind;
7108 2822a352 2019-10-15 stsp argv += optind;
7109 2822a352 2019-10-15 stsp
7110 2822a352 2019-10-15 stsp if (argc != 1)
7111 2822a352 2019-10-15 stsp usage_integrate();
7112 2822a352 2019-10-15 stsp branch_arg = argv[0];
7113 2822a352 2019-10-15 stsp
7114 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7115 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7116 2822a352 2019-10-15 stsp err(1, "pledge");
7117 2822a352 2019-10-15 stsp
7118 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7119 2822a352 2019-10-15 stsp if (cwd == NULL) {
7120 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7121 2822a352 2019-10-15 stsp goto done;
7122 2822a352 2019-10-15 stsp }
7123 2822a352 2019-10-15 stsp
7124 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7125 2822a352 2019-10-15 stsp if (error)
7126 2822a352 2019-10-15 stsp goto done;
7127 2822a352 2019-10-15 stsp
7128 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7129 2822a352 2019-10-15 stsp if (error)
7130 2822a352 2019-10-15 stsp goto done;
7131 2822a352 2019-10-15 stsp
7132 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7133 2822a352 2019-10-15 stsp NULL);
7134 2822a352 2019-10-15 stsp if (error != NULL)
7135 2822a352 2019-10-15 stsp goto done;
7136 2822a352 2019-10-15 stsp
7137 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7138 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7139 2822a352 2019-10-15 stsp if (error)
7140 2822a352 2019-10-15 stsp goto done;
7141 2822a352 2019-10-15 stsp
7142 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7143 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7144 2822a352 2019-10-15 stsp goto done;
7145 2822a352 2019-10-15 stsp }
7146 2822a352 2019-10-15 stsp
7147 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7148 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7149 2822a352 2019-10-15 stsp if (error)
7150 2822a352 2019-10-15 stsp goto done;
7151 2822a352 2019-10-15 stsp
7152 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7153 2822a352 2019-10-15 stsp if (refname == NULL) {
7154 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7155 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7156 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7157 2822a352 2019-10-15 stsp goto done;
7158 2822a352 2019-10-15 stsp }
7159 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7160 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7161 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7162 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7163 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7164 2822a352 2019-10-15 stsp goto done;
7165 2822a352 2019-10-15 stsp }
7166 2822a352 2019-10-15 stsp
7167 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7168 2822a352 2019-10-15 stsp if (error)
7169 2822a352 2019-10-15 stsp goto done;
7170 2822a352 2019-10-15 stsp
7171 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7172 2822a352 2019-10-15 stsp if (error)
7173 2822a352 2019-10-15 stsp goto done;
7174 2822a352 2019-10-15 stsp
7175 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7176 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7177 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7178 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7179 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7180 2822a352 2019-10-15 stsp goto done;
7181 2822a352 2019-10-15 stsp }
7182 2822a352 2019-10-15 stsp
7183 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7184 2822a352 2019-10-15 stsp if (error) {
7185 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7186 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7187 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7188 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7189 2822a352 2019-10-15 stsp goto done;
7190 2822a352 2019-10-15 stsp }
7191 2822a352 2019-10-15 stsp
7192 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7193 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7194 2822a352 2019-10-15 stsp check_cancelled, NULL);
7195 2822a352 2019-10-15 stsp if (error)
7196 2822a352 2019-10-15 stsp goto done;
7197 2822a352 2019-10-15 stsp
7198 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7199 2822a352 2019-10-15 stsp done:
7200 715dc77e 2019-08-03 stsp if (repo)
7201 715dc77e 2019-08-03 stsp got_repo_close(repo);
7202 2822a352 2019-10-15 stsp if (worktree)
7203 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7204 2822a352 2019-10-15 stsp free(cwd);
7205 2822a352 2019-10-15 stsp free(base_commit_id);
7206 2822a352 2019-10-15 stsp free(commit_id);
7207 2822a352 2019-10-15 stsp free(refname);
7208 2822a352 2019-10-15 stsp free(base_refname);
7209 715dc77e 2019-08-03 stsp return error;
7210 715dc77e 2019-08-03 stsp }
7211 715dc77e 2019-08-03 stsp
7212 715dc77e 2019-08-03 stsp __dead static void
7213 715dc77e 2019-08-03 stsp usage_stage(void)
7214 715dc77e 2019-08-03 stsp {
7215 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7216 f3055044 2019-08-07 stsp "[file-path ...]\n",
7217 715dc77e 2019-08-03 stsp getprogname());
7218 715dc77e 2019-08-03 stsp exit(1);
7219 715dc77e 2019-08-03 stsp }
7220 715dc77e 2019-08-03 stsp
7221 715dc77e 2019-08-03 stsp static const struct got_error *
7222 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7223 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7224 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7225 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7226 408b4ebc 2019-08-03 stsp {
7227 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7228 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7229 408b4ebc 2019-08-03 stsp
7230 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7231 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7232 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7233 408b4ebc 2019-08-03 stsp return NULL;
7234 408b4ebc 2019-08-03 stsp
7235 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7236 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7237 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7238 408b4ebc 2019-08-03 stsp else
7239 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7240 408b4ebc 2019-08-03 stsp if (err)
7241 408b4ebc 2019-08-03 stsp return err;
7242 408b4ebc 2019-08-03 stsp
7243 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7244 408b4ebc 2019-08-03 stsp free(id_str);
7245 dc424a06 2019-08-07 stsp return NULL;
7246 dc424a06 2019-08-07 stsp }
7247 2e1f37b0 2019-08-08 stsp
7248 dc424a06 2019-08-07 stsp static const struct got_error *
7249 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7250 715dc77e 2019-08-03 stsp {
7251 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7252 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7253 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7254 715dc77e 2019-08-03 stsp char *cwd = NULL;
7255 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7256 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7257 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7258 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7259 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7260 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7261 715dc77e 2019-08-03 stsp
7262 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7263 715dc77e 2019-08-03 stsp
7264 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7265 715dc77e 2019-08-03 stsp switch (ch) {
7266 408b4ebc 2019-08-03 stsp case 'l':
7267 408b4ebc 2019-08-03 stsp list_stage = 1;
7268 408b4ebc 2019-08-03 stsp break;
7269 dc424a06 2019-08-07 stsp case 'p':
7270 dc424a06 2019-08-07 stsp pflag = 1;
7271 dc424a06 2019-08-07 stsp break;
7272 dc424a06 2019-08-07 stsp case 'F':
7273 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7274 dc424a06 2019-08-07 stsp break;
7275 715dc77e 2019-08-03 stsp default:
7276 715dc77e 2019-08-03 stsp usage_stage();
7277 715dc77e 2019-08-03 stsp /* NOTREACHED */
7278 715dc77e 2019-08-03 stsp }
7279 715dc77e 2019-08-03 stsp }
7280 715dc77e 2019-08-03 stsp
7281 715dc77e 2019-08-03 stsp argc -= optind;
7282 715dc77e 2019-08-03 stsp argv += optind;
7283 715dc77e 2019-08-03 stsp
7284 715dc77e 2019-08-03 stsp #ifndef PROFILE
7285 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7286 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7287 715dc77e 2019-08-03 stsp err(1, "pledge");
7288 715dc77e 2019-08-03 stsp #endif
7289 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7290 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7291 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7292 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7293 715dc77e 2019-08-03 stsp
7294 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7295 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7296 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7297 715dc77e 2019-08-03 stsp goto done;
7298 715dc77e 2019-08-03 stsp }
7299 715dc77e 2019-08-03 stsp
7300 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7301 715dc77e 2019-08-03 stsp if (error)
7302 715dc77e 2019-08-03 stsp goto done;
7303 715dc77e 2019-08-03 stsp
7304 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7305 c9956ddf 2019-09-08 stsp NULL);
7306 715dc77e 2019-08-03 stsp if (error != NULL)
7307 715dc77e 2019-08-03 stsp goto done;
7308 715dc77e 2019-08-03 stsp
7309 dc424a06 2019-08-07 stsp if (patch_script_path) {
7310 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7311 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7312 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7313 dc424a06 2019-08-07 stsp patch_script_path);
7314 dc424a06 2019-08-07 stsp goto done;
7315 dc424a06 2019-08-07 stsp }
7316 dc424a06 2019-08-07 stsp }
7317 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7318 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7319 715dc77e 2019-08-03 stsp if (error)
7320 715dc77e 2019-08-03 stsp goto done;
7321 715dc77e 2019-08-03 stsp
7322 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7323 6d022e97 2019-08-04 stsp if (error)
7324 6d022e97 2019-08-04 stsp goto done;
7325 715dc77e 2019-08-03 stsp
7326 6d022e97 2019-08-04 stsp if (list_stage)
7327 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7328 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7329 2e1f37b0 2019-08-08 stsp else {
7330 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7331 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7332 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7333 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7334 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7335 2e1f37b0 2019-08-08 stsp }
7336 ad493afc 2019-08-03 stsp done:
7337 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7338 2e1f37b0 2019-08-08 stsp error == NULL)
7339 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7340 ad493afc 2019-08-03 stsp if (repo)
7341 ad493afc 2019-08-03 stsp got_repo_close(repo);
7342 ad493afc 2019-08-03 stsp if (worktree)
7343 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7344 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7345 ad493afc 2019-08-03 stsp free((char *)pe->path);
7346 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7347 ad493afc 2019-08-03 stsp free(cwd);
7348 ad493afc 2019-08-03 stsp return error;
7349 ad493afc 2019-08-03 stsp }
7350 ad493afc 2019-08-03 stsp
7351 ad493afc 2019-08-03 stsp __dead static void
7352 ad493afc 2019-08-03 stsp usage_unstage(void)
7353 ad493afc 2019-08-03 stsp {
7354 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7355 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7356 ad493afc 2019-08-03 stsp getprogname());
7357 ad493afc 2019-08-03 stsp exit(1);
7358 ad493afc 2019-08-03 stsp }
7359 ad493afc 2019-08-03 stsp
7360 ad493afc 2019-08-03 stsp
7361 ad493afc 2019-08-03 stsp static const struct got_error *
7362 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7363 ad493afc 2019-08-03 stsp {
7364 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7365 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7366 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7367 ad493afc 2019-08-03 stsp char *cwd = NULL;
7368 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7369 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7370 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7371 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7372 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7373 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7374 ad493afc 2019-08-03 stsp
7375 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7376 ad493afc 2019-08-03 stsp
7377 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7378 ad493afc 2019-08-03 stsp switch (ch) {
7379 2e1f37b0 2019-08-08 stsp case 'p':
7380 2e1f37b0 2019-08-08 stsp pflag = 1;
7381 2e1f37b0 2019-08-08 stsp break;
7382 2e1f37b0 2019-08-08 stsp case 'F':
7383 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7384 2e1f37b0 2019-08-08 stsp break;
7385 ad493afc 2019-08-03 stsp default:
7386 ad493afc 2019-08-03 stsp usage_unstage();
7387 ad493afc 2019-08-03 stsp /* NOTREACHED */
7388 ad493afc 2019-08-03 stsp }
7389 ad493afc 2019-08-03 stsp }
7390 ad493afc 2019-08-03 stsp
7391 ad493afc 2019-08-03 stsp argc -= optind;
7392 ad493afc 2019-08-03 stsp argv += optind;
7393 ad493afc 2019-08-03 stsp
7394 ad493afc 2019-08-03 stsp #ifndef PROFILE
7395 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7396 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7397 ad493afc 2019-08-03 stsp err(1, "pledge");
7398 ad493afc 2019-08-03 stsp #endif
7399 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7400 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7401 2e1f37b0 2019-08-08 stsp
7402 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7403 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7404 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7405 ad493afc 2019-08-03 stsp goto done;
7406 ad493afc 2019-08-03 stsp }
7407 ad493afc 2019-08-03 stsp
7408 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7409 ad493afc 2019-08-03 stsp if (error)
7410 ad493afc 2019-08-03 stsp goto done;
7411 ad493afc 2019-08-03 stsp
7412 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7413 c9956ddf 2019-09-08 stsp NULL);
7414 ad493afc 2019-08-03 stsp if (error != NULL)
7415 ad493afc 2019-08-03 stsp goto done;
7416 ad493afc 2019-08-03 stsp
7417 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7418 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7419 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7420 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7421 2e1f37b0 2019-08-08 stsp patch_script_path);
7422 2e1f37b0 2019-08-08 stsp goto done;
7423 2e1f37b0 2019-08-08 stsp }
7424 2e1f37b0 2019-08-08 stsp }
7425 2e1f37b0 2019-08-08 stsp
7426 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7427 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7428 ad493afc 2019-08-03 stsp if (error)
7429 ad493afc 2019-08-03 stsp goto done;
7430 ad493afc 2019-08-03 stsp
7431 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7432 ad493afc 2019-08-03 stsp if (error)
7433 ad493afc 2019-08-03 stsp goto done;
7434 ad493afc 2019-08-03 stsp
7435 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7436 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7437 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7438 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7439 715dc77e 2019-08-03 stsp done:
7440 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7441 2e1f37b0 2019-08-08 stsp error == NULL)
7442 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7443 0ebf8283 2019-07-24 stsp if (repo)
7444 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7445 715dc77e 2019-08-03 stsp if (worktree)
7446 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7447 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7448 715dc77e 2019-08-03 stsp free((char *)pe->path);
7449 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7450 715dc77e 2019-08-03 stsp free(cwd);
7451 01073a5d 2019-08-22 stsp return error;
7452 01073a5d 2019-08-22 stsp }
7453 01073a5d 2019-08-22 stsp
7454 01073a5d 2019-08-22 stsp __dead static void
7455 01073a5d 2019-08-22 stsp usage_cat(void)
7456 01073a5d 2019-08-22 stsp {
7457 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7458 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7459 01073a5d 2019-08-22 stsp exit(1);
7460 01073a5d 2019-08-22 stsp }
7461 01073a5d 2019-08-22 stsp
7462 01073a5d 2019-08-22 stsp static const struct got_error *
7463 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7464 01073a5d 2019-08-22 stsp {
7465 01073a5d 2019-08-22 stsp const struct got_error *err;
7466 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7467 01073a5d 2019-08-22 stsp
7468 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7469 01073a5d 2019-08-22 stsp if (err)
7470 01073a5d 2019-08-22 stsp return err;
7471 01073a5d 2019-08-22 stsp
7472 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7473 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7474 01073a5d 2019-08-22 stsp return err;
7475 01073a5d 2019-08-22 stsp }
7476 01073a5d 2019-08-22 stsp
7477 01073a5d 2019-08-22 stsp static const struct got_error *
7478 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7479 01073a5d 2019-08-22 stsp {
7480 01073a5d 2019-08-22 stsp const struct got_error *err;
7481 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7482 56e0773d 2019-11-28 stsp int nentries, i;
7483 01073a5d 2019-08-22 stsp
7484 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7485 01073a5d 2019-08-22 stsp if (err)
7486 01073a5d 2019-08-22 stsp return err;
7487 01073a5d 2019-08-22 stsp
7488 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7489 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7490 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7491 01073a5d 2019-08-22 stsp char *id_str;
7492 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7493 01073a5d 2019-08-22 stsp break;
7494 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7495 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7496 01073a5d 2019-08-22 stsp if (err)
7497 01073a5d 2019-08-22 stsp break;
7498 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7499 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7500 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7501 01073a5d 2019-08-22 stsp free(id_str);
7502 01073a5d 2019-08-22 stsp }
7503 01073a5d 2019-08-22 stsp
7504 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7505 01073a5d 2019-08-22 stsp return err;
7506 01073a5d 2019-08-22 stsp }
7507 01073a5d 2019-08-22 stsp
7508 01073a5d 2019-08-22 stsp static const struct got_error *
7509 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7510 01073a5d 2019-08-22 stsp {
7511 01073a5d 2019-08-22 stsp const struct got_error *err;
7512 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7513 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7514 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7515 01073a5d 2019-08-22 stsp char *id_str = NULL;
7516 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7517 01073a5d 2019-08-22 stsp
7518 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7519 01073a5d 2019-08-22 stsp if (err)
7520 01073a5d 2019-08-22 stsp return err;
7521 01073a5d 2019-08-22 stsp
7522 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7523 01073a5d 2019-08-22 stsp if (err)
7524 01073a5d 2019-08-22 stsp goto done;
7525 01073a5d 2019-08-22 stsp
7526 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7527 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7528 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7529 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7530 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7531 01073a5d 2019-08-22 stsp char *pid_str;
7532 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7533 01073a5d 2019-08-22 stsp if (err)
7534 01073a5d 2019-08-22 stsp goto done;
7535 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7536 01073a5d 2019-08-22 stsp free(pid_str);
7537 01073a5d 2019-08-22 stsp }
7538 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7539 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7540 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7541 01073a5d 2019-08-22 stsp
7542 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7543 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7544 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7545 01073a5d 2019-08-22 stsp
7546 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7547 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7548 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7549 01073a5d 2019-08-22 stsp done:
7550 01073a5d 2019-08-22 stsp free(id_str);
7551 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7552 01073a5d 2019-08-22 stsp return err;
7553 01073a5d 2019-08-22 stsp }
7554 01073a5d 2019-08-22 stsp
7555 01073a5d 2019-08-22 stsp static const struct got_error *
7556 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7557 01073a5d 2019-08-22 stsp {
7558 01073a5d 2019-08-22 stsp const struct got_error *err;
7559 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7560 01073a5d 2019-08-22 stsp char *id_str = NULL;
7561 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7562 01073a5d 2019-08-22 stsp
7563 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7564 01073a5d 2019-08-22 stsp if (err)
7565 01073a5d 2019-08-22 stsp return err;
7566 01073a5d 2019-08-22 stsp
7567 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7568 01073a5d 2019-08-22 stsp if (err)
7569 01073a5d 2019-08-22 stsp goto done;
7570 01073a5d 2019-08-22 stsp
7571 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7572 e15d5241 2019-08-22 stsp
7573 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7574 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7575 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7576 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7577 01073a5d 2019-08-22 stsp break;
7578 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7579 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7580 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7581 01073a5d 2019-08-22 stsp break;
7582 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7583 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7584 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7585 01073a5d 2019-08-22 stsp break;
7586 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7587 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7588 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7589 01073a5d 2019-08-22 stsp break;
7590 01073a5d 2019-08-22 stsp default:
7591 01073a5d 2019-08-22 stsp break;
7592 01073a5d 2019-08-22 stsp }
7593 01073a5d 2019-08-22 stsp
7594 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7595 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7596 e15d5241 2019-08-22 stsp
7597 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7598 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7599 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7600 01073a5d 2019-08-22 stsp
7601 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7602 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7603 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7604 01073a5d 2019-08-22 stsp done:
7605 01073a5d 2019-08-22 stsp free(id_str);
7606 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7607 01073a5d 2019-08-22 stsp return err;
7608 01073a5d 2019-08-22 stsp }
7609 01073a5d 2019-08-22 stsp
7610 01073a5d 2019-08-22 stsp static const struct got_error *
7611 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7612 01073a5d 2019-08-22 stsp {
7613 01073a5d 2019-08-22 stsp const struct got_error *error;
7614 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7615 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7616 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7617 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7618 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7619 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7620 01073a5d 2019-08-22 stsp
7621 01073a5d 2019-08-22 stsp #ifndef PROFILE
7622 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7623 01073a5d 2019-08-22 stsp NULL) == -1)
7624 01073a5d 2019-08-22 stsp err(1, "pledge");
7625 01073a5d 2019-08-22 stsp #endif
7626 01073a5d 2019-08-22 stsp
7627 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7628 01073a5d 2019-08-22 stsp switch (ch) {
7629 896e9b6f 2019-08-26 stsp case 'c':
7630 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7631 896e9b6f 2019-08-26 stsp break;
7632 01073a5d 2019-08-22 stsp case 'r':
7633 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7634 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7635 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7636 9ba1d308 2019-10-21 stsp optarg);
7637 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7638 01073a5d 2019-08-22 stsp break;
7639 896e9b6f 2019-08-26 stsp case 'P':
7640 896e9b6f 2019-08-26 stsp force_path = 1;
7641 896e9b6f 2019-08-26 stsp break;
7642 01073a5d 2019-08-22 stsp default:
7643 01073a5d 2019-08-22 stsp usage_cat();
7644 01073a5d 2019-08-22 stsp /* NOTREACHED */
7645 01073a5d 2019-08-22 stsp }
7646 01073a5d 2019-08-22 stsp }
7647 01073a5d 2019-08-22 stsp
7648 01073a5d 2019-08-22 stsp argc -= optind;
7649 01073a5d 2019-08-22 stsp argv += optind;
7650 01073a5d 2019-08-22 stsp
7651 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7652 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7653 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7654 01073a5d 2019-08-22 stsp goto done;
7655 01073a5d 2019-08-22 stsp }
7656 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7657 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7658 01073a5d 2019-08-22 stsp goto done;
7659 01073a5d 2019-08-22 stsp if (worktree) {
7660 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7661 01073a5d 2019-08-22 stsp repo_path = strdup(
7662 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7663 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7664 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7665 01073a5d 2019-08-22 stsp goto done;
7666 01073a5d 2019-08-22 stsp }
7667 01073a5d 2019-08-22 stsp }
7668 01073a5d 2019-08-22 stsp }
7669 01073a5d 2019-08-22 stsp
7670 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7671 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7672 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7673 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7674 01073a5d 2019-08-22 stsp }
7675 01073a5d 2019-08-22 stsp
7676 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7677 01073a5d 2019-08-22 stsp free(repo_path);
7678 01073a5d 2019-08-22 stsp if (error != NULL)
7679 01073a5d 2019-08-22 stsp goto done;
7680 01073a5d 2019-08-22 stsp
7681 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7682 896e9b6f 2019-08-26 stsp if (error)
7683 896e9b6f 2019-08-26 stsp goto done;
7684 896e9b6f 2019-08-26 stsp
7685 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7686 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7687 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7688 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7689 01073a5d 2019-08-22 stsp if (error)
7690 01073a5d 2019-08-22 stsp goto done;
7691 01073a5d 2019-08-22 stsp
7692 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7693 896e9b6f 2019-08-26 stsp if (force_path) {
7694 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7695 896e9b6f 2019-08-26 stsp argv[i]);
7696 896e9b6f 2019-08-26 stsp if (error)
7697 896e9b6f 2019-08-26 stsp break;
7698 896e9b6f 2019-08-26 stsp } else {
7699 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7700 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7701 896e9b6f 2019-08-26 stsp if (error) {
7702 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7703 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7704 896e9b6f 2019-08-26 stsp break;
7705 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7706 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7707 896e9b6f 2019-08-26 stsp if (error)
7708 896e9b6f 2019-08-26 stsp break;
7709 896e9b6f 2019-08-26 stsp }
7710 896e9b6f 2019-08-26 stsp }
7711 01073a5d 2019-08-22 stsp
7712 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7713 01073a5d 2019-08-22 stsp if (error)
7714 01073a5d 2019-08-22 stsp break;
7715 01073a5d 2019-08-22 stsp
7716 01073a5d 2019-08-22 stsp switch (obj_type) {
7717 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7718 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7719 01073a5d 2019-08-22 stsp break;
7720 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7721 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7722 01073a5d 2019-08-22 stsp break;
7723 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7724 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7725 01073a5d 2019-08-22 stsp break;
7726 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7727 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7728 01073a5d 2019-08-22 stsp break;
7729 01073a5d 2019-08-22 stsp default:
7730 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7731 01073a5d 2019-08-22 stsp break;
7732 01073a5d 2019-08-22 stsp }
7733 01073a5d 2019-08-22 stsp if (error)
7734 01073a5d 2019-08-22 stsp break;
7735 01073a5d 2019-08-22 stsp free(label);
7736 01073a5d 2019-08-22 stsp label = NULL;
7737 01073a5d 2019-08-22 stsp free(id);
7738 01073a5d 2019-08-22 stsp id = NULL;
7739 01073a5d 2019-08-22 stsp }
7740 01073a5d 2019-08-22 stsp done:
7741 01073a5d 2019-08-22 stsp free(label);
7742 01073a5d 2019-08-22 stsp free(id);
7743 896e9b6f 2019-08-26 stsp free(commit_id);
7744 01073a5d 2019-08-22 stsp if (worktree)
7745 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7746 01073a5d 2019-08-22 stsp if (repo) {
7747 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7748 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7749 01073a5d 2019-08-22 stsp if (error == NULL)
7750 01073a5d 2019-08-22 stsp error = repo_error;
7751 01073a5d 2019-08-22 stsp }
7752 0ebf8283 2019-07-24 stsp return error;
7753 0ebf8283 2019-07-24 stsp }