Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 5c860e29 2018-03-12 stsp #endif
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp }
70 99437157 2018-11-11 stsp
71 99437157 2018-11-11 stsp static void
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp {
74 99437157 2018-11-11 stsp sigpipe_received = 1;
75 99437157 2018-11-11 stsp }
76 5c860e29 2018-03-12 stsp
77 99437157 2018-11-11 stsp
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
83 5c860e29 2018-03-12 stsp };
84 5c860e29 2018-03-12 stsp
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
93 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
94 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
95 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
96 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
97 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
98 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
99 d00136be 2019-03-26 stsp __dead static void usage_add(void);
100 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
101 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
102 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
103 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
104 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
105 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
106 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
107 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
108 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
109 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
110 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
111 5c860e29 2018-03-12 stsp
112 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
113 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
114 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
115 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
116 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
118 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
119 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
120 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
122 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
123 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
124 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
125 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
126 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
127 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
128 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
129 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
130 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
131 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
132 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
133 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
134 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
135 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
136 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
137 5c860e29 2018-03-12 stsp
138 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
139 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
140 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
141 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
142 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
143 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
144 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
145 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
146 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
147 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
148 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
149 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
150 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
151 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
152 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
153 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
154 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
155 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
156 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
158 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
159 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
160 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
161 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
162 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
163 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
164 5c860e29 2018-03-12 stsp };
165 ce5b7c56 2019-07-09 stsp
166 ce5b7c56 2019-07-09 stsp static void
167 ce5b7c56 2019-07-09 stsp list_commands(void)
168 ce5b7c56 2019-07-09 stsp {
169 ce5b7c56 2019-07-09 stsp int i;
170 ce5b7c56 2019-07-09 stsp
171 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
172 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
173 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
175 ce5b7c56 2019-07-09 stsp }
176 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
177 ce5b7c56 2019-07-09 stsp }
178 5c860e29 2018-03-12 stsp
179 5c860e29 2018-03-12 stsp int
180 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
181 5c860e29 2018-03-12 stsp {
182 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
183 5c860e29 2018-03-12 stsp unsigned int i;
184 5c860e29 2018-03-12 stsp int ch;
185 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
186 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
187 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
188 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
189 83cd27f8 2020-01-13 stsp };
190 5c860e29 2018-03-12 stsp
191 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
192 5c860e29 2018-03-12 stsp
193 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 5c860e29 2018-03-12 stsp switch (ch) {
195 1b6b95a8 2018-03-12 stsp case 'h':
196 1b6b95a8 2018-03-12 stsp hflag = 1;
197 53ccebc2 2019-07-30 stsp break;
198 53ccebc2 2019-07-30 stsp case 'V':
199 53ccebc2 2019-07-30 stsp Vflag = 1;
200 1b6b95a8 2018-03-12 stsp break;
201 5c860e29 2018-03-12 stsp default:
202 ce5b7c56 2019-07-09 stsp usage(hflag);
203 5c860e29 2018-03-12 stsp /* NOTREACHED */
204 5c860e29 2018-03-12 stsp }
205 5c860e29 2018-03-12 stsp }
206 5c860e29 2018-03-12 stsp
207 5c860e29 2018-03-12 stsp argc -= optind;
208 5c860e29 2018-03-12 stsp argv += optind;
209 1e70621d 2018-03-27 stsp optind = 0;
210 53ccebc2 2019-07-30 stsp
211 53ccebc2 2019-07-30 stsp if (Vflag) {
212 53ccebc2 2019-07-30 stsp got_version_print_str();
213 53ccebc2 2019-07-30 stsp return 1;
214 53ccebc2 2019-07-30 stsp }
215 5c860e29 2018-03-12 stsp
216 5c860e29 2018-03-12 stsp if (argc <= 0)
217 ce5b7c56 2019-07-09 stsp usage(hflag);
218 5c860e29 2018-03-12 stsp
219 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
220 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
221 99437157 2018-11-11 stsp
222 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
223 d7d4f210 2018-03-12 stsp const struct got_error *error;
224 d7d4f210 2018-03-12 stsp
225 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
226 5c860e29 2018-03-12 stsp
227 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
229 5c860e29 2018-03-12 stsp continue;
230 5c860e29 2018-03-12 stsp
231 1b6b95a8 2018-03-12 stsp if (hflag)
232 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
233 1b6b95a8 2018-03-12 stsp
234 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
235 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
236 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
237 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
238 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 70015d7a 2019-11-08 stsp !(sigint_received &&
240 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 d7d4f210 2018-03-12 stsp return 1;
243 d7d4f210 2018-03-12 stsp }
244 d7d4f210 2018-03-12 stsp
245 d7d4f210 2018-03-12 stsp return 0;
246 5c860e29 2018-03-12 stsp }
247 5c860e29 2018-03-12 stsp
248 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 ce5b7c56 2019-07-09 stsp list_commands();
250 5c860e29 2018-03-12 stsp return 1;
251 5c860e29 2018-03-12 stsp }
252 5c860e29 2018-03-12 stsp
253 4ed7e80c 2018-05-20 stsp __dead static void
254 ce5b7c56 2019-07-09 stsp usage(int hflag)
255 5c860e29 2018-03-12 stsp {
256 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 53ccebc2 2019-07-30 stsp getprogname());
258 ce5b7c56 2019-07-09 stsp if (hflag)
259 ce5b7c56 2019-07-09 stsp list_commands();
260 5c860e29 2018-03-12 stsp exit(1);
261 5c860e29 2018-03-12 stsp }
262 5c860e29 2018-03-12 stsp
263 0266afb7 2019-01-04 stsp static const struct got_error *
264 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
265 e2ba3d07 2019-05-13 stsp {
266 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
267 e2ba3d07 2019-05-13 stsp const char *editor;
268 8920fa04 2019-08-18 stsp
269 8920fa04 2019-08-18 stsp *abspath = NULL;
270 e2ba3d07 2019-05-13 stsp
271 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
272 e2ba3d07 2019-05-13 stsp if (editor == NULL)
273 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
274 e2ba3d07 2019-05-13 stsp
275 0ee7065d 2019-05-13 stsp if (editor) {
276 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
277 0ee7065d 2019-05-13 stsp if (err)
278 0ee7065d 2019-05-13 stsp return err;
279 0ee7065d 2019-05-13 stsp }
280 e2ba3d07 2019-05-13 stsp
281 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
282 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
283 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
284 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
285 0ee7065d 2019-05-13 stsp }
286 0ee7065d 2019-05-13 stsp
287 e2ba3d07 2019-05-13 stsp return NULL;
288 e2ba3d07 2019-05-13 stsp }
289 e2ba3d07 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp static const struct got_error *
291 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
292 c530dc23 2019-07-23 stsp const char *worktree_path)
293 0266afb7 2019-01-04 stsp {
294 163ce85a 2019-05-13 stsp const struct got_error *err;
295 0266afb7 2019-01-04 stsp
296 37c06ea4 2019-07-15 stsp #ifdef PROFILE
297 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
298 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
299 37c06ea4 2019-07-15 stsp #endif
300 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
302 0266afb7 2019-01-04 stsp
303 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
305 0266afb7 2019-01-04 stsp
306 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 0266afb7 2019-01-04 stsp
309 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
310 163ce85a 2019-05-13 stsp if (err != NULL)
311 163ce85a 2019-05-13 stsp return err;
312 0266afb7 2019-01-04 stsp
313 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
314 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp return NULL;
317 2c7829a4 2019-06-17 stsp }
318 2c7829a4 2019-06-17 stsp
319 2c7829a4 2019-06-17 stsp __dead static void
320 2c7829a4 2019-06-17 stsp usage_init(void)
321 2c7829a4 2019-06-17 stsp {
322 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 2c7829a4 2019-06-17 stsp exit(1);
324 2c7829a4 2019-06-17 stsp }
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp static const struct got_error *
327 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
328 2c7829a4 2019-06-17 stsp {
329 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
330 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
331 2c7829a4 2019-06-17 stsp int ch;
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
334 2c7829a4 2019-06-17 stsp switch (ch) {
335 2c7829a4 2019-06-17 stsp default:
336 2c7829a4 2019-06-17 stsp usage_init();
337 2c7829a4 2019-06-17 stsp /* NOTREACHED */
338 2c7829a4 2019-06-17 stsp }
339 2c7829a4 2019-06-17 stsp }
340 2c7829a4 2019-06-17 stsp
341 2c7829a4 2019-06-17 stsp argc -= optind;
342 2c7829a4 2019-06-17 stsp argv += optind;
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp #ifndef PROFILE
345 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 2c7829a4 2019-06-17 stsp err(1, "pledge");
347 2c7829a4 2019-06-17 stsp #endif
348 2c7829a4 2019-06-17 stsp if (argc != 1)
349 bc20e173 2019-06-17 stsp usage_init();
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
352 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
353 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
354 2c7829a4 2019-06-17 stsp
355 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
356 2c7829a4 2019-06-17 stsp
357 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
358 2c7829a4 2019-06-17 stsp if (error &&
359 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 2c7829a4 2019-06-17 stsp goto done;
361 2c7829a4 2019-06-17 stsp
362 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
363 2c7829a4 2019-06-17 stsp if (error)
364 2c7829a4 2019-06-17 stsp goto done;
365 2c7829a4 2019-06-17 stsp
366 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
367 3ce1b845 2019-07-15 stsp done:
368 3ce1b845 2019-07-15 stsp free(repo_path);
369 3ce1b845 2019-07-15 stsp return error;
370 3ce1b845 2019-07-15 stsp }
371 3ce1b845 2019-07-15 stsp
372 3ce1b845 2019-07-15 stsp __dead static void
373 3ce1b845 2019-07-15 stsp usage_import(void)
374 3ce1b845 2019-07-15 stsp {
375 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
377 3ce1b845 2019-07-15 stsp exit(1);
378 3ce1b845 2019-07-15 stsp }
379 3ce1b845 2019-07-15 stsp
380 3ce1b845 2019-07-15 stsp int
381 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
382 3ce1b845 2019-07-15 stsp {
383 3ce1b845 2019-07-15 stsp pid_t pid;
384 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
385 3ce1b845 2019-07-15 stsp int st = -1;
386 3ce1b845 2019-07-15 stsp
387 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
389 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
390 3ce1b845 2019-07-15 stsp
391 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
392 3ce1b845 2019-07-15 stsp case -1:
393 3ce1b845 2019-07-15 stsp goto doneediting;
394 3ce1b845 2019-07-15 stsp case 0:
395 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
396 3ce1b845 2019-07-15 stsp _exit(127);
397 3ce1b845 2019-07-15 stsp }
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
400 3ce1b845 2019-07-15 stsp if (errno != EINTR)
401 3ce1b845 2019-07-15 stsp break;
402 3ce1b845 2019-07-15 stsp
403 3ce1b845 2019-07-15 stsp doneediting:
404 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
406 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
407 3ce1b845 2019-07-15 stsp
408 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
409 3ce1b845 2019-07-15 stsp errno = EINTR;
410 3ce1b845 2019-07-15 stsp return -1;
411 3ce1b845 2019-07-15 stsp }
412 3ce1b845 2019-07-15 stsp
413 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp static const struct got_error *
417 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 3ce1b845 2019-07-15 stsp const char *initial_content)
419 3ce1b845 2019-07-15 stsp {
420 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
421 3ce1b845 2019-07-15 stsp char buf[1024];
422 3ce1b845 2019-07-15 stsp struct stat st, st2;
423 3ce1b845 2019-07-15 stsp FILE *fp;
424 3ce1b845 2019-07-15 stsp int content_changed = 0;
425 3ce1b845 2019-07-15 stsp size_t len;
426 3ce1b845 2019-07-15 stsp
427 3ce1b845 2019-07-15 stsp *logmsg = NULL;
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
443 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
444 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
445 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
446 3ce1b845 2019-07-15 stsp len = 0;
447 3ce1b845 2019-07-15 stsp
448 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
449 3ce1b845 2019-07-15 stsp if (fp == NULL) {
450 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
451 3ce1b845 2019-07-15 stsp goto done;
452 3ce1b845 2019-07-15 stsp }
453 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
454 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
455 3ce1b845 2019-07-15 stsp content_changed = 1;
456 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
458 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp fclose(fp);
461 3ce1b845 2019-07-15 stsp
462 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
464 3ce1b845 2019-07-15 stsp len--;
465 3ce1b845 2019-07-15 stsp }
466 3ce1b845 2019-07-15 stsp
467 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
468 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
470 3ce1b845 2019-07-15 stsp done:
471 3ce1b845 2019-07-15 stsp if (err) {
472 3ce1b845 2019-07-15 stsp free(*logmsg);
473 3ce1b845 2019-07-15 stsp *logmsg = NULL;
474 3ce1b845 2019-07-15 stsp }
475 3ce1b845 2019-07-15 stsp return err;
476 3ce1b845 2019-07-15 stsp }
477 3ce1b845 2019-07-15 stsp
478 3ce1b845 2019-07-15 stsp static const struct got_error *
479 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
481 3ce1b845 2019-07-15 stsp {
482 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
483 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
484 3ce1b845 2019-07-15 stsp int fd;
485 3ce1b845 2019-07-15 stsp
486 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
487 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
488 3ce1b845 2019-07-15 stsp branch_name) == -1)
489 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
490 3ce1b845 2019-07-15 stsp
491 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
492 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
493 3ce1b845 2019-07-15 stsp if (err)
494 3ce1b845 2019-07-15 stsp goto done;
495 3ce1b845 2019-07-15 stsp
496 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
497 3ce1b845 2019-07-15 stsp close(fd);
498 3ce1b845 2019-07-15 stsp
499 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 3ce1b845 2019-07-15 stsp done:
501 3ce1b845 2019-07-15 stsp free(initial_content);
502 3ce1b845 2019-07-15 stsp return err;
503 3ce1b845 2019-07-15 stsp }
504 3ce1b845 2019-07-15 stsp
505 3ce1b845 2019-07-15 stsp static const struct got_error *
506 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
507 3ce1b845 2019-07-15 stsp {
508 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
509 3ce1b845 2019-07-15 stsp return NULL;
510 3ce1b845 2019-07-15 stsp }
511 3ce1b845 2019-07-15 stsp
512 3ce1b845 2019-07-15 stsp static const struct got_error *
513 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
514 84792843 2019-08-09 stsp {
515 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
516 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
517 84792843 2019-08-09 stsp
518 84792843 2019-08-09 stsp *author = NULL;
519 aba9c984 2019-09-08 stsp
520 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
521 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
522 c9956ddf 2019-09-08 stsp if (name && email) {
523 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
524 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
525 aba9c984 2019-09-08 stsp return NULL;
526 aba9c984 2019-09-08 stsp }
527 84792843 2019-08-09 stsp
528 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
529 84792843 2019-08-09 stsp if (got_author == NULL) {
530 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
531 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
532 c9956ddf 2019-09-08 stsp if (name && email) {
533 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
534 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
535 c9956ddf 2019-09-08 stsp return NULL;
536 c9956ddf 2019-09-08 stsp }
537 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
538 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
539 84792843 2019-08-09 stsp }
540 84792843 2019-08-09 stsp
541 aba9c984 2019-09-08 stsp *author = strdup(got_author);
542 aba9c984 2019-09-08 stsp if (*author == NULL)
543 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
544 84792843 2019-08-09 stsp
545 84792843 2019-08-09 stsp /*
546 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
547 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
548 84792843 2019-08-09 stsp */
549 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
550 84792843 2019-08-09 stsp got_author++;
551 aba9c984 2019-09-08 stsp if (*got_author != '<') {
552 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 aba9c984 2019-09-08 stsp goto done;
554 aba9c984 2019-09-08 stsp }
555 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
556 84792843 2019-08-09 stsp got_author++;
557 aba9c984 2019-09-08 stsp if (*got_author != '@') {
558 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 aba9c984 2019-09-08 stsp goto done;
560 aba9c984 2019-09-08 stsp }
561 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
562 84792843 2019-08-09 stsp got_author++;
563 84792843 2019-08-09 stsp if (*got_author != '>')
564 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 aba9c984 2019-09-08 stsp done:
566 aba9c984 2019-09-08 stsp if (err) {
567 aba9c984 2019-09-08 stsp free(*author);
568 aba9c984 2019-09-08 stsp *author = NULL;
569 aba9c984 2019-09-08 stsp }
570 aba9c984 2019-09-08 stsp return err;
571 c9956ddf 2019-09-08 stsp }
572 c9956ddf 2019-09-08 stsp
573 c9956ddf 2019-09-08 stsp static const struct got_error *
574 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
575 c9956ddf 2019-09-08 stsp {
576 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
577 c9956ddf 2019-09-08 stsp
578 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
579 c9956ddf 2019-09-08 stsp if (homedir) {
580 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
582 c9956ddf 2019-09-08 stsp
583 c9956ddf 2019-09-08 stsp }
584 c9956ddf 2019-09-08 stsp return NULL;
585 84792843 2019-08-09 stsp }
586 84792843 2019-08-09 stsp
587 84792843 2019-08-09 stsp static const struct got_error *
588 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
589 3ce1b845 2019-07-15 stsp {
590 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
591 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
594 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
596 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
598 3ce1b845 2019-07-15 stsp int ch;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
600 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
601 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
602 3ce1b845 2019-07-15 stsp
603 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
604 3ce1b845 2019-07-15 stsp
605 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 3ce1b845 2019-07-15 stsp switch (ch) {
607 3ce1b845 2019-07-15 stsp case 'b':
608 3ce1b845 2019-07-15 stsp branch_name = optarg;
609 3ce1b845 2019-07-15 stsp break;
610 3ce1b845 2019-07-15 stsp case 'm':
611 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
612 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
613 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
614 3ce1b845 2019-07-15 stsp goto done;
615 3ce1b845 2019-07-15 stsp }
616 3ce1b845 2019-07-15 stsp break;
617 3ce1b845 2019-07-15 stsp case 'r':
618 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
619 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
620 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
621 9ba1d308 2019-10-21 stsp optarg);
622 3ce1b845 2019-07-15 stsp goto done;
623 3ce1b845 2019-07-15 stsp }
624 3ce1b845 2019-07-15 stsp break;
625 3ce1b845 2019-07-15 stsp case 'I':
626 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
629 3ce1b845 2019-07-15 stsp NULL);
630 3ce1b845 2019-07-15 stsp if (error)
631 3ce1b845 2019-07-15 stsp goto done;
632 3ce1b845 2019-07-15 stsp break;
633 3ce1b845 2019-07-15 stsp default:
634 b2b65d18 2019-08-22 stsp usage_import();
635 3ce1b845 2019-07-15 stsp /* NOTREACHED */
636 3ce1b845 2019-07-15 stsp }
637 3ce1b845 2019-07-15 stsp }
638 3ce1b845 2019-07-15 stsp
639 3ce1b845 2019-07-15 stsp argc -= optind;
640 3ce1b845 2019-07-15 stsp argv += optind;
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp #ifndef PROFILE
643 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 aba9c984 2019-09-08 stsp "unveil",
645 3ce1b845 2019-07-15 stsp NULL) == -1)
646 3ce1b845 2019-07-15 stsp err(1, "pledge");
647 3ce1b845 2019-07-15 stsp #endif
648 3ce1b845 2019-07-15 stsp if (argc != 1)
649 3ce1b845 2019-07-15 stsp usage_import();
650 2c7829a4 2019-06-17 stsp
651 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
652 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
653 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
654 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
655 3ce1b845 2019-07-15 stsp }
656 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
657 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
658 c9956ddf 2019-09-08 stsp if (error)
659 c9956ddf 2019-09-08 stsp goto done;
660 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
661 3ce1b845 2019-07-15 stsp if (error)
662 3ce1b845 2019-07-15 stsp goto done;
663 aba9c984 2019-09-08 stsp
664 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
665 aba9c984 2019-09-08 stsp if (error)
666 aba9c984 2019-09-08 stsp return error;
667 e560b7e0 2019-11-28 stsp
668 e560b7e0 2019-11-28 stsp /*
669 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
670 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
671 e560b7e0 2019-11-28 stsp * an unintended typo.
672 e560b7e0 2019-11-28 stsp */
673 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
674 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 3ce1b845 2019-07-15 stsp
676 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
678 3ce1b845 2019-07-15 stsp goto done;
679 3ce1b845 2019-07-15 stsp }
680 3ce1b845 2019-07-15 stsp
681 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
682 3ce1b845 2019-07-15 stsp if (error) {
683 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
684 3ce1b845 2019-07-15 stsp goto done;
685 3ce1b845 2019-07-15 stsp } else {
686 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 3ce1b845 2019-07-15 stsp "import target branch already exists");
688 3ce1b845 2019-07-15 stsp goto done;
689 3ce1b845 2019-07-15 stsp }
690 3ce1b845 2019-07-15 stsp
691 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
692 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
693 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
694 3ce1b845 2019-07-15 stsp goto done;
695 3ce1b845 2019-07-15 stsp }
696 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
697 3ce1b845 2019-07-15 stsp
698 3ce1b845 2019-07-15 stsp /*
699 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
700 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
701 3ce1b845 2019-07-15 stsp */
702 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
703 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
704 3ce1b845 2019-07-15 stsp if (error)
705 3ce1b845 2019-07-15 stsp goto done;
706 8e158b01 2019-09-22 stsp free(logmsg);
707 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 ef293bdd 2019-10-21 stsp path_dir, refname);
709 ef293bdd 2019-10-21 stsp if (error) {
710 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
712 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
713 3ce1b845 2019-07-15 stsp goto done;
714 ef293bdd 2019-10-21 stsp }
715 3ce1b845 2019-07-15 stsp }
716 3ce1b845 2019-07-15 stsp
717 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
718 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
719 ef293bdd 2019-10-21 stsp if (logmsg_path)
720 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
721 3ce1b845 2019-07-15 stsp goto done;
722 ef293bdd 2019-10-21 stsp }
723 3ce1b845 2019-07-15 stsp
724 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 ef293bdd 2019-10-21 stsp if (error) {
726 ef293bdd 2019-10-21 stsp if (logmsg_path)
727 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
728 ef293bdd 2019-10-21 stsp goto done;
729 ef293bdd 2019-10-21 stsp }
730 ef293bdd 2019-10-21 stsp
731 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
733 ef293bdd 2019-10-21 stsp if (error) {
734 ef293bdd 2019-10-21 stsp if (logmsg_path)
735 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
736 3ce1b845 2019-07-15 stsp goto done;
737 ef293bdd 2019-10-21 stsp }
738 3ce1b845 2019-07-15 stsp
739 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 ef293bdd 2019-10-21 stsp if (error) {
741 ef293bdd 2019-10-21 stsp if (logmsg_path)
742 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
743 3ce1b845 2019-07-15 stsp goto done;
744 ef293bdd 2019-10-21 stsp }
745 3ce1b845 2019-07-15 stsp
746 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
747 ef293bdd 2019-10-21 stsp if (error) {
748 ef293bdd 2019-10-21 stsp if (logmsg_path)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
750 3ce1b845 2019-07-15 stsp goto done;
751 ef293bdd 2019-10-21 stsp }
752 3ce1b845 2019-07-15 stsp
753 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
754 ef293bdd 2019-10-21 stsp if (error) {
755 ef293bdd 2019-10-21 stsp if (logmsg_path)
756 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
757 3ce1b845 2019-07-15 stsp goto done;
758 ef293bdd 2019-10-21 stsp }
759 3ce1b845 2019-07-15 stsp
760 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 3ce1b845 2019-07-15 stsp if (error) {
762 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
763 ef293bdd 2019-10-21 stsp if (logmsg_path)
764 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
765 3ce1b845 2019-07-15 stsp goto done;
766 ef293bdd 2019-10-21 stsp }
767 3ce1b845 2019-07-15 stsp
768 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 3ce1b845 2019-07-15 stsp branch_ref);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
773 3ce1b845 2019-07-15 stsp goto done;
774 ef293bdd 2019-10-21 stsp }
775 3ce1b845 2019-07-15 stsp
776 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
777 ef293bdd 2019-10-21 stsp if (error) {
778 ef293bdd 2019-10-21 stsp if (logmsg_path)
779 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
780 3ce1b845 2019-07-15 stsp goto done;
781 ef293bdd 2019-10-21 stsp }
782 3ce1b845 2019-07-15 stsp }
783 3ce1b845 2019-07-15 stsp
784 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
785 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
786 2c7829a4 2019-06-17 stsp done:
787 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
788 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
789 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
790 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
792 8e158b01 2019-09-22 stsp free(logmsg);
793 ef293bdd 2019-10-21 stsp free(logmsg_path);
794 2c7829a4 2019-06-17 stsp free(repo_path);
795 3ce1b845 2019-07-15 stsp free(editor);
796 3ce1b845 2019-07-15 stsp free(refname);
797 3ce1b845 2019-07-15 stsp free(new_commit_id);
798 3ce1b845 2019-07-15 stsp free(id_str);
799 aba9c984 2019-09-08 stsp free(author);
800 c9956ddf 2019-09-08 stsp free(gitconfig_path);
801 3ce1b845 2019-07-15 stsp if (branch_ref)
802 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
803 3ce1b845 2019-07-15 stsp if (head_ref)
804 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
805 2c7829a4 2019-06-17 stsp return error;
806 93658fb9 2020-03-18 stsp }
807 93658fb9 2020-03-18 stsp
808 93658fb9 2020-03-18 stsp __dead static void
809 93658fb9 2020-03-18 stsp usage_clone(void)
810 93658fb9 2020-03-18 stsp {
811 9df6f38b 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url [target-directory]\n",
812 9df6f38b 2020-03-18 stsp getprogname());
813 93658fb9 2020-03-18 stsp exit(1);
814 0266afb7 2019-01-04 stsp }
815 0266afb7 2019-01-04 stsp
816 4ed7e80c 2018-05-20 stsp __dead static void
817 c09a553d 2018-03-12 stsp usage_checkout(void)
818 c09a553d 2018-03-12 stsp {
819 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
820 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
821 c09a553d 2018-03-12 stsp exit(1);
822 92a684f4 2018-03-12 stsp }
823 92a684f4 2018-03-12 stsp
824 7f47418f 2019-12-20 stsp static void
825 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
826 7f47418f 2019-12-20 stsp {
827 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
828 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
829 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
830 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
831 7f47418f 2019-12-20 stsp getprogname());
832 7f47418f 2019-12-20 stsp }
833 7f47418f 2019-12-20 stsp
834 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
835 7f47418f 2019-12-20 stsp const char *worktree_path;
836 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
837 7f47418f 2019-12-20 stsp };
838 7f47418f 2019-12-20 stsp
839 1ee397ad 2019-07-12 stsp static const struct got_error *
840 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
841 92a684f4 2018-03-12 stsp {
842 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
843 a484d721 2019-06-10 stsp
844 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
845 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
846 7f47418f 2019-12-20 stsp return NULL;
847 7f47418f 2019-12-20 stsp
848 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
849 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
850 1ee397ad 2019-07-12 stsp return NULL;
851 7f47418f 2019-12-20 stsp }
852 92a684f4 2018-03-12 stsp
853 92a684f4 2018-03-12 stsp while (path[0] == '/')
854 92a684f4 2018-03-12 stsp path++;
855 92a684f4 2018-03-12 stsp
856 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
857 1ee397ad 2019-07-12 stsp return NULL;
858 99437157 2018-11-11 stsp }
859 99437157 2018-11-11 stsp
860 99437157 2018-11-11 stsp static const struct got_error *
861 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
862 99437157 2018-11-11 stsp {
863 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
864 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
865 99437157 2018-11-11 stsp return NULL;
866 8069f636 2019-01-12 stsp }
867 8069f636 2019-01-12 stsp
868 8069f636 2019-01-12 stsp static const struct got_error *
869 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
870 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
871 3aef623b 2019-10-15 stsp struct got_repository *repo)
872 8069f636 2019-01-12 stsp {
873 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
874 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
875 8069f636 2019-01-12 stsp
876 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
877 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
878 36a38700 2019-05-10 stsp if (err)
879 36a38700 2019-05-10 stsp return err;
880 8069f636 2019-01-12 stsp
881 d5bea539 2019-05-13 stsp if (yca_id == NULL)
882 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
883 8069f636 2019-01-12 stsp
884 d5bea539 2019-05-13 stsp /*
885 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
886 d5bea539 2019-05-13 stsp * and the work tree's base commit.
887 d5bea539 2019-05-13 stsp *
888 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
889 d5bea539 2019-05-13 stsp *
890 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
891 d5bea539 2019-05-13 stsp * \ /
892 d5bea539 2019-05-13 stsp * C E
893 d5bea539 2019-05-13 stsp * \ /
894 d5bea539 2019-05-13 stsp * B (yca)
895 d5bea539 2019-05-13 stsp * |
896 d5bea539 2019-05-13 stsp * A
897 d5bea539 2019-05-13 stsp *
898 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
899 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
900 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
901 d5bea539 2019-05-13 stsp */
902 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
903 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
904 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
905 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
906 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
907 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
908 8069f636 2019-01-12 stsp
909 d5bea539 2019-05-13 stsp free(yca_id);
910 d5bea539 2019-05-13 stsp return NULL;
911 c09a553d 2018-03-12 stsp }
912 a367ff0f 2019-05-14 stsp
913 a367ff0f 2019-05-14 stsp static const struct got_error *
914 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
915 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
916 a51a74b3 2019-07-27 stsp struct got_repository *repo)
917 a367ff0f 2019-05-14 stsp {
918 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
919 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
920 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
921 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
922 a367ff0f 2019-05-14 stsp
923 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
924 a367ff0f 2019-05-14 stsp if (err)
925 a51a74b3 2019-07-27 stsp goto done;
926 a51a74b3 2019-07-27 stsp
927 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
928 a51a74b3 2019-07-27 stsp is_same_branch = 1;
929 a51a74b3 2019-07-27 stsp goto done;
930 a51a74b3 2019-07-27 stsp }
931 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
932 a51a74b3 2019-07-27 stsp is_same_branch = 1;
933 a367ff0f 2019-05-14 stsp goto done;
934 a51a74b3 2019-07-27 stsp }
935 a367ff0f 2019-05-14 stsp
936 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
937 a367ff0f 2019-05-14 stsp if (err)
938 a367ff0f 2019-05-14 stsp goto done;
939 a367ff0f 2019-05-14 stsp
940 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
941 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
942 a367ff0f 2019-05-14 stsp if (err)
943 a367ff0f 2019-05-14 stsp goto done;
944 a367ff0f 2019-05-14 stsp
945 a367ff0f 2019-05-14 stsp for (;;) {
946 a367ff0f 2019-05-14 stsp struct got_object_id *id;
947 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
948 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
949 a367ff0f 2019-05-14 stsp if (err) {
950 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
951 a367ff0f 2019-05-14 stsp err = NULL;
952 ee780d5c 2020-01-04 stsp break;
953 a367ff0f 2019-05-14 stsp }
954 c09a553d 2018-03-12 stsp
955 a367ff0f 2019-05-14 stsp if (id) {
956 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
957 a51a74b3 2019-07-27 stsp break;
958 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
959 a367ff0f 2019-05-14 stsp is_same_branch = 1;
960 a367ff0f 2019-05-14 stsp break;
961 a367ff0f 2019-05-14 stsp }
962 a367ff0f 2019-05-14 stsp }
963 a367ff0f 2019-05-14 stsp }
964 a367ff0f 2019-05-14 stsp done:
965 a367ff0f 2019-05-14 stsp if (graph)
966 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
967 a367ff0f 2019-05-14 stsp free(head_commit_id);
968 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
969 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
970 a367ff0f 2019-05-14 stsp return err;
971 93658fb9 2020-03-18 stsp }
972 892ac3b6 2020-03-18 stsp
973 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
974 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
975 892ac3b6 2020-03-18 stsp int last_p_indexed;
976 892ac3b6 2020-03-18 stsp int last_p_resolved;
977 892ac3b6 2020-03-18 stsp };
978 93658fb9 2020-03-18 stsp
979 93658fb9 2020-03-18 stsp static const struct got_error *
980 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
981 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
982 531c3985 2020-03-18 stsp {
983 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
984 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
985 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
986 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
987 b2409d58 2020-03-18 stsp
988 b2409d58 2020-03-18 stsp if (message && message[0] != '\0' && message[0] != '\n') {
989 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
990 b2409d58 2020-03-18 stsp if (strchr(message, '\n') == 0)
991 b2409d58 2020-03-18 stsp printf("\n");
992 892ac3b6 2020-03-18 stsp fflush(stdout);
993 b2409d58 2020-03-18 stsp }
994 b2409d58 2020-03-18 stsp
995 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
996 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
997 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
998 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
999 892ac3b6 2020-03-18 stsp print_size = 1;
1000 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1001 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1002 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1003 892ac3b6 2020-03-18 stsp }
1004 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1005 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1006 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1007 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1008 892ac3b6 2020-03-18 stsp print_indexed = 1;
1009 892ac3b6 2020-03-18 stsp print_size = 1;
1010 892ac3b6 2020-03-18 stsp }
1011 61cc1a7a 2020-03-18 stsp }
1012 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1013 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1014 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1015 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1016 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1017 892ac3b6 2020-03-18 stsp print_resolved = 1;
1018 892ac3b6 2020-03-18 stsp print_indexed = 1;
1019 892ac3b6 2020-03-18 stsp print_size = 1;
1020 892ac3b6 2020-03-18 stsp }
1021 61cc1a7a 2020-03-18 stsp }
1022 b2409d58 2020-03-18 stsp
1023 d2cdc636 2020-03-18 stsp }
1024 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1025 892ac3b6 2020-03-18 stsp printf("\r");
1026 892ac3b6 2020-03-18 stsp if (print_size)
1027 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1028 892ac3b6 2020-03-18 stsp if (print_indexed)
1029 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1030 892ac3b6 2020-03-18 stsp if (print_resolved)
1031 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1032 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1033 892ac3b6 2020-03-18 stsp fflush(stdout);
1034 892ac3b6 2020-03-18 stsp
1035 892ac3b6 2020-03-18 stsp if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
1036 892ac3b6 2020-03-18 stsp nobj_resolved == nobj_total - nobj_loose)
1037 892ac3b6 2020-03-18 stsp printf("\nWriting pack index...\n");
1038 892ac3b6 2020-03-18 stsp
1039 531c3985 2020-03-18 stsp return NULL;
1040 531c3985 2020-03-18 stsp }
1041 531c3985 2020-03-18 stsp
1042 531c3985 2020-03-18 stsp static const struct got_error *
1043 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1044 93658fb9 2020-03-18 stsp {
1045 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1046 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1047 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1048 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1049 bb64b798 2020-03-18 stsp const char *repo_path;
1050 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1051 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
1052 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1053 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1054 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
1055 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1056 93658fb9 2020-03-18 stsp
1057 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1058 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1059 d9b4d0c0 2020-03-18 stsp
1060 9df6f38b 2020-03-18 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1061 93658fb9 2020-03-18 stsp switch (ch) {
1062 93658fb9 2020-03-18 stsp default:
1063 93658fb9 2020-03-18 stsp usage_clone();
1064 93658fb9 2020-03-18 stsp break;
1065 93658fb9 2020-03-18 stsp }
1066 93658fb9 2020-03-18 stsp }
1067 93658fb9 2020-03-18 stsp argc -= optind;
1068 93658fb9 2020-03-18 stsp argv += optind;
1069 39c64a6a 2020-03-18 stsp
1070 93658fb9 2020-03-18 stsp uri = argv[0];
1071 9df6f38b 2020-03-18 stsp
1072 9df6f38b 2020-03-18 stsp if (argc == 1)
1073 93658fb9 2020-03-18 stsp dirname = NULL;
1074 9df6f38b 2020-03-18 stsp else if (argc == 2)
1075 93658fb9 2020-03-18 stsp dirname = argv[1];
1076 93658fb9 2020-03-18 stsp else
1077 93658fb9 2020-03-18 stsp usage_clone();
1078 09838ffc 2020-03-18 stsp
1079 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1080 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1081 39c64a6a 2020-03-18 stsp if (error)
1082 09838ffc 2020-03-18 stsp goto done;
1083 09838ffc 2020-03-18 stsp
1084 39c64a6a 2020-03-18 stsp #ifndef PROFILE
1085 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1086 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1087 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1088 39c64a6a 2020-03-18 stsp err(1, "pledge");
1089 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1090 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1091 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1092 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1093 39c64a6a 2020-03-18 stsp err(1, "pledge");
1094 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1095 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1096 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1097 39c64a6a 2020-03-18 stsp goto done;
1098 39c64a6a 2020-03-18 stsp } else {
1099 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1100 39c64a6a 2020-03-18 stsp goto done;
1101 39c64a6a 2020-03-18 stsp }
1102 39c64a6a 2020-03-18 stsp #endif
1103 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1104 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1105 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1106 bb64b798 2020-03-18 stsp goto done;
1107 bb64b798 2020-03-18 stsp }
1108 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1109 bb64b798 2020-03-18 stsp } else
1110 bb64b798 2020-03-18 stsp repo_path = dirname;
1111 bb64b798 2020-03-18 stsp
1112 39c64a6a 2020-03-18 stsp error = got_path_mkdir(repo_path);
1113 39c64a6a 2020-03-18 stsp if (error)
1114 bb64b798 2020-03-18 stsp goto done;
1115 bb64b798 2020-03-18 stsp
1116 39c64a6a 2020-03-18 stsp error = got_repo_init(repo_path);
1117 39c64a6a 2020-03-18 stsp if (error)
1118 bb64b798 2020-03-18 stsp goto done;
1119 bb64b798 2020-03-18 stsp
1120 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
1121 39c64a6a 2020-03-18 stsp if (error)
1122 294dfefd 2020-03-18 stsp goto done;
1123 294dfefd 2020-03-18 stsp
1124 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1125 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1126 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1127 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1128 ee448f5f 2020-03-18 stsp goto done;
1129 ee448f5f 2020-03-18 stsp }
1130 ee448f5f 2020-03-18 stsp }
1131 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1132 ee448f5f 2020-03-18 stsp if (error)
1133 ee448f5f 2020-03-18 stsp goto done;
1134 ee448f5f 2020-03-18 stsp
1135 39c64a6a 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1136 39c64a6a 2020-03-18 stsp if (error)
1137 bb64b798 2020-03-18 stsp goto done;
1138 294dfefd 2020-03-18 stsp
1139 294dfefd 2020-03-18 stsp printf("Connected to %s:%s\n", host, port);
1140 bb64b798 2020-03-18 stsp
1141 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1142 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1143 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1144 39c64a6a 2020-03-18 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1145 892ac3b6 2020-03-18 stsp repo, fetch_progress, &fpa);
1146 39c64a6a 2020-03-18 stsp if (error)
1147 d9b4d0c0 2020-03-18 stsp goto done;
1148 d9b4d0c0 2020-03-18 stsp
1149 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1150 39c64a6a 2020-03-18 stsp if (error)
1151 d9b4d0c0 2020-03-18 stsp goto done;
1152 d9b4d0c0 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1153 d9b4d0c0 2020-03-18 stsp free(id_str);
1154 d9b4d0c0 2020-03-18 stsp
1155 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1156 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1157 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1158 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1159 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1160 d9b4d0c0 2020-03-18 stsp
1161 668a20f6 2020-03-18 stsp
1162 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1163 39c64a6a 2020-03-18 stsp if (error)
1164 d9b4d0c0 2020-03-18 stsp goto done;
1165 d9b4d0c0 2020-03-18 stsp
1166 668a20f6 2020-03-18 stsp #if 0
1167 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, id);
1168 39c64a6a 2020-03-18 stsp if (error)
1169 d9b4d0c0 2020-03-18 stsp goto done;
1170 d9b4d0c0 2020-03-18 stsp printf("%s: %s\n", got_ref_get_name(ref), id_str);
1171 d9b4d0c0 2020-03-18 stsp free(id_str);
1172 668a20f6 2020-03-18 stsp #endif
1173 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1174 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1175 39c64a6a 2020-03-18 stsp if (error)
1176 d9b4d0c0 2020-03-18 stsp goto done;
1177 d9b4d0c0 2020-03-18 stsp }
1178 d9b4d0c0 2020-03-18 stsp
1179 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1180 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1181 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1182 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1183 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1184 d9b4d0c0 2020-03-18 stsp
1185 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1186 d9b4d0c0 2020-03-18 stsp continue;
1187 d9b4d0c0 2020-03-18 stsp
1188 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1189 39c64a6a 2020-03-18 stsp if (error) {
1190 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1191 d9b4d0c0 2020-03-18 stsp continue;
1192 d9b4d0c0 2020-03-18 stsp goto done;
1193 d9b4d0c0 2020-03-18 stsp }
1194 d9b4d0c0 2020-03-18 stsp
1195 39c64a6a 2020-03-18 stsp error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1196 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1197 39c64a6a 2020-03-18 stsp if (error)
1198 d9b4d0c0 2020-03-18 stsp goto done;
1199 d9b4d0c0 2020-03-18 stsp
1200 d9b4d0c0 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1201 d9b4d0c0 2020-03-18 stsp got_ref_get_symref_target(symref));
1202 d9b4d0c0 2020-03-18 stsp
1203 39c64a6a 2020-03-18 stsp error = got_ref_write(symref, repo);
1204 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1205 d9b4d0c0 2020-03-18 stsp break;
1206 d9b4d0c0 2020-03-18 stsp }
1207 d9b4d0c0 2020-03-18 stsp
1208 09838ffc 2020-03-18 stsp done:
1209 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1210 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1211 bb64b798 2020-03-18 stsp if (repo)
1212 bb64b798 2020-03-18 stsp got_repo_close(repo);
1213 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1214 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1215 d9b4d0c0 2020-03-18 stsp free(pe->data);
1216 d9b4d0c0 2020-03-18 stsp }
1217 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1218 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1219 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1220 d9b4d0c0 2020-03-18 stsp free(pe->data);
1221 d9b4d0c0 2020-03-18 stsp }
1222 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1223 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1224 09838ffc 2020-03-18 stsp free(proto);
1225 09838ffc 2020-03-18 stsp free(host);
1226 09838ffc 2020-03-18 stsp free(port);
1227 09838ffc 2020-03-18 stsp free(server_path);
1228 09838ffc 2020-03-18 stsp free(repo_name);
1229 bb64b798 2020-03-18 stsp free(default_destdir);
1230 39c64a6a 2020-03-18 stsp return error;
1231 a367ff0f 2019-05-14 stsp }
1232 8069f636 2019-01-12 stsp
1233 4ed7e80c 2018-05-20 stsp static const struct got_error *
1234 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1235 4b6c9460 2020-03-05 stsp {
1236 4b6c9460 2020-03-05 stsp static char msg[512];
1237 4b6c9460 2020-03-05 stsp const char *branch_name;
1238 4b6c9460 2020-03-05 stsp
1239 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1240 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1241 4b6c9460 2020-03-05 stsp else
1242 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1243 4b6c9460 2020-03-05 stsp
1244 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1245 4b6c9460 2020-03-05 stsp branch_name += 11;
1246 4b6c9460 2020-03-05 stsp
1247 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1248 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1249 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1250 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1251 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1252 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1253 4b6c9460 2020-03-05 stsp
1254 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1255 4b6c9460 2020-03-05 stsp }
1256 4b6c9460 2020-03-05 stsp
1257 4b6c9460 2020-03-05 stsp static const struct got_error *
1258 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1259 c09a553d 2018-03-12 stsp {
1260 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1261 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1262 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1263 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1264 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1265 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1266 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1267 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1268 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1269 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1270 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1271 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1272 f2ea84fa 2019-07-27 stsp
1273 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1274 c09a553d 2018-03-12 stsp
1275 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1276 0bb8a95e 2018-03-12 stsp switch (ch) {
1277 08573d5b 2019-05-14 stsp case 'b':
1278 08573d5b 2019-05-14 stsp branch_name = optarg;
1279 08573d5b 2019-05-14 stsp break;
1280 8069f636 2019-01-12 stsp case 'c':
1281 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1282 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1283 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1284 bb51a5b4 2020-01-13 stsp break;
1285 bb51a5b4 2020-01-13 stsp case 'E':
1286 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1287 8069f636 2019-01-12 stsp break;
1288 0bb8a95e 2018-03-12 stsp case 'p':
1289 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1290 0bb8a95e 2018-03-12 stsp break;
1291 0bb8a95e 2018-03-12 stsp default:
1292 2deda0b9 2019-03-07 stsp usage_checkout();
1293 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1294 0bb8a95e 2018-03-12 stsp }
1295 0bb8a95e 2018-03-12 stsp }
1296 0bb8a95e 2018-03-12 stsp
1297 0bb8a95e 2018-03-12 stsp argc -= optind;
1298 0bb8a95e 2018-03-12 stsp argv += optind;
1299 0bb8a95e 2018-03-12 stsp
1300 6715a751 2018-03-16 stsp #ifndef PROFILE
1301 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1302 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1303 c09a553d 2018-03-12 stsp err(1, "pledge");
1304 6715a751 2018-03-16 stsp #endif
1305 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1306 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1307 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1308 76089277 2018-04-01 stsp if (repo_path == NULL)
1309 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1310 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1311 76089277 2018-04-01 stsp if (cwd == NULL) {
1312 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1313 76089277 2018-04-01 stsp goto done;
1314 76089277 2018-04-01 stsp }
1315 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1316 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1317 230a42bd 2019-05-11 jcs if (base == NULL) {
1318 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1319 230a42bd 2019-05-11 jcs path_prefix);
1320 230a42bd 2019-05-11 jcs goto done;
1321 230a42bd 2019-05-11 jcs }
1322 230a42bd 2019-05-11 jcs } else {
1323 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1324 230a42bd 2019-05-11 jcs if (base == NULL) {
1325 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1326 230a42bd 2019-05-11 jcs repo_path);
1327 230a42bd 2019-05-11 jcs goto done;
1328 230a42bd 2019-05-11 jcs }
1329 76089277 2018-04-01 stsp }
1330 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1331 c09a553d 2018-03-12 stsp if (dotgit)
1332 c09a553d 2018-03-12 stsp *dotgit = '\0';
1333 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1334 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1335 c09a553d 2018-03-12 stsp free(cwd);
1336 76089277 2018-04-01 stsp goto done;
1337 c09a553d 2018-03-12 stsp }
1338 c09a553d 2018-03-12 stsp free(cwd);
1339 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1340 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1341 76089277 2018-04-01 stsp if (repo_path == NULL) {
1342 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1343 76089277 2018-04-01 stsp goto done;
1344 76089277 2018-04-01 stsp }
1345 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1346 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1347 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1348 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1349 b4b3a7dd 2019-07-22 stsp argv[1]);
1350 b4b3a7dd 2019-07-22 stsp goto done;
1351 b4b3a7dd 2019-07-22 stsp }
1352 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1353 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1354 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1355 b4b3a7dd 2019-07-22 stsp goto done;
1356 b4b3a7dd 2019-07-22 stsp }
1357 76089277 2018-04-01 stsp }
1358 c09a553d 2018-03-12 stsp } else
1359 c09a553d 2018-03-12 stsp usage_checkout();
1360 c09a553d 2018-03-12 stsp
1361 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1362 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1363 13bfb272 2019-05-10 jcs
1364 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1365 c09a553d 2018-03-12 stsp if (error != NULL)
1366 c02c541e 2019-03-29 stsp goto done;
1367 c02c541e 2019-03-29 stsp
1368 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1369 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1370 c530dc23 2019-07-23 stsp if (error) {
1371 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1372 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1373 c530dc23 2019-07-23 stsp goto done;
1374 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1375 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1376 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1377 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1378 c530dc23 2019-07-23 stsp goto done;
1379 c530dc23 2019-07-23 stsp }
1380 c530dc23 2019-07-23 stsp }
1381 c530dc23 2019-07-23 stsp
1382 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1383 c02c541e 2019-03-29 stsp if (error)
1384 c09a553d 2018-03-12 stsp goto done;
1385 8069f636 2019-01-12 stsp
1386 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1387 c09a553d 2018-03-12 stsp if (error != NULL)
1388 c09a553d 2018-03-12 stsp goto done;
1389 c09a553d 2018-03-12 stsp
1390 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1391 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1392 c09a553d 2018-03-12 stsp goto done;
1393 c09a553d 2018-03-12 stsp
1394 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1395 c09a553d 2018-03-12 stsp if (error != NULL)
1396 c09a553d 2018-03-12 stsp goto done;
1397 c09a553d 2018-03-12 stsp
1398 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1399 e5dc7198 2018-12-29 stsp path_prefix);
1400 e5dc7198 2018-12-29 stsp if (error != NULL)
1401 e5dc7198 2018-12-29 stsp goto done;
1402 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1403 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1404 49520a32 2018-12-29 stsp goto done;
1405 49520a32 2018-12-29 stsp }
1406 49520a32 2018-12-29 stsp
1407 8069f636 2019-01-12 stsp if (commit_id_str) {
1408 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1409 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1410 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1411 30837e32 2019-07-25 stsp if (error)
1412 8069f636 2019-01-12 stsp goto done;
1413 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1414 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1415 8069f636 2019-01-12 stsp if (error != NULL) {
1416 8069f636 2019-01-12 stsp free(commit_id);
1417 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1418 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1419 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1420 4b6c9460 2020-03-05 stsp }
1421 8069f636 2019-01-12 stsp goto done;
1422 8069f636 2019-01-12 stsp }
1423 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1424 4b6c9460 2020-03-05 stsp if (error) {
1425 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1426 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1427 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1428 4b6c9460 2020-03-05 stsp }
1429 45d344f6 2019-05-14 stsp goto done;
1430 4b6c9460 2020-03-05 stsp }
1431 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1432 8069f636 2019-01-12 stsp commit_id);
1433 8069f636 2019-01-12 stsp free(commit_id);
1434 8069f636 2019-01-12 stsp if (error)
1435 8069f636 2019-01-12 stsp goto done;
1436 8069f636 2019-01-12 stsp }
1437 8069f636 2019-01-12 stsp
1438 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1439 f2ea84fa 2019-07-27 stsp if (error)
1440 f2ea84fa 2019-07-27 stsp goto done;
1441 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1442 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1443 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1444 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1445 c09a553d 2018-03-12 stsp if (error != NULL)
1446 c09a553d 2018-03-12 stsp goto done;
1447 c09a553d 2018-03-12 stsp
1448 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1449 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1450 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1451 c09a553d 2018-03-12 stsp done:
1452 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1453 8069f636 2019-01-12 stsp free(commit_id_str);
1454 76089277 2018-04-01 stsp free(repo_path);
1455 507dc3bb 2018-12-29 stsp free(worktree_path);
1456 507dc3bb 2018-12-29 stsp return error;
1457 507dc3bb 2018-12-29 stsp }
1458 507dc3bb 2018-12-29 stsp
1459 507dc3bb 2018-12-29 stsp __dead static void
1460 507dc3bb 2018-12-29 stsp usage_update(void)
1461 507dc3bb 2018-12-29 stsp {
1462 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1463 507dc3bb 2018-12-29 stsp getprogname());
1464 507dc3bb 2018-12-29 stsp exit(1);
1465 507dc3bb 2018-12-29 stsp }
1466 507dc3bb 2018-12-29 stsp
1467 1ee397ad 2019-07-12 stsp static const struct got_error *
1468 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1469 507dc3bb 2018-12-29 stsp {
1470 784955db 2019-01-12 stsp int *did_something = arg;
1471 784955db 2019-01-12 stsp
1472 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1473 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1474 1ee397ad 2019-07-12 stsp return NULL;
1475 507dc3bb 2018-12-29 stsp
1476 1545c615 2019-02-10 stsp *did_something = 1;
1477 a484d721 2019-06-10 stsp
1478 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1479 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1480 1ee397ad 2019-07-12 stsp return NULL;
1481 a484d721 2019-06-10 stsp
1482 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1483 507dc3bb 2018-12-29 stsp path++;
1484 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1485 1ee397ad 2019-07-12 stsp return NULL;
1486 be7061eb 2018-12-30 stsp }
1487 be7061eb 2018-12-30 stsp
1488 be7061eb 2018-12-30 stsp static const struct got_error *
1489 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1490 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1491 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1492 a1fb16d8 2019-05-24 stsp {
1493 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1494 a1fb16d8 2019-05-24 stsp char *base_id_str;
1495 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1496 a1fb16d8 2019-05-24 stsp
1497 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1498 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1499 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1500 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1501 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1502 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1503 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1504 a1fb16d8 2019-05-24 stsp }
1505 a1fb16d8 2019-05-24 stsp
1506 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1507 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1508 a1fb16d8 2019-05-24 stsp if (err) {
1509 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1510 a1fb16d8 2019-05-24 stsp return err;
1511 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1512 a1fb16d8 2019-05-24 stsp }
1513 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1514 a1fb16d8 2019-05-24 stsp return NULL;
1515 a1fb16d8 2019-05-24 stsp
1516 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1517 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1518 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1519 a1fb16d8 2019-05-24 stsp if (err)
1520 a1fb16d8 2019-05-24 stsp return err;
1521 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1522 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1523 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1524 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1525 0ebf8283 2019-07-24 stsp return NULL;
1526 0ebf8283 2019-07-24 stsp }
1527 0ebf8283 2019-07-24 stsp
1528 0ebf8283 2019-07-24 stsp static const struct got_error *
1529 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1530 0ebf8283 2019-07-24 stsp {
1531 0ebf8283 2019-07-24 stsp const struct got_error *err;
1532 0ebf8283 2019-07-24 stsp int in_progress;
1533 0ebf8283 2019-07-24 stsp
1534 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1535 0ebf8283 2019-07-24 stsp if (err)
1536 0ebf8283 2019-07-24 stsp return err;
1537 0ebf8283 2019-07-24 stsp if (in_progress)
1538 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1539 0ebf8283 2019-07-24 stsp
1540 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1541 0ebf8283 2019-07-24 stsp if (err)
1542 0ebf8283 2019-07-24 stsp return err;
1543 0ebf8283 2019-07-24 stsp if (in_progress)
1544 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1545 0ebf8283 2019-07-24 stsp
1546 a1fb16d8 2019-05-24 stsp return NULL;
1547 a5edda0a 2019-07-27 stsp }
1548 a5edda0a 2019-07-27 stsp
1549 a5edda0a 2019-07-27 stsp static const struct got_error *
1550 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1551 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1552 a5edda0a 2019-07-27 stsp {
1553 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1554 a5edda0a 2019-07-27 stsp char *path;
1555 a5edda0a 2019-07-27 stsp int i;
1556 a5edda0a 2019-07-27 stsp
1557 a5edda0a 2019-07-27 stsp if (argc == 0) {
1558 a5edda0a 2019-07-27 stsp path = strdup("");
1559 a5edda0a 2019-07-27 stsp if (path == NULL)
1560 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1561 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1562 a5edda0a 2019-07-27 stsp }
1563 a5edda0a 2019-07-27 stsp
1564 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1565 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1566 a5edda0a 2019-07-27 stsp if (err)
1567 a5edda0a 2019-07-27 stsp break;
1568 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1569 a5edda0a 2019-07-27 stsp if (err) {
1570 a5edda0a 2019-07-27 stsp free(path);
1571 a5edda0a 2019-07-27 stsp break;
1572 a5edda0a 2019-07-27 stsp }
1573 a5edda0a 2019-07-27 stsp }
1574 a5edda0a 2019-07-27 stsp
1575 a5edda0a 2019-07-27 stsp return err;
1576 a1fb16d8 2019-05-24 stsp }
1577 a1fb16d8 2019-05-24 stsp
1578 a1fb16d8 2019-05-24 stsp static const struct got_error *
1579 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1580 507dc3bb 2018-12-29 stsp {
1581 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1582 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1583 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1584 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1585 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1586 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1587 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1588 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1589 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1590 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1591 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1592 507dc3bb 2018-12-29 stsp
1593 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1594 f2ea84fa 2019-07-27 stsp
1595 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1596 507dc3bb 2018-12-29 stsp switch (ch) {
1597 024e9686 2019-05-14 stsp case 'b':
1598 024e9686 2019-05-14 stsp branch_name = optarg;
1599 024e9686 2019-05-14 stsp break;
1600 507dc3bb 2018-12-29 stsp case 'c':
1601 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1602 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1603 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1604 507dc3bb 2018-12-29 stsp break;
1605 507dc3bb 2018-12-29 stsp default:
1606 2deda0b9 2019-03-07 stsp usage_update();
1607 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1608 507dc3bb 2018-12-29 stsp }
1609 507dc3bb 2018-12-29 stsp }
1610 507dc3bb 2018-12-29 stsp
1611 507dc3bb 2018-12-29 stsp argc -= optind;
1612 507dc3bb 2018-12-29 stsp argv += optind;
1613 507dc3bb 2018-12-29 stsp
1614 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1615 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1616 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1617 507dc3bb 2018-12-29 stsp err(1, "pledge");
1618 507dc3bb 2018-12-29 stsp #endif
1619 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1620 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1621 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1622 c4cdcb68 2019-04-03 stsp goto done;
1623 c4cdcb68 2019-04-03 stsp }
1624 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1625 7d5807f4 2019-07-11 stsp if (error)
1626 7d5807f4 2019-07-11 stsp goto done;
1627 7d5807f4 2019-07-11 stsp
1628 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1629 f2ea84fa 2019-07-27 stsp if (error)
1630 f2ea84fa 2019-07-27 stsp goto done;
1631 507dc3bb 2018-12-29 stsp
1632 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1633 c9956ddf 2019-09-08 stsp NULL);
1634 507dc3bb 2018-12-29 stsp if (error != NULL)
1635 507dc3bb 2018-12-29 stsp goto done;
1636 507dc3bb 2018-12-29 stsp
1637 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1638 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1639 087fb88c 2019-08-04 stsp if (error)
1640 087fb88c 2019-08-04 stsp goto done;
1641 087fb88c 2019-08-04 stsp
1642 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1643 0266afb7 2019-01-04 stsp if (error)
1644 0266afb7 2019-01-04 stsp goto done;
1645 0266afb7 2019-01-04 stsp
1646 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1647 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1648 024e9686 2019-05-14 stsp if (error != NULL)
1649 024e9686 2019-05-14 stsp goto done;
1650 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1651 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1652 9c4b8182 2019-01-02 stsp if (error != NULL)
1653 9c4b8182 2019-01-02 stsp goto done;
1654 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1655 507dc3bb 2018-12-29 stsp if (error != NULL)
1656 507dc3bb 2018-12-29 stsp goto done;
1657 507dc3bb 2018-12-29 stsp } else {
1658 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1659 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1660 04f57cb3 2019-07-25 stsp free(commit_id_str);
1661 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1662 30837e32 2019-07-25 stsp if (error)
1663 a297e751 2019-07-11 stsp goto done;
1664 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1665 a297e751 2019-07-11 stsp if (error)
1666 507dc3bb 2018-12-29 stsp goto done;
1667 507dc3bb 2018-12-29 stsp }
1668 35c965b2 2018-12-29 stsp
1669 a1fb16d8 2019-05-24 stsp if (branch_name) {
1670 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1671 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1672 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1673 f2ea84fa 2019-07-27 stsp continue;
1674 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1675 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1676 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1677 024e9686 2019-05-14 stsp goto done;
1678 024e9686 2019-05-14 stsp }
1679 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1680 024e9686 2019-05-14 stsp if (error)
1681 024e9686 2019-05-14 stsp goto done;
1682 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1683 3aef623b 2019-10-15 stsp repo);
1684 a367ff0f 2019-05-14 stsp free(head_commit_id);
1685 024e9686 2019-05-14 stsp if (error != NULL)
1686 024e9686 2019-05-14 stsp goto done;
1687 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1688 a367ff0f 2019-05-14 stsp if (error)
1689 a367ff0f 2019-05-14 stsp goto done;
1690 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1691 024e9686 2019-05-14 stsp if (error)
1692 024e9686 2019-05-14 stsp goto done;
1693 024e9686 2019-05-14 stsp } else {
1694 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1695 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1696 a367ff0f 2019-05-14 stsp if (error != NULL) {
1697 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1698 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1699 024e9686 2019-05-14 stsp goto done;
1700 a367ff0f 2019-05-14 stsp }
1701 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1702 a367ff0f 2019-05-14 stsp if (error)
1703 a367ff0f 2019-05-14 stsp goto done;
1704 024e9686 2019-05-14 stsp }
1705 507dc3bb 2018-12-29 stsp
1706 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1707 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1708 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1709 507dc3bb 2018-12-29 stsp commit_id);
1710 507dc3bb 2018-12-29 stsp if (error)
1711 507dc3bb 2018-12-29 stsp goto done;
1712 507dc3bb 2018-12-29 stsp }
1713 507dc3bb 2018-12-29 stsp
1714 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1715 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1716 507dc3bb 2018-12-29 stsp if (error != NULL)
1717 507dc3bb 2018-12-29 stsp goto done;
1718 9c4b8182 2019-01-02 stsp
1719 784955db 2019-01-12 stsp if (did_something)
1720 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1721 784955db 2019-01-12 stsp else
1722 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1723 507dc3bb 2018-12-29 stsp done:
1724 c09a553d 2018-03-12 stsp free(worktree_path);
1725 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1726 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1727 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1728 507dc3bb 2018-12-29 stsp free(commit_id);
1729 9c4b8182 2019-01-02 stsp free(commit_id_str);
1730 c09a553d 2018-03-12 stsp return error;
1731 c09a553d 2018-03-12 stsp }
1732 c09a553d 2018-03-12 stsp
1733 f42b1b34 2018-03-12 stsp static const struct got_error *
1734 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1735 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1736 63035f9f 2019-10-06 stsp struct got_repository *repo)
1737 5c860e29 2018-03-12 stsp {
1738 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1739 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1740 79109fed 2018-03-27 stsp
1741 44392932 2019-08-25 stsp if (blob_id1) {
1742 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1743 44392932 2019-08-25 stsp if (err)
1744 44392932 2019-08-25 stsp goto done;
1745 44392932 2019-08-25 stsp }
1746 79109fed 2018-03-27 stsp
1747 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1748 44392932 2019-08-25 stsp if (err)
1749 44392932 2019-08-25 stsp goto done;
1750 79109fed 2018-03-27 stsp
1751 44392932 2019-08-25 stsp while (path[0] == '/')
1752 44392932 2019-08-25 stsp path++;
1753 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1754 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1755 44392932 2019-08-25 stsp done:
1756 44392932 2019-08-25 stsp if (blob1)
1757 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1758 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1759 44392932 2019-08-25 stsp return err;
1760 44392932 2019-08-25 stsp }
1761 44392932 2019-08-25 stsp
1762 44392932 2019-08-25 stsp static const struct got_error *
1763 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1764 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1765 63035f9f 2019-10-06 stsp struct got_repository *repo)
1766 44392932 2019-08-25 stsp {
1767 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1768 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1769 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1770 44392932 2019-08-25 stsp
1771 44392932 2019-08-25 stsp if (tree_id1) {
1772 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1773 79109fed 2018-03-27 stsp if (err)
1774 44392932 2019-08-25 stsp goto done;
1775 79109fed 2018-03-27 stsp }
1776 79109fed 2018-03-27 stsp
1777 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1778 0f2b3dca 2018-12-22 stsp if (err)
1779 0f2b3dca 2018-12-22 stsp goto done;
1780 0f2b3dca 2018-12-22 stsp
1781 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1782 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1783 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1784 44392932 2019-08-25 stsp while (path[0] == '/')
1785 44392932 2019-08-25 stsp path++;
1786 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1787 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1788 0f2b3dca 2018-12-22 stsp done:
1789 79109fed 2018-03-27 stsp if (tree1)
1790 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1791 366e0a5f 2019-10-10 stsp if (tree2)
1792 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1793 44392932 2019-08-25 stsp return err;
1794 44392932 2019-08-25 stsp }
1795 44392932 2019-08-25 stsp
1796 44392932 2019-08-25 stsp static const struct got_error *
1797 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1798 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1799 44392932 2019-08-25 stsp {
1800 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1801 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1802 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1803 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1804 44392932 2019-08-25 stsp struct got_object_qid *qid;
1805 44392932 2019-08-25 stsp
1806 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1807 44392932 2019-08-25 stsp if (qid != NULL) {
1808 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1809 44392932 2019-08-25 stsp qid->id);
1810 44392932 2019-08-25 stsp if (err)
1811 44392932 2019-08-25 stsp return err;
1812 44392932 2019-08-25 stsp }
1813 44392932 2019-08-25 stsp
1814 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1815 44392932 2019-08-25 stsp int obj_type;
1816 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1817 44392932 2019-08-25 stsp if (err)
1818 44392932 2019-08-25 stsp goto done;
1819 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1820 44392932 2019-08-25 stsp if (err) {
1821 44392932 2019-08-25 stsp free(obj_id2);
1822 44392932 2019-08-25 stsp goto done;
1823 44392932 2019-08-25 stsp }
1824 44392932 2019-08-25 stsp if (pcommit) {
1825 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1826 44392932 2019-08-25 stsp qid->id, path);
1827 44392932 2019-08-25 stsp if (err) {
1828 44392932 2019-08-25 stsp free(obj_id2);
1829 44392932 2019-08-25 stsp goto done;
1830 44392932 2019-08-25 stsp }
1831 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1832 44392932 2019-08-25 stsp if (err) {
1833 44392932 2019-08-25 stsp free(obj_id2);
1834 44392932 2019-08-25 stsp goto done;
1835 44392932 2019-08-25 stsp }
1836 44392932 2019-08-25 stsp }
1837 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1838 44392932 2019-08-25 stsp if (err) {
1839 44392932 2019-08-25 stsp free(obj_id2);
1840 44392932 2019-08-25 stsp goto done;
1841 44392932 2019-08-25 stsp }
1842 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1843 44392932 2019-08-25 stsp switch (obj_type) {
1844 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1845 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1846 63035f9f 2019-10-06 stsp 0, repo);
1847 44392932 2019-08-25 stsp break;
1848 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1849 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1850 63035f9f 2019-10-06 stsp 0, repo);
1851 44392932 2019-08-25 stsp break;
1852 44392932 2019-08-25 stsp default:
1853 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1854 44392932 2019-08-25 stsp break;
1855 44392932 2019-08-25 stsp }
1856 44392932 2019-08-25 stsp free(obj_id1);
1857 44392932 2019-08-25 stsp free(obj_id2);
1858 44392932 2019-08-25 stsp } else {
1859 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1860 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1861 44392932 2019-08-25 stsp if (err)
1862 44392932 2019-08-25 stsp goto done;
1863 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1864 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1865 44392932 2019-08-25 stsp if (err)
1866 44392932 2019-08-25 stsp goto done;
1867 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1868 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1869 44392932 2019-08-25 stsp }
1870 44392932 2019-08-25 stsp done:
1871 0f2b3dca 2018-12-22 stsp free(id_str1);
1872 0f2b3dca 2018-12-22 stsp free(id_str2);
1873 44392932 2019-08-25 stsp if (pcommit)
1874 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1875 79109fed 2018-03-27 stsp return err;
1876 79109fed 2018-03-27 stsp }
1877 79109fed 2018-03-27 stsp
1878 4bb494d5 2018-06-16 stsp static char *
1879 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1880 6c281f94 2018-06-11 stsp {
1881 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1882 09867e48 2019-08-13 stsp char *p, *s;
1883 09867e48 2019-08-13 stsp
1884 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1885 09867e48 2019-08-13 stsp if (tm == NULL)
1886 09867e48 2019-08-13 stsp return NULL;
1887 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1888 09867e48 2019-08-13 stsp if (s == NULL)
1889 09867e48 2019-08-13 stsp return NULL;
1890 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1891 6c281f94 2018-06-11 stsp if (p)
1892 6c281f94 2018-06-11 stsp *p = '\0';
1893 6c281f94 2018-06-11 stsp return s;
1894 6c281f94 2018-06-11 stsp }
1895 dc424a06 2019-08-07 stsp
1896 6841bf13 2019-11-29 kn static const struct got_error *
1897 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1898 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1899 6841bf13 2019-11-29 kn {
1900 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1901 6841bf13 2019-11-29 kn regmatch_t regmatch;
1902 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1903 6841bf13 2019-11-29 kn
1904 6841bf13 2019-11-29 kn *have_match = 0;
1905 6841bf13 2019-11-29 kn
1906 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1907 6841bf13 2019-11-29 kn if (err)
1908 6841bf13 2019-11-29 kn return err;
1909 6841bf13 2019-11-29 kn
1910 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1911 6841bf13 2019-11-29 kn if (err)
1912 6841bf13 2019-11-29 kn goto done;
1913 6841bf13 2019-11-29 kn
1914 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1915 6841bf13 2019-11-29 kn *have_match = 1;
1916 6841bf13 2019-11-29 kn done:
1917 6841bf13 2019-11-29 kn free(id_str);
1918 6841bf13 2019-11-29 kn free(logmsg);
1919 6841bf13 2019-11-29 kn return err;
1920 6841bf13 2019-11-29 kn }
1921 6841bf13 2019-11-29 kn
1922 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1923 6c281f94 2018-06-11 stsp
1924 79109fed 2018-03-27 stsp static const struct got_error *
1925 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1926 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1927 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1928 79109fed 2018-03-27 stsp {
1929 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1930 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1931 6c281f94 2018-06-11 stsp char datebuf[26];
1932 45d799e2 2018-12-23 stsp time_t committer_time;
1933 45d799e2 2018-12-23 stsp const char *author, *committer;
1934 199a4027 2019-02-02 stsp char *refs_str = NULL;
1935 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1936 5c860e29 2018-03-12 stsp
1937 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1938 199a4027 2019-02-02 stsp char *s;
1939 199a4027 2019-02-02 stsp const char *name;
1940 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1941 a436ad14 2019-08-13 stsp int cmp;
1942 a436ad14 2019-08-13 stsp
1943 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1944 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1945 d9498b20 2019-02-05 stsp continue;
1946 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1947 199a4027 2019-02-02 stsp name += 5;
1948 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1949 7143d404 2019-03-12 stsp continue;
1950 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1951 e34f9ed6 2019-02-02 stsp name += 6;
1952 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1953 141c2bff 2019-02-04 stsp name += 8;
1954 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1955 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1956 5d844a1e 2019-08-13 stsp if (err) {
1957 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1958 5d844a1e 2019-08-13 stsp return err;
1959 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1960 5d844a1e 2019-08-13 stsp err = NULL;
1961 5d844a1e 2019-08-13 stsp tag = NULL;
1962 5d844a1e 2019-08-13 stsp }
1963 a436ad14 2019-08-13 stsp }
1964 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1965 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1966 a436ad14 2019-08-13 stsp if (tag)
1967 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1968 a436ad14 2019-08-13 stsp if (cmp != 0)
1969 a436ad14 2019-08-13 stsp continue;
1970 199a4027 2019-02-02 stsp s = refs_str;
1971 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1972 199a4027 2019-02-02 stsp name) == -1) {
1973 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1974 199a4027 2019-02-02 stsp free(s);
1975 34ca4898 2019-08-13 stsp return err;
1976 199a4027 2019-02-02 stsp }
1977 199a4027 2019-02-02 stsp free(s);
1978 199a4027 2019-02-02 stsp }
1979 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1980 8bf5b3c9 2018-03-17 stsp if (err)
1981 8bf5b3c9 2018-03-17 stsp return err;
1982 788c352e 2018-06-16 stsp
1983 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1984 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1985 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1986 832c249c 2018-06-10 stsp free(id_str);
1987 d3d493d7 2019-02-21 stsp id_str = NULL;
1988 d3d493d7 2019-02-21 stsp free(refs_str);
1989 d3d493d7 2019-02-21 stsp refs_str = NULL;
1990 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1991 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1992 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1993 09867e48 2019-08-13 stsp if (datestr)
1994 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1995 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1996 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1997 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1998 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1999 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2000 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2001 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2002 3fe1abad 2018-06-10 stsp int n = 1;
2003 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2004 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2005 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2006 a0603db2 2018-06-10 stsp if (err)
2007 a0603db2 2018-06-10 stsp return err;
2008 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2009 a0603db2 2018-06-10 stsp free(id_str);
2010 a0603db2 2018-06-10 stsp }
2011 a0603db2 2018-06-10 stsp }
2012 832c249c 2018-06-10 stsp
2013 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2014 5943eee2 2019-08-13 stsp if (err)
2015 5943eee2 2019-08-13 stsp return err;
2016 8bf5b3c9 2018-03-17 stsp
2017 621015ac 2018-07-23 stsp logmsg = logmsg0;
2018 832c249c 2018-06-10 stsp do {
2019 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2020 832c249c 2018-06-10 stsp if (line)
2021 832c249c 2018-06-10 stsp printf(" %s\n", line);
2022 832c249c 2018-06-10 stsp } while (line);
2023 621015ac 2018-07-23 stsp free(logmsg0);
2024 832c249c 2018-06-10 stsp
2025 971751ac 2018-03-27 stsp if (show_patch) {
2026 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2027 971751ac 2018-03-27 stsp if (err == 0)
2028 971751ac 2018-03-27 stsp printf("\n");
2029 971751ac 2018-03-27 stsp }
2030 07862c20 2018-09-15 stsp
2031 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2032 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2033 07862c20 2018-09-15 stsp return err;
2034 f42b1b34 2018-03-12 stsp }
2035 5c860e29 2018-03-12 stsp
2036 f42b1b34 2018-03-12 stsp static const struct got_error *
2037 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2038 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2039 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2040 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2041 f42b1b34 2018-03-12 stsp {
2042 f42b1b34 2018-03-12 stsp const struct got_error *err;
2043 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2044 6841bf13 2019-11-29 kn regex_t regex;
2045 6841bf13 2019-11-29 kn int have_match;
2046 372ccdbb 2018-06-10 stsp
2047 6841bf13 2019-11-29 kn if (search_pattern &&
2048 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2049 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2050 6841bf13 2019-11-29 kn
2051 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2052 f42b1b34 2018-03-12 stsp if (err)
2053 f42b1b34 2018-03-12 stsp return err;
2054 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2055 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2056 372ccdbb 2018-06-10 stsp if (err)
2057 fcc85cad 2018-07-22 stsp goto done;
2058 656b1f76 2019-05-11 jcs for (;;) {
2059 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2060 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2061 8bf5b3c9 2018-03-17 stsp
2062 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2063 84453469 2018-11-11 stsp break;
2064 84453469 2018-11-11 stsp
2065 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2066 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2067 372ccdbb 2018-06-10 stsp if (err) {
2068 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2069 9ba79e04 2018-06-11 stsp err = NULL;
2070 ee780d5c 2020-01-04 stsp break;
2071 7e665116 2018-04-02 stsp }
2072 b43fbaa0 2018-06-11 stsp if (id == NULL)
2073 7e665116 2018-04-02 stsp break;
2074 b43fbaa0 2018-06-11 stsp
2075 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2076 b43fbaa0 2018-06-11 stsp if (err)
2077 fcc85cad 2018-07-22 stsp break;
2078 6841bf13 2019-11-29 kn
2079 6841bf13 2019-11-29 kn if (search_pattern) {
2080 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2081 6841bf13 2019-11-29 kn if (err) {
2082 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2083 6841bf13 2019-11-29 kn break;
2084 6841bf13 2019-11-29 kn }
2085 6841bf13 2019-11-29 kn if (have_match == 0) {
2086 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2087 6841bf13 2019-11-29 kn continue;
2088 6841bf13 2019-11-29 kn }
2089 6841bf13 2019-11-29 kn }
2090 6841bf13 2019-11-29 kn
2091 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2092 44392932 2019-08-25 stsp diff_context, refs);
2093 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2094 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2095 7e665116 2018-04-02 stsp break;
2096 31cedeaf 2018-09-15 stsp }
2097 fcc85cad 2018-07-22 stsp done:
2098 6841bf13 2019-11-29 kn if (search_pattern)
2099 6841bf13 2019-11-29 kn regfree(&regex);
2100 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2101 f42b1b34 2018-03-12 stsp return err;
2102 f42b1b34 2018-03-12 stsp }
2103 5c860e29 2018-03-12 stsp
2104 4ed7e80c 2018-05-20 stsp __dead static void
2105 6f3d1eb0 2018-03-12 stsp usage_log(void)
2106 6f3d1eb0 2018-03-12 stsp {
2107 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2108 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2109 6f3d1eb0 2018-03-12 stsp exit(1);
2110 b1ebc001 2019-08-13 stsp }
2111 b1ebc001 2019-08-13 stsp
2112 b1ebc001 2019-08-13 stsp static int
2113 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2114 b1ebc001 2019-08-13 stsp {
2115 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2116 b1ebc001 2019-08-13 stsp long long n;
2117 b1ebc001 2019-08-13 stsp const char *errstr;
2118 b1ebc001 2019-08-13 stsp
2119 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2120 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2121 b1ebc001 2019-08-13 stsp return 0;
2122 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2123 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2124 b1ebc001 2019-08-13 stsp return 0;
2125 b1ebc001 2019-08-13 stsp return n;
2126 6f3d1eb0 2018-03-12 stsp }
2127 6f3d1eb0 2018-03-12 stsp
2128 4ed7e80c 2018-05-20 stsp static const struct got_error *
2129 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2130 f42b1b34 2018-03-12 stsp {
2131 f42b1b34 2018-03-12 stsp const struct got_error *error;
2132 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2133 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2134 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2135 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2136 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2137 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2138 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2139 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2140 64a96a6d 2018-04-01 stsp const char *errstr;
2141 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2142 1b3893a2 2019-03-18 stsp
2143 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2144 5c860e29 2018-03-12 stsp
2145 6715a751 2018-03-16 stsp #ifndef PROFILE
2146 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2147 6098196c 2019-01-04 stsp NULL)
2148 ad242220 2018-09-08 stsp == -1)
2149 f42b1b34 2018-03-12 stsp err(1, "pledge");
2150 6715a751 2018-03-16 stsp #endif
2151 79109fed 2018-03-27 stsp
2152 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2153 b1ebc001 2019-08-13 stsp
2154 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2155 79109fed 2018-03-27 stsp switch (ch) {
2156 79109fed 2018-03-27 stsp case 'p':
2157 79109fed 2018-03-27 stsp show_patch = 1;
2158 d142fc45 2018-04-01 stsp break;
2159 d142fc45 2018-04-01 stsp case 'c':
2160 d142fc45 2018-04-01 stsp start_commit = optarg;
2161 64a96a6d 2018-04-01 stsp break;
2162 c0cc5c62 2018-10-18 stsp case 'C':
2163 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2164 4a8520aa 2018-10-18 stsp &errstr);
2165 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2166 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2167 c0cc5c62 2018-10-18 stsp break;
2168 64a96a6d 2018-04-01 stsp case 'l':
2169 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2170 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2171 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2172 cc54c501 2019-07-15 stsp break;
2173 48c8c60d 2020-01-27 stsp case 'b':
2174 48c8c60d 2020-01-27 stsp log_branches = 1;
2175 a0603db2 2018-06-10 stsp break;
2176 04ca23f4 2018-07-16 stsp case 'r':
2177 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2178 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2179 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2180 9ba1d308 2019-10-21 stsp optarg);
2181 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2182 04ca23f4 2018-07-16 stsp break;
2183 6841bf13 2019-11-29 kn case 's':
2184 6841bf13 2019-11-29 kn search_pattern = optarg;
2185 6841bf13 2019-11-29 kn break;
2186 79109fed 2018-03-27 stsp default:
2187 2deda0b9 2019-03-07 stsp usage_log();
2188 79109fed 2018-03-27 stsp /* NOTREACHED */
2189 79109fed 2018-03-27 stsp }
2190 79109fed 2018-03-27 stsp }
2191 79109fed 2018-03-27 stsp
2192 79109fed 2018-03-27 stsp argc -= optind;
2193 79109fed 2018-03-27 stsp argv += optind;
2194 f42b1b34 2018-03-12 stsp
2195 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2196 dc1edbfa 2019-11-29 kn diff_context = 3;
2197 dc1edbfa 2019-11-29 kn else if (!show_patch)
2198 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2199 dc1edbfa 2019-11-29 kn
2200 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2201 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2202 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2203 04ca23f4 2018-07-16 stsp goto done;
2204 04ca23f4 2018-07-16 stsp }
2205 cffc0aa4 2019-02-05 stsp
2206 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2207 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2208 cffc0aa4 2019-02-05 stsp goto done;
2209 cffc0aa4 2019-02-05 stsp error = NULL;
2210 cffc0aa4 2019-02-05 stsp
2211 e7301579 2019-03-18 stsp if (argc == 0) {
2212 cbd1af7a 2019-03-18 stsp path = strdup("");
2213 e7301579 2019-03-18 stsp if (path == NULL) {
2214 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2215 cbd1af7a 2019-03-18 stsp goto done;
2216 e7301579 2019-03-18 stsp }
2217 e7301579 2019-03-18 stsp } else if (argc == 1) {
2218 e7301579 2019-03-18 stsp if (worktree) {
2219 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2220 e7301579 2019-03-18 stsp argv[0]);
2221 e7301579 2019-03-18 stsp if (error)
2222 e7301579 2019-03-18 stsp goto done;
2223 e7301579 2019-03-18 stsp } else {
2224 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2225 e7301579 2019-03-18 stsp if (path == NULL) {
2226 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2227 e7301579 2019-03-18 stsp goto done;
2228 e7301579 2019-03-18 stsp }
2229 e7301579 2019-03-18 stsp }
2230 cbd1af7a 2019-03-18 stsp } else
2231 cbd1af7a 2019-03-18 stsp usage_log();
2232 cbd1af7a 2019-03-18 stsp
2233 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2234 5486daa2 2019-05-11 stsp repo_path = worktree ?
2235 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2236 5486daa2 2019-05-11 stsp }
2237 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2238 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2239 cffc0aa4 2019-02-05 stsp goto done;
2240 04ca23f4 2018-07-16 stsp }
2241 6098196c 2019-01-04 stsp
2242 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2243 d7d4f210 2018-03-12 stsp if (error != NULL)
2244 04ca23f4 2018-07-16 stsp goto done;
2245 f42b1b34 2018-03-12 stsp
2246 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2247 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2248 c02c541e 2019-03-29 stsp if (error)
2249 c02c541e 2019-03-29 stsp goto done;
2250 c02c541e 2019-03-29 stsp
2251 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2252 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2253 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2254 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2255 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2256 3235492e 2018-04-01 stsp if (error != NULL)
2257 3235492e 2018-04-01 stsp return error;
2258 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2259 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2260 3235492e 2018-04-01 stsp if (error != NULL)
2261 3235492e 2018-04-01 stsp return error;
2262 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2263 3235492e 2018-04-01 stsp } else {
2264 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2265 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2266 3235492e 2018-04-01 stsp if (error == NULL) {
2267 1caad61b 2019-02-01 stsp int obj_type;
2268 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2269 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2270 d1f2edc9 2018-06-13 stsp if (error != NULL)
2271 1caad61b 2019-02-01 stsp goto done;
2272 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2273 1caad61b 2019-02-01 stsp if (error != NULL)
2274 1caad61b 2019-02-01 stsp goto done;
2275 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2276 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2277 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2278 1caad61b 2019-02-01 stsp if (error != NULL)
2279 1caad61b 2019-02-01 stsp goto done;
2280 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2281 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2282 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2283 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2284 1caad61b 2019-02-01 stsp goto done;
2285 1caad61b 2019-02-01 stsp }
2286 1caad61b 2019-02-01 stsp free(id);
2287 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2288 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2289 1caad61b 2019-02-01 stsp if (id == NULL)
2290 638f9024 2019-05-13 stsp error = got_error_from_errno(
2291 230a42bd 2019-05-11 jcs "got_object_id_dup");
2292 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2293 1caad61b 2019-02-01 stsp if (error)
2294 1caad61b 2019-02-01 stsp goto done;
2295 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2296 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2297 1caad61b 2019-02-01 stsp goto done;
2298 1caad61b 2019-02-01 stsp }
2299 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2300 d1f2edc9 2018-06-13 stsp if (error != NULL)
2301 1caad61b 2019-02-01 stsp goto done;
2302 d1f2edc9 2018-06-13 stsp }
2303 15a94983 2018-12-23 stsp if (commit == NULL) {
2304 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2305 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2306 d1f2edc9 2018-06-13 stsp if (error != NULL)
2307 d1f2edc9 2018-06-13 stsp return error;
2308 3235492e 2018-04-01 stsp }
2309 3235492e 2018-04-01 stsp }
2310 d7d4f210 2018-03-12 stsp if (error != NULL)
2311 04ca23f4 2018-07-16 stsp goto done;
2312 04ca23f4 2018-07-16 stsp
2313 deeabeae 2019-08-27 stsp if (worktree) {
2314 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2315 deeabeae 2019-08-27 stsp char *p;
2316 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2317 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2318 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2319 deeabeae 2019-08-27 stsp goto done;
2320 deeabeae 2019-08-27 stsp }
2321 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2322 deeabeae 2019-08-27 stsp free(p);
2323 deeabeae 2019-08-27 stsp } else
2324 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2325 04ca23f4 2018-07-16 stsp if (error != NULL)
2326 04ca23f4 2018-07-16 stsp goto done;
2327 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2328 04ca23f4 2018-07-16 stsp free(path);
2329 04ca23f4 2018-07-16 stsp path = in_repo_path;
2330 04ca23f4 2018-07-16 stsp }
2331 199a4027 2019-02-02 stsp
2332 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2333 199a4027 2019-02-02 stsp if (error)
2334 199a4027 2019-02-02 stsp goto done;
2335 04ca23f4 2018-07-16 stsp
2336 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2337 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2338 04ca23f4 2018-07-16 stsp done:
2339 04ca23f4 2018-07-16 stsp free(path);
2340 04ca23f4 2018-07-16 stsp free(repo_path);
2341 04ca23f4 2018-07-16 stsp free(cwd);
2342 f42b1b34 2018-03-12 stsp free(id);
2343 cffc0aa4 2019-02-05 stsp if (worktree)
2344 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2345 ad242220 2018-09-08 stsp if (repo) {
2346 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2347 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2348 ad242220 2018-09-08 stsp if (error == NULL)
2349 ad242220 2018-09-08 stsp error = repo_error;
2350 ad242220 2018-09-08 stsp }
2351 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2352 8bf5b3c9 2018-03-17 stsp return error;
2353 5c860e29 2018-03-12 stsp }
2354 5c860e29 2018-03-12 stsp
2355 4ed7e80c 2018-05-20 stsp __dead static void
2356 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2357 3f8b7d6a 2018-04-01 stsp {
2358 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2359 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2360 3f8b7d6a 2018-04-01 stsp exit(1);
2361 b00d56cd 2018-04-01 stsp }
2362 b00d56cd 2018-04-01 stsp
2363 b72f483a 2019-02-05 stsp struct print_diff_arg {
2364 b72f483a 2019-02-05 stsp struct got_repository *repo;
2365 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2366 b72f483a 2019-02-05 stsp int diff_context;
2367 3fc0c068 2019-02-10 stsp const char *id_str;
2368 3fc0c068 2019-02-10 stsp int header_shown;
2369 98eaaa12 2019-08-03 stsp int diff_staged;
2370 63035f9f 2019-10-06 stsp int ignore_whitespace;
2371 b72f483a 2019-02-05 stsp };
2372 b72f483a 2019-02-05 stsp
2373 4ed7e80c 2018-05-20 stsp static const struct got_error *
2374 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2375 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2376 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2377 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2378 b72f483a 2019-02-05 stsp {
2379 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2380 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2381 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2382 12463d8b 2019-12-13 stsp int fd = -1;
2383 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2384 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2385 b72f483a 2019-02-05 stsp struct stat sb;
2386 b72f483a 2019-02-05 stsp
2387 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2388 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2389 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2390 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2391 98eaaa12 2019-08-03 stsp return NULL;
2392 98eaaa12 2019-08-03 stsp } else {
2393 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2394 98eaaa12 2019-08-03 stsp return NULL;
2395 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2396 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2397 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2398 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2399 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2400 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2401 98eaaa12 2019-08-03 stsp return NULL;
2402 98eaaa12 2019-08-03 stsp }
2403 3fc0c068 2019-02-10 stsp
2404 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2405 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2406 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2407 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2408 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2409 3fc0c068 2019-02-10 stsp }
2410 b72f483a 2019-02-05 stsp
2411 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2412 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2413 98eaaa12 2019-08-03 stsp switch (staged_status) {
2414 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2415 98eaaa12 2019-08-03 stsp label1 = path;
2416 98eaaa12 2019-08-03 stsp label2 = path;
2417 98eaaa12 2019-08-03 stsp break;
2418 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2419 98eaaa12 2019-08-03 stsp label2 = path;
2420 98eaaa12 2019-08-03 stsp break;
2421 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2422 98eaaa12 2019-08-03 stsp label1 = path;
2423 98eaaa12 2019-08-03 stsp break;
2424 98eaaa12 2019-08-03 stsp default:
2425 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2426 98eaaa12 2019-08-03 stsp }
2427 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2428 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2429 63035f9f 2019-10-06 stsp a->repo, stdout);
2430 98eaaa12 2019-08-03 stsp }
2431 98eaaa12 2019-08-03 stsp
2432 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2433 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2434 4ce46740 2019-08-08 stsp char *id_str;
2435 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2436 408b4ebc 2019-08-03 stsp 8192);
2437 4ce46740 2019-08-08 stsp if (err)
2438 4ce46740 2019-08-08 stsp goto done;
2439 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2440 4ce46740 2019-08-08 stsp if (err)
2441 4ce46740 2019-08-08 stsp goto done;
2442 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2443 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2444 4ce46740 2019-08-08 stsp free(id_str);
2445 4ce46740 2019-08-08 stsp goto done;
2446 4ce46740 2019-08-08 stsp }
2447 4ce46740 2019-08-08 stsp free(id_str);
2448 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2449 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2450 4ce46740 2019-08-08 stsp if (err)
2451 4ce46740 2019-08-08 stsp goto done;
2452 4ce46740 2019-08-08 stsp }
2453 d00136be 2019-03-26 stsp
2454 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2455 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2456 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2457 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2458 2ec1f75b 2019-03-26 stsp goto done;
2459 2ec1f75b 2019-03-26 stsp }
2460 b72f483a 2019-02-05 stsp
2461 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2462 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2463 12463d8b 2019-12-13 stsp if (fd == -1) {
2464 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2465 12463d8b 2019-12-13 stsp goto done;
2466 12463d8b 2019-12-13 stsp }
2467 12463d8b 2019-12-13 stsp } else {
2468 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2469 12463d8b 2019-12-13 stsp if (fd == -1) {
2470 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2471 12463d8b 2019-12-13 stsp goto done;
2472 12463d8b 2019-12-13 stsp }
2473 12463d8b 2019-12-13 stsp }
2474 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2475 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2476 2ec1f75b 2019-03-26 stsp goto done;
2477 2ec1f75b 2019-03-26 stsp }
2478 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2479 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2480 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2481 2ec1f75b 2019-03-26 stsp goto done;
2482 2ec1f75b 2019-03-26 stsp }
2483 12463d8b 2019-12-13 stsp fd = -1;
2484 2ec1f75b 2019-03-26 stsp } else
2485 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2486 b72f483a 2019-02-05 stsp
2487 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2488 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2489 b72f483a 2019-02-05 stsp done:
2490 b72f483a 2019-02-05 stsp if (blob1)
2491 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2492 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2493 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2494 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2495 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2496 b72f483a 2019-02-05 stsp free(abspath);
2497 927df6b7 2019-02-10 stsp return err;
2498 927df6b7 2019-02-10 stsp }
2499 927df6b7 2019-02-10 stsp
2500 927df6b7 2019-02-10 stsp static const struct got_error *
2501 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2502 b00d56cd 2018-04-01 stsp {
2503 b00d56cd 2018-04-01 stsp const struct got_error *error;
2504 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2505 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2506 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2507 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2508 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2509 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2510 15a94983 2018-12-23 stsp int type1, type2;
2511 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2512 c0cc5c62 2018-10-18 stsp const char *errstr;
2513 927df6b7 2019-02-10 stsp char *path = NULL;
2514 b00d56cd 2018-04-01 stsp
2515 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2516 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2517 25eccc22 2019-01-04 stsp NULL) == -1)
2518 b00d56cd 2018-04-01 stsp err(1, "pledge");
2519 b00d56cd 2018-04-01 stsp #endif
2520 b00d56cd 2018-04-01 stsp
2521 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2522 b00d56cd 2018-04-01 stsp switch (ch) {
2523 c0cc5c62 2018-10-18 stsp case 'C':
2524 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2525 dfcab68b 2019-11-29 kn &errstr);
2526 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2527 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2528 c0cc5c62 2018-10-18 stsp break;
2529 b72f483a 2019-02-05 stsp case 'r':
2530 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2531 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2532 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2533 9ba1d308 2019-10-21 stsp optarg);
2534 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2535 b72f483a 2019-02-05 stsp break;
2536 98eaaa12 2019-08-03 stsp case 's':
2537 98eaaa12 2019-08-03 stsp diff_staged = 1;
2538 63035f9f 2019-10-06 stsp break;
2539 63035f9f 2019-10-06 stsp case 'w':
2540 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2541 98eaaa12 2019-08-03 stsp break;
2542 b00d56cd 2018-04-01 stsp default:
2543 2deda0b9 2019-03-07 stsp usage_diff();
2544 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2545 b00d56cd 2018-04-01 stsp }
2546 b00d56cd 2018-04-01 stsp }
2547 b00d56cd 2018-04-01 stsp
2548 b00d56cd 2018-04-01 stsp argc -= optind;
2549 b00d56cd 2018-04-01 stsp argv += optind;
2550 b00d56cd 2018-04-01 stsp
2551 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2552 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2553 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2554 b72f483a 2019-02-05 stsp goto done;
2555 b72f483a 2019-02-05 stsp }
2556 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2557 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2558 927df6b7 2019-02-10 stsp goto done;
2559 927df6b7 2019-02-10 stsp if (argc <= 1) {
2560 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2561 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2562 927df6b7 2019-02-10 stsp goto done;
2563 927df6b7 2019-02-10 stsp }
2564 b72f483a 2019-02-05 stsp if (repo_path)
2565 b72f483a 2019-02-05 stsp errx(1,
2566 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2567 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2568 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2569 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2570 927df6b7 2019-02-10 stsp goto done;
2571 927df6b7 2019-02-10 stsp }
2572 927df6b7 2019-02-10 stsp if (argc == 1) {
2573 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2574 cbd1af7a 2019-03-18 stsp argv[0]);
2575 927df6b7 2019-02-10 stsp if (error)
2576 927df6b7 2019-02-10 stsp goto done;
2577 927df6b7 2019-02-10 stsp } else {
2578 927df6b7 2019-02-10 stsp path = strdup("");
2579 927df6b7 2019-02-10 stsp if (path == NULL) {
2580 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2581 927df6b7 2019-02-10 stsp goto done;
2582 927df6b7 2019-02-10 stsp }
2583 927df6b7 2019-02-10 stsp }
2584 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2585 98eaaa12 2019-08-03 stsp if (diff_staged)
2586 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2587 98eaaa12 2019-08-03 stsp "objects in repository");
2588 15a94983 2018-12-23 stsp id_str1 = argv[0];
2589 15a94983 2018-12-23 stsp id_str2 = argv[1];
2590 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2591 30db809c 2019-06-05 stsp repo_path =
2592 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2593 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2594 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2595 30db809c 2019-06-05 stsp goto done;
2596 30db809c 2019-06-05 stsp }
2597 30db809c 2019-06-05 stsp }
2598 b00d56cd 2018-04-01 stsp } else
2599 b00d56cd 2018-04-01 stsp usage_diff();
2600 25eccc22 2019-01-04 stsp
2601 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2602 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2603 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2604 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2605 b72f483a 2019-02-05 stsp }
2606 b00d56cd 2018-04-01 stsp
2607 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2608 76089277 2018-04-01 stsp free(repo_path);
2609 b00d56cd 2018-04-01 stsp if (error != NULL)
2610 b00d56cd 2018-04-01 stsp goto done;
2611 b00d56cd 2018-04-01 stsp
2612 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2613 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2614 c02c541e 2019-03-29 stsp if (error)
2615 c02c541e 2019-03-29 stsp goto done;
2616 c02c541e 2019-03-29 stsp
2617 30db809c 2019-06-05 stsp if (argc <= 1) {
2618 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2619 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2620 b72f483a 2019-02-05 stsp char *id_str;
2621 72ea6654 2019-07-27 stsp
2622 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2623 72ea6654 2019-07-27 stsp
2624 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2625 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2626 b72f483a 2019-02-05 stsp if (error)
2627 b72f483a 2019-02-05 stsp goto done;
2628 b72f483a 2019-02-05 stsp arg.repo = repo;
2629 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2630 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2631 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2632 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2633 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2634 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2635 b72f483a 2019-02-05 stsp
2636 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2637 72ea6654 2019-07-27 stsp if (error)
2638 72ea6654 2019-07-27 stsp goto done;
2639 72ea6654 2019-07-27 stsp
2640 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2641 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2642 b72f483a 2019-02-05 stsp free(id_str);
2643 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2644 b72f483a 2019-02-05 stsp goto done;
2645 b72f483a 2019-02-05 stsp }
2646 b72f483a 2019-02-05 stsp
2647 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2648 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2649 0adb7196 2019-08-11 stsp if (error)
2650 0adb7196 2019-08-11 stsp goto done;
2651 b00d56cd 2018-04-01 stsp
2652 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2653 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2654 0adb7196 2019-08-11 stsp if (error)
2655 0adb7196 2019-08-11 stsp goto done;
2656 b00d56cd 2018-04-01 stsp
2657 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2658 15a94983 2018-12-23 stsp if (error)
2659 15a94983 2018-12-23 stsp goto done;
2660 15a94983 2018-12-23 stsp
2661 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2662 15a94983 2018-12-23 stsp if (error)
2663 15a94983 2018-12-23 stsp goto done;
2664 15a94983 2018-12-23 stsp
2665 15a94983 2018-12-23 stsp if (type1 != type2) {
2666 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2667 b00d56cd 2018-04-01 stsp goto done;
2668 b00d56cd 2018-04-01 stsp }
2669 b00d56cd 2018-04-01 stsp
2670 15a94983 2018-12-23 stsp switch (type1) {
2671 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2672 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2673 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2674 b00d56cd 2018-04-01 stsp break;
2675 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2676 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2677 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2678 b00d56cd 2018-04-01 stsp break;
2679 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2680 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2681 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2682 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2683 b00d56cd 2018-04-01 stsp break;
2684 b00d56cd 2018-04-01 stsp default:
2685 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2686 b00d56cd 2018-04-01 stsp }
2687 b00d56cd 2018-04-01 stsp done:
2688 5e70831e 2019-05-28 stsp free(label1);
2689 5e70831e 2019-05-28 stsp free(label2);
2690 15a94983 2018-12-23 stsp free(id1);
2691 15a94983 2018-12-23 stsp free(id2);
2692 927df6b7 2019-02-10 stsp free(path);
2693 b72f483a 2019-02-05 stsp if (worktree)
2694 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2695 ad242220 2018-09-08 stsp if (repo) {
2696 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2697 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2698 ad242220 2018-09-08 stsp if (error == NULL)
2699 ad242220 2018-09-08 stsp error = repo_error;
2700 ad242220 2018-09-08 stsp }
2701 b00d56cd 2018-04-01 stsp return error;
2702 404c43c4 2018-06-21 stsp }
2703 404c43c4 2018-06-21 stsp
2704 404c43c4 2018-06-21 stsp __dead static void
2705 404c43c4 2018-06-21 stsp usage_blame(void)
2706 404c43c4 2018-06-21 stsp {
2707 9270e621 2019-02-05 stsp fprintf(stderr,
2708 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2709 404c43c4 2018-06-21 stsp getprogname());
2710 404c43c4 2018-06-21 stsp exit(1);
2711 b00d56cd 2018-04-01 stsp }
2712 b00d56cd 2018-04-01 stsp
2713 28315671 2019-08-14 stsp struct blame_line {
2714 28315671 2019-08-14 stsp int annotated;
2715 28315671 2019-08-14 stsp char *id_str;
2716 82f6abb8 2019-08-14 stsp char *committer;
2717 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2718 28315671 2019-08-14 stsp };
2719 28315671 2019-08-14 stsp
2720 28315671 2019-08-14 stsp struct blame_cb_args {
2721 28315671 2019-08-14 stsp struct blame_line *lines;
2722 28315671 2019-08-14 stsp int nlines;
2723 7ef28ff8 2019-08-14 stsp int nlines_prec;
2724 28315671 2019-08-14 stsp int lineno_cur;
2725 28315671 2019-08-14 stsp off_t *line_offsets;
2726 28315671 2019-08-14 stsp FILE *f;
2727 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2728 28315671 2019-08-14 stsp };
2729 28315671 2019-08-14 stsp
2730 404c43c4 2018-06-21 stsp static const struct got_error *
2731 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2732 28315671 2019-08-14 stsp {
2733 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2734 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2735 28315671 2019-08-14 stsp struct blame_line *bline;
2736 82f6abb8 2019-08-14 stsp char *line = NULL;
2737 28315671 2019-08-14 stsp size_t linesize = 0;
2738 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2739 28315671 2019-08-14 stsp off_t offset;
2740 bcb49d15 2019-08-14 stsp struct tm tm;
2741 bcb49d15 2019-08-14 stsp time_t committer_time;
2742 28315671 2019-08-14 stsp
2743 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2744 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2745 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2746 28315671 2019-08-14 stsp
2747 28315671 2019-08-14 stsp if (sigint_received)
2748 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2749 28315671 2019-08-14 stsp
2750 2839f8b9 2019-08-18 stsp if (lineno == -1)
2751 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2752 2839f8b9 2019-08-18 stsp
2753 28315671 2019-08-14 stsp /* Annotate this line. */
2754 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2755 28315671 2019-08-14 stsp if (bline->annotated)
2756 28315671 2019-08-14 stsp return NULL;
2757 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2758 28315671 2019-08-14 stsp if (err)
2759 28315671 2019-08-14 stsp return err;
2760 82f6abb8 2019-08-14 stsp
2761 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2762 82f6abb8 2019-08-14 stsp if (err)
2763 82f6abb8 2019-08-14 stsp goto done;
2764 82f6abb8 2019-08-14 stsp
2765 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2766 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2767 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2768 bcb49d15 2019-08-14 stsp goto done;
2769 bcb49d15 2019-08-14 stsp }
2770 bcb49d15 2019-08-14 stsp
2771 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2772 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2773 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2774 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2775 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2776 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2777 82f6abb8 2019-08-14 stsp goto done;
2778 82f6abb8 2019-08-14 stsp }
2779 28315671 2019-08-14 stsp bline->annotated = 1;
2780 28315671 2019-08-14 stsp
2781 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2782 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2783 28315671 2019-08-14 stsp if (!bline->annotated)
2784 82f6abb8 2019-08-14 stsp goto done;
2785 28315671 2019-08-14 stsp
2786 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2787 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2788 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2789 82f6abb8 2019-08-14 stsp goto done;
2790 82f6abb8 2019-08-14 stsp }
2791 28315671 2019-08-14 stsp
2792 28315671 2019-08-14 stsp while (bline->annotated) {
2793 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2794 82f6abb8 2019-08-14 stsp size_t len;
2795 82f6abb8 2019-08-14 stsp
2796 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2797 28315671 2019-08-14 stsp if (ferror(a->f))
2798 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2799 28315671 2019-08-14 stsp break;
2800 28315671 2019-08-14 stsp }
2801 82f6abb8 2019-08-14 stsp
2802 82f6abb8 2019-08-14 stsp committer = bline->committer;
2803 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2804 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2805 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2806 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2807 82f6abb8 2019-08-14 stsp if (at)
2808 82f6abb8 2019-08-14 stsp *at = '\0';
2809 82f6abb8 2019-08-14 stsp len = strlen(committer);
2810 82f6abb8 2019-08-14 stsp if (len >= 9)
2811 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2812 28315671 2019-08-14 stsp
2813 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2814 28315671 2019-08-14 stsp if (nl)
2815 28315671 2019-08-14 stsp *nl = '\0';
2816 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2817 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2818 28315671 2019-08-14 stsp
2819 28315671 2019-08-14 stsp a->lineno_cur++;
2820 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2821 28315671 2019-08-14 stsp }
2822 82f6abb8 2019-08-14 stsp done:
2823 82f6abb8 2019-08-14 stsp if (commit)
2824 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2825 28315671 2019-08-14 stsp free(line);
2826 28315671 2019-08-14 stsp return err;
2827 28315671 2019-08-14 stsp }
2828 28315671 2019-08-14 stsp
2829 28315671 2019-08-14 stsp static const struct got_error *
2830 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2831 404c43c4 2018-06-21 stsp {
2832 404c43c4 2018-06-21 stsp const struct got_error *error;
2833 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2834 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2835 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2836 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2837 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2838 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2839 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2840 28315671 2019-08-14 stsp struct blame_cb_args bca;
2841 28315671 2019-08-14 stsp int ch, obj_type, i;
2842 b02560ec 2019-08-19 stsp size_t filesize;
2843 90f3c347 2019-08-19 stsp
2844 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2845 404c43c4 2018-06-21 stsp
2846 404c43c4 2018-06-21 stsp #ifndef PROFILE
2847 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2848 36e2fb66 2019-01-04 stsp NULL) == -1)
2849 404c43c4 2018-06-21 stsp err(1, "pledge");
2850 404c43c4 2018-06-21 stsp #endif
2851 404c43c4 2018-06-21 stsp
2852 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2853 404c43c4 2018-06-21 stsp switch (ch) {
2854 404c43c4 2018-06-21 stsp case 'c':
2855 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2856 404c43c4 2018-06-21 stsp break;
2857 66bea077 2018-08-02 stsp case 'r':
2858 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2859 66bea077 2018-08-02 stsp if (repo_path == NULL)
2860 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2861 9ba1d308 2019-10-21 stsp optarg);
2862 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2863 66bea077 2018-08-02 stsp break;
2864 404c43c4 2018-06-21 stsp default:
2865 2deda0b9 2019-03-07 stsp usage_blame();
2866 404c43c4 2018-06-21 stsp /* NOTREACHED */
2867 404c43c4 2018-06-21 stsp }
2868 404c43c4 2018-06-21 stsp }
2869 404c43c4 2018-06-21 stsp
2870 404c43c4 2018-06-21 stsp argc -= optind;
2871 404c43c4 2018-06-21 stsp argv += optind;
2872 404c43c4 2018-06-21 stsp
2873 a39318fd 2018-08-02 stsp if (argc == 1)
2874 404c43c4 2018-06-21 stsp path = argv[0];
2875 a39318fd 2018-08-02 stsp else
2876 404c43c4 2018-06-21 stsp usage_blame();
2877 404c43c4 2018-06-21 stsp
2878 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2879 66bea077 2018-08-02 stsp if (cwd == NULL) {
2880 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2881 66bea077 2018-08-02 stsp goto done;
2882 66bea077 2018-08-02 stsp }
2883 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2884 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2885 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2886 66bea077 2018-08-02 stsp goto done;
2887 0c06baac 2019-02-05 stsp else
2888 0c06baac 2019-02-05 stsp error = NULL;
2889 0c06baac 2019-02-05 stsp if (worktree) {
2890 0c06baac 2019-02-05 stsp repo_path =
2891 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2892 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2893 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2894 0c06baac 2019-02-05 stsp if (error)
2895 0c06baac 2019-02-05 stsp goto done;
2896 0c06baac 2019-02-05 stsp } else {
2897 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2898 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2899 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2900 0c06baac 2019-02-05 stsp goto done;
2901 0c06baac 2019-02-05 stsp }
2902 66bea077 2018-08-02 stsp }
2903 66bea077 2018-08-02 stsp }
2904 36e2fb66 2019-01-04 stsp
2905 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2906 c02c541e 2019-03-29 stsp if (error != NULL)
2907 36e2fb66 2019-01-04 stsp goto done;
2908 66bea077 2018-08-02 stsp
2909 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2910 c02c541e 2019-03-29 stsp if (error)
2911 404c43c4 2018-06-21 stsp goto done;
2912 404c43c4 2018-06-21 stsp
2913 0c06baac 2019-02-05 stsp if (worktree) {
2914 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2915 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2916 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2917 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2918 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2919 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2920 6efaaa2d 2019-02-05 stsp path) == -1) {
2921 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2922 0c06baac 2019-02-05 stsp goto done;
2923 0c06baac 2019-02-05 stsp }
2924 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2925 0c06baac 2019-02-05 stsp free(p);
2926 0c06baac 2019-02-05 stsp } else {
2927 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2928 0c06baac 2019-02-05 stsp }
2929 0c06baac 2019-02-05 stsp if (error)
2930 66bea077 2018-08-02 stsp goto done;
2931 66bea077 2018-08-02 stsp
2932 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2933 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2934 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2935 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2936 404c43c4 2018-06-21 stsp if (error != NULL)
2937 66bea077 2018-08-02 stsp goto done;
2938 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2939 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2940 404c43c4 2018-06-21 stsp if (error != NULL)
2941 66bea077 2018-08-02 stsp goto done;
2942 404c43c4 2018-06-21 stsp } else {
2943 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2944 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2945 30837e32 2019-07-25 stsp if (error)
2946 66bea077 2018-08-02 stsp goto done;
2947 404c43c4 2018-06-21 stsp }
2948 404c43c4 2018-06-21 stsp
2949 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2950 28315671 2019-08-14 stsp if (error)
2951 28315671 2019-08-14 stsp goto done;
2952 28315671 2019-08-14 stsp
2953 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2954 28315671 2019-08-14 stsp if (error)
2955 28315671 2019-08-14 stsp goto done;
2956 28315671 2019-08-14 stsp
2957 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2958 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2959 28315671 2019-08-14 stsp goto done;
2960 28315671 2019-08-14 stsp }
2961 28315671 2019-08-14 stsp
2962 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2963 28315671 2019-08-14 stsp if (error)
2964 28315671 2019-08-14 stsp goto done;
2965 28315671 2019-08-14 stsp bca.f = got_opentemp();
2966 28315671 2019-08-14 stsp if (bca.f == NULL) {
2967 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2968 28315671 2019-08-14 stsp goto done;
2969 28315671 2019-08-14 stsp }
2970 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2971 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2972 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2973 28315671 2019-08-14 stsp goto done;
2974 28315671 2019-08-14 stsp
2975 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2976 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2977 b02560ec 2019-08-19 stsp bca.nlines--;
2978 b02560ec 2019-08-19 stsp
2979 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2980 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2981 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2982 28315671 2019-08-14 stsp goto done;
2983 28315671 2019-08-14 stsp }
2984 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2985 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2986 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2987 7ef28ff8 2019-08-14 stsp while (i > 0) {
2988 7ef28ff8 2019-08-14 stsp i /= 10;
2989 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2990 7ef28ff8 2019-08-14 stsp }
2991 82f6abb8 2019-08-14 stsp bca.repo = repo;
2992 7ef28ff8 2019-08-14 stsp
2993 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2994 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2995 404c43c4 2018-06-21 stsp done:
2996 66bea077 2018-08-02 stsp free(in_repo_path);
2997 66bea077 2018-08-02 stsp free(repo_path);
2998 66bea077 2018-08-02 stsp free(cwd);
2999 404c43c4 2018-06-21 stsp free(commit_id);
3000 28315671 2019-08-14 stsp free(obj_id);
3001 28315671 2019-08-14 stsp if (blob)
3002 28315671 2019-08-14 stsp got_object_blob_close(blob);
3003 0c06baac 2019-02-05 stsp if (worktree)
3004 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3005 ad242220 2018-09-08 stsp if (repo) {
3006 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3007 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3008 ad242220 2018-09-08 stsp if (error == NULL)
3009 ad242220 2018-09-08 stsp error = repo_error;
3010 ad242220 2018-09-08 stsp }
3011 3affba96 2019-09-22 stsp if (bca.lines) {
3012 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3013 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3014 3affba96 2019-09-22 stsp free(bline->id_str);
3015 3affba96 2019-09-22 stsp free(bline->committer);
3016 3affba96 2019-09-22 stsp }
3017 3affba96 2019-09-22 stsp free(bca.lines);
3018 28315671 2019-08-14 stsp }
3019 28315671 2019-08-14 stsp free(bca.line_offsets);
3020 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3021 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3022 404c43c4 2018-06-21 stsp return error;
3023 5de5890b 2018-10-18 stsp }
3024 5de5890b 2018-10-18 stsp
3025 5de5890b 2018-10-18 stsp __dead static void
3026 5de5890b 2018-10-18 stsp usage_tree(void)
3027 5de5890b 2018-10-18 stsp {
3028 c1669e2e 2019-01-09 stsp fprintf(stderr,
3029 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3030 5de5890b 2018-10-18 stsp getprogname());
3031 5de5890b 2018-10-18 stsp exit(1);
3032 5de5890b 2018-10-18 stsp }
3033 5de5890b 2018-10-18 stsp
3034 c1669e2e 2019-01-09 stsp static void
3035 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3036 c1669e2e 2019-01-09 stsp const char *root_path)
3037 c1669e2e 2019-01-09 stsp {
3038 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3039 848d6979 2019-08-12 stsp const char *modestr = "";
3040 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3041 5de5890b 2018-10-18 stsp
3042 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3043 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3044 c1669e2e 2019-01-09 stsp path++;
3045 c1669e2e 2019-01-09 stsp
3046 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3047 63c5ca5d 2019-08-24 stsp modestr = "$";
3048 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3049 848d6979 2019-08-12 stsp modestr = "@";
3050 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3051 848d6979 2019-08-12 stsp modestr = "/";
3052 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3053 848d6979 2019-08-12 stsp modestr = "*";
3054 848d6979 2019-08-12 stsp
3055 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3056 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3057 c1669e2e 2019-01-09 stsp }
3058 c1669e2e 2019-01-09 stsp
3059 5de5890b 2018-10-18 stsp static const struct got_error *
3060 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3061 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3062 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3063 5de5890b 2018-10-18 stsp {
3064 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3065 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3066 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3067 56e0773d 2019-11-28 stsp int nentries, i;
3068 5de5890b 2018-10-18 stsp
3069 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3070 5de5890b 2018-10-18 stsp if (err)
3071 5de5890b 2018-10-18 stsp goto done;
3072 5de5890b 2018-10-18 stsp
3073 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3074 5de5890b 2018-10-18 stsp if (err)
3075 5de5890b 2018-10-18 stsp goto done;
3076 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3077 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3078 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3079 5de5890b 2018-10-18 stsp char *id = NULL;
3080 84453469 2018-11-11 stsp
3081 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3082 84453469 2018-11-11 stsp break;
3083 84453469 2018-11-11 stsp
3084 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3085 5de5890b 2018-10-18 stsp if (show_ids) {
3086 5de5890b 2018-10-18 stsp char *id_str;
3087 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3088 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3089 5de5890b 2018-10-18 stsp if (err)
3090 5de5890b 2018-10-18 stsp goto done;
3091 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3092 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3093 5de5890b 2018-10-18 stsp free(id_str);
3094 5de5890b 2018-10-18 stsp goto done;
3095 5de5890b 2018-10-18 stsp }
3096 5de5890b 2018-10-18 stsp free(id_str);
3097 5de5890b 2018-10-18 stsp }
3098 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3099 5de5890b 2018-10-18 stsp free(id);
3100 c1669e2e 2019-01-09 stsp
3101 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3102 c1669e2e 2019-01-09 stsp char *child_path;
3103 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3104 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3105 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3106 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3107 c1669e2e 2019-01-09 stsp goto done;
3108 c1669e2e 2019-01-09 stsp }
3109 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3110 c1669e2e 2019-01-09 stsp root_path, repo);
3111 c1669e2e 2019-01-09 stsp free(child_path);
3112 c1669e2e 2019-01-09 stsp if (err)
3113 c1669e2e 2019-01-09 stsp goto done;
3114 c1669e2e 2019-01-09 stsp }
3115 5de5890b 2018-10-18 stsp }
3116 5de5890b 2018-10-18 stsp done:
3117 5de5890b 2018-10-18 stsp if (tree)
3118 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3119 5de5890b 2018-10-18 stsp free(tree_id);
3120 5de5890b 2018-10-18 stsp return err;
3121 404c43c4 2018-06-21 stsp }
3122 404c43c4 2018-06-21 stsp
3123 5de5890b 2018-10-18 stsp static const struct got_error *
3124 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3125 5de5890b 2018-10-18 stsp {
3126 5de5890b 2018-10-18 stsp const struct got_error *error;
3127 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3128 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3129 7a2c19d6 2019-02-05 stsp const char *path;
3130 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3131 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3132 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3133 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3134 5de5890b 2018-10-18 stsp int ch;
3135 5de5890b 2018-10-18 stsp
3136 5de5890b 2018-10-18 stsp #ifndef PROFILE
3137 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3138 0f8d269b 2019-01-04 stsp NULL) == -1)
3139 5de5890b 2018-10-18 stsp err(1, "pledge");
3140 5de5890b 2018-10-18 stsp #endif
3141 5de5890b 2018-10-18 stsp
3142 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3143 5de5890b 2018-10-18 stsp switch (ch) {
3144 5de5890b 2018-10-18 stsp case 'c':
3145 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3146 5de5890b 2018-10-18 stsp break;
3147 5de5890b 2018-10-18 stsp case 'r':
3148 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3149 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3150 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3151 9ba1d308 2019-10-21 stsp optarg);
3152 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3153 5de5890b 2018-10-18 stsp break;
3154 5de5890b 2018-10-18 stsp case 'i':
3155 5de5890b 2018-10-18 stsp show_ids = 1;
3156 5de5890b 2018-10-18 stsp break;
3157 c1669e2e 2019-01-09 stsp case 'R':
3158 c1669e2e 2019-01-09 stsp recurse = 1;
3159 c1669e2e 2019-01-09 stsp break;
3160 5de5890b 2018-10-18 stsp default:
3161 2deda0b9 2019-03-07 stsp usage_tree();
3162 5de5890b 2018-10-18 stsp /* NOTREACHED */
3163 5de5890b 2018-10-18 stsp }
3164 5de5890b 2018-10-18 stsp }
3165 5de5890b 2018-10-18 stsp
3166 5de5890b 2018-10-18 stsp argc -= optind;
3167 5de5890b 2018-10-18 stsp argv += optind;
3168 5de5890b 2018-10-18 stsp
3169 5de5890b 2018-10-18 stsp if (argc == 1)
3170 5de5890b 2018-10-18 stsp path = argv[0];
3171 5de5890b 2018-10-18 stsp else if (argc > 1)
3172 5de5890b 2018-10-18 stsp usage_tree();
3173 5de5890b 2018-10-18 stsp else
3174 7a2c19d6 2019-02-05 stsp path = NULL;
3175 7a2c19d6 2019-02-05 stsp
3176 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3177 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3178 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3179 9bf7a39b 2019-02-05 stsp goto done;
3180 9bf7a39b 2019-02-05 stsp }
3181 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3182 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3183 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3184 7a2c19d6 2019-02-05 stsp goto done;
3185 7a2c19d6 2019-02-05 stsp else
3186 7a2c19d6 2019-02-05 stsp error = NULL;
3187 7a2c19d6 2019-02-05 stsp if (worktree) {
3188 7a2c19d6 2019-02-05 stsp repo_path =
3189 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3190 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3191 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3192 7a2c19d6 2019-02-05 stsp if (error)
3193 7a2c19d6 2019-02-05 stsp goto done;
3194 7a2c19d6 2019-02-05 stsp } else {
3195 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3196 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3197 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3198 7a2c19d6 2019-02-05 stsp goto done;
3199 7a2c19d6 2019-02-05 stsp }
3200 5de5890b 2018-10-18 stsp }
3201 5de5890b 2018-10-18 stsp }
3202 5de5890b 2018-10-18 stsp
3203 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3204 5de5890b 2018-10-18 stsp if (error != NULL)
3205 c02c541e 2019-03-29 stsp goto done;
3206 c02c541e 2019-03-29 stsp
3207 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3208 c02c541e 2019-03-29 stsp if (error)
3209 5de5890b 2018-10-18 stsp goto done;
3210 5de5890b 2018-10-18 stsp
3211 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3212 9bf7a39b 2019-02-05 stsp if (worktree) {
3213 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3214 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3215 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3216 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3217 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3218 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3219 9bf7a39b 2019-02-05 stsp goto done;
3220 9bf7a39b 2019-02-05 stsp }
3221 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3222 9bf7a39b 2019-02-05 stsp free(p);
3223 9bf7a39b 2019-02-05 stsp if (error)
3224 9bf7a39b 2019-02-05 stsp goto done;
3225 9bf7a39b 2019-02-05 stsp } else
3226 9bf7a39b 2019-02-05 stsp path = "/";
3227 9bf7a39b 2019-02-05 stsp }
3228 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3229 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3230 9bf7a39b 2019-02-05 stsp if (error != NULL)
3231 9bf7a39b 2019-02-05 stsp goto done;
3232 9bf7a39b 2019-02-05 stsp }
3233 5de5890b 2018-10-18 stsp
3234 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3235 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3236 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3237 5de5890b 2018-10-18 stsp if (error != NULL)
3238 5de5890b 2018-10-18 stsp goto done;
3239 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3240 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3241 5de5890b 2018-10-18 stsp if (error != NULL)
3242 5de5890b 2018-10-18 stsp goto done;
3243 5de5890b 2018-10-18 stsp } else {
3244 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3245 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3246 30837e32 2019-07-25 stsp if (error)
3247 5de5890b 2018-10-18 stsp goto done;
3248 5de5890b 2018-10-18 stsp }
3249 5de5890b 2018-10-18 stsp
3250 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3251 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3252 5de5890b 2018-10-18 stsp done:
3253 5de5890b 2018-10-18 stsp free(in_repo_path);
3254 5de5890b 2018-10-18 stsp free(repo_path);
3255 5de5890b 2018-10-18 stsp free(cwd);
3256 5de5890b 2018-10-18 stsp free(commit_id);
3257 7a2c19d6 2019-02-05 stsp if (worktree)
3258 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3259 5de5890b 2018-10-18 stsp if (repo) {
3260 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3261 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3262 5de5890b 2018-10-18 stsp if (error == NULL)
3263 5de5890b 2018-10-18 stsp error = repo_error;
3264 5de5890b 2018-10-18 stsp }
3265 5de5890b 2018-10-18 stsp return error;
3266 5de5890b 2018-10-18 stsp }
3267 5de5890b 2018-10-18 stsp
3268 6bad629b 2019-02-04 stsp __dead static void
3269 6bad629b 2019-02-04 stsp usage_status(void)
3270 6bad629b 2019-02-04 stsp {
3271 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3272 6bad629b 2019-02-04 stsp exit(1);
3273 6bad629b 2019-02-04 stsp }
3274 5c860e29 2018-03-12 stsp
3275 b72f483a 2019-02-05 stsp static const struct got_error *
3276 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3277 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3278 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3279 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3280 6bad629b 2019-02-04 stsp {
3281 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3282 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3283 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3284 b72f483a 2019-02-05 stsp return NULL;
3285 6bad629b 2019-02-04 stsp }
3286 5c860e29 2018-03-12 stsp
3287 6bad629b 2019-02-04 stsp static const struct got_error *
3288 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3289 6bad629b 2019-02-04 stsp {
3290 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3291 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3292 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3293 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3294 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3295 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3296 a5edda0a 2019-07-27 stsp int ch;
3297 5c860e29 2018-03-12 stsp
3298 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3299 72ea6654 2019-07-27 stsp
3300 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3301 6bad629b 2019-02-04 stsp switch (ch) {
3302 5c860e29 2018-03-12 stsp default:
3303 2deda0b9 2019-03-07 stsp usage_status();
3304 6bad629b 2019-02-04 stsp /* NOTREACHED */
3305 5c860e29 2018-03-12 stsp }
3306 5c860e29 2018-03-12 stsp }
3307 5c860e29 2018-03-12 stsp
3308 6bad629b 2019-02-04 stsp argc -= optind;
3309 6bad629b 2019-02-04 stsp argv += optind;
3310 5c860e29 2018-03-12 stsp
3311 6bad629b 2019-02-04 stsp #ifndef PROFILE
3312 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3313 6bad629b 2019-02-04 stsp NULL) == -1)
3314 6bad629b 2019-02-04 stsp err(1, "pledge");
3315 f42b1b34 2018-03-12 stsp #endif
3316 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3317 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3318 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3319 927df6b7 2019-02-10 stsp goto done;
3320 927df6b7 2019-02-10 stsp }
3321 927df6b7 2019-02-10 stsp
3322 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3323 927df6b7 2019-02-10 stsp if (error != NULL)
3324 a5edda0a 2019-07-27 stsp goto done;
3325 6bad629b 2019-02-04 stsp
3326 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3327 c9956ddf 2019-09-08 stsp NULL);
3328 6bad629b 2019-02-04 stsp if (error != NULL)
3329 6bad629b 2019-02-04 stsp goto done;
3330 6bad629b 2019-02-04 stsp
3331 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3332 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3333 087fb88c 2019-08-04 stsp if (error)
3334 087fb88c 2019-08-04 stsp goto done;
3335 087fb88c 2019-08-04 stsp
3336 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3337 6bad629b 2019-02-04 stsp if (error)
3338 6bad629b 2019-02-04 stsp goto done;
3339 6bad629b 2019-02-04 stsp
3340 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3341 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3342 6bad629b 2019-02-04 stsp done:
3343 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3344 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3345 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3346 927df6b7 2019-02-10 stsp free(cwd);
3347 d0eebce4 2019-03-11 stsp return error;
3348 d0eebce4 2019-03-11 stsp }
3349 d0eebce4 2019-03-11 stsp
3350 d0eebce4 2019-03-11 stsp __dead static void
3351 d0eebce4 2019-03-11 stsp usage_ref(void)
3352 d0eebce4 2019-03-11 stsp {
3353 d0eebce4 2019-03-11 stsp fprintf(stderr,
3354 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3355 d0eebce4 2019-03-11 stsp getprogname());
3356 d0eebce4 2019-03-11 stsp exit(1);
3357 d0eebce4 2019-03-11 stsp }
3358 d0eebce4 2019-03-11 stsp
3359 d0eebce4 2019-03-11 stsp static const struct got_error *
3360 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3361 d0eebce4 2019-03-11 stsp {
3362 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3363 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3364 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3365 d0eebce4 2019-03-11 stsp
3366 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3367 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3368 d0eebce4 2019-03-11 stsp if (err)
3369 d0eebce4 2019-03-11 stsp return err;
3370 d0eebce4 2019-03-11 stsp
3371 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3372 d0eebce4 2019-03-11 stsp char *refstr;
3373 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3374 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3375 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3376 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3377 d0eebce4 2019-03-11 stsp free(refstr);
3378 d0eebce4 2019-03-11 stsp }
3379 d0eebce4 2019-03-11 stsp
3380 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3381 d0eebce4 2019-03-11 stsp return NULL;
3382 d0eebce4 2019-03-11 stsp }
3383 d0eebce4 2019-03-11 stsp
3384 d0eebce4 2019-03-11 stsp static const struct got_error *
3385 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3386 d0eebce4 2019-03-11 stsp {
3387 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3388 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3389 d0eebce4 2019-03-11 stsp
3390 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3391 d0eebce4 2019-03-11 stsp if (err)
3392 d0eebce4 2019-03-11 stsp return err;
3393 d0eebce4 2019-03-11 stsp
3394 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3395 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3396 d0eebce4 2019-03-11 stsp return err;
3397 d0eebce4 2019-03-11 stsp }
3398 d0eebce4 2019-03-11 stsp
3399 d0eebce4 2019-03-11 stsp static const struct got_error *
3400 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3401 d0eebce4 2019-03-11 stsp {
3402 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3403 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3404 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3405 d1644381 2019-07-14 stsp
3406 d1644381 2019-07-14 stsp /*
3407 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3408 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3409 d1644381 2019-07-14 stsp * an unintended typo.
3410 d1644381 2019-07-14 stsp */
3411 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3412 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3413 d0eebce4 2019-03-11 stsp
3414 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3415 dd88155e 2019-06-29 stsp repo);
3416 d83d9d5c 2019-05-13 stsp if (err) {
3417 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3418 d0eebce4 2019-03-11 stsp
3419 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3420 d83d9d5c 2019-05-13 stsp return err;
3421 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3422 d83d9d5c 2019-05-13 stsp if (err)
3423 d83d9d5c 2019-05-13 stsp return err;
3424 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3425 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3426 d83d9d5c 2019-05-13 stsp if (err)
3427 d83d9d5c 2019-05-13 stsp return err;
3428 d83d9d5c 2019-05-13 stsp }
3429 d83d9d5c 2019-05-13 stsp
3430 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3431 d0eebce4 2019-03-11 stsp if (err)
3432 d0eebce4 2019-03-11 stsp goto done;
3433 d0eebce4 2019-03-11 stsp
3434 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3435 d0eebce4 2019-03-11 stsp done:
3436 d0eebce4 2019-03-11 stsp if (ref)
3437 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3438 d0eebce4 2019-03-11 stsp free(id);
3439 d1c1ae5f 2019-08-12 stsp return err;
3440 d1c1ae5f 2019-08-12 stsp }
3441 d1c1ae5f 2019-08-12 stsp
3442 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3443 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3444 d1c1ae5f 2019-08-12 stsp {
3445 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3446 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3447 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3448 d1c1ae5f 2019-08-12 stsp
3449 d1c1ae5f 2019-08-12 stsp /*
3450 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3451 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3452 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3453 d1c1ae5f 2019-08-12 stsp */
3454 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3455 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3456 d1c1ae5f 2019-08-12 stsp
3457 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3458 d1c1ae5f 2019-08-12 stsp if (err)
3459 d1c1ae5f 2019-08-12 stsp return err;
3460 d1c1ae5f 2019-08-12 stsp
3461 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3462 d1c1ae5f 2019-08-12 stsp if (err)
3463 d1c1ae5f 2019-08-12 stsp goto done;
3464 d1c1ae5f 2019-08-12 stsp
3465 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3466 d1c1ae5f 2019-08-12 stsp done:
3467 d1c1ae5f 2019-08-12 stsp if (target_ref)
3468 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3469 d1c1ae5f 2019-08-12 stsp if (ref)
3470 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3471 d0eebce4 2019-03-11 stsp return err;
3472 d0eebce4 2019-03-11 stsp }
3473 d0eebce4 2019-03-11 stsp
3474 d0eebce4 2019-03-11 stsp static const struct got_error *
3475 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3476 d0eebce4 2019-03-11 stsp {
3477 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3478 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3479 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3480 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3481 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3482 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3483 d0eebce4 2019-03-11 stsp
3484 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3485 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3486 d0eebce4 2019-03-11 stsp switch (ch) {
3487 d0eebce4 2019-03-11 stsp case 'd':
3488 d0eebce4 2019-03-11 stsp delref = optarg;
3489 d0eebce4 2019-03-11 stsp break;
3490 d0eebce4 2019-03-11 stsp case 'r':
3491 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3492 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3493 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3494 9ba1d308 2019-10-21 stsp optarg);
3495 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3496 d0eebce4 2019-03-11 stsp break;
3497 d0eebce4 2019-03-11 stsp case 'l':
3498 d0eebce4 2019-03-11 stsp do_list = 1;
3499 d1c1ae5f 2019-08-12 stsp break;
3500 d1c1ae5f 2019-08-12 stsp case 's':
3501 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3502 d0eebce4 2019-03-11 stsp break;
3503 d0eebce4 2019-03-11 stsp default:
3504 d0eebce4 2019-03-11 stsp usage_ref();
3505 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3506 d0eebce4 2019-03-11 stsp }
3507 d0eebce4 2019-03-11 stsp }
3508 d0eebce4 2019-03-11 stsp
3509 d0eebce4 2019-03-11 stsp if (do_list && delref)
3510 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3511 d0eebce4 2019-03-11 stsp
3512 d0eebce4 2019-03-11 stsp argc -= optind;
3513 d0eebce4 2019-03-11 stsp argv += optind;
3514 d0eebce4 2019-03-11 stsp
3515 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3516 d1c1ae5f 2019-08-12 stsp if (create_symref)
3517 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3518 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3519 d0eebce4 2019-03-11 stsp if (argc > 0)
3520 d0eebce4 2019-03-11 stsp usage_ref();
3521 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3522 d0eebce4 2019-03-11 stsp usage_ref();
3523 d0eebce4 2019-03-11 stsp
3524 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3525 e0b57350 2019-03-12 stsp if (do_list) {
3526 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3527 e0b57350 2019-03-12 stsp NULL) == -1)
3528 e0b57350 2019-03-12 stsp err(1, "pledge");
3529 e0b57350 2019-03-12 stsp } else {
3530 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3531 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3532 e0b57350 2019-03-12 stsp err(1, "pledge");
3533 e0b57350 2019-03-12 stsp }
3534 d0eebce4 2019-03-11 stsp #endif
3535 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3536 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3537 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3538 d0eebce4 2019-03-11 stsp goto done;
3539 d0eebce4 2019-03-11 stsp }
3540 d0eebce4 2019-03-11 stsp
3541 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3542 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3543 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3544 d0eebce4 2019-03-11 stsp goto done;
3545 d0eebce4 2019-03-11 stsp else
3546 d0eebce4 2019-03-11 stsp error = NULL;
3547 d0eebce4 2019-03-11 stsp if (worktree) {
3548 d0eebce4 2019-03-11 stsp repo_path =
3549 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3550 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3551 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3552 d0eebce4 2019-03-11 stsp if (error)
3553 d0eebce4 2019-03-11 stsp goto done;
3554 d0eebce4 2019-03-11 stsp } else {
3555 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3556 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3557 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3558 d0eebce4 2019-03-11 stsp goto done;
3559 d0eebce4 2019-03-11 stsp }
3560 d0eebce4 2019-03-11 stsp }
3561 d0eebce4 2019-03-11 stsp }
3562 d0eebce4 2019-03-11 stsp
3563 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3564 d0eebce4 2019-03-11 stsp if (error != NULL)
3565 d0eebce4 2019-03-11 stsp goto done;
3566 d0eebce4 2019-03-11 stsp
3567 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3568 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3569 c02c541e 2019-03-29 stsp if (error)
3570 c02c541e 2019-03-29 stsp goto done;
3571 c02c541e 2019-03-29 stsp
3572 d0eebce4 2019-03-11 stsp if (do_list)
3573 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3574 d0eebce4 2019-03-11 stsp else if (delref)
3575 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3576 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3577 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3578 d0eebce4 2019-03-11 stsp else
3579 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3580 4e759de4 2019-06-26 stsp done:
3581 4e759de4 2019-06-26 stsp if (repo)
3582 4e759de4 2019-06-26 stsp got_repo_close(repo);
3583 4e759de4 2019-06-26 stsp if (worktree)
3584 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3585 4e759de4 2019-06-26 stsp free(cwd);
3586 4e759de4 2019-06-26 stsp free(repo_path);
3587 4e759de4 2019-06-26 stsp return error;
3588 4e759de4 2019-06-26 stsp }
3589 4e759de4 2019-06-26 stsp
3590 4e759de4 2019-06-26 stsp __dead static void
3591 4e759de4 2019-06-26 stsp usage_branch(void)
3592 4e759de4 2019-06-26 stsp {
3593 4e759de4 2019-06-26 stsp fprintf(stderr,
3594 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3595 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3596 4e759de4 2019-06-26 stsp exit(1);
3597 4e759de4 2019-06-26 stsp }
3598 4e759de4 2019-06-26 stsp
3599 4e759de4 2019-06-26 stsp static const struct got_error *
3600 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3601 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3602 4e99b47e 2019-10-04 stsp {
3603 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3604 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3605 4e99b47e 2019-10-04 stsp char *refstr;
3606 4e99b47e 2019-10-04 stsp
3607 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3608 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3609 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3610 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3611 4e99b47e 2019-10-04 stsp
3612 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3613 4e99b47e 2019-10-04 stsp if (err)
3614 4e99b47e 2019-10-04 stsp return err;
3615 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3616 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3617 4e99b47e 2019-10-04 stsp marker = "* ";
3618 4e99b47e 2019-10-04 stsp else
3619 4e99b47e 2019-10-04 stsp marker = "~ ";
3620 4e99b47e 2019-10-04 stsp free(id);
3621 4e99b47e 2019-10-04 stsp }
3622 4e99b47e 2019-10-04 stsp
3623 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3624 4e99b47e 2019-10-04 stsp refname += 11;
3625 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3626 4e99b47e 2019-10-04 stsp refname += 18;
3627 4e99b47e 2019-10-04 stsp
3628 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3629 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3630 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3631 4e99b47e 2019-10-04 stsp
3632 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3633 4e99b47e 2019-10-04 stsp free(refstr);
3634 4e99b47e 2019-10-04 stsp return NULL;
3635 4e99b47e 2019-10-04 stsp }
3636 4e99b47e 2019-10-04 stsp
3637 4e99b47e 2019-10-04 stsp static const struct got_error *
3638 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3639 ad89fa31 2019-10-04 stsp {
3640 ad89fa31 2019-10-04 stsp const char *refname;
3641 ad89fa31 2019-10-04 stsp
3642 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3643 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3644 ad89fa31 2019-10-04 stsp
3645 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3646 ad89fa31 2019-10-04 stsp
3647 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3648 ad89fa31 2019-10-04 stsp refname += 11;
3649 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3650 ad89fa31 2019-10-04 stsp refname += 18;
3651 ad89fa31 2019-10-04 stsp
3652 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3653 ad89fa31 2019-10-04 stsp
3654 ad89fa31 2019-10-04 stsp return NULL;
3655 ad89fa31 2019-10-04 stsp }
3656 ad89fa31 2019-10-04 stsp
3657 ad89fa31 2019-10-04 stsp static const struct got_error *
3658 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3659 4e759de4 2019-06-26 stsp {
3660 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3661 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3662 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3663 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3664 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3665 4e759de4 2019-06-26 stsp
3666 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3667 ba882ee3 2019-07-11 stsp
3668 4e99b47e 2019-10-04 stsp if (worktree) {
3669 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3670 4e99b47e 2019-10-04 stsp worktree);
3671 4e99b47e 2019-10-04 stsp if (err)
3672 4e99b47e 2019-10-04 stsp return err;
3673 4e99b47e 2019-10-04 stsp
3674 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3675 4e99b47e 2019-10-04 stsp worktree);
3676 4e99b47e 2019-10-04 stsp if (err)
3677 4e99b47e 2019-10-04 stsp return err;
3678 4e99b47e 2019-10-04 stsp
3679 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3680 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3681 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3682 4e99b47e 2019-10-04 stsp if (err)
3683 4e99b47e 2019-10-04 stsp return err;
3684 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3685 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3686 4e99b47e 2019-10-04 stsp }
3687 4e99b47e 2019-10-04 stsp }
3688 4e99b47e 2019-10-04 stsp
3689 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3690 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3691 4e759de4 2019-06-26 stsp if (err)
3692 4e759de4 2019-06-26 stsp return err;
3693 4e759de4 2019-06-26 stsp
3694 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3695 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3696 4e759de4 2019-06-26 stsp
3697 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3698 4e759de4 2019-06-26 stsp return NULL;
3699 4e759de4 2019-06-26 stsp }
3700 4e759de4 2019-06-26 stsp
3701 4e759de4 2019-06-26 stsp static const struct got_error *
3702 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3703 45cd4e47 2019-08-25 stsp const char *branch_name)
3704 4e759de4 2019-06-26 stsp {
3705 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3706 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3707 4e759de4 2019-06-26 stsp char *refname;
3708 4e759de4 2019-06-26 stsp
3709 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3710 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3711 4e759de4 2019-06-26 stsp
3712 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3713 4e759de4 2019-06-26 stsp if (err)
3714 4e759de4 2019-06-26 stsp goto done;
3715 4e759de4 2019-06-26 stsp
3716 45cd4e47 2019-08-25 stsp if (worktree &&
3717 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3718 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3719 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3720 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3721 45cd4e47 2019-08-25 stsp goto done;
3722 45cd4e47 2019-08-25 stsp }
3723 45cd4e47 2019-08-25 stsp
3724 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3725 4e759de4 2019-06-26 stsp done:
3726 45cd4e47 2019-08-25 stsp if (ref)
3727 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3728 4e759de4 2019-06-26 stsp free(refname);
3729 4e759de4 2019-06-26 stsp return err;
3730 4e759de4 2019-06-26 stsp }
3731 4e759de4 2019-06-26 stsp
3732 4e759de4 2019-06-26 stsp static const struct got_error *
3733 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3734 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3735 4e759de4 2019-06-26 stsp {
3736 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3737 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3738 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3739 d3f84d51 2019-07-11 stsp
3740 d3f84d51 2019-07-11 stsp /*
3741 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3742 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3743 d3f84d51 2019-07-11 stsp * an unintended typo.
3744 d3f84d51 2019-07-11 stsp */
3745 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3746 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3747 4e759de4 2019-06-26 stsp
3748 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3749 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3750 4e759de4 2019-06-26 stsp goto done;
3751 4e759de4 2019-06-26 stsp }
3752 4e759de4 2019-06-26 stsp
3753 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3754 4e759de4 2019-06-26 stsp if (err == NULL) {
3755 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3756 4e759de4 2019-06-26 stsp goto done;
3757 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3758 4e759de4 2019-06-26 stsp goto done;
3759 4e759de4 2019-06-26 stsp
3760 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3761 4e759de4 2019-06-26 stsp if (err)
3762 4e759de4 2019-06-26 stsp goto done;
3763 4e759de4 2019-06-26 stsp
3764 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3765 d0eebce4 2019-03-11 stsp done:
3766 4e759de4 2019-06-26 stsp if (ref)
3767 4e759de4 2019-06-26 stsp got_ref_close(ref);
3768 4e759de4 2019-06-26 stsp free(base_refname);
3769 4e759de4 2019-06-26 stsp free(refname);
3770 4e759de4 2019-06-26 stsp return err;
3771 4e759de4 2019-06-26 stsp }
3772 4e759de4 2019-06-26 stsp
3773 4e759de4 2019-06-26 stsp static const struct got_error *
3774 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3775 4e759de4 2019-06-26 stsp {
3776 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3777 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3778 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3779 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3780 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3781 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3782 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3783 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3784 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3785 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3786 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3787 4e759de4 2019-06-26 stsp
3788 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3789 da76fce2 2020-02-24 stsp
3790 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3791 4e759de4 2019-06-26 stsp switch (ch) {
3792 a74f7e83 2019-11-10 stsp case 'c':
3793 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3794 a74f7e83 2019-11-10 stsp break;
3795 4e759de4 2019-06-26 stsp case 'd':
3796 4e759de4 2019-06-26 stsp delref = optarg;
3797 4e759de4 2019-06-26 stsp break;
3798 4e759de4 2019-06-26 stsp case 'r':
3799 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3800 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3801 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3802 9ba1d308 2019-10-21 stsp optarg);
3803 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3804 4e759de4 2019-06-26 stsp break;
3805 4e759de4 2019-06-26 stsp case 'l':
3806 4e759de4 2019-06-26 stsp do_list = 1;
3807 da76fce2 2020-02-24 stsp break;
3808 da76fce2 2020-02-24 stsp case 'n':
3809 da76fce2 2020-02-24 stsp do_update = 0;
3810 4e759de4 2019-06-26 stsp break;
3811 4e759de4 2019-06-26 stsp default:
3812 4e759de4 2019-06-26 stsp usage_branch();
3813 4e759de4 2019-06-26 stsp /* NOTREACHED */
3814 4e759de4 2019-06-26 stsp }
3815 4e759de4 2019-06-26 stsp }
3816 4e759de4 2019-06-26 stsp
3817 4e759de4 2019-06-26 stsp if (do_list && delref)
3818 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3819 4e759de4 2019-06-26 stsp
3820 4e759de4 2019-06-26 stsp argc -= optind;
3821 4e759de4 2019-06-26 stsp argv += optind;
3822 ad89fa31 2019-10-04 stsp
3823 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3824 ad89fa31 2019-10-04 stsp do_show = 1;
3825 4e759de4 2019-06-26 stsp
3826 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3827 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3828 a74f7e83 2019-11-10 stsp
3829 4e759de4 2019-06-26 stsp if (do_list || delref) {
3830 4e759de4 2019-06-26 stsp if (argc > 0)
3831 4e759de4 2019-06-26 stsp usage_branch();
3832 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3833 4e759de4 2019-06-26 stsp usage_branch();
3834 4e759de4 2019-06-26 stsp
3835 4e759de4 2019-06-26 stsp #ifndef PROFILE
3836 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3837 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3838 4e759de4 2019-06-26 stsp NULL) == -1)
3839 4e759de4 2019-06-26 stsp err(1, "pledge");
3840 4e759de4 2019-06-26 stsp } else {
3841 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3842 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3843 4e759de4 2019-06-26 stsp err(1, "pledge");
3844 4e759de4 2019-06-26 stsp }
3845 4e759de4 2019-06-26 stsp #endif
3846 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3847 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3848 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3849 4e759de4 2019-06-26 stsp goto done;
3850 4e759de4 2019-06-26 stsp }
3851 4e759de4 2019-06-26 stsp
3852 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3853 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3854 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3855 4e759de4 2019-06-26 stsp goto done;
3856 4e759de4 2019-06-26 stsp else
3857 4e759de4 2019-06-26 stsp error = NULL;
3858 4e759de4 2019-06-26 stsp if (worktree) {
3859 4e759de4 2019-06-26 stsp repo_path =
3860 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3861 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3862 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3863 4e759de4 2019-06-26 stsp if (error)
3864 4e759de4 2019-06-26 stsp goto done;
3865 4e759de4 2019-06-26 stsp } else {
3866 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3867 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3868 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3869 4e759de4 2019-06-26 stsp goto done;
3870 4e759de4 2019-06-26 stsp }
3871 4e759de4 2019-06-26 stsp }
3872 4e759de4 2019-06-26 stsp }
3873 4e759de4 2019-06-26 stsp
3874 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3875 4e759de4 2019-06-26 stsp if (error != NULL)
3876 4e759de4 2019-06-26 stsp goto done;
3877 4e759de4 2019-06-26 stsp
3878 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3879 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3880 4e759de4 2019-06-26 stsp if (error)
3881 4e759de4 2019-06-26 stsp goto done;
3882 4e759de4 2019-06-26 stsp
3883 ad89fa31 2019-10-04 stsp if (do_show)
3884 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3885 ad89fa31 2019-10-04 stsp else if (do_list)
3886 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3887 4e759de4 2019-06-26 stsp else if (delref)
3888 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3889 4e759de4 2019-06-26 stsp else {
3890 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3891 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3892 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3893 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3894 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3895 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3896 a74f7e83 2019-11-10 stsp if (error)
3897 a74f7e83 2019-11-10 stsp goto done;
3898 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3899 da76fce2 2020-02-24 stsp if (error)
3900 da76fce2 2020-02-24 stsp goto done;
3901 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3902 da76fce2 2020-02-24 stsp int did_something = 0;
3903 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3904 da76fce2 2020-02-24 stsp
3905 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3906 da76fce2 2020-02-24 stsp if (error)
3907 da76fce2 2020-02-24 stsp goto done;
3908 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3909 da76fce2 2020-02-24 stsp worktree);
3910 da76fce2 2020-02-24 stsp if (error)
3911 da76fce2 2020-02-24 stsp goto done;
3912 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3913 da76fce2 2020-02-24 stsp == -1) {
3914 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3915 da76fce2 2020-02-24 stsp goto done;
3916 da76fce2 2020-02-24 stsp }
3917 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3918 da76fce2 2020-02-24 stsp free(branch_refname);
3919 da76fce2 2020-02-24 stsp if (error)
3920 da76fce2 2020-02-24 stsp goto done;
3921 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3922 da76fce2 2020-02-24 stsp repo);
3923 da76fce2 2020-02-24 stsp if (error)
3924 da76fce2 2020-02-24 stsp goto done;
3925 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3926 da76fce2 2020-02-24 stsp commit_id);
3927 da76fce2 2020-02-24 stsp if (error)
3928 da76fce2 2020-02-24 stsp goto done;
3929 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3930 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3931 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3932 da76fce2 2020-02-24 stsp if (error)
3933 da76fce2 2020-02-24 stsp goto done;
3934 da76fce2 2020-02-24 stsp if (did_something)
3935 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3936 da76fce2 2020-02-24 stsp }
3937 4e759de4 2019-06-26 stsp }
3938 4e759de4 2019-06-26 stsp done:
3939 da76fce2 2020-02-24 stsp if (ref)
3940 da76fce2 2020-02-24 stsp got_ref_close(ref);
3941 d0eebce4 2019-03-11 stsp if (repo)
3942 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3943 d0eebce4 2019-03-11 stsp if (worktree)
3944 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3945 d0eebce4 2019-03-11 stsp free(cwd);
3946 d0eebce4 2019-03-11 stsp free(repo_path);
3947 da76fce2 2020-02-24 stsp free(commit_id);
3948 da76fce2 2020-02-24 stsp free(commit_id_str);
3949 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3950 da76fce2 2020-02-24 stsp free((char *)pe->path);
3951 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3952 d00136be 2019-03-26 stsp return error;
3953 d00136be 2019-03-26 stsp }
3954 d00136be 2019-03-26 stsp
3955 8e7bd50a 2019-08-22 stsp
3956 d00136be 2019-03-26 stsp __dead static void
3957 8e7bd50a 2019-08-22 stsp usage_tag(void)
3958 8e7bd50a 2019-08-22 stsp {
3959 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3960 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3961 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3962 8e7bd50a 2019-08-22 stsp exit(1);
3963 b8bad2ba 2019-08-23 stsp }
3964 b8bad2ba 2019-08-23 stsp
3965 b8bad2ba 2019-08-23 stsp #if 0
3966 b8bad2ba 2019-08-23 stsp static const struct got_error *
3967 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3968 b8bad2ba 2019-08-23 stsp {
3969 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3970 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3971 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3972 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3973 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3974 b8bad2ba 2019-08-23 stsp
3975 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3976 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3977 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3978 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3979 b8bad2ba 2019-08-23 stsp if (err)
3980 b8bad2ba 2019-08-23 stsp return err;
3981 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3982 b8bad2ba 2019-08-23 stsp continue;
3983 b8bad2ba 2019-08-23 stsp } else {
3984 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3985 b8bad2ba 2019-08-23 stsp if (err)
3986 b8bad2ba 2019-08-23 stsp break;
3987 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3988 b8bad2ba 2019-08-23 stsp free(re_id);
3989 b8bad2ba 2019-08-23 stsp if (err)
3990 b8bad2ba 2019-08-23 stsp break;
3991 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3992 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3993 b8bad2ba 2019-08-23 stsp }
3994 b8bad2ba 2019-08-23 stsp
3995 b8bad2ba 2019-08-23 stsp while (se) {
3996 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3997 b8bad2ba 2019-08-23 stsp if (err)
3998 b8bad2ba 2019-08-23 stsp break;
3999 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4000 b8bad2ba 2019-08-23 stsp free(se_id);
4001 b8bad2ba 2019-08-23 stsp if (err)
4002 b8bad2ba 2019-08-23 stsp break;
4003 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4004 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4005 b8bad2ba 2019-08-23 stsp
4006 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4007 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4008 b8bad2ba 2019-08-23 stsp if (err)
4009 b8bad2ba 2019-08-23 stsp return err;
4010 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4011 b8bad2ba 2019-08-23 stsp break;
4012 b8bad2ba 2019-08-23 stsp }
4013 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4014 b8bad2ba 2019-08-23 stsp continue;
4015 b8bad2ba 2019-08-23 stsp }
4016 b8bad2ba 2019-08-23 stsp }
4017 b8bad2ba 2019-08-23 stsp done:
4018 b8bad2ba 2019-08-23 stsp return err;
4019 8e7bd50a 2019-08-22 stsp }
4020 b8bad2ba 2019-08-23 stsp #endif
4021 b8bad2ba 2019-08-23 stsp
4022 b8bad2ba 2019-08-23 stsp static const struct got_error *
4023 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4024 8e7bd50a 2019-08-22 stsp {
4025 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4026 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4027 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4028 8e7bd50a 2019-08-22 stsp
4029 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4030 8e7bd50a 2019-08-22 stsp
4031 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4032 8e7bd50a 2019-08-22 stsp if (err)
4033 8e7bd50a 2019-08-22 stsp return err;
4034 8e7bd50a 2019-08-22 stsp
4035 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4036 8e7bd50a 2019-08-22 stsp const char *refname;
4037 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4038 8e7bd50a 2019-08-22 stsp char datebuf[26];
4039 d4efa91b 2020-01-14 stsp const char *tagger;
4040 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4041 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4042 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4043 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4044 8e7bd50a 2019-08-22 stsp
4045 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4046 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4047 8e7bd50a 2019-08-22 stsp continue;
4048 8e7bd50a 2019-08-22 stsp refname += 10;
4049 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4050 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4051 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4052 8e7bd50a 2019-08-22 stsp break;
4053 8e7bd50a 2019-08-22 stsp }
4054 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4055 8e7bd50a 2019-08-22 stsp free(refstr);
4056 8e7bd50a 2019-08-22 stsp
4057 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4058 8e7bd50a 2019-08-22 stsp if (err)
4059 8e7bd50a 2019-08-22 stsp break;
4060 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4061 d4efa91b 2020-01-14 stsp if (err) {
4062 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4063 d4efa91b 2020-01-14 stsp free(id);
4064 d4efa91b 2020-01-14 stsp break;
4065 d4efa91b 2020-01-14 stsp }
4066 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4067 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4068 d4efa91b 2020-01-14 stsp if (err) {
4069 d4efa91b 2020-01-14 stsp free(id);
4070 d4efa91b 2020-01-14 stsp break;
4071 d4efa91b 2020-01-14 stsp }
4072 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4073 d4efa91b 2020-01-14 stsp tagger_time =
4074 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4075 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4076 d4efa91b 2020-01-14 stsp free(id);
4077 d4efa91b 2020-01-14 stsp if (err)
4078 d4efa91b 2020-01-14 stsp break;
4079 d4efa91b 2020-01-14 stsp } else {
4080 d4efa91b 2020-01-14 stsp free(id);
4081 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4082 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4083 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4084 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4085 d4efa91b 2020-01-14 stsp if (err)
4086 d4efa91b 2020-01-14 stsp break;
4087 d4efa91b 2020-01-14 stsp }
4088 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4089 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4090 2417344c 2019-08-23 stsp if (datestr)
4091 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4092 d4efa91b 2020-01-14 stsp if (commit)
4093 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4094 d4efa91b 2020-01-14 stsp else {
4095 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4096 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4097 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4098 d4efa91b 2020-01-14 stsp id_str);
4099 d4efa91b 2020-01-14 stsp break;
4100 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4101 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4102 d4efa91b 2020-01-14 stsp id_str);
4103 d4efa91b 2020-01-14 stsp break;
4104 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4105 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4106 d4efa91b 2020-01-14 stsp id_str);
4107 d4efa91b 2020-01-14 stsp break;
4108 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4109 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4110 d4efa91b 2020-01-14 stsp id_str);
4111 d4efa91b 2020-01-14 stsp break;
4112 d4efa91b 2020-01-14 stsp default:
4113 d4efa91b 2020-01-14 stsp break;
4114 d4efa91b 2020-01-14 stsp }
4115 8e7bd50a 2019-08-22 stsp }
4116 8e7bd50a 2019-08-22 stsp free(id_str);
4117 d4efa91b 2020-01-14 stsp if (commit) {
4118 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4119 d4efa91b 2020-01-14 stsp if (err)
4120 d4efa91b 2020-01-14 stsp break;
4121 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4122 d4efa91b 2020-01-14 stsp } else {
4123 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4124 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4125 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4126 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4127 d4efa91b 2020-01-14 stsp break;
4128 d4efa91b 2020-01-14 stsp }
4129 8e7bd50a 2019-08-22 stsp }
4130 8e7bd50a 2019-08-22 stsp
4131 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4132 8e7bd50a 2019-08-22 stsp do {
4133 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4134 8e7bd50a 2019-08-22 stsp if (line)
4135 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4136 8e7bd50a 2019-08-22 stsp } while (line);
4137 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4138 8e7bd50a 2019-08-22 stsp }
4139 8e7bd50a 2019-08-22 stsp
4140 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4141 8e7bd50a 2019-08-22 stsp return NULL;
4142 8e7bd50a 2019-08-22 stsp }
4143 8e7bd50a 2019-08-22 stsp
4144 8e7bd50a 2019-08-22 stsp static const struct got_error *
4145 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4146 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4147 8e7bd50a 2019-08-22 stsp {
4148 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4149 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4150 f372d5cd 2019-10-21 stsp char *editor = NULL;
4151 8e7bd50a 2019-08-22 stsp int fd = -1;
4152 8e7bd50a 2019-08-22 stsp
4153 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4154 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4155 8e7bd50a 2019-08-22 stsp goto done;
4156 8e7bd50a 2019-08-22 stsp }
4157 8e7bd50a 2019-08-22 stsp
4158 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4159 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4160 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4161 8e7bd50a 2019-08-22 stsp goto done;
4162 8e7bd50a 2019-08-22 stsp }
4163 8e7bd50a 2019-08-22 stsp
4164 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4165 8e7bd50a 2019-08-22 stsp if (err)
4166 8e7bd50a 2019-08-22 stsp goto done;
4167 8e7bd50a 2019-08-22 stsp
4168 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4169 8e7bd50a 2019-08-22 stsp close(fd);
4170 8e7bd50a 2019-08-22 stsp
4171 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4172 8e7bd50a 2019-08-22 stsp if (err)
4173 8e7bd50a 2019-08-22 stsp goto done;
4174 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4175 8e7bd50a 2019-08-22 stsp done:
4176 8e7bd50a 2019-08-22 stsp free(initial_content);
4177 8e7bd50a 2019-08-22 stsp free(template);
4178 8e7bd50a 2019-08-22 stsp free(editor);
4179 8e7bd50a 2019-08-22 stsp
4180 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4181 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4182 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4183 8e7bd50a 2019-08-22 stsp if (err) {
4184 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4185 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4186 8e7bd50a 2019-08-22 stsp }
4187 8e7bd50a 2019-08-22 stsp }
4188 8e7bd50a 2019-08-22 stsp return err;
4189 8e7bd50a 2019-08-22 stsp }
4190 8e7bd50a 2019-08-22 stsp
4191 8e7bd50a 2019-08-22 stsp static const struct got_error *
4192 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4193 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4194 8e7bd50a 2019-08-22 stsp {
4195 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4196 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4197 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4198 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4199 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4200 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4201 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4202 8e7bd50a 2019-08-22 stsp
4203 8e7bd50a 2019-08-22 stsp /*
4204 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4205 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4206 8e7bd50a 2019-08-22 stsp * an unintended typo.
4207 8e7bd50a 2019-08-22 stsp */
4208 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4209 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4210 8e7bd50a 2019-08-22 stsp
4211 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4212 8e7bd50a 2019-08-22 stsp if (err)
4213 8e7bd50a 2019-08-22 stsp return err;
4214 8e7bd50a 2019-08-22 stsp
4215 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4216 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4217 8e7bd50a 2019-08-22 stsp if (err)
4218 8e7bd50a 2019-08-22 stsp goto done;
4219 8e7bd50a 2019-08-22 stsp
4220 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4221 8e7bd50a 2019-08-22 stsp if (err)
4222 8e7bd50a 2019-08-22 stsp goto done;
4223 8e7bd50a 2019-08-22 stsp
4224 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4225 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4226 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4227 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4228 8e7bd50a 2019-08-22 stsp goto done;
4229 8e7bd50a 2019-08-22 stsp }
4230 8e7bd50a 2019-08-22 stsp tag_name += 10;
4231 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4232 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4233 8e7bd50a 2019-08-22 stsp goto done;
4234 8e7bd50a 2019-08-22 stsp }
4235 8e7bd50a 2019-08-22 stsp
4236 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4237 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4238 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4239 8e7bd50a 2019-08-22 stsp goto done;
4240 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4241 8e7bd50a 2019-08-22 stsp goto done;
4242 8e7bd50a 2019-08-22 stsp
4243 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4244 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4245 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4246 f372d5cd 2019-10-21 stsp if (err) {
4247 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4248 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4249 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4250 8e7bd50a 2019-08-22 stsp goto done;
4251 f372d5cd 2019-10-21 stsp }
4252 8e7bd50a 2019-08-22 stsp }
4253 8e7bd50a 2019-08-22 stsp
4254 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4255 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4256 f372d5cd 2019-10-21 stsp if (err) {
4257 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4258 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4259 8e7bd50a 2019-08-22 stsp goto done;
4260 f372d5cd 2019-10-21 stsp }
4261 8e7bd50a 2019-08-22 stsp
4262 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4263 f372d5cd 2019-10-21 stsp if (err) {
4264 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4265 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4266 8e7bd50a 2019-08-22 stsp goto done;
4267 f372d5cd 2019-10-21 stsp }
4268 8e7bd50a 2019-08-22 stsp
4269 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4270 f372d5cd 2019-10-21 stsp if (err) {
4271 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4272 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4273 f372d5cd 2019-10-21 stsp goto done;
4274 f372d5cd 2019-10-21 stsp }
4275 8e7bd50a 2019-08-22 stsp
4276 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4277 f372d5cd 2019-10-21 stsp if (err) {
4278 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4279 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4280 f372d5cd 2019-10-21 stsp goto done;
4281 8e7bd50a 2019-08-22 stsp }
4282 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4283 8e7bd50a 2019-08-22 stsp done:
4284 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4285 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4286 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4287 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4288 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4289 f372d5cd 2019-10-21 stsp free(tag_id_str);
4290 8e7bd50a 2019-08-22 stsp if (ref)
4291 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4292 8e7bd50a 2019-08-22 stsp free(commit_id);
4293 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4294 8e7bd50a 2019-08-22 stsp free(refname);
4295 8e7bd50a 2019-08-22 stsp free(tagmsg);
4296 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4297 aba9c984 2019-09-08 stsp free(tagger);
4298 8e7bd50a 2019-08-22 stsp return err;
4299 8e7bd50a 2019-08-22 stsp }
4300 8e7bd50a 2019-08-22 stsp
4301 8e7bd50a 2019-08-22 stsp static const struct got_error *
4302 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4303 8e7bd50a 2019-08-22 stsp {
4304 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4305 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4306 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4307 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4308 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4309 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4310 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4311 8e7bd50a 2019-08-22 stsp
4312 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4313 8e7bd50a 2019-08-22 stsp switch (ch) {
4314 80106605 2020-02-24 stsp case 'c':
4315 80106605 2020-02-24 stsp commit_id_arg = optarg;
4316 80106605 2020-02-24 stsp break;
4317 8e7bd50a 2019-08-22 stsp case 'm':
4318 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4319 8e7bd50a 2019-08-22 stsp break;
4320 8e7bd50a 2019-08-22 stsp case 'r':
4321 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4322 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4323 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4324 9ba1d308 2019-10-21 stsp optarg);
4325 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4326 8e7bd50a 2019-08-22 stsp break;
4327 8e7bd50a 2019-08-22 stsp case 'l':
4328 8e7bd50a 2019-08-22 stsp do_list = 1;
4329 8e7bd50a 2019-08-22 stsp break;
4330 8e7bd50a 2019-08-22 stsp default:
4331 8e7bd50a 2019-08-22 stsp usage_tag();
4332 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4333 8e7bd50a 2019-08-22 stsp }
4334 8e7bd50a 2019-08-22 stsp }
4335 8e7bd50a 2019-08-22 stsp
4336 8e7bd50a 2019-08-22 stsp argc -= optind;
4337 8e7bd50a 2019-08-22 stsp argv += optind;
4338 8e7bd50a 2019-08-22 stsp
4339 8e7bd50a 2019-08-22 stsp if (do_list) {
4340 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4341 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4342 8e7bd50a 2019-08-22 stsp if (tagmsg)
4343 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4344 8e7bd50a 2019-08-22 stsp if (argc > 0)
4345 8e7bd50a 2019-08-22 stsp usage_tag();
4346 80106605 2020-02-24 stsp } else if (argc != 1)
4347 8e7bd50a 2019-08-22 stsp usage_tag();
4348 80106605 2020-02-24 stsp
4349 a2887370 2019-08-23 stsp tag_name = argv[0];
4350 8e7bd50a 2019-08-22 stsp
4351 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4352 8e7bd50a 2019-08-22 stsp if (do_list) {
4353 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4354 8e7bd50a 2019-08-22 stsp NULL) == -1)
4355 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4356 8e7bd50a 2019-08-22 stsp } else {
4357 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4358 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4359 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4360 8e7bd50a 2019-08-22 stsp }
4361 8e7bd50a 2019-08-22 stsp #endif
4362 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4363 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4364 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4365 8e7bd50a 2019-08-22 stsp goto done;
4366 8e7bd50a 2019-08-22 stsp }
4367 8e7bd50a 2019-08-22 stsp
4368 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4369 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4370 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4371 8e7bd50a 2019-08-22 stsp goto done;
4372 8e7bd50a 2019-08-22 stsp else
4373 8e7bd50a 2019-08-22 stsp error = NULL;
4374 8e7bd50a 2019-08-22 stsp if (worktree) {
4375 8e7bd50a 2019-08-22 stsp repo_path =
4376 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4377 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4378 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4379 8e7bd50a 2019-08-22 stsp if (error)
4380 8e7bd50a 2019-08-22 stsp goto done;
4381 8e7bd50a 2019-08-22 stsp } else {
4382 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4383 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4384 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4385 8e7bd50a 2019-08-22 stsp goto done;
4386 8e7bd50a 2019-08-22 stsp }
4387 8e7bd50a 2019-08-22 stsp }
4388 8e7bd50a 2019-08-22 stsp }
4389 8e7bd50a 2019-08-22 stsp
4390 8e7bd50a 2019-08-22 stsp if (do_list) {
4391 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4392 c9956ddf 2019-09-08 stsp if (error != NULL)
4393 c9956ddf 2019-09-08 stsp goto done;
4394 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4395 8e7bd50a 2019-08-22 stsp if (error)
4396 8e7bd50a 2019-08-22 stsp goto done;
4397 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4398 8e7bd50a 2019-08-22 stsp } else {
4399 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4400 c9956ddf 2019-09-08 stsp if (error)
4401 c9956ddf 2019-09-08 stsp goto done;
4402 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4403 c9956ddf 2019-09-08 stsp if (error != NULL)
4404 c9956ddf 2019-09-08 stsp goto done;
4405 c9956ddf 2019-09-08 stsp
4406 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4407 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4408 8e7bd50a 2019-08-22 stsp if (error)
4409 8e7bd50a 2019-08-22 stsp goto done;
4410 8e7bd50a 2019-08-22 stsp }
4411 8e7bd50a 2019-08-22 stsp
4412 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4413 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4414 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4415 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4416 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4417 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4418 8e7bd50a 2019-08-22 stsp if (error)
4419 8e7bd50a 2019-08-22 stsp goto done;
4420 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4421 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4422 8e7bd50a 2019-08-22 stsp if (error)
4423 8e7bd50a 2019-08-22 stsp goto done;
4424 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4425 8e7bd50a 2019-08-22 stsp free(commit_id);
4426 8e7bd50a 2019-08-22 stsp if (error)
4427 8e7bd50a 2019-08-22 stsp goto done;
4428 8e7bd50a 2019-08-22 stsp }
4429 8e7bd50a 2019-08-22 stsp
4430 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4431 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4432 8e7bd50a 2019-08-22 stsp }
4433 8e7bd50a 2019-08-22 stsp done:
4434 8e7bd50a 2019-08-22 stsp if (repo)
4435 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4436 8e7bd50a 2019-08-22 stsp if (worktree)
4437 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4438 8e7bd50a 2019-08-22 stsp free(cwd);
4439 8e7bd50a 2019-08-22 stsp free(repo_path);
4440 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4441 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4442 8e7bd50a 2019-08-22 stsp return error;
4443 8e7bd50a 2019-08-22 stsp }
4444 8e7bd50a 2019-08-22 stsp
4445 8e7bd50a 2019-08-22 stsp __dead static void
4446 d00136be 2019-03-26 stsp usage_add(void)
4447 d00136be 2019-03-26 stsp {
4448 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4449 022fae89 2019-12-06 tracey getprogname());
4450 d00136be 2019-03-26 stsp exit(1);
4451 d00136be 2019-03-26 stsp }
4452 d00136be 2019-03-26 stsp
4453 d00136be 2019-03-26 stsp static const struct got_error *
4454 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4455 4e68cba3 2019-11-23 stsp {
4456 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4457 4e68cba3 2019-11-23 stsp path++;
4458 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4459 4e68cba3 2019-11-23 stsp return NULL;
4460 4e68cba3 2019-11-23 stsp }
4461 4e68cba3 2019-11-23 stsp
4462 4e68cba3 2019-11-23 stsp static const struct got_error *
4463 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4464 d00136be 2019-03-26 stsp {
4465 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4466 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4467 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4468 1dd54920 2019-05-11 stsp char *cwd = NULL;
4469 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4470 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4471 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4472 1dd54920 2019-05-11 stsp
4473 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4474 d00136be 2019-03-26 stsp
4475 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4476 d00136be 2019-03-26 stsp switch (ch) {
4477 022fae89 2019-12-06 tracey case 'I':
4478 022fae89 2019-12-06 tracey no_ignores = 1;
4479 022fae89 2019-12-06 tracey break;
4480 4e68cba3 2019-11-23 stsp case 'R':
4481 4e68cba3 2019-11-23 stsp can_recurse = 1;
4482 4e68cba3 2019-11-23 stsp break;
4483 d00136be 2019-03-26 stsp default:
4484 d00136be 2019-03-26 stsp usage_add();
4485 d00136be 2019-03-26 stsp /* NOTREACHED */
4486 d00136be 2019-03-26 stsp }
4487 d00136be 2019-03-26 stsp }
4488 d00136be 2019-03-26 stsp
4489 d00136be 2019-03-26 stsp argc -= optind;
4490 d00136be 2019-03-26 stsp argv += optind;
4491 d00136be 2019-03-26 stsp
4492 43012d58 2019-07-14 stsp #ifndef PROFILE
4493 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4494 43012d58 2019-07-14 stsp NULL) == -1)
4495 43012d58 2019-07-14 stsp err(1, "pledge");
4496 43012d58 2019-07-14 stsp #endif
4497 723c305c 2019-05-11 jcs if (argc < 1)
4498 d00136be 2019-03-26 stsp usage_add();
4499 d00136be 2019-03-26 stsp
4500 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4501 d00136be 2019-03-26 stsp if (cwd == NULL) {
4502 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4503 d00136be 2019-03-26 stsp goto done;
4504 d00136be 2019-03-26 stsp }
4505 723c305c 2019-05-11 jcs
4506 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4507 d00136be 2019-03-26 stsp if (error)
4508 d00136be 2019-03-26 stsp goto done;
4509 d00136be 2019-03-26 stsp
4510 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4511 c9956ddf 2019-09-08 stsp NULL);
4512 031a5338 2019-03-26 stsp if (error != NULL)
4513 031a5338 2019-03-26 stsp goto done;
4514 031a5338 2019-03-26 stsp
4515 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4516 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4517 d00136be 2019-03-26 stsp if (error)
4518 d00136be 2019-03-26 stsp goto done;
4519 d00136be 2019-03-26 stsp
4520 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4521 6d022e97 2019-08-04 stsp if (error)
4522 022fae89 2019-12-06 tracey goto done;
4523 022fae89 2019-12-06 tracey
4524 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4525 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4526 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4527 6d022e97 2019-08-04 stsp goto done;
4528 723c305c 2019-05-11 jcs
4529 022fae89 2019-12-06 tracey }
4530 022fae89 2019-12-06 tracey
4531 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4532 4e68cba3 2019-11-23 stsp char *ondisk_path;
4533 4e68cba3 2019-11-23 stsp struct stat sb;
4534 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4535 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4536 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4537 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4538 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4539 4e68cba3 2019-11-23 stsp goto done;
4540 4e68cba3 2019-11-23 stsp }
4541 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4542 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4543 4e68cba3 2019-11-23 stsp free(ondisk_path);
4544 4e68cba3 2019-11-23 stsp continue;
4545 4e68cba3 2019-11-23 stsp }
4546 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4547 4e68cba3 2019-11-23 stsp ondisk_path);
4548 4e68cba3 2019-11-23 stsp free(ondisk_path);
4549 4e68cba3 2019-11-23 stsp goto done;
4550 4e68cba3 2019-11-23 stsp }
4551 4e68cba3 2019-11-23 stsp free(ondisk_path);
4552 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4553 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4554 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4555 4e68cba3 2019-11-23 stsp goto done;
4556 4e68cba3 2019-11-23 stsp }
4557 4e68cba3 2019-11-23 stsp }
4558 4e68cba3 2019-11-23 stsp }
4559 022fae89 2019-12-06 tracey
4560 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4561 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4562 d00136be 2019-03-26 stsp done:
4563 031a5338 2019-03-26 stsp if (repo)
4564 031a5338 2019-03-26 stsp got_repo_close(repo);
4565 d00136be 2019-03-26 stsp if (worktree)
4566 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4567 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4568 1dd54920 2019-05-11 stsp free((char *)pe->path);
4569 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4570 2ec1f75b 2019-03-26 stsp free(cwd);
4571 2ec1f75b 2019-03-26 stsp return error;
4572 2ec1f75b 2019-03-26 stsp }
4573 2ec1f75b 2019-03-26 stsp
4574 2ec1f75b 2019-03-26 stsp __dead static void
4575 648e4ef7 2019-07-09 stsp usage_remove(void)
4576 2ec1f75b 2019-03-26 stsp {
4577 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4578 f2a9dc41 2019-12-13 tracey getprogname());
4579 2ec1f75b 2019-03-26 stsp exit(1);
4580 2a06fe5f 2019-08-24 stsp }
4581 2a06fe5f 2019-08-24 stsp
4582 2a06fe5f 2019-08-24 stsp static const struct got_error *
4583 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4584 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4585 2a06fe5f 2019-08-24 stsp {
4586 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4587 f2a9dc41 2019-12-13 tracey path++;
4588 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4589 2a06fe5f 2019-08-24 stsp return NULL;
4590 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4591 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4592 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4593 2a06fe5f 2019-08-24 stsp return NULL;
4594 2ec1f75b 2019-03-26 stsp }
4595 2ec1f75b 2019-03-26 stsp
4596 2ec1f75b 2019-03-26 stsp static const struct got_error *
4597 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4598 2ec1f75b 2019-03-26 stsp {
4599 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4600 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4601 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4602 17ed4618 2019-06-02 stsp char *cwd = NULL;
4603 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4604 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4605 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4606 2ec1f75b 2019-03-26 stsp
4607 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4608 17ed4618 2019-06-02 stsp
4609 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4610 2ec1f75b 2019-03-26 stsp switch (ch) {
4611 2ec1f75b 2019-03-26 stsp case 'f':
4612 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4613 2ec1f75b 2019-03-26 stsp break;
4614 70e3e7f5 2019-12-13 tracey case 'k':
4615 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4616 70e3e7f5 2019-12-13 tracey break;
4617 f2a9dc41 2019-12-13 tracey case 'R':
4618 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4619 f2a9dc41 2019-12-13 tracey break;
4620 2ec1f75b 2019-03-26 stsp default:
4621 f2a9dc41 2019-12-13 tracey usage_remove();
4622 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4623 2ec1f75b 2019-03-26 stsp }
4624 2ec1f75b 2019-03-26 stsp }
4625 2ec1f75b 2019-03-26 stsp
4626 2ec1f75b 2019-03-26 stsp argc -= optind;
4627 2ec1f75b 2019-03-26 stsp argv += optind;
4628 2ec1f75b 2019-03-26 stsp
4629 43012d58 2019-07-14 stsp #ifndef PROFILE
4630 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4631 43012d58 2019-07-14 stsp NULL) == -1)
4632 43012d58 2019-07-14 stsp err(1, "pledge");
4633 43012d58 2019-07-14 stsp #endif
4634 17ed4618 2019-06-02 stsp if (argc < 1)
4635 648e4ef7 2019-07-09 stsp usage_remove();
4636 2ec1f75b 2019-03-26 stsp
4637 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4638 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4639 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4640 2ec1f75b 2019-03-26 stsp goto done;
4641 2ec1f75b 2019-03-26 stsp }
4642 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4643 2ec1f75b 2019-03-26 stsp if (error)
4644 2ec1f75b 2019-03-26 stsp goto done;
4645 2ec1f75b 2019-03-26 stsp
4646 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4647 c9956ddf 2019-09-08 stsp NULL);
4648 2af4a041 2019-05-11 jcs if (error)
4649 2ec1f75b 2019-03-26 stsp goto done;
4650 2ec1f75b 2019-03-26 stsp
4651 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4652 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4653 2ec1f75b 2019-03-26 stsp if (error)
4654 2ec1f75b 2019-03-26 stsp goto done;
4655 2ec1f75b 2019-03-26 stsp
4656 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4657 6d022e97 2019-08-04 stsp if (error)
4658 6d022e97 2019-08-04 stsp goto done;
4659 17ed4618 2019-06-02 stsp
4660 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4661 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4662 f2a9dc41 2019-12-13 tracey struct stat sb;
4663 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4664 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4665 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4666 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4667 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4668 f2a9dc41 2019-12-13 tracey goto done;
4669 f2a9dc41 2019-12-13 tracey }
4670 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4671 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4672 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4673 f2a9dc41 2019-12-13 tracey continue;
4674 f2a9dc41 2019-12-13 tracey }
4675 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4676 f2a9dc41 2019-12-13 tracey ondisk_path);
4677 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4678 f2a9dc41 2019-12-13 tracey goto done;
4679 f2a9dc41 2019-12-13 tracey }
4680 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4681 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4682 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4683 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4684 f2a9dc41 2019-12-13 tracey goto done;
4685 f2a9dc41 2019-12-13 tracey }
4686 f2a9dc41 2019-12-13 tracey }
4687 f2a9dc41 2019-12-13 tracey }
4688 f2a9dc41 2019-12-13 tracey
4689 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4690 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4691 a129376b 2019-03-28 stsp done:
4692 a129376b 2019-03-28 stsp if (repo)
4693 a129376b 2019-03-28 stsp got_repo_close(repo);
4694 a129376b 2019-03-28 stsp if (worktree)
4695 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4696 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4697 17ed4618 2019-06-02 stsp free((char *)pe->path);
4698 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4699 a129376b 2019-03-28 stsp free(cwd);
4700 a129376b 2019-03-28 stsp return error;
4701 a129376b 2019-03-28 stsp }
4702 a129376b 2019-03-28 stsp
4703 a129376b 2019-03-28 stsp __dead static void
4704 a129376b 2019-03-28 stsp usage_revert(void)
4705 a129376b 2019-03-28 stsp {
4706 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4707 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4708 a129376b 2019-03-28 stsp exit(1);
4709 a129376b 2019-03-28 stsp }
4710 a129376b 2019-03-28 stsp
4711 1ee397ad 2019-07-12 stsp static const struct got_error *
4712 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4713 a129376b 2019-03-28 stsp {
4714 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4715 fb9704af 2020-01-27 stsp return NULL;
4716 fb9704af 2020-01-27 stsp
4717 a129376b 2019-03-28 stsp while (path[0] == '/')
4718 a129376b 2019-03-28 stsp path++;
4719 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4720 33aa809d 2019-08-08 stsp return NULL;
4721 33aa809d 2019-08-08 stsp }
4722 33aa809d 2019-08-08 stsp
4723 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4724 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4725 33aa809d 2019-08-08 stsp const char *action;
4726 33aa809d 2019-08-08 stsp };
4727 33aa809d 2019-08-08 stsp
4728 33aa809d 2019-08-08 stsp static const struct got_error *
4729 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4730 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4731 33aa809d 2019-08-08 stsp {
4732 33aa809d 2019-08-08 stsp char *line = NULL;
4733 33aa809d 2019-08-08 stsp size_t linesize = 0;
4734 33aa809d 2019-08-08 stsp ssize_t linelen;
4735 33aa809d 2019-08-08 stsp
4736 33aa809d 2019-08-08 stsp switch (status) {
4737 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4738 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4739 33aa809d 2019-08-08 stsp break;
4740 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4741 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4742 33aa809d 2019-08-08 stsp break;
4743 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4744 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4745 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4746 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4747 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4748 33aa809d 2019-08-08 stsp printf("%s", line);
4749 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4750 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4751 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4752 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4753 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4754 33aa809d 2019-08-08 stsp break;
4755 33aa809d 2019-08-08 stsp default:
4756 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4757 33aa809d 2019-08-08 stsp }
4758 33aa809d 2019-08-08 stsp
4759 33aa809d 2019-08-08 stsp return NULL;
4760 33aa809d 2019-08-08 stsp }
4761 33aa809d 2019-08-08 stsp
4762 33aa809d 2019-08-08 stsp static const struct got_error *
4763 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4764 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4765 33aa809d 2019-08-08 stsp {
4766 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4767 33aa809d 2019-08-08 stsp char *line = NULL;
4768 33aa809d 2019-08-08 stsp size_t linesize = 0;
4769 33aa809d 2019-08-08 stsp ssize_t linelen;
4770 33aa809d 2019-08-08 stsp int resp = ' ';
4771 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4772 33aa809d 2019-08-08 stsp
4773 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4774 33aa809d 2019-08-08 stsp
4775 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4776 33aa809d 2019-08-08 stsp char *nl;
4777 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4778 33aa809d 2019-08-08 stsp a->action);
4779 33aa809d 2019-08-08 stsp if (err)
4780 33aa809d 2019-08-08 stsp return err;
4781 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4782 33aa809d 2019-08-08 stsp if (linelen == -1) {
4783 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4784 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4785 33aa809d 2019-08-08 stsp return NULL;
4786 33aa809d 2019-08-08 stsp }
4787 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4788 33aa809d 2019-08-08 stsp if (nl)
4789 33aa809d 2019-08-08 stsp *nl = '\0';
4790 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4791 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4792 33aa809d 2019-08-08 stsp printf("y\n");
4793 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4794 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4795 33aa809d 2019-08-08 stsp printf("n\n");
4796 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4797 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4798 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4799 33aa809d 2019-08-08 stsp printf("q\n");
4800 33aa809d 2019-08-08 stsp } else
4801 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4802 33aa809d 2019-08-08 stsp free(line);
4803 33aa809d 2019-08-08 stsp return NULL;
4804 33aa809d 2019-08-08 stsp }
4805 33aa809d 2019-08-08 stsp
4806 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4807 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4808 33aa809d 2019-08-08 stsp a->action);
4809 33aa809d 2019-08-08 stsp if (err)
4810 33aa809d 2019-08-08 stsp return err;
4811 33aa809d 2019-08-08 stsp resp = getchar();
4812 33aa809d 2019-08-08 stsp if (resp == '\n')
4813 33aa809d 2019-08-08 stsp resp = getchar();
4814 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4815 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4816 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4817 33aa809d 2019-08-08 stsp resp = ' ';
4818 33aa809d 2019-08-08 stsp }
4819 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4820 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4821 33aa809d 2019-08-08 stsp resp = ' ';
4822 33aa809d 2019-08-08 stsp }
4823 33aa809d 2019-08-08 stsp }
4824 33aa809d 2019-08-08 stsp
4825 33aa809d 2019-08-08 stsp if (resp == 'y')
4826 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4827 33aa809d 2019-08-08 stsp else if (resp == 'n')
4828 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4829 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4830 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4831 33aa809d 2019-08-08 stsp
4832 1ee397ad 2019-07-12 stsp return NULL;
4833 a129376b 2019-03-28 stsp }
4834 a129376b 2019-03-28 stsp
4835 33aa809d 2019-08-08 stsp
4836 a129376b 2019-03-28 stsp static const struct got_error *
4837 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4838 a129376b 2019-03-28 stsp {
4839 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4840 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4841 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4842 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4843 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4844 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4845 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4846 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4847 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4848 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4849 a129376b 2019-03-28 stsp
4850 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4851 e20a8b6f 2019-06-04 stsp
4852 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4853 a129376b 2019-03-28 stsp switch (ch) {
4854 33aa809d 2019-08-08 stsp case 'p':
4855 33aa809d 2019-08-08 stsp pflag = 1;
4856 33aa809d 2019-08-08 stsp break;
4857 33aa809d 2019-08-08 stsp case 'F':
4858 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4859 33aa809d 2019-08-08 stsp break;
4860 0f6d7415 2019-08-08 stsp case 'R':
4861 0f6d7415 2019-08-08 stsp can_recurse = 1;
4862 0f6d7415 2019-08-08 stsp break;
4863 a129376b 2019-03-28 stsp default:
4864 a129376b 2019-03-28 stsp usage_revert();
4865 a129376b 2019-03-28 stsp /* NOTREACHED */
4866 a129376b 2019-03-28 stsp }
4867 a129376b 2019-03-28 stsp }
4868 a129376b 2019-03-28 stsp
4869 a129376b 2019-03-28 stsp argc -= optind;
4870 a129376b 2019-03-28 stsp argv += optind;
4871 a129376b 2019-03-28 stsp
4872 43012d58 2019-07-14 stsp #ifndef PROFILE
4873 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4874 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4875 43012d58 2019-07-14 stsp err(1, "pledge");
4876 43012d58 2019-07-14 stsp #endif
4877 e20a8b6f 2019-06-04 stsp if (argc < 1)
4878 a129376b 2019-03-28 stsp usage_revert();
4879 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4880 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4881 a129376b 2019-03-28 stsp
4882 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4883 a129376b 2019-03-28 stsp if (cwd == NULL) {
4884 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4885 a129376b 2019-03-28 stsp goto done;
4886 a129376b 2019-03-28 stsp }
4887 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4888 2ec1f75b 2019-03-26 stsp if (error)
4889 2ec1f75b 2019-03-26 stsp goto done;
4890 a129376b 2019-03-28 stsp
4891 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4892 c9956ddf 2019-09-08 stsp NULL);
4893 a129376b 2019-03-28 stsp if (error != NULL)
4894 a129376b 2019-03-28 stsp goto done;
4895 a129376b 2019-03-28 stsp
4896 33aa809d 2019-08-08 stsp if (patch_script_path) {
4897 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4898 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4899 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4900 33aa809d 2019-08-08 stsp patch_script_path);
4901 33aa809d 2019-08-08 stsp goto done;
4902 33aa809d 2019-08-08 stsp }
4903 33aa809d 2019-08-08 stsp }
4904 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4905 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4906 a129376b 2019-03-28 stsp if (error)
4907 a129376b 2019-03-28 stsp goto done;
4908 a129376b 2019-03-28 stsp
4909 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4910 6d022e97 2019-08-04 stsp if (error)
4911 6d022e97 2019-08-04 stsp goto done;
4912 00db391e 2019-08-03 stsp
4913 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4914 0f6d7415 2019-08-08 stsp char *ondisk_path;
4915 0f6d7415 2019-08-08 stsp struct stat sb;
4916 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4917 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4918 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4919 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4920 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4921 0f6d7415 2019-08-08 stsp goto done;
4922 0f6d7415 2019-08-08 stsp }
4923 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4924 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4925 0f6d7415 2019-08-08 stsp free(ondisk_path);
4926 0f6d7415 2019-08-08 stsp continue;
4927 0f6d7415 2019-08-08 stsp }
4928 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4929 0f6d7415 2019-08-08 stsp ondisk_path);
4930 0f6d7415 2019-08-08 stsp free(ondisk_path);
4931 0f6d7415 2019-08-08 stsp goto done;
4932 0f6d7415 2019-08-08 stsp }
4933 0f6d7415 2019-08-08 stsp free(ondisk_path);
4934 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4935 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4936 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4937 0f6d7415 2019-08-08 stsp goto done;
4938 0f6d7415 2019-08-08 stsp }
4939 0f6d7415 2019-08-08 stsp }
4940 0f6d7415 2019-08-08 stsp }
4941 0f6d7415 2019-08-08 stsp
4942 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4943 33aa809d 2019-08-08 stsp cpa.action = "revert";
4944 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4945 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4946 2ec1f75b 2019-03-26 stsp done:
4947 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4948 33aa809d 2019-08-08 stsp error == NULL)
4949 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4950 2ec1f75b 2019-03-26 stsp if (repo)
4951 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4952 2ec1f75b 2019-03-26 stsp if (worktree)
4953 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4954 2ec1f75b 2019-03-26 stsp free(path);
4955 d00136be 2019-03-26 stsp free(cwd);
4956 6bad629b 2019-02-04 stsp return error;
4957 c4296144 2019-05-09 stsp }
4958 c4296144 2019-05-09 stsp
4959 c4296144 2019-05-09 stsp __dead static void
4960 c4296144 2019-05-09 stsp usage_commit(void)
4961 c4296144 2019-05-09 stsp {
4962 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4963 5c1e53bc 2019-07-28 stsp getprogname());
4964 c4296144 2019-05-09 stsp exit(1);
4965 33ad4cbe 2019-05-12 jcs }
4966 33ad4cbe 2019-05-12 jcs
4967 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4968 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4969 e2ba3d07 2019-05-13 stsp const char *editor;
4970 e0870e44 2019-05-13 stsp const char *worktree_path;
4971 76d98825 2019-06-03 stsp const char *branch_name;
4972 314a6357 2019-05-13 stsp const char *repo_path;
4973 e0870e44 2019-05-13 stsp char *logmsg_path;
4974 e2ba3d07 2019-05-13 stsp
4975 e2ba3d07 2019-05-13 stsp };
4976 e0870e44 2019-05-13 stsp
4977 33ad4cbe 2019-05-12 jcs static const struct got_error *
4978 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4979 33ad4cbe 2019-05-12 jcs void *arg)
4980 33ad4cbe 2019-05-12 jcs {
4981 76d98825 2019-06-03 stsp char *initial_content = NULL;
4982 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4983 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4984 e0870e44 2019-05-13 stsp char *template = NULL;
4985 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4986 3ce1b845 2019-07-15 stsp int fd;
4987 33ad4cbe 2019-05-12 jcs size_t len;
4988 33ad4cbe 2019-05-12 jcs
4989 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4990 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4991 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4992 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4993 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4994 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4995 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4996 33ad4cbe 2019-05-12 jcs return NULL;
4997 33ad4cbe 2019-05-12 jcs }
4998 33ad4cbe 2019-05-12 jcs
4999 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5000 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5001 76d98825 2019-06-03 stsp
5002 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5003 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5004 76d98825 2019-06-03 stsp a->branch_name) == -1)
5005 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5006 e0870e44 2019-05-13 stsp
5007 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5008 793c30b5 2019-05-13 stsp if (err)
5009 e0870e44 2019-05-13 stsp goto done;
5010 33ad4cbe 2019-05-12 jcs
5011 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5012 33ad4cbe 2019-05-12 jcs
5013 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5014 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5015 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5016 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5017 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5018 33ad4cbe 2019-05-12 jcs }
5019 33ad4cbe 2019-05-12 jcs close(fd);
5020 33ad4cbe 2019-05-12 jcs
5021 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5022 33ad4cbe 2019-05-12 jcs done:
5023 76d98825 2019-06-03 stsp free(initial_content);
5024 e0870e44 2019-05-13 stsp free(template);
5025 314a6357 2019-05-13 stsp
5026 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5027 3ce1b845 2019-07-15 stsp if (err == NULL) {
5028 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5029 3ce1b845 2019-07-15 stsp if (err) {
5030 3ce1b845 2019-07-15 stsp free(*logmsg);
5031 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5032 3ce1b845 2019-07-15 stsp }
5033 3ce1b845 2019-07-15 stsp }
5034 33ad4cbe 2019-05-12 jcs return err;
5035 6bad629b 2019-02-04 stsp }
5036 c4296144 2019-05-09 stsp
5037 c4296144 2019-05-09 stsp static const struct got_error *
5038 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5039 c4296144 2019-05-09 stsp {
5040 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5041 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5042 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5043 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5044 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5045 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5046 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5047 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5048 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5049 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5050 5c1e53bc 2019-07-28 stsp
5051 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5052 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5053 c4296144 2019-05-09 stsp
5054 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5055 c4296144 2019-05-09 stsp switch (ch) {
5056 c4296144 2019-05-09 stsp case 'm':
5057 c4296144 2019-05-09 stsp logmsg = optarg;
5058 c4296144 2019-05-09 stsp break;
5059 c4296144 2019-05-09 stsp default:
5060 c4296144 2019-05-09 stsp usage_commit();
5061 c4296144 2019-05-09 stsp /* NOTREACHED */
5062 c4296144 2019-05-09 stsp }
5063 c4296144 2019-05-09 stsp }
5064 c4296144 2019-05-09 stsp
5065 c4296144 2019-05-09 stsp argc -= optind;
5066 c4296144 2019-05-09 stsp argv += optind;
5067 c4296144 2019-05-09 stsp
5068 43012d58 2019-07-14 stsp #ifndef PROFILE
5069 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5070 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5071 43012d58 2019-07-14 stsp err(1, "pledge");
5072 43012d58 2019-07-14 stsp #endif
5073 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5074 c4296144 2019-05-09 stsp if (cwd == NULL) {
5075 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5076 c4296144 2019-05-09 stsp goto done;
5077 c4296144 2019-05-09 stsp }
5078 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5079 7d5807f4 2019-07-11 stsp if (error)
5080 7d5807f4 2019-07-11 stsp goto done;
5081 7d5807f4 2019-07-11 stsp
5082 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5083 c4296144 2019-05-09 stsp if (error)
5084 a698f62e 2019-07-25 stsp goto done;
5085 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5086 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5087 c4296144 2019-05-09 stsp goto done;
5088 a698f62e 2019-07-25 stsp }
5089 5c1e53bc 2019-07-28 stsp
5090 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5091 916f288c 2019-07-30 stsp worktree);
5092 5c1e53bc 2019-07-28 stsp if (error)
5093 5c1e53bc 2019-07-28 stsp goto done;
5094 c4296144 2019-05-09 stsp
5095 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5096 c9956ddf 2019-09-08 stsp if (error)
5097 c9956ddf 2019-09-08 stsp goto done;
5098 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5099 c9956ddf 2019-09-08 stsp gitconfig_path);
5100 c4296144 2019-05-09 stsp if (error != NULL)
5101 c4296144 2019-05-09 stsp goto done;
5102 c4296144 2019-05-09 stsp
5103 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5104 aba9c984 2019-09-08 stsp if (error)
5105 aba9c984 2019-09-08 stsp return error;
5106 aba9c984 2019-09-08 stsp
5107 314a6357 2019-05-13 stsp /*
5108 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5109 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5110 314a6357 2019-05-13 stsp */
5111 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5112 314a6357 2019-05-13 stsp error = get_editor(&editor);
5113 314a6357 2019-05-13 stsp else
5114 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5115 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5116 c4296144 2019-05-09 stsp if (error)
5117 c4296144 2019-05-09 stsp goto done;
5118 c4296144 2019-05-09 stsp
5119 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5120 087fb88c 2019-08-04 stsp if (error)
5121 087fb88c 2019-08-04 stsp goto done;
5122 087fb88c 2019-08-04 stsp
5123 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5124 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5125 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5126 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5127 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5128 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5129 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5130 916f288c 2019-07-30 stsp goto done;
5131 916f288c 2019-07-30 stsp }
5132 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5133 916f288c 2019-07-30 stsp }
5134 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5135 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5136 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5137 e0870e44 2019-05-13 stsp if (error) {
5138 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5139 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5140 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5141 c4296144 2019-05-09 stsp goto done;
5142 e0870e44 2019-05-13 stsp }
5143 c4296144 2019-05-09 stsp
5144 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5145 c4296144 2019-05-09 stsp if (error)
5146 c4296144 2019-05-09 stsp goto done;
5147 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5148 c4296144 2019-05-09 stsp done:
5149 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5150 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5151 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5152 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5153 7266f21f 2019-10-21 stsp error == NULL)
5154 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5155 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5156 c4296144 2019-05-09 stsp if (repo)
5157 c4296144 2019-05-09 stsp got_repo_close(repo);
5158 c4296144 2019-05-09 stsp if (worktree)
5159 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5160 c4296144 2019-05-09 stsp free(cwd);
5161 c4296144 2019-05-09 stsp free(id_str);
5162 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5163 e2ba3d07 2019-05-13 stsp free(editor);
5164 aba9c984 2019-09-08 stsp free(author);
5165 234035bc 2019-06-01 stsp return error;
5166 234035bc 2019-06-01 stsp }
5167 234035bc 2019-06-01 stsp
5168 234035bc 2019-06-01 stsp __dead static void
5169 234035bc 2019-06-01 stsp usage_cherrypick(void)
5170 234035bc 2019-06-01 stsp {
5171 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5172 234035bc 2019-06-01 stsp exit(1);
5173 234035bc 2019-06-01 stsp }
5174 234035bc 2019-06-01 stsp
5175 234035bc 2019-06-01 stsp static const struct got_error *
5176 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5177 234035bc 2019-06-01 stsp {
5178 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5179 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5180 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5181 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5182 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5183 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5184 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5185 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5186 234035bc 2019-06-01 stsp int ch, did_something = 0;
5187 234035bc 2019-06-01 stsp
5188 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5189 234035bc 2019-06-01 stsp switch (ch) {
5190 234035bc 2019-06-01 stsp default:
5191 234035bc 2019-06-01 stsp usage_cherrypick();
5192 234035bc 2019-06-01 stsp /* NOTREACHED */
5193 234035bc 2019-06-01 stsp }
5194 234035bc 2019-06-01 stsp }
5195 234035bc 2019-06-01 stsp
5196 234035bc 2019-06-01 stsp argc -= optind;
5197 234035bc 2019-06-01 stsp argv += optind;
5198 234035bc 2019-06-01 stsp
5199 43012d58 2019-07-14 stsp #ifndef PROFILE
5200 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5201 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5202 43012d58 2019-07-14 stsp err(1, "pledge");
5203 43012d58 2019-07-14 stsp #endif
5204 234035bc 2019-06-01 stsp if (argc != 1)
5205 234035bc 2019-06-01 stsp usage_cherrypick();
5206 234035bc 2019-06-01 stsp
5207 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5208 234035bc 2019-06-01 stsp if (cwd == NULL) {
5209 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5210 234035bc 2019-06-01 stsp goto done;
5211 234035bc 2019-06-01 stsp }
5212 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5213 234035bc 2019-06-01 stsp if (error)
5214 234035bc 2019-06-01 stsp goto done;
5215 234035bc 2019-06-01 stsp
5216 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5217 c9956ddf 2019-09-08 stsp NULL);
5218 234035bc 2019-06-01 stsp if (error != NULL)
5219 234035bc 2019-06-01 stsp goto done;
5220 234035bc 2019-06-01 stsp
5221 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5222 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5223 234035bc 2019-06-01 stsp if (error)
5224 234035bc 2019-06-01 stsp goto done;
5225 234035bc 2019-06-01 stsp
5226 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5227 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5228 234035bc 2019-06-01 stsp if (error != NULL) {
5229 234035bc 2019-06-01 stsp struct got_reference *ref;
5230 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5231 234035bc 2019-06-01 stsp goto done;
5232 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5233 234035bc 2019-06-01 stsp if (error != NULL)
5234 234035bc 2019-06-01 stsp goto done;
5235 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5236 234035bc 2019-06-01 stsp got_ref_close(ref);
5237 234035bc 2019-06-01 stsp if (error != NULL)
5238 234035bc 2019-06-01 stsp goto done;
5239 234035bc 2019-06-01 stsp }
5240 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5241 234035bc 2019-06-01 stsp if (error)
5242 234035bc 2019-06-01 stsp goto done;
5243 234035bc 2019-06-01 stsp
5244 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5245 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5246 234035bc 2019-06-01 stsp if (error != NULL)
5247 234035bc 2019-06-01 stsp goto done;
5248 234035bc 2019-06-01 stsp
5249 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5250 234035bc 2019-06-01 stsp if (error) {
5251 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5252 234035bc 2019-06-01 stsp goto done;
5253 234035bc 2019-06-01 stsp error = NULL;
5254 234035bc 2019-06-01 stsp } else {
5255 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5256 234035bc 2019-06-01 stsp goto done;
5257 234035bc 2019-06-01 stsp }
5258 234035bc 2019-06-01 stsp
5259 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5260 234035bc 2019-06-01 stsp if (error)
5261 234035bc 2019-06-01 stsp goto done;
5262 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5263 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5264 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5265 03415a1a 2019-06-02 stsp NULL);
5266 234035bc 2019-06-01 stsp if (error != NULL)
5267 234035bc 2019-06-01 stsp goto done;
5268 234035bc 2019-06-01 stsp
5269 234035bc 2019-06-01 stsp if (did_something)
5270 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5271 234035bc 2019-06-01 stsp done:
5272 234035bc 2019-06-01 stsp if (commit)
5273 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5274 234035bc 2019-06-01 stsp free(commit_id_str);
5275 234035bc 2019-06-01 stsp if (head_ref)
5276 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5277 234035bc 2019-06-01 stsp if (worktree)
5278 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5279 234035bc 2019-06-01 stsp if (repo)
5280 234035bc 2019-06-01 stsp got_repo_close(repo);
5281 c4296144 2019-05-09 stsp return error;
5282 c4296144 2019-05-09 stsp }
5283 5ef14e63 2019-06-02 stsp
5284 5ef14e63 2019-06-02 stsp __dead static void
5285 5ef14e63 2019-06-02 stsp usage_backout(void)
5286 5ef14e63 2019-06-02 stsp {
5287 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5288 5ef14e63 2019-06-02 stsp exit(1);
5289 5ef14e63 2019-06-02 stsp }
5290 5ef14e63 2019-06-02 stsp
5291 5ef14e63 2019-06-02 stsp static const struct got_error *
5292 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5293 5ef14e63 2019-06-02 stsp {
5294 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5295 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5296 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5297 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5298 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5299 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5300 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5301 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5302 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5303 5ef14e63 2019-06-02 stsp
5304 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5305 5ef14e63 2019-06-02 stsp switch (ch) {
5306 5ef14e63 2019-06-02 stsp default:
5307 5ef14e63 2019-06-02 stsp usage_backout();
5308 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5309 5ef14e63 2019-06-02 stsp }
5310 5ef14e63 2019-06-02 stsp }
5311 5ef14e63 2019-06-02 stsp
5312 5ef14e63 2019-06-02 stsp argc -= optind;
5313 5ef14e63 2019-06-02 stsp argv += optind;
5314 5ef14e63 2019-06-02 stsp
5315 43012d58 2019-07-14 stsp #ifndef PROFILE
5316 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5317 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5318 43012d58 2019-07-14 stsp err(1, "pledge");
5319 43012d58 2019-07-14 stsp #endif
5320 5ef14e63 2019-06-02 stsp if (argc != 1)
5321 5ef14e63 2019-06-02 stsp usage_backout();
5322 5ef14e63 2019-06-02 stsp
5323 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5324 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5325 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5326 5ef14e63 2019-06-02 stsp goto done;
5327 5ef14e63 2019-06-02 stsp }
5328 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5329 5ef14e63 2019-06-02 stsp if (error)
5330 5ef14e63 2019-06-02 stsp goto done;
5331 5ef14e63 2019-06-02 stsp
5332 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5333 c9956ddf 2019-09-08 stsp NULL);
5334 5ef14e63 2019-06-02 stsp if (error != NULL)
5335 5ef14e63 2019-06-02 stsp goto done;
5336 5ef14e63 2019-06-02 stsp
5337 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5338 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5339 5ef14e63 2019-06-02 stsp if (error)
5340 5ef14e63 2019-06-02 stsp goto done;
5341 5ef14e63 2019-06-02 stsp
5342 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5343 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5344 5ef14e63 2019-06-02 stsp if (error != NULL) {
5345 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5346 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5347 5ef14e63 2019-06-02 stsp goto done;
5348 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5349 5ef14e63 2019-06-02 stsp if (error != NULL)
5350 5ef14e63 2019-06-02 stsp goto done;
5351 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5352 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5353 5ef14e63 2019-06-02 stsp if (error != NULL)
5354 5ef14e63 2019-06-02 stsp goto done;
5355 5ef14e63 2019-06-02 stsp }
5356 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5357 5ef14e63 2019-06-02 stsp if (error)
5358 5ef14e63 2019-06-02 stsp goto done;
5359 5ef14e63 2019-06-02 stsp
5360 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5361 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5362 5ef14e63 2019-06-02 stsp if (error != NULL)
5363 5ef14e63 2019-06-02 stsp goto done;
5364 5ef14e63 2019-06-02 stsp
5365 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5366 5ef14e63 2019-06-02 stsp if (error)
5367 5ef14e63 2019-06-02 stsp goto done;
5368 5ef14e63 2019-06-02 stsp
5369 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5370 5ef14e63 2019-06-02 stsp if (error)
5371 5ef14e63 2019-06-02 stsp goto done;
5372 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5373 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5374 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5375 5ef14e63 2019-06-02 stsp goto done;
5376 5ef14e63 2019-06-02 stsp }
5377 5ef14e63 2019-06-02 stsp
5378 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5379 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5380 5ef14e63 2019-06-02 stsp if (error != NULL)
5381 5ef14e63 2019-06-02 stsp goto done;
5382 5ef14e63 2019-06-02 stsp
5383 5ef14e63 2019-06-02 stsp if (did_something)
5384 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5385 5ef14e63 2019-06-02 stsp done:
5386 5ef14e63 2019-06-02 stsp if (commit)
5387 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5388 5ef14e63 2019-06-02 stsp free(commit_id_str);
5389 5ef14e63 2019-06-02 stsp if (head_ref)
5390 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5391 818c7501 2019-07-11 stsp if (worktree)
5392 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5393 818c7501 2019-07-11 stsp if (repo)
5394 818c7501 2019-07-11 stsp got_repo_close(repo);
5395 818c7501 2019-07-11 stsp return error;
5396 818c7501 2019-07-11 stsp }
5397 818c7501 2019-07-11 stsp
5398 818c7501 2019-07-11 stsp __dead static void
5399 818c7501 2019-07-11 stsp usage_rebase(void)
5400 818c7501 2019-07-11 stsp {
5401 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5402 af54c8f8 2019-07-11 stsp getprogname());
5403 818c7501 2019-07-11 stsp exit(1);
5404 0ebf8283 2019-07-24 stsp }
5405 0ebf8283 2019-07-24 stsp
5406 0ebf8283 2019-07-24 stsp void
5407 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5408 0ebf8283 2019-07-24 stsp {
5409 0ebf8283 2019-07-24 stsp char *nl;
5410 0ebf8283 2019-07-24 stsp size_t len;
5411 0ebf8283 2019-07-24 stsp
5412 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5413 0ebf8283 2019-07-24 stsp if (len > limit)
5414 0ebf8283 2019-07-24 stsp len = limit;
5415 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5416 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5417 0ebf8283 2019-07-24 stsp if (nl)
5418 0ebf8283 2019-07-24 stsp *nl = '\0';
5419 0ebf8283 2019-07-24 stsp }
5420 0ebf8283 2019-07-24 stsp
5421 0ebf8283 2019-07-24 stsp static const struct got_error *
5422 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5423 0ebf8283 2019-07-24 stsp {
5424 5943eee2 2019-08-13 stsp const struct got_error *err;
5425 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5426 5943eee2 2019-08-13 stsp const char *s;
5427 0ebf8283 2019-07-24 stsp
5428 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5429 5943eee2 2019-08-13 stsp if (err)
5430 5943eee2 2019-08-13 stsp return err;
5431 0ebf8283 2019-07-24 stsp
5432 5943eee2 2019-08-13 stsp s = logmsg0;
5433 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5434 5943eee2 2019-08-13 stsp s++;
5435 5943eee2 2019-08-13 stsp
5436 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5437 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5438 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5439 5943eee2 2019-08-13 stsp goto done;
5440 5943eee2 2019-08-13 stsp }
5441 0ebf8283 2019-07-24 stsp
5442 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5443 5943eee2 2019-08-13 stsp done:
5444 5943eee2 2019-08-13 stsp free(logmsg0);
5445 a0ea4fc0 2020-02-28 stsp return err;
5446 a0ea4fc0 2020-02-28 stsp }
5447 a0ea4fc0 2020-02-28 stsp
5448 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5449 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5450 a0ea4fc0 2020-02-28 stsp {
5451 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5452 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5453 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5454 a0ea4fc0 2020-02-28 stsp
5455 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5456 a0ea4fc0 2020-02-28 stsp if (err)
5457 a0ea4fc0 2020-02-28 stsp return err;
5458 a0ea4fc0 2020-02-28 stsp
5459 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5460 a0ea4fc0 2020-02-28 stsp if (err)
5461 a0ea4fc0 2020-02-28 stsp goto done;
5462 a0ea4fc0 2020-02-28 stsp
5463 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5464 a0ea4fc0 2020-02-28 stsp
5465 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5466 a0ea4fc0 2020-02-28 stsp if (err)
5467 a0ea4fc0 2020-02-28 stsp goto done;
5468 a0ea4fc0 2020-02-28 stsp
5469 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5470 a0ea4fc0 2020-02-28 stsp done:
5471 a0ea4fc0 2020-02-28 stsp free(id_str);
5472 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5473 a0ea4fc0 2020-02-28 stsp free(logmsg);
5474 5943eee2 2019-08-13 stsp return err;
5475 818c7501 2019-07-11 stsp }
5476 818c7501 2019-07-11 stsp
5477 818c7501 2019-07-11 stsp static const struct got_error *
5478 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5479 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5480 818c7501 2019-07-11 stsp {
5481 818c7501 2019-07-11 stsp const struct got_error *err;
5482 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5483 818c7501 2019-07-11 stsp
5484 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5485 818c7501 2019-07-11 stsp if (err)
5486 818c7501 2019-07-11 stsp goto done;
5487 818c7501 2019-07-11 stsp
5488 ff0d2220 2019-07-11 stsp if (new_id) {
5489 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5490 ff0d2220 2019-07-11 stsp if (err)
5491 ff0d2220 2019-07-11 stsp goto done;
5492 ff0d2220 2019-07-11 stsp }
5493 818c7501 2019-07-11 stsp
5494 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5495 ff0d2220 2019-07-11 stsp if (new_id_str)
5496 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5497 0ebf8283 2019-07-24 stsp
5498 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5499 0ebf8283 2019-07-24 stsp if (err)
5500 0ebf8283 2019-07-24 stsp goto done;
5501 0ebf8283 2019-07-24 stsp
5502 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5503 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5504 818c7501 2019-07-11 stsp done:
5505 818c7501 2019-07-11 stsp free(old_id_str);
5506 818c7501 2019-07-11 stsp free(new_id_str);
5507 272a1371 2020-02-28 stsp free(logmsg);
5508 818c7501 2019-07-11 stsp return err;
5509 818c7501 2019-07-11 stsp }
5510 818c7501 2019-07-11 stsp
5511 1ee397ad 2019-07-12 stsp static const struct got_error *
5512 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5513 818c7501 2019-07-11 stsp {
5514 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5515 818c7501 2019-07-11 stsp
5516 818c7501 2019-07-11 stsp while (path[0] == '/')
5517 818c7501 2019-07-11 stsp path++;
5518 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5519 818c7501 2019-07-11 stsp
5520 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5521 1ee397ad 2019-07-12 stsp return NULL;
5522 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5523 818c7501 2019-07-11 stsp *rebase_status = status;
5524 1ee397ad 2019-07-12 stsp return NULL;
5525 818c7501 2019-07-11 stsp }
5526 818c7501 2019-07-11 stsp
5527 818c7501 2019-07-11 stsp static const struct got_error *
5528 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5529 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5530 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5531 818c7501 2019-07-11 stsp {
5532 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5533 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5534 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5535 818c7501 2019-07-11 stsp }
5536 818c7501 2019-07-11 stsp
5537 818c7501 2019-07-11 stsp static const struct got_error *
5538 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5539 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5540 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5541 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5542 ff0d2220 2019-07-11 stsp {
5543 ff0d2220 2019-07-11 stsp const struct got_error *error;
5544 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5545 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5546 ff0d2220 2019-07-11 stsp
5547 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5548 ff0d2220 2019-07-11 stsp if (error)
5549 ff0d2220 2019-07-11 stsp return error;
5550 ff0d2220 2019-07-11 stsp
5551 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5552 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5553 ff0d2220 2019-07-11 stsp if (error) {
5554 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5555 ff0d2220 2019-07-11 stsp goto done;
5556 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5557 ff0d2220 2019-07-11 stsp } else {
5558 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5559 ff0d2220 2019-07-11 stsp free(new_commit_id);
5560 ff0d2220 2019-07-11 stsp }
5561 ff0d2220 2019-07-11 stsp done:
5562 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5563 ff0d2220 2019-07-11 stsp return error;
5564 64c6d990 2019-07-11 stsp }
5565 64c6d990 2019-07-11 stsp
5566 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5567 64c6d990 2019-07-11 stsp const char *path_prefix;
5568 64c6d990 2019-07-11 stsp size_t len;
5569 8ca9bd68 2019-07-25 stsp int errcode;
5570 64c6d990 2019-07-11 stsp };
5571 64c6d990 2019-07-11 stsp
5572 64c6d990 2019-07-11 stsp static const struct got_error *
5573 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5574 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5575 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5576 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5577 64c6d990 2019-07-11 stsp {
5578 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5579 64c6d990 2019-07-11 stsp
5580 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5581 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5582 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5583 64c6d990 2019-07-11 stsp
5584 64c6d990 2019-07-11 stsp return NULL;
5585 64c6d990 2019-07-11 stsp }
5586 64c6d990 2019-07-11 stsp
5587 64c6d990 2019-07-11 stsp static const struct got_error *
5588 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5589 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5590 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5591 64c6d990 2019-07-11 stsp {
5592 64c6d990 2019-07-11 stsp const struct got_error *err;
5593 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5594 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5595 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5596 64c6d990 2019-07-11 stsp
5597 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5598 64c6d990 2019-07-11 stsp return NULL;
5599 64c6d990 2019-07-11 stsp
5600 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5601 64c6d990 2019-07-11 stsp if (err)
5602 64c6d990 2019-07-11 stsp goto done;
5603 64c6d990 2019-07-11 stsp
5604 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5605 64c6d990 2019-07-11 stsp if (err)
5606 64c6d990 2019-07-11 stsp goto done;
5607 64c6d990 2019-07-11 stsp
5608 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5609 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5610 64c6d990 2019-07-11 stsp if (err)
5611 64c6d990 2019-07-11 stsp goto done;
5612 64c6d990 2019-07-11 stsp
5613 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5614 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5615 64c6d990 2019-07-11 stsp if (err)
5616 64c6d990 2019-07-11 stsp goto done;
5617 64c6d990 2019-07-11 stsp
5618 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5619 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5620 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5621 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5622 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5623 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5624 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5625 64c6d990 2019-07-11 stsp done:
5626 64c6d990 2019-07-11 stsp if (tree1)
5627 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5628 64c6d990 2019-07-11 stsp if (tree2)
5629 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5630 64c6d990 2019-07-11 stsp if (commit)
5631 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5632 64c6d990 2019-07-11 stsp if (parent_commit)
5633 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5634 64c6d990 2019-07-11 stsp return err;
5635 ff0d2220 2019-07-11 stsp }
5636 ff0d2220 2019-07-11 stsp
5637 ff0d2220 2019-07-11 stsp static const struct got_error *
5638 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5639 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5640 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5641 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5642 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5643 af61c510 2019-07-19 stsp {
5644 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5645 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5646 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5647 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5648 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5649 af61c510 2019-07-19 stsp
5650 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5651 af61c510 2019-07-19 stsp if (err)
5652 af61c510 2019-07-19 stsp return err;
5653 af61c510 2019-07-19 stsp
5654 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5655 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5656 af61c510 2019-07-19 stsp if (err)
5657 af61c510 2019-07-19 stsp goto done;
5658 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5659 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5660 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5661 af61c510 2019-07-19 stsp if (err) {
5662 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5663 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5664 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5665 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5666 af61c510 2019-07-19 stsp "been reached?!?");
5667 ee780d5c 2020-01-04 stsp }
5668 ee780d5c 2020-01-04 stsp goto done;
5669 af61c510 2019-07-19 stsp } else {
5670 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5671 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5672 af61c510 2019-07-19 stsp if (err)
5673 af61c510 2019-07-19 stsp goto done;
5674 af61c510 2019-07-19 stsp
5675 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5676 af61c510 2019-07-19 stsp if (err)
5677 af61c510 2019-07-19 stsp goto done;
5678 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5679 af61c510 2019-07-19 stsp commit_id = parent_id;
5680 af61c510 2019-07-19 stsp }
5681 af61c510 2019-07-19 stsp }
5682 af61c510 2019-07-19 stsp done:
5683 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5684 af61c510 2019-07-19 stsp return err;
5685 af61c510 2019-07-19 stsp }
5686 af61c510 2019-07-19 stsp
5687 af61c510 2019-07-19 stsp static const struct got_error *
5688 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5689 818c7501 2019-07-11 stsp {
5690 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5691 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5692 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5693 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5694 818c7501 2019-07-11 stsp char *cwd = NULL;
5695 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5696 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5697 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5698 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5699 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5700 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5701 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5702 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5703 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5704 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5705 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5706 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5707 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5708 818c7501 2019-07-11 stsp
5709 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5710 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5711 818c7501 2019-07-11 stsp
5712 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5713 818c7501 2019-07-11 stsp switch (ch) {
5714 818c7501 2019-07-11 stsp case 'a':
5715 818c7501 2019-07-11 stsp abort_rebase = 1;
5716 818c7501 2019-07-11 stsp break;
5717 818c7501 2019-07-11 stsp case 'c':
5718 818c7501 2019-07-11 stsp continue_rebase = 1;
5719 818c7501 2019-07-11 stsp break;
5720 818c7501 2019-07-11 stsp default:
5721 818c7501 2019-07-11 stsp usage_rebase();
5722 818c7501 2019-07-11 stsp /* NOTREACHED */
5723 818c7501 2019-07-11 stsp }
5724 818c7501 2019-07-11 stsp }
5725 818c7501 2019-07-11 stsp
5726 818c7501 2019-07-11 stsp argc -= optind;
5727 818c7501 2019-07-11 stsp argv += optind;
5728 818c7501 2019-07-11 stsp
5729 43012d58 2019-07-14 stsp #ifndef PROFILE
5730 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5731 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5732 43012d58 2019-07-14 stsp err(1, "pledge");
5733 43012d58 2019-07-14 stsp #endif
5734 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5735 818c7501 2019-07-11 stsp usage_rebase();
5736 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5737 818c7501 2019-07-11 stsp if (argc != 0)
5738 818c7501 2019-07-11 stsp usage_rebase();
5739 818c7501 2019-07-11 stsp } else if (argc != 1)
5740 818c7501 2019-07-11 stsp usage_rebase();
5741 818c7501 2019-07-11 stsp
5742 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5743 818c7501 2019-07-11 stsp if (cwd == NULL) {
5744 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5745 818c7501 2019-07-11 stsp goto done;
5746 818c7501 2019-07-11 stsp }
5747 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5748 818c7501 2019-07-11 stsp if (error)
5749 818c7501 2019-07-11 stsp goto done;
5750 818c7501 2019-07-11 stsp
5751 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5752 c9956ddf 2019-09-08 stsp NULL);
5753 818c7501 2019-07-11 stsp if (error != NULL)
5754 818c7501 2019-07-11 stsp goto done;
5755 818c7501 2019-07-11 stsp
5756 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5757 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5758 7ef62c4e 2020-02-24 stsp if (error)
5759 7ef62c4e 2020-02-24 stsp goto done;
5760 7ef62c4e 2020-02-24 stsp
5761 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5762 7ef62c4e 2020-02-24 stsp worktree);
5763 818c7501 2019-07-11 stsp if (error)
5764 7ef62c4e 2020-02-24 stsp goto done;
5765 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5766 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5767 818c7501 2019-07-11 stsp goto done;
5768 7ef62c4e 2020-02-24 stsp }
5769 818c7501 2019-07-11 stsp
5770 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5771 818c7501 2019-07-11 stsp if (error)
5772 818c7501 2019-07-11 stsp goto done;
5773 818c7501 2019-07-11 stsp
5774 f6794adc 2019-07-23 stsp if (abort_rebase) {
5775 818c7501 2019-07-11 stsp int did_something;
5776 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5777 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5778 f6794adc 2019-07-23 stsp goto done;
5779 f6794adc 2019-07-23 stsp }
5780 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5781 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5782 3e3a69f1 2019-07-25 stsp worktree, repo);
5783 818c7501 2019-07-11 stsp if (error)
5784 818c7501 2019-07-11 stsp goto done;
5785 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5786 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5787 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5788 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5789 818c7501 2019-07-11 stsp if (error)
5790 818c7501 2019-07-11 stsp goto done;
5791 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5792 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5793 818c7501 2019-07-11 stsp }
5794 818c7501 2019-07-11 stsp
5795 818c7501 2019-07-11 stsp if (continue_rebase) {
5796 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5797 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5798 f6794adc 2019-07-23 stsp goto done;
5799 f6794adc 2019-07-23 stsp }
5800 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5801 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5802 3e3a69f1 2019-07-25 stsp worktree, repo);
5803 818c7501 2019-07-11 stsp if (error)
5804 818c7501 2019-07-11 stsp goto done;
5805 818c7501 2019-07-11 stsp
5806 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5807 01757395 2019-07-12 stsp resume_commit_id, repo);
5808 818c7501 2019-07-11 stsp if (error)
5809 818c7501 2019-07-11 stsp goto done;
5810 818c7501 2019-07-11 stsp
5811 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5812 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5813 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5814 818c7501 2019-07-11 stsp goto done;
5815 818c7501 2019-07-11 stsp }
5816 818c7501 2019-07-11 stsp } else {
5817 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5818 818c7501 2019-07-11 stsp if (error != NULL)
5819 818c7501 2019-07-11 stsp goto done;
5820 ff0d2220 2019-07-11 stsp }
5821 818c7501 2019-07-11 stsp
5822 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5823 ff0d2220 2019-07-11 stsp if (error)
5824 ff0d2220 2019-07-11 stsp goto done;
5825 ff0d2220 2019-07-11 stsp
5826 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5827 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5828 a51a74b3 2019-07-27 stsp
5829 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5830 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5831 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5832 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5833 818c7501 2019-07-11 stsp if (error)
5834 818c7501 2019-07-11 stsp goto done;
5835 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5836 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5837 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5838 818c7501 2019-07-11 stsp "with work tree's branch");
5839 818c7501 2019-07-11 stsp goto done;
5840 818c7501 2019-07-11 stsp }
5841 818c7501 2019-07-11 stsp
5842 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5843 a51a74b3 2019-07-27 stsp if (error) {
5844 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5845 a51a74b3 2019-07-27 stsp goto done;
5846 a51a74b3 2019-07-27 stsp error = NULL;
5847 a51a74b3 2019-07-27 stsp } else {
5848 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5849 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5850 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5851 a51a74b3 2019-07-27 stsp goto done;
5852 a51a74b3 2019-07-27 stsp }
5853 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5854 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5855 818c7501 2019-07-11 stsp if (error)
5856 818c7501 2019-07-11 stsp goto done;
5857 818c7501 2019-07-11 stsp }
5858 818c7501 2019-07-11 stsp
5859 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5860 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5861 818c7501 2019-07-11 stsp if (error)
5862 818c7501 2019-07-11 stsp goto done;
5863 818c7501 2019-07-11 stsp
5864 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5865 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5866 fc66b545 2019-08-12 stsp if (pid == NULL) {
5867 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5868 fc66b545 2019-08-12 stsp int did_something;
5869 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5870 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5871 fc66b545 2019-08-12 stsp &did_something);
5872 fc66b545 2019-08-12 stsp if (error)
5873 fc66b545 2019-08-12 stsp goto done;
5874 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5875 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5876 fc66b545 2019-08-12 stsp }
5877 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5878 fc66b545 2019-08-12 stsp goto done;
5879 fc66b545 2019-08-12 stsp }
5880 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5881 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5882 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5883 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5884 818c7501 2019-07-11 stsp commit = NULL;
5885 818c7501 2019-07-11 stsp if (error)
5886 818c7501 2019-07-11 stsp goto done;
5887 64c6d990 2019-07-11 stsp
5888 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5889 38b0338b 2019-11-29 stsp if (continue_rebase) {
5890 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5891 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5892 38b0338b 2019-11-29 stsp goto done;
5893 38b0338b 2019-11-29 stsp } else {
5894 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5895 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5896 38b0338b 2019-11-29 stsp char *id_str;
5897 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5898 38b0338b 2019-11-29 stsp new_base_branch);
5899 38b0338b 2019-11-29 stsp if (error)
5900 38b0338b 2019-11-29 stsp goto done;
5901 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5902 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5903 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5904 38b0338b 2019-11-29 stsp free(id_str);
5905 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5906 38b0338b 2019-11-29 stsp new_head_commit_id);
5907 38b0338b 2019-11-29 stsp if (error)
5908 38b0338b 2019-11-29 stsp goto done;
5909 38b0338b 2019-11-29 stsp }
5910 818c7501 2019-07-11 stsp }
5911 818c7501 2019-07-11 stsp
5912 818c7501 2019-07-11 stsp pid = NULL;
5913 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5914 818c7501 2019-07-11 stsp commit_id = qid->id;
5915 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5916 818c7501 2019-07-11 stsp pid = qid;
5917 818c7501 2019-07-11 stsp
5918 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5919 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5920 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5921 818c7501 2019-07-11 stsp if (error)
5922 818c7501 2019-07-11 stsp goto done;
5923 818c7501 2019-07-11 stsp
5924 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5925 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5926 a0ea4fc0 2020-02-28 stsp if (error)
5927 a0ea4fc0 2020-02-28 stsp goto done;
5928 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5929 818c7501 2019-07-11 stsp break;
5930 01757395 2019-07-12 stsp }
5931 818c7501 2019-07-11 stsp
5932 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5933 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5934 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5935 818c7501 2019-07-11 stsp if (error)
5936 818c7501 2019-07-11 stsp goto done;
5937 818c7501 2019-07-11 stsp }
5938 818c7501 2019-07-11 stsp
5939 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5940 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5941 818c7501 2019-07-11 stsp if (error)
5942 818c7501 2019-07-11 stsp goto done;
5943 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5944 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5945 818c7501 2019-07-11 stsp } else
5946 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5947 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5948 818c7501 2019-07-11 stsp done:
5949 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5950 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5951 818c7501 2019-07-11 stsp free(resume_commit_id);
5952 818c7501 2019-07-11 stsp free(yca_id);
5953 818c7501 2019-07-11 stsp if (commit)
5954 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5955 818c7501 2019-07-11 stsp if (branch)
5956 818c7501 2019-07-11 stsp got_ref_close(branch);
5957 818c7501 2019-07-11 stsp if (new_base_branch)
5958 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5959 818c7501 2019-07-11 stsp if (tmp_branch)
5960 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5961 5ef14e63 2019-06-02 stsp if (worktree)
5962 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5963 5ef14e63 2019-06-02 stsp if (repo)
5964 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5965 5ef14e63 2019-06-02 stsp return error;
5966 0ebf8283 2019-07-24 stsp }
5967 0ebf8283 2019-07-24 stsp
5968 0ebf8283 2019-07-24 stsp __dead static void
5969 0ebf8283 2019-07-24 stsp usage_histedit(void)
5970 0ebf8283 2019-07-24 stsp {
5971 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5972 0ebf8283 2019-07-24 stsp getprogname());
5973 0ebf8283 2019-07-24 stsp exit(1);
5974 0ebf8283 2019-07-24 stsp }
5975 0ebf8283 2019-07-24 stsp
5976 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5977 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5978 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5979 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5980 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5981 0ebf8283 2019-07-24 stsp
5982 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5983 0ebf8283 2019-07-24 stsp unsigned char code;
5984 0ebf8283 2019-07-24 stsp const char *name;
5985 0ebf8283 2019-07-24 stsp const char *desc;
5986 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5987 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5988 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5989 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5990 82997472 2020-01-29 stsp "be used" },
5991 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5992 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5993 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5994 0ebf8283 2019-07-24 stsp };
5995 0ebf8283 2019-07-24 stsp
5996 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5997 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5998 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5999 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6000 0ebf8283 2019-07-24 stsp char *logmsg;
6001 0ebf8283 2019-07-24 stsp };
6002 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6003 0ebf8283 2019-07-24 stsp
6004 0ebf8283 2019-07-24 stsp static const struct got_error *
6005 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6006 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6007 0ebf8283 2019-07-24 stsp {
6008 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6009 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6010 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6011 8138f3e1 2019-08-11 stsp int n;
6012 0ebf8283 2019-07-24 stsp
6013 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_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 err = get_short_logmsg(&logmsg, 34, commit);
6018 0ebf8283 2019-07-24 stsp if (err)
6019 0ebf8283 2019-07-24 stsp goto done;
6020 0ebf8283 2019-07-24 stsp
6021 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6022 0ebf8283 2019-07-24 stsp if (err)
6023 0ebf8283 2019-07-24 stsp goto done;
6024 0ebf8283 2019-07-24 stsp
6025 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6026 0ebf8283 2019-07-24 stsp if (n < 0)
6027 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6028 0ebf8283 2019-07-24 stsp done:
6029 0ebf8283 2019-07-24 stsp if (commit)
6030 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6031 0ebf8283 2019-07-24 stsp free(id_str);
6032 0ebf8283 2019-07-24 stsp free(logmsg);
6033 0ebf8283 2019-07-24 stsp return err;
6034 0ebf8283 2019-07-24 stsp }
6035 0ebf8283 2019-07-24 stsp
6036 0ebf8283 2019-07-24 stsp static const struct got_error *
6037 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6038 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6039 0ebf8283 2019-07-24 stsp {
6040 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6041 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6042 0ebf8283 2019-07-24 stsp
6043 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6044 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6045 0ebf8283 2019-07-24 stsp
6046 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6047 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6048 0ebf8283 2019-07-24 stsp f, repo);
6049 0ebf8283 2019-07-24 stsp if (err)
6050 0ebf8283 2019-07-24 stsp break;
6051 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6052 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6053 083957f4 2020-02-24 stsp if (n < 0) {
6054 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6055 083957f4 2020-02-24 stsp break;
6056 083957f4 2020-02-24 stsp }
6057 083957f4 2020-02-24 stsp }
6058 0ebf8283 2019-07-24 stsp }
6059 0ebf8283 2019-07-24 stsp
6060 0ebf8283 2019-07-24 stsp return err;
6061 0ebf8283 2019-07-24 stsp }
6062 0ebf8283 2019-07-24 stsp
6063 0ebf8283 2019-07-24 stsp static const struct got_error *
6064 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6065 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6066 0ebf8283 2019-07-24 stsp {
6067 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6068 0ebf8283 2019-07-24 stsp int n, i;
6069 514f2ffe 2020-01-29 stsp char *id_str;
6070 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6071 514f2ffe 2020-01-29 stsp
6072 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6073 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6074 514f2ffe 2020-01-29 stsp if (err)
6075 514f2ffe 2020-01-29 stsp return err;
6076 514f2ffe 2020-01-29 stsp
6077 514f2ffe 2020-01-29 stsp n = fprintf(f,
6078 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6079 514f2ffe 2020-01-29 stsp "# commit %s\n"
6080 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6081 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6082 514f2ffe 2020-01-29 stsp if (n < 0) {
6083 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6084 514f2ffe 2020-01-29 stsp goto done;
6085 514f2ffe 2020-01-29 stsp }
6086 0ebf8283 2019-07-24 stsp
6087 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6088 514f2ffe 2020-01-29 stsp if (n < 0) {
6089 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6090 514f2ffe 2020-01-29 stsp goto done;
6091 514f2ffe 2020-01-29 stsp }
6092 0ebf8283 2019-07-24 stsp
6093 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6094 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6095 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6096 0ebf8283 2019-07-24 stsp cmd->desc);
6097 0ebf8283 2019-07-24 stsp if (n < 0) {
6098 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6099 0ebf8283 2019-07-24 stsp break;
6100 0ebf8283 2019-07-24 stsp }
6101 0ebf8283 2019-07-24 stsp }
6102 514f2ffe 2020-01-29 stsp done:
6103 514f2ffe 2020-01-29 stsp free(id_str);
6104 0ebf8283 2019-07-24 stsp return err;
6105 0ebf8283 2019-07-24 stsp }
6106 0ebf8283 2019-07-24 stsp
6107 0ebf8283 2019-07-24 stsp static const struct got_error *
6108 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6109 0ebf8283 2019-07-24 stsp {
6110 0ebf8283 2019-07-24 stsp static char msg[42];
6111 0ebf8283 2019-07-24 stsp int ret;
6112 0ebf8283 2019-07-24 stsp
6113 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6114 0ebf8283 2019-07-24 stsp lineno);
6115 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6116 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6117 0ebf8283 2019-07-24 stsp
6118 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6119 0ebf8283 2019-07-24 stsp }
6120 0ebf8283 2019-07-24 stsp
6121 0ebf8283 2019-07-24 stsp static const struct got_error *
6122 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6123 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6124 0ebf8283 2019-07-24 stsp {
6125 0ebf8283 2019-07-24 stsp const struct got_error *err;
6126 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6127 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6128 0ebf8283 2019-07-24 stsp
6129 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6130 0ebf8283 2019-07-24 stsp if (err)
6131 0ebf8283 2019-07-24 stsp return err;
6132 0ebf8283 2019-07-24 stsp
6133 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6134 0ebf8283 2019-07-24 stsp if (err)
6135 0ebf8283 2019-07-24 stsp goto done;
6136 0ebf8283 2019-07-24 stsp
6137 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6138 5943eee2 2019-08-13 stsp if (err)
6139 5943eee2 2019-08-13 stsp goto done;
6140 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6141 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6142 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6143 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6144 0ebf8283 2019-07-24 stsp }
6145 0ebf8283 2019-07-24 stsp done:
6146 0ebf8283 2019-07-24 stsp if (folded_commit)
6147 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6148 0ebf8283 2019-07-24 stsp free(id_str);
6149 5943eee2 2019-08-13 stsp free(folded_logmsg);
6150 0ebf8283 2019-07-24 stsp return err;
6151 0ebf8283 2019-07-24 stsp }
6152 0ebf8283 2019-07-24 stsp
6153 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6154 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6155 0ebf8283 2019-07-24 stsp {
6156 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6157 0ebf8283 2019-07-24 stsp
6158 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6159 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6160 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6161 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6162 3f9de99f 2019-07-24 stsp folded = prev;
6163 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6164 0ebf8283 2019-07-24 stsp }
6165 0ebf8283 2019-07-24 stsp
6166 0ebf8283 2019-07-24 stsp return folded;
6167 0ebf8283 2019-07-24 stsp }
6168 0ebf8283 2019-07-24 stsp
6169 0ebf8283 2019-07-24 stsp static const struct got_error *
6170 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6171 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6172 0ebf8283 2019-07-24 stsp {
6173 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6174 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6175 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6176 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6177 0ebf8283 2019-07-24 stsp int fd;
6178 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6179 0ebf8283 2019-07-24 stsp
6180 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6181 0ebf8283 2019-07-24 stsp if (err)
6182 0ebf8283 2019-07-24 stsp return err;
6183 0ebf8283 2019-07-24 stsp
6184 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6185 0ebf8283 2019-07-24 stsp if (folded) {
6186 0ebf8283 2019-07-24 stsp while (folded != hle) {
6187 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6188 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6189 3f9de99f 2019-07-24 stsp continue;
6190 3f9de99f 2019-07-24 stsp }
6191 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6192 0ebf8283 2019-07-24 stsp logmsg, repo);
6193 0ebf8283 2019-07-24 stsp if (err)
6194 0ebf8283 2019-07-24 stsp goto done;
6195 0ebf8283 2019-07-24 stsp free(logmsg);
6196 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6197 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6198 0ebf8283 2019-07-24 stsp }
6199 0ebf8283 2019-07-24 stsp }
6200 0ebf8283 2019-07-24 stsp
6201 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6202 0ebf8283 2019-07-24 stsp if (err)
6203 0ebf8283 2019-07-24 stsp goto done;
6204 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6205 5943eee2 2019-08-13 stsp if (err)
6206 5943eee2 2019-08-13 stsp goto done;
6207 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6208 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6209 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6210 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6211 0ebf8283 2019-07-24 stsp goto done;
6212 0ebf8283 2019-07-24 stsp }
6213 0ebf8283 2019-07-24 stsp free(logmsg);
6214 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6215 0ebf8283 2019-07-24 stsp
6216 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6217 0ebf8283 2019-07-24 stsp if (err)
6218 0ebf8283 2019-07-24 stsp goto done;
6219 0ebf8283 2019-07-24 stsp
6220 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6221 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6222 0ebf8283 2019-07-24 stsp if (err)
6223 0ebf8283 2019-07-24 stsp goto done;
6224 0ebf8283 2019-07-24 stsp
6225 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6226 0ebf8283 2019-07-24 stsp close(fd);
6227 0ebf8283 2019-07-24 stsp
6228 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6229 0ebf8283 2019-07-24 stsp if (err)
6230 0ebf8283 2019-07-24 stsp goto done;
6231 0ebf8283 2019-07-24 stsp
6232 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6233 0ebf8283 2019-07-24 stsp if (err) {
6234 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6235 0ebf8283 2019-07-24 stsp goto done;
6236 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6237 0ebf8283 2019-07-24 stsp }
6238 0ebf8283 2019-07-24 stsp done:
6239 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6240 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6241 0ebf8283 2019-07-24 stsp free(logmsg_path);
6242 0ebf8283 2019-07-24 stsp free(logmsg);
6243 5943eee2 2019-08-13 stsp free(orig_logmsg);
6244 0ebf8283 2019-07-24 stsp free(editor);
6245 0ebf8283 2019-07-24 stsp if (commit)
6246 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6247 0ebf8283 2019-07-24 stsp return err;
6248 5ef14e63 2019-06-02 stsp }
6249 0ebf8283 2019-07-24 stsp
6250 0ebf8283 2019-07-24 stsp static const struct got_error *
6251 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6252 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6253 0ebf8283 2019-07-24 stsp {
6254 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6255 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6256 0ebf8283 2019-07-24 stsp size_t size;
6257 0ebf8283 2019-07-24 stsp ssize_t len;
6258 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6259 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6260 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6261 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6262 0ebf8283 2019-07-24 stsp
6263 0ebf8283 2019-07-24 stsp for (;;) {
6264 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6265 0ebf8283 2019-07-24 stsp if (len == -1) {
6266 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6267 0ebf8283 2019-07-24 stsp if (feof(f))
6268 0ebf8283 2019-07-24 stsp break;
6269 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6270 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6271 0ebf8283 2019-07-24 stsp break;
6272 0ebf8283 2019-07-24 stsp }
6273 0ebf8283 2019-07-24 stsp lineno++;
6274 0ebf8283 2019-07-24 stsp p = line;
6275 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6276 0ebf8283 2019-07-24 stsp p++;
6277 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6278 0ebf8283 2019-07-24 stsp free(line);
6279 0ebf8283 2019-07-24 stsp line = NULL;
6280 0ebf8283 2019-07-24 stsp continue;
6281 0ebf8283 2019-07-24 stsp }
6282 0ebf8283 2019-07-24 stsp cmd = NULL;
6283 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6284 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6285 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6286 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6287 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6288 0ebf8283 2019-07-24 stsp break;
6289 0ebf8283 2019-07-24 stsp }
6290 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6291 0ebf8283 2019-07-24 stsp p++;
6292 0ebf8283 2019-07-24 stsp break;
6293 0ebf8283 2019-07-24 stsp }
6294 0ebf8283 2019-07-24 stsp }
6295 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6296 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6297 0ebf8283 2019-07-24 stsp break;
6298 0ebf8283 2019-07-24 stsp }
6299 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6300 0ebf8283 2019-07-24 stsp p++;
6301 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6302 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6303 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6304 0ebf8283 2019-07-24 stsp break;
6305 0ebf8283 2019-07-24 stsp }
6306 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6307 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6308 0ebf8283 2019-07-24 stsp if (err)
6309 0ebf8283 2019-07-24 stsp break;
6310 0ebf8283 2019-07-24 stsp } else {
6311 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6312 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6313 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6314 0ebf8283 2019-07-24 stsp break;
6315 0ebf8283 2019-07-24 stsp }
6316 0ebf8283 2019-07-24 stsp }
6317 0ebf8283 2019-07-24 stsp free(line);
6318 0ebf8283 2019-07-24 stsp line = NULL;
6319 0ebf8283 2019-07-24 stsp continue;
6320 0ebf8283 2019-07-24 stsp } else {
6321 0ebf8283 2019-07-24 stsp end = p;
6322 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6323 0ebf8283 2019-07-24 stsp end++;
6324 0ebf8283 2019-07-24 stsp *end = '\0';
6325 0ebf8283 2019-07-24 stsp
6326 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6327 0ebf8283 2019-07-24 stsp if (err) {
6328 0ebf8283 2019-07-24 stsp /* override error code */
6329 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6330 0ebf8283 2019-07-24 stsp break;
6331 0ebf8283 2019-07-24 stsp }
6332 0ebf8283 2019-07-24 stsp }
6333 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6334 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6335 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6336 0ebf8283 2019-07-24 stsp break;
6337 0ebf8283 2019-07-24 stsp }
6338 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6339 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6340 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6341 0ebf8283 2019-07-24 stsp commit_id = NULL;
6342 0ebf8283 2019-07-24 stsp free(line);
6343 0ebf8283 2019-07-24 stsp line = NULL;
6344 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6345 0ebf8283 2019-07-24 stsp }
6346 0ebf8283 2019-07-24 stsp
6347 0ebf8283 2019-07-24 stsp free(line);
6348 0ebf8283 2019-07-24 stsp free(commit_id);
6349 0ebf8283 2019-07-24 stsp return err;
6350 0ebf8283 2019-07-24 stsp }
6351 0ebf8283 2019-07-24 stsp
6352 0ebf8283 2019-07-24 stsp static const struct got_error *
6353 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6354 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6355 bfce7f83 2019-07-27 stsp {
6356 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6357 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6358 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6359 5b87815e 2020-03-05 stsp static char msg[92];
6360 bfce7f83 2019-07-27 stsp char *id_str;
6361 bfce7f83 2019-07-27 stsp
6362 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6363 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6364 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6365 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6366 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6367 5b87815e 2020-03-05 stsp
6368 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6369 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6370 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6371 5b87815e 2020-03-05 stsp if (hle == hle2)
6372 5b87815e 2020-03-05 stsp continue;
6373 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6374 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6375 5b87815e 2020-03-05 stsp continue;
6376 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6377 5b87815e 2020-03-05 stsp if (err)
6378 5b87815e 2020-03-05 stsp return err;
6379 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6380 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6381 5b87815e 2020-03-05 stsp free(id_str);
6382 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6383 5b87815e 2020-03-05 stsp }
6384 5b87815e 2020-03-05 stsp }
6385 bfce7f83 2019-07-27 stsp
6386 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6387 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6388 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6389 bfce7f83 2019-07-27 stsp break;
6390 bfce7f83 2019-07-27 stsp }
6391 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6392 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6393 bfce7f83 2019-07-27 stsp if (err)
6394 bfce7f83 2019-07-27 stsp return err;
6395 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6396 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6397 bfce7f83 2019-07-27 stsp free(id_str);
6398 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6399 bfce7f83 2019-07-27 stsp }
6400 bfce7f83 2019-07-27 stsp }
6401 bfce7f83 2019-07-27 stsp
6402 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6403 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6404 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6405 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6406 bfce7f83 2019-07-27 stsp
6407 bfce7f83 2019-07-27 stsp return NULL;
6408 bfce7f83 2019-07-27 stsp }
6409 bfce7f83 2019-07-27 stsp
6410 bfce7f83 2019-07-27 stsp static const struct got_error *
6411 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6412 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6413 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6414 0ebf8283 2019-07-24 stsp {
6415 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6416 0ebf8283 2019-07-24 stsp char *editor;
6417 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6418 0ebf8283 2019-07-24 stsp
6419 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6420 0ebf8283 2019-07-24 stsp if (err)
6421 0ebf8283 2019-07-24 stsp return err;
6422 0ebf8283 2019-07-24 stsp
6423 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6424 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6425 0ebf8283 2019-07-24 stsp goto done;
6426 0ebf8283 2019-07-24 stsp }
6427 0ebf8283 2019-07-24 stsp
6428 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6429 0ebf8283 2019-07-24 stsp if (f == NULL) {
6430 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6431 0ebf8283 2019-07-24 stsp goto done;
6432 0ebf8283 2019-07-24 stsp }
6433 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6434 bfce7f83 2019-07-27 stsp if (err)
6435 bfce7f83 2019-07-27 stsp goto done;
6436 bfce7f83 2019-07-27 stsp
6437 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6438 0ebf8283 2019-07-24 stsp done:
6439 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6440 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6441 0ebf8283 2019-07-24 stsp free(editor);
6442 0ebf8283 2019-07-24 stsp return err;
6443 0ebf8283 2019-07-24 stsp }
6444 0ebf8283 2019-07-24 stsp
6445 0ebf8283 2019-07-24 stsp static const struct got_error *
6446 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6447 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6448 514f2ffe 2020-01-29 stsp struct got_repository *);
6449 0ebf8283 2019-07-24 stsp
6450 0ebf8283 2019-07-24 stsp static const struct got_error *
6451 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6452 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6453 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6454 0ebf8283 2019-07-24 stsp {
6455 0ebf8283 2019-07-24 stsp const struct got_error *err;
6456 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6457 0ebf8283 2019-07-24 stsp char *path = NULL;
6458 0ebf8283 2019-07-24 stsp
6459 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6460 0ebf8283 2019-07-24 stsp if (err)
6461 0ebf8283 2019-07-24 stsp return err;
6462 0ebf8283 2019-07-24 stsp
6463 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6464 0ebf8283 2019-07-24 stsp if (err)
6465 0ebf8283 2019-07-24 stsp goto done;
6466 0ebf8283 2019-07-24 stsp
6467 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6468 0ebf8283 2019-07-24 stsp if (err)
6469 0ebf8283 2019-07-24 stsp goto done;
6470 0ebf8283 2019-07-24 stsp
6471 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6472 083957f4 2020-02-24 stsp rewind(f);
6473 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6474 083957f4 2020-02-24 stsp } else {
6475 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6476 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6477 0ebf8283 2019-07-24 stsp goto done;
6478 083957f4 2020-02-24 stsp }
6479 083957f4 2020-02-24 stsp f = NULL;
6480 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6481 083957f4 2020-02-24 stsp if (err) {
6482 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6483 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6484 083957f4 2020-02-24 stsp goto done;
6485 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6486 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6487 083957f4 2020-02-24 stsp }
6488 0ebf8283 2019-07-24 stsp }
6489 0ebf8283 2019-07-24 stsp done:
6490 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6491 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6492 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6493 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6494 0ebf8283 2019-07-24 stsp free(path);
6495 0ebf8283 2019-07-24 stsp return err;
6496 0ebf8283 2019-07-24 stsp }
6497 0ebf8283 2019-07-24 stsp
6498 0ebf8283 2019-07-24 stsp static const struct got_error *
6499 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6500 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6501 0ebf8283 2019-07-24 stsp {
6502 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6503 0ebf8283 2019-07-24 stsp char *path = NULL;
6504 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6505 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6506 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6507 0ebf8283 2019-07-24 stsp
6508 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6509 0ebf8283 2019-07-24 stsp if (err)
6510 0ebf8283 2019-07-24 stsp return err;
6511 0ebf8283 2019-07-24 stsp
6512 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6513 0ebf8283 2019-07-24 stsp if (f == NULL) {
6514 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6515 0ebf8283 2019-07-24 stsp goto done;
6516 0ebf8283 2019-07-24 stsp }
6517 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6518 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6519 0ebf8283 2019-07-24 stsp repo);
6520 0ebf8283 2019-07-24 stsp if (err)
6521 0ebf8283 2019-07-24 stsp break;
6522 0ebf8283 2019-07-24 stsp
6523 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6524 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6525 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6526 0ebf8283 2019-07-24 stsp if (n < 0) {
6527 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6528 0ebf8283 2019-07-24 stsp break;
6529 0ebf8283 2019-07-24 stsp }
6530 0ebf8283 2019-07-24 stsp }
6531 0ebf8283 2019-07-24 stsp }
6532 0ebf8283 2019-07-24 stsp done:
6533 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6534 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6535 0ebf8283 2019-07-24 stsp free(path);
6536 0ebf8283 2019-07-24 stsp if (commit)
6537 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6538 0ebf8283 2019-07-24 stsp return err;
6539 0ebf8283 2019-07-24 stsp }
6540 0ebf8283 2019-07-24 stsp
6541 bfce7f83 2019-07-27 stsp void
6542 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6543 bfce7f83 2019-07-27 stsp {
6544 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6545 bfce7f83 2019-07-27 stsp
6546 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6547 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6548 bfce7f83 2019-07-27 stsp free(hle);
6549 bfce7f83 2019-07-27 stsp }
6550 bfce7f83 2019-07-27 stsp }
6551 bfce7f83 2019-07-27 stsp
6552 0ebf8283 2019-07-24 stsp static const struct got_error *
6553 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6554 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6555 0ebf8283 2019-07-24 stsp {
6556 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6557 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6558 0ebf8283 2019-07-24 stsp
6559 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6560 0ebf8283 2019-07-24 stsp if (f == NULL) {
6561 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6562 0ebf8283 2019-07-24 stsp goto done;
6563 0ebf8283 2019-07-24 stsp }
6564 0ebf8283 2019-07-24 stsp
6565 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6566 0ebf8283 2019-07-24 stsp done:
6567 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6568 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6569 0ebf8283 2019-07-24 stsp return err;
6570 0ebf8283 2019-07-24 stsp }
6571 0ebf8283 2019-07-24 stsp
6572 0ebf8283 2019-07-24 stsp static const struct got_error *
6573 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6574 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6575 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6576 0ebf8283 2019-07-24 stsp {
6577 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6578 0ebf8283 2019-07-24 stsp int resp = ' ';
6579 0ebf8283 2019-07-24 stsp
6580 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6581 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6582 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6583 0ebf8283 2019-07-24 stsp resp = getchar();
6584 a61a4414 2019-08-07 stsp if (resp == '\n')
6585 a61a4414 2019-08-07 stsp resp = getchar();
6586 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6587 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6588 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6589 bfce7f83 2019-07-27 stsp repo);
6590 426ebf2e 2019-07-25 stsp if (err) {
6591 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6592 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6593 426ebf2e 2019-07-25 stsp break;
6594 bfce7f83 2019-07-27 stsp prev_err = err;
6595 426ebf2e 2019-07-25 stsp resp = ' ';
6596 426ebf2e 2019-07-25 stsp continue;
6597 426ebf2e 2019-07-25 stsp }
6598 bfce7f83 2019-07-27 stsp break;
6599 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6600 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6601 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6602 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6603 426ebf2e 2019-07-25 stsp if (err) {
6604 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6605 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6606 426ebf2e 2019-07-25 stsp break;
6607 bfce7f83 2019-07-27 stsp prev_err = err;
6608 426ebf2e 2019-07-25 stsp resp = ' ';
6609 426ebf2e 2019-07-25 stsp continue;
6610 426ebf2e 2019-07-25 stsp }
6611 bfce7f83 2019-07-27 stsp break;
6612 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6613 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6614 0ebf8283 2019-07-24 stsp break;
6615 426ebf2e 2019-07-25 stsp } else
6616 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6617 0ebf8283 2019-07-24 stsp }
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp return err;
6620 0ebf8283 2019-07-24 stsp }
6621 0ebf8283 2019-07-24 stsp
6622 0ebf8283 2019-07-24 stsp static const struct got_error *
6623 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6624 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6625 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6626 0ebf8283 2019-07-24 stsp {
6627 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6628 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6629 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6630 3e3a69f1 2019-07-25 stsp branch, repo);
6631 0ebf8283 2019-07-24 stsp }
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp static const struct got_error *
6634 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6635 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6636 0ebf8283 2019-07-24 stsp {
6637 0ebf8283 2019-07-24 stsp const struct got_error *err;
6638 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6639 0ebf8283 2019-07-24 stsp
6640 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6641 0ebf8283 2019-07-24 stsp if (err)
6642 0ebf8283 2019-07-24 stsp goto done;
6643 0ebf8283 2019-07-24 stsp
6644 0ebf8283 2019-07-24 stsp if (new_id) {
6645 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6646 0ebf8283 2019-07-24 stsp if (err)
6647 0ebf8283 2019-07-24 stsp goto done;
6648 0ebf8283 2019-07-24 stsp }
6649 0ebf8283 2019-07-24 stsp
6650 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6651 0ebf8283 2019-07-24 stsp if (new_id_str)
6652 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6653 0ebf8283 2019-07-24 stsp
6654 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6655 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6656 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6657 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6658 0ebf8283 2019-07-24 stsp goto done;
6659 0ebf8283 2019-07-24 stsp }
6660 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6661 0ebf8283 2019-07-24 stsp } else {
6662 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6663 0ebf8283 2019-07-24 stsp if (err)
6664 0ebf8283 2019-07-24 stsp goto done;
6665 0ebf8283 2019-07-24 stsp }
6666 0ebf8283 2019-07-24 stsp
6667 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6668 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6669 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6670 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6671 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6672 0ebf8283 2019-07-24 stsp break;
6673 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6674 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6675 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6676 0ebf8283 2019-07-24 stsp logmsg);
6677 0ebf8283 2019-07-24 stsp break;
6678 0ebf8283 2019-07-24 stsp default:
6679 0ebf8283 2019-07-24 stsp break;
6680 0ebf8283 2019-07-24 stsp }
6681 0ebf8283 2019-07-24 stsp done:
6682 0ebf8283 2019-07-24 stsp free(old_id_str);
6683 0ebf8283 2019-07-24 stsp free(new_id_str);
6684 0ebf8283 2019-07-24 stsp return err;
6685 0ebf8283 2019-07-24 stsp }
6686 0ebf8283 2019-07-24 stsp
6687 0ebf8283 2019-07-24 stsp static const struct got_error *
6688 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6689 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6690 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6691 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6692 0ebf8283 2019-07-24 stsp {
6693 0ebf8283 2019-07-24 stsp const struct got_error *err;
6694 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6695 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6696 0ebf8283 2019-07-24 stsp
6697 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6698 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6699 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6700 0ebf8283 2019-07-24 stsp if (err)
6701 0ebf8283 2019-07-24 stsp return err;
6702 0ebf8283 2019-07-24 stsp }
6703 0ebf8283 2019-07-24 stsp
6704 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6705 0ebf8283 2019-07-24 stsp if (err)
6706 0ebf8283 2019-07-24 stsp return err;
6707 0ebf8283 2019-07-24 stsp
6708 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6709 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6710 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6711 0ebf8283 2019-07-24 stsp if (err) {
6712 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6713 0ebf8283 2019-07-24 stsp goto done;
6714 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6715 0ebf8283 2019-07-24 stsp } else {
6716 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6717 0ebf8283 2019-07-24 stsp free(new_commit_id);
6718 0ebf8283 2019-07-24 stsp }
6719 0ebf8283 2019-07-24 stsp done:
6720 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6721 0ebf8283 2019-07-24 stsp return err;
6722 0ebf8283 2019-07-24 stsp }
6723 0ebf8283 2019-07-24 stsp
6724 0ebf8283 2019-07-24 stsp static const struct got_error *
6725 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6726 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6727 0ebf8283 2019-07-24 stsp {
6728 0ebf8283 2019-07-24 stsp const struct got_error *error;
6729 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6730 0ebf8283 2019-07-24 stsp
6731 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6732 0ebf8283 2019-07-24 stsp repo);
6733 0ebf8283 2019-07-24 stsp if (error)
6734 0ebf8283 2019-07-24 stsp return error;
6735 0ebf8283 2019-07-24 stsp
6736 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6737 0ebf8283 2019-07-24 stsp if (error)
6738 0ebf8283 2019-07-24 stsp return error;
6739 0ebf8283 2019-07-24 stsp
6740 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6741 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6742 0ebf8283 2019-07-24 stsp return error;
6743 ab20a43a 2020-01-29 stsp }
6744 ab20a43a 2020-01-29 stsp
6745 ab20a43a 2020-01-29 stsp static const struct got_error *
6746 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6747 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6748 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6749 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6750 ab20a43a 2020-01-29 stsp {
6751 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6752 ab20a43a 2020-01-29 stsp
6753 ab20a43a 2020-01-29 stsp switch (status) {
6754 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6755 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6756 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6757 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6758 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6759 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6760 ab20a43a 2020-01-29 stsp default:
6761 ab20a43a 2020-01-29 stsp break;
6762 ab20a43a 2020-01-29 stsp }
6763 ab20a43a 2020-01-29 stsp
6764 ab20a43a 2020-01-29 stsp switch (staged_status) {
6765 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6766 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6767 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6768 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6769 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6770 ab20a43a 2020-01-29 stsp default:
6771 ab20a43a 2020-01-29 stsp break;
6772 ab20a43a 2020-01-29 stsp }
6773 ab20a43a 2020-01-29 stsp
6774 ab20a43a 2020-01-29 stsp return NULL;
6775 0ebf8283 2019-07-24 stsp }
6776 0ebf8283 2019-07-24 stsp
6777 0ebf8283 2019-07-24 stsp static const struct got_error *
6778 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6779 0ebf8283 2019-07-24 stsp {
6780 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6781 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6782 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6783 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6784 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6785 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6786 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6787 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6788 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6789 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6790 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6791 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6792 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6793 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6794 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6795 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6796 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6797 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6798 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6799 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6800 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6801 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6802 0ebf8283 2019-07-24 stsp
6803 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6804 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6805 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6806 0ebf8283 2019-07-24 stsp
6807 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6808 0ebf8283 2019-07-24 stsp switch (ch) {
6809 0ebf8283 2019-07-24 stsp case 'a':
6810 0ebf8283 2019-07-24 stsp abort_edit = 1;
6811 0ebf8283 2019-07-24 stsp break;
6812 0ebf8283 2019-07-24 stsp case 'c':
6813 0ebf8283 2019-07-24 stsp continue_edit = 1;
6814 0ebf8283 2019-07-24 stsp break;
6815 0ebf8283 2019-07-24 stsp case 'F':
6816 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6817 0ebf8283 2019-07-24 stsp break;
6818 083957f4 2020-02-24 stsp case 'm':
6819 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6820 083957f4 2020-02-24 stsp break;
6821 0ebf8283 2019-07-24 stsp default:
6822 0ebf8283 2019-07-24 stsp usage_histedit();
6823 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6824 0ebf8283 2019-07-24 stsp }
6825 0ebf8283 2019-07-24 stsp }
6826 0ebf8283 2019-07-24 stsp
6827 0ebf8283 2019-07-24 stsp argc -= optind;
6828 0ebf8283 2019-07-24 stsp argv += optind;
6829 0ebf8283 2019-07-24 stsp
6830 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6831 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6832 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6833 0ebf8283 2019-07-24 stsp err(1, "pledge");
6834 0ebf8283 2019-07-24 stsp #endif
6835 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6836 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6837 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6838 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6839 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6840 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6841 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6842 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6843 0ebf8283 2019-07-24 stsp if (argc != 0)
6844 0ebf8283 2019-07-24 stsp usage_histedit();
6845 0ebf8283 2019-07-24 stsp
6846 0ebf8283 2019-07-24 stsp /*
6847 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6848 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6849 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6850 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6851 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6852 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6853 0ebf8283 2019-07-24 stsp */
6854 0ebf8283 2019-07-24 stsp
6855 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6856 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6857 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6858 0ebf8283 2019-07-24 stsp goto done;
6859 0ebf8283 2019-07-24 stsp }
6860 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6861 0ebf8283 2019-07-24 stsp if (error)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp
6864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6865 c9956ddf 2019-09-08 stsp NULL);
6866 0ebf8283 2019-07-24 stsp if (error != NULL)
6867 0ebf8283 2019-07-24 stsp goto done;
6868 0ebf8283 2019-07-24 stsp
6869 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6870 0ebf8283 2019-07-24 stsp if (error)
6871 0ebf8283 2019-07-24 stsp goto done;
6872 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6873 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp }
6876 0ebf8283 2019-07-24 stsp
6877 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6878 0ebf8283 2019-07-24 stsp if (error)
6879 0ebf8283 2019-07-24 stsp goto done;
6880 0ebf8283 2019-07-24 stsp
6881 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6882 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6883 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6884 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6885 083957f4 2020-02-24 stsp "before the -m option can be used");
6886 083957f4 2020-02-24 stsp goto done;
6887 083957f4 2020-02-24 stsp }
6888 083957f4 2020-02-24 stsp
6889 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6890 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6891 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6892 3e3a69f1 2019-07-25 stsp worktree, repo);
6893 0ebf8283 2019-07-24 stsp if (error)
6894 0ebf8283 2019-07-24 stsp goto done;
6895 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6896 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6897 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6898 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6899 0ebf8283 2019-07-24 stsp if (error)
6900 0ebf8283 2019-07-24 stsp goto done;
6901 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6902 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6903 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6904 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6905 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6906 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp }
6908 0ebf8283 2019-07-24 stsp
6909 0ebf8283 2019-07-24 stsp if (continue_edit) {
6910 0ebf8283 2019-07-24 stsp char *path;
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6913 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6914 0ebf8283 2019-07-24 stsp goto done;
6915 0ebf8283 2019-07-24 stsp }
6916 0ebf8283 2019-07-24 stsp
6917 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6918 0ebf8283 2019-07-24 stsp if (error)
6919 0ebf8283 2019-07-24 stsp goto done;
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6922 0ebf8283 2019-07-24 stsp free(path);
6923 0ebf8283 2019-07-24 stsp if (error)
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp
6926 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6927 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6928 3e3a69f1 2019-07-25 stsp worktree, repo);
6929 0ebf8283 2019-07-24 stsp if (error)
6930 0ebf8283 2019-07-24 stsp goto done;
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6933 0ebf8283 2019-07-24 stsp if (error)
6934 0ebf8283 2019-07-24 stsp goto done;
6935 0ebf8283 2019-07-24 stsp
6936 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6937 0ebf8283 2019-07-24 stsp head_commit_id);
6938 0ebf8283 2019-07-24 stsp if (error)
6939 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6941 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6942 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6943 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6944 3aac7cf7 2019-07-25 stsp goto done;
6945 3aac7cf7 2019-07-25 stsp }
6946 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6947 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6948 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6949 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6950 0ebf8283 2019-07-24 stsp commit = NULL;
6951 0ebf8283 2019-07-24 stsp if (error)
6952 0ebf8283 2019-07-24 stsp goto done;
6953 0ebf8283 2019-07-24 stsp } else {
6954 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6955 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6956 0ebf8283 2019-07-24 stsp goto done;
6957 0ebf8283 2019-07-24 stsp }
6958 0ebf8283 2019-07-24 stsp
6959 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6960 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6961 0ebf8283 2019-07-24 stsp if (error != NULL)
6962 0ebf8283 2019-07-24 stsp goto done;
6963 0ebf8283 2019-07-24 stsp
6964 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6965 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6966 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6967 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6968 c7d20a3f 2019-07-30 stsp goto done;
6969 c7d20a3f 2019-07-30 stsp }
6970 c7d20a3f 2019-07-30 stsp
6971 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6972 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6973 bfce7f83 2019-07-27 stsp branch = NULL;
6974 0ebf8283 2019-07-24 stsp if (error)
6975 0ebf8283 2019-07-24 stsp goto done;
6976 0ebf8283 2019-07-24 stsp
6977 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6978 0ebf8283 2019-07-24 stsp head_commit_id);
6979 0ebf8283 2019-07-24 stsp if (error)
6980 0ebf8283 2019-07-24 stsp goto done;
6981 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6982 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6983 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6984 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6985 3aac7cf7 2019-07-25 stsp goto done;
6986 3aac7cf7 2019-07-25 stsp }
6987 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6988 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6989 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6990 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6991 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6992 0ebf8283 2019-07-24 stsp commit = NULL;
6993 0ebf8283 2019-07-24 stsp if (error)
6994 0ebf8283 2019-07-24 stsp goto done;
6995 0ebf8283 2019-07-24 stsp
6996 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6997 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6998 c5996fff 2020-01-29 stsp goto done;
6999 c5996fff 2020-01-29 stsp }
7000 c5996fff 2020-01-29 stsp
7001 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7002 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7003 bfce7f83 2019-07-27 stsp if (error)
7004 bfce7f83 2019-07-27 stsp goto done;
7005 bfce7f83 2019-07-27 stsp
7006 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7007 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7008 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7009 6ba2bea2 2019-07-27 stsp if (error) {
7010 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7011 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7012 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7013 0ebf8283 2019-07-24 stsp goto done;
7014 6ba2bea2 2019-07-27 stsp }
7015 0ebf8283 2019-07-24 stsp } else {
7016 514f2ffe 2020-01-29 stsp const char *branch_name;
7017 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7018 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7019 514f2ffe 2020-01-29 stsp branch_name += 11;
7020 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7021 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7022 6ba2bea2 2019-07-27 stsp if (error) {
7023 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7024 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7025 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7026 0ebf8283 2019-07-24 stsp goto done;
7027 6ba2bea2 2019-07-27 stsp }
7028 0ebf8283 2019-07-24 stsp
7029 0ebf8283 2019-07-24 stsp }
7030 0ebf8283 2019-07-24 stsp
7031 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7032 0ebf8283 2019-07-24 stsp repo);
7033 6ba2bea2 2019-07-27 stsp if (error) {
7034 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7035 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7036 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7037 0ebf8283 2019-07-24 stsp goto done;
7038 6ba2bea2 2019-07-27 stsp }
7039 0ebf8283 2019-07-24 stsp
7040 0ebf8283 2019-07-24 stsp }
7041 0ebf8283 2019-07-24 stsp
7042 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7043 4ec14e60 2019-08-28 hiltjo if (error)
7044 0ebf8283 2019-07-24 stsp goto done;
7045 0ebf8283 2019-07-24 stsp
7046 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7047 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7048 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7049 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7050 0ebf8283 2019-07-24 stsp continue;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7053 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7054 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7055 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7056 0ebf8283 2019-07-24 stsp repo);
7057 ab20a43a 2020-01-29 stsp if (error)
7058 ab20a43a 2020-01-29 stsp goto done;
7059 0ebf8283 2019-07-24 stsp } else {
7060 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7061 ab20a43a 2020-01-29 stsp int have_changes = 0;
7062 ab20a43a 2020-01-29 stsp
7063 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7064 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7065 ab20a43a 2020-01-29 stsp if (error)
7066 ab20a43a 2020-01-29 stsp goto done;
7067 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7068 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7069 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7070 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7071 ab20a43a 2020-01-29 stsp if (error) {
7072 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7073 ab20a43a 2020-01-29 stsp goto done;
7074 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7075 ab20a43a 2020-01-29 stsp goto done;
7076 ab20a43a 2020-01-29 stsp }
7077 ab20a43a 2020-01-29 stsp if (have_changes) {
7078 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7079 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7080 ab20a43a 2020-01-29 stsp if (error)
7081 ab20a43a 2020-01-29 stsp goto done;
7082 ab20a43a 2020-01-29 stsp } else {
7083 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7084 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7085 ab20a43a 2020-01-29 stsp if (error)
7086 ab20a43a 2020-01-29 stsp goto done;
7087 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7088 ab20a43a 2020-01-29 stsp hle, NULL);
7089 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7090 ab20a43a 2020-01-29 stsp commit = NULL;
7091 ab20a43a 2020-01-29 stsp if (error)
7092 ab20a43a 2020-01-29 stsp goto done;
7093 ab20a43a 2020-01-29 stsp }
7094 0ebf8283 2019-07-24 stsp }
7095 0ebf8283 2019-07-24 stsp continue;
7096 0ebf8283 2019-07-24 stsp }
7097 0ebf8283 2019-07-24 stsp
7098 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7099 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7100 0ebf8283 2019-07-24 stsp if (error)
7101 0ebf8283 2019-07-24 stsp goto done;
7102 0ebf8283 2019-07-24 stsp continue;
7103 0ebf8283 2019-07-24 stsp }
7104 0ebf8283 2019-07-24 stsp
7105 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7106 0ebf8283 2019-07-24 stsp hle->commit_id);
7107 0ebf8283 2019-07-24 stsp if (error)
7108 0ebf8283 2019-07-24 stsp goto done;
7109 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7110 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7111 0ebf8283 2019-07-24 stsp
7112 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7113 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7114 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7115 0ebf8283 2019-07-24 stsp if (error)
7116 0ebf8283 2019-07-24 stsp goto done;
7117 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7118 0ebf8283 2019-07-24 stsp commit = NULL;
7119 0ebf8283 2019-07-24 stsp
7120 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7121 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7122 a0ea4fc0 2020-02-28 stsp repo);
7123 a0ea4fc0 2020-02-28 stsp if (error)
7124 a0ea4fc0 2020-02-28 stsp goto done;
7125 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7126 0ebf8283 2019-07-24 stsp break;
7127 0ebf8283 2019-07-24 stsp }
7128 0ebf8283 2019-07-24 stsp
7129 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7130 0ebf8283 2019-07-24 stsp char *id_str;
7131 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7132 0ebf8283 2019-07-24 stsp if (error)
7133 0ebf8283 2019-07-24 stsp goto done;
7134 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7135 0ebf8283 2019-07-24 stsp id_str);
7136 0ebf8283 2019-07-24 stsp free(id_str);
7137 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7138 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7139 3e3a69f1 2019-07-25 stsp fileindex);
7140 0ebf8283 2019-07-24 stsp goto done;
7141 0ebf8283 2019-07-24 stsp }
7142 0ebf8283 2019-07-24 stsp
7143 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7144 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7145 0ebf8283 2019-07-24 stsp if (error)
7146 0ebf8283 2019-07-24 stsp goto done;
7147 0ebf8283 2019-07-24 stsp continue;
7148 0ebf8283 2019-07-24 stsp }
7149 0ebf8283 2019-07-24 stsp
7150 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7151 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7152 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7153 0ebf8283 2019-07-24 stsp if (error)
7154 0ebf8283 2019-07-24 stsp goto done;
7155 0ebf8283 2019-07-24 stsp }
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7158 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7159 0ebf8283 2019-07-24 stsp if (error)
7160 0ebf8283 2019-07-24 stsp goto done;
7161 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7162 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7163 0ebf8283 2019-07-24 stsp } else
7164 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7165 3e3a69f1 2019-07-25 stsp branch, repo);
7166 0ebf8283 2019-07-24 stsp done:
7167 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7168 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7169 0ebf8283 2019-07-24 stsp free(head_commit_id);
7170 0ebf8283 2019-07-24 stsp free(base_commit_id);
7171 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7172 0ebf8283 2019-07-24 stsp if (commit)
7173 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7174 0ebf8283 2019-07-24 stsp if (branch)
7175 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7176 0ebf8283 2019-07-24 stsp if (tmp_branch)
7177 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7178 0ebf8283 2019-07-24 stsp if (worktree)
7179 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7180 2822a352 2019-10-15 stsp if (repo)
7181 2822a352 2019-10-15 stsp got_repo_close(repo);
7182 2822a352 2019-10-15 stsp return error;
7183 2822a352 2019-10-15 stsp }
7184 2822a352 2019-10-15 stsp
7185 2822a352 2019-10-15 stsp __dead static void
7186 2822a352 2019-10-15 stsp usage_integrate(void)
7187 2822a352 2019-10-15 stsp {
7188 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7189 2822a352 2019-10-15 stsp exit(1);
7190 2822a352 2019-10-15 stsp }
7191 2822a352 2019-10-15 stsp
7192 2822a352 2019-10-15 stsp static const struct got_error *
7193 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7194 2822a352 2019-10-15 stsp {
7195 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7196 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7197 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7198 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7199 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7200 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7201 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7202 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7203 2822a352 2019-10-15 stsp int ch, did_something = 0;
7204 2822a352 2019-10-15 stsp
7205 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7206 2822a352 2019-10-15 stsp switch (ch) {
7207 2822a352 2019-10-15 stsp default:
7208 2822a352 2019-10-15 stsp usage_integrate();
7209 2822a352 2019-10-15 stsp /* NOTREACHED */
7210 2822a352 2019-10-15 stsp }
7211 2822a352 2019-10-15 stsp }
7212 2822a352 2019-10-15 stsp
7213 2822a352 2019-10-15 stsp argc -= optind;
7214 2822a352 2019-10-15 stsp argv += optind;
7215 2822a352 2019-10-15 stsp
7216 2822a352 2019-10-15 stsp if (argc != 1)
7217 2822a352 2019-10-15 stsp usage_integrate();
7218 2822a352 2019-10-15 stsp branch_arg = argv[0];
7219 2822a352 2019-10-15 stsp
7220 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7221 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7222 2822a352 2019-10-15 stsp err(1, "pledge");
7223 2822a352 2019-10-15 stsp
7224 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7225 2822a352 2019-10-15 stsp if (cwd == NULL) {
7226 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7227 2822a352 2019-10-15 stsp goto done;
7228 2822a352 2019-10-15 stsp }
7229 2822a352 2019-10-15 stsp
7230 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7231 2822a352 2019-10-15 stsp if (error)
7232 2822a352 2019-10-15 stsp goto done;
7233 2822a352 2019-10-15 stsp
7234 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7235 2822a352 2019-10-15 stsp if (error)
7236 2822a352 2019-10-15 stsp goto done;
7237 2822a352 2019-10-15 stsp
7238 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7239 2822a352 2019-10-15 stsp NULL);
7240 2822a352 2019-10-15 stsp if (error != NULL)
7241 2822a352 2019-10-15 stsp goto done;
7242 2822a352 2019-10-15 stsp
7243 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7244 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7245 2822a352 2019-10-15 stsp if (error)
7246 2822a352 2019-10-15 stsp goto done;
7247 2822a352 2019-10-15 stsp
7248 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7249 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7250 2822a352 2019-10-15 stsp goto done;
7251 2822a352 2019-10-15 stsp }
7252 2822a352 2019-10-15 stsp
7253 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7254 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7255 2822a352 2019-10-15 stsp if (error)
7256 2822a352 2019-10-15 stsp goto done;
7257 2822a352 2019-10-15 stsp
7258 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7259 2822a352 2019-10-15 stsp if (refname == NULL) {
7260 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7261 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7262 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7263 2822a352 2019-10-15 stsp goto done;
7264 2822a352 2019-10-15 stsp }
7265 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7266 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7267 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7268 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7269 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7270 2822a352 2019-10-15 stsp goto done;
7271 2822a352 2019-10-15 stsp }
7272 2822a352 2019-10-15 stsp
7273 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7274 2822a352 2019-10-15 stsp if (error)
7275 2822a352 2019-10-15 stsp goto done;
7276 2822a352 2019-10-15 stsp
7277 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7278 2822a352 2019-10-15 stsp if (error)
7279 2822a352 2019-10-15 stsp goto done;
7280 2822a352 2019-10-15 stsp
7281 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7282 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7283 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7284 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7285 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7286 2822a352 2019-10-15 stsp goto done;
7287 2822a352 2019-10-15 stsp }
7288 2822a352 2019-10-15 stsp
7289 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7290 2822a352 2019-10-15 stsp if (error) {
7291 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7292 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7293 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7294 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7295 2822a352 2019-10-15 stsp goto done;
7296 2822a352 2019-10-15 stsp }
7297 2822a352 2019-10-15 stsp
7298 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7299 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7300 2822a352 2019-10-15 stsp check_cancelled, NULL);
7301 2822a352 2019-10-15 stsp if (error)
7302 2822a352 2019-10-15 stsp goto done;
7303 2822a352 2019-10-15 stsp
7304 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7305 2822a352 2019-10-15 stsp done:
7306 715dc77e 2019-08-03 stsp if (repo)
7307 715dc77e 2019-08-03 stsp got_repo_close(repo);
7308 2822a352 2019-10-15 stsp if (worktree)
7309 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7310 2822a352 2019-10-15 stsp free(cwd);
7311 2822a352 2019-10-15 stsp free(base_commit_id);
7312 2822a352 2019-10-15 stsp free(commit_id);
7313 2822a352 2019-10-15 stsp free(refname);
7314 2822a352 2019-10-15 stsp free(base_refname);
7315 715dc77e 2019-08-03 stsp return error;
7316 715dc77e 2019-08-03 stsp }
7317 715dc77e 2019-08-03 stsp
7318 715dc77e 2019-08-03 stsp __dead static void
7319 715dc77e 2019-08-03 stsp usage_stage(void)
7320 715dc77e 2019-08-03 stsp {
7321 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7322 f3055044 2019-08-07 stsp "[file-path ...]\n",
7323 715dc77e 2019-08-03 stsp getprogname());
7324 715dc77e 2019-08-03 stsp exit(1);
7325 715dc77e 2019-08-03 stsp }
7326 715dc77e 2019-08-03 stsp
7327 715dc77e 2019-08-03 stsp static const struct got_error *
7328 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7329 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7330 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7331 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7332 408b4ebc 2019-08-03 stsp {
7333 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7334 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7335 408b4ebc 2019-08-03 stsp
7336 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7337 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7338 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7339 408b4ebc 2019-08-03 stsp return NULL;
7340 408b4ebc 2019-08-03 stsp
7341 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7342 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7343 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7344 408b4ebc 2019-08-03 stsp else
7345 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7346 408b4ebc 2019-08-03 stsp if (err)
7347 408b4ebc 2019-08-03 stsp return err;
7348 408b4ebc 2019-08-03 stsp
7349 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7350 408b4ebc 2019-08-03 stsp free(id_str);
7351 dc424a06 2019-08-07 stsp return NULL;
7352 dc424a06 2019-08-07 stsp }
7353 2e1f37b0 2019-08-08 stsp
7354 dc424a06 2019-08-07 stsp static const struct got_error *
7355 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7356 715dc77e 2019-08-03 stsp {
7357 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7358 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7359 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7360 715dc77e 2019-08-03 stsp char *cwd = NULL;
7361 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7362 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7363 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7364 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7365 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7366 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7367 715dc77e 2019-08-03 stsp
7368 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7369 715dc77e 2019-08-03 stsp
7370 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7371 715dc77e 2019-08-03 stsp switch (ch) {
7372 408b4ebc 2019-08-03 stsp case 'l':
7373 408b4ebc 2019-08-03 stsp list_stage = 1;
7374 408b4ebc 2019-08-03 stsp break;
7375 dc424a06 2019-08-07 stsp case 'p':
7376 dc424a06 2019-08-07 stsp pflag = 1;
7377 dc424a06 2019-08-07 stsp break;
7378 dc424a06 2019-08-07 stsp case 'F':
7379 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7380 dc424a06 2019-08-07 stsp break;
7381 715dc77e 2019-08-03 stsp default:
7382 715dc77e 2019-08-03 stsp usage_stage();
7383 715dc77e 2019-08-03 stsp /* NOTREACHED */
7384 715dc77e 2019-08-03 stsp }
7385 715dc77e 2019-08-03 stsp }
7386 715dc77e 2019-08-03 stsp
7387 715dc77e 2019-08-03 stsp argc -= optind;
7388 715dc77e 2019-08-03 stsp argv += optind;
7389 715dc77e 2019-08-03 stsp
7390 715dc77e 2019-08-03 stsp #ifndef PROFILE
7391 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7392 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7393 715dc77e 2019-08-03 stsp err(1, "pledge");
7394 715dc77e 2019-08-03 stsp #endif
7395 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7396 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7397 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7398 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7399 715dc77e 2019-08-03 stsp
7400 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7401 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7402 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7403 715dc77e 2019-08-03 stsp goto done;
7404 715dc77e 2019-08-03 stsp }
7405 715dc77e 2019-08-03 stsp
7406 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7407 715dc77e 2019-08-03 stsp if (error)
7408 715dc77e 2019-08-03 stsp goto done;
7409 715dc77e 2019-08-03 stsp
7410 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7411 c9956ddf 2019-09-08 stsp NULL);
7412 715dc77e 2019-08-03 stsp if (error != NULL)
7413 715dc77e 2019-08-03 stsp goto done;
7414 715dc77e 2019-08-03 stsp
7415 dc424a06 2019-08-07 stsp if (patch_script_path) {
7416 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7417 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7418 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7419 dc424a06 2019-08-07 stsp patch_script_path);
7420 dc424a06 2019-08-07 stsp goto done;
7421 dc424a06 2019-08-07 stsp }
7422 dc424a06 2019-08-07 stsp }
7423 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7424 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7425 715dc77e 2019-08-03 stsp if (error)
7426 715dc77e 2019-08-03 stsp goto done;
7427 715dc77e 2019-08-03 stsp
7428 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7429 6d022e97 2019-08-04 stsp if (error)
7430 6d022e97 2019-08-04 stsp goto done;
7431 715dc77e 2019-08-03 stsp
7432 6d022e97 2019-08-04 stsp if (list_stage)
7433 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7434 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7435 2e1f37b0 2019-08-08 stsp else {
7436 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7437 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7438 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7439 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7440 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7441 2e1f37b0 2019-08-08 stsp }
7442 ad493afc 2019-08-03 stsp done:
7443 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7444 2e1f37b0 2019-08-08 stsp error == NULL)
7445 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7446 ad493afc 2019-08-03 stsp if (repo)
7447 ad493afc 2019-08-03 stsp got_repo_close(repo);
7448 ad493afc 2019-08-03 stsp if (worktree)
7449 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7450 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7451 ad493afc 2019-08-03 stsp free((char *)pe->path);
7452 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7453 ad493afc 2019-08-03 stsp free(cwd);
7454 ad493afc 2019-08-03 stsp return error;
7455 ad493afc 2019-08-03 stsp }
7456 ad493afc 2019-08-03 stsp
7457 ad493afc 2019-08-03 stsp __dead static void
7458 ad493afc 2019-08-03 stsp usage_unstage(void)
7459 ad493afc 2019-08-03 stsp {
7460 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7461 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7462 ad493afc 2019-08-03 stsp getprogname());
7463 ad493afc 2019-08-03 stsp exit(1);
7464 ad493afc 2019-08-03 stsp }
7465 ad493afc 2019-08-03 stsp
7466 ad493afc 2019-08-03 stsp
7467 ad493afc 2019-08-03 stsp static const struct got_error *
7468 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7469 ad493afc 2019-08-03 stsp {
7470 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7471 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7472 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7473 ad493afc 2019-08-03 stsp char *cwd = NULL;
7474 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7475 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7476 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7477 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7478 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7479 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7480 ad493afc 2019-08-03 stsp
7481 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7482 ad493afc 2019-08-03 stsp
7483 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7484 ad493afc 2019-08-03 stsp switch (ch) {
7485 2e1f37b0 2019-08-08 stsp case 'p':
7486 2e1f37b0 2019-08-08 stsp pflag = 1;
7487 2e1f37b0 2019-08-08 stsp break;
7488 2e1f37b0 2019-08-08 stsp case 'F':
7489 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7490 2e1f37b0 2019-08-08 stsp break;
7491 ad493afc 2019-08-03 stsp default:
7492 ad493afc 2019-08-03 stsp usage_unstage();
7493 ad493afc 2019-08-03 stsp /* NOTREACHED */
7494 ad493afc 2019-08-03 stsp }
7495 ad493afc 2019-08-03 stsp }
7496 ad493afc 2019-08-03 stsp
7497 ad493afc 2019-08-03 stsp argc -= optind;
7498 ad493afc 2019-08-03 stsp argv += optind;
7499 ad493afc 2019-08-03 stsp
7500 ad493afc 2019-08-03 stsp #ifndef PROFILE
7501 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7502 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7503 ad493afc 2019-08-03 stsp err(1, "pledge");
7504 ad493afc 2019-08-03 stsp #endif
7505 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7506 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7507 2e1f37b0 2019-08-08 stsp
7508 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7509 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7510 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7511 ad493afc 2019-08-03 stsp goto done;
7512 ad493afc 2019-08-03 stsp }
7513 ad493afc 2019-08-03 stsp
7514 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7515 ad493afc 2019-08-03 stsp if (error)
7516 ad493afc 2019-08-03 stsp goto done;
7517 ad493afc 2019-08-03 stsp
7518 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7519 c9956ddf 2019-09-08 stsp NULL);
7520 ad493afc 2019-08-03 stsp if (error != NULL)
7521 ad493afc 2019-08-03 stsp goto done;
7522 ad493afc 2019-08-03 stsp
7523 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7524 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7525 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7526 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7527 2e1f37b0 2019-08-08 stsp patch_script_path);
7528 2e1f37b0 2019-08-08 stsp goto done;
7529 2e1f37b0 2019-08-08 stsp }
7530 2e1f37b0 2019-08-08 stsp }
7531 2e1f37b0 2019-08-08 stsp
7532 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7533 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7534 ad493afc 2019-08-03 stsp if (error)
7535 ad493afc 2019-08-03 stsp goto done;
7536 ad493afc 2019-08-03 stsp
7537 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7538 ad493afc 2019-08-03 stsp if (error)
7539 ad493afc 2019-08-03 stsp goto done;
7540 ad493afc 2019-08-03 stsp
7541 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7542 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7543 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7544 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7545 715dc77e 2019-08-03 stsp done:
7546 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7547 2e1f37b0 2019-08-08 stsp error == NULL)
7548 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7549 0ebf8283 2019-07-24 stsp if (repo)
7550 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7551 715dc77e 2019-08-03 stsp if (worktree)
7552 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7553 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7554 715dc77e 2019-08-03 stsp free((char *)pe->path);
7555 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7556 715dc77e 2019-08-03 stsp free(cwd);
7557 01073a5d 2019-08-22 stsp return error;
7558 01073a5d 2019-08-22 stsp }
7559 01073a5d 2019-08-22 stsp
7560 01073a5d 2019-08-22 stsp __dead static void
7561 01073a5d 2019-08-22 stsp usage_cat(void)
7562 01073a5d 2019-08-22 stsp {
7563 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7564 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7565 01073a5d 2019-08-22 stsp exit(1);
7566 01073a5d 2019-08-22 stsp }
7567 01073a5d 2019-08-22 stsp
7568 01073a5d 2019-08-22 stsp static const struct got_error *
7569 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7570 01073a5d 2019-08-22 stsp {
7571 01073a5d 2019-08-22 stsp const struct got_error *err;
7572 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7573 01073a5d 2019-08-22 stsp
7574 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7575 01073a5d 2019-08-22 stsp if (err)
7576 01073a5d 2019-08-22 stsp return err;
7577 01073a5d 2019-08-22 stsp
7578 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7579 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7580 01073a5d 2019-08-22 stsp return err;
7581 01073a5d 2019-08-22 stsp }
7582 01073a5d 2019-08-22 stsp
7583 01073a5d 2019-08-22 stsp static const struct got_error *
7584 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7585 01073a5d 2019-08-22 stsp {
7586 01073a5d 2019-08-22 stsp const struct got_error *err;
7587 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7588 56e0773d 2019-11-28 stsp int nentries, i;
7589 01073a5d 2019-08-22 stsp
7590 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7591 01073a5d 2019-08-22 stsp if (err)
7592 01073a5d 2019-08-22 stsp return err;
7593 01073a5d 2019-08-22 stsp
7594 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7595 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7596 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7597 01073a5d 2019-08-22 stsp char *id_str;
7598 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7599 01073a5d 2019-08-22 stsp break;
7600 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7601 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7602 01073a5d 2019-08-22 stsp if (err)
7603 01073a5d 2019-08-22 stsp break;
7604 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7605 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7606 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7607 01073a5d 2019-08-22 stsp free(id_str);
7608 01073a5d 2019-08-22 stsp }
7609 01073a5d 2019-08-22 stsp
7610 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7611 01073a5d 2019-08-22 stsp return err;
7612 01073a5d 2019-08-22 stsp }
7613 01073a5d 2019-08-22 stsp
7614 01073a5d 2019-08-22 stsp static const struct got_error *
7615 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7616 01073a5d 2019-08-22 stsp {
7617 01073a5d 2019-08-22 stsp const struct got_error *err;
7618 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7619 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7620 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7621 01073a5d 2019-08-22 stsp char *id_str = NULL;
7622 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7623 01073a5d 2019-08-22 stsp
7624 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7625 01073a5d 2019-08-22 stsp if (err)
7626 01073a5d 2019-08-22 stsp return err;
7627 01073a5d 2019-08-22 stsp
7628 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7629 01073a5d 2019-08-22 stsp if (err)
7630 01073a5d 2019-08-22 stsp goto done;
7631 01073a5d 2019-08-22 stsp
7632 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7633 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7634 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7635 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7636 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7637 01073a5d 2019-08-22 stsp char *pid_str;
7638 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7639 01073a5d 2019-08-22 stsp if (err)
7640 01073a5d 2019-08-22 stsp goto done;
7641 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7642 01073a5d 2019-08-22 stsp free(pid_str);
7643 01073a5d 2019-08-22 stsp }
7644 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7645 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7646 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7647 01073a5d 2019-08-22 stsp
7648 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7649 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7650 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7651 01073a5d 2019-08-22 stsp
7652 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7653 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7654 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7655 01073a5d 2019-08-22 stsp done:
7656 01073a5d 2019-08-22 stsp free(id_str);
7657 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7658 01073a5d 2019-08-22 stsp return err;
7659 01073a5d 2019-08-22 stsp }
7660 01073a5d 2019-08-22 stsp
7661 01073a5d 2019-08-22 stsp static const struct got_error *
7662 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7663 01073a5d 2019-08-22 stsp {
7664 01073a5d 2019-08-22 stsp const struct got_error *err;
7665 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7666 01073a5d 2019-08-22 stsp char *id_str = NULL;
7667 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7668 01073a5d 2019-08-22 stsp
7669 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7670 01073a5d 2019-08-22 stsp if (err)
7671 01073a5d 2019-08-22 stsp return err;
7672 01073a5d 2019-08-22 stsp
7673 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7674 01073a5d 2019-08-22 stsp if (err)
7675 01073a5d 2019-08-22 stsp goto done;
7676 01073a5d 2019-08-22 stsp
7677 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7678 e15d5241 2019-08-22 stsp
7679 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7680 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7681 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7682 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7683 01073a5d 2019-08-22 stsp break;
7684 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7685 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7686 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7687 01073a5d 2019-08-22 stsp break;
7688 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7689 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7690 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7691 01073a5d 2019-08-22 stsp break;
7692 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7693 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7694 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7695 01073a5d 2019-08-22 stsp break;
7696 01073a5d 2019-08-22 stsp default:
7697 01073a5d 2019-08-22 stsp break;
7698 01073a5d 2019-08-22 stsp }
7699 01073a5d 2019-08-22 stsp
7700 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7701 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7702 e15d5241 2019-08-22 stsp
7703 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7704 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7705 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7706 01073a5d 2019-08-22 stsp
7707 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7708 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7709 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7710 01073a5d 2019-08-22 stsp done:
7711 01073a5d 2019-08-22 stsp free(id_str);
7712 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7713 01073a5d 2019-08-22 stsp return err;
7714 01073a5d 2019-08-22 stsp }
7715 01073a5d 2019-08-22 stsp
7716 01073a5d 2019-08-22 stsp static const struct got_error *
7717 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7718 01073a5d 2019-08-22 stsp {
7719 01073a5d 2019-08-22 stsp const struct got_error *error;
7720 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7721 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7722 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7723 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7724 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7725 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7726 01073a5d 2019-08-22 stsp
7727 01073a5d 2019-08-22 stsp #ifndef PROFILE
7728 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7729 01073a5d 2019-08-22 stsp NULL) == -1)
7730 01073a5d 2019-08-22 stsp err(1, "pledge");
7731 01073a5d 2019-08-22 stsp #endif
7732 01073a5d 2019-08-22 stsp
7733 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7734 01073a5d 2019-08-22 stsp switch (ch) {
7735 896e9b6f 2019-08-26 stsp case 'c':
7736 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7737 896e9b6f 2019-08-26 stsp break;
7738 01073a5d 2019-08-22 stsp case 'r':
7739 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7740 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7741 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7742 9ba1d308 2019-10-21 stsp optarg);
7743 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7744 01073a5d 2019-08-22 stsp break;
7745 896e9b6f 2019-08-26 stsp case 'P':
7746 896e9b6f 2019-08-26 stsp force_path = 1;
7747 896e9b6f 2019-08-26 stsp break;
7748 01073a5d 2019-08-22 stsp default:
7749 01073a5d 2019-08-22 stsp usage_cat();
7750 01073a5d 2019-08-22 stsp /* NOTREACHED */
7751 01073a5d 2019-08-22 stsp }
7752 01073a5d 2019-08-22 stsp }
7753 01073a5d 2019-08-22 stsp
7754 01073a5d 2019-08-22 stsp argc -= optind;
7755 01073a5d 2019-08-22 stsp argv += optind;
7756 01073a5d 2019-08-22 stsp
7757 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7758 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7759 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7760 01073a5d 2019-08-22 stsp goto done;
7761 01073a5d 2019-08-22 stsp }
7762 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7763 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7764 01073a5d 2019-08-22 stsp goto done;
7765 01073a5d 2019-08-22 stsp if (worktree) {
7766 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7767 01073a5d 2019-08-22 stsp repo_path = strdup(
7768 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7769 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7770 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7771 01073a5d 2019-08-22 stsp goto done;
7772 01073a5d 2019-08-22 stsp }
7773 01073a5d 2019-08-22 stsp }
7774 01073a5d 2019-08-22 stsp }
7775 01073a5d 2019-08-22 stsp
7776 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7777 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7778 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7779 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7780 01073a5d 2019-08-22 stsp }
7781 01073a5d 2019-08-22 stsp
7782 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7783 01073a5d 2019-08-22 stsp free(repo_path);
7784 01073a5d 2019-08-22 stsp if (error != NULL)
7785 01073a5d 2019-08-22 stsp goto done;
7786 01073a5d 2019-08-22 stsp
7787 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7788 896e9b6f 2019-08-26 stsp if (error)
7789 896e9b6f 2019-08-26 stsp goto done;
7790 896e9b6f 2019-08-26 stsp
7791 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7792 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7793 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7794 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7795 01073a5d 2019-08-22 stsp if (error)
7796 01073a5d 2019-08-22 stsp goto done;
7797 01073a5d 2019-08-22 stsp
7798 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7799 896e9b6f 2019-08-26 stsp if (force_path) {
7800 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7801 896e9b6f 2019-08-26 stsp argv[i]);
7802 896e9b6f 2019-08-26 stsp if (error)
7803 896e9b6f 2019-08-26 stsp break;
7804 896e9b6f 2019-08-26 stsp } else {
7805 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7806 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7807 896e9b6f 2019-08-26 stsp if (error) {
7808 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7809 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7810 896e9b6f 2019-08-26 stsp break;
7811 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7812 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7813 896e9b6f 2019-08-26 stsp if (error)
7814 896e9b6f 2019-08-26 stsp break;
7815 896e9b6f 2019-08-26 stsp }
7816 896e9b6f 2019-08-26 stsp }
7817 01073a5d 2019-08-22 stsp
7818 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7819 01073a5d 2019-08-22 stsp if (error)
7820 01073a5d 2019-08-22 stsp break;
7821 01073a5d 2019-08-22 stsp
7822 01073a5d 2019-08-22 stsp switch (obj_type) {
7823 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7824 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7825 01073a5d 2019-08-22 stsp break;
7826 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7827 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7828 01073a5d 2019-08-22 stsp break;
7829 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7830 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7831 01073a5d 2019-08-22 stsp break;
7832 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7833 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7834 01073a5d 2019-08-22 stsp break;
7835 01073a5d 2019-08-22 stsp default:
7836 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7837 01073a5d 2019-08-22 stsp break;
7838 01073a5d 2019-08-22 stsp }
7839 01073a5d 2019-08-22 stsp if (error)
7840 01073a5d 2019-08-22 stsp break;
7841 01073a5d 2019-08-22 stsp free(label);
7842 01073a5d 2019-08-22 stsp label = NULL;
7843 01073a5d 2019-08-22 stsp free(id);
7844 01073a5d 2019-08-22 stsp id = NULL;
7845 01073a5d 2019-08-22 stsp }
7846 01073a5d 2019-08-22 stsp done:
7847 01073a5d 2019-08-22 stsp free(label);
7848 01073a5d 2019-08-22 stsp free(id);
7849 896e9b6f 2019-08-26 stsp free(commit_id);
7850 01073a5d 2019-08-22 stsp if (worktree)
7851 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7852 01073a5d 2019-08-22 stsp if (repo) {
7853 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7854 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7855 01073a5d 2019-08-22 stsp if (error == NULL)
7856 01073a5d 2019-08-22 stsp error = repo_error;
7857 01073a5d 2019-08-22 stsp }
7858 0ebf8283 2019-07-24 stsp return error;
7859 0ebf8283 2019-07-24 stsp }