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