Blame


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