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