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