Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.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 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 5c860e29 2018-03-12 stsp
38 f42b1b34 2018-03-12 stsp #include "got_error.h"
39 f42b1b34 2018-03-12 stsp #include "got_object.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 f42b1b34 2018-03-12 stsp #include "got_repository.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 c09a553d 2018-03-12 stsp #include "got_worktree.h"
44 79109fed 2018-03-27 stsp #include "got_diff.h"
45 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
46 404c43c4 2018-06-21 stsp #include "got_blame.h"
47 63219cd2 2019-01-04 stsp #include "got_privsep.h"
48 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
49 5c860e29 2018-03-12 stsp
50 5c860e29 2018-03-12 stsp #ifndef nitems
51 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 5c860e29 2018-03-12 stsp #endif
53 99437157 2018-11-11 stsp
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static void
58 99437157 2018-11-11 stsp catch_sigint(int signo)
59 99437157 2018-11-11 stsp {
60 99437157 2018-11-11 stsp sigint_received = 1;
61 99437157 2018-11-11 stsp }
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static void
64 99437157 2018-11-11 stsp catch_sigpipe(int signo)
65 99437157 2018-11-11 stsp {
66 99437157 2018-11-11 stsp sigpipe_received = 1;
67 99437157 2018-11-11 stsp }
68 5c860e29 2018-03-12 stsp
69 99437157 2018-11-11 stsp
70 8cfb4057 2019-07-09 stsp struct got_cmd {
71 5c860e29 2018-03-12 stsp const char *cmd_name;
72 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
73 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
74 97b3a7be 2019-07-09 stsp const char *cmd_alias;
75 5c860e29 2018-03-12 stsp };
76 5c860e29 2018-03-12 stsp
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
79 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
81 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
84 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
85 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
86 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
87 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
88 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
89 d00136be 2019-03-26 stsp __dead static void usage_add(void);
90 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
91 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
92 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
93 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
94 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
95 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
96 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
97 5c860e29 2018-03-12 stsp
98 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
99 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
100 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
101 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
102 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
104 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
105 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
107 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
108 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
109 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
110 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
111 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
112 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
113 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
114 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
115 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
116 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
117 5c860e29 2018-03-12 stsp
118 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
119 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
120 3ce1b845 2019-07-15 stsp { "import", cmd_import, usage_import, "" },
121 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
122 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
123 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
124 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
125 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
126 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
127 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
128 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
129 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
130 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
131 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
132 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
133 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
134 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
136 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
137 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
138 5c860e29 2018-03-12 stsp };
139 ce5b7c56 2019-07-09 stsp
140 ce5b7c56 2019-07-09 stsp static void
141 ce5b7c56 2019-07-09 stsp list_commands(void)
142 ce5b7c56 2019-07-09 stsp {
143 ce5b7c56 2019-07-09 stsp int i;
144 ce5b7c56 2019-07-09 stsp
145 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
146 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
147 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
148 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
149 ce5b7c56 2019-07-09 stsp }
150 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
151 ce5b7c56 2019-07-09 stsp }
152 5c860e29 2018-03-12 stsp
153 5c860e29 2018-03-12 stsp int
154 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
155 5c860e29 2018-03-12 stsp {
156 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
157 5c860e29 2018-03-12 stsp unsigned int i;
158 5c860e29 2018-03-12 stsp int ch;
159 1b6b95a8 2018-03-12 stsp int hflag = 0;
160 5c860e29 2018-03-12 stsp
161 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
162 5c860e29 2018-03-12 stsp
163 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
164 5c860e29 2018-03-12 stsp switch (ch) {
165 1b6b95a8 2018-03-12 stsp case 'h':
166 1b6b95a8 2018-03-12 stsp hflag = 1;
167 1b6b95a8 2018-03-12 stsp break;
168 5c860e29 2018-03-12 stsp default:
169 ce5b7c56 2019-07-09 stsp usage(hflag);
170 5c860e29 2018-03-12 stsp /* NOTREACHED */
171 5c860e29 2018-03-12 stsp }
172 5c860e29 2018-03-12 stsp }
173 5c860e29 2018-03-12 stsp
174 5c860e29 2018-03-12 stsp argc -= optind;
175 5c860e29 2018-03-12 stsp argv += optind;
176 1e70621d 2018-03-27 stsp optind = 0;
177 5c860e29 2018-03-12 stsp
178 5c860e29 2018-03-12 stsp if (argc <= 0)
179 ce5b7c56 2019-07-09 stsp usage(hflag);
180 5c860e29 2018-03-12 stsp
181 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
182 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
183 99437157 2018-11-11 stsp
184 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
185 d7d4f210 2018-03-12 stsp const struct got_error *error;
186 d7d4f210 2018-03-12 stsp
187 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
188 5c860e29 2018-03-12 stsp
189 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
191 5c860e29 2018-03-12 stsp continue;
192 5c860e29 2018-03-12 stsp
193 1b6b95a8 2018-03-12 stsp if (hflag)
194 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
195 1b6b95a8 2018-03-12 stsp
196 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
197 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
198 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
199 d7d4f210 2018-03-12 stsp return 1;
200 d7d4f210 2018-03-12 stsp }
201 d7d4f210 2018-03-12 stsp
202 d7d4f210 2018-03-12 stsp return 0;
203 5c860e29 2018-03-12 stsp }
204 5c860e29 2018-03-12 stsp
205 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
206 ce5b7c56 2019-07-09 stsp list_commands();
207 5c860e29 2018-03-12 stsp return 1;
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 4ed7e80c 2018-05-20 stsp __dead static void
211 ce5b7c56 2019-07-09 stsp usage(int hflag)
212 5c860e29 2018-03-12 stsp {
213 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
214 ce5b7c56 2019-07-09 stsp if (hflag)
215 ce5b7c56 2019-07-09 stsp list_commands();
216 5c860e29 2018-03-12 stsp exit(1);
217 5c860e29 2018-03-12 stsp }
218 5c860e29 2018-03-12 stsp
219 0266afb7 2019-01-04 stsp static const struct got_error *
220 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
221 e2ba3d07 2019-05-13 stsp {
222 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
223 e2ba3d07 2019-05-13 stsp const char *editor;
224 e2ba3d07 2019-05-13 stsp
225 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
226 e2ba3d07 2019-05-13 stsp if (editor == NULL)
227 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
228 e2ba3d07 2019-05-13 stsp
229 0ee7065d 2019-05-13 stsp if (editor) {
230 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
231 0ee7065d 2019-05-13 stsp if (err)
232 0ee7065d 2019-05-13 stsp return err;
233 0ee7065d 2019-05-13 stsp }
234 e2ba3d07 2019-05-13 stsp
235 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
236 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
237 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
238 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
239 0ee7065d 2019-05-13 stsp }
240 0ee7065d 2019-05-13 stsp
241 e2ba3d07 2019-05-13 stsp return NULL;
242 e2ba3d07 2019-05-13 stsp }
243 e2ba3d07 2019-05-13 stsp
244 e2ba3d07 2019-05-13 stsp static const struct got_error *
245 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
246 c530dc23 2019-07-23 stsp const char *worktree_path)
247 0266afb7 2019-01-04 stsp {
248 163ce85a 2019-05-13 stsp const struct got_error *err;
249 0266afb7 2019-01-04 stsp
250 37c06ea4 2019-07-15 stsp #ifdef PROFILE
251 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
252 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
253 37c06ea4 2019-07-15 stsp #endif
254 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
256 0266afb7 2019-01-04 stsp
257 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
259 0266afb7 2019-01-04 stsp
260 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
261 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
262 0266afb7 2019-01-04 stsp
263 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
264 163ce85a 2019-05-13 stsp if (err != NULL)
265 163ce85a 2019-05-13 stsp return err;
266 0266afb7 2019-01-04 stsp
267 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
268 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
269 0266afb7 2019-01-04 stsp
270 0266afb7 2019-01-04 stsp return NULL;
271 2c7829a4 2019-06-17 stsp }
272 2c7829a4 2019-06-17 stsp
273 2c7829a4 2019-06-17 stsp __dead static void
274 2c7829a4 2019-06-17 stsp usage_init(void)
275 2c7829a4 2019-06-17 stsp {
276 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
277 2c7829a4 2019-06-17 stsp exit(1);
278 2c7829a4 2019-06-17 stsp }
279 2c7829a4 2019-06-17 stsp
280 2c7829a4 2019-06-17 stsp static const struct got_error *
281 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
282 2c7829a4 2019-06-17 stsp {
283 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
284 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
285 2c7829a4 2019-06-17 stsp int ch;
286 2c7829a4 2019-06-17 stsp
287 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
288 2c7829a4 2019-06-17 stsp switch (ch) {
289 2c7829a4 2019-06-17 stsp default:
290 2c7829a4 2019-06-17 stsp usage_init();
291 2c7829a4 2019-06-17 stsp /* NOTREACHED */
292 2c7829a4 2019-06-17 stsp }
293 2c7829a4 2019-06-17 stsp }
294 2c7829a4 2019-06-17 stsp
295 2c7829a4 2019-06-17 stsp argc -= optind;
296 2c7829a4 2019-06-17 stsp argv += optind;
297 2c7829a4 2019-06-17 stsp
298 2c7829a4 2019-06-17 stsp #ifndef PROFILE
299 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
300 2c7829a4 2019-06-17 stsp err(1, "pledge");
301 2c7829a4 2019-06-17 stsp #endif
302 2c7829a4 2019-06-17 stsp if (argc != 1)
303 bc20e173 2019-06-17 stsp usage_init();
304 2c7829a4 2019-06-17 stsp
305 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
306 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
307 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
308 2c7829a4 2019-06-17 stsp
309 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
310 2c7829a4 2019-06-17 stsp
311 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
312 2c7829a4 2019-06-17 stsp if (error &&
313 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
314 2c7829a4 2019-06-17 stsp goto done;
315 2c7829a4 2019-06-17 stsp
316 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
317 2c7829a4 2019-06-17 stsp if (error)
318 2c7829a4 2019-06-17 stsp goto done;
319 2c7829a4 2019-06-17 stsp
320 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
321 2c7829a4 2019-06-17 stsp if (error != NULL)
322 3ce1b845 2019-07-15 stsp goto done;
323 3ce1b845 2019-07-15 stsp
324 3ce1b845 2019-07-15 stsp done:
325 3ce1b845 2019-07-15 stsp free(repo_path);
326 3ce1b845 2019-07-15 stsp return error;
327 3ce1b845 2019-07-15 stsp }
328 3ce1b845 2019-07-15 stsp
329 3ce1b845 2019-07-15 stsp __dead static void
330 3ce1b845 2019-07-15 stsp usage_import(void)
331 3ce1b845 2019-07-15 stsp {
332 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
334 3ce1b845 2019-07-15 stsp exit(1);
335 3ce1b845 2019-07-15 stsp }
336 3ce1b845 2019-07-15 stsp
337 3ce1b845 2019-07-15 stsp int
338 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
339 3ce1b845 2019-07-15 stsp {
340 3ce1b845 2019-07-15 stsp pid_t pid;
341 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
342 3ce1b845 2019-07-15 stsp int st = -1;
343 3ce1b845 2019-07-15 stsp
344 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
345 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
346 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
347 3ce1b845 2019-07-15 stsp
348 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
349 3ce1b845 2019-07-15 stsp case -1:
350 3ce1b845 2019-07-15 stsp goto doneediting;
351 3ce1b845 2019-07-15 stsp case 0:
352 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
353 3ce1b845 2019-07-15 stsp _exit(127);
354 3ce1b845 2019-07-15 stsp }
355 3ce1b845 2019-07-15 stsp
356 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
357 3ce1b845 2019-07-15 stsp if (errno != EINTR)
358 3ce1b845 2019-07-15 stsp break;
359 3ce1b845 2019-07-15 stsp
360 3ce1b845 2019-07-15 stsp doneediting:
361 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
362 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
363 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
364 3ce1b845 2019-07-15 stsp
365 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
366 3ce1b845 2019-07-15 stsp errno = EINTR;
367 3ce1b845 2019-07-15 stsp return -1;
368 3ce1b845 2019-07-15 stsp }
369 3ce1b845 2019-07-15 stsp
370 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
371 3ce1b845 2019-07-15 stsp }
372 3ce1b845 2019-07-15 stsp
373 3ce1b845 2019-07-15 stsp static const struct got_error *
374 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 3ce1b845 2019-07-15 stsp const char *initial_content)
376 3ce1b845 2019-07-15 stsp {
377 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
378 3ce1b845 2019-07-15 stsp char buf[1024];
379 3ce1b845 2019-07-15 stsp struct stat st, st2;
380 3ce1b845 2019-07-15 stsp FILE *fp;
381 3ce1b845 2019-07-15 stsp int content_changed = 0;
382 3ce1b845 2019-07-15 stsp size_t len;
383 3ce1b845 2019-07-15 stsp
384 3ce1b845 2019-07-15 stsp *logmsg = NULL;
385 3ce1b845 2019-07-15 stsp
386 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
387 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
388 3ce1b845 2019-07-15 stsp
389 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
390 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
391 3ce1b845 2019-07-15 stsp
392 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
393 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
394 3ce1b845 2019-07-15 stsp
395 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
400 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
401 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
402 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
403 3ce1b845 2019-07-15 stsp len = 0;
404 3ce1b845 2019-07-15 stsp
405 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
406 3ce1b845 2019-07-15 stsp if (fp == NULL) {
407 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
408 3ce1b845 2019-07-15 stsp goto done;
409 3ce1b845 2019-07-15 stsp }
410 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
411 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
412 3ce1b845 2019-07-15 stsp content_changed = 1;
413 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
415 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
416 3ce1b845 2019-07-15 stsp }
417 3ce1b845 2019-07-15 stsp fclose(fp);
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
421 3ce1b845 2019-07-15 stsp len--;
422 3ce1b845 2019-07-15 stsp }
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
425 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
427 3ce1b845 2019-07-15 stsp done:
428 3ce1b845 2019-07-15 stsp if (err) {
429 3ce1b845 2019-07-15 stsp free(*logmsg);
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
431 3ce1b845 2019-07-15 stsp }
432 3ce1b845 2019-07-15 stsp return err;
433 3ce1b845 2019-07-15 stsp }
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp static const struct got_error *
436 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 3ce1b845 2019-07-15 stsp const char *branch_name)
438 3ce1b845 2019-07-15 stsp {
439 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
440 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
441 3ce1b845 2019-07-15 stsp int fd;
442 3ce1b845 2019-07-15 stsp
443 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
444 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
445 3ce1b845 2019-07-15 stsp branch_name) == -1)
446 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
447 3ce1b845 2019-07-15 stsp
448 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
449 3ce1b845 2019-07-15 stsp if (err)
450 3ce1b845 2019-07-15 stsp goto done;
451 3ce1b845 2019-07-15 stsp
452 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
453 3ce1b845 2019-07-15 stsp close(fd);
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
456 3ce1b845 2019-07-15 stsp done:
457 3ce1b845 2019-07-15 stsp free(initial_content);
458 3ce1b845 2019-07-15 stsp free(logmsg_path);
459 3ce1b845 2019-07-15 stsp return err;
460 3ce1b845 2019-07-15 stsp }
461 3ce1b845 2019-07-15 stsp
462 3ce1b845 2019-07-15 stsp static const struct got_error *
463 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
464 3ce1b845 2019-07-15 stsp {
465 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
466 3ce1b845 2019-07-15 stsp return NULL;
467 3ce1b845 2019-07-15 stsp }
468 3ce1b845 2019-07-15 stsp
469 3ce1b845 2019-07-15 stsp static const struct got_error *
470 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
471 3ce1b845 2019-07-15 stsp {
472 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
473 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
474 3ce1b845 2019-07-15 stsp char *editor = NULL;
475 3ce1b845 2019-07-15 stsp const char *got_author = getenv("GOT_AUTHOR");
476 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
477 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
478 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
479 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
481 3ce1b845 2019-07-15 stsp int ch;
482 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
483 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
486 3ce1b845 2019-07-15 stsp
487 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
488 3ce1b845 2019-07-15 stsp switch (ch) {
489 3ce1b845 2019-07-15 stsp case 'b':
490 3ce1b845 2019-07-15 stsp branch_name = optarg;
491 3ce1b845 2019-07-15 stsp break;
492 3ce1b845 2019-07-15 stsp case 'm':
493 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
494 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
495 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
496 3ce1b845 2019-07-15 stsp goto done;
497 3ce1b845 2019-07-15 stsp }
498 3ce1b845 2019-07-15 stsp break;
499 3ce1b845 2019-07-15 stsp case 'r':
500 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
501 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
502 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp }
505 3ce1b845 2019-07-15 stsp break;
506 3ce1b845 2019-07-15 stsp case 'I':
507 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
508 3ce1b845 2019-07-15 stsp break;
509 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
510 3ce1b845 2019-07-15 stsp NULL);
511 3ce1b845 2019-07-15 stsp if (error)
512 3ce1b845 2019-07-15 stsp goto done;
513 3ce1b845 2019-07-15 stsp break;
514 3ce1b845 2019-07-15 stsp default:
515 3ce1b845 2019-07-15 stsp usage_init();
516 3ce1b845 2019-07-15 stsp /* NOTREACHED */
517 3ce1b845 2019-07-15 stsp }
518 3ce1b845 2019-07-15 stsp }
519 3ce1b845 2019-07-15 stsp
520 3ce1b845 2019-07-15 stsp argc -= optind;
521 3ce1b845 2019-07-15 stsp argv += optind;
522 3ce1b845 2019-07-15 stsp
523 3ce1b845 2019-07-15 stsp #ifndef PROFILE
524 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
525 3ce1b845 2019-07-15 stsp NULL) == -1)
526 3ce1b845 2019-07-15 stsp err(1, "pledge");
527 3ce1b845 2019-07-15 stsp #endif
528 3ce1b845 2019-07-15 stsp if (argc != 1)
529 3ce1b845 2019-07-15 stsp usage_import();
530 3ce1b845 2019-07-15 stsp
531 3ce1b845 2019-07-15 stsp if (got_author == NULL) {
532 3ce1b845 2019-07-15 stsp /* TODO: Look current user up in password database */
533 3ce1b845 2019-07-15 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 2c7829a4 2019-06-17 stsp goto done;
535 3ce1b845 2019-07-15 stsp }
536 2c7829a4 2019-06-17 stsp
537 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
538 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
539 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
540 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
541 3ce1b845 2019-07-15 stsp }
542 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
543 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
544 3ce1b845 2019-07-15 stsp if (error)
545 3ce1b845 2019-07-15 stsp goto done;
546 3ce1b845 2019-07-15 stsp
547 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
549 3ce1b845 2019-07-15 stsp goto done;
550 3ce1b845 2019-07-15 stsp }
551 3ce1b845 2019-07-15 stsp
552 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
553 3ce1b845 2019-07-15 stsp if (error) {
554 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
555 3ce1b845 2019-07-15 stsp goto done;
556 3ce1b845 2019-07-15 stsp } else {
557 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 3ce1b845 2019-07-15 stsp "import target branch already exists");
559 3ce1b845 2019-07-15 stsp goto done;
560 3ce1b845 2019-07-15 stsp }
561 3ce1b845 2019-07-15 stsp
562 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
563 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
564 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
565 3ce1b845 2019-07-15 stsp goto done;
566 3ce1b845 2019-07-15 stsp }
567 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
568 3ce1b845 2019-07-15 stsp
569 3ce1b845 2019-07-15 stsp /*
570 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
571 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
572 3ce1b845 2019-07-15 stsp */
573 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
574 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
575 3ce1b845 2019-07-15 stsp if (error)
576 3ce1b845 2019-07-15 stsp goto done;
577 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
578 3ce1b845 2019-07-15 stsp if (error)
579 3ce1b845 2019-07-15 stsp goto done;
580 3ce1b845 2019-07-15 stsp }
581 3ce1b845 2019-07-15 stsp
582 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
583 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
584 3ce1b845 2019-07-15 stsp
585 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
586 3ce1b845 2019-07-15 stsp if (error)
587 3ce1b845 2019-07-15 stsp goto done;
588 3ce1b845 2019-07-15 stsp
589 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 3ce1b845 2019-07-15 stsp got_author, &ignores, repo, import_progress, NULL);
591 3ce1b845 2019-07-15 stsp if (error)
592 3ce1b845 2019-07-15 stsp goto done;
593 3ce1b845 2019-07-15 stsp
594 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
595 3ce1b845 2019-07-15 stsp if (error)
596 3ce1b845 2019-07-15 stsp goto done;
597 3ce1b845 2019-07-15 stsp
598 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
599 3ce1b845 2019-07-15 stsp if (error)
600 3ce1b845 2019-07-15 stsp goto done;
601 3ce1b845 2019-07-15 stsp
602 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
603 3ce1b845 2019-07-15 stsp if (error)
604 3ce1b845 2019-07-15 stsp goto done;
605 3ce1b845 2019-07-15 stsp
606 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
607 3ce1b845 2019-07-15 stsp if (error) {
608 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
609 3ce1b845 2019-07-15 stsp goto done;
610 3ce1b845 2019-07-15 stsp
611 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
612 3ce1b845 2019-07-15 stsp branch_ref);
613 3ce1b845 2019-07-15 stsp if (error)
614 3ce1b845 2019-07-15 stsp goto done;
615 3ce1b845 2019-07-15 stsp
616 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
617 3ce1b845 2019-07-15 stsp if (error)
618 3ce1b845 2019-07-15 stsp goto done;
619 3ce1b845 2019-07-15 stsp }
620 3ce1b845 2019-07-15 stsp
621 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
622 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
623 2c7829a4 2019-06-17 stsp done:
624 2c7829a4 2019-06-17 stsp free(repo_path);
625 3ce1b845 2019-07-15 stsp free(editor);
626 3ce1b845 2019-07-15 stsp free(refname);
627 3ce1b845 2019-07-15 stsp free(new_commit_id);
628 3ce1b845 2019-07-15 stsp free(id_str);
629 3ce1b845 2019-07-15 stsp if (branch_ref)
630 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
631 3ce1b845 2019-07-15 stsp if (head_ref)
632 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
633 2c7829a4 2019-06-17 stsp return error;
634 0266afb7 2019-01-04 stsp }
635 0266afb7 2019-01-04 stsp
636 4ed7e80c 2018-05-20 stsp __dead static void
637 c09a553d 2018-03-12 stsp usage_checkout(void)
638 c09a553d 2018-03-12 stsp {
639 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
641 c09a553d 2018-03-12 stsp exit(1);
642 92a684f4 2018-03-12 stsp }
643 92a684f4 2018-03-12 stsp
644 1ee397ad 2019-07-12 stsp static const struct got_error *
645 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
646 92a684f4 2018-03-12 stsp {
647 92a684f4 2018-03-12 stsp char *worktree_path = arg;
648 a484d721 2019-06-10 stsp
649 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
650 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
651 1ee397ad 2019-07-12 stsp return NULL;
652 92a684f4 2018-03-12 stsp
653 92a684f4 2018-03-12 stsp while (path[0] == '/')
654 92a684f4 2018-03-12 stsp path++;
655 92a684f4 2018-03-12 stsp
656 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
657 1ee397ad 2019-07-12 stsp return NULL;
658 99437157 2018-11-11 stsp }
659 99437157 2018-11-11 stsp
660 99437157 2018-11-11 stsp static const struct got_error *
661 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
662 99437157 2018-11-11 stsp {
663 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
664 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
665 99437157 2018-11-11 stsp return NULL;
666 8069f636 2019-01-12 stsp }
667 8069f636 2019-01-12 stsp
668 8069f636 2019-01-12 stsp static const struct got_error *
669 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
670 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
671 8069f636 2019-01-12 stsp {
672 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
673 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
674 8069f636 2019-01-12 stsp
675 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
677 36a38700 2019-05-10 stsp if (err)
678 36a38700 2019-05-10 stsp return err;
679 8069f636 2019-01-12 stsp
680 d5bea539 2019-05-13 stsp if (yca_id == NULL)
681 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
682 8069f636 2019-01-12 stsp
683 d5bea539 2019-05-13 stsp /*
684 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
685 d5bea539 2019-05-13 stsp * and the work tree's base commit.
686 d5bea539 2019-05-13 stsp *
687 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
688 d5bea539 2019-05-13 stsp *
689 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
690 d5bea539 2019-05-13 stsp * \ /
691 d5bea539 2019-05-13 stsp * C E
692 d5bea539 2019-05-13 stsp * \ /
693 d5bea539 2019-05-13 stsp * B (yca)
694 d5bea539 2019-05-13 stsp * |
695 d5bea539 2019-05-13 stsp * A
696 d5bea539 2019-05-13 stsp *
697 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
698 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
699 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
700 d5bea539 2019-05-13 stsp */
701 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
703 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
704 8069f636 2019-01-12 stsp
705 d5bea539 2019-05-13 stsp free(yca_id);
706 d5bea539 2019-05-13 stsp return NULL;
707 c09a553d 2018-03-12 stsp }
708 a367ff0f 2019-05-14 stsp
709 a367ff0f 2019-05-14 stsp static const struct got_error *
710 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
711 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
712 a51a74b3 2019-07-27 stsp struct got_repository *repo)
713 a367ff0f 2019-05-14 stsp {
714 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
715 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
716 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
717 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
718 a367ff0f 2019-05-14 stsp
719 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
720 a367ff0f 2019-05-14 stsp if (err)
721 a51a74b3 2019-07-27 stsp goto done;
722 a51a74b3 2019-07-27 stsp
723 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
724 a51a74b3 2019-07-27 stsp is_same_branch = 1;
725 a51a74b3 2019-07-27 stsp goto done;
726 a51a74b3 2019-07-27 stsp }
727 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
728 a51a74b3 2019-07-27 stsp is_same_branch = 1;
729 a367ff0f 2019-05-14 stsp goto done;
730 a51a74b3 2019-07-27 stsp }
731 a367ff0f 2019-05-14 stsp
732 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
733 a367ff0f 2019-05-14 stsp if (err)
734 a367ff0f 2019-05-14 stsp goto done;
735 a367ff0f 2019-05-14 stsp
736 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
737 a367ff0f 2019-05-14 stsp if (err)
738 a367ff0f 2019-05-14 stsp goto done;
739 a367ff0f 2019-05-14 stsp
740 a367ff0f 2019-05-14 stsp for (;;) {
741 a367ff0f 2019-05-14 stsp struct got_object_id *id;
742 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
743 a367ff0f 2019-05-14 stsp if (err) {
744 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
745 a367ff0f 2019-05-14 stsp err = NULL;
746 a367ff0f 2019-05-14 stsp break;
747 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
748 a367ff0f 2019-05-14 stsp break;
749 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
750 a367ff0f 2019-05-14 stsp repo);
751 a367ff0f 2019-05-14 stsp if (err)
752 a367ff0f 2019-05-14 stsp break;
753 a367ff0f 2019-05-14 stsp }
754 c09a553d 2018-03-12 stsp
755 a367ff0f 2019-05-14 stsp if (id) {
756 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
757 a51a74b3 2019-07-27 stsp break;
758 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
759 a367ff0f 2019-05-14 stsp is_same_branch = 1;
760 a367ff0f 2019-05-14 stsp break;
761 a367ff0f 2019-05-14 stsp }
762 a367ff0f 2019-05-14 stsp }
763 a367ff0f 2019-05-14 stsp }
764 a367ff0f 2019-05-14 stsp done:
765 a367ff0f 2019-05-14 stsp if (graph)
766 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
767 a367ff0f 2019-05-14 stsp free(head_commit_id);
768 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
769 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
770 04f57cb3 2019-07-25 stsp return err;
771 04f57cb3 2019-07-25 stsp }
772 04f57cb3 2019-07-25 stsp
773 04f57cb3 2019-07-25 stsp static const struct got_error *
774 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
775 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
776 04f57cb3 2019-07-25 stsp {
777 04f57cb3 2019-07-25 stsp const struct got_error *err;
778 04f57cb3 2019-07-25 stsp struct got_reference *ref;
779 04f57cb3 2019-07-25 stsp
780 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
781 04f57cb3 2019-07-25 stsp if (err == NULL) {
782 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
783 04f57cb3 2019-07-25 stsp got_ref_close(ref);
784 04f57cb3 2019-07-25 stsp } else {
785 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
786 04f57cb3 2019-07-25 stsp return err;
787 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
788 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
789 04f57cb3 2019-07-25 stsp }
790 a367ff0f 2019-05-14 stsp return err;
791 a367ff0f 2019-05-14 stsp }
792 8069f636 2019-01-12 stsp
793 4ed7e80c 2018-05-20 stsp static const struct got_error *
794 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
795 c09a553d 2018-03-12 stsp {
796 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
797 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
798 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
799 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
800 c09a553d 2018-03-12 stsp char *repo_path = NULL;
801 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
802 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
803 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
804 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
805 72151b04 2019-05-11 stsp int ch, same_path_prefix;
806 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
807 f2ea84fa 2019-07-27 stsp
808 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
809 c09a553d 2018-03-12 stsp
810 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
811 0bb8a95e 2018-03-12 stsp switch (ch) {
812 08573d5b 2019-05-14 stsp case 'b':
813 08573d5b 2019-05-14 stsp branch_name = optarg;
814 08573d5b 2019-05-14 stsp break;
815 8069f636 2019-01-12 stsp case 'c':
816 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
817 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
818 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
819 8069f636 2019-01-12 stsp break;
820 0bb8a95e 2018-03-12 stsp case 'p':
821 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
822 0bb8a95e 2018-03-12 stsp break;
823 0bb8a95e 2018-03-12 stsp default:
824 2deda0b9 2019-03-07 stsp usage_checkout();
825 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
826 0bb8a95e 2018-03-12 stsp }
827 0bb8a95e 2018-03-12 stsp }
828 0bb8a95e 2018-03-12 stsp
829 0bb8a95e 2018-03-12 stsp argc -= optind;
830 0bb8a95e 2018-03-12 stsp argv += optind;
831 0bb8a95e 2018-03-12 stsp
832 6715a751 2018-03-16 stsp #ifndef PROFILE
833 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
834 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
835 c09a553d 2018-03-12 stsp err(1, "pledge");
836 6715a751 2018-03-16 stsp #endif
837 0bb8a95e 2018-03-12 stsp if (argc == 1) {
838 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
839 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
840 76089277 2018-04-01 stsp if (repo_path == NULL)
841 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
842 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
843 76089277 2018-04-01 stsp if (cwd == NULL) {
844 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
845 76089277 2018-04-01 stsp goto done;
846 76089277 2018-04-01 stsp }
847 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
848 230a42bd 2019-05-11 jcs base = basename(path_prefix);
849 230a42bd 2019-05-11 jcs if (base == NULL) {
850 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
851 230a42bd 2019-05-11 jcs path_prefix);
852 230a42bd 2019-05-11 jcs goto done;
853 230a42bd 2019-05-11 jcs }
854 230a42bd 2019-05-11 jcs } else {
855 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
856 230a42bd 2019-05-11 jcs if (base == NULL) {
857 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
858 230a42bd 2019-05-11 jcs repo_path);
859 230a42bd 2019-05-11 jcs goto done;
860 230a42bd 2019-05-11 jcs }
861 76089277 2018-04-01 stsp }
862 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
863 c09a553d 2018-03-12 stsp if (dotgit)
864 c09a553d 2018-03-12 stsp *dotgit = '\0';
865 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
866 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
867 c09a553d 2018-03-12 stsp free(cwd);
868 76089277 2018-04-01 stsp goto done;
869 c09a553d 2018-03-12 stsp }
870 c09a553d 2018-03-12 stsp free(cwd);
871 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
872 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
873 76089277 2018-04-01 stsp if (repo_path == NULL) {
874 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
875 76089277 2018-04-01 stsp goto done;
876 76089277 2018-04-01 stsp }
877 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
878 76089277 2018-04-01 stsp if (worktree_path == NULL) {
879 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
880 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
881 b4b3a7dd 2019-07-22 stsp argv[1]);
882 b4b3a7dd 2019-07-22 stsp goto done;
883 b4b3a7dd 2019-07-22 stsp }
884 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
885 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
886 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
887 b4b3a7dd 2019-07-22 stsp goto done;
888 b4b3a7dd 2019-07-22 stsp }
889 76089277 2018-04-01 stsp }
890 c09a553d 2018-03-12 stsp } else
891 c09a553d 2018-03-12 stsp usage_checkout();
892 c09a553d 2018-03-12 stsp
893 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
894 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
895 13bfb272 2019-05-10 jcs
896 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
897 c09a553d 2018-03-12 stsp if (error != NULL)
898 c02c541e 2019-03-29 stsp goto done;
899 c02c541e 2019-03-29 stsp
900 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
901 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
902 c530dc23 2019-07-23 stsp if (error) {
903 c530dc23 2019-07-23 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
904 c530dc23 2019-07-23 stsp goto done;
905 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
906 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
907 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
908 c530dc23 2019-07-23 stsp goto done;
909 c530dc23 2019-07-23 stsp }
910 c530dc23 2019-07-23 stsp }
911 c530dc23 2019-07-23 stsp
912 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
913 c02c541e 2019-03-29 stsp if (error)
914 c09a553d 2018-03-12 stsp goto done;
915 8069f636 2019-01-12 stsp
916 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
917 c09a553d 2018-03-12 stsp if (error != NULL)
918 c09a553d 2018-03-12 stsp goto done;
919 c09a553d 2018-03-12 stsp
920 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
921 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
922 c09a553d 2018-03-12 stsp goto done;
923 c09a553d 2018-03-12 stsp
924 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
925 c09a553d 2018-03-12 stsp if (error != NULL)
926 c09a553d 2018-03-12 stsp goto done;
927 c09a553d 2018-03-12 stsp
928 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
929 e5dc7198 2018-12-29 stsp path_prefix);
930 e5dc7198 2018-12-29 stsp if (error != NULL)
931 e5dc7198 2018-12-29 stsp goto done;
932 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
933 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
934 49520a32 2018-12-29 stsp goto done;
935 49520a32 2018-12-29 stsp }
936 49520a32 2018-12-29 stsp
937 8069f636 2019-01-12 stsp if (commit_id_str) {
938 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
939 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
940 30837e32 2019-07-25 stsp if (error)
941 8069f636 2019-01-12 stsp goto done;
942 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
943 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
944 8069f636 2019-01-12 stsp if (error != NULL) {
945 8069f636 2019-01-12 stsp free(commit_id);
946 8069f636 2019-01-12 stsp goto done;
947 8069f636 2019-01-12 stsp }
948 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
949 45d344f6 2019-05-14 stsp if (error)
950 45d344f6 2019-05-14 stsp goto done;
951 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
952 8069f636 2019-01-12 stsp commit_id);
953 8069f636 2019-01-12 stsp free(commit_id);
954 8069f636 2019-01-12 stsp if (error)
955 8069f636 2019-01-12 stsp goto done;
956 8069f636 2019-01-12 stsp }
957 8069f636 2019-01-12 stsp
958 f2ea84fa 2019-07-27 stsp error = got_pathlist_append(NULL, &paths, "", NULL);
959 f2ea84fa 2019-07-27 stsp if (error)
960 f2ea84fa 2019-07-27 stsp goto done;
961 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
962 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
963 c09a553d 2018-03-12 stsp if (error != NULL)
964 c09a553d 2018-03-12 stsp goto done;
965 c09a553d 2018-03-12 stsp
966 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
967 c09a553d 2018-03-12 stsp
968 c09a553d 2018-03-12 stsp done:
969 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
970 8069f636 2019-01-12 stsp free(commit_id_str);
971 76089277 2018-04-01 stsp free(repo_path);
972 507dc3bb 2018-12-29 stsp free(worktree_path);
973 507dc3bb 2018-12-29 stsp return error;
974 507dc3bb 2018-12-29 stsp }
975 507dc3bb 2018-12-29 stsp
976 507dc3bb 2018-12-29 stsp __dead static void
977 507dc3bb 2018-12-29 stsp usage_update(void)
978 507dc3bb 2018-12-29 stsp {
979 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
980 507dc3bb 2018-12-29 stsp getprogname());
981 507dc3bb 2018-12-29 stsp exit(1);
982 507dc3bb 2018-12-29 stsp }
983 507dc3bb 2018-12-29 stsp
984 1ee397ad 2019-07-12 stsp static const struct got_error *
985 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
986 507dc3bb 2018-12-29 stsp {
987 784955db 2019-01-12 stsp int *did_something = arg;
988 784955db 2019-01-12 stsp
989 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
990 1ee397ad 2019-07-12 stsp return NULL;
991 507dc3bb 2018-12-29 stsp
992 1545c615 2019-02-10 stsp *did_something = 1;
993 a484d721 2019-06-10 stsp
994 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
995 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
996 1ee397ad 2019-07-12 stsp return NULL;
997 a484d721 2019-06-10 stsp
998 507dc3bb 2018-12-29 stsp while (path[0] == '/')
999 507dc3bb 2018-12-29 stsp path++;
1000 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1001 1ee397ad 2019-07-12 stsp return NULL;
1002 be7061eb 2018-12-30 stsp }
1003 be7061eb 2018-12-30 stsp
1004 be7061eb 2018-12-30 stsp static const struct got_error *
1005 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1006 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1007 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1008 a1fb16d8 2019-05-24 stsp {
1009 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1010 a1fb16d8 2019-05-24 stsp char *base_id_str;
1011 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1012 a1fb16d8 2019-05-24 stsp
1013 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1014 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1015 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1016 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1017 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1018 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1019 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1020 a1fb16d8 2019-05-24 stsp }
1021 a1fb16d8 2019-05-24 stsp
1022 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1023 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1024 a1fb16d8 2019-05-24 stsp if (err) {
1025 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1026 a1fb16d8 2019-05-24 stsp return err;
1027 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1028 a1fb16d8 2019-05-24 stsp }
1029 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1030 a1fb16d8 2019-05-24 stsp return NULL;
1031 a1fb16d8 2019-05-24 stsp
1032 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1033 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1034 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1035 a1fb16d8 2019-05-24 stsp if (err)
1036 a1fb16d8 2019-05-24 stsp return err;
1037 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1038 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1039 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1040 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1041 0ebf8283 2019-07-24 stsp return NULL;
1042 0ebf8283 2019-07-24 stsp }
1043 0ebf8283 2019-07-24 stsp
1044 0ebf8283 2019-07-24 stsp static const struct got_error *
1045 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1046 0ebf8283 2019-07-24 stsp {
1047 0ebf8283 2019-07-24 stsp const struct got_error *err;
1048 0ebf8283 2019-07-24 stsp int in_progress;
1049 0ebf8283 2019-07-24 stsp
1050 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1051 0ebf8283 2019-07-24 stsp if (err)
1052 0ebf8283 2019-07-24 stsp return err;
1053 0ebf8283 2019-07-24 stsp if (in_progress)
1054 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1055 0ebf8283 2019-07-24 stsp
1056 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1057 0ebf8283 2019-07-24 stsp if (err)
1058 0ebf8283 2019-07-24 stsp return err;
1059 0ebf8283 2019-07-24 stsp if (in_progress)
1060 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1061 0ebf8283 2019-07-24 stsp
1062 a1fb16d8 2019-05-24 stsp return NULL;
1063 a5edda0a 2019-07-27 stsp }
1064 a5edda0a 2019-07-27 stsp
1065 a5edda0a 2019-07-27 stsp static const struct got_error *
1066 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1067 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1068 a5edda0a 2019-07-27 stsp {
1069 a5edda0a 2019-07-27 stsp const struct got_error *err;
1070 a5edda0a 2019-07-27 stsp char *path;
1071 a5edda0a 2019-07-27 stsp int i;
1072 a5edda0a 2019-07-27 stsp
1073 a5edda0a 2019-07-27 stsp if (argc == 0) {
1074 a5edda0a 2019-07-27 stsp path = strdup("");
1075 a5edda0a 2019-07-27 stsp if (path == NULL)
1076 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1077 a5edda0a 2019-07-27 stsp return got_pathlist_append(NULL, paths, path, NULL);
1078 a5edda0a 2019-07-27 stsp }
1079 a5edda0a 2019-07-27 stsp
1080 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1081 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1082 a5edda0a 2019-07-27 stsp if (err)
1083 a5edda0a 2019-07-27 stsp break;
1084 a5edda0a 2019-07-27 stsp err = got_pathlist_append(NULL, paths, path, NULL);
1085 a5edda0a 2019-07-27 stsp if (err) {
1086 a5edda0a 2019-07-27 stsp free(path);
1087 a5edda0a 2019-07-27 stsp break;
1088 a5edda0a 2019-07-27 stsp }
1089 a5edda0a 2019-07-27 stsp }
1090 a5edda0a 2019-07-27 stsp
1091 a5edda0a 2019-07-27 stsp return err;
1092 a1fb16d8 2019-05-24 stsp }
1093 a1fb16d8 2019-05-24 stsp
1094 a1fb16d8 2019-05-24 stsp static const struct got_error *
1095 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1096 507dc3bb 2018-12-29 stsp {
1097 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1098 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1099 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1100 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1101 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1102 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1103 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1104 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1105 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1106 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1107 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1108 507dc3bb 2018-12-29 stsp
1109 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1110 f2ea84fa 2019-07-27 stsp
1111 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1112 507dc3bb 2018-12-29 stsp switch (ch) {
1113 024e9686 2019-05-14 stsp case 'b':
1114 024e9686 2019-05-14 stsp branch_name = optarg;
1115 024e9686 2019-05-14 stsp break;
1116 507dc3bb 2018-12-29 stsp case 'c':
1117 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1118 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1119 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1120 507dc3bb 2018-12-29 stsp break;
1121 507dc3bb 2018-12-29 stsp default:
1122 2deda0b9 2019-03-07 stsp usage_update();
1123 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1124 507dc3bb 2018-12-29 stsp }
1125 507dc3bb 2018-12-29 stsp }
1126 507dc3bb 2018-12-29 stsp
1127 507dc3bb 2018-12-29 stsp argc -= optind;
1128 507dc3bb 2018-12-29 stsp argv += optind;
1129 507dc3bb 2018-12-29 stsp
1130 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1131 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1132 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1133 507dc3bb 2018-12-29 stsp err(1, "pledge");
1134 507dc3bb 2018-12-29 stsp #endif
1135 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1136 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1137 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1138 c4cdcb68 2019-04-03 stsp goto done;
1139 c4cdcb68 2019-04-03 stsp }
1140 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1141 7d5807f4 2019-07-11 stsp if (error)
1142 7d5807f4 2019-07-11 stsp goto done;
1143 7d5807f4 2019-07-11 stsp
1144 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1145 c4cdcb68 2019-04-03 stsp if (error)
1146 7d5807f4 2019-07-11 stsp goto done;
1147 c4cdcb68 2019-04-03 stsp
1148 f2ea84fa 2019-07-27 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1149 f2ea84fa 2019-07-27 stsp if (error)
1150 f2ea84fa 2019-07-27 stsp goto done;
1151 507dc3bb 2018-12-29 stsp
1152 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1153 507dc3bb 2018-12-29 stsp if (error != NULL)
1154 507dc3bb 2018-12-29 stsp goto done;
1155 507dc3bb 2018-12-29 stsp
1156 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1157 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1158 0266afb7 2019-01-04 stsp if (error)
1159 0266afb7 2019-01-04 stsp goto done;
1160 0266afb7 2019-01-04 stsp
1161 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1162 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1163 024e9686 2019-05-14 stsp if (error != NULL)
1164 024e9686 2019-05-14 stsp goto done;
1165 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1166 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1167 9c4b8182 2019-01-02 stsp if (error != NULL)
1168 9c4b8182 2019-01-02 stsp goto done;
1169 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1170 507dc3bb 2018-12-29 stsp if (error != NULL)
1171 507dc3bb 2018-12-29 stsp goto done;
1172 507dc3bb 2018-12-29 stsp } else {
1173 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1174 04f57cb3 2019-07-25 stsp free(commit_id_str);
1175 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1176 30837e32 2019-07-25 stsp if (error)
1177 a297e751 2019-07-11 stsp goto done;
1178 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1179 a297e751 2019-07-11 stsp if (error)
1180 507dc3bb 2018-12-29 stsp goto done;
1181 507dc3bb 2018-12-29 stsp }
1182 35c965b2 2018-12-29 stsp
1183 a1fb16d8 2019-05-24 stsp if (branch_name) {
1184 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1185 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1186 f2ea84fa 2019-07-27 stsp if (strlen(pe->path) == 0)
1187 f2ea84fa 2019-07-27 stsp continue;
1188 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1189 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1190 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1191 024e9686 2019-05-14 stsp goto done;
1192 024e9686 2019-05-14 stsp }
1193 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1194 024e9686 2019-05-14 stsp if (error)
1195 024e9686 2019-05-14 stsp goto done;
1196 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1197 a367ff0f 2019-05-14 stsp free(head_commit_id);
1198 024e9686 2019-05-14 stsp if (error != NULL)
1199 024e9686 2019-05-14 stsp goto done;
1200 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1201 a367ff0f 2019-05-14 stsp if (error)
1202 a367ff0f 2019-05-14 stsp goto done;
1203 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1204 024e9686 2019-05-14 stsp if (error)
1205 024e9686 2019-05-14 stsp goto done;
1206 024e9686 2019-05-14 stsp } else {
1207 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1208 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1209 a367ff0f 2019-05-14 stsp if (error != NULL) {
1210 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1211 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1212 024e9686 2019-05-14 stsp goto done;
1213 a367ff0f 2019-05-14 stsp }
1214 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1215 a367ff0f 2019-05-14 stsp if (error)
1216 a367ff0f 2019-05-14 stsp goto done;
1217 024e9686 2019-05-14 stsp }
1218 507dc3bb 2018-12-29 stsp
1219 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1220 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1221 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1222 507dc3bb 2018-12-29 stsp commit_id);
1223 507dc3bb 2018-12-29 stsp if (error)
1224 507dc3bb 2018-12-29 stsp goto done;
1225 507dc3bb 2018-12-29 stsp }
1226 507dc3bb 2018-12-29 stsp
1227 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1228 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1229 507dc3bb 2018-12-29 stsp if (error != NULL)
1230 507dc3bb 2018-12-29 stsp goto done;
1231 9c4b8182 2019-01-02 stsp
1232 784955db 2019-01-12 stsp if (did_something)
1233 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1234 784955db 2019-01-12 stsp else
1235 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1236 507dc3bb 2018-12-29 stsp done:
1237 c09a553d 2018-03-12 stsp free(worktree_path);
1238 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1239 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1240 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1241 507dc3bb 2018-12-29 stsp free(commit_id);
1242 9c4b8182 2019-01-02 stsp free(commit_id_str);
1243 c09a553d 2018-03-12 stsp return error;
1244 c09a553d 2018-03-12 stsp }
1245 c09a553d 2018-03-12 stsp
1246 f42b1b34 2018-03-12 stsp static const struct got_error *
1247 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1248 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1249 5c860e29 2018-03-12 stsp {
1250 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1251 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1252 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1253 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1254 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1255 79109fed 2018-03-27 stsp
1256 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1257 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1258 79109fed 2018-03-27 stsp if (err)
1259 79109fed 2018-03-27 stsp return err;
1260 79109fed 2018-03-27 stsp
1261 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1262 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1263 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1264 79109fed 2018-03-27 stsp
1265 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1266 79109fed 2018-03-27 stsp if (err)
1267 79109fed 2018-03-27 stsp return err;
1268 79109fed 2018-03-27 stsp
1269 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1270 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1271 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1272 0f2b3dca 2018-12-22 stsp if (err)
1273 0f2b3dca 2018-12-22 stsp return err;
1274 0f2b3dca 2018-12-22 stsp
1275 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1276 79109fed 2018-03-27 stsp if (err)
1277 79109fed 2018-03-27 stsp return err;
1278 79109fed 2018-03-27 stsp }
1279 79109fed 2018-03-27 stsp
1280 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1281 0f2b3dca 2018-12-22 stsp if (err)
1282 0f2b3dca 2018-12-22 stsp goto done;
1283 0f2b3dca 2018-12-22 stsp
1284 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1285 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1286 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1287 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1288 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1289 0f2b3dca 2018-12-22 stsp done:
1290 79109fed 2018-03-27 stsp if (tree1)
1291 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1292 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1293 0f2b3dca 2018-12-22 stsp free(id_str1);
1294 0f2b3dca 2018-12-22 stsp free(id_str2);
1295 79109fed 2018-03-27 stsp return err;
1296 79109fed 2018-03-27 stsp }
1297 79109fed 2018-03-27 stsp
1298 4bb494d5 2018-06-16 stsp static char *
1299 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1300 6c281f94 2018-06-11 stsp {
1301 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1302 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1303 6c281f94 2018-06-11 stsp if (p)
1304 6c281f94 2018-06-11 stsp *p = '\0';
1305 6c281f94 2018-06-11 stsp return s;
1306 6c281f94 2018-06-11 stsp }
1307 6c281f94 2018-06-11 stsp
1308 79109fed 2018-03-27 stsp static const struct got_error *
1309 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1310 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1311 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1312 79109fed 2018-03-27 stsp {
1313 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1314 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1315 6c281f94 2018-06-11 stsp char datebuf[26];
1316 45d799e2 2018-12-23 stsp time_t committer_time;
1317 45d799e2 2018-12-23 stsp const char *author, *committer;
1318 199a4027 2019-02-02 stsp char *refs_str = NULL;
1319 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1320 5c860e29 2018-03-12 stsp
1321 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1322 199a4027 2019-02-02 stsp char *s;
1323 199a4027 2019-02-02 stsp const char *name;
1324 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1325 199a4027 2019-02-02 stsp continue;
1326 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1327 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1328 d9498b20 2019-02-05 stsp continue;
1329 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1330 199a4027 2019-02-02 stsp name += 5;
1331 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1332 7143d404 2019-03-12 stsp continue;
1333 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1334 e34f9ed6 2019-02-02 stsp name += 6;
1335 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1336 141c2bff 2019-02-04 stsp name += 8;
1337 199a4027 2019-02-02 stsp s = refs_str;
1338 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1339 199a4027 2019-02-02 stsp name) == -1) {
1340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1341 199a4027 2019-02-02 stsp free(s);
1342 199a4027 2019-02-02 stsp break;
1343 199a4027 2019-02-02 stsp }
1344 199a4027 2019-02-02 stsp free(s);
1345 199a4027 2019-02-02 stsp }
1346 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1347 8bf5b3c9 2018-03-17 stsp if (err)
1348 8bf5b3c9 2018-03-17 stsp return err;
1349 788c352e 2018-06-16 stsp
1350 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1351 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1352 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1353 832c249c 2018-06-10 stsp free(id_str);
1354 d3d493d7 2019-02-21 stsp id_str = NULL;
1355 d3d493d7 2019-02-21 stsp free(refs_str);
1356 d3d493d7 2019-02-21 stsp refs_str = NULL;
1357 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1358 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1359 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1360 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1361 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1362 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1363 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1364 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1365 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1366 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1367 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1368 3fe1abad 2018-06-10 stsp int n = 1;
1369 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1370 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1371 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1372 a0603db2 2018-06-10 stsp if (err)
1373 a0603db2 2018-06-10 stsp return err;
1374 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1375 a0603db2 2018-06-10 stsp free(id_str);
1376 a0603db2 2018-06-10 stsp }
1377 a0603db2 2018-06-10 stsp }
1378 832c249c 2018-06-10 stsp
1379 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1380 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1381 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1382 8bf5b3c9 2018-03-17 stsp
1383 621015ac 2018-07-23 stsp logmsg = logmsg0;
1384 832c249c 2018-06-10 stsp do {
1385 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1386 832c249c 2018-06-10 stsp if (line)
1387 832c249c 2018-06-10 stsp printf(" %s\n", line);
1388 832c249c 2018-06-10 stsp } while (line);
1389 621015ac 2018-07-23 stsp free(logmsg0);
1390 832c249c 2018-06-10 stsp
1391 971751ac 2018-03-27 stsp if (show_patch) {
1392 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1393 971751ac 2018-03-27 stsp if (err == 0)
1394 971751ac 2018-03-27 stsp printf("\n");
1395 971751ac 2018-03-27 stsp }
1396 07862c20 2018-09-15 stsp
1397 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1398 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1399 07862c20 2018-09-15 stsp return err;
1400 f42b1b34 2018-03-12 stsp }
1401 5c860e29 2018-03-12 stsp
1402 f42b1b34 2018-03-12 stsp static const struct got_error *
1403 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1404 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1405 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1406 f42b1b34 2018-03-12 stsp {
1407 f42b1b34 2018-03-12 stsp const struct got_error *err;
1408 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1409 372ccdbb 2018-06-10 stsp
1410 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1411 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1412 f42b1b34 2018-03-12 stsp if (err)
1413 f42b1b34 2018-03-12 stsp return err;
1414 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1415 372ccdbb 2018-06-10 stsp if (err)
1416 fcc85cad 2018-07-22 stsp goto done;
1417 656b1f76 2019-05-11 jcs for (;;) {
1418 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1419 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1420 8bf5b3c9 2018-03-17 stsp
1421 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1422 84453469 2018-11-11 stsp break;
1423 84453469 2018-11-11 stsp
1424 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1425 372ccdbb 2018-06-10 stsp if (err) {
1426 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1427 9ba79e04 2018-06-11 stsp err = NULL;
1428 9ba79e04 2018-06-11 stsp break;
1429 9ba79e04 2018-06-11 stsp }
1430 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1431 372ccdbb 2018-06-10 stsp break;
1432 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1433 372ccdbb 2018-06-10 stsp if (err)
1434 372ccdbb 2018-06-10 stsp break;
1435 372ccdbb 2018-06-10 stsp else
1436 372ccdbb 2018-06-10 stsp continue;
1437 7e665116 2018-04-02 stsp }
1438 b43fbaa0 2018-06-11 stsp if (id == NULL)
1439 7e665116 2018-04-02 stsp break;
1440 b43fbaa0 2018-06-11 stsp
1441 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1442 b43fbaa0 2018-06-11 stsp if (err)
1443 fcc85cad 2018-07-22 stsp break;
1444 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1445 199a4027 2019-02-02 stsp refs);
1446 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1447 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1448 7e665116 2018-04-02 stsp break;
1449 31cedeaf 2018-09-15 stsp }
1450 fcc85cad 2018-07-22 stsp done:
1451 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1452 f42b1b34 2018-03-12 stsp return err;
1453 f42b1b34 2018-03-12 stsp }
1454 5c860e29 2018-03-12 stsp
1455 4ed7e80c 2018-05-20 stsp __dead static void
1456 6f3d1eb0 2018-03-12 stsp usage_log(void)
1457 6f3d1eb0 2018-03-12 stsp {
1458 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1459 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1460 6f3d1eb0 2018-03-12 stsp exit(1);
1461 6f3d1eb0 2018-03-12 stsp }
1462 6f3d1eb0 2018-03-12 stsp
1463 4ed7e80c 2018-05-20 stsp static const struct got_error *
1464 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1465 f42b1b34 2018-03-12 stsp {
1466 f42b1b34 2018-03-12 stsp const struct got_error *error;
1467 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1468 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1469 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1470 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1471 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1472 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1473 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1474 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1475 64a96a6d 2018-04-01 stsp const char *errstr;
1476 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1477 1b3893a2 2019-03-18 stsp
1478 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1479 5c860e29 2018-03-12 stsp
1480 6715a751 2018-03-16 stsp #ifndef PROFILE
1481 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1482 6098196c 2019-01-04 stsp NULL)
1483 ad242220 2018-09-08 stsp == -1)
1484 f42b1b34 2018-03-12 stsp err(1, "pledge");
1485 6715a751 2018-03-16 stsp #endif
1486 79109fed 2018-03-27 stsp
1487 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1488 79109fed 2018-03-27 stsp switch (ch) {
1489 79109fed 2018-03-27 stsp case 'p':
1490 79109fed 2018-03-27 stsp show_patch = 1;
1491 d142fc45 2018-04-01 stsp break;
1492 d142fc45 2018-04-01 stsp case 'c':
1493 d142fc45 2018-04-01 stsp start_commit = optarg;
1494 64a96a6d 2018-04-01 stsp break;
1495 c0cc5c62 2018-10-18 stsp case 'C':
1496 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1497 4a8520aa 2018-10-18 stsp &errstr);
1498 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1499 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1500 c0cc5c62 2018-10-18 stsp break;
1501 64a96a6d 2018-04-01 stsp case 'l':
1502 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1503 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1504 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1505 cc54c501 2019-07-15 stsp break;
1506 cc54c501 2019-07-15 stsp case 'f':
1507 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1508 a0603db2 2018-06-10 stsp break;
1509 04ca23f4 2018-07-16 stsp case 'r':
1510 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1511 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1512 04ca23f4 2018-07-16 stsp err(1, "-r option");
1513 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1514 04ca23f4 2018-07-16 stsp break;
1515 79109fed 2018-03-27 stsp default:
1516 2deda0b9 2019-03-07 stsp usage_log();
1517 79109fed 2018-03-27 stsp /* NOTREACHED */
1518 79109fed 2018-03-27 stsp }
1519 79109fed 2018-03-27 stsp }
1520 79109fed 2018-03-27 stsp
1521 79109fed 2018-03-27 stsp argc -= optind;
1522 79109fed 2018-03-27 stsp argv += optind;
1523 f42b1b34 2018-03-12 stsp
1524 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1525 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1526 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1527 04ca23f4 2018-07-16 stsp goto done;
1528 04ca23f4 2018-07-16 stsp }
1529 cffc0aa4 2019-02-05 stsp
1530 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1531 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1532 cffc0aa4 2019-02-05 stsp goto done;
1533 cffc0aa4 2019-02-05 stsp error = NULL;
1534 cffc0aa4 2019-02-05 stsp
1535 e7301579 2019-03-18 stsp if (argc == 0) {
1536 cbd1af7a 2019-03-18 stsp path = strdup("");
1537 e7301579 2019-03-18 stsp if (path == NULL) {
1538 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1539 cbd1af7a 2019-03-18 stsp goto done;
1540 e7301579 2019-03-18 stsp }
1541 e7301579 2019-03-18 stsp } else if (argc == 1) {
1542 e7301579 2019-03-18 stsp if (worktree) {
1543 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1544 e7301579 2019-03-18 stsp argv[0]);
1545 e7301579 2019-03-18 stsp if (error)
1546 e7301579 2019-03-18 stsp goto done;
1547 e7301579 2019-03-18 stsp } else {
1548 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1549 e7301579 2019-03-18 stsp if (path == NULL) {
1550 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1551 e7301579 2019-03-18 stsp goto done;
1552 e7301579 2019-03-18 stsp }
1553 e7301579 2019-03-18 stsp }
1554 cbd1af7a 2019-03-18 stsp } else
1555 cbd1af7a 2019-03-18 stsp usage_log();
1556 cbd1af7a 2019-03-18 stsp
1557 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1558 5486daa2 2019-05-11 stsp repo_path = worktree ?
1559 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1560 5486daa2 2019-05-11 stsp }
1561 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1562 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1563 cffc0aa4 2019-02-05 stsp goto done;
1564 04ca23f4 2018-07-16 stsp }
1565 6098196c 2019-01-04 stsp
1566 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1567 d7d4f210 2018-03-12 stsp if (error != NULL)
1568 04ca23f4 2018-07-16 stsp goto done;
1569 f42b1b34 2018-03-12 stsp
1570 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1571 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1572 c02c541e 2019-03-29 stsp if (error)
1573 c02c541e 2019-03-29 stsp goto done;
1574 c02c541e 2019-03-29 stsp
1575 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1576 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1577 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1578 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1579 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1580 3235492e 2018-04-01 stsp if (error != NULL)
1581 3235492e 2018-04-01 stsp return error;
1582 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1583 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1584 3235492e 2018-04-01 stsp if (error != NULL)
1585 3235492e 2018-04-01 stsp return error;
1586 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1587 3235492e 2018-04-01 stsp } else {
1588 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1589 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1590 3235492e 2018-04-01 stsp if (error == NULL) {
1591 1caad61b 2019-02-01 stsp int obj_type;
1592 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1593 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1594 d1f2edc9 2018-06-13 stsp if (error != NULL)
1595 1caad61b 2019-02-01 stsp goto done;
1596 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1597 1caad61b 2019-02-01 stsp if (error != NULL)
1598 1caad61b 2019-02-01 stsp goto done;
1599 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1600 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1601 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1602 1caad61b 2019-02-01 stsp if (error != NULL)
1603 1caad61b 2019-02-01 stsp goto done;
1604 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1605 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1606 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1607 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1608 1caad61b 2019-02-01 stsp goto done;
1609 1caad61b 2019-02-01 stsp }
1610 1caad61b 2019-02-01 stsp free(id);
1611 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1612 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1613 1caad61b 2019-02-01 stsp if (id == NULL)
1614 638f9024 2019-05-13 stsp error = got_error_from_errno(
1615 230a42bd 2019-05-11 jcs "got_object_id_dup");
1616 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1617 1caad61b 2019-02-01 stsp if (error)
1618 1caad61b 2019-02-01 stsp goto done;
1619 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1620 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1621 1caad61b 2019-02-01 stsp goto done;
1622 1caad61b 2019-02-01 stsp }
1623 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1624 d1f2edc9 2018-06-13 stsp if (error != NULL)
1625 1caad61b 2019-02-01 stsp goto done;
1626 d1f2edc9 2018-06-13 stsp }
1627 15a94983 2018-12-23 stsp if (commit == NULL) {
1628 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1629 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1630 d1f2edc9 2018-06-13 stsp if (error != NULL)
1631 d1f2edc9 2018-06-13 stsp return error;
1632 3235492e 2018-04-01 stsp }
1633 3235492e 2018-04-01 stsp }
1634 d7d4f210 2018-03-12 stsp if (error != NULL)
1635 04ca23f4 2018-07-16 stsp goto done;
1636 04ca23f4 2018-07-16 stsp
1637 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1638 04ca23f4 2018-07-16 stsp if (error != NULL)
1639 04ca23f4 2018-07-16 stsp goto done;
1640 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1641 04ca23f4 2018-07-16 stsp free(path);
1642 04ca23f4 2018-07-16 stsp path = in_repo_path;
1643 04ca23f4 2018-07-16 stsp }
1644 199a4027 2019-02-02 stsp
1645 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1646 199a4027 2019-02-02 stsp if (error)
1647 199a4027 2019-02-02 stsp goto done;
1648 04ca23f4 2018-07-16 stsp
1649 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1650 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1651 04ca23f4 2018-07-16 stsp done:
1652 04ca23f4 2018-07-16 stsp free(path);
1653 04ca23f4 2018-07-16 stsp free(repo_path);
1654 04ca23f4 2018-07-16 stsp free(cwd);
1655 f42b1b34 2018-03-12 stsp free(id);
1656 cffc0aa4 2019-02-05 stsp if (worktree)
1657 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1658 ad242220 2018-09-08 stsp if (repo) {
1659 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1660 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1661 ad242220 2018-09-08 stsp if (error == NULL)
1662 ad242220 2018-09-08 stsp error = repo_error;
1663 ad242220 2018-09-08 stsp }
1664 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1665 8bf5b3c9 2018-03-17 stsp return error;
1666 5c860e29 2018-03-12 stsp }
1667 5c860e29 2018-03-12 stsp
1668 4ed7e80c 2018-05-20 stsp __dead static void
1669 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1670 3f8b7d6a 2018-04-01 stsp {
1671 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1672 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1673 3f8b7d6a 2018-04-01 stsp exit(1);
1674 b00d56cd 2018-04-01 stsp }
1675 b00d56cd 2018-04-01 stsp
1676 b72f483a 2019-02-05 stsp struct print_diff_arg {
1677 b72f483a 2019-02-05 stsp struct got_repository *repo;
1678 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1679 b72f483a 2019-02-05 stsp int diff_context;
1680 3fc0c068 2019-02-10 stsp const char *id_str;
1681 3fc0c068 2019-02-10 stsp int header_shown;
1682 b72f483a 2019-02-05 stsp };
1683 b72f483a 2019-02-05 stsp
1684 4ed7e80c 2018-05-20 stsp static const struct got_error *
1685 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1686 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1687 b72f483a 2019-02-05 stsp {
1688 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1689 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1690 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1691 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1692 b72f483a 2019-02-05 stsp char *abspath = NULL;
1693 b72f483a 2019-02-05 stsp struct stat sb;
1694 b72f483a 2019-02-05 stsp
1695 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1696 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1697 b72f483a 2019-02-05 stsp return NULL;
1698 3fc0c068 2019-02-10 stsp
1699 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1700 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1701 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1702 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1703 3fc0c068 2019-02-10 stsp }
1704 b72f483a 2019-02-05 stsp
1705 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1706 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1707 d00136be 2019-03-26 stsp if (err)
1708 d00136be 2019-03-26 stsp goto done;
1709 b72f483a 2019-02-05 stsp
1710 d00136be 2019-03-26 stsp }
1711 d00136be 2019-03-26 stsp
1712 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1713 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1714 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1715 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1716 2ec1f75b 2019-03-26 stsp goto done;
1717 2ec1f75b 2019-03-26 stsp }
1718 b72f483a 2019-02-05 stsp
1719 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1720 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1721 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1722 2ec1f75b 2019-03-26 stsp goto done;
1723 2ec1f75b 2019-03-26 stsp }
1724 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1725 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1726 2ec1f75b 2019-03-26 stsp goto done;
1727 2ec1f75b 2019-03-26 stsp }
1728 2ec1f75b 2019-03-26 stsp } else
1729 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1730 b72f483a 2019-02-05 stsp
1731 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1732 b72f483a 2019-02-05 stsp stdout);
1733 b72f483a 2019-02-05 stsp done:
1734 b72f483a 2019-02-05 stsp if (blob1)
1735 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1736 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1737 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1738 b72f483a 2019-02-05 stsp free(abspath);
1739 927df6b7 2019-02-10 stsp return err;
1740 927df6b7 2019-02-10 stsp }
1741 927df6b7 2019-02-10 stsp
1742 927df6b7 2019-02-10 stsp static const struct got_error *
1743 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1744 b00d56cd 2018-04-01 stsp {
1745 b00d56cd 2018-04-01 stsp const struct got_error *error;
1746 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1747 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1748 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1749 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1750 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1751 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1752 15a94983 2018-12-23 stsp int type1, type2;
1753 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1754 c0cc5c62 2018-10-18 stsp const char *errstr;
1755 927df6b7 2019-02-10 stsp char *path = NULL;
1756 b00d56cd 2018-04-01 stsp
1757 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1758 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1759 25eccc22 2019-01-04 stsp NULL) == -1)
1760 b00d56cd 2018-04-01 stsp err(1, "pledge");
1761 b00d56cd 2018-04-01 stsp #endif
1762 b00d56cd 2018-04-01 stsp
1763 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1764 b00d56cd 2018-04-01 stsp switch (ch) {
1765 c0cc5c62 2018-10-18 stsp case 'C':
1766 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1767 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1768 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1769 c0cc5c62 2018-10-18 stsp break;
1770 b72f483a 2019-02-05 stsp case 'r':
1771 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1772 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1773 b72f483a 2019-02-05 stsp err(1, "-r option");
1774 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1775 b72f483a 2019-02-05 stsp break;
1776 b00d56cd 2018-04-01 stsp default:
1777 2deda0b9 2019-03-07 stsp usage_diff();
1778 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1779 b00d56cd 2018-04-01 stsp }
1780 b00d56cd 2018-04-01 stsp }
1781 b00d56cd 2018-04-01 stsp
1782 b00d56cd 2018-04-01 stsp argc -= optind;
1783 b00d56cd 2018-04-01 stsp argv += optind;
1784 b00d56cd 2018-04-01 stsp
1785 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1786 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1787 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1788 b72f483a 2019-02-05 stsp goto done;
1789 b72f483a 2019-02-05 stsp }
1790 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1791 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1792 927df6b7 2019-02-10 stsp goto done;
1793 927df6b7 2019-02-10 stsp if (argc <= 1) {
1794 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1795 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1796 927df6b7 2019-02-10 stsp goto done;
1797 927df6b7 2019-02-10 stsp }
1798 b72f483a 2019-02-05 stsp if (repo_path)
1799 b72f483a 2019-02-05 stsp errx(1,
1800 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1801 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1802 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1803 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1804 927df6b7 2019-02-10 stsp goto done;
1805 927df6b7 2019-02-10 stsp }
1806 927df6b7 2019-02-10 stsp if (argc == 1) {
1807 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1808 cbd1af7a 2019-03-18 stsp argv[0]);
1809 927df6b7 2019-02-10 stsp if (error)
1810 927df6b7 2019-02-10 stsp goto done;
1811 927df6b7 2019-02-10 stsp } else {
1812 927df6b7 2019-02-10 stsp path = strdup("");
1813 927df6b7 2019-02-10 stsp if (path == NULL) {
1814 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1815 927df6b7 2019-02-10 stsp goto done;
1816 927df6b7 2019-02-10 stsp }
1817 927df6b7 2019-02-10 stsp }
1818 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1819 15a94983 2018-12-23 stsp id_str1 = argv[0];
1820 15a94983 2018-12-23 stsp id_str2 = argv[1];
1821 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1822 30db809c 2019-06-05 stsp repo_path =
1823 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1824 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1825 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1826 30db809c 2019-06-05 stsp goto done;
1827 30db809c 2019-06-05 stsp }
1828 30db809c 2019-06-05 stsp }
1829 b00d56cd 2018-04-01 stsp } else
1830 b00d56cd 2018-04-01 stsp usage_diff();
1831 25eccc22 2019-01-04 stsp
1832 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1833 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1834 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1835 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1836 b72f483a 2019-02-05 stsp }
1837 b00d56cd 2018-04-01 stsp
1838 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1839 76089277 2018-04-01 stsp free(repo_path);
1840 b00d56cd 2018-04-01 stsp if (error != NULL)
1841 b00d56cd 2018-04-01 stsp goto done;
1842 b00d56cd 2018-04-01 stsp
1843 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1844 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1845 c02c541e 2019-03-29 stsp if (error)
1846 c02c541e 2019-03-29 stsp goto done;
1847 c02c541e 2019-03-29 stsp
1848 30db809c 2019-06-05 stsp if (argc <= 1) {
1849 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1850 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
1851 b72f483a 2019-02-05 stsp char *id_str;
1852 72ea6654 2019-07-27 stsp
1853 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
1854 72ea6654 2019-07-27 stsp
1855 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1856 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1857 b72f483a 2019-02-05 stsp if (error)
1858 b72f483a 2019-02-05 stsp goto done;
1859 b72f483a 2019-02-05 stsp arg.repo = repo;
1860 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1861 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1862 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1863 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1864 b72f483a 2019-02-05 stsp
1865 72ea6654 2019-07-27 stsp error = got_pathlist_append(NULL, &paths, path, NULL);
1866 72ea6654 2019-07-27 stsp if (error)
1867 72ea6654 2019-07-27 stsp goto done;
1868 72ea6654 2019-07-27 stsp
1869 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
1870 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1871 b72f483a 2019-02-05 stsp free(id_str);
1872 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
1873 b72f483a 2019-02-05 stsp goto done;
1874 b72f483a 2019-02-05 stsp }
1875 b72f483a 2019-02-05 stsp
1876 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1877 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1878 e02e74af 2019-05-28 stsp if (error) {
1879 e02e74af 2019-05-28 stsp struct got_reference *ref;
1880 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1881 e02e74af 2019-05-28 stsp goto done;
1882 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1883 e02e74af 2019-05-28 stsp if (error != NULL)
1884 e02e74af 2019-05-28 stsp goto done;
1885 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1886 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1887 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1888 5e70831e 2019-05-28 stsp goto done;
1889 5e70831e 2019-05-28 stsp }
1890 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1891 e02e74af 2019-05-28 stsp got_ref_close(ref);
1892 e02e74af 2019-05-28 stsp if (error != NULL)
1893 5e70831e 2019-05-28 stsp goto done;
1894 5e70831e 2019-05-28 stsp } else {
1895 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1896 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1897 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1898 e02e74af 2019-05-28 stsp goto done;
1899 5e70831e 2019-05-28 stsp }
1900 e02e74af 2019-05-28 stsp }
1901 b00d56cd 2018-04-01 stsp
1902 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1903 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1904 e02e74af 2019-05-28 stsp if (error) {
1905 e02e74af 2019-05-28 stsp struct got_reference *ref;
1906 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1907 e02e74af 2019-05-28 stsp goto done;
1908 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1909 e02e74af 2019-05-28 stsp if (error != NULL)
1910 5e70831e 2019-05-28 stsp goto done;
1911 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1912 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1913 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1914 e02e74af 2019-05-28 stsp goto done;
1915 5e70831e 2019-05-28 stsp }
1916 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1917 e02e74af 2019-05-28 stsp got_ref_close(ref);
1918 e02e74af 2019-05-28 stsp if (error != NULL)
1919 e02e74af 2019-05-28 stsp goto done;
1920 5e70831e 2019-05-28 stsp } else {
1921 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1922 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1923 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1924 5e70831e 2019-05-28 stsp goto done;
1925 5e70831e 2019-05-28 stsp }
1926 e02e74af 2019-05-28 stsp }
1927 b00d56cd 2018-04-01 stsp
1928 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1929 15a94983 2018-12-23 stsp if (error)
1930 15a94983 2018-12-23 stsp goto done;
1931 15a94983 2018-12-23 stsp
1932 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1933 15a94983 2018-12-23 stsp if (error)
1934 15a94983 2018-12-23 stsp goto done;
1935 15a94983 2018-12-23 stsp
1936 15a94983 2018-12-23 stsp if (type1 != type2) {
1937 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1938 b00d56cd 2018-04-01 stsp goto done;
1939 b00d56cd 2018-04-01 stsp }
1940 b00d56cd 2018-04-01 stsp
1941 15a94983 2018-12-23 stsp switch (type1) {
1942 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1943 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1944 54156555 2018-12-24 stsp diff_context, repo, stdout);
1945 b00d56cd 2018-04-01 stsp break;
1946 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1947 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1948 54156555 2018-12-24 stsp diff_context, repo, stdout);
1949 b00d56cd 2018-04-01 stsp break;
1950 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1951 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1952 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1953 c0cc5c62 2018-10-18 stsp repo, stdout);
1954 b00d56cd 2018-04-01 stsp break;
1955 b00d56cd 2018-04-01 stsp default:
1956 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1957 b00d56cd 2018-04-01 stsp }
1958 b00d56cd 2018-04-01 stsp
1959 b00d56cd 2018-04-01 stsp done:
1960 5e70831e 2019-05-28 stsp free(label1);
1961 5e70831e 2019-05-28 stsp free(label2);
1962 15a94983 2018-12-23 stsp free(id1);
1963 15a94983 2018-12-23 stsp free(id2);
1964 927df6b7 2019-02-10 stsp free(path);
1965 b72f483a 2019-02-05 stsp if (worktree)
1966 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1967 ad242220 2018-09-08 stsp if (repo) {
1968 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1969 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1970 ad242220 2018-09-08 stsp if (error == NULL)
1971 ad242220 2018-09-08 stsp error = repo_error;
1972 ad242220 2018-09-08 stsp }
1973 b00d56cd 2018-04-01 stsp return error;
1974 404c43c4 2018-06-21 stsp }
1975 404c43c4 2018-06-21 stsp
1976 404c43c4 2018-06-21 stsp __dead static void
1977 404c43c4 2018-06-21 stsp usage_blame(void)
1978 404c43c4 2018-06-21 stsp {
1979 9270e621 2019-02-05 stsp fprintf(stderr,
1980 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1981 404c43c4 2018-06-21 stsp getprogname());
1982 404c43c4 2018-06-21 stsp exit(1);
1983 b00d56cd 2018-04-01 stsp }
1984 b00d56cd 2018-04-01 stsp
1985 404c43c4 2018-06-21 stsp static const struct got_error *
1986 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1987 404c43c4 2018-06-21 stsp {
1988 404c43c4 2018-06-21 stsp const struct got_error *error;
1989 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1990 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1991 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1992 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1993 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1994 404c43c4 2018-06-21 stsp int ch;
1995 404c43c4 2018-06-21 stsp
1996 404c43c4 2018-06-21 stsp #ifndef PROFILE
1997 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1998 36e2fb66 2019-01-04 stsp NULL) == -1)
1999 404c43c4 2018-06-21 stsp err(1, "pledge");
2000 404c43c4 2018-06-21 stsp #endif
2001 404c43c4 2018-06-21 stsp
2002 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2003 404c43c4 2018-06-21 stsp switch (ch) {
2004 404c43c4 2018-06-21 stsp case 'c':
2005 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2006 404c43c4 2018-06-21 stsp break;
2007 66bea077 2018-08-02 stsp case 'r':
2008 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2009 66bea077 2018-08-02 stsp if (repo_path == NULL)
2010 66bea077 2018-08-02 stsp err(1, "-r option");
2011 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2012 66bea077 2018-08-02 stsp break;
2013 404c43c4 2018-06-21 stsp default:
2014 2deda0b9 2019-03-07 stsp usage_blame();
2015 404c43c4 2018-06-21 stsp /* NOTREACHED */
2016 404c43c4 2018-06-21 stsp }
2017 404c43c4 2018-06-21 stsp }
2018 404c43c4 2018-06-21 stsp
2019 404c43c4 2018-06-21 stsp argc -= optind;
2020 404c43c4 2018-06-21 stsp argv += optind;
2021 404c43c4 2018-06-21 stsp
2022 a39318fd 2018-08-02 stsp if (argc == 1)
2023 404c43c4 2018-06-21 stsp path = argv[0];
2024 a39318fd 2018-08-02 stsp else
2025 404c43c4 2018-06-21 stsp usage_blame();
2026 404c43c4 2018-06-21 stsp
2027 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2028 66bea077 2018-08-02 stsp if (cwd == NULL) {
2029 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2030 66bea077 2018-08-02 stsp goto done;
2031 66bea077 2018-08-02 stsp }
2032 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2033 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2034 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2035 66bea077 2018-08-02 stsp goto done;
2036 0c06baac 2019-02-05 stsp else
2037 0c06baac 2019-02-05 stsp error = NULL;
2038 0c06baac 2019-02-05 stsp if (worktree) {
2039 0c06baac 2019-02-05 stsp repo_path =
2040 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2041 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2042 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2043 0c06baac 2019-02-05 stsp if (error)
2044 0c06baac 2019-02-05 stsp goto done;
2045 0c06baac 2019-02-05 stsp } else {
2046 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2047 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2048 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2049 0c06baac 2019-02-05 stsp goto done;
2050 0c06baac 2019-02-05 stsp }
2051 66bea077 2018-08-02 stsp }
2052 66bea077 2018-08-02 stsp }
2053 36e2fb66 2019-01-04 stsp
2054 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2055 c02c541e 2019-03-29 stsp if (error != NULL)
2056 36e2fb66 2019-01-04 stsp goto done;
2057 66bea077 2018-08-02 stsp
2058 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2059 c02c541e 2019-03-29 stsp if (error)
2060 404c43c4 2018-06-21 stsp goto done;
2061 404c43c4 2018-06-21 stsp
2062 0c06baac 2019-02-05 stsp if (worktree) {
2063 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2064 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2065 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2066 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2067 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2068 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2069 6efaaa2d 2019-02-05 stsp path) == -1) {
2070 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2071 0c06baac 2019-02-05 stsp goto done;
2072 0c06baac 2019-02-05 stsp }
2073 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2074 0c06baac 2019-02-05 stsp free(p);
2075 0c06baac 2019-02-05 stsp } else {
2076 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2077 0c06baac 2019-02-05 stsp }
2078 0c06baac 2019-02-05 stsp if (error)
2079 66bea077 2018-08-02 stsp goto done;
2080 66bea077 2018-08-02 stsp
2081 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2082 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2083 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2084 404c43c4 2018-06-21 stsp if (error != NULL)
2085 66bea077 2018-08-02 stsp goto done;
2086 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2087 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2088 404c43c4 2018-06-21 stsp if (error != NULL)
2089 66bea077 2018-08-02 stsp goto done;
2090 404c43c4 2018-06-21 stsp } else {
2091 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2092 30837e32 2019-07-25 stsp if (error)
2093 66bea077 2018-08-02 stsp goto done;
2094 404c43c4 2018-06-21 stsp }
2095 404c43c4 2018-06-21 stsp
2096 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2097 404c43c4 2018-06-21 stsp done:
2098 66bea077 2018-08-02 stsp free(in_repo_path);
2099 66bea077 2018-08-02 stsp free(repo_path);
2100 66bea077 2018-08-02 stsp free(cwd);
2101 404c43c4 2018-06-21 stsp free(commit_id);
2102 0c06baac 2019-02-05 stsp if (worktree)
2103 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2104 ad242220 2018-09-08 stsp if (repo) {
2105 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2106 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2107 ad242220 2018-09-08 stsp if (error == NULL)
2108 ad242220 2018-09-08 stsp error = repo_error;
2109 ad242220 2018-09-08 stsp }
2110 404c43c4 2018-06-21 stsp return error;
2111 5de5890b 2018-10-18 stsp }
2112 5de5890b 2018-10-18 stsp
2113 5de5890b 2018-10-18 stsp __dead static void
2114 5de5890b 2018-10-18 stsp usage_tree(void)
2115 5de5890b 2018-10-18 stsp {
2116 c1669e2e 2019-01-09 stsp fprintf(stderr,
2117 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2118 5de5890b 2018-10-18 stsp getprogname());
2119 5de5890b 2018-10-18 stsp exit(1);
2120 5de5890b 2018-10-18 stsp }
2121 5de5890b 2018-10-18 stsp
2122 c1669e2e 2019-01-09 stsp static void
2123 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2124 c1669e2e 2019-01-09 stsp const char *root_path)
2125 c1669e2e 2019-01-09 stsp {
2126 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2127 5de5890b 2018-10-18 stsp
2128 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2129 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2130 c1669e2e 2019-01-09 stsp path++;
2131 c1669e2e 2019-01-09 stsp
2132 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2133 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2134 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2135 c1669e2e 2019-01-09 stsp }
2136 c1669e2e 2019-01-09 stsp
2137 5de5890b 2018-10-18 stsp static const struct got_error *
2138 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2139 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2140 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2141 5de5890b 2018-10-18 stsp {
2142 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2143 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2144 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2145 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2146 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2147 5de5890b 2018-10-18 stsp
2148 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2149 5de5890b 2018-10-18 stsp if (err)
2150 5de5890b 2018-10-18 stsp goto done;
2151 5de5890b 2018-10-18 stsp
2152 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2153 5de5890b 2018-10-18 stsp if (err)
2154 5de5890b 2018-10-18 stsp goto done;
2155 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2156 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2157 5de5890b 2018-10-18 stsp while (te) {
2158 5de5890b 2018-10-18 stsp char *id = NULL;
2159 84453469 2018-11-11 stsp
2160 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2161 84453469 2018-11-11 stsp break;
2162 84453469 2018-11-11 stsp
2163 5de5890b 2018-10-18 stsp if (show_ids) {
2164 5de5890b 2018-10-18 stsp char *id_str;
2165 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2166 5de5890b 2018-10-18 stsp if (err)
2167 5de5890b 2018-10-18 stsp goto done;
2168 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2169 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2170 5de5890b 2018-10-18 stsp free(id_str);
2171 5de5890b 2018-10-18 stsp goto done;
2172 5de5890b 2018-10-18 stsp }
2173 5de5890b 2018-10-18 stsp free(id_str);
2174 5de5890b 2018-10-18 stsp }
2175 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2176 5de5890b 2018-10-18 stsp free(id);
2177 c1669e2e 2019-01-09 stsp
2178 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2179 c1669e2e 2019-01-09 stsp char *child_path;
2180 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2181 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2182 c1669e2e 2019-01-09 stsp te->name) == -1) {
2183 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2184 c1669e2e 2019-01-09 stsp goto done;
2185 c1669e2e 2019-01-09 stsp }
2186 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2187 c1669e2e 2019-01-09 stsp root_path, repo);
2188 c1669e2e 2019-01-09 stsp free(child_path);
2189 c1669e2e 2019-01-09 stsp if (err)
2190 c1669e2e 2019-01-09 stsp goto done;
2191 c1669e2e 2019-01-09 stsp }
2192 c1669e2e 2019-01-09 stsp
2193 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2194 5de5890b 2018-10-18 stsp }
2195 5de5890b 2018-10-18 stsp done:
2196 5de5890b 2018-10-18 stsp if (tree)
2197 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2198 5de5890b 2018-10-18 stsp free(tree_id);
2199 5de5890b 2018-10-18 stsp return err;
2200 404c43c4 2018-06-21 stsp }
2201 404c43c4 2018-06-21 stsp
2202 5de5890b 2018-10-18 stsp static const struct got_error *
2203 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2204 5de5890b 2018-10-18 stsp {
2205 5de5890b 2018-10-18 stsp const struct got_error *error;
2206 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2207 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2208 7a2c19d6 2019-02-05 stsp const char *path;
2209 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2210 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2211 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2212 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2213 5de5890b 2018-10-18 stsp int ch;
2214 5de5890b 2018-10-18 stsp
2215 5de5890b 2018-10-18 stsp #ifndef PROFILE
2216 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2217 0f8d269b 2019-01-04 stsp NULL) == -1)
2218 5de5890b 2018-10-18 stsp err(1, "pledge");
2219 5de5890b 2018-10-18 stsp #endif
2220 5de5890b 2018-10-18 stsp
2221 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2222 5de5890b 2018-10-18 stsp switch (ch) {
2223 5de5890b 2018-10-18 stsp case 'c':
2224 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2225 5de5890b 2018-10-18 stsp break;
2226 5de5890b 2018-10-18 stsp case 'r':
2227 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2228 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2229 5de5890b 2018-10-18 stsp err(1, "-r option");
2230 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2231 5de5890b 2018-10-18 stsp break;
2232 5de5890b 2018-10-18 stsp case 'i':
2233 5de5890b 2018-10-18 stsp show_ids = 1;
2234 5de5890b 2018-10-18 stsp break;
2235 c1669e2e 2019-01-09 stsp case 'R':
2236 c1669e2e 2019-01-09 stsp recurse = 1;
2237 c1669e2e 2019-01-09 stsp break;
2238 5de5890b 2018-10-18 stsp default:
2239 2deda0b9 2019-03-07 stsp usage_tree();
2240 5de5890b 2018-10-18 stsp /* NOTREACHED */
2241 5de5890b 2018-10-18 stsp }
2242 5de5890b 2018-10-18 stsp }
2243 5de5890b 2018-10-18 stsp
2244 5de5890b 2018-10-18 stsp argc -= optind;
2245 5de5890b 2018-10-18 stsp argv += optind;
2246 5de5890b 2018-10-18 stsp
2247 5de5890b 2018-10-18 stsp if (argc == 1)
2248 5de5890b 2018-10-18 stsp path = argv[0];
2249 5de5890b 2018-10-18 stsp else if (argc > 1)
2250 5de5890b 2018-10-18 stsp usage_tree();
2251 5de5890b 2018-10-18 stsp else
2252 7a2c19d6 2019-02-05 stsp path = NULL;
2253 7a2c19d6 2019-02-05 stsp
2254 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2255 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2256 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2257 9bf7a39b 2019-02-05 stsp goto done;
2258 9bf7a39b 2019-02-05 stsp }
2259 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2260 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2261 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2262 7a2c19d6 2019-02-05 stsp goto done;
2263 7a2c19d6 2019-02-05 stsp else
2264 7a2c19d6 2019-02-05 stsp error = NULL;
2265 7a2c19d6 2019-02-05 stsp if (worktree) {
2266 7a2c19d6 2019-02-05 stsp repo_path =
2267 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2268 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2269 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2270 7a2c19d6 2019-02-05 stsp if (error)
2271 7a2c19d6 2019-02-05 stsp goto done;
2272 7a2c19d6 2019-02-05 stsp } else {
2273 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2274 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2275 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2276 7a2c19d6 2019-02-05 stsp goto done;
2277 7a2c19d6 2019-02-05 stsp }
2278 5de5890b 2018-10-18 stsp }
2279 5de5890b 2018-10-18 stsp }
2280 5de5890b 2018-10-18 stsp
2281 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2282 5de5890b 2018-10-18 stsp if (error != NULL)
2283 c02c541e 2019-03-29 stsp goto done;
2284 c02c541e 2019-03-29 stsp
2285 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2286 c02c541e 2019-03-29 stsp if (error)
2287 5de5890b 2018-10-18 stsp goto done;
2288 5de5890b 2018-10-18 stsp
2289 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2290 9bf7a39b 2019-02-05 stsp if (worktree) {
2291 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2292 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2293 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2294 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2295 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2296 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2297 9bf7a39b 2019-02-05 stsp goto done;
2298 9bf7a39b 2019-02-05 stsp }
2299 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2300 9bf7a39b 2019-02-05 stsp free(p);
2301 9bf7a39b 2019-02-05 stsp if (error)
2302 9bf7a39b 2019-02-05 stsp goto done;
2303 9bf7a39b 2019-02-05 stsp } else
2304 9bf7a39b 2019-02-05 stsp path = "/";
2305 9bf7a39b 2019-02-05 stsp }
2306 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2307 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2308 9bf7a39b 2019-02-05 stsp if (error != NULL)
2309 9bf7a39b 2019-02-05 stsp goto done;
2310 9bf7a39b 2019-02-05 stsp }
2311 5de5890b 2018-10-18 stsp
2312 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2313 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2314 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2315 5de5890b 2018-10-18 stsp if (error != NULL)
2316 5de5890b 2018-10-18 stsp goto done;
2317 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2318 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2319 5de5890b 2018-10-18 stsp if (error != NULL)
2320 5de5890b 2018-10-18 stsp goto done;
2321 5de5890b 2018-10-18 stsp } else {
2322 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2323 30837e32 2019-07-25 stsp if (error)
2324 5de5890b 2018-10-18 stsp goto done;
2325 5de5890b 2018-10-18 stsp }
2326 5de5890b 2018-10-18 stsp
2327 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2328 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2329 5de5890b 2018-10-18 stsp done:
2330 5de5890b 2018-10-18 stsp free(in_repo_path);
2331 5de5890b 2018-10-18 stsp free(repo_path);
2332 5de5890b 2018-10-18 stsp free(cwd);
2333 5de5890b 2018-10-18 stsp free(commit_id);
2334 7a2c19d6 2019-02-05 stsp if (worktree)
2335 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2336 5de5890b 2018-10-18 stsp if (repo) {
2337 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2338 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2339 5de5890b 2018-10-18 stsp if (error == NULL)
2340 5de5890b 2018-10-18 stsp error = repo_error;
2341 5de5890b 2018-10-18 stsp }
2342 5de5890b 2018-10-18 stsp return error;
2343 5de5890b 2018-10-18 stsp }
2344 5de5890b 2018-10-18 stsp
2345 6bad629b 2019-02-04 stsp __dead static void
2346 6bad629b 2019-02-04 stsp usage_status(void)
2347 6bad629b 2019-02-04 stsp {
2348 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2349 6bad629b 2019-02-04 stsp exit(1);
2350 6bad629b 2019-02-04 stsp }
2351 5c860e29 2018-03-12 stsp
2352 b72f483a 2019-02-05 stsp static const struct got_error *
2353 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
2354 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2355 6bad629b 2019-02-04 stsp {
2356 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
2357 b72f483a 2019-02-05 stsp return NULL;
2358 6bad629b 2019-02-04 stsp }
2359 5c860e29 2018-03-12 stsp
2360 6bad629b 2019-02-04 stsp static const struct got_error *
2361 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2362 6bad629b 2019-02-04 stsp {
2363 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2364 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2365 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2366 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2367 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2368 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2369 a5edda0a 2019-07-27 stsp int ch;
2370 5c860e29 2018-03-12 stsp
2371 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2372 72ea6654 2019-07-27 stsp
2373 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2374 6bad629b 2019-02-04 stsp switch (ch) {
2375 5c860e29 2018-03-12 stsp default:
2376 2deda0b9 2019-03-07 stsp usage_status();
2377 6bad629b 2019-02-04 stsp /* NOTREACHED */
2378 5c860e29 2018-03-12 stsp }
2379 5c860e29 2018-03-12 stsp }
2380 5c860e29 2018-03-12 stsp
2381 6bad629b 2019-02-04 stsp argc -= optind;
2382 6bad629b 2019-02-04 stsp argv += optind;
2383 5c860e29 2018-03-12 stsp
2384 6bad629b 2019-02-04 stsp #ifndef PROFILE
2385 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2386 6bad629b 2019-02-04 stsp NULL) == -1)
2387 6bad629b 2019-02-04 stsp err(1, "pledge");
2388 f42b1b34 2018-03-12 stsp #endif
2389 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2390 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2391 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2392 927df6b7 2019-02-10 stsp goto done;
2393 927df6b7 2019-02-10 stsp }
2394 927df6b7 2019-02-10 stsp
2395 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2396 927df6b7 2019-02-10 stsp if (error != NULL)
2397 927df6b7 2019-02-10 stsp goto done;
2398 927df6b7 2019-02-10 stsp
2399 a5edda0a 2019-07-27 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2400 a5edda0a 2019-07-27 stsp if (error)
2401 a5edda0a 2019-07-27 stsp goto done;
2402 6bad629b 2019-02-04 stsp
2403 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2404 6bad629b 2019-02-04 stsp if (error != NULL)
2405 6bad629b 2019-02-04 stsp goto done;
2406 6bad629b 2019-02-04 stsp
2407 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2408 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2409 6bad629b 2019-02-04 stsp if (error)
2410 6bad629b 2019-02-04 stsp goto done;
2411 6bad629b 2019-02-04 stsp
2412 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2413 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2414 6bad629b 2019-02-04 stsp done:
2415 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2416 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2417 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2418 927df6b7 2019-02-10 stsp free(cwd);
2419 d0eebce4 2019-03-11 stsp return error;
2420 d0eebce4 2019-03-11 stsp }
2421 d0eebce4 2019-03-11 stsp
2422 d0eebce4 2019-03-11 stsp __dead static void
2423 d0eebce4 2019-03-11 stsp usage_ref(void)
2424 d0eebce4 2019-03-11 stsp {
2425 d0eebce4 2019-03-11 stsp fprintf(stderr,
2426 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2427 d0eebce4 2019-03-11 stsp getprogname());
2428 d0eebce4 2019-03-11 stsp exit(1);
2429 d0eebce4 2019-03-11 stsp }
2430 d0eebce4 2019-03-11 stsp
2431 d0eebce4 2019-03-11 stsp static const struct got_error *
2432 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2433 d0eebce4 2019-03-11 stsp {
2434 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2435 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2436 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2437 d0eebce4 2019-03-11 stsp
2438 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2439 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2440 d0eebce4 2019-03-11 stsp if (err)
2441 d0eebce4 2019-03-11 stsp return err;
2442 d0eebce4 2019-03-11 stsp
2443 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2444 d0eebce4 2019-03-11 stsp char *refstr;
2445 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2446 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2447 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2448 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2449 d0eebce4 2019-03-11 stsp free(refstr);
2450 d0eebce4 2019-03-11 stsp }
2451 d0eebce4 2019-03-11 stsp
2452 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2453 d0eebce4 2019-03-11 stsp return NULL;
2454 d0eebce4 2019-03-11 stsp }
2455 d0eebce4 2019-03-11 stsp
2456 d0eebce4 2019-03-11 stsp static const struct got_error *
2457 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2458 d0eebce4 2019-03-11 stsp {
2459 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2460 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2461 d0eebce4 2019-03-11 stsp
2462 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2463 d0eebce4 2019-03-11 stsp if (err)
2464 d0eebce4 2019-03-11 stsp return err;
2465 d0eebce4 2019-03-11 stsp
2466 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2467 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2468 d0eebce4 2019-03-11 stsp return err;
2469 d0eebce4 2019-03-11 stsp }
2470 d0eebce4 2019-03-11 stsp
2471 d0eebce4 2019-03-11 stsp static const struct got_error *
2472 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2473 d0eebce4 2019-03-11 stsp {
2474 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2475 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2476 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2477 d1644381 2019-07-14 stsp
2478 d1644381 2019-07-14 stsp /*
2479 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2480 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2481 d1644381 2019-07-14 stsp * an unintended typo.
2482 d1644381 2019-07-14 stsp */
2483 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2484 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2485 d0eebce4 2019-03-11 stsp
2486 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2487 dd88155e 2019-06-29 stsp repo);
2488 d83d9d5c 2019-05-13 stsp if (err) {
2489 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2490 d0eebce4 2019-03-11 stsp
2491 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2492 d83d9d5c 2019-05-13 stsp return err;
2493 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2494 d83d9d5c 2019-05-13 stsp if (err)
2495 d83d9d5c 2019-05-13 stsp return err;
2496 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2497 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2498 d83d9d5c 2019-05-13 stsp if (err)
2499 d83d9d5c 2019-05-13 stsp return err;
2500 d83d9d5c 2019-05-13 stsp }
2501 d83d9d5c 2019-05-13 stsp
2502 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2503 d0eebce4 2019-03-11 stsp if (err)
2504 d0eebce4 2019-03-11 stsp goto done;
2505 d0eebce4 2019-03-11 stsp
2506 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2507 d0eebce4 2019-03-11 stsp done:
2508 d0eebce4 2019-03-11 stsp if (ref)
2509 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2510 d0eebce4 2019-03-11 stsp free(id);
2511 d0eebce4 2019-03-11 stsp return err;
2512 d0eebce4 2019-03-11 stsp }
2513 d0eebce4 2019-03-11 stsp
2514 d0eebce4 2019-03-11 stsp static const struct got_error *
2515 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2516 d0eebce4 2019-03-11 stsp {
2517 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2518 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2519 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2520 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2521 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2522 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2523 d0eebce4 2019-03-11 stsp
2524 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2525 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2526 d0eebce4 2019-03-11 stsp switch (ch) {
2527 d0eebce4 2019-03-11 stsp case 'd':
2528 d0eebce4 2019-03-11 stsp delref = optarg;
2529 d0eebce4 2019-03-11 stsp break;
2530 d0eebce4 2019-03-11 stsp case 'r':
2531 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2532 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2533 d0eebce4 2019-03-11 stsp err(1, "-r option");
2534 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2535 d0eebce4 2019-03-11 stsp break;
2536 d0eebce4 2019-03-11 stsp case 'l':
2537 d0eebce4 2019-03-11 stsp do_list = 1;
2538 d0eebce4 2019-03-11 stsp break;
2539 d0eebce4 2019-03-11 stsp default:
2540 d0eebce4 2019-03-11 stsp usage_ref();
2541 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2542 d0eebce4 2019-03-11 stsp }
2543 d0eebce4 2019-03-11 stsp }
2544 d0eebce4 2019-03-11 stsp
2545 d0eebce4 2019-03-11 stsp if (do_list && delref)
2546 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2547 d0eebce4 2019-03-11 stsp
2548 d0eebce4 2019-03-11 stsp argc -= optind;
2549 d0eebce4 2019-03-11 stsp argv += optind;
2550 d0eebce4 2019-03-11 stsp
2551 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2552 d0eebce4 2019-03-11 stsp if (argc > 0)
2553 d0eebce4 2019-03-11 stsp usage_ref();
2554 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2555 d0eebce4 2019-03-11 stsp usage_ref();
2556 d0eebce4 2019-03-11 stsp
2557 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2558 e0b57350 2019-03-12 stsp if (do_list) {
2559 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2560 e0b57350 2019-03-12 stsp NULL) == -1)
2561 e0b57350 2019-03-12 stsp err(1, "pledge");
2562 e0b57350 2019-03-12 stsp } else {
2563 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2564 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2565 e0b57350 2019-03-12 stsp err(1, "pledge");
2566 e0b57350 2019-03-12 stsp }
2567 d0eebce4 2019-03-11 stsp #endif
2568 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2569 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2570 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2571 d0eebce4 2019-03-11 stsp goto done;
2572 d0eebce4 2019-03-11 stsp }
2573 d0eebce4 2019-03-11 stsp
2574 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2575 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2576 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2577 d0eebce4 2019-03-11 stsp goto done;
2578 d0eebce4 2019-03-11 stsp else
2579 d0eebce4 2019-03-11 stsp error = NULL;
2580 d0eebce4 2019-03-11 stsp if (worktree) {
2581 d0eebce4 2019-03-11 stsp repo_path =
2582 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2583 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2584 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2585 d0eebce4 2019-03-11 stsp if (error)
2586 d0eebce4 2019-03-11 stsp goto done;
2587 d0eebce4 2019-03-11 stsp } else {
2588 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2589 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2590 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2591 d0eebce4 2019-03-11 stsp goto done;
2592 d0eebce4 2019-03-11 stsp }
2593 d0eebce4 2019-03-11 stsp }
2594 d0eebce4 2019-03-11 stsp }
2595 d0eebce4 2019-03-11 stsp
2596 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2597 d0eebce4 2019-03-11 stsp if (error != NULL)
2598 d0eebce4 2019-03-11 stsp goto done;
2599 d0eebce4 2019-03-11 stsp
2600 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2601 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2602 c02c541e 2019-03-29 stsp if (error)
2603 c02c541e 2019-03-29 stsp goto done;
2604 c02c541e 2019-03-29 stsp
2605 d0eebce4 2019-03-11 stsp if (do_list)
2606 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2607 d0eebce4 2019-03-11 stsp else if (delref)
2608 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2609 d0eebce4 2019-03-11 stsp else
2610 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2611 4e759de4 2019-06-26 stsp done:
2612 4e759de4 2019-06-26 stsp if (repo)
2613 4e759de4 2019-06-26 stsp got_repo_close(repo);
2614 4e759de4 2019-06-26 stsp if (worktree)
2615 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2616 4e759de4 2019-06-26 stsp free(cwd);
2617 4e759de4 2019-06-26 stsp free(repo_path);
2618 4e759de4 2019-06-26 stsp return error;
2619 4e759de4 2019-06-26 stsp }
2620 4e759de4 2019-06-26 stsp
2621 4e759de4 2019-06-26 stsp __dead static void
2622 4e759de4 2019-06-26 stsp usage_branch(void)
2623 4e759de4 2019-06-26 stsp {
2624 4e759de4 2019-06-26 stsp fprintf(stderr,
2625 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2626 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2627 4e759de4 2019-06-26 stsp exit(1);
2628 4e759de4 2019-06-26 stsp }
2629 4e759de4 2019-06-26 stsp
2630 4e759de4 2019-06-26 stsp static const struct got_error *
2631 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2632 4e759de4 2019-06-26 stsp {
2633 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2634 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2635 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2636 4e759de4 2019-06-26 stsp
2637 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2638 ba882ee3 2019-07-11 stsp
2639 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2640 4e759de4 2019-06-26 stsp if (err)
2641 4e759de4 2019-06-26 stsp return err;
2642 4e759de4 2019-06-26 stsp
2643 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2644 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2645 4e759de4 2019-06-26 stsp char *refstr;
2646 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2647 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2648 4e759de4 2019-06-26 stsp continue;
2649 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2650 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2651 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2652 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2653 ba882ee3 2019-07-11 stsp if (err)
2654 ba882ee3 2019-07-11 stsp return err;
2655 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2656 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2657 ba882ee3 2019-07-11 stsp marker = "* ";
2658 ba882ee3 2019-07-11 stsp else
2659 ba882ee3 2019-07-11 stsp marker = "~ ";
2660 ba882ee3 2019-07-11 stsp free(id);
2661 ba882ee3 2019-07-11 stsp }
2662 4e759de4 2019-06-26 stsp refname += 11;
2663 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2664 4e759de4 2019-06-26 stsp if (refstr == NULL)
2665 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2666 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2667 4e759de4 2019-06-26 stsp free(refstr);
2668 4e759de4 2019-06-26 stsp }
2669 4e759de4 2019-06-26 stsp
2670 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2671 4e759de4 2019-06-26 stsp return NULL;
2672 4e759de4 2019-06-26 stsp }
2673 4e759de4 2019-06-26 stsp
2674 4e759de4 2019-06-26 stsp static const struct got_error *
2675 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2676 4e759de4 2019-06-26 stsp {
2677 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2678 4e759de4 2019-06-26 stsp struct got_reference *ref;
2679 4e759de4 2019-06-26 stsp char *refname;
2680 4e759de4 2019-06-26 stsp
2681 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2682 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2683 4e759de4 2019-06-26 stsp
2684 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2685 4e759de4 2019-06-26 stsp if (err)
2686 4e759de4 2019-06-26 stsp goto done;
2687 4e759de4 2019-06-26 stsp
2688 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2689 4e759de4 2019-06-26 stsp got_ref_close(ref);
2690 4e759de4 2019-06-26 stsp done:
2691 4e759de4 2019-06-26 stsp free(refname);
2692 4e759de4 2019-06-26 stsp return err;
2693 4e759de4 2019-06-26 stsp }
2694 4e759de4 2019-06-26 stsp
2695 4e759de4 2019-06-26 stsp static const struct got_error *
2696 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2697 4e759de4 2019-06-26 stsp const char *base_branch)
2698 4e759de4 2019-06-26 stsp {
2699 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2700 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2701 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2702 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2703 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2704 d3f84d51 2019-07-11 stsp
2705 d3f84d51 2019-07-11 stsp /*
2706 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2707 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2708 d3f84d51 2019-07-11 stsp * an unintended typo.
2709 d3f84d51 2019-07-11 stsp */
2710 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2711 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2712 4e759de4 2019-06-26 stsp
2713 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2714 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2715 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2716 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2717 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2718 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2719 4e759de4 2019-06-26 stsp
2720 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2721 4e759de4 2019-06-26 stsp if (err)
2722 4e759de4 2019-06-26 stsp goto done;
2723 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2724 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2725 4e759de4 2019-06-26 stsp if (err)
2726 4e759de4 2019-06-26 stsp goto done;
2727 4e759de4 2019-06-26 stsp
2728 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2729 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2730 4e759de4 2019-06-26 stsp goto done;
2731 4e759de4 2019-06-26 stsp }
2732 4e759de4 2019-06-26 stsp
2733 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2734 4e759de4 2019-06-26 stsp if (err == NULL) {
2735 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2736 4e759de4 2019-06-26 stsp goto done;
2737 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2738 4e759de4 2019-06-26 stsp goto done;
2739 4e759de4 2019-06-26 stsp
2740 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2741 4e759de4 2019-06-26 stsp if (err)
2742 4e759de4 2019-06-26 stsp goto done;
2743 4e759de4 2019-06-26 stsp
2744 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2745 d0eebce4 2019-03-11 stsp done:
2746 4e759de4 2019-06-26 stsp if (ref)
2747 4e759de4 2019-06-26 stsp got_ref_close(ref);
2748 4e759de4 2019-06-26 stsp free(id);
2749 4e759de4 2019-06-26 stsp free(base_refname);
2750 4e759de4 2019-06-26 stsp free(refname);
2751 4e759de4 2019-06-26 stsp return err;
2752 4e759de4 2019-06-26 stsp }
2753 4e759de4 2019-06-26 stsp
2754 4e759de4 2019-06-26 stsp static const struct got_error *
2755 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2756 4e759de4 2019-06-26 stsp {
2757 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2758 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2759 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2760 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2761 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2762 4e759de4 2019-06-26 stsp const char *delref = NULL;
2763 4e759de4 2019-06-26 stsp
2764 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2765 4e759de4 2019-06-26 stsp switch (ch) {
2766 4e759de4 2019-06-26 stsp case 'd':
2767 4e759de4 2019-06-26 stsp delref = optarg;
2768 4e759de4 2019-06-26 stsp break;
2769 4e759de4 2019-06-26 stsp case 'r':
2770 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2771 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2772 4e759de4 2019-06-26 stsp err(1, "-r option");
2773 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2774 4e759de4 2019-06-26 stsp break;
2775 4e759de4 2019-06-26 stsp case 'l':
2776 4e759de4 2019-06-26 stsp do_list = 1;
2777 4e759de4 2019-06-26 stsp break;
2778 4e759de4 2019-06-26 stsp default:
2779 4e759de4 2019-06-26 stsp usage_branch();
2780 4e759de4 2019-06-26 stsp /* NOTREACHED */
2781 4e759de4 2019-06-26 stsp }
2782 4e759de4 2019-06-26 stsp }
2783 4e759de4 2019-06-26 stsp
2784 4e759de4 2019-06-26 stsp if (do_list && delref)
2785 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2786 4e759de4 2019-06-26 stsp
2787 4e759de4 2019-06-26 stsp argc -= optind;
2788 4e759de4 2019-06-26 stsp argv += optind;
2789 4e759de4 2019-06-26 stsp
2790 4e759de4 2019-06-26 stsp if (do_list || delref) {
2791 4e759de4 2019-06-26 stsp if (argc > 0)
2792 4e759de4 2019-06-26 stsp usage_branch();
2793 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2794 4e759de4 2019-06-26 stsp usage_branch();
2795 4e759de4 2019-06-26 stsp
2796 4e759de4 2019-06-26 stsp #ifndef PROFILE
2797 4e759de4 2019-06-26 stsp if (do_list) {
2798 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2799 4e759de4 2019-06-26 stsp NULL) == -1)
2800 4e759de4 2019-06-26 stsp err(1, "pledge");
2801 4e759de4 2019-06-26 stsp } else {
2802 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2803 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2804 4e759de4 2019-06-26 stsp err(1, "pledge");
2805 4e759de4 2019-06-26 stsp }
2806 4e759de4 2019-06-26 stsp #endif
2807 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2808 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2809 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2810 4e759de4 2019-06-26 stsp goto done;
2811 4e759de4 2019-06-26 stsp }
2812 4e759de4 2019-06-26 stsp
2813 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2814 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2815 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2816 4e759de4 2019-06-26 stsp goto done;
2817 4e759de4 2019-06-26 stsp else
2818 4e759de4 2019-06-26 stsp error = NULL;
2819 4e759de4 2019-06-26 stsp if (worktree) {
2820 4e759de4 2019-06-26 stsp repo_path =
2821 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2822 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2823 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2824 4e759de4 2019-06-26 stsp if (error)
2825 4e759de4 2019-06-26 stsp goto done;
2826 4e759de4 2019-06-26 stsp } else {
2827 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2828 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2829 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2830 4e759de4 2019-06-26 stsp goto done;
2831 4e759de4 2019-06-26 stsp }
2832 4e759de4 2019-06-26 stsp }
2833 4e759de4 2019-06-26 stsp }
2834 4e759de4 2019-06-26 stsp
2835 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2836 4e759de4 2019-06-26 stsp if (error != NULL)
2837 4e759de4 2019-06-26 stsp goto done;
2838 4e759de4 2019-06-26 stsp
2839 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2840 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2841 4e759de4 2019-06-26 stsp if (error)
2842 4e759de4 2019-06-26 stsp goto done;
2843 4e759de4 2019-06-26 stsp
2844 4e759de4 2019-06-26 stsp if (do_list)
2845 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2846 4e759de4 2019-06-26 stsp else if (delref)
2847 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2848 4e759de4 2019-06-26 stsp else {
2849 4e759de4 2019-06-26 stsp const char *base_branch;
2850 4e759de4 2019-06-26 stsp if (argc == 1) {
2851 4e759de4 2019-06-26 stsp base_branch = worktree ?
2852 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2853 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2854 4e759de4 2019-06-26 stsp } else
2855 4e759de4 2019-06-26 stsp base_branch = argv[1];
2856 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2857 4e759de4 2019-06-26 stsp }
2858 4e759de4 2019-06-26 stsp done:
2859 d0eebce4 2019-03-11 stsp if (repo)
2860 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2861 d0eebce4 2019-03-11 stsp if (worktree)
2862 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2863 d0eebce4 2019-03-11 stsp free(cwd);
2864 d0eebce4 2019-03-11 stsp free(repo_path);
2865 d00136be 2019-03-26 stsp return error;
2866 d00136be 2019-03-26 stsp }
2867 d00136be 2019-03-26 stsp
2868 d00136be 2019-03-26 stsp __dead static void
2869 d00136be 2019-03-26 stsp usage_add(void)
2870 d00136be 2019-03-26 stsp {
2871 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2872 d00136be 2019-03-26 stsp exit(1);
2873 d00136be 2019-03-26 stsp }
2874 d00136be 2019-03-26 stsp
2875 d00136be 2019-03-26 stsp static const struct got_error *
2876 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2877 d00136be 2019-03-26 stsp {
2878 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2879 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2880 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2881 1dd54920 2019-05-11 stsp char *cwd = NULL;
2882 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2883 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2884 723c305c 2019-05-11 jcs int ch, x;
2885 1dd54920 2019-05-11 stsp
2886 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2887 d00136be 2019-03-26 stsp
2888 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2889 d00136be 2019-03-26 stsp switch (ch) {
2890 d00136be 2019-03-26 stsp default:
2891 d00136be 2019-03-26 stsp usage_add();
2892 d00136be 2019-03-26 stsp /* NOTREACHED */
2893 d00136be 2019-03-26 stsp }
2894 d00136be 2019-03-26 stsp }
2895 d00136be 2019-03-26 stsp
2896 d00136be 2019-03-26 stsp argc -= optind;
2897 d00136be 2019-03-26 stsp argv += optind;
2898 d00136be 2019-03-26 stsp
2899 43012d58 2019-07-14 stsp #ifndef PROFILE
2900 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2901 43012d58 2019-07-14 stsp NULL) == -1)
2902 43012d58 2019-07-14 stsp err(1, "pledge");
2903 43012d58 2019-07-14 stsp #endif
2904 723c305c 2019-05-11 jcs if (argc < 1)
2905 d00136be 2019-03-26 stsp usage_add();
2906 d00136be 2019-03-26 stsp
2907 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2908 d00136be 2019-03-26 stsp if (cwd == NULL) {
2909 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2910 d00136be 2019-03-26 stsp goto done;
2911 d00136be 2019-03-26 stsp }
2912 723c305c 2019-05-11 jcs
2913 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2914 d00136be 2019-03-26 stsp if (error)
2915 d00136be 2019-03-26 stsp goto done;
2916 d00136be 2019-03-26 stsp
2917 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2918 031a5338 2019-03-26 stsp if (error != NULL)
2919 031a5338 2019-03-26 stsp goto done;
2920 031a5338 2019-03-26 stsp
2921 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2922 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2923 d00136be 2019-03-26 stsp if (error)
2924 d00136be 2019-03-26 stsp goto done;
2925 d00136be 2019-03-26 stsp
2926 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2927 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2928 723c305c 2019-05-11 jcs if (path == NULL) {
2929 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2930 723c305c 2019-05-11 jcs goto done;
2931 723c305c 2019-05-11 jcs }
2932 723c305c 2019-05-11 jcs
2933 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2934 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2935 1dd54920 2019-05-11 stsp if (error) {
2936 1dd54920 2019-05-11 stsp free(path);
2937 723c305c 2019-05-11 jcs goto done;
2938 1dd54920 2019-05-11 stsp }
2939 723c305c 2019-05-11 jcs }
2940 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2941 1dd54920 2019-05-11 stsp NULL, repo);
2942 d00136be 2019-03-26 stsp done:
2943 031a5338 2019-03-26 stsp if (repo)
2944 031a5338 2019-03-26 stsp got_repo_close(repo);
2945 d00136be 2019-03-26 stsp if (worktree)
2946 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2947 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2948 1dd54920 2019-05-11 stsp free((char *)pe->path);
2949 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2950 2ec1f75b 2019-03-26 stsp free(cwd);
2951 2ec1f75b 2019-03-26 stsp return error;
2952 2ec1f75b 2019-03-26 stsp }
2953 2ec1f75b 2019-03-26 stsp
2954 2ec1f75b 2019-03-26 stsp __dead static void
2955 648e4ef7 2019-07-09 stsp usage_remove(void)
2956 2ec1f75b 2019-03-26 stsp {
2957 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2958 2ec1f75b 2019-03-26 stsp exit(1);
2959 2ec1f75b 2019-03-26 stsp }
2960 2ec1f75b 2019-03-26 stsp
2961 2ec1f75b 2019-03-26 stsp static const struct got_error *
2962 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2963 2ec1f75b 2019-03-26 stsp {
2964 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2965 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2966 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2967 17ed4618 2019-06-02 stsp char *cwd = NULL;
2968 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2969 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2970 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2971 2ec1f75b 2019-03-26 stsp
2972 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2973 17ed4618 2019-06-02 stsp
2974 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2975 2ec1f75b 2019-03-26 stsp switch (ch) {
2976 2ec1f75b 2019-03-26 stsp case 'f':
2977 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2978 2ec1f75b 2019-03-26 stsp break;
2979 2ec1f75b 2019-03-26 stsp default:
2980 2ec1f75b 2019-03-26 stsp usage_add();
2981 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2982 2ec1f75b 2019-03-26 stsp }
2983 2ec1f75b 2019-03-26 stsp }
2984 2ec1f75b 2019-03-26 stsp
2985 2ec1f75b 2019-03-26 stsp argc -= optind;
2986 2ec1f75b 2019-03-26 stsp argv += optind;
2987 2ec1f75b 2019-03-26 stsp
2988 43012d58 2019-07-14 stsp #ifndef PROFILE
2989 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2990 43012d58 2019-07-14 stsp NULL) == -1)
2991 43012d58 2019-07-14 stsp err(1, "pledge");
2992 43012d58 2019-07-14 stsp #endif
2993 17ed4618 2019-06-02 stsp if (argc < 1)
2994 648e4ef7 2019-07-09 stsp usage_remove();
2995 2ec1f75b 2019-03-26 stsp
2996 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2997 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2998 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2999 2ec1f75b 2019-03-26 stsp goto done;
3000 2ec1f75b 2019-03-26 stsp }
3001 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3002 2ec1f75b 2019-03-26 stsp if (error)
3003 2ec1f75b 2019-03-26 stsp goto done;
3004 2ec1f75b 2019-03-26 stsp
3005 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3006 2af4a041 2019-05-11 jcs if (error)
3007 2ec1f75b 2019-03-26 stsp goto done;
3008 2ec1f75b 2019-03-26 stsp
3009 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3010 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3011 2ec1f75b 2019-03-26 stsp if (error)
3012 2ec1f75b 2019-03-26 stsp goto done;
3013 2ec1f75b 2019-03-26 stsp
3014 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
3015 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
3016 17ed4618 2019-06-02 stsp if (path == NULL) {
3017 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
3018 17ed4618 2019-06-02 stsp goto done;
3019 17ed4618 2019-06-02 stsp }
3020 17ed4618 2019-06-02 stsp
3021 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
3022 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3023 17ed4618 2019-06-02 stsp if (error) {
3024 17ed4618 2019-06-02 stsp free(path);
3025 17ed4618 2019-06-02 stsp goto done;
3026 17ed4618 2019-06-02 stsp }
3027 17ed4618 2019-06-02 stsp }
3028 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3029 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3030 a129376b 2019-03-28 stsp if (error)
3031 a129376b 2019-03-28 stsp goto done;
3032 a129376b 2019-03-28 stsp done:
3033 a129376b 2019-03-28 stsp if (repo)
3034 a129376b 2019-03-28 stsp got_repo_close(repo);
3035 a129376b 2019-03-28 stsp if (worktree)
3036 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3037 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3038 17ed4618 2019-06-02 stsp free((char *)pe->path);
3039 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3040 a129376b 2019-03-28 stsp free(cwd);
3041 a129376b 2019-03-28 stsp return error;
3042 a129376b 2019-03-28 stsp }
3043 a129376b 2019-03-28 stsp
3044 a129376b 2019-03-28 stsp __dead static void
3045 a129376b 2019-03-28 stsp usage_revert(void)
3046 a129376b 2019-03-28 stsp {
3047 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3048 a129376b 2019-03-28 stsp exit(1);
3049 a129376b 2019-03-28 stsp }
3050 a129376b 2019-03-28 stsp
3051 1ee397ad 2019-07-12 stsp static const struct got_error *
3052 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3053 a129376b 2019-03-28 stsp {
3054 a129376b 2019-03-28 stsp while (path[0] == '/')
3055 a129376b 2019-03-28 stsp path++;
3056 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3057 1ee397ad 2019-07-12 stsp return NULL;
3058 a129376b 2019-03-28 stsp }
3059 a129376b 2019-03-28 stsp
3060 a129376b 2019-03-28 stsp static const struct got_error *
3061 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3062 a129376b 2019-03-28 stsp {
3063 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3064 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3065 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3066 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3067 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3068 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3069 e20a8b6f 2019-06-04 stsp int ch, i;
3070 a129376b 2019-03-28 stsp
3071 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3072 e20a8b6f 2019-06-04 stsp
3073 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3074 a129376b 2019-03-28 stsp switch (ch) {
3075 a129376b 2019-03-28 stsp default:
3076 a129376b 2019-03-28 stsp usage_revert();
3077 a129376b 2019-03-28 stsp /* NOTREACHED */
3078 a129376b 2019-03-28 stsp }
3079 a129376b 2019-03-28 stsp }
3080 a129376b 2019-03-28 stsp
3081 a129376b 2019-03-28 stsp argc -= optind;
3082 a129376b 2019-03-28 stsp argv += optind;
3083 a129376b 2019-03-28 stsp
3084 43012d58 2019-07-14 stsp #ifndef PROFILE
3085 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3086 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3087 43012d58 2019-07-14 stsp err(1, "pledge");
3088 43012d58 2019-07-14 stsp #endif
3089 e20a8b6f 2019-06-04 stsp if (argc < 1)
3090 a129376b 2019-03-28 stsp usage_revert();
3091 a129376b 2019-03-28 stsp
3092 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
3093 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
3094 e20a8b6f 2019-06-04 stsp if (path == NULL) {
3095 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
3096 e20a8b6f 2019-06-04 stsp goto done;
3097 e20a8b6f 2019-06-04 stsp }
3098 e20a8b6f 2019-06-04 stsp
3099 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
3100 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3101 e20a8b6f 2019-06-04 stsp if (error) {
3102 e20a8b6f 2019-06-04 stsp free(path);
3103 e20a8b6f 2019-06-04 stsp goto done;
3104 e20a8b6f 2019-06-04 stsp }
3105 a129376b 2019-03-28 stsp }
3106 a129376b 2019-03-28 stsp
3107 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3108 a129376b 2019-03-28 stsp if (cwd == NULL) {
3109 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3110 a129376b 2019-03-28 stsp goto done;
3111 a129376b 2019-03-28 stsp }
3112 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3113 2ec1f75b 2019-03-26 stsp if (error)
3114 2ec1f75b 2019-03-26 stsp goto done;
3115 a129376b 2019-03-28 stsp
3116 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3117 a129376b 2019-03-28 stsp if (error != NULL)
3118 a129376b 2019-03-28 stsp goto done;
3119 a129376b 2019-03-28 stsp
3120 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3121 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3122 a129376b 2019-03-28 stsp if (error)
3123 a129376b 2019-03-28 stsp goto done;
3124 a129376b 2019-03-28 stsp
3125 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3126 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3127 a129376b 2019-03-28 stsp if (error)
3128 a129376b 2019-03-28 stsp goto done;
3129 2ec1f75b 2019-03-26 stsp done:
3130 2ec1f75b 2019-03-26 stsp if (repo)
3131 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3132 2ec1f75b 2019-03-26 stsp if (worktree)
3133 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3134 2ec1f75b 2019-03-26 stsp free(path);
3135 d00136be 2019-03-26 stsp free(cwd);
3136 6bad629b 2019-02-04 stsp return error;
3137 c4296144 2019-05-09 stsp }
3138 c4296144 2019-05-09 stsp
3139 c4296144 2019-05-09 stsp __dead static void
3140 c4296144 2019-05-09 stsp usage_commit(void)
3141 c4296144 2019-05-09 stsp {
3142 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3143 5c1e53bc 2019-07-28 stsp getprogname());
3144 c4296144 2019-05-09 stsp exit(1);
3145 33ad4cbe 2019-05-12 jcs }
3146 33ad4cbe 2019-05-12 jcs
3147 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3148 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3149 e2ba3d07 2019-05-13 stsp const char *editor;
3150 e0870e44 2019-05-13 stsp const char *worktree_path;
3151 76d98825 2019-06-03 stsp const char *branch_name;
3152 314a6357 2019-05-13 stsp const char *repo_path;
3153 e0870e44 2019-05-13 stsp char *logmsg_path;
3154 e2ba3d07 2019-05-13 stsp
3155 e2ba3d07 2019-05-13 stsp };
3156 e0870e44 2019-05-13 stsp
3157 33ad4cbe 2019-05-12 jcs static const struct got_error *
3158 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3159 33ad4cbe 2019-05-12 jcs void *arg)
3160 33ad4cbe 2019-05-12 jcs {
3161 76d98825 2019-06-03 stsp char *initial_content = NULL;
3162 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3163 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3164 e0870e44 2019-05-13 stsp char *template = NULL;
3165 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3166 3ce1b845 2019-07-15 stsp int fd;
3167 33ad4cbe 2019-05-12 jcs size_t len;
3168 33ad4cbe 2019-05-12 jcs
3169 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3170 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3171 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3172 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3173 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3174 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3175 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3176 33ad4cbe 2019-05-12 jcs return NULL;
3177 33ad4cbe 2019-05-12 jcs }
3178 33ad4cbe 2019-05-12 jcs
3179 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3180 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3181 76d98825 2019-06-03 stsp
3182 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3183 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3184 76d98825 2019-06-03 stsp a->branch_name) == -1)
3185 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3186 e0870e44 2019-05-13 stsp
3187 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3188 793c30b5 2019-05-13 stsp if (err)
3189 e0870e44 2019-05-13 stsp goto done;
3190 33ad4cbe 2019-05-12 jcs
3191 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3192 33ad4cbe 2019-05-12 jcs
3193 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3194 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3195 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3196 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3197 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3198 33ad4cbe 2019-05-12 jcs }
3199 33ad4cbe 2019-05-12 jcs close(fd);
3200 33ad4cbe 2019-05-12 jcs
3201 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3202 33ad4cbe 2019-05-12 jcs done:
3203 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3204 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3205 76d98825 2019-06-03 stsp free(initial_content);
3206 e0870e44 2019-05-13 stsp free(template);
3207 314a6357 2019-05-13 stsp
3208 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3209 3ce1b845 2019-07-15 stsp if (err == NULL) {
3210 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3211 3ce1b845 2019-07-15 stsp if (err) {
3212 3ce1b845 2019-07-15 stsp free(*logmsg);
3213 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3214 3ce1b845 2019-07-15 stsp }
3215 3ce1b845 2019-07-15 stsp }
3216 33ad4cbe 2019-05-12 jcs return err;
3217 6bad629b 2019-02-04 stsp }
3218 c4296144 2019-05-09 stsp
3219 c4296144 2019-05-09 stsp static const struct got_error *
3220 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3221 c4296144 2019-05-09 stsp {
3222 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3223 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3224 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3225 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3226 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3227 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3228 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3229 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3230 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3231 a698f62e 2019-07-25 stsp int ch, rebase_in_progress;
3232 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3233 5c1e53bc 2019-07-28 stsp
3234 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3235 c4296144 2019-05-09 stsp
3236 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3237 c4296144 2019-05-09 stsp switch (ch) {
3238 c4296144 2019-05-09 stsp case 'm':
3239 c4296144 2019-05-09 stsp logmsg = optarg;
3240 c4296144 2019-05-09 stsp break;
3241 c4296144 2019-05-09 stsp default:
3242 c4296144 2019-05-09 stsp usage_commit();
3243 c4296144 2019-05-09 stsp /* NOTREACHED */
3244 c4296144 2019-05-09 stsp }
3245 c4296144 2019-05-09 stsp }
3246 c4296144 2019-05-09 stsp
3247 c4296144 2019-05-09 stsp argc -= optind;
3248 c4296144 2019-05-09 stsp argv += optind;
3249 c4296144 2019-05-09 stsp
3250 43012d58 2019-07-14 stsp #ifndef PROFILE
3251 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3252 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3253 43012d58 2019-07-14 stsp err(1, "pledge");
3254 43012d58 2019-07-14 stsp #endif
3255 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3256 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3257 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3258 35bd8fed 2019-05-09 stsp goto done;
3259 35bd8fed 2019-05-09 stsp }
3260 c4296144 2019-05-09 stsp
3261 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3262 c4296144 2019-05-09 stsp if (cwd == NULL) {
3263 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3264 c4296144 2019-05-09 stsp goto done;
3265 c4296144 2019-05-09 stsp }
3266 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3267 7d5807f4 2019-07-11 stsp if (error)
3268 7d5807f4 2019-07-11 stsp goto done;
3269 7d5807f4 2019-07-11 stsp
3270 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3271 c4296144 2019-05-09 stsp if (error)
3272 a698f62e 2019-07-25 stsp goto done;
3273 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3274 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3275 c4296144 2019-05-09 stsp goto done;
3276 a698f62e 2019-07-25 stsp }
3277 5c1e53bc 2019-07-28 stsp
3278 5c1e53bc 2019-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3279 5c1e53bc 2019-07-28 stsp if (error)
3280 5c1e53bc 2019-07-28 stsp goto done;
3281 c4296144 2019-05-09 stsp
3282 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3283 c4296144 2019-05-09 stsp if (error != NULL)
3284 c4296144 2019-05-09 stsp goto done;
3285 c4296144 2019-05-09 stsp
3286 314a6357 2019-05-13 stsp /*
3287 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3288 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3289 314a6357 2019-05-13 stsp */
3290 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3291 314a6357 2019-05-13 stsp error = get_editor(&editor);
3292 314a6357 2019-05-13 stsp else
3293 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3294 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3295 c4296144 2019-05-09 stsp if (error)
3296 c4296144 2019-05-09 stsp goto done;
3297 c4296144 2019-05-09 stsp
3298 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3299 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3300 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3301 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3302 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3303 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
3304 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3305 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
3306 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3307 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3308 5c1e53bc 2019-07-28 stsp error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3309 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3310 e0870e44 2019-05-13 stsp if (error) {
3311 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3312 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3313 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3314 c4296144 2019-05-09 stsp goto done;
3315 e0870e44 2019-05-13 stsp }
3316 c4296144 2019-05-09 stsp
3317 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3318 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3319 e0870e44 2019-05-13 stsp
3320 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3321 c4296144 2019-05-09 stsp if (error)
3322 c4296144 2019-05-09 stsp goto done;
3323 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3324 c4296144 2019-05-09 stsp done:
3325 c4296144 2019-05-09 stsp if (repo)
3326 c4296144 2019-05-09 stsp got_repo_close(repo);
3327 c4296144 2019-05-09 stsp if (worktree)
3328 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3329 c4296144 2019-05-09 stsp free(cwd);
3330 c4296144 2019-05-09 stsp free(id_str);
3331 e2ba3d07 2019-05-13 stsp free(editor);
3332 234035bc 2019-06-01 stsp return error;
3333 234035bc 2019-06-01 stsp }
3334 234035bc 2019-06-01 stsp
3335 234035bc 2019-06-01 stsp __dead static void
3336 234035bc 2019-06-01 stsp usage_cherrypick(void)
3337 234035bc 2019-06-01 stsp {
3338 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3339 234035bc 2019-06-01 stsp exit(1);
3340 234035bc 2019-06-01 stsp }
3341 234035bc 2019-06-01 stsp
3342 234035bc 2019-06-01 stsp static const struct got_error *
3343 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3344 234035bc 2019-06-01 stsp {
3345 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3346 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3347 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3348 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3349 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3350 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3351 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3352 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3353 234035bc 2019-06-01 stsp int ch, did_something = 0;
3354 234035bc 2019-06-01 stsp
3355 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3356 234035bc 2019-06-01 stsp switch (ch) {
3357 234035bc 2019-06-01 stsp default:
3358 234035bc 2019-06-01 stsp usage_cherrypick();
3359 234035bc 2019-06-01 stsp /* NOTREACHED */
3360 234035bc 2019-06-01 stsp }
3361 234035bc 2019-06-01 stsp }
3362 234035bc 2019-06-01 stsp
3363 234035bc 2019-06-01 stsp argc -= optind;
3364 234035bc 2019-06-01 stsp argv += optind;
3365 234035bc 2019-06-01 stsp
3366 43012d58 2019-07-14 stsp #ifndef PROFILE
3367 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3368 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3369 43012d58 2019-07-14 stsp err(1, "pledge");
3370 43012d58 2019-07-14 stsp #endif
3371 234035bc 2019-06-01 stsp if (argc != 1)
3372 234035bc 2019-06-01 stsp usage_cherrypick();
3373 234035bc 2019-06-01 stsp
3374 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3375 234035bc 2019-06-01 stsp if (cwd == NULL) {
3376 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3377 234035bc 2019-06-01 stsp goto done;
3378 234035bc 2019-06-01 stsp }
3379 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3380 234035bc 2019-06-01 stsp if (error)
3381 234035bc 2019-06-01 stsp goto done;
3382 234035bc 2019-06-01 stsp
3383 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3384 234035bc 2019-06-01 stsp if (error != NULL)
3385 234035bc 2019-06-01 stsp goto done;
3386 234035bc 2019-06-01 stsp
3387 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3388 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3389 234035bc 2019-06-01 stsp if (error)
3390 234035bc 2019-06-01 stsp goto done;
3391 234035bc 2019-06-01 stsp
3392 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3393 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3394 234035bc 2019-06-01 stsp if (error != NULL) {
3395 234035bc 2019-06-01 stsp struct got_reference *ref;
3396 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3397 234035bc 2019-06-01 stsp goto done;
3398 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3399 234035bc 2019-06-01 stsp if (error != NULL)
3400 234035bc 2019-06-01 stsp goto done;
3401 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3402 234035bc 2019-06-01 stsp got_ref_close(ref);
3403 234035bc 2019-06-01 stsp if (error != NULL)
3404 234035bc 2019-06-01 stsp goto done;
3405 234035bc 2019-06-01 stsp }
3406 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3407 234035bc 2019-06-01 stsp if (error)
3408 234035bc 2019-06-01 stsp goto done;
3409 234035bc 2019-06-01 stsp
3410 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3411 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3412 234035bc 2019-06-01 stsp if (error != NULL)
3413 234035bc 2019-06-01 stsp goto done;
3414 234035bc 2019-06-01 stsp
3415 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3416 234035bc 2019-06-01 stsp if (error) {
3417 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3418 234035bc 2019-06-01 stsp goto done;
3419 234035bc 2019-06-01 stsp error = NULL;
3420 234035bc 2019-06-01 stsp } else {
3421 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3422 234035bc 2019-06-01 stsp goto done;
3423 234035bc 2019-06-01 stsp }
3424 234035bc 2019-06-01 stsp
3425 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3426 234035bc 2019-06-01 stsp if (error)
3427 234035bc 2019-06-01 stsp goto done;
3428 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3429 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3430 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3431 03415a1a 2019-06-02 stsp NULL);
3432 234035bc 2019-06-01 stsp if (error != NULL)
3433 234035bc 2019-06-01 stsp goto done;
3434 234035bc 2019-06-01 stsp
3435 234035bc 2019-06-01 stsp if (did_something)
3436 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3437 234035bc 2019-06-01 stsp done:
3438 234035bc 2019-06-01 stsp if (commit)
3439 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3440 234035bc 2019-06-01 stsp free(commit_id_str);
3441 234035bc 2019-06-01 stsp if (head_ref)
3442 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3443 234035bc 2019-06-01 stsp if (worktree)
3444 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3445 234035bc 2019-06-01 stsp if (repo)
3446 234035bc 2019-06-01 stsp got_repo_close(repo);
3447 c4296144 2019-05-09 stsp return error;
3448 c4296144 2019-05-09 stsp }
3449 5ef14e63 2019-06-02 stsp
3450 5ef14e63 2019-06-02 stsp __dead static void
3451 5ef14e63 2019-06-02 stsp usage_backout(void)
3452 5ef14e63 2019-06-02 stsp {
3453 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3454 5ef14e63 2019-06-02 stsp exit(1);
3455 5ef14e63 2019-06-02 stsp }
3456 5ef14e63 2019-06-02 stsp
3457 5ef14e63 2019-06-02 stsp static const struct got_error *
3458 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3459 5ef14e63 2019-06-02 stsp {
3460 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3461 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3462 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3463 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3464 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3465 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3466 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3467 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3468 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3469 5ef14e63 2019-06-02 stsp
3470 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3471 5ef14e63 2019-06-02 stsp switch (ch) {
3472 5ef14e63 2019-06-02 stsp default:
3473 5ef14e63 2019-06-02 stsp usage_backout();
3474 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3475 5ef14e63 2019-06-02 stsp }
3476 5ef14e63 2019-06-02 stsp }
3477 5ef14e63 2019-06-02 stsp
3478 5ef14e63 2019-06-02 stsp argc -= optind;
3479 5ef14e63 2019-06-02 stsp argv += optind;
3480 5ef14e63 2019-06-02 stsp
3481 43012d58 2019-07-14 stsp #ifndef PROFILE
3482 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3483 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3484 43012d58 2019-07-14 stsp err(1, "pledge");
3485 43012d58 2019-07-14 stsp #endif
3486 5ef14e63 2019-06-02 stsp if (argc != 1)
3487 5ef14e63 2019-06-02 stsp usage_backout();
3488 5ef14e63 2019-06-02 stsp
3489 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3490 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3491 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3492 5ef14e63 2019-06-02 stsp goto done;
3493 5ef14e63 2019-06-02 stsp }
3494 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3495 5ef14e63 2019-06-02 stsp if (error)
3496 5ef14e63 2019-06-02 stsp goto done;
3497 5ef14e63 2019-06-02 stsp
3498 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3499 5ef14e63 2019-06-02 stsp if (error != NULL)
3500 5ef14e63 2019-06-02 stsp goto done;
3501 5ef14e63 2019-06-02 stsp
3502 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3503 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3504 5ef14e63 2019-06-02 stsp if (error)
3505 5ef14e63 2019-06-02 stsp goto done;
3506 5ef14e63 2019-06-02 stsp
3507 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3508 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3509 5ef14e63 2019-06-02 stsp if (error != NULL) {
3510 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3511 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3512 5ef14e63 2019-06-02 stsp goto done;
3513 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3514 5ef14e63 2019-06-02 stsp if (error != NULL)
3515 5ef14e63 2019-06-02 stsp goto done;
3516 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3517 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3518 5ef14e63 2019-06-02 stsp if (error != NULL)
3519 5ef14e63 2019-06-02 stsp goto done;
3520 5ef14e63 2019-06-02 stsp }
3521 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3522 5ef14e63 2019-06-02 stsp if (error)
3523 5ef14e63 2019-06-02 stsp goto done;
3524 5ef14e63 2019-06-02 stsp
3525 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3526 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3527 5ef14e63 2019-06-02 stsp if (error != NULL)
3528 5ef14e63 2019-06-02 stsp goto done;
3529 5ef14e63 2019-06-02 stsp
3530 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3531 5ef14e63 2019-06-02 stsp if (error)
3532 5ef14e63 2019-06-02 stsp goto done;
3533 5ef14e63 2019-06-02 stsp
3534 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3535 5ef14e63 2019-06-02 stsp if (error)
3536 5ef14e63 2019-06-02 stsp goto done;
3537 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3538 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3539 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3540 5ef14e63 2019-06-02 stsp goto done;
3541 5ef14e63 2019-06-02 stsp }
3542 5ef14e63 2019-06-02 stsp
3543 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3544 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3545 5ef14e63 2019-06-02 stsp if (error != NULL)
3546 5ef14e63 2019-06-02 stsp goto done;
3547 5ef14e63 2019-06-02 stsp
3548 5ef14e63 2019-06-02 stsp if (did_something)
3549 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3550 5ef14e63 2019-06-02 stsp done:
3551 5ef14e63 2019-06-02 stsp if (commit)
3552 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3553 5ef14e63 2019-06-02 stsp free(commit_id_str);
3554 5ef14e63 2019-06-02 stsp if (head_ref)
3555 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3556 818c7501 2019-07-11 stsp if (worktree)
3557 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3558 818c7501 2019-07-11 stsp if (repo)
3559 818c7501 2019-07-11 stsp got_repo_close(repo);
3560 818c7501 2019-07-11 stsp return error;
3561 818c7501 2019-07-11 stsp }
3562 818c7501 2019-07-11 stsp
3563 818c7501 2019-07-11 stsp __dead static void
3564 818c7501 2019-07-11 stsp usage_rebase(void)
3565 818c7501 2019-07-11 stsp {
3566 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3567 af54c8f8 2019-07-11 stsp getprogname());
3568 818c7501 2019-07-11 stsp exit(1);
3569 0ebf8283 2019-07-24 stsp }
3570 0ebf8283 2019-07-24 stsp
3571 0ebf8283 2019-07-24 stsp void
3572 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3573 0ebf8283 2019-07-24 stsp {
3574 0ebf8283 2019-07-24 stsp char *nl;
3575 0ebf8283 2019-07-24 stsp size_t len;
3576 0ebf8283 2019-07-24 stsp
3577 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3578 0ebf8283 2019-07-24 stsp if (len > limit)
3579 0ebf8283 2019-07-24 stsp len = limit;
3580 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3581 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3582 0ebf8283 2019-07-24 stsp if (nl)
3583 0ebf8283 2019-07-24 stsp *nl = '\0';
3584 0ebf8283 2019-07-24 stsp }
3585 0ebf8283 2019-07-24 stsp
3586 0ebf8283 2019-07-24 stsp static const struct got_error *
3587 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3588 0ebf8283 2019-07-24 stsp {
3589 0ebf8283 2019-07-24 stsp const char *logmsg0 = NULL;
3590 0ebf8283 2019-07-24 stsp
3591 0ebf8283 2019-07-24 stsp logmsg0 = got_object_commit_get_logmsg(commit);
3592 0ebf8283 2019-07-24 stsp
3593 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)logmsg0[0]))
3594 0ebf8283 2019-07-24 stsp logmsg0++;
3595 0ebf8283 2019-07-24 stsp
3596 0ebf8283 2019-07-24 stsp *logmsg = strdup(logmsg0);
3597 0ebf8283 2019-07-24 stsp if (*logmsg == NULL)
3598 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
3599 0ebf8283 2019-07-24 stsp
3600 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3601 0ebf8283 2019-07-24 stsp return NULL;
3602 818c7501 2019-07-11 stsp }
3603 818c7501 2019-07-11 stsp
3604 818c7501 2019-07-11 stsp static const struct got_error *
3605 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3606 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3607 818c7501 2019-07-11 stsp {
3608 818c7501 2019-07-11 stsp const struct got_error *err;
3609 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3610 818c7501 2019-07-11 stsp
3611 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3612 818c7501 2019-07-11 stsp if (err)
3613 818c7501 2019-07-11 stsp goto done;
3614 818c7501 2019-07-11 stsp
3615 ff0d2220 2019-07-11 stsp if (new_id) {
3616 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3617 ff0d2220 2019-07-11 stsp if (err)
3618 ff0d2220 2019-07-11 stsp goto done;
3619 ff0d2220 2019-07-11 stsp }
3620 818c7501 2019-07-11 stsp
3621 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3622 ff0d2220 2019-07-11 stsp if (new_id_str)
3623 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3624 0ebf8283 2019-07-24 stsp
3625 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3626 0ebf8283 2019-07-24 stsp if (err)
3627 0ebf8283 2019-07-24 stsp goto done;
3628 0ebf8283 2019-07-24 stsp
3629 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3630 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3631 818c7501 2019-07-11 stsp done:
3632 818c7501 2019-07-11 stsp free(old_id_str);
3633 818c7501 2019-07-11 stsp free(new_id_str);
3634 818c7501 2019-07-11 stsp return err;
3635 818c7501 2019-07-11 stsp }
3636 818c7501 2019-07-11 stsp
3637 1ee397ad 2019-07-12 stsp static const struct got_error *
3638 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3639 818c7501 2019-07-11 stsp {
3640 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3641 818c7501 2019-07-11 stsp
3642 818c7501 2019-07-11 stsp while (path[0] == '/')
3643 818c7501 2019-07-11 stsp path++;
3644 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3645 818c7501 2019-07-11 stsp
3646 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3647 1ee397ad 2019-07-12 stsp return NULL;
3648 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3649 818c7501 2019-07-11 stsp *rebase_status = status;
3650 1ee397ad 2019-07-12 stsp return NULL;
3651 818c7501 2019-07-11 stsp }
3652 818c7501 2019-07-11 stsp
3653 818c7501 2019-07-11 stsp static const struct got_error *
3654 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3655 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
3656 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
3657 818c7501 2019-07-11 stsp {
3658 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3659 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
3660 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3661 818c7501 2019-07-11 stsp }
3662 818c7501 2019-07-11 stsp
3663 818c7501 2019-07-11 stsp static const struct got_error *
3664 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3665 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3666 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
3667 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
3668 ff0d2220 2019-07-11 stsp {
3669 ff0d2220 2019-07-11 stsp const struct got_error *error;
3670 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3671 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3672 ff0d2220 2019-07-11 stsp
3673 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3674 ff0d2220 2019-07-11 stsp if (error)
3675 ff0d2220 2019-07-11 stsp return error;
3676 ff0d2220 2019-07-11 stsp
3677 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3678 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
3679 ff0d2220 2019-07-11 stsp if (error) {
3680 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3681 ff0d2220 2019-07-11 stsp goto done;
3682 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3683 ff0d2220 2019-07-11 stsp } else {
3684 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3685 ff0d2220 2019-07-11 stsp free(new_commit_id);
3686 ff0d2220 2019-07-11 stsp }
3687 ff0d2220 2019-07-11 stsp done:
3688 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3689 ff0d2220 2019-07-11 stsp return error;
3690 64c6d990 2019-07-11 stsp }
3691 64c6d990 2019-07-11 stsp
3692 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3693 64c6d990 2019-07-11 stsp const char *path_prefix;
3694 64c6d990 2019-07-11 stsp size_t len;
3695 8ca9bd68 2019-07-25 stsp int errcode;
3696 64c6d990 2019-07-11 stsp };
3697 64c6d990 2019-07-11 stsp
3698 64c6d990 2019-07-11 stsp static const struct got_error *
3699 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3700 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3701 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3702 64c6d990 2019-07-11 stsp struct got_repository *repo)
3703 64c6d990 2019-07-11 stsp {
3704 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3705 64c6d990 2019-07-11 stsp
3706 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3707 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3708 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
3709 64c6d990 2019-07-11 stsp
3710 64c6d990 2019-07-11 stsp return NULL;
3711 64c6d990 2019-07-11 stsp }
3712 64c6d990 2019-07-11 stsp
3713 64c6d990 2019-07-11 stsp static const struct got_error *
3714 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
3715 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3716 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
3717 64c6d990 2019-07-11 stsp {
3718 64c6d990 2019-07-11 stsp const struct got_error *err;
3719 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3720 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3721 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3722 64c6d990 2019-07-11 stsp
3723 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3724 64c6d990 2019-07-11 stsp return NULL;
3725 64c6d990 2019-07-11 stsp
3726 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3727 64c6d990 2019-07-11 stsp if (err)
3728 64c6d990 2019-07-11 stsp goto done;
3729 64c6d990 2019-07-11 stsp
3730 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3731 64c6d990 2019-07-11 stsp if (err)
3732 64c6d990 2019-07-11 stsp goto done;
3733 64c6d990 2019-07-11 stsp
3734 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3735 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3736 64c6d990 2019-07-11 stsp if (err)
3737 64c6d990 2019-07-11 stsp goto done;
3738 64c6d990 2019-07-11 stsp
3739 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3740 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3741 64c6d990 2019-07-11 stsp if (err)
3742 64c6d990 2019-07-11 stsp goto done;
3743 64c6d990 2019-07-11 stsp
3744 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3745 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
3746 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
3747 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
3748 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
3749 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3750 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
3751 64c6d990 2019-07-11 stsp done:
3752 64c6d990 2019-07-11 stsp if (tree1)
3753 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3754 64c6d990 2019-07-11 stsp if (tree2)
3755 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3756 64c6d990 2019-07-11 stsp if (commit)
3757 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3758 64c6d990 2019-07-11 stsp if (parent_commit)
3759 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3760 64c6d990 2019-07-11 stsp return err;
3761 ff0d2220 2019-07-11 stsp }
3762 ff0d2220 2019-07-11 stsp
3763 ff0d2220 2019-07-11 stsp static const struct got_error *
3764 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
3765 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
3766 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3767 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
3768 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
3769 af61c510 2019-07-19 stsp {
3770 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
3771 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
3772 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
3773 af61c510 2019-07-19 stsp struct got_object_qid *qid;
3774 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
3775 af61c510 2019-07-19 stsp
3776 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3777 af61c510 2019-07-19 stsp if (err)
3778 af61c510 2019-07-19 stsp return err;
3779 af61c510 2019-07-19 stsp
3780 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3781 af61c510 2019-07-19 stsp if (err)
3782 af61c510 2019-07-19 stsp goto done;
3783 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3784 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
3785 af61c510 2019-07-19 stsp if (err) {
3786 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
3787 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
3788 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
3789 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
3790 af61c510 2019-07-19 stsp "been reached?!?");
3791 af61c510 2019-07-19 stsp goto done;
3792 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3793 af61c510 2019-07-19 stsp goto done;
3794 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
3795 af61c510 2019-07-19 stsp if (err)
3796 af61c510 2019-07-19 stsp goto done;
3797 af61c510 2019-07-19 stsp } else {
3798 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
3799 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
3800 af61c510 2019-07-19 stsp if (err)
3801 af61c510 2019-07-19 stsp goto done;
3802 af61c510 2019-07-19 stsp
3803 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
3804 af61c510 2019-07-19 stsp if (err)
3805 af61c510 2019-07-19 stsp goto done;
3806 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3807 af61c510 2019-07-19 stsp commit_id = parent_id;
3808 af61c510 2019-07-19 stsp }
3809 af61c510 2019-07-19 stsp }
3810 af61c510 2019-07-19 stsp done:
3811 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
3812 af61c510 2019-07-19 stsp return err;
3813 af61c510 2019-07-19 stsp }
3814 af61c510 2019-07-19 stsp
3815 af61c510 2019-07-19 stsp static const struct got_error *
3816 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3817 818c7501 2019-07-11 stsp {
3818 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3819 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3820 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3821 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
3822 818c7501 2019-07-11 stsp char *cwd = NULL;
3823 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3824 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3825 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3826 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3827 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3828 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3829 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3830 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3831 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3832 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3833 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3834 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3835 818c7501 2019-07-11 stsp
3836 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3837 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3838 818c7501 2019-07-11 stsp
3839 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3840 818c7501 2019-07-11 stsp switch (ch) {
3841 818c7501 2019-07-11 stsp case 'a':
3842 818c7501 2019-07-11 stsp abort_rebase = 1;
3843 818c7501 2019-07-11 stsp break;
3844 818c7501 2019-07-11 stsp case 'c':
3845 818c7501 2019-07-11 stsp continue_rebase = 1;
3846 818c7501 2019-07-11 stsp break;
3847 818c7501 2019-07-11 stsp default:
3848 818c7501 2019-07-11 stsp usage_rebase();
3849 818c7501 2019-07-11 stsp /* NOTREACHED */
3850 818c7501 2019-07-11 stsp }
3851 818c7501 2019-07-11 stsp }
3852 818c7501 2019-07-11 stsp
3853 818c7501 2019-07-11 stsp argc -= optind;
3854 818c7501 2019-07-11 stsp argv += optind;
3855 818c7501 2019-07-11 stsp
3856 43012d58 2019-07-14 stsp #ifndef PROFILE
3857 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3858 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3859 43012d58 2019-07-14 stsp err(1, "pledge");
3860 43012d58 2019-07-14 stsp #endif
3861 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3862 818c7501 2019-07-11 stsp usage_rebase();
3863 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3864 818c7501 2019-07-11 stsp if (argc != 0)
3865 818c7501 2019-07-11 stsp usage_rebase();
3866 818c7501 2019-07-11 stsp } else if (argc != 1)
3867 818c7501 2019-07-11 stsp usage_rebase();
3868 818c7501 2019-07-11 stsp
3869 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3870 818c7501 2019-07-11 stsp if (cwd == NULL) {
3871 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3872 818c7501 2019-07-11 stsp goto done;
3873 818c7501 2019-07-11 stsp }
3874 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3875 818c7501 2019-07-11 stsp if (error)
3876 818c7501 2019-07-11 stsp goto done;
3877 818c7501 2019-07-11 stsp
3878 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3879 818c7501 2019-07-11 stsp if (error != NULL)
3880 818c7501 2019-07-11 stsp goto done;
3881 818c7501 2019-07-11 stsp
3882 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3883 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3884 818c7501 2019-07-11 stsp if (error)
3885 818c7501 2019-07-11 stsp goto done;
3886 818c7501 2019-07-11 stsp
3887 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3888 818c7501 2019-07-11 stsp if (error)
3889 818c7501 2019-07-11 stsp goto done;
3890 818c7501 2019-07-11 stsp
3891 f6794adc 2019-07-23 stsp if (abort_rebase) {
3892 818c7501 2019-07-11 stsp int did_something;
3893 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3894 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3895 f6794adc 2019-07-23 stsp goto done;
3896 f6794adc 2019-07-23 stsp }
3897 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3898 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3899 3e3a69f1 2019-07-25 stsp worktree, repo);
3900 818c7501 2019-07-11 stsp if (error)
3901 818c7501 2019-07-11 stsp goto done;
3902 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3903 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3904 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
3905 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3906 818c7501 2019-07-11 stsp if (error)
3907 818c7501 2019-07-11 stsp goto done;
3908 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3909 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3910 818c7501 2019-07-11 stsp }
3911 818c7501 2019-07-11 stsp
3912 818c7501 2019-07-11 stsp if (continue_rebase) {
3913 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3914 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3915 f6794adc 2019-07-23 stsp goto done;
3916 f6794adc 2019-07-23 stsp }
3917 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3918 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3919 3e3a69f1 2019-07-25 stsp worktree, repo);
3920 818c7501 2019-07-11 stsp if (error)
3921 818c7501 2019-07-11 stsp goto done;
3922 818c7501 2019-07-11 stsp
3923 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3924 01757395 2019-07-12 stsp resume_commit_id, repo);
3925 818c7501 2019-07-11 stsp if (error)
3926 818c7501 2019-07-11 stsp goto done;
3927 818c7501 2019-07-11 stsp
3928 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3929 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3930 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3931 818c7501 2019-07-11 stsp goto done;
3932 818c7501 2019-07-11 stsp }
3933 818c7501 2019-07-11 stsp } else {
3934 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3935 818c7501 2019-07-11 stsp if (error != NULL)
3936 818c7501 2019-07-11 stsp goto done;
3937 ff0d2220 2019-07-11 stsp }
3938 818c7501 2019-07-11 stsp
3939 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3940 ff0d2220 2019-07-11 stsp if (error)
3941 ff0d2220 2019-07-11 stsp goto done;
3942 ff0d2220 2019-07-11 stsp
3943 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3944 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
3945 a51a74b3 2019-07-27 stsp
3946 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
3947 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3948 a51a74b3 2019-07-27 stsp base_commit_id, branch_head_commit_id, repo);
3949 818c7501 2019-07-11 stsp if (error)
3950 818c7501 2019-07-11 stsp goto done;
3951 818c7501 2019-07-11 stsp if (yca_id == NULL) {
3952 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3953 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3954 818c7501 2019-07-11 stsp "with work tree's branch");
3955 818c7501 2019-07-11 stsp goto done;
3956 818c7501 2019-07-11 stsp }
3957 818c7501 2019-07-11 stsp
3958 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
3959 a51a74b3 2019-07-27 stsp if (error) {
3960 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
3961 a51a74b3 2019-07-27 stsp goto done;
3962 a51a74b3 2019-07-27 stsp error = NULL;
3963 a51a74b3 2019-07-27 stsp } else {
3964 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3965 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
3966 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
3967 a51a74b3 2019-07-27 stsp goto done;
3968 a51a74b3 2019-07-27 stsp }
3969 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3970 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
3971 818c7501 2019-07-11 stsp if (error)
3972 818c7501 2019-07-11 stsp goto done;
3973 818c7501 2019-07-11 stsp }
3974 818c7501 2019-07-11 stsp
3975 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3976 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3977 818c7501 2019-07-11 stsp if (error)
3978 818c7501 2019-07-11 stsp goto done;
3979 818c7501 2019-07-11 stsp
3980 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3981 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
3982 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
3983 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
3984 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
3985 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3986 818c7501 2019-07-11 stsp commit = NULL;
3987 818c7501 2019-07-11 stsp if (error)
3988 818c7501 2019-07-11 stsp goto done;
3989 64c6d990 2019-07-11 stsp
3990 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
3991 ff0d2220 2019-07-11 stsp if (continue_rebase)
3992 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
3993 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
3994 ff0d2220 2019-07-11 stsp else
3995 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
3996 818c7501 2019-07-11 stsp goto done;
3997 818c7501 2019-07-11 stsp }
3998 818c7501 2019-07-11 stsp
3999 818c7501 2019-07-11 stsp pid = NULL;
4000 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4001 818c7501 2019-07-11 stsp commit_id = qid->id;
4002 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4003 818c7501 2019-07-11 stsp pid = qid;
4004 818c7501 2019-07-11 stsp
4005 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4006 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4007 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4008 818c7501 2019-07-11 stsp if (error)
4009 818c7501 2019-07-11 stsp goto done;
4010 818c7501 2019-07-11 stsp
4011 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4012 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4013 818c7501 2019-07-11 stsp break;
4014 01757395 2019-07-12 stsp }
4015 818c7501 2019-07-11 stsp
4016 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4017 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4018 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4019 818c7501 2019-07-11 stsp if (error)
4020 818c7501 2019-07-11 stsp goto done;
4021 818c7501 2019-07-11 stsp }
4022 818c7501 2019-07-11 stsp
4023 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4024 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4025 818c7501 2019-07-11 stsp if (error)
4026 818c7501 2019-07-11 stsp goto done;
4027 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4028 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4029 818c7501 2019-07-11 stsp } else
4030 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4031 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4032 818c7501 2019-07-11 stsp done:
4033 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4034 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4035 818c7501 2019-07-11 stsp free(resume_commit_id);
4036 818c7501 2019-07-11 stsp free(yca_id);
4037 818c7501 2019-07-11 stsp if (commit)
4038 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4039 818c7501 2019-07-11 stsp if (branch)
4040 818c7501 2019-07-11 stsp got_ref_close(branch);
4041 818c7501 2019-07-11 stsp if (new_base_branch)
4042 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4043 818c7501 2019-07-11 stsp if (tmp_branch)
4044 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4045 5ef14e63 2019-06-02 stsp if (worktree)
4046 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4047 5ef14e63 2019-06-02 stsp if (repo)
4048 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4049 5ef14e63 2019-06-02 stsp return error;
4050 0ebf8283 2019-07-24 stsp }
4051 0ebf8283 2019-07-24 stsp
4052 0ebf8283 2019-07-24 stsp __dead static void
4053 0ebf8283 2019-07-24 stsp usage_histedit(void)
4054 0ebf8283 2019-07-24 stsp {
4055 0ebf8283 2019-07-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4056 0ebf8283 2019-07-24 stsp getprogname());
4057 0ebf8283 2019-07-24 stsp exit(1);
4058 0ebf8283 2019-07-24 stsp }
4059 0ebf8283 2019-07-24 stsp
4060 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4061 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4062 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4063 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4064 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4065 0ebf8283 2019-07-24 stsp
4066 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4067 0ebf8283 2019-07-24 stsp unsigned char code;
4068 0ebf8283 2019-07-24 stsp const char *name;
4069 0ebf8283 2019-07-24 stsp const char *desc;
4070 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4071 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4072 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4073 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4074 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4075 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4076 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4077 0ebf8283 2019-07-24 stsp };
4078 0ebf8283 2019-07-24 stsp
4079 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4080 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4081 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4082 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4083 0ebf8283 2019-07-24 stsp char *logmsg;
4084 0ebf8283 2019-07-24 stsp };
4085 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4086 0ebf8283 2019-07-24 stsp
4087 0ebf8283 2019-07-24 stsp static const struct got_error *
4088 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4089 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4090 0ebf8283 2019-07-24 stsp {
4091 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4092 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4093 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4094 0ebf8283 2019-07-24 stsp size_t n;
4095 0ebf8283 2019-07-24 stsp
4096 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4097 0ebf8283 2019-07-24 stsp if (err)
4098 0ebf8283 2019-07-24 stsp goto done;
4099 0ebf8283 2019-07-24 stsp
4100 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4101 0ebf8283 2019-07-24 stsp if (err)
4102 0ebf8283 2019-07-24 stsp goto done;
4103 0ebf8283 2019-07-24 stsp
4104 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4105 0ebf8283 2019-07-24 stsp if (err)
4106 0ebf8283 2019-07-24 stsp goto done;
4107 0ebf8283 2019-07-24 stsp
4108 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4109 0ebf8283 2019-07-24 stsp if (n < 0)
4110 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4111 0ebf8283 2019-07-24 stsp done:
4112 0ebf8283 2019-07-24 stsp if (commit)
4113 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4114 0ebf8283 2019-07-24 stsp free(id_str);
4115 0ebf8283 2019-07-24 stsp free(logmsg);
4116 0ebf8283 2019-07-24 stsp return err;
4117 0ebf8283 2019-07-24 stsp }
4118 0ebf8283 2019-07-24 stsp
4119 0ebf8283 2019-07-24 stsp static const struct got_error *
4120 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4121 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4122 0ebf8283 2019-07-24 stsp {
4123 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4124 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4125 0ebf8283 2019-07-24 stsp
4126 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4127 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4128 0ebf8283 2019-07-24 stsp
4129 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4130 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4131 0ebf8283 2019-07-24 stsp f, repo);
4132 0ebf8283 2019-07-24 stsp if (err)
4133 0ebf8283 2019-07-24 stsp break;
4134 0ebf8283 2019-07-24 stsp }
4135 0ebf8283 2019-07-24 stsp
4136 0ebf8283 2019-07-24 stsp return err;
4137 0ebf8283 2019-07-24 stsp }
4138 0ebf8283 2019-07-24 stsp
4139 0ebf8283 2019-07-24 stsp static const struct got_error *
4140 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4141 0ebf8283 2019-07-24 stsp {
4142 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4143 0ebf8283 2019-07-24 stsp int n, i;
4144 0ebf8283 2019-07-24 stsp
4145 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4146 0ebf8283 2019-07-24 stsp if (n < 0)
4147 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4148 0ebf8283 2019-07-24 stsp
4149 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4150 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4151 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4152 0ebf8283 2019-07-24 stsp cmd->desc);
4153 0ebf8283 2019-07-24 stsp if (n < 0) {
4154 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4155 0ebf8283 2019-07-24 stsp break;
4156 0ebf8283 2019-07-24 stsp }
4157 0ebf8283 2019-07-24 stsp }
4158 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4159 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4160 0ebf8283 2019-07-24 stsp if (n < 0)
4161 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4162 0ebf8283 2019-07-24 stsp return err;
4163 0ebf8283 2019-07-24 stsp }
4164 0ebf8283 2019-07-24 stsp
4165 0ebf8283 2019-07-24 stsp static const struct got_error *
4166 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4167 0ebf8283 2019-07-24 stsp {
4168 0ebf8283 2019-07-24 stsp static char msg[42];
4169 0ebf8283 2019-07-24 stsp int ret;
4170 0ebf8283 2019-07-24 stsp
4171 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4172 0ebf8283 2019-07-24 stsp lineno);
4173 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4174 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4175 0ebf8283 2019-07-24 stsp
4176 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4177 0ebf8283 2019-07-24 stsp }
4178 0ebf8283 2019-07-24 stsp
4179 0ebf8283 2019-07-24 stsp static const struct got_error *
4180 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4181 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4182 0ebf8283 2019-07-24 stsp {
4183 0ebf8283 2019-07-24 stsp const struct got_error *err;
4184 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4185 0ebf8283 2019-07-24 stsp char *id_str;
4186 0ebf8283 2019-07-24 stsp
4187 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4188 0ebf8283 2019-07-24 stsp if (err)
4189 0ebf8283 2019-07-24 stsp return err;
4190 0ebf8283 2019-07-24 stsp
4191 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4192 0ebf8283 2019-07-24 stsp if (err)
4193 0ebf8283 2019-07-24 stsp goto done;
4194 0ebf8283 2019-07-24 stsp
4195 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4196 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4197 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(folded_commit)) == -1) {
4198 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4199 0ebf8283 2019-07-24 stsp goto done;
4200 0ebf8283 2019-07-24 stsp }
4201 0ebf8283 2019-07-24 stsp done:
4202 0ebf8283 2019-07-24 stsp if (folded_commit)
4203 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4204 0ebf8283 2019-07-24 stsp free(id_str);
4205 0ebf8283 2019-07-24 stsp return err;
4206 0ebf8283 2019-07-24 stsp }
4207 0ebf8283 2019-07-24 stsp
4208 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4209 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4210 0ebf8283 2019-07-24 stsp {
4211 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4212 0ebf8283 2019-07-24 stsp
4213 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4214 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4215 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4216 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4217 3f9de99f 2019-07-24 stsp folded = prev;
4218 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4219 0ebf8283 2019-07-24 stsp }
4220 0ebf8283 2019-07-24 stsp
4221 0ebf8283 2019-07-24 stsp return folded;
4222 0ebf8283 2019-07-24 stsp }
4223 0ebf8283 2019-07-24 stsp
4224 0ebf8283 2019-07-24 stsp static const struct got_error *
4225 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4226 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4227 0ebf8283 2019-07-24 stsp {
4228 0ebf8283 2019-07-24 stsp char *logmsg_path = NULL, *id_str = NULL;
4229 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4230 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4231 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4232 0ebf8283 2019-07-24 stsp int fd;
4233 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4234 0ebf8283 2019-07-24 stsp
4235 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4236 0ebf8283 2019-07-24 stsp if (err)
4237 0ebf8283 2019-07-24 stsp return err;
4238 0ebf8283 2019-07-24 stsp
4239 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4240 0ebf8283 2019-07-24 stsp if (folded) {
4241 0ebf8283 2019-07-24 stsp while (folded != hle) {
4242 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4243 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4244 3f9de99f 2019-07-24 stsp continue;
4245 3f9de99f 2019-07-24 stsp }
4246 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4247 0ebf8283 2019-07-24 stsp logmsg, repo);
4248 0ebf8283 2019-07-24 stsp if (err)
4249 0ebf8283 2019-07-24 stsp goto done;
4250 0ebf8283 2019-07-24 stsp free(logmsg);
4251 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4252 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4253 0ebf8283 2019-07-24 stsp }
4254 0ebf8283 2019-07-24 stsp }
4255 0ebf8283 2019-07-24 stsp
4256 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4257 0ebf8283 2019-07-24 stsp if (err)
4258 0ebf8283 2019-07-24 stsp goto done;
4259 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4260 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4261 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", id_str,
4262 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(commit)) == -1) {
4263 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4264 0ebf8283 2019-07-24 stsp goto done;
4265 0ebf8283 2019-07-24 stsp }
4266 0ebf8283 2019-07-24 stsp free(logmsg);
4267 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4268 0ebf8283 2019-07-24 stsp
4269 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4270 0ebf8283 2019-07-24 stsp if (err)
4271 0ebf8283 2019-07-24 stsp goto done;
4272 0ebf8283 2019-07-24 stsp
4273 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4274 0ebf8283 2019-07-24 stsp if (err)
4275 0ebf8283 2019-07-24 stsp goto done;
4276 0ebf8283 2019-07-24 stsp
4277 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4278 0ebf8283 2019-07-24 stsp close(fd);
4279 0ebf8283 2019-07-24 stsp
4280 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4281 0ebf8283 2019-07-24 stsp if (err)
4282 0ebf8283 2019-07-24 stsp goto done;
4283 0ebf8283 2019-07-24 stsp
4284 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4285 0ebf8283 2019-07-24 stsp if (err) {
4286 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4287 0ebf8283 2019-07-24 stsp goto done;
4288 0ebf8283 2019-07-24 stsp err = NULL;
4289 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4290 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL)
4291 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4292 0ebf8283 2019-07-24 stsp }
4293 0ebf8283 2019-07-24 stsp done:
4294 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4295 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4296 0ebf8283 2019-07-24 stsp free(logmsg_path);
4297 0ebf8283 2019-07-24 stsp free(logmsg);
4298 0ebf8283 2019-07-24 stsp free(editor);
4299 0ebf8283 2019-07-24 stsp if (commit)
4300 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4301 0ebf8283 2019-07-24 stsp return err;
4302 5ef14e63 2019-06-02 stsp }
4303 0ebf8283 2019-07-24 stsp
4304 0ebf8283 2019-07-24 stsp static const struct got_error *
4305 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4306 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4307 0ebf8283 2019-07-24 stsp {
4308 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4309 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4310 0ebf8283 2019-07-24 stsp size_t size;
4311 0ebf8283 2019-07-24 stsp ssize_t len;
4312 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4313 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4314 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4315 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4316 0ebf8283 2019-07-24 stsp
4317 0ebf8283 2019-07-24 stsp for (;;) {
4318 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4319 0ebf8283 2019-07-24 stsp if (len == -1) {
4320 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4321 0ebf8283 2019-07-24 stsp if (feof(f))
4322 0ebf8283 2019-07-24 stsp break;
4323 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4324 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4325 0ebf8283 2019-07-24 stsp break;
4326 0ebf8283 2019-07-24 stsp }
4327 0ebf8283 2019-07-24 stsp lineno++;
4328 0ebf8283 2019-07-24 stsp p = line;
4329 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4330 0ebf8283 2019-07-24 stsp p++;
4331 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4332 0ebf8283 2019-07-24 stsp free(line);
4333 0ebf8283 2019-07-24 stsp line = NULL;
4334 0ebf8283 2019-07-24 stsp continue;
4335 0ebf8283 2019-07-24 stsp }
4336 0ebf8283 2019-07-24 stsp cmd = NULL;
4337 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4338 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4339 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4340 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4341 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4342 0ebf8283 2019-07-24 stsp break;
4343 0ebf8283 2019-07-24 stsp }
4344 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4345 0ebf8283 2019-07-24 stsp p++;
4346 0ebf8283 2019-07-24 stsp break;
4347 0ebf8283 2019-07-24 stsp }
4348 0ebf8283 2019-07-24 stsp }
4349 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4350 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4351 0ebf8283 2019-07-24 stsp break;
4352 0ebf8283 2019-07-24 stsp }
4353 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4354 0ebf8283 2019-07-24 stsp p++;
4355 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4356 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4357 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4358 0ebf8283 2019-07-24 stsp break;
4359 0ebf8283 2019-07-24 stsp }
4360 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4361 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4362 0ebf8283 2019-07-24 stsp if (err)
4363 0ebf8283 2019-07-24 stsp break;
4364 0ebf8283 2019-07-24 stsp } else {
4365 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4366 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4367 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4368 0ebf8283 2019-07-24 stsp break;
4369 0ebf8283 2019-07-24 stsp }
4370 0ebf8283 2019-07-24 stsp }
4371 0ebf8283 2019-07-24 stsp free(line);
4372 0ebf8283 2019-07-24 stsp line = NULL;
4373 0ebf8283 2019-07-24 stsp continue;
4374 0ebf8283 2019-07-24 stsp } else {
4375 0ebf8283 2019-07-24 stsp end = p;
4376 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4377 0ebf8283 2019-07-24 stsp end++;
4378 0ebf8283 2019-07-24 stsp *end = '\0';
4379 0ebf8283 2019-07-24 stsp
4380 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4381 0ebf8283 2019-07-24 stsp if (err) {
4382 0ebf8283 2019-07-24 stsp /* override error code */
4383 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4384 0ebf8283 2019-07-24 stsp break;
4385 0ebf8283 2019-07-24 stsp }
4386 0ebf8283 2019-07-24 stsp }
4387 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4388 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4389 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4390 0ebf8283 2019-07-24 stsp break;
4391 0ebf8283 2019-07-24 stsp }
4392 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4393 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4394 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4395 0ebf8283 2019-07-24 stsp commit_id = NULL;
4396 0ebf8283 2019-07-24 stsp free(line);
4397 0ebf8283 2019-07-24 stsp line = NULL;
4398 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4399 0ebf8283 2019-07-24 stsp }
4400 0ebf8283 2019-07-24 stsp
4401 0ebf8283 2019-07-24 stsp free(line);
4402 0ebf8283 2019-07-24 stsp free(commit_id);
4403 0ebf8283 2019-07-24 stsp return err;
4404 0ebf8283 2019-07-24 stsp }
4405 0ebf8283 2019-07-24 stsp
4406 0ebf8283 2019-07-24 stsp static const struct got_error *
4407 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4408 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4409 bfce7f83 2019-07-27 stsp {
4410 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
4411 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
4412 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4413 bfce7f83 2019-07-27 stsp static char msg[80];
4414 bfce7f83 2019-07-27 stsp char *id_str;
4415 bfce7f83 2019-07-27 stsp
4416 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
4417 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4418 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
4419 bfce7f83 2019-07-27 stsp
4420 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4421 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4422 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4423 bfce7f83 2019-07-27 stsp break;
4424 bfce7f83 2019-07-27 stsp }
4425 bfce7f83 2019-07-27 stsp if (hle == NULL) {
4426 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
4427 bfce7f83 2019-07-27 stsp if (err)
4428 bfce7f83 2019-07-27 stsp return err;
4429 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
4430 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
4431 bfce7f83 2019-07-27 stsp free(id_str);
4432 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4433 bfce7f83 2019-07-27 stsp }
4434 bfce7f83 2019-07-27 stsp }
4435 bfce7f83 2019-07-27 stsp
4436 bfce7f83 2019-07-27 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4437 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4438 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
4439 bfce7f83 2019-07-27 stsp
4440 bfce7f83 2019-07-27 stsp return NULL;
4441 bfce7f83 2019-07-27 stsp }
4442 bfce7f83 2019-07-27 stsp
4443 bfce7f83 2019-07-27 stsp static const struct got_error *
4444 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4445 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
4446 bfce7f83 2019-07-27 stsp struct got_repository *repo)
4447 0ebf8283 2019-07-24 stsp {
4448 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4449 0ebf8283 2019-07-24 stsp char *editor;
4450 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4451 0ebf8283 2019-07-24 stsp
4452 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4453 0ebf8283 2019-07-24 stsp if (err)
4454 0ebf8283 2019-07-24 stsp return err;
4455 0ebf8283 2019-07-24 stsp
4456 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4457 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4458 0ebf8283 2019-07-24 stsp goto done;
4459 0ebf8283 2019-07-24 stsp }
4460 0ebf8283 2019-07-24 stsp
4461 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4462 0ebf8283 2019-07-24 stsp if (f == NULL) {
4463 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4464 0ebf8283 2019-07-24 stsp goto done;
4465 0ebf8283 2019-07-24 stsp }
4466 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4467 bfce7f83 2019-07-27 stsp if (err)
4468 bfce7f83 2019-07-27 stsp goto done;
4469 bfce7f83 2019-07-27 stsp
4470 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
4471 0ebf8283 2019-07-24 stsp done:
4472 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4473 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4474 0ebf8283 2019-07-24 stsp free(editor);
4475 0ebf8283 2019-07-24 stsp return err;
4476 0ebf8283 2019-07-24 stsp }
4477 0ebf8283 2019-07-24 stsp
4478 0ebf8283 2019-07-24 stsp static const struct got_error *
4479 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4480 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4481 0ebf8283 2019-07-24 stsp
4482 0ebf8283 2019-07-24 stsp static const struct got_error *
4483 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4484 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4485 0ebf8283 2019-07-24 stsp {
4486 0ebf8283 2019-07-24 stsp const struct got_error *err;
4487 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4488 0ebf8283 2019-07-24 stsp char *path = NULL;
4489 0ebf8283 2019-07-24 stsp
4490 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4491 0ebf8283 2019-07-24 stsp if (err)
4492 0ebf8283 2019-07-24 stsp return err;
4493 0ebf8283 2019-07-24 stsp
4494 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4495 0ebf8283 2019-07-24 stsp if (err)
4496 0ebf8283 2019-07-24 stsp goto done;
4497 0ebf8283 2019-07-24 stsp
4498 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4499 0ebf8283 2019-07-24 stsp if (err)
4500 0ebf8283 2019-07-24 stsp goto done;
4501 0ebf8283 2019-07-24 stsp
4502 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4503 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4504 0ebf8283 2019-07-24 stsp goto done;
4505 0ebf8283 2019-07-24 stsp }
4506 0ebf8283 2019-07-24 stsp f = NULL;
4507 0ebf8283 2019-07-24 stsp
4508 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
4509 0ebf8283 2019-07-24 stsp if (err) {
4510 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4511 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4512 0ebf8283 2019-07-24 stsp goto done;
4513 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
4514 0ebf8283 2019-07-24 stsp commits, path, repo);
4515 0ebf8283 2019-07-24 stsp }
4516 0ebf8283 2019-07-24 stsp done:
4517 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4518 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4519 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4520 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4521 0ebf8283 2019-07-24 stsp free(path);
4522 0ebf8283 2019-07-24 stsp return err;
4523 0ebf8283 2019-07-24 stsp }
4524 0ebf8283 2019-07-24 stsp
4525 0ebf8283 2019-07-24 stsp static const struct got_error *
4526 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4527 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4528 0ebf8283 2019-07-24 stsp {
4529 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4530 0ebf8283 2019-07-24 stsp char *path = NULL;
4531 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4532 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4533 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4534 0ebf8283 2019-07-24 stsp
4535 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
4536 0ebf8283 2019-07-24 stsp if (err)
4537 0ebf8283 2019-07-24 stsp return err;
4538 0ebf8283 2019-07-24 stsp
4539 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4540 0ebf8283 2019-07-24 stsp if (f == NULL) {
4541 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4542 0ebf8283 2019-07-24 stsp goto done;
4543 0ebf8283 2019-07-24 stsp }
4544 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4545 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4546 0ebf8283 2019-07-24 stsp repo);
4547 0ebf8283 2019-07-24 stsp if (err)
4548 0ebf8283 2019-07-24 stsp break;
4549 0ebf8283 2019-07-24 stsp
4550 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4551 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4552 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4553 0ebf8283 2019-07-24 stsp if (n < 0) {
4554 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4555 0ebf8283 2019-07-24 stsp break;
4556 0ebf8283 2019-07-24 stsp }
4557 0ebf8283 2019-07-24 stsp }
4558 0ebf8283 2019-07-24 stsp }
4559 0ebf8283 2019-07-24 stsp done:
4560 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4561 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4562 0ebf8283 2019-07-24 stsp free(path);
4563 0ebf8283 2019-07-24 stsp if (commit)
4564 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4565 0ebf8283 2019-07-24 stsp return err;
4566 0ebf8283 2019-07-24 stsp }
4567 0ebf8283 2019-07-24 stsp
4568 bfce7f83 2019-07-27 stsp void
4569 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
4570 bfce7f83 2019-07-27 stsp {
4571 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4572 bfce7f83 2019-07-27 stsp
4573 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
4574 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
4575 bfce7f83 2019-07-27 stsp free(hle);
4576 bfce7f83 2019-07-27 stsp }
4577 bfce7f83 2019-07-27 stsp }
4578 bfce7f83 2019-07-27 stsp
4579 0ebf8283 2019-07-24 stsp static const struct got_error *
4580 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4581 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4582 0ebf8283 2019-07-24 stsp {
4583 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4584 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4585 0ebf8283 2019-07-24 stsp
4586 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4587 0ebf8283 2019-07-24 stsp if (f == NULL) {
4588 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4589 0ebf8283 2019-07-24 stsp goto done;
4590 0ebf8283 2019-07-24 stsp }
4591 0ebf8283 2019-07-24 stsp
4592 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4593 0ebf8283 2019-07-24 stsp done:
4594 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4595 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4596 0ebf8283 2019-07-24 stsp return err;
4597 0ebf8283 2019-07-24 stsp }
4598 0ebf8283 2019-07-24 stsp
4599 0ebf8283 2019-07-24 stsp static const struct got_error *
4600 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4601 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
4602 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4603 0ebf8283 2019-07-24 stsp {
4604 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
4605 0ebf8283 2019-07-24 stsp int resp = ' ';
4606 0ebf8283 2019-07-24 stsp
4607 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
4608 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4609 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
4610 0ebf8283 2019-07-24 stsp resp = getchar();
4611 426ebf2e 2019-07-25 stsp if (resp == 'c') {
4612 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4613 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
4614 bfce7f83 2019-07-27 stsp repo);
4615 426ebf2e 2019-07-25 stsp if (err) {
4616 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4617 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4618 426ebf2e 2019-07-25 stsp break;
4619 bfce7f83 2019-07-27 stsp prev_err = err;
4620 426ebf2e 2019-07-25 stsp resp = ' ';
4621 426ebf2e 2019-07-25 stsp continue;
4622 426ebf2e 2019-07-25 stsp }
4623 bfce7f83 2019-07-27 stsp break;
4624 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
4625 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4626 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
4627 0ebf8283 2019-07-24 stsp commits, repo);
4628 426ebf2e 2019-07-25 stsp if (err) {
4629 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4630 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4631 426ebf2e 2019-07-25 stsp break;
4632 bfce7f83 2019-07-27 stsp prev_err = err;
4633 426ebf2e 2019-07-25 stsp resp = ' ';
4634 426ebf2e 2019-07-25 stsp continue;
4635 426ebf2e 2019-07-25 stsp }
4636 bfce7f83 2019-07-27 stsp break;
4637 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
4638 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4639 0ebf8283 2019-07-24 stsp break;
4640 426ebf2e 2019-07-25 stsp } else
4641 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
4642 0ebf8283 2019-07-24 stsp }
4643 0ebf8283 2019-07-24 stsp
4644 0ebf8283 2019-07-24 stsp return err;
4645 0ebf8283 2019-07-24 stsp }
4646 0ebf8283 2019-07-24 stsp
4647 0ebf8283 2019-07-24 stsp static const struct got_error *
4648 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
4649 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4650 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
4651 0ebf8283 2019-07-24 stsp {
4652 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4653 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4654 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4655 3e3a69f1 2019-07-25 stsp branch, repo);
4656 0ebf8283 2019-07-24 stsp }
4657 0ebf8283 2019-07-24 stsp
4658 0ebf8283 2019-07-24 stsp static const struct got_error *
4659 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
4660 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4661 0ebf8283 2019-07-24 stsp {
4662 0ebf8283 2019-07-24 stsp const struct got_error *err;
4663 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4664 0ebf8283 2019-07-24 stsp
4665 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
4666 0ebf8283 2019-07-24 stsp if (err)
4667 0ebf8283 2019-07-24 stsp goto done;
4668 0ebf8283 2019-07-24 stsp
4669 0ebf8283 2019-07-24 stsp if (new_id) {
4670 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
4671 0ebf8283 2019-07-24 stsp if (err)
4672 0ebf8283 2019-07-24 stsp goto done;
4673 0ebf8283 2019-07-24 stsp }
4674 0ebf8283 2019-07-24 stsp
4675 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
4676 0ebf8283 2019-07-24 stsp if (new_id_str)
4677 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
4678 0ebf8283 2019-07-24 stsp
4679 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4680 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
4681 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
4682 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4683 0ebf8283 2019-07-24 stsp goto done;
4684 0ebf8283 2019-07-24 stsp }
4685 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
4686 0ebf8283 2019-07-24 stsp } else {
4687 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4688 0ebf8283 2019-07-24 stsp if (err)
4689 0ebf8283 2019-07-24 stsp goto done;
4690 0ebf8283 2019-07-24 stsp }
4691 0ebf8283 2019-07-24 stsp
4692 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
4693 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
4694 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
4695 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
4696 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4697 0ebf8283 2019-07-24 stsp break;
4698 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
4699 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
4700 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4701 0ebf8283 2019-07-24 stsp logmsg);
4702 0ebf8283 2019-07-24 stsp break;
4703 0ebf8283 2019-07-24 stsp default:
4704 0ebf8283 2019-07-24 stsp break;
4705 0ebf8283 2019-07-24 stsp }
4706 0ebf8283 2019-07-24 stsp
4707 0ebf8283 2019-07-24 stsp done:
4708 0ebf8283 2019-07-24 stsp free(old_id_str);
4709 0ebf8283 2019-07-24 stsp free(new_id_str);
4710 0ebf8283 2019-07-24 stsp return err;
4711 0ebf8283 2019-07-24 stsp }
4712 0ebf8283 2019-07-24 stsp
4713 0ebf8283 2019-07-24 stsp static const struct got_error *
4714 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
4715 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4716 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4717 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4718 0ebf8283 2019-07-24 stsp {
4719 0ebf8283 2019-07-24 stsp const struct got_error *err;
4720 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4721 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
4722 0ebf8283 2019-07-24 stsp
4723 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4724 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
4725 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4726 0ebf8283 2019-07-24 stsp if (err)
4727 0ebf8283 2019-07-24 stsp return err;
4728 0ebf8283 2019-07-24 stsp }
4729 0ebf8283 2019-07-24 stsp
4730 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4731 0ebf8283 2019-07-24 stsp if (err)
4732 0ebf8283 2019-07-24 stsp return err;
4733 0ebf8283 2019-07-24 stsp
4734 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4735 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
4736 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
4737 0ebf8283 2019-07-24 stsp if (err) {
4738 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4739 0ebf8283 2019-07-24 stsp goto done;
4740 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
4741 0ebf8283 2019-07-24 stsp } else {
4742 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
4743 0ebf8283 2019-07-24 stsp free(new_commit_id);
4744 0ebf8283 2019-07-24 stsp }
4745 0ebf8283 2019-07-24 stsp done:
4746 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4747 0ebf8283 2019-07-24 stsp return err;
4748 0ebf8283 2019-07-24 stsp }
4749 0ebf8283 2019-07-24 stsp
4750 0ebf8283 2019-07-24 stsp static const struct got_error *
4751 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
4752 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4753 0ebf8283 2019-07-24 stsp {
4754 0ebf8283 2019-07-24 stsp const struct got_error *error;
4755 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4756 0ebf8283 2019-07-24 stsp
4757 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4758 0ebf8283 2019-07-24 stsp repo);
4759 0ebf8283 2019-07-24 stsp if (error)
4760 0ebf8283 2019-07-24 stsp return error;
4761 0ebf8283 2019-07-24 stsp
4762 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4763 0ebf8283 2019-07-24 stsp if (error)
4764 0ebf8283 2019-07-24 stsp return error;
4765 0ebf8283 2019-07-24 stsp
4766 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
4767 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4768 0ebf8283 2019-07-24 stsp return error;
4769 0ebf8283 2019-07-24 stsp }
4770 0ebf8283 2019-07-24 stsp
4771 0ebf8283 2019-07-24 stsp static const struct got_error *
4772 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
4773 0ebf8283 2019-07-24 stsp {
4774 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
4775 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
4776 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4777 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
4778 0ebf8283 2019-07-24 stsp char *cwd = NULL;
4779 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
4780 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
4781 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
4782 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
4783 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
4784 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4785 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
4786 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4787 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
4788 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4789 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
4790 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
4791 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
4792 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
4793 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
4794 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4795 0ebf8283 2019-07-24 stsp
4796 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
4797 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
4798 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
4799 0ebf8283 2019-07-24 stsp
4800 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
4801 0ebf8283 2019-07-24 stsp switch (ch) {
4802 0ebf8283 2019-07-24 stsp case 'a':
4803 0ebf8283 2019-07-24 stsp abort_edit = 1;
4804 0ebf8283 2019-07-24 stsp break;
4805 0ebf8283 2019-07-24 stsp case 'c':
4806 0ebf8283 2019-07-24 stsp continue_edit = 1;
4807 0ebf8283 2019-07-24 stsp break;
4808 0ebf8283 2019-07-24 stsp case 'F':
4809 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
4810 0ebf8283 2019-07-24 stsp break;
4811 0ebf8283 2019-07-24 stsp default:
4812 0ebf8283 2019-07-24 stsp usage_histedit();
4813 0ebf8283 2019-07-24 stsp /* NOTREACHED */
4814 0ebf8283 2019-07-24 stsp }
4815 0ebf8283 2019-07-24 stsp }
4816 0ebf8283 2019-07-24 stsp
4817 0ebf8283 2019-07-24 stsp argc -= optind;
4818 0ebf8283 2019-07-24 stsp argv += optind;
4819 0ebf8283 2019-07-24 stsp
4820 0ebf8283 2019-07-24 stsp #ifndef PROFILE
4821 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4822 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
4823 0ebf8283 2019-07-24 stsp err(1, "pledge");
4824 0ebf8283 2019-07-24 stsp #endif
4825 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
4826 0ebf8283 2019-07-24 stsp usage_histedit();
4827 0ebf8283 2019-07-24 stsp if (argc != 0)
4828 0ebf8283 2019-07-24 stsp usage_histedit();
4829 0ebf8283 2019-07-24 stsp
4830 0ebf8283 2019-07-24 stsp /*
4831 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
4832 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
4833 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
4834 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
4835 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
4836 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
4837 0ebf8283 2019-07-24 stsp */
4838 0ebf8283 2019-07-24 stsp
4839 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
4840 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
4841 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
4842 0ebf8283 2019-07-24 stsp goto done;
4843 0ebf8283 2019-07-24 stsp }
4844 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
4845 0ebf8283 2019-07-24 stsp if (error)
4846 0ebf8283 2019-07-24 stsp goto done;
4847 0ebf8283 2019-07-24 stsp
4848 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4849 0ebf8283 2019-07-24 stsp if (error != NULL)
4850 0ebf8283 2019-07-24 stsp goto done;
4851 0ebf8283 2019-07-24 stsp
4852 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4853 0ebf8283 2019-07-24 stsp if (error)
4854 0ebf8283 2019-07-24 stsp goto done;
4855 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
4856 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
4857 0ebf8283 2019-07-24 stsp goto done;
4858 0ebf8283 2019-07-24 stsp }
4859 0ebf8283 2019-07-24 stsp
4860 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4861 0ebf8283 2019-07-24 stsp if (error)
4862 0ebf8283 2019-07-24 stsp goto done;
4863 0ebf8283 2019-07-24 stsp
4864 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
4865 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4866 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4867 3e3a69f1 2019-07-25 stsp worktree, repo);
4868 0ebf8283 2019-07-24 stsp if (error)
4869 0ebf8283 2019-07-24 stsp goto done;
4870 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4871 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4872 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
4873 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
4874 0ebf8283 2019-07-24 stsp if (error)
4875 0ebf8283 2019-07-24 stsp goto done;
4876 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
4877 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4878 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
4879 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
4880 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4881 0ebf8283 2019-07-24 stsp goto done;
4882 0ebf8283 2019-07-24 stsp }
4883 0ebf8283 2019-07-24 stsp
4884 0ebf8283 2019-07-24 stsp if (continue_edit) {
4885 0ebf8283 2019-07-24 stsp char *path;
4886 0ebf8283 2019-07-24 stsp
4887 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
4888 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4889 0ebf8283 2019-07-24 stsp goto done;
4890 0ebf8283 2019-07-24 stsp }
4891 0ebf8283 2019-07-24 stsp
4892 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
4893 0ebf8283 2019-07-24 stsp if (error)
4894 0ebf8283 2019-07-24 stsp goto done;
4895 0ebf8283 2019-07-24 stsp
4896 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
4897 0ebf8283 2019-07-24 stsp free(path);
4898 0ebf8283 2019-07-24 stsp if (error)
4899 0ebf8283 2019-07-24 stsp goto done;
4900 0ebf8283 2019-07-24 stsp
4901 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4902 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4903 3e3a69f1 2019-07-25 stsp worktree, repo);
4904 0ebf8283 2019-07-24 stsp if (error)
4905 0ebf8283 2019-07-24 stsp goto done;
4906 0ebf8283 2019-07-24 stsp
4907 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4908 0ebf8283 2019-07-24 stsp if (error)
4909 0ebf8283 2019-07-24 stsp goto done;
4910 0ebf8283 2019-07-24 stsp
4911 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4912 0ebf8283 2019-07-24 stsp head_commit_id);
4913 0ebf8283 2019-07-24 stsp if (error)
4914 0ebf8283 2019-07-24 stsp goto done;
4915 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4916 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4917 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
4918 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4919 3aac7cf7 2019-07-25 stsp goto done;
4920 3aac7cf7 2019-07-25 stsp }
4921 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
4922 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
4923 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
4924 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4925 0ebf8283 2019-07-24 stsp commit = NULL;
4926 0ebf8283 2019-07-24 stsp if (error)
4927 0ebf8283 2019-07-24 stsp goto done;
4928 0ebf8283 2019-07-24 stsp } else {
4929 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
4930 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
4931 0ebf8283 2019-07-24 stsp goto done;
4932 0ebf8283 2019-07-24 stsp }
4933 0ebf8283 2019-07-24 stsp
4934 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
4935 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
4936 0ebf8283 2019-07-24 stsp if (error != NULL)
4937 0ebf8283 2019-07-24 stsp goto done;
4938 0ebf8283 2019-07-24 stsp
4939 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4940 bfce7f83 2019-07-27 stsp got_ref_close(branch);
4941 bfce7f83 2019-07-27 stsp branch = NULL;
4942 0ebf8283 2019-07-24 stsp if (error)
4943 0ebf8283 2019-07-24 stsp goto done;
4944 0ebf8283 2019-07-24 stsp
4945 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4946 0ebf8283 2019-07-24 stsp head_commit_id);
4947 0ebf8283 2019-07-24 stsp if (error)
4948 0ebf8283 2019-07-24 stsp goto done;
4949 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4950 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4951 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
4952 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4953 3aac7cf7 2019-07-25 stsp goto done;
4954 3aac7cf7 2019-07-25 stsp }
4955 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
4956 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
4957 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
4958 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
4959 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4960 0ebf8283 2019-07-24 stsp commit = NULL;
4961 0ebf8283 2019-07-24 stsp if (error)
4962 0ebf8283 2019-07-24 stsp goto done;
4963 0ebf8283 2019-07-24 stsp
4964 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4965 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
4966 bfce7f83 2019-07-27 stsp if (error)
4967 bfce7f83 2019-07-27 stsp goto done;
4968 bfce7f83 2019-07-27 stsp
4969 0ebf8283 2019-07-24 stsp if (edit_script_path) {
4970 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
4971 0ebf8283 2019-07-24 stsp edit_script_path, repo);
4972 6ba2bea2 2019-07-27 stsp if (error) {
4973 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
4974 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
4975 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
4976 0ebf8283 2019-07-24 stsp goto done;
4977 6ba2bea2 2019-07-27 stsp }
4978 0ebf8283 2019-07-24 stsp } else {
4979 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
4980 0ebf8283 2019-07-24 stsp repo);
4981 6ba2bea2 2019-07-27 stsp if (error) {
4982 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
4983 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
4984 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
4985 0ebf8283 2019-07-24 stsp goto done;
4986 6ba2bea2 2019-07-27 stsp }
4987 0ebf8283 2019-07-24 stsp
4988 0ebf8283 2019-07-24 stsp }
4989 0ebf8283 2019-07-24 stsp
4990 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
4991 0ebf8283 2019-07-24 stsp repo);
4992 6ba2bea2 2019-07-27 stsp if (error) {
4993 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
4994 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
4995 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
4996 0ebf8283 2019-07-24 stsp goto done;
4997 6ba2bea2 2019-07-27 stsp }
4998 0ebf8283 2019-07-24 stsp
4999 0ebf8283 2019-07-24 stsp }
5000 0ebf8283 2019-07-24 stsp
5001 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5002 0ebf8283 2019-07-24 stsp if (error)
5003 0ebf8283 2019-07-24 stsp goto done;
5004 0ebf8283 2019-07-24 stsp
5005 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5006 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5007 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5008 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5009 0ebf8283 2019-07-24 stsp continue;
5010 0ebf8283 2019-07-24 stsp
5011 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5012 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5013 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5014 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5015 0ebf8283 2019-07-24 stsp repo);
5016 0ebf8283 2019-07-24 stsp } else {
5017 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5018 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5019 0ebf8283 2019-07-24 stsp }
5020 0ebf8283 2019-07-24 stsp if (error)
5021 0ebf8283 2019-07-24 stsp goto done;
5022 0ebf8283 2019-07-24 stsp continue;
5023 0ebf8283 2019-07-24 stsp }
5024 0ebf8283 2019-07-24 stsp
5025 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5026 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5027 0ebf8283 2019-07-24 stsp if (error)
5028 0ebf8283 2019-07-24 stsp goto done;
5029 0ebf8283 2019-07-24 stsp continue;
5030 0ebf8283 2019-07-24 stsp }
5031 0ebf8283 2019-07-24 stsp
5032 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5033 0ebf8283 2019-07-24 stsp hle->commit_id);
5034 0ebf8283 2019-07-24 stsp if (error)
5035 0ebf8283 2019-07-24 stsp goto done;
5036 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5037 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5038 0ebf8283 2019-07-24 stsp
5039 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5040 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5041 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5042 0ebf8283 2019-07-24 stsp if (error)
5043 0ebf8283 2019-07-24 stsp goto done;
5044 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5045 0ebf8283 2019-07-24 stsp commit = NULL;
5046 0ebf8283 2019-07-24 stsp
5047 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5048 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5049 0ebf8283 2019-07-24 stsp break;
5050 0ebf8283 2019-07-24 stsp }
5051 0ebf8283 2019-07-24 stsp
5052 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5053 0ebf8283 2019-07-24 stsp char *id_str;
5054 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5055 0ebf8283 2019-07-24 stsp if (error)
5056 0ebf8283 2019-07-24 stsp goto done;
5057 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5058 0ebf8283 2019-07-24 stsp id_str);
5059 0ebf8283 2019-07-24 stsp free(id_str);
5060 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5061 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5062 3e3a69f1 2019-07-25 stsp fileindex);
5063 0ebf8283 2019-07-24 stsp goto done;
5064 0ebf8283 2019-07-24 stsp }
5065 0ebf8283 2019-07-24 stsp
5066 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5067 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5068 0ebf8283 2019-07-24 stsp if (error)
5069 0ebf8283 2019-07-24 stsp goto done;
5070 0ebf8283 2019-07-24 stsp continue;
5071 0ebf8283 2019-07-24 stsp }
5072 0ebf8283 2019-07-24 stsp
5073 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5074 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5075 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5076 0ebf8283 2019-07-24 stsp if (error)
5077 0ebf8283 2019-07-24 stsp goto done;
5078 0ebf8283 2019-07-24 stsp }
5079 0ebf8283 2019-07-24 stsp
5080 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5081 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5082 0ebf8283 2019-07-24 stsp if (error)
5083 0ebf8283 2019-07-24 stsp goto done;
5084 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5085 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5086 0ebf8283 2019-07-24 stsp } else
5087 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5088 3e3a69f1 2019-07-25 stsp branch, repo);
5089 0ebf8283 2019-07-24 stsp done:
5090 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5091 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5092 0ebf8283 2019-07-24 stsp free(head_commit_id);
5093 0ebf8283 2019-07-24 stsp free(base_commit_id);
5094 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5095 0ebf8283 2019-07-24 stsp if (commit)
5096 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5097 0ebf8283 2019-07-24 stsp if (branch)
5098 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5099 0ebf8283 2019-07-24 stsp if (tmp_branch)
5100 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5101 0ebf8283 2019-07-24 stsp if (worktree)
5102 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5103 0ebf8283 2019-07-24 stsp if (repo)
5104 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5105 0ebf8283 2019-07-24 stsp return error;
5106 0ebf8283 2019-07-24 stsp }