Blame


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