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 93658fb9 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
812 93658fb9 2020-03-18 stsp exit(1);
813 0266afb7 2019-01-04 stsp }
814 0266afb7 2019-01-04 stsp
815 4ed7e80c 2018-05-20 stsp __dead static void
816 c09a553d 2018-03-12 stsp usage_checkout(void)
817 c09a553d 2018-03-12 stsp {
818 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
819 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
820 c09a553d 2018-03-12 stsp exit(1);
821 92a684f4 2018-03-12 stsp }
822 92a684f4 2018-03-12 stsp
823 7f47418f 2019-12-20 stsp static void
824 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
825 7f47418f 2019-12-20 stsp {
826 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
827 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
828 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
829 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
830 7f47418f 2019-12-20 stsp getprogname());
831 7f47418f 2019-12-20 stsp }
832 7f47418f 2019-12-20 stsp
833 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
834 7f47418f 2019-12-20 stsp const char *worktree_path;
835 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
836 7f47418f 2019-12-20 stsp };
837 7f47418f 2019-12-20 stsp
838 1ee397ad 2019-07-12 stsp static const struct got_error *
839 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
840 92a684f4 2018-03-12 stsp {
841 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
842 a484d721 2019-06-10 stsp
843 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
844 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
845 7f47418f 2019-12-20 stsp return NULL;
846 7f47418f 2019-12-20 stsp
847 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
848 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
849 1ee397ad 2019-07-12 stsp return NULL;
850 7f47418f 2019-12-20 stsp }
851 92a684f4 2018-03-12 stsp
852 92a684f4 2018-03-12 stsp while (path[0] == '/')
853 92a684f4 2018-03-12 stsp path++;
854 92a684f4 2018-03-12 stsp
855 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
856 1ee397ad 2019-07-12 stsp return NULL;
857 99437157 2018-11-11 stsp }
858 99437157 2018-11-11 stsp
859 99437157 2018-11-11 stsp static const struct got_error *
860 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
861 99437157 2018-11-11 stsp {
862 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
863 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
864 99437157 2018-11-11 stsp return NULL;
865 8069f636 2019-01-12 stsp }
866 8069f636 2019-01-12 stsp
867 8069f636 2019-01-12 stsp static const struct got_error *
868 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
869 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
870 3aef623b 2019-10-15 stsp struct got_repository *repo)
871 8069f636 2019-01-12 stsp {
872 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
873 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
874 8069f636 2019-01-12 stsp
875 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
876 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
877 36a38700 2019-05-10 stsp if (err)
878 36a38700 2019-05-10 stsp return err;
879 8069f636 2019-01-12 stsp
880 d5bea539 2019-05-13 stsp if (yca_id == NULL)
881 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
882 8069f636 2019-01-12 stsp
883 d5bea539 2019-05-13 stsp /*
884 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
885 d5bea539 2019-05-13 stsp * and the work tree's base commit.
886 d5bea539 2019-05-13 stsp *
887 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
888 d5bea539 2019-05-13 stsp *
889 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
890 d5bea539 2019-05-13 stsp * \ /
891 d5bea539 2019-05-13 stsp * C E
892 d5bea539 2019-05-13 stsp * \ /
893 d5bea539 2019-05-13 stsp * B (yca)
894 d5bea539 2019-05-13 stsp * |
895 d5bea539 2019-05-13 stsp * A
896 d5bea539 2019-05-13 stsp *
897 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
898 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
899 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
900 d5bea539 2019-05-13 stsp */
901 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
902 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
903 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
904 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
905 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
906 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
907 8069f636 2019-01-12 stsp
908 d5bea539 2019-05-13 stsp free(yca_id);
909 d5bea539 2019-05-13 stsp return NULL;
910 c09a553d 2018-03-12 stsp }
911 a367ff0f 2019-05-14 stsp
912 a367ff0f 2019-05-14 stsp static const struct got_error *
913 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
914 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
915 a51a74b3 2019-07-27 stsp struct got_repository *repo)
916 a367ff0f 2019-05-14 stsp {
917 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
918 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
919 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
920 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
921 a367ff0f 2019-05-14 stsp
922 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
923 a367ff0f 2019-05-14 stsp if (err)
924 a51a74b3 2019-07-27 stsp goto done;
925 a51a74b3 2019-07-27 stsp
926 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
927 a51a74b3 2019-07-27 stsp is_same_branch = 1;
928 a51a74b3 2019-07-27 stsp goto done;
929 a51a74b3 2019-07-27 stsp }
930 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
931 a51a74b3 2019-07-27 stsp is_same_branch = 1;
932 a367ff0f 2019-05-14 stsp goto done;
933 a51a74b3 2019-07-27 stsp }
934 a367ff0f 2019-05-14 stsp
935 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
936 a367ff0f 2019-05-14 stsp if (err)
937 a367ff0f 2019-05-14 stsp goto done;
938 a367ff0f 2019-05-14 stsp
939 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
940 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
941 a367ff0f 2019-05-14 stsp if (err)
942 a367ff0f 2019-05-14 stsp goto done;
943 a367ff0f 2019-05-14 stsp
944 a367ff0f 2019-05-14 stsp for (;;) {
945 a367ff0f 2019-05-14 stsp struct got_object_id *id;
946 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
947 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
948 a367ff0f 2019-05-14 stsp if (err) {
949 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
950 a367ff0f 2019-05-14 stsp err = NULL;
951 ee780d5c 2020-01-04 stsp break;
952 a367ff0f 2019-05-14 stsp }
953 c09a553d 2018-03-12 stsp
954 a367ff0f 2019-05-14 stsp if (id) {
955 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 a51a74b3 2019-07-27 stsp break;
957 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
958 a367ff0f 2019-05-14 stsp is_same_branch = 1;
959 a367ff0f 2019-05-14 stsp break;
960 a367ff0f 2019-05-14 stsp }
961 a367ff0f 2019-05-14 stsp }
962 a367ff0f 2019-05-14 stsp }
963 a367ff0f 2019-05-14 stsp done:
964 a367ff0f 2019-05-14 stsp if (graph)
965 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
966 a367ff0f 2019-05-14 stsp free(head_commit_id);
967 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
968 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
969 a367ff0f 2019-05-14 stsp return err;
970 93658fb9 2020-03-18 stsp }
971 93658fb9 2020-03-18 stsp
972 93658fb9 2020-03-18 stsp static const struct got_error *
973 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
974 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
975 531c3985 2020-03-18 stsp {
976 b2409d58 2020-03-18 stsp int p;
977 d2cdc636 2020-03-18 stsp char scaled[FMT_SCALED_STRSIZE];
978 b2409d58 2020-03-18 stsp
979 b2409d58 2020-03-18 stsp if (message && message[0] != '\0' && message[0] != '\n') {
980 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
981 b2409d58 2020-03-18 stsp if (strchr(message, '\n') == 0)
982 b2409d58 2020-03-18 stsp printf("\n");
983 b2409d58 2020-03-18 stsp }
984 b2409d58 2020-03-18 stsp
985 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
986 668a20f6 2020-03-18 stsp printf("\r");
987 baa9fea0 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled) == 0)
988 668a20f6 2020-03-18 stsp printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
989 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
990 61cc1a7a 2020-03-18 stsp p = (nobj_indexed * 100) / nobj_total;
991 55f98ccb 2020-03-18 stsp printf("; indexing %d%% (%d)", p, nobj_indexed);
992 61cc1a7a 2020-03-18 stsp }
993 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
994 61cc1a7a 2020-03-18 stsp p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
995 55f98ccb 2020-03-18 stsp printf("; resolving deltas %d%% (%d)",
996 55f98ccb 2020-03-18 stsp p, nobj_resolved);
997 61cc1a7a 2020-03-18 stsp }
998 b2409d58 2020-03-18 stsp if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
999 b2409d58 2020-03-18 stsp nobj_resolved == nobj_total - nobj_loose)
1000 b2409d58 2020-03-18 stsp printf("\nWriting pack index...\n");
1001 b2409d58 2020-03-18 stsp
1002 d2cdc636 2020-03-18 stsp }
1003 531c3985 2020-03-18 stsp fflush(stdout);
1004 531c3985 2020-03-18 stsp return NULL;
1005 531c3985 2020-03-18 stsp }
1006 531c3985 2020-03-18 stsp
1007 531c3985 2020-03-18 stsp static const struct got_error *
1008 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1009 93658fb9 2020-03-18 stsp {
1010 09838ffc 2020-03-18 stsp const struct got_error *err = NULL;
1011 09838ffc 2020-03-18 stsp const char *uri, *branch_filter, *dirname;
1012 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1013 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1014 bb64b798 2020-03-18 stsp const char *repo_path;
1015 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1016 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
1017 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1018 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1019 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
1020 93658fb9 2020-03-18 stsp
1021 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1022 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1023 d9b4d0c0 2020-03-18 stsp
1024 93658fb9 2020-03-18 stsp while ((ch = getopt(argc, argv, "b:")) != -1) {
1025 93658fb9 2020-03-18 stsp switch (ch) {
1026 93658fb9 2020-03-18 stsp case 'b':
1027 93658fb9 2020-03-18 stsp branch_filter = optarg;
1028 93658fb9 2020-03-18 stsp break;
1029 93658fb9 2020-03-18 stsp default:
1030 93658fb9 2020-03-18 stsp usage_clone();
1031 93658fb9 2020-03-18 stsp break;
1032 93658fb9 2020-03-18 stsp }
1033 93658fb9 2020-03-18 stsp }
1034 93658fb9 2020-03-18 stsp argc -= optind;
1035 93658fb9 2020-03-18 stsp argv += optind;
1036 93658fb9 2020-03-18 stsp uri = argv[0];
1037 93658fb9 2020-03-18 stsp if(argc == 1)
1038 93658fb9 2020-03-18 stsp dirname = NULL;
1039 93658fb9 2020-03-18 stsp else if(argc == 2)
1040 93658fb9 2020-03-18 stsp dirname = argv[1];
1041 93658fb9 2020-03-18 stsp else
1042 93658fb9 2020-03-18 stsp usage_clone();
1043 09838ffc 2020-03-18 stsp
1044 09838ffc 2020-03-18 stsp err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1045 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1046 09838ffc 2020-03-18 stsp if (err)
1047 09838ffc 2020-03-18 stsp goto done;
1048 09838ffc 2020-03-18 stsp
1049 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1050 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1051 bb64b798 2020-03-18 stsp err = got_error_from_errno("asprintf");
1052 bb64b798 2020-03-18 stsp goto done;
1053 bb64b798 2020-03-18 stsp }
1054 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1055 bb64b798 2020-03-18 stsp } else
1056 bb64b798 2020-03-18 stsp repo_path = dirname;
1057 bb64b798 2020-03-18 stsp
1058 bb64b798 2020-03-18 stsp err = got_path_mkdir(repo_path);
1059 bb64b798 2020-03-18 stsp if (err)
1060 bb64b798 2020-03-18 stsp goto done;
1061 bb64b798 2020-03-18 stsp
1062 bb64b798 2020-03-18 stsp err = got_repo_init(repo_path);
1063 294dfefd 2020-03-18 stsp if (err)
1064 bb64b798 2020-03-18 stsp goto done;
1065 bb64b798 2020-03-18 stsp
1066 bb64b798 2020-03-18 stsp err = got_repo_open(&repo, repo_path, NULL);
1067 294dfefd 2020-03-18 stsp if (err)
1068 294dfefd 2020-03-18 stsp goto done;
1069 294dfefd 2020-03-18 stsp
1070 294dfefd 2020-03-18 stsp err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1071 bb64b798 2020-03-18 stsp if (err)
1072 bb64b798 2020-03-18 stsp goto done;
1073 294dfefd 2020-03-18 stsp
1074 294dfefd 2020-03-18 stsp printf("Connected to %s:%s\n", host, port);
1075 bb64b798 2020-03-18 stsp
1076 07e52fce 2020-03-18 stsp err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1077 b2409d58 2020-03-18 stsp repo, fetch_progress, NULL);
1078 d9b4d0c0 2020-03-18 stsp if (err)
1079 d9b4d0c0 2020-03-18 stsp goto done;
1080 d9b4d0c0 2020-03-18 stsp
1081 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, pack_hash);
1082 d9b4d0c0 2020-03-18 stsp if (err)
1083 d9b4d0c0 2020-03-18 stsp goto done;
1084 d9b4d0c0 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1085 d9b4d0c0 2020-03-18 stsp free(id_str);
1086 d9b4d0c0 2020-03-18 stsp
1087 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1088 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1089 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1090 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1091 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1092 d9b4d0c0 2020-03-18 stsp
1093 668a20f6 2020-03-18 stsp
1094 668a20f6 2020-03-18 stsp err = got_ref_alloc(&ref, refname, id);
1095 d9b4d0c0 2020-03-18 stsp if (err)
1096 d9b4d0c0 2020-03-18 stsp goto done;
1097 d9b4d0c0 2020-03-18 stsp
1098 668a20f6 2020-03-18 stsp #if 0
1099 668a20f6 2020-03-18 stsp err = got_object_id_str(&id_str, id);
1100 668a20f6 2020-03-18 stsp if (err)
1101 d9b4d0c0 2020-03-18 stsp goto done;
1102 d9b4d0c0 2020-03-18 stsp printf("%s: %s\n", got_ref_get_name(ref), id_str);
1103 d9b4d0c0 2020-03-18 stsp free(id_str);
1104 668a20f6 2020-03-18 stsp #endif
1105 d9b4d0c0 2020-03-18 stsp err = got_ref_write(ref, repo);
1106 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1107 d9b4d0c0 2020-03-18 stsp if (err)
1108 d9b4d0c0 2020-03-18 stsp goto done;
1109 d9b4d0c0 2020-03-18 stsp }
1110 d9b4d0c0 2020-03-18 stsp
1111 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1112 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1113 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1114 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1115 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1116 d9b4d0c0 2020-03-18 stsp
1117 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1118 d9b4d0c0 2020-03-18 stsp continue;
1119 d9b4d0c0 2020-03-18 stsp
1120 d9b4d0c0 2020-03-18 stsp err = got_ref_open(&target_ref, repo, target, 0);
1121 d9b4d0c0 2020-03-18 stsp if (err) {
1122 d9b4d0c0 2020-03-18 stsp if (err->code == GOT_ERR_NOT_REF)
1123 d9b4d0c0 2020-03-18 stsp continue;
1124 d9b4d0c0 2020-03-18 stsp goto done;
1125 d9b4d0c0 2020-03-18 stsp }
1126 d9b4d0c0 2020-03-18 stsp
1127 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1128 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1129 d9b4d0c0 2020-03-18 stsp if (err)
1130 d9b4d0c0 2020-03-18 stsp goto done;
1131 d9b4d0c0 2020-03-18 stsp
1132 d9b4d0c0 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1133 d9b4d0c0 2020-03-18 stsp got_ref_get_symref_target(symref));
1134 d9b4d0c0 2020-03-18 stsp
1135 d9b4d0c0 2020-03-18 stsp err = got_ref_write(symref, repo);
1136 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1137 d9b4d0c0 2020-03-18 stsp break;
1138 d9b4d0c0 2020-03-18 stsp }
1139 d9b4d0c0 2020-03-18 stsp
1140 09838ffc 2020-03-18 stsp done:
1141 20eb36d0 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1142 20eb36d0 2020-03-18 stsp err = got_error_from_errno("close");
1143 bb64b798 2020-03-18 stsp if (repo)
1144 bb64b798 2020-03-18 stsp got_repo_close(repo);
1145 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1146 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1147 d9b4d0c0 2020-03-18 stsp free(pe->data);
1148 d9b4d0c0 2020-03-18 stsp }
1149 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1150 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1151 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1152 d9b4d0c0 2020-03-18 stsp free(pe->data);
1153 d9b4d0c0 2020-03-18 stsp }
1154 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1155 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1156 09838ffc 2020-03-18 stsp free(proto);
1157 09838ffc 2020-03-18 stsp free(host);
1158 09838ffc 2020-03-18 stsp free(port);
1159 09838ffc 2020-03-18 stsp free(server_path);
1160 09838ffc 2020-03-18 stsp free(repo_name);
1161 bb64b798 2020-03-18 stsp free(default_destdir);
1162 09838ffc 2020-03-18 stsp return err;
1163 a367ff0f 2019-05-14 stsp }
1164 8069f636 2019-01-12 stsp
1165 4ed7e80c 2018-05-20 stsp static const struct got_error *
1166 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1167 4b6c9460 2020-03-05 stsp {
1168 4b6c9460 2020-03-05 stsp static char msg[512];
1169 4b6c9460 2020-03-05 stsp const char *branch_name;
1170 4b6c9460 2020-03-05 stsp
1171 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1172 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1173 4b6c9460 2020-03-05 stsp else
1174 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1175 4b6c9460 2020-03-05 stsp
1176 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1177 4b6c9460 2020-03-05 stsp branch_name += 11;
1178 4b6c9460 2020-03-05 stsp
1179 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1180 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1181 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1182 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1183 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1184 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1185 4b6c9460 2020-03-05 stsp
1186 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1187 4b6c9460 2020-03-05 stsp }
1188 4b6c9460 2020-03-05 stsp
1189 4b6c9460 2020-03-05 stsp static const struct got_error *
1190 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1191 c09a553d 2018-03-12 stsp {
1192 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1193 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1194 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1195 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1196 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1197 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1198 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1199 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1200 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1201 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1202 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1203 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1204 f2ea84fa 2019-07-27 stsp
1205 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1206 c09a553d 2018-03-12 stsp
1207 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1208 0bb8a95e 2018-03-12 stsp switch (ch) {
1209 08573d5b 2019-05-14 stsp case 'b':
1210 08573d5b 2019-05-14 stsp branch_name = optarg;
1211 08573d5b 2019-05-14 stsp break;
1212 8069f636 2019-01-12 stsp case 'c':
1213 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1214 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1215 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1216 bb51a5b4 2020-01-13 stsp break;
1217 bb51a5b4 2020-01-13 stsp case 'E':
1218 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1219 8069f636 2019-01-12 stsp break;
1220 0bb8a95e 2018-03-12 stsp case 'p':
1221 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1222 0bb8a95e 2018-03-12 stsp break;
1223 0bb8a95e 2018-03-12 stsp default:
1224 2deda0b9 2019-03-07 stsp usage_checkout();
1225 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1226 0bb8a95e 2018-03-12 stsp }
1227 0bb8a95e 2018-03-12 stsp }
1228 0bb8a95e 2018-03-12 stsp
1229 0bb8a95e 2018-03-12 stsp argc -= optind;
1230 0bb8a95e 2018-03-12 stsp argv += optind;
1231 0bb8a95e 2018-03-12 stsp
1232 6715a751 2018-03-16 stsp #ifndef PROFILE
1233 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1234 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1235 c09a553d 2018-03-12 stsp err(1, "pledge");
1236 6715a751 2018-03-16 stsp #endif
1237 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1238 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1239 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1240 76089277 2018-04-01 stsp if (repo_path == NULL)
1241 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1242 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1243 76089277 2018-04-01 stsp if (cwd == NULL) {
1244 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1245 76089277 2018-04-01 stsp goto done;
1246 76089277 2018-04-01 stsp }
1247 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1248 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1249 230a42bd 2019-05-11 jcs if (base == NULL) {
1250 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1251 230a42bd 2019-05-11 jcs path_prefix);
1252 230a42bd 2019-05-11 jcs goto done;
1253 230a42bd 2019-05-11 jcs }
1254 230a42bd 2019-05-11 jcs } else {
1255 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1256 230a42bd 2019-05-11 jcs if (base == NULL) {
1257 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1258 230a42bd 2019-05-11 jcs repo_path);
1259 230a42bd 2019-05-11 jcs goto done;
1260 230a42bd 2019-05-11 jcs }
1261 76089277 2018-04-01 stsp }
1262 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1263 c09a553d 2018-03-12 stsp if (dotgit)
1264 c09a553d 2018-03-12 stsp *dotgit = '\0';
1265 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1266 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1267 c09a553d 2018-03-12 stsp free(cwd);
1268 76089277 2018-04-01 stsp goto done;
1269 c09a553d 2018-03-12 stsp }
1270 c09a553d 2018-03-12 stsp free(cwd);
1271 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1272 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1273 76089277 2018-04-01 stsp if (repo_path == NULL) {
1274 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1275 76089277 2018-04-01 stsp goto done;
1276 76089277 2018-04-01 stsp }
1277 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1278 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1279 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1280 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1281 b4b3a7dd 2019-07-22 stsp argv[1]);
1282 b4b3a7dd 2019-07-22 stsp goto done;
1283 b4b3a7dd 2019-07-22 stsp }
1284 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1285 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1286 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1287 b4b3a7dd 2019-07-22 stsp goto done;
1288 b4b3a7dd 2019-07-22 stsp }
1289 76089277 2018-04-01 stsp }
1290 c09a553d 2018-03-12 stsp } else
1291 c09a553d 2018-03-12 stsp usage_checkout();
1292 c09a553d 2018-03-12 stsp
1293 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1294 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1295 13bfb272 2019-05-10 jcs
1296 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1297 c09a553d 2018-03-12 stsp if (error != NULL)
1298 c02c541e 2019-03-29 stsp goto done;
1299 c02c541e 2019-03-29 stsp
1300 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1301 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1302 c530dc23 2019-07-23 stsp if (error) {
1303 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1304 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1305 c530dc23 2019-07-23 stsp goto done;
1306 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1307 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1308 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1309 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1310 c530dc23 2019-07-23 stsp goto done;
1311 c530dc23 2019-07-23 stsp }
1312 c530dc23 2019-07-23 stsp }
1313 c530dc23 2019-07-23 stsp
1314 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1315 c02c541e 2019-03-29 stsp if (error)
1316 c09a553d 2018-03-12 stsp goto done;
1317 8069f636 2019-01-12 stsp
1318 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1319 c09a553d 2018-03-12 stsp if (error != NULL)
1320 c09a553d 2018-03-12 stsp goto done;
1321 c09a553d 2018-03-12 stsp
1322 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1323 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1324 c09a553d 2018-03-12 stsp goto done;
1325 c09a553d 2018-03-12 stsp
1326 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1327 c09a553d 2018-03-12 stsp if (error != NULL)
1328 c09a553d 2018-03-12 stsp goto done;
1329 c09a553d 2018-03-12 stsp
1330 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1331 e5dc7198 2018-12-29 stsp path_prefix);
1332 e5dc7198 2018-12-29 stsp if (error != NULL)
1333 e5dc7198 2018-12-29 stsp goto done;
1334 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1335 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1336 49520a32 2018-12-29 stsp goto done;
1337 49520a32 2018-12-29 stsp }
1338 49520a32 2018-12-29 stsp
1339 8069f636 2019-01-12 stsp if (commit_id_str) {
1340 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1341 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1342 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1343 30837e32 2019-07-25 stsp if (error)
1344 8069f636 2019-01-12 stsp goto done;
1345 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1346 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1347 8069f636 2019-01-12 stsp if (error != NULL) {
1348 8069f636 2019-01-12 stsp free(commit_id);
1349 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1350 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1351 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1352 4b6c9460 2020-03-05 stsp }
1353 8069f636 2019-01-12 stsp goto done;
1354 8069f636 2019-01-12 stsp }
1355 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1356 4b6c9460 2020-03-05 stsp if (error) {
1357 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1358 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1359 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1360 4b6c9460 2020-03-05 stsp }
1361 45d344f6 2019-05-14 stsp goto done;
1362 4b6c9460 2020-03-05 stsp }
1363 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1364 8069f636 2019-01-12 stsp commit_id);
1365 8069f636 2019-01-12 stsp free(commit_id);
1366 8069f636 2019-01-12 stsp if (error)
1367 8069f636 2019-01-12 stsp goto done;
1368 8069f636 2019-01-12 stsp }
1369 8069f636 2019-01-12 stsp
1370 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1371 f2ea84fa 2019-07-27 stsp if (error)
1372 f2ea84fa 2019-07-27 stsp goto done;
1373 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1374 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1375 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1376 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1377 c09a553d 2018-03-12 stsp if (error != NULL)
1378 c09a553d 2018-03-12 stsp goto done;
1379 c09a553d 2018-03-12 stsp
1380 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1381 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1382 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1383 c09a553d 2018-03-12 stsp done:
1384 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1385 8069f636 2019-01-12 stsp free(commit_id_str);
1386 76089277 2018-04-01 stsp free(repo_path);
1387 507dc3bb 2018-12-29 stsp free(worktree_path);
1388 507dc3bb 2018-12-29 stsp return error;
1389 507dc3bb 2018-12-29 stsp }
1390 507dc3bb 2018-12-29 stsp
1391 507dc3bb 2018-12-29 stsp __dead static void
1392 507dc3bb 2018-12-29 stsp usage_update(void)
1393 507dc3bb 2018-12-29 stsp {
1394 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1395 507dc3bb 2018-12-29 stsp getprogname());
1396 507dc3bb 2018-12-29 stsp exit(1);
1397 507dc3bb 2018-12-29 stsp }
1398 507dc3bb 2018-12-29 stsp
1399 1ee397ad 2019-07-12 stsp static const struct got_error *
1400 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1401 507dc3bb 2018-12-29 stsp {
1402 784955db 2019-01-12 stsp int *did_something = arg;
1403 784955db 2019-01-12 stsp
1404 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1405 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1406 1ee397ad 2019-07-12 stsp return NULL;
1407 507dc3bb 2018-12-29 stsp
1408 1545c615 2019-02-10 stsp *did_something = 1;
1409 a484d721 2019-06-10 stsp
1410 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1411 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1412 1ee397ad 2019-07-12 stsp return NULL;
1413 a484d721 2019-06-10 stsp
1414 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1415 507dc3bb 2018-12-29 stsp path++;
1416 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1417 1ee397ad 2019-07-12 stsp return NULL;
1418 be7061eb 2018-12-30 stsp }
1419 be7061eb 2018-12-30 stsp
1420 be7061eb 2018-12-30 stsp static const struct got_error *
1421 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1422 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1423 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1424 a1fb16d8 2019-05-24 stsp {
1425 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1426 a1fb16d8 2019-05-24 stsp char *base_id_str;
1427 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1428 a1fb16d8 2019-05-24 stsp
1429 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1430 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1431 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1432 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1433 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1434 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1435 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1436 a1fb16d8 2019-05-24 stsp }
1437 a1fb16d8 2019-05-24 stsp
1438 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1439 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1440 a1fb16d8 2019-05-24 stsp if (err) {
1441 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1442 a1fb16d8 2019-05-24 stsp return err;
1443 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1444 a1fb16d8 2019-05-24 stsp }
1445 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1446 a1fb16d8 2019-05-24 stsp return NULL;
1447 a1fb16d8 2019-05-24 stsp
1448 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1449 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1450 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1451 a1fb16d8 2019-05-24 stsp if (err)
1452 a1fb16d8 2019-05-24 stsp return err;
1453 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1454 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1455 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1456 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1457 0ebf8283 2019-07-24 stsp return NULL;
1458 0ebf8283 2019-07-24 stsp }
1459 0ebf8283 2019-07-24 stsp
1460 0ebf8283 2019-07-24 stsp static const struct got_error *
1461 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1462 0ebf8283 2019-07-24 stsp {
1463 0ebf8283 2019-07-24 stsp const struct got_error *err;
1464 0ebf8283 2019-07-24 stsp int in_progress;
1465 0ebf8283 2019-07-24 stsp
1466 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1467 0ebf8283 2019-07-24 stsp if (err)
1468 0ebf8283 2019-07-24 stsp return err;
1469 0ebf8283 2019-07-24 stsp if (in_progress)
1470 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1471 0ebf8283 2019-07-24 stsp
1472 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1473 0ebf8283 2019-07-24 stsp if (err)
1474 0ebf8283 2019-07-24 stsp return err;
1475 0ebf8283 2019-07-24 stsp if (in_progress)
1476 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1477 0ebf8283 2019-07-24 stsp
1478 a1fb16d8 2019-05-24 stsp return NULL;
1479 a5edda0a 2019-07-27 stsp }
1480 a5edda0a 2019-07-27 stsp
1481 a5edda0a 2019-07-27 stsp static const struct got_error *
1482 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1483 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1484 a5edda0a 2019-07-27 stsp {
1485 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1486 a5edda0a 2019-07-27 stsp char *path;
1487 a5edda0a 2019-07-27 stsp int i;
1488 a5edda0a 2019-07-27 stsp
1489 a5edda0a 2019-07-27 stsp if (argc == 0) {
1490 a5edda0a 2019-07-27 stsp path = strdup("");
1491 a5edda0a 2019-07-27 stsp if (path == NULL)
1492 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1493 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1494 a5edda0a 2019-07-27 stsp }
1495 a5edda0a 2019-07-27 stsp
1496 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1497 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1498 a5edda0a 2019-07-27 stsp if (err)
1499 a5edda0a 2019-07-27 stsp break;
1500 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1501 a5edda0a 2019-07-27 stsp if (err) {
1502 a5edda0a 2019-07-27 stsp free(path);
1503 a5edda0a 2019-07-27 stsp break;
1504 a5edda0a 2019-07-27 stsp }
1505 a5edda0a 2019-07-27 stsp }
1506 a5edda0a 2019-07-27 stsp
1507 a5edda0a 2019-07-27 stsp return err;
1508 a1fb16d8 2019-05-24 stsp }
1509 a1fb16d8 2019-05-24 stsp
1510 a1fb16d8 2019-05-24 stsp static const struct got_error *
1511 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1512 507dc3bb 2018-12-29 stsp {
1513 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1514 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1515 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1516 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1517 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1518 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1519 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1520 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1521 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1522 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1523 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1524 507dc3bb 2018-12-29 stsp
1525 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1526 f2ea84fa 2019-07-27 stsp
1527 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1528 507dc3bb 2018-12-29 stsp switch (ch) {
1529 024e9686 2019-05-14 stsp case 'b':
1530 024e9686 2019-05-14 stsp branch_name = optarg;
1531 024e9686 2019-05-14 stsp break;
1532 507dc3bb 2018-12-29 stsp case 'c':
1533 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1534 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1535 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1536 507dc3bb 2018-12-29 stsp break;
1537 507dc3bb 2018-12-29 stsp default:
1538 2deda0b9 2019-03-07 stsp usage_update();
1539 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1540 507dc3bb 2018-12-29 stsp }
1541 507dc3bb 2018-12-29 stsp }
1542 507dc3bb 2018-12-29 stsp
1543 507dc3bb 2018-12-29 stsp argc -= optind;
1544 507dc3bb 2018-12-29 stsp argv += optind;
1545 507dc3bb 2018-12-29 stsp
1546 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1547 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1548 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1549 507dc3bb 2018-12-29 stsp err(1, "pledge");
1550 507dc3bb 2018-12-29 stsp #endif
1551 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1552 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1553 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1554 c4cdcb68 2019-04-03 stsp goto done;
1555 c4cdcb68 2019-04-03 stsp }
1556 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1557 7d5807f4 2019-07-11 stsp if (error)
1558 7d5807f4 2019-07-11 stsp goto done;
1559 7d5807f4 2019-07-11 stsp
1560 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1561 f2ea84fa 2019-07-27 stsp if (error)
1562 f2ea84fa 2019-07-27 stsp goto done;
1563 507dc3bb 2018-12-29 stsp
1564 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1565 c9956ddf 2019-09-08 stsp NULL);
1566 507dc3bb 2018-12-29 stsp if (error != NULL)
1567 507dc3bb 2018-12-29 stsp goto done;
1568 507dc3bb 2018-12-29 stsp
1569 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1570 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1571 087fb88c 2019-08-04 stsp if (error)
1572 087fb88c 2019-08-04 stsp goto done;
1573 087fb88c 2019-08-04 stsp
1574 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1575 0266afb7 2019-01-04 stsp if (error)
1576 0266afb7 2019-01-04 stsp goto done;
1577 0266afb7 2019-01-04 stsp
1578 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1579 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1580 024e9686 2019-05-14 stsp if (error != NULL)
1581 024e9686 2019-05-14 stsp goto done;
1582 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1583 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1584 9c4b8182 2019-01-02 stsp if (error != NULL)
1585 9c4b8182 2019-01-02 stsp goto done;
1586 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1587 507dc3bb 2018-12-29 stsp if (error != NULL)
1588 507dc3bb 2018-12-29 stsp goto done;
1589 507dc3bb 2018-12-29 stsp } else {
1590 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1591 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1592 04f57cb3 2019-07-25 stsp free(commit_id_str);
1593 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1594 30837e32 2019-07-25 stsp if (error)
1595 a297e751 2019-07-11 stsp goto done;
1596 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1597 a297e751 2019-07-11 stsp if (error)
1598 507dc3bb 2018-12-29 stsp goto done;
1599 507dc3bb 2018-12-29 stsp }
1600 35c965b2 2018-12-29 stsp
1601 a1fb16d8 2019-05-24 stsp if (branch_name) {
1602 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1603 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1604 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1605 f2ea84fa 2019-07-27 stsp continue;
1606 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1607 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1608 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1609 024e9686 2019-05-14 stsp goto done;
1610 024e9686 2019-05-14 stsp }
1611 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1612 024e9686 2019-05-14 stsp if (error)
1613 024e9686 2019-05-14 stsp goto done;
1614 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1615 3aef623b 2019-10-15 stsp repo);
1616 a367ff0f 2019-05-14 stsp free(head_commit_id);
1617 024e9686 2019-05-14 stsp if (error != NULL)
1618 024e9686 2019-05-14 stsp goto done;
1619 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1620 a367ff0f 2019-05-14 stsp if (error)
1621 a367ff0f 2019-05-14 stsp goto done;
1622 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1623 024e9686 2019-05-14 stsp if (error)
1624 024e9686 2019-05-14 stsp goto done;
1625 024e9686 2019-05-14 stsp } else {
1626 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1627 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1628 a367ff0f 2019-05-14 stsp if (error != NULL) {
1629 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1630 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1631 024e9686 2019-05-14 stsp goto done;
1632 a367ff0f 2019-05-14 stsp }
1633 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1634 a367ff0f 2019-05-14 stsp if (error)
1635 a367ff0f 2019-05-14 stsp goto done;
1636 024e9686 2019-05-14 stsp }
1637 507dc3bb 2018-12-29 stsp
1638 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1639 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1640 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1641 507dc3bb 2018-12-29 stsp commit_id);
1642 507dc3bb 2018-12-29 stsp if (error)
1643 507dc3bb 2018-12-29 stsp goto done;
1644 507dc3bb 2018-12-29 stsp }
1645 507dc3bb 2018-12-29 stsp
1646 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1647 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1648 507dc3bb 2018-12-29 stsp if (error != NULL)
1649 507dc3bb 2018-12-29 stsp goto done;
1650 9c4b8182 2019-01-02 stsp
1651 784955db 2019-01-12 stsp if (did_something)
1652 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1653 784955db 2019-01-12 stsp else
1654 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1655 507dc3bb 2018-12-29 stsp done:
1656 c09a553d 2018-03-12 stsp free(worktree_path);
1657 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1658 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1659 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1660 507dc3bb 2018-12-29 stsp free(commit_id);
1661 9c4b8182 2019-01-02 stsp free(commit_id_str);
1662 c09a553d 2018-03-12 stsp return error;
1663 c09a553d 2018-03-12 stsp }
1664 c09a553d 2018-03-12 stsp
1665 f42b1b34 2018-03-12 stsp static const struct got_error *
1666 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1667 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1668 63035f9f 2019-10-06 stsp struct got_repository *repo)
1669 5c860e29 2018-03-12 stsp {
1670 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1671 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1672 79109fed 2018-03-27 stsp
1673 44392932 2019-08-25 stsp if (blob_id1) {
1674 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1675 44392932 2019-08-25 stsp if (err)
1676 44392932 2019-08-25 stsp goto done;
1677 44392932 2019-08-25 stsp }
1678 79109fed 2018-03-27 stsp
1679 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1680 44392932 2019-08-25 stsp if (err)
1681 44392932 2019-08-25 stsp goto done;
1682 79109fed 2018-03-27 stsp
1683 44392932 2019-08-25 stsp while (path[0] == '/')
1684 44392932 2019-08-25 stsp path++;
1685 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1686 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1687 44392932 2019-08-25 stsp done:
1688 44392932 2019-08-25 stsp if (blob1)
1689 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1690 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1691 44392932 2019-08-25 stsp return err;
1692 44392932 2019-08-25 stsp }
1693 44392932 2019-08-25 stsp
1694 44392932 2019-08-25 stsp static const struct got_error *
1695 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1696 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1697 63035f9f 2019-10-06 stsp struct got_repository *repo)
1698 44392932 2019-08-25 stsp {
1699 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1700 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1701 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1702 44392932 2019-08-25 stsp
1703 44392932 2019-08-25 stsp if (tree_id1) {
1704 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1705 79109fed 2018-03-27 stsp if (err)
1706 44392932 2019-08-25 stsp goto done;
1707 79109fed 2018-03-27 stsp }
1708 79109fed 2018-03-27 stsp
1709 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1710 0f2b3dca 2018-12-22 stsp if (err)
1711 0f2b3dca 2018-12-22 stsp goto done;
1712 0f2b3dca 2018-12-22 stsp
1713 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1714 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1715 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1716 44392932 2019-08-25 stsp while (path[0] == '/')
1717 44392932 2019-08-25 stsp path++;
1718 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1719 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1720 0f2b3dca 2018-12-22 stsp done:
1721 79109fed 2018-03-27 stsp if (tree1)
1722 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1723 366e0a5f 2019-10-10 stsp if (tree2)
1724 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1725 44392932 2019-08-25 stsp return err;
1726 44392932 2019-08-25 stsp }
1727 44392932 2019-08-25 stsp
1728 44392932 2019-08-25 stsp static const struct got_error *
1729 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1730 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1731 44392932 2019-08-25 stsp {
1732 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1733 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1734 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1735 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1736 44392932 2019-08-25 stsp struct got_object_qid *qid;
1737 44392932 2019-08-25 stsp
1738 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1739 44392932 2019-08-25 stsp if (qid != NULL) {
1740 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1741 44392932 2019-08-25 stsp qid->id);
1742 44392932 2019-08-25 stsp if (err)
1743 44392932 2019-08-25 stsp return err;
1744 44392932 2019-08-25 stsp }
1745 44392932 2019-08-25 stsp
1746 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1747 44392932 2019-08-25 stsp int obj_type;
1748 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1749 44392932 2019-08-25 stsp if (err)
1750 44392932 2019-08-25 stsp goto done;
1751 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1752 44392932 2019-08-25 stsp if (err) {
1753 44392932 2019-08-25 stsp free(obj_id2);
1754 44392932 2019-08-25 stsp goto done;
1755 44392932 2019-08-25 stsp }
1756 44392932 2019-08-25 stsp if (pcommit) {
1757 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1758 44392932 2019-08-25 stsp qid->id, path);
1759 44392932 2019-08-25 stsp if (err) {
1760 44392932 2019-08-25 stsp free(obj_id2);
1761 44392932 2019-08-25 stsp goto done;
1762 44392932 2019-08-25 stsp }
1763 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1764 44392932 2019-08-25 stsp if (err) {
1765 44392932 2019-08-25 stsp free(obj_id2);
1766 44392932 2019-08-25 stsp goto done;
1767 44392932 2019-08-25 stsp }
1768 44392932 2019-08-25 stsp }
1769 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1770 44392932 2019-08-25 stsp if (err) {
1771 44392932 2019-08-25 stsp free(obj_id2);
1772 44392932 2019-08-25 stsp goto done;
1773 44392932 2019-08-25 stsp }
1774 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1775 44392932 2019-08-25 stsp switch (obj_type) {
1776 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1777 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1778 63035f9f 2019-10-06 stsp 0, repo);
1779 44392932 2019-08-25 stsp break;
1780 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1781 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1782 63035f9f 2019-10-06 stsp 0, repo);
1783 44392932 2019-08-25 stsp break;
1784 44392932 2019-08-25 stsp default:
1785 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1786 44392932 2019-08-25 stsp break;
1787 44392932 2019-08-25 stsp }
1788 44392932 2019-08-25 stsp free(obj_id1);
1789 44392932 2019-08-25 stsp free(obj_id2);
1790 44392932 2019-08-25 stsp } else {
1791 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1792 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1793 44392932 2019-08-25 stsp if (err)
1794 44392932 2019-08-25 stsp goto done;
1795 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1796 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1797 44392932 2019-08-25 stsp if (err)
1798 44392932 2019-08-25 stsp goto done;
1799 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1800 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1801 44392932 2019-08-25 stsp }
1802 44392932 2019-08-25 stsp done:
1803 0f2b3dca 2018-12-22 stsp free(id_str1);
1804 0f2b3dca 2018-12-22 stsp free(id_str2);
1805 44392932 2019-08-25 stsp if (pcommit)
1806 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1807 79109fed 2018-03-27 stsp return err;
1808 79109fed 2018-03-27 stsp }
1809 79109fed 2018-03-27 stsp
1810 4bb494d5 2018-06-16 stsp static char *
1811 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1812 6c281f94 2018-06-11 stsp {
1813 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1814 09867e48 2019-08-13 stsp char *p, *s;
1815 09867e48 2019-08-13 stsp
1816 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1817 09867e48 2019-08-13 stsp if (tm == NULL)
1818 09867e48 2019-08-13 stsp return NULL;
1819 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1820 09867e48 2019-08-13 stsp if (s == NULL)
1821 09867e48 2019-08-13 stsp return NULL;
1822 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1823 6c281f94 2018-06-11 stsp if (p)
1824 6c281f94 2018-06-11 stsp *p = '\0';
1825 6c281f94 2018-06-11 stsp return s;
1826 6c281f94 2018-06-11 stsp }
1827 dc424a06 2019-08-07 stsp
1828 6841bf13 2019-11-29 kn static const struct got_error *
1829 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1830 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1831 6841bf13 2019-11-29 kn {
1832 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1833 6841bf13 2019-11-29 kn regmatch_t regmatch;
1834 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1835 6841bf13 2019-11-29 kn
1836 6841bf13 2019-11-29 kn *have_match = 0;
1837 6841bf13 2019-11-29 kn
1838 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1839 6841bf13 2019-11-29 kn if (err)
1840 6841bf13 2019-11-29 kn return err;
1841 6841bf13 2019-11-29 kn
1842 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1843 6841bf13 2019-11-29 kn if (err)
1844 6841bf13 2019-11-29 kn goto done;
1845 6841bf13 2019-11-29 kn
1846 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1847 6841bf13 2019-11-29 kn *have_match = 1;
1848 6841bf13 2019-11-29 kn done:
1849 6841bf13 2019-11-29 kn free(id_str);
1850 6841bf13 2019-11-29 kn free(logmsg);
1851 6841bf13 2019-11-29 kn return err;
1852 6841bf13 2019-11-29 kn }
1853 6841bf13 2019-11-29 kn
1854 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1855 6c281f94 2018-06-11 stsp
1856 79109fed 2018-03-27 stsp static const struct got_error *
1857 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1858 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1859 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1860 79109fed 2018-03-27 stsp {
1861 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1862 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1863 6c281f94 2018-06-11 stsp char datebuf[26];
1864 45d799e2 2018-12-23 stsp time_t committer_time;
1865 45d799e2 2018-12-23 stsp const char *author, *committer;
1866 199a4027 2019-02-02 stsp char *refs_str = NULL;
1867 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1868 5c860e29 2018-03-12 stsp
1869 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1870 199a4027 2019-02-02 stsp char *s;
1871 199a4027 2019-02-02 stsp const char *name;
1872 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1873 a436ad14 2019-08-13 stsp int cmp;
1874 a436ad14 2019-08-13 stsp
1875 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1876 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1877 d9498b20 2019-02-05 stsp continue;
1878 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1879 199a4027 2019-02-02 stsp name += 5;
1880 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1881 7143d404 2019-03-12 stsp continue;
1882 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1883 e34f9ed6 2019-02-02 stsp name += 6;
1884 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1885 141c2bff 2019-02-04 stsp name += 8;
1886 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1887 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1888 5d844a1e 2019-08-13 stsp if (err) {
1889 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1890 5d844a1e 2019-08-13 stsp return err;
1891 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1892 5d844a1e 2019-08-13 stsp err = NULL;
1893 5d844a1e 2019-08-13 stsp tag = NULL;
1894 5d844a1e 2019-08-13 stsp }
1895 a436ad14 2019-08-13 stsp }
1896 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1897 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1898 a436ad14 2019-08-13 stsp if (tag)
1899 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1900 a436ad14 2019-08-13 stsp if (cmp != 0)
1901 a436ad14 2019-08-13 stsp continue;
1902 199a4027 2019-02-02 stsp s = refs_str;
1903 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1904 199a4027 2019-02-02 stsp name) == -1) {
1905 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1906 199a4027 2019-02-02 stsp free(s);
1907 34ca4898 2019-08-13 stsp return err;
1908 199a4027 2019-02-02 stsp }
1909 199a4027 2019-02-02 stsp free(s);
1910 199a4027 2019-02-02 stsp }
1911 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1912 8bf5b3c9 2018-03-17 stsp if (err)
1913 8bf5b3c9 2018-03-17 stsp return err;
1914 788c352e 2018-06-16 stsp
1915 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1916 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1917 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1918 832c249c 2018-06-10 stsp free(id_str);
1919 d3d493d7 2019-02-21 stsp id_str = NULL;
1920 d3d493d7 2019-02-21 stsp free(refs_str);
1921 d3d493d7 2019-02-21 stsp refs_str = NULL;
1922 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1923 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1924 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1925 09867e48 2019-08-13 stsp if (datestr)
1926 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1927 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1928 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1929 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1930 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1931 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1932 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1933 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1934 3fe1abad 2018-06-10 stsp int n = 1;
1935 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1936 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1937 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1938 a0603db2 2018-06-10 stsp if (err)
1939 a0603db2 2018-06-10 stsp return err;
1940 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1941 a0603db2 2018-06-10 stsp free(id_str);
1942 a0603db2 2018-06-10 stsp }
1943 a0603db2 2018-06-10 stsp }
1944 832c249c 2018-06-10 stsp
1945 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1946 5943eee2 2019-08-13 stsp if (err)
1947 5943eee2 2019-08-13 stsp return err;
1948 8bf5b3c9 2018-03-17 stsp
1949 621015ac 2018-07-23 stsp logmsg = logmsg0;
1950 832c249c 2018-06-10 stsp do {
1951 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1952 832c249c 2018-06-10 stsp if (line)
1953 832c249c 2018-06-10 stsp printf(" %s\n", line);
1954 832c249c 2018-06-10 stsp } while (line);
1955 621015ac 2018-07-23 stsp free(logmsg0);
1956 832c249c 2018-06-10 stsp
1957 971751ac 2018-03-27 stsp if (show_patch) {
1958 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1959 971751ac 2018-03-27 stsp if (err == 0)
1960 971751ac 2018-03-27 stsp printf("\n");
1961 971751ac 2018-03-27 stsp }
1962 07862c20 2018-09-15 stsp
1963 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1964 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1965 07862c20 2018-09-15 stsp return err;
1966 f42b1b34 2018-03-12 stsp }
1967 5c860e29 2018-03-12 stsp
1968 f42b1b34 2018-03-12 stsp static const struct got_error *
1969 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1970 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1971 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1972 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1973 f42b1b34 2018-03-12 stsp {
1974 f42b1b34 2018-03-12 stsp const struct got_error *err;
1975 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1976 6841bf13 2019-11-29 kn regex_t regex;
1977 6841bf13 2019-11-29 kn int have_match;
1978 372ccdbb 2018-06-10 stsp
1979 6841bf13 2019-11-29 kn if (search_pattern &&
1980 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1981 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1982 6841bf13 2019-11-29 kn
1983 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1984 f42b1b34 2018-03-12 stsp if (err)
1985 f42b1b34 2018-03-12 stsp return err;
1986 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1987 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1988 372ccdbb 2018-06-10 stsp if (err)
1989 fcc85cad 2018-07-22 stsp goto done;
1990 656b1f76 2019-05-11 jcs for (;;) {
1991 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1992 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1993 8bf5b3c9 2018-03-17 stsp
1994 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1995 84453469 2018-11-11 stsp break;
1996 84453469 2018-11-11 stsp
1997 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1998 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1999 372ccdbb 2018-06-10 stsp if (err) {
2000 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2001 9ba79e04 2018-06-11 stsp err = NULL;
2002 ee780d5c 2020-01-04 stsp break;
2003 7e665116 2018-04-02 stsp }
2004 b43fbaa0 2018-06-11 stsp if (id == NULL)
2005 7e665116 2018-04-02 stsp break;
2006 b43fbaa0 2018-06-11 stsp
2007 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2008 b43fbaa0 2018-06-11 stsp if (err)
2009 fcc85cad 2018-07-22 stsp break;
2010 6841bf13 2019-11-29 kn
2011 6841bf13 2019-11-29 kn if (search_pattern) {
2012 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2013 6841bf13 2019-11-29 kn if (err) {
2014 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2015 6841bf13 2019-11-29 kn break;
2016 6841bf13 2019-11-29 kn }
2017 6841bf13 2019-11-29 kn if (have_match == 0) {
2018 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2019 6841bf13 2019-11-29 kn continue;
2020 6841bf13 2019-11-29 kn }
2021 6841bf13 2019-11-29 kn }
2022 6841bf13 2019-11-29 kn
2023 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2024 44392932 2019-08-25 stsp diff_context, refs);
2025 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2026 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2027 7e665116 2018-04-02 stsp break;
2028 31cedeaf 2018-09-15 stsp }
2029 fcc85cad 2018-07-22 stsp done:
2030 6841bf13 2019-11-29 kn if (search_pattern)
2031 6841bf13 2019-11-29 kn regfree(&regex);
2032 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2033 f42b1b34 2018-03-12 stsp return err;
2034 f42b1b34 2018-03-12 stsp }
2035 5c860e29 2018-03-12 stsp
2036 4ed7e80c 2018-05-20 stsp __dead static void
2037 6f3d1eb0 2018-03-12 stsp usage_log(void)
2038 6f3d1eb0 2018-03-12 stsp {
2039 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2040 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2041 6f3d1eb0 2018-03-12 stsp exit(1);
2042 b1ebc001 2019-08-13 stsp }
2043 b1ebc001 2019-08-13 stsp
2044 b1ebc001 2019-08-13 stsp static int
2045 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2046 b1ebc001 2019-08-13 stsp {
2047 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2048 b1ebc001 2019-08-13 stsp long long n;
2049 b1ebc001 2019-08-13 stsp const char *errstr;
2050 b1ebc001 2019-08-13 stsp
2051 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2052 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2053 b1ebc001 2019-08-13 stsp return 0;
2054 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2055 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2056 b1ebc001 2019-08-13 stsp return 0;
2057 b1ebc001 2019-08-13 stsp return n;
2058 6f3d1eb0 2018-03-12 stsp }
2059 6f3d1eb0 2018-03-12 stsp
2060 4ed7e80c 2018-05-20 stsp static const struct got_error *
2061 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2062 f42b1b34 2018-03-12 stsp {
2063 f42b1b34 2018-03-12 stsp const struct got_error *error;
2064 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2065 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2066 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2067 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2068 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2069 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2070 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2071 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2072 64a96a6d 2018-04-01 stsp const char *errstr;
2073 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2074 1b3893a2 2019-03-18 stsp
2075 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2076 5c860e29 2018-03-12 stsp
2077 6715a751 2018-03-16 stsp #ifndef PROFILE
2078 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2079 6098196c 2019-01-04 stsp NULL)
2080 ad242220 2018-09-08 stsp == -1)
2081 f42b1b34 2018-03-12 stsp err(1, "pledge");
2082 6715a751 2018-03-16 stsp #endif
2083 79109fed 2018-03-27 stsp
2084 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2085 b1ebc001 2019-08-13 stsp
2086 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2087 79109fed 2018-03-27 stsp switch (ch) {
2088 79109fed 2018-03-27 stsp case 'p':
2089 79109fed 2018-03-27 stsp show_patch = 1;
2090 d142fc45 2018-04-01 stsp break;
2091 d142fc45 2018-04-01 stsp case 'c':
2092 d142fc45 2018-04-01 stsp start_commit = optarg;
2093 64a96a6d 2018-04-01 stsp break;
2094 c0cc5c62 2018-10-18 stsp case 'C':
2095 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2096 4a8520aa 2018-10-18 stsp &errstr);
2097 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2098 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2099 c0cc5c62 2018-10-18 stsp break;
2100 64a96a6d 2018-04-01 stsp case 'l':
2101 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2102 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2103 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2104 cc54c501 2019-07-15 stsp break;
2105 48c8c60d 2020-01-27 stsp case 'b':
2106 48c8c60d 2020-01-27 stsp log_branches = 1;
2107 a0603db2 2018-06-10 stsp break;
2108 04ca23f4 2018-07-16 stsp case 'r':
2109 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2110 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2111 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2112 9ba1d308 2019-10-21 stsp optarg);
2113 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2114 04ca23f4 2018-07-16 stsp break;
2115 6841bf13 2019-11-29 kn case 's':
2116 6841bf13 2019-11-29 kn search_pattern = optarg;
2117 6841bf13 2019-11-29 kn break;
2118 79109fed 2018-03-27 stsp default:
2119 2deda0b9 2019-03-07 stsp usage_log();
2120 79109fed 2018-03-27 stsp /* NOTREACHED */
2121 79109fed 2018-03-27 stsp }
2122 79109fed 2018-03-27 stsp }
2123 79109fed 2018-03-27 stsp
2124 79109fed 2018-03-27 stsp argc -= optind;
2125 79109fed 2018-03-27 stsp argv += optind;
2126 f42b1b34 2018-03-12 stsp
2127 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2128 dc1edbfa 2019-11-29 kn diff_context = 3;
2129 dc1edbfa 2019-11-29 kn else if (!show_patch)
2130 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2131 dc1edbfa 2019-11-29 kn
2132 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2133 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2134 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2135 04ca23f4 2018-07-16 stsp goto done;
2136 04ca23f4 2018-07-16 stsp }
2137 cffc0aa4 2019-02-05 stsp
2138 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2139 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2140 cffc0aa4 2019-02-05 stsp goto done;
2141 cffc0aa4 2019-02-05 stsp error = NULL;
2142 cffc0aa4 2019-02-05 stsp
2143 e7301579 2019-03-18 stsp if (argc == 0) {
2144 cbd1af7a 2019-03-18 stsp path = strdup("");
2145 e7301579 2019-03-18 stsp if (path == NULL) {
2146 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2147 cbd1af7a 2019-03-18 stsp goto done;
2148 e7301579 2019-03-18 stsp }
2149 e7301579 2019-03-18 stsp } else if (argc == 1) {
2150 e7301579 2019-03-18 stsp if (worktree) {
2151 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2152 e7301579 2019-03-18 stsp argv[0]);
2153 e7301579 2019-03-18 stsp if (error)
2154 e7301579 2019-03-18 stsp goto done;
2155 e7301579 2019-03-18 stsp } else {
2156 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2157 e7301579 2019-03-18 stsp if (path == NULL) {
2158 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2159 e7301579 2019-03-18 stsp goto done;
2160 e7301579 2019-03-18 stsp }
2161 e7301579 2019-03-18 stsp }
2162 cbd1af7a 2019-03-18 stsp } else
2163 cbd1af7a 2019-03-18 stsp usage_log();
2164 cbd1af7a 2019-03-18 stsp
2165 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2166 5486daa2 2019-05-11 stsp repo_path = worktree ?
2167 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2168 5486daa2 2019-05-11 stsp }
2169 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2170 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2171 cffc0aa4 2019-02-05 stsp goto done;
2172 04ca23f4 2018-07-16 stsp }
2173 6098196c 2019-01-04 stsp
2174 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2175 d7d4f210 2018-03-12 stsp if (error != NULL)
2176 04ca23f4 2018-07-16 stsp goto done;
2177 f42b1b34 2018-03-12 stsp
2178 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2179 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2180 c02c541e 2019-03-29 stsp if (error)
2181 c02c541e 2019-03-29 stsp goto done;
2182 c02c541e 2019-03-29 stsp
2183 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2184 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2185 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2186 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2187 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2188 3235492e 2018-04-01 stsp if (error != NULL)
2189 3235492e 2018-04-01 stsp return error;
2190 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2191 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2192 3235492e 2018-04-01 stsp if (error != NULL)
2193 3235492e 2018-04-01 stsp return error;
2194 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2195 3235492e 2018-04-01 stsp } else {
2196 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2197 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2198 3235492e 2018-04-01 stsp if (error == NULL) {
2199 1caad61b 2019-02-01 stsp int obj_type;
2200 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2201 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2202 d1f2edc9 2018-06-13 stsp if (error != NULL)
2203 1caad61b 2019-02-01 stsp goto done;
2204 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2205 1caad61b 2019-02-01 stsp if (error != NULL)
2206 1caad61b 2019-02-01 stsp goto done;
2207 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2208 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2209 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2210 1caad61b 2019-02-01 stsp if (error != NULL)
2211 1caad61b 2019-02-01 stsp goto done;
2212 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2213 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2214 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2215 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2216 1caad61b 2019-02-01 stsp goto done;
2217 1caad61b 2019-02-01 stsp }
2218 1caad61b 2019-02-01 stsp free(id);
2219 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2220 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2221 1caad61b 2019-02-01 stsp if (id == NULL)
2222 638f9024 2019-05-13 stsp error = got_error_from_errno(
2223 230a42bd 2019-05-11 jcs "got_object_id_dup");
2224 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2225 1caad61b 2019-02-01 stsp if (error)
2226 1caad61b 2019-02-01 stsp goto done;
2227 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2228 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2229 1caad61b 2019-02-01 stsp goto done;
2230 1caad61b 2019-02-01 stsp }
2231 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2232 d1f2edc9 2018-06-13 stsp if (error != NULL)
2233 1caad61b 2019-02-01 stsp goto done;
2234 d1f2edc9 2018-06-13 stsp }
2235 15a94983 2018-12-23 stsp if (commit == NULL) {
2236 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2237 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2238 d1f2edc9 2018-06-13 stsp if (error != NULL)
2239 d1f2edc9 2018-06-13 stsp return error;
2240 3235492e 2018-04-01 stsp }
2241 3235492e 2018-04-01 stsp }
2242 d7d4f210 2018-03-12 stsp if (error != NULL)
2243 04ca23f4 2018-07-16 stsp goto done;
2244 04ca23f4 2018-07-16 stsp
2245 deeabeae 2019-08-27 stsp if (worktree) {
2246 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2247 deeabeae 2019-08-27 stsp char *p;
2248 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2249 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2250 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2251 deeabeae 2019-08-27 stsp goto done;
2252 deeabeae 2019-08-27 stsp }
2253 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2254 deeabeae 2019-08-27 stsp free(p);
2255 deeabeae 2019-08-27 stsp } else
2256 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2257 04ca23f4 2018-07-16 stsp if (error != NULL)
2258 04ca23f4 2018-07-16 stsp goto done;
2259 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2260 04ca23f4 2018-07-16 stsp free(path);
2261 04ca23f4 2018-07-16 stsp path = in_repo_path;
2262 04ca23f4 2018-07-16 stsp }
2263 199a4027 2019-02-02 stsp
2264 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2265 199a4027 2019-02-02 stsp if (error)
2266 199a4027 2019-02-02 stsp goto done;
2267 04ca23f4 2018-07-16 stsp
2268 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2269 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2270 04ca23f4 2018-07-16 stsp done:
2271 04ca23f4 2018-07-16 stsp free(path);
2272 04ca23f4 2018-07-16 stsp free(repo_path);
2273 04ca23f4 2018-07-16 stsp free(cwd);
2274 f42b1b34 2018-03-12 stsp free(id);
2275 cffc0aa4 2019-02-05 stsp if (worktree)
2276 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2277 ad242220 2018-09-08 stsp if (repo) {
2278 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2279 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2280 ad242220 2018-09-08 stsp if (error == NULL)
2281 ad242220 2018-09-08 stsp error = repo_error;
2282 ad242220 2018-09-08 stsp }
2283 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2284 8bf5b3c9 2018-03-17 stsp return error;
2285 5c860e29 2018-03-12 stsp }
2286 5c860e29 2018-03-12 stsp
2287 4ed7e80c 2018-05-20 stsp __dead static void
2288 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2289 3f8b7d6a 2018-04-01 stsp {
2290 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2291 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2292 3f8b7d6a 2018-04-01 stsp exit(1);
2293 b00d56cd 2018-04-01 stsp }
2294 b00d56cd 2018-04-01 stsp
2295 b72f483a 2019-02-05 stsp struct print_diff_arg {
2296 b72f483a 2019-02-05 stsp struct got_repository *repo;
2297 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2298 b72f483a 2019-02-05 stsp int diff_context;
2299 3fc0c068 2019-02-10 stsp const char *id_str;
2300 3fc0c068 2019-02-10 stsp int header_shown;
2301 98eaaa12 2019-08-03 stsp int diff_staged;
2302 63035f9f 2019-10-06 stsp int ignore_whitespace;
2303 b72f483a 2019-02-05 stsp };
2304 b72f483a 2019-02-05 stsp
2305 4ed7e80c 2018-05-20 stsp static const struct got_error *
2306 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2307 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2308 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2309 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2310 b72f483a 2019-02-05 stsp {
2311 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2312 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2313 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2314 12463d8b 2019-12-13 stsp int fd = -1;
2315 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2316 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2317 b72f483a 2019-02-05 stsp struct stat sb;
2318 b72f483a 2019-02-05 stsp
2319 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2320 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2321 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2322 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2323 98eaaa12 2019-08-03 stsp return NULL;
2324 98eaaa12 2019-08-03 stsp } else {
2325 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2326 98eaaa12 2019-08-03 stsp return NULL;
2327 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2328 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2329 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2330 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2331 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2332 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2333 98eaaa12 2019-08-03 stsp return NULL;
2334 98eaaa12 2019-08-03 stsp }
2335 3fc0c068 2019-02-10 stsp
2336 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2337 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2338 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2339 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2340 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2341 3fc0c068 2019-02-10 stsp }
2342 b72f483a 2019-02-05 stsp
2343 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2344 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2345 98eaaa12 2019-08-03 stsp switch (staged_status) {
2346 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2347 98eaaa12 2019-08-03 stsp label1 = path;
2348 98eaaa12 2019-08-03 stsp label2 = path;
2349 98eaaa12 2019-08-03 stsp break;
2350 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2351 98eaaa12 2019-08-03 stsp label2 = path;
2352 98eaaa12 2019-08-03 stsp break;
2353 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2354 98eaaa12 2019-08-03 stsp label1 = path;
2355 98eaaa12 2019-08-03 stsp break;
2356 98eaaa12 2019-08-03 stsp default:
2357 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2358 98eaaa12 2019-08-03 stsp }
2359 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2360 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2361 63035f9f 2019-10-06 stsp a->repo, stdout);
2362 98eaaa12 2019-08-03 stsp }
2363 98eaaa12 2019-08-03 stsp
2364 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2365 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2366 4ce46740 2019-08-08 stsp char *id_str;
2367 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2368 408b4ebc 2019-08-03 stsp 8192);
2369 4ce46740 2019-08-08 stsp if (err)
2370 4ce46740 2019-08-08 stsp goto done;
2371 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2372 4ce46740 2019-08-08 stsp if (err)
2373 4ce46740 2019-08-08 stsp goto done;
2374 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2375 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2376 4ce46740 2019-08-08 stsp free(id_str);
2377 4ce46740 2019-08-08 stsp goto done;
2378 4ce46740 2019-08-08 stsp }
2379 4ce46740 2019-08-08 stsp free(id_str);
2380 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2381 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2382 4ce46740 2019-08-08 stsp if (err)
2383 4ce46740 2019-08-08 stsp goto done;
2384 4ce46740 2019-08-08 stsp }
2385 d00136be 2019-03-26 stsp
2386 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2387 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2388 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2389 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2390 2ec1f75b 2019-03-26 stsp goto done;
2391 2ec1f75b 2019-03-26 stsp }
2392 b72f483a 2019-02-05 stsp
2393 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2394 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2395 12463d8b 2019-12-13 stsp if (fd == -1) {
2396 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2397 12463d8b 2019-12-13 stsp goto done;
2398 12463d8b 2019-12-13 stsp }
2399 12463d8b 2019-12-13 stsp } else {
2400 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2401 12463d8b 2019-12-13 stsp if (fd == -1) {
2402 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2403 12463d8b 2019-12-13 stsp goto done;
2404 12463d8b 2019-12-13 stsp }
2405 12463d8b 2019-12-13 stsp }
2406 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2407 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2408 2ec1f75b 2019-03-26 stsp goto done;
2409 2ec1f75b 2019-03-26 stsp }
2410 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2411 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2412 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2413 2ec1f75b 2019-03-26 stsp goto done;
2414 2ec1f75b 2019-03-26 stsp }
2415 12463d8b 2019-12-13 stsp fd = -1;
2416 2ec1f75b 2019-03-26 stsp } else
2417 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2418 b72f483a 2019-02-05 stsp
2419 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2420 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2421 b72f483a 2019-02-05 stsp done:
2422 b72f483a 2019-02-05 stsp if (blob1)
2423 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2424 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2425 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2426 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2427 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2428 b72f483a 2019-02-05 stsp free(abspath);
2429 927df6b7 2019-02-10 stsp return err;
2430 927df6b7 2019-02-10 stsp }
2431 927df6b7 2019-02-10 stsp
2432 927df6b7 2019-02-10 stsp static const struct got_error *
2433 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2434 b00d56cd 2018-04-01 stsp {
2435 b00d56cd 2018-04-01 stsp const struct got_error *error;
2436 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2437 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2438 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2439 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2440 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2441 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2442 15a94983 2018-12-23 stsp int type1, type2;
2443 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2444 c0cc5c62 2018-10-18 stsp const char *errstr;
2445 927df6b7 2019-02-10 stsp char *path = NULL;
2446 b00d56cd 2018-04-01 stsp
2447 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2448 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2449 25eccc22 2019-01-04 stsp NULL) == -1)
2450 b00d56cd 2018-04-01 stsp err(1, "pledge");
2451 b00d56cd 2018-04-01 stsp #endif
2452 b00d56cd 2018-04-01 stsp
2453 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2454 b00d56cd 2018-04-01 stsp switch (ch) {
2455 c0cc5c62 2018-10-18 stsp case 'C':
2456 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2457 dfcab68b 2019-11-29 kn &errstr);
2458 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2459 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2460 c0cc5c62 2018-10-18 stsp break;
2461 b72f483a 2019-02-05 stsp case 'r':
2462 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2463 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2464 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2465 9ba1d308 2019-10-21 stsp optarg);
2466 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2467 b72f483a 2019-02-05 stsp break;
2468 98eaaa12 2019-08-03 stsp case 's':
2469 98eaaa12 2019-08-03 stsp diff_staged = 1;
2470 63035f9f 2019-10-06 stsp break;
2471 63035f9f 2019-10-06 stsp case 'w':
2472 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2473 98eaaa12 2019-08-03 stsp break;
2474 b00d56cd 2018-04-01 stsp default:
2475 2deda0b9 2019-03-07 stsp usage_diff();
2476 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2477 b00d56cd 2018-04-01 stsp }
2478 b00d56cd 2018-04-01 stsp }
2479 b00d56cd 2018-04-01 stsp
2480 b00d56cd 2018-04-01 stsp argc -= optind;
2481 b00d56cd 2018-04-01 stsp argv += optind;
2482 b00d56cd 2018-04-01 stsp
2483 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2484 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2485 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2486 b72f483a 2019-02-05 stsp goto done;
2487 b72f483a 2019-02-05 stsp }
2488 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2489 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2490 927df6b7 2019-02-10 stsp goto done;
2491 927df6b7 2019-02-10 stsp if (argc <= 1) {
2492 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2493 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2494 927df6b7 2019-02-10 stsp goto done;
2495 927df6b7 2019-02-10 stsp }
2496 b72f483a 2019-02-05 stsp if (repo_path)
2497 b72f483a 2019-02-05 stsp errx(1,
2498 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2499 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2500 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2501 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2502 927df6b7 2019-02-10 stsp goto done;
2503 927df6b7 2019-02-10 stsp }
2504 927df6b7 2019-02-10 stsp if (argc == 1) {
2505 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2506 cbd1af7a 2019-03-18 stsp argv[0]);
2507 927df6b7 2019-02-10 stsp if (error)
2508 927df6b7 2019-02-10 stsp goto done;
2509 927df6b7 2019-02-10 stsp } else {
2510 927df6b7 2019-02-10 stsp path = strdup("");
2511 927df6b7 2019-02-10 stsp if (path == NULL) {
2512 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2513 927df6b7 2019-02-10 stsp goto done;
2514 927df6b7 2019-02-10 stsp }
2515 927df6b7 2019-02-10 stsp }
2516 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2517 98eaaa12 2019-08-03 stsp if (diff_staged)
2518 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2519 98eaaa12 2019-08-03 stsp "objects in repository");
2520 15a94983 2018-12-23 stsp id_str1 = argv[0];
2521 15a94983 2018-12-23 stsp id_str2 = argv[1];
2522 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2523 30db809c 2019-06-05 stsp repo_path =
2524 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2525 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2526 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2527 30db809c 2019-06-05 stsp goto done;
2528 30db809c 2019-06-05 stsp }
2529 30db809c 2019-06-05 stsp }
2530 b00d56cd 2018-04-01 stsp } else
2531 b00d56cd 2018-04-01 stsp usage_diff();
2532 25eccc22 2019-01-04 stsp
2533 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2534 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2535 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2536 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2537 b72f483a 2019-02-05 stsp }
2538 b00d56cd 2018-04-01 stsp
2539 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2540 76089277 2018-04-01 stsp free(repo_path);
2541 b00d56cd 2018-04-01 stsp if (error != NULL)
2542 b00d56cd 2018-04-01 stsp goto done;
2543 b00d56cd 2018-04-01 stsp
2544 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2545 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2546 c02c541e 2019-03-29 stsp if (error)
2547 c02c541e 2019-03-29 stsp goto done;
2548 c02c541e 2019-03-29 stsp
2549 30db809c 2019-06-05 stsp if (argc <= 1) {
2550 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2551 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2552 b72f483a 2019-02-05 stsp char *id_str;
2553 72ea6654 2019-07-27 stsp
2554 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2555 72ea6654 2019-07-27 stsp
2556 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2557 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2558 b72f483a 2019-02-05 stsp if (error)
2559 b72f483a 2019-02-05 stsp goto done;
2560 b72f483a 2019-02-05 stsp arg.repo = repo;
2561 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2562 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2563 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2564 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2565 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2566 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2567 b72f483a 2019-02-05 stsp
2568 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2569 72ea6654 2019-07-27 stsp if (error)
2570 72ea6654 2019-07-27 stsp goto done;
2571 72ea6654 2019-07-27 stsp
2572 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2573 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2574 b72f483a 2019-02-05 stsp free(id_str);
2575 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2576 b72f483a 2019-02-05 stsp goto done;
2577 b72f483a 2019-02-05 stsp }
2578 b72f483a 2019-02-05 stsp
2579 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2580 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2581 0adb7196 2019-08-11 stsp if (error)
2582 0adb7196 2019-08-11 stsp goto done;
2583 b00d56cd 2018-04-01 stsp
2584 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2585 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2586 0adb7196 2019-08-11 stsp if (error)
2587 0adb7196 2019-08-11 stsp goto done;
2588 b00d56cd 2018-04-01 stsp
2589 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2590 15a94983 2018-12-23 stsp if (error)
2591 15a94983 2018-12-23 stsp goto done;
2592 15a94983 2018-12-23 stsp
2593 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2594 15a94983 2018-12-23 stsp if (error)
2595 15a94983 2018-12-23 stsp goto done;
2596 15a94983 2018-12-23 stsp
2597 15a94983 2018-12-23 stsp if (type1 != type2) {
2598 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2599 b00d56cd 2018-04-01 stsp goto done;
2600 b00d56cd 2018-04-01 stsp }
2601 b00d56cd 2018-04-01 stsp
2602 15a94983 2018-12-23 stsp switch (type1) {
2603 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2604 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2605 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2606 b00d56cd 2018-04-01 stsp break;
2607 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2608 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2609 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2610 b00d56cd 2018-04-01 stsp break;
2611 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2612 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2613 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2614 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2615 b00d56cd 2018-04-01 stsp break;
2616 b00d56cd 2018-04-01 stsp default:
2617 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2618 b00d56cd 2018-04-01 stsp }
2619 b00d56cd 2018-04-01 stsp done:
2620 5e70831e 2019-05-28 stsp free(label1);
2621 5e70831e 2019-05-28 stsp free(label2);
2622 15a94983 2018-12-23 stsp free(id1);
2623 15a94983 2018-12-23 stsp free(id2);
2624 927df6b7 2019-02-10 stsp free(path);
2625 b72f483a 2019-02-05 stsp if (worktree)
2626 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2627 ad242220 2018-09-08 stsp if (repo) {
2628 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2629 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2630 ad242220 2018-09-08 stsp if (error == NULL)
2631 ad242220 2018-09-08 stsp error = repo_error;
2632 ad242220 2018-09-08 stsp }
2633 b00d56cd 2018-04-01 stsp return error;
2634 404c43c4 2018-06-21 stsp }
2635 404c43c4 2018-06-21 stsp
2636 404c43c4 2018-06-21 stsp __dead static void
2637 404c43c4 2018-06-21 stsp usage_blame(void)
2638 404c43c4 2018-06-21 stsp {
2639 9270e621 2019-02-05 stsp fprintf(stderr,
2640 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2641 404c43c4 2018-06-21 stsp getprogname());
2642 404c43c4 2018-06-21 stsp exit(1);
2643 b00d56cd 2018-04-01 stsp }
2644 b00d56cd 2018-04-01 stsp
2645 28315671 2019-08-14 stsp struct blame_line {
2646 28315671 2019-08-14 stsp int annotated;
2647 28315671 2019-08-14 stsp char *id_str;
2648 82f6abb8 2019-08-14 stsp char *committer;
2649 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2650 28315671 2019-08-14 stsp };
2651 28315671 2019-08-14 stsp
2652 28315671 2019-08-14 stsp struct blame_cb_args {
2653 28315671 2019-08-14 stsp struct blame_line *lines;
2654 28315671 2019-08-14 stsp int nlines;
2655 7ef28ff8 2019-08-14 stsp int nlines_prec;
2656 28315671 2019-08-14 stsp int lineno_cur;
2657 28315671 2019-08-14 stsp off_t *line_offsets;
2658 28315671 2019-08-14 stsp FILE *f;
2659 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2660 28315671 2019-08-14 stsp };
2661 28315671 2019-08-14 stsp
2662 404c43c4 2018-06-21 stsp static const struct got_error *
2663 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2664 28315671 2019-08-14 stsp {
2665 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2666 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2667 28315671 2019-08-14 stsp struct blame_line *bline;
2668 82f6abb8 2019-08-14 stsp char *line = NULL;
2669 28315671 2019-08-14 stsp size_t linesize = 0;
2670 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2671 28315671 2019-08-14 stsp off_t offset;
2672 bcb49d15 2019-08-14 stsp struct tm tm;
2673 bcb49d15 2019-08-14 stsp time_t committer_time;
2674 28315671 2019-08-14 stsp
2675 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2676 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2677 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2678 28315671 2019-08-14 stsp
2679 28315671 2019-08-14 stsp if (sigint_received)
2680 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2681 28315671 2019-08-14 stsp
2682 2839f8b9 2019-08-18 stsp if (lineno == -1)
2683 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2684 2839f8b9 2019-08-18 stsp
2685 28315671 2019-08-14 stsp /* Annotate this line. */
2686 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2687 28315671 2019-08-14 stsp if (bline->annotated)
2688 28315671 2019-08-14 stsp return NULL;
2689 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2690 28315671 2019-08-14 stsp if (err)
2691 28315671 2019-08-14 stsp return err;
2692 82f6abb8 2019-08-14 stsp
2693 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2694 82f6abb8 2019-08-14 stsp if (err)
2695 82f6abb8 2019-08-14 stsp goto done;
2696 82f6abb8 2019-08-14 stsp
2697 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2698 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2699 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2700 bcb49d15 2019-08-14 stsp goto done;
2701 bcb49d15 2019-08-14 stsp }
2702 bcb49d15 2019-08-14 stsp
2703 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2704 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2705 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2706 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2707 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2708 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2709 82f6abb8 2019-08-14 stsp goto done;
2710 82f6abb8 2019-08-14 stsp }
2711 28315671 2019-08-14 stsp bline->annotated = 1;
2712 28315671 2019-08-14 stsp
2713 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2714 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2715 28315671 2019-08-14 stsp if (!bline->annotated)
2716 82f6abb8 2019-08-14 stsp goto done;
2717 28315671 2019-08-14 stsp
2718 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2719 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2720 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2721 82f6abb8 2019-08-14 stsp goto done;
2722 82f6abb8 2019-08-14 stsp }
2723 28315671 2019-08-14 stsp
2724 28315671 2019-08-14 stsp while (bline->annotated) {
2725 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2726 82f6abb8 2019-08-14 stsp size_t len;
2727 82f6abb8 2019-08-14 stsp
2728 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2729 28315671 2019-08-14 stsp if (ferror(a->f))
2730 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2731 28315671 2019-08-14 stsp break;
2732 28315671 2019-08-14 stsp }
2733 82f6abb8 2019-08-14 stsp
2734 82f6abb8 2019-08-14 stsp committer = bline->committer;
2735 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2736 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2737 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2738 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2739 82f6abb8 2019-08-14 stsp if (at)
2740 82f6abb8 2019-08-14 stsp *at = '\0';
2741 82f6abb8 2019-08-14 stsp len = strlen(committer);
2742 82f6abb8 2019-08-14 stsp if (len >= 9)
2743 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2744 28315671 2019-08-14 stsp
2745 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2746 28315671 2019-08-14 stsp if (nl)
2747 28315671 2019-08-14 stsp *nl = '\0';
2748 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2749 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2750 28315671 2019-08-14 stsp
2751 28315671 2019-08-14 stsp a->lineno_cur++;
2752 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2753 28315671 2019-08-14 stsp }
2754 82f6abb8 2019-08-14 stsp done:
2755 82f6abb8 2019-08-14 stsp if (commit)
2756 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2757 28315671 2019-08-14 stsp free(line);
2758 28315671 2019-08-14 stsp return err;
2759 28315671 2019-08-14 stsp }
2760 28315671 2019-08-14 stsp
2761 28315671 2019-08-14 stsp static const struct got_error *
2762 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2763 404c43c4 2018-06-21 stsp {
2764 404c43c4 2018-06-21 stsp const struct got_error *error;
2765 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2766 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2767 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2768 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2769 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2770 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2771 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2772 28315671 2019-08-14 stsp struct blame_cb_args bca;
2773 28315671 2019-08-14 stsp int ch, obj_type, i;
2774 b02560ec 2019-08-19 stsp size_t filesize;
2775 90f3c347 2019-08-19 stsp
2776 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2777 404c43c4 2018-06-21 stsp
2778 404c43c4 2018-06-21 stsp #ifndef PROFILE
2779 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2780 36e2fb66 2019-01-04 stsp NULL) == -1)
2781 404c43c4 2018-06-21 stsp err(1, "pledge");
2782 404c43c4 2018-06-21 stsp #endif
2783 404c43c4 2018-06-21 stsp
2784 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2785 404c43c4 2018-06-21 stsp switch (ch) {
2786 404c43c4 2018-06-21 stsp case 'c':
2787 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2788 404c43c4 2018-06-21 stsp break;
2789 66bea077 2018-08-02 stsp case 'r':
2790 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2791 66bea077 2018-08-02 stsp if (repo_path == NULL)
2792 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2793 9ba1d308 2019-10-21 stsp optarg);
2794 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2795 66bea077 2018-08-02 stsp break;
2796 404c43c4 2018-06-21 stsp default:
2797 2deda0b9 2019-03-07 stsp usage_blame();
2798 404c43c4 2018-06-21 stsp /* NOTREACHED */
2799 404c43c4 2018-06-21 stsp }
2800 404c43c4 2018-06-21 stsp }
2801 404c43c4 2018-06-21 stsp
2802 404c43c4 2018-06-21 stsp argc -= optind;
2803 404c43c4 2018-06-21 stsp argv += optind;
2804 404c43c4 2018-06-21 stsp
2805 a39318fd 2018-08-02 stsp if (argc == 1)
2806 404c43c4 2018-06-21 stsp path = argv[0];
2807 a39318fd 2018-08-02 stsp else
2808 404c43c4 2018-06-21 stsp usage_blame();
2809 404c43c4 2018-06-21 stsp
2810 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2811 66bea077 2018-08-02 stsp if (cwd == NULL) {
2812 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2813 66bea077 2018-08-02 stsp goto done;
2814 66bea077 2018-08-02 stsp }
2815 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2816 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2817 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2818 66bea077 2018-08-02 stsp goto done;
2819 0c06baac 2019-02-05 stsp else
2820 0c06baac 2019-02-05 stsp error = NULL;
2821 0c06baac 2019-02-05 stsp if (worktree) {
2822 0c06baac 2019-02-05 stsp repo_path =
2823 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2824 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2825 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2826 0c06baac 2019-02-05 stsp if (error)
2827 0c06baac 2019-02-05 stsp goto done;
2828 0c06baac 2019-02-05 stsp } else {
2829 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2830 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2831 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2832 0c06baac 2019-02-05 stsp goto done;
2833 0c06baac 2019-02-05 stsp }
2834 66bea077 2018-08-02 stsp }
2835 66bea077 2018-08-02 stsp }
2836 36e2fb66 2019-01-04 stsp
2837 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2838 c02c541e 2019-03-29 stsp if (error != NULL)
2839 36e2fb66 2019-01-04 stsp goto done;
2840 66bea077 2018-08-02 stsp
2841 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2842 c02c541e 2019-03-29 stsp if (error)
2843 404c43c4 2018-06-21 stsp goto done;
2844 404c43c4 2018-06-21 stsp
2845 0c06baac 2019-02-05 stsp if (worktree) {
2846 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2847 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2848 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2849 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2850 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2851 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2852 6efaaa2d 2019-02-05 stsp path) == -1) {
2853 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2854 0c06baac 2019-02-05 stsp goto done;
2855 0c06baac 2019-02-05 stsp }
2856 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2857 0c06baac 2019-02-05 stsp free(p);
2858 0c06baac 2019-02-05 stsp } else {
2859 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2860 0c06baac 2019-02-05 stsp }
2861 0c06baac 2019-02-05 stsp if (error)
2862 66bea077 2018-08-02 stsp goto done;
2863 66bea077 2018-08-02 stsp
2864 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2865 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2866 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2867 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2868 404c43c4 2018-06-21 stsp if (error != NULL)
2869 66bea077 2018-08-02 stsp goto done;
2870 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2871 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2872 404c43c4 2018-06-21 stsp if (error != NULL)
2873 66bea077 2018-08-02 stsp goto done;
2874 404c43c4 2018-06-21 stsp } else {
2875 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2876 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2877 30837e32 2019-07-25 stsp if (error)
2878 66bea077 2018-08-02 stsp goto done;
2879 404c43c4 2018-06-21 stsp }
2880 404c43c4 2018-06-21 stsp
2881 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2882 28315671 2019-08-14 stsp if (error)
2883 28315671 2019-08-14 stsp goto done;
2884 28315671 2019-08-14 stsp
2885 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2886 28315671 2019-08-14 stsp if (error)
2887 28315671 2019-08-14 stsp goto done;
2888 28315671 2019-08-14 stsp
2889 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2890 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2891 28315671 2019-08-14 stsp goto done;
2892 28315671 2019-08-14 stsp }
2893 28315671 2019-08-14 stsp
2894 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2895 28315671 2019-08-14 stsp if (error)
2896 28315671 2019-08-14 stsp goto done;
2897 28315671 2019-08-14 stsp bca.f = got_opentemp();
2898 28315671 2019-08-14 stsp if (bca.f == NULL) {
2899 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2900 28315671 2019-08-14 stsp goto done;
2901 28315671 2019-08-14 stsp }
2902 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2903 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2904 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2905 28315671 2019-08-14 stsp goto done;
2906 28315671 2019-08-14 stsp
2907 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2908 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2909 b02560ec 2019-08-19 stsp bca.nlines--;
2910 b02560ec 2019-08-19 stsp
2911 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2912 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2913 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2914 28315671 2019-08-14 stsp goto done;
2915 28315671 2019-08-14 stsp }
2916 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2917 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2918 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2919 7ef28ff8 2019-08-14 stsp while (i > 0) {
2920 7ef28ff8 2019-08-14 stsp i /= 10;
2921 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2922 7ef28ff8 2019-08-14 stsp }
2923 82f6abb8 2019-08-14 stsp bca.repo = repo;
2924 7ef28ff8 2019-08-14 stsp
2925 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2926 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2927 404c43c4 2018-06-21 stsp done:
2928 66bea077 2018-08-02 stsp free(in_repo_path);
2929 66bea077 2018-08-02 stsp free(repo_path);
2930 66bea077 2018-08-02 stsp free(cwd);
2931 404c43c4 2018-06-21 stsp free(commit_id);
2932 28315671 2019-08-14 stsp free(obj_id);
2933 28315671 2019-08-14 stsp if (blob)
2934 28315671 2019-08-14 stsp got_object_blob_close(blob);
2935 0c06baac 2019-02-05 stsp if (worktree)
2936 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2937 ad242220 2018-09-08 stsp if (repo) {
2938 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2939 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2940 ad242220 2018-09-08 stsp if (error == NULL)
2941 ad242220 2018-09-08 stsp error = repo_error;
2942 ad242220 2018-09-08 stsp }
2943 3affba96 2019-09-22 stsp if (bca.lines) {
2944 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2945 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2946 3affba96 2019-09-22 stsp free(bline->id_str);
2947 3affba96 2019-09-22 stsp free(bline->committer);
2948 3affba96 2019-09-22 stsp }
2949 3affba96 2019-09-22 stsp free(bca.lines);
2950 28315671 2019-08-14 stsp }
2951 28315671 2019-08-14 stsp free(bca.line_offsets);
2952 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2953 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2954 404c43c4 2018-06-21 stsp return error;
2955 5de5890b 2018-10-18 stsp }
2956 5de5890b 2018-10-18 stsp
2957 5de5890b 2018-10-18 stsp __dead static void
2958 5de5890b 2018-10-18 stsp usage_tree(void)
2959 5de5890b 2018-10-18 stsp {
2960 c1669e2e 2019-01-09 stsp fprintf(stderr,
2961 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2962 5de5890b 2018-10-18 stsp getprogname());
2963 5de5890b 2018-10-18 stsp exit(1);
2964 5de5890b 2018-10-18 stsp }
2965 5de5890b 2018-10-18 stsp
2966 c1669e2e 2019-01-09 stsp static void
2967 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2968 c1669e2e 2019-01-09 stsp const char *root_path)
2969 c1669e2e 2019-01-09 stsp {
2970 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2971 848d6979 2019-08-12 stsp const char *modestr = "";
2972 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2973 5de5890b 2018-10-18 stsp
2974 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2975 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2976 c1669e2e 2019-01-09 stsp path++;
2977 c1669e2e 2019-01-09 stsp
2978 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2979 63c5ca5d 2019-08-24 stsp modestr = "$";
2980 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2981 848d6979 2019-08-12 stsp modestr = "@";
2982 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2983 848d6979 2019-08-12 stsp modestr = "/";
2984 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2985 848d6979 2019-08-12 stsp modestr = "*";
2986 848d6979 2019-08-12 stsp
2987 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2988 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2989 c1669e2e 2019-01-09 stsp }
2990 c1669e2e 2019-01-09 stsp
2991 5de5890b 2018-10-18 stsp static const struct got_error *
2992 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2993 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2994 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2995 5de5890b 2018-10-18 stsp {
2996 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2997 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2998 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2999 56e0773d 2019-11-28 stsp int nentries, i;
3000 5de5890b 2018-10-18 stsp
3001 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3002 5de5890b 2018-10-18 stsp if (err)
3003 5de5890b 2018-10-18 stsp goto done;
3004 5de5890b 2018-10-18 stsp
3005 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3006 5de5890b 2018-10-18 stsp if (err)
3007 5de5890b 2018-10-18 stsp goto done;
3008 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3009 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3010 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3011 5de5890b 2018-10-18 stsp char *id = NULL;
3012 84453469 2018-11-11 stsp
3013 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3014 84453469 2018-11-11 stsp break;
3015 84453469 2018-11-11 stsp
3016 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3017 5de5890b 2018-10-18 stsp if (show_ids) {
3018 5de5890b 2018-10-18 stsp char *id_str;
3019 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3020 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3021 5de5890b 2018-10-18 stsp if (err)
3022 5de5890b 2018-10-18 stsp goto done;
3023 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3024 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3025 5de5890b 2018-10-18 stsp free(id_str);
3026 5de5890b 2018-10-18 stsp goto done;
3027 5de5890b 2018-10-18 stsp }
3028 5de5890b 2018-10-18 stsp free(id_str);
3029 5de5890b 2018-10-18 stsp }
3030 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3031 5de5890b 2018-10-18 stsp free(id);
3032 c1669e2e 2019-01-09 stsp
3033 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3034 c1669e2e 2019-01-09 stsp char *child_path;
3035 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3036 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3037 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3038 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3039 c1669e2e 2019-01-09 stsp goto done;
3040 c1669e2e 2019-01-09 stsp }
3041 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3042 c1669e2e 2019-01-09 stsp root_path, repo);
3043 c1669e2e 2019-01-09 stsp free(child_path);
3044 c1669e2e 2019-01-09 stsp if (err)
3045 c1669e2e 2019-01-09 stsp goto done;
3046 c1669e2e 2019-01-09 stsp }
3047 5de5890b 2018-10-18 stsp }
3048 5de5890b 2018-10-18 stsp done:
3049 5de5890b 2018-10-18 stsp if (tree)
3050 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3051 5de5890b 2018-10-18 stsp free(tree_id);
3052 5de5890b 2018-10-18 stsp return err;
3053 404c43c4 2018-06-21 stsp }
3054 404c43c4 2018-06-21 stsp
3055 5de5890b 2018-10-18 stsp static const struct got_error *
3056 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3057 5de5890b 2018-10-18 stsp {
3058 5de5890b 2018-10-18 stsp const struct got_error *error;
3059 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3060 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3061 7a2c19d6 2019-02-05 stsp const char *path;
3062 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3063 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3064 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3065 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3066 5de5890b 2018-10-18 stsp int ch;
3067 5de5890b 2018-10-18 stsp
3068 5de5890b 2018-10-18 stsp #ifndef PROFILE
3069 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3070 0f8d269b 2019-01-04 stsp NULL) == -1)
3071 5de5890b 2018-10-18 stsp err(1, "pledge");
3072 5de5890b 2018-10-18 stsp #endif
3073 5de5890b 2018-10-18 stsp
3074 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3075 5de5890b 2018-10-18 stsp switch (ch) {
3076 5de5890b 2018-10-18 stsp case 'c':
3077 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3078 5de5890b 2018-10-18 stsp break;
3079 5de5890b 2018-10-18 stsp case 'r':
3080 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3081 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3082 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3083 9ba1d308 2019-10-21 stsp optarg);
3084 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3085 5de5890b 2018-10-18 stsp break;
3086 5de5890b 2018-10-18 stsp case 'i':
3087 5de5890b 2018-10-18 stsp show_ids = 1;
3088 5de5890b 2018-10-18 stsp break;
3089 c1669e2e 2019-01-09 stsp case 'R':
3090 c1669e2e 2019-01-09 stsp recurse = 1;
3091 c1669e2e 2019-01-09 stsp break;
3092 5de5890b 2018-10-18 stsp default:
3093 2deda0b9 2019-03-07 stsp usage_tree();
3094 5de5890b 2018-10-18 stsp /* NOTREACHED */
3095 5de5890b 2018-10-18 stsp }
3096 5de5890b 2018-10-18 stsp }
3097 5de5890b 2018-10-18 stsp
3098 5de5890b 2018-10-18 stsp argc -= optind;
3099 5de5890b 2018-10-18 stsp argv += optind;
3100 5de5890b 2018-10-18 stsp
3101 5de5890b 2018-10-18 stsp if (argc == 1)
3102 5de5890b 2018-10-18 stsp path = argv[0];
3103 5de5890b 2018-10-18 stsp else if (argc > 1)
3104 5de5890b 2018-10-18 stsp usage_tree();
3105 5de5890b 2018-10-18 stsp else
3106 7a2c19d6 2019-02-05 stsp path = NULL;
3107 7a2c19d6 2019-02-05 stsp
3108 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3109 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3110 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3111 9bf7a39b 2019-02-05 stsp goto done;
3112 9bf7a39b 2019-02-05 stsp }
3113 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3114 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3115 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3116 7a2c19d6 2019-02-05 stsp goto done;
3117 7a2c19d6 2019-02-05 stsp else
3118 7a2c19d6 2019-02-05 stsp error = NULL;
3119 7a2c19d6 2019-02-05 stsp if (worktree) {
3120 7a2c19d6 2019-02-05 stsp repo_path =
3121 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3122 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3123 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3124 7a2c19d6 2019-02-05 stsp if (error)
3125 7a2c19d6 2019-02-05 stsp goto done;
3126 7a2c19d6 2019-02-05 stsp } else {
3127 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3128 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3129 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3130 7a2c19d6 2019-02-05 stsp goto done;
3131 7a2c19d6 2019-02-05 stsp }
3132 5de5890b 2018-10-18 stsp }
3133 5de5890b 2018-10-18 stsp }
3134 5de5890b 2018-10-18 stsp
3135 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3136 5de5890b 2018-10-18 stsp if (error != NULL)
3137 c02c541e 2019-03-29 stsp goto done;
3138 c02c541e 2019-03-29 stsp
3139 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3140 c02c541e 2019-03-29 stsp if (error)
3141 5de5890b 2018-10-18 stsp goto done;
3142 5de5890b 2018-10-18 stsp
3143 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3144 9bf7a39b 2019-02-05 stsp if (worktree) {
3145 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3146 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3147 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3148 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3149 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3150 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3151 9bf7a39b 2019-02-05 stsp goto done;
3152 9bf7a39b 2019-02-05 stsp }
3153 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3154 9bf7a39b 2019-02-05 stsp free(p);
3155 9bf7a39b 2019-02-05 stsp if (error)
3156 9bf7a39b 2019-02-05 stsp goto done;
3157 9bf7a39b 2019-02-05 stsp } else
3158 9bf7a39b 2019-02-05 stsp path = "/";
3159 9bf7a39b 2019-02-05 stsp }
3160 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3161 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3162 9bf7a39b 2019-02-05 stsp if (error != NULL)
3163 9bf7a39b 2019-02-05 stsp goto done;
3164 9bf7a39b 2019-02-05 stsp }
3165 5de5890b 2018-10-18 stsp
3166 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3167 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3168 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3169 5de5890b 2018-10-18 stsp if (error != NULL)
3170 5de5890b 2018-10-18 stsp goto done;
3171 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3172 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3173 5de5890b 2018-10-18 stsp if (error != NULL)
3174 5de5890b 2018-10-18 stsp goto done;
3175 5de5890b 2018-10-18 stsp } else {
3176 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3177 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3178 30837e32 2019-07-25 stsp if (error)
3179 5de5890b 2018-10-18 stsp goto done;
3180 5de5890b 2018-10-18 stsp }
3181 5de5890b 2018-10-18 stsp
3182 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3183 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3184 5de5890b 2018-10-18 stsp done:
3185 5de5890b 2018-10-18 stsp free(in_repo_path);
3186 5de5890b 2018-10-18 stsp free(repo_path);
3187 5de5890b 2018-10-18 stsp free(cwd);
3188 5de5890b 2018-10-18 stsp free(commit_id);
3189 7a2c19d6 2019-02-05 stsp if (worktree)
3190 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3191 5de5890b 2018-10-18 stsp if (repo) {
3192 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3193 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3194 5de5890b 2018-10-18 stsp if (error == NULL)
3195 5de5890b 2018-10-18 stsp error = repo_error;
3196 5de5890b 2018-10-18 stsp }
3197 5de5890b 2018-10-18 stsp return error;
3198 5de5890b 2018-10-18 stsp }
3199 5de5890b 2018-10-18 stsp
3200 6bad629b 2019-02-04 stsp __dead static void
3201 6bad629b 2019-02-04 stsp usage_status(void)
3202 6bad629b 2019-02-04 stsp {
3203 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3204 6bad629b 2019-02-04 stsp exit(1);
3205 6bad629b 2019-02-04 stsp }
3206 5c860e29 2018-03-12 stsp
3207 b72f483a 2019-02-05 stsp static const struct got_error *
3208 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3209 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3210 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3211 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3212 6bad629b 2019-02-04 stsp {
3213 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3214 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3215 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3216 b72f483a 2019-02-05 stsp return NULL;
3217 6bad629b 2019-02-04 stsp }
3218 5c860e29 2018-03-12 stsp
3219 6bad629b 2019-02-04 stsp static const struct got_error *
3220 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3221 6bad629b 2019-02-04 stsp {
3222 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3223 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3224 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3225 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3226 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3227 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3228 a5edda0a 2019-07-27 stsp int ch;
3229 5c860e29 2018-03-12 stsp
3230 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3231 72ea6654 2019-07-27 stsp
3232 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3233 6bad629b 2019-02-04 stsp switch (ch) {
3234 5c860e29 2018-03-12 stsp default:
3235 2deda0b9 2019-03-07 stsp usage_status();
3236 6bad629b 2019-02-04 stsp /* NOTREACHED */
3237 5c860e29 2018-03-12 stsp }
3238 5c860e29 2018-03-12 stsp }
3239 5c860e29 2018-03-12 stsp
3240 6bad629b 2019-02-04 stsp argc -= optind;
3241 6bad629b 2019-02-04 stsp argv += optind;
3242 5c860e29 2018-03-12 stsp
3243 6bad629b 2019-02-04 stsp #ifndef PROFILE
3244 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3245 6bad629b 2019-02-04 stsp NULL) == -1)
3246 6bad629b 2019-02-04 stsp err(1, "pledge");
3247 f42b1b34 2018-03-12 stsp #endif
3248 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3249 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3250 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3251 927df6b7 2019-02-10 stsp goto done;
3252 927df6b7 2019-02-10 stsp }
3253 927df6b7 2019-02-10 stsp
3254 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3255 927df6b7 2019-02-10 stsp if (error != NULL)
3256 a5edda0a 2019-07-27 stsp goto done;
3257 6bad629b 2019-02-04 stsp
3258 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3259 c9956ddf 2019-09-08 stsp NULL);
3260 6bad629b 2019-02-04 stsp if (error != NULL)
3261 6bad629b 2019-02-04 stsp goto done;
3262 6bad629b 2019-02-04 stsp
3263 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3264 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3265 087fb88c 2019-08-04 stsp if (error)
3266 087fb88c 2019-08-04 stsp goto done;
3267 087fb88c 2019-08-04 stsp
3268 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3269 6bad629b 2019-02-04 stsp if (error)
3270 6bad629b 2019-02-04 stsp goto done;
3271 6bad629b 2019-02-04 stsp
3272 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3273 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3274 6bad629b 2019-02-04 stsp done:
3275 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3276 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3277 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3278 927df6b7 2019-02-10 stsp free(cwd);
3279 d0eebce4 2019-03-11 stsp return error;
3280 d0eebce4 2019-03-11 stsp }
3281 d0eebce4 2019-03-11 stsp
3282 d0eebce4 2019-03-11 stsp __dead static void
3283 d0eebce4 2019-03-11 stsp usage_ref(void)
3284 d0eebce4 2019-03-11 stsp {
3285 d0eebce4 2019-03-11 stsp fprintf(stderr,
3286 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3287 d0eebce4 2019-03-11 stsp getprogname());
3288 d0eebce4 2019-03-11 stsp exit(1);
3289 d0eebce4 2019-03-11 stsp }
3290 d0eebce4 2019-03-11 stsp
3291 d0eebce4 2019-03-11 stsp static const struct got_error *
3292 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3293 d0eebce4 2019-03-11 stsp {
3294 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3295 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3296 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3297 d0eebce4 2019-03-11 stsp
3298 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3299 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3300 d0eebce4 2019-03-11 stsp if (err)
3301 d0eebce4 2019-03-11 stsp return err;
3302 d0eebce4 2019-03-11 stsp
3303 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3304 d0eebce4 2019-03-11 stsp char *refstr;
3305 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3306 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3307 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3308 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3309 d0eebce4 2019-03-11 stsp free(refstr);
3310 d0eebce4 2019-03-11 stsp }
3311 d0eebce4 2019-03-11 stsp
3312 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3313 d0eebce4 2019-03-11 stsp return NULL;
3314 d0eebce4 2019-03-11 stsp }
3315 d0eebce4 2019-03-11 stsp
3316 d0eebce4 2019-03-11 stsp static const struct got_error *
3317 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3318 d0eebce4 2019-03-11 stsp {
3319 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3320 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3321 d0eebce4 2019-03-11 stsp
3322 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3323 d0eebce4 2019-03-11 stsp if (err)
3324 d0eebce4 2019-03-11 stsp return err;
3325 d0eebce4 2019-03-11 stsp
3326 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3327 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3328 d0eebce4 2019-03-11 stsp return err;
3329 d0eebce4 2019-03-11 stsp }
3330 d0eebce4 2019-03-11 stsp
3331 d0eebce4 2019-03-11 stsp static const struct got_error *
3332 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3333 d0eebce4 2019-03-11 stsp {
3334 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3335 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3336 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3337 d1644381 2019-07-14 stsp
3338 d1644381 2019-07-14 stsp /*
3339 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3340 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3341 d1644381 2019-07-14 stsp * an unintended typo.
3342 d1644381 2019-07-14 stsp */
3343 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3344 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3345 d0eebce4 2019-03-11 stsp
3346 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3347 dd88155e 2019-06-29 stsp repo);
3348 d83d9d5c 2019-05-13 stsp if (err) {
3349 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3350 d0eebce4 2019-03-11 stsp
3351 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3352 d83d9d5c 2019-05-13 stsp return err;
3353 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3354 d83d9d5c 2019-05-13 stsp if (err)
3355 d83d9d5c 2019-05-13 stsp return err;
3356 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3357 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3358 d83d9d5c 2019-05-13 stsp if (err)
3359 d83d9d5c 2019-05-13 stsp return err;
3360 d83d9d5c 2019-05-13 stsp }
3361 d83d9d5c 2019-05-13 stsp
3362 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3363 d0eebce4 2019-03-11 stsp if (err)
3364 d0eebce4 2019-03-11 stsp goto done;
3365 d0eebce4 2019-03-11 stsp
3366 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3367 d0eebce4 2019-03-11 stsp done:
3368 d0eebce4 2019-03-11 stsp if (ref)
3369 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3370 d0eebce4 2019-03-11 stsp free(id);
3371 d1c1ae5f 2019-08-12 stsp return err;
3372 d1c1ae5f 2019-08-12 stsp }
3373 d1c1ae5f 2019-08-12 stsp
3374 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3375 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3376 d1c1ae5f 2019-08-12 stsp {
3377 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3378 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3379 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3380 d1c1ae5f 2019-08-12 stsp
3381 d1c1ae5f 2019-08-12 stsp /*
3382 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3383 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3384 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3385 d1c1ae5f 2019-08-12 stsp */
3386 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3387 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3388 d1c1ae5f 2019-08-12 stsp
3389 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3390 d1c1ae5f 2019-08-12 stsp if (err)
3391 d1c1ae5f 2019-08-12 stsp return err;
3392 d1c1ae5f 2019-08-12 stsp
3393 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3394 d1c1ae5f 2019-08-12 stsp if (err)
3395 d1c1ae5f 2019-08-12 stsp goto done;
3396 d1c1ae5f 2019-08-12 stsp
3397 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3398 d1c1ae5f 2019-08-12 stsp done:
3399 d1c1ae5f 2019-08-12 stsp if (target_ref)
3400 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3401 d1c1ae5f 2019-08-12 stsp if (ref)
3402 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3403 d0eebce4 2019-03-11 stsp return err;
3404 d0eebce4 2019-03-11 stsp }
3405 d0eebce4 2019-03-11 stsp
3406 d0eebce4 2019-03-11 stsp static const struct got_error *
3407 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3408 d0eebce4 2019-03-11 stsp {
3409 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3410 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3411 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3412 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3413 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3414 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3415 d0eebce4 2019-03-11 stsp
3416 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3417 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3418 d0eebce4 2019-03-11 stsp switch (ch) {
3419 d0eebce4 2019-03-11 stsp case 'd':
3420 d0eebce4 2019-03-11 stsp delref = optarg;
3421 d0eebce4 2019-03-11 stsp break;
3422 d0eebce4 2019-03-11 stsp case 'r':
3423 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3424 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3425 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3426 9ba1d308 2019-10-21 stsp optarg);
3427 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3428 d0eebce4 2019-03-11 stsp break;
3429 d0eebce4 2019-03-11 stsp case 'l':
3430 d0eebce4 2019-03-11 stsp do_list = 1;
3431 d1c1ae5f 2019-08-12 stsp break;
3432 d1c1ae5f 2019-08-12 stsp case 's':
3433 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3434 d0eebce4 2019-03-11 stsp break;
3435 d0eebce4 2019-03-11 stsp default:
3436 d0eebce4 2019-03-11 stsp usage_ref();
3437 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3438 d0eebce4 2019-03-11 stsp }
3439 d0eebce4 2019-03-11 stsp }
3440 d0eebce4 2019-03-11 stsp
3441 d0eebce4 2019-03-11 stsp if (do_list && delref)
3442 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3443 d0eebce4 2019-03-11 stsp
3444 d0eebce4 2019-03-11 stsp argc -= optind;
3445 d0eebce4 2019-03-11 stsp argv += optind;
3446 d0eebce4 2019-03-11 stsp
3447 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3448 d1c1ae5f 2019-08-12 stsp if (create_symref)
3449 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3450 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3451 d0eebce4 2019-03-11 stsp if (argc > 0)
3452 d0eebce4 2019-03-11 stsp usage_ref();
3453 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3454 d0eebce4 2019-03-11 stsp usage_ref();
3455 d0eebce4 2019-03-11 stsp
3456 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3457 e0b57350 2019-03-12 stsp if (do_list) {
3458 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3459 e0b57350 2019-03-12 stsp NULL) == -1)
3460 e0b57350 2019-03-12 stsp err(1, "pledge");
3461 e0b57350 2019-03-12 stsp } else {
3462 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3463 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3464 e0b57350 2019-03-12 stsp err(1, "pledge");
3465 e0b57350 2019-03-12 stsp }
3466 d0eebce4 2019-03-11 stsp #endif
3467 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3468 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3469 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3470 d0eebce4 2019-03-11 stsp goto done;
3471 d0eebce4 2019-03-11 stsp }
3472 d0eebce4 2019-03-11 stsp
3473 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3474 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3475 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3476 d0eebce4 2019-03-11 stsp goto done;
3477 d0eebce4 2019-03-11 stsp else
3478 d0eebce4 2019-03-11 stsp error = NULL;
3479 d0eebce4 2019-03-11 stsp if (worktree) {
3480 d0eebce4 2019-03-11 stsp repo_path =
3481 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3482 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3483 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3484 d0eebce4 2019-03-11 stsp if (error)
3485 d0eebce4 2019-03-11 stsp goto done;
3486 d0eebce4 2019-03-11 stsp } else {
3487 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3488 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3489 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3490 d0eebce4 2019-03-11 stsp goto done;
3491 d0eebce4 2019-03-11 stsp }
3492 d0eebce4 2019-03-11 stsp }
3493 d0eebce4 2019-03-11 stsp }
3494 d0eebce4 2019-03-11 stsp
3495 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3496 d0eebce4 2019-03-11 stsp if (error != NULL)
3497 d0eebce4 2019-03-11 stsp goto done;
3498 d0eebce4 2019-03-11 stsp
3499 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3500 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3501 c02c541e 2019-03-29 stsp if (error)
3502 c02c541e 2019-03-29 stsp goto done;
3503 c02c541e 2019-03-29 stsp
3504 d0eebce4 2019-03-11 stsp if (do_list)
3505 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3506 d0eebce4 2019-03-11 stsp else if (delref)
3507 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3508 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3509 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3510 d0eebce4 2019-03-11 stsp else
3511 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3512 4e759de4 2019-06-26 stsp done:
3513 4e759de4 2019-06-26 stsp if (repo)
3514 4e759de4 2019-06-26 stsp got_repo_close(repo);
3515 4e759de4 2019-06-26 stsp if (worktree)
3516 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3517 4e759de4 2019-06-26 stsp free(cwd);
3518 4e759de4 2019-06-26 stsp free(repo_path);
3519 4e759de4 2019-06-26 stsp return error;
3520 4e759de4 2019-06-26 stsp }
3521 4e759de4 2019-06-26 stsp
3522 4e759de4 2019-06-26 stsp __dead static void
3523 4e759de4 2019-06-26 stsp usage_branch(void)
3524 4e759de4 2019-06-26 stsp {
3525 4e759de4 2019-06-26 stsp fprintf(stderr,
3526 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3527 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3528 4e759de4 2019-06-26 stsp exit(1);
3529 4e759de4 2019-06-26 stsp }
3530 4e759de4 2019-06-26 stsp
3531 4e759de4 2019-06-26 stsp static const struct got_error *
3532 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3533 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3534 4e99b47e 2019-10-04 stsp {
3535 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3536 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3537 4e99b47e 2019-10-04 stsp char *refstr;
3538 4e99b47e 2019-10-04 stsp
3539 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3540 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3541 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3542 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3543 4e99b47e 2019-10-04 stsp
3544 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3545 4e99b47e 2019-10-04 stsp if (err)
3546 4e99b47e 2019-10-04 stsp return err;
3547 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3548 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3549 4e99b47e 2019-10-04 stsp marker = "* ";
3550 4e99b47e 2019-10-04 stsp else
3551 4e99b47e 2019-10-04 stsp marker = "~ ";
3552 4e99b47e 2019-10-04 stsp free(id);
3553 4e99b47e 2019-10-04 stsp }
3554 4e99b47e 2019-10-04 stsp
3555 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3556 4e99b47e 2019-10-04 stsp refname += 11;
3557 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3558 4e99b47e 2019-10-04 stsp refname += 18;
3559 4e99b47e 2019-10-04 stsp
3560 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3561 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3562 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3563 4e99b47e 2019-10-04 stsp
3564 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3565 4e99b47e 2019-10-04 stsp free(refstr);
3566 4e99b47e 2019-10-04 stsp return NULL;
3567 4e99b47e 2019-10-04 stsp }
3568 4e99b47e 2019-10-04 stsp
3569 4e99b47e 2019-10-04 stsp static const struct got_error *
3570 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3571 ad89fa31 2019-10-04 stsp {
3572 ad89fa31 2019-10-04 stsp const char *refname;
3573 ad89fa31 2019-10-04 stsp
3574 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3575 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3576 ad89fa31 2019-10-04 stsp
3577 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3578 ad89fa31 2019-10-04 stsp
3579 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3580 ad89fa31 2019-10-04 stsp refname += 11;
3581 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3582 ad89fa31 2019-10-04 stsp refname += 18;
3583 ad89fa31 2019-10-04 stsp
3584 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3585 ad89fa31 2019-10-04 stsp
3586 ad89fa31 2019-10-04 stsp return NULL;
3587 ad89fa31 2019-10-04 stsp }
3588 ad89fa31 2019-10-04 stsp
3589 ad89fa31 2019-10-04 stsp static const struct got_error *
3590 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3591 4e759de4 2019-06-26 stsp {
3592 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3593 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3594 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3595 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3596 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3597 4e759de4 2019-06-26 stsp
3598 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3599 ba882ee3 2019-07-11 stsp
3600 4e99b47e 2019-10-04 stsp if (worktree) {
3601 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3602 4e99b47e 2019-10-04 stsp worktree);
3603 4e99b47e 2019-10-04 stsp if (err)
3604 4e99b47e 2019-10-04 stsp return err;
3605 4e99b47e 2019-10-04 stsp
3606 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3607 4e99b47e 2019-10-04 stsp worktree);
3608 4e99b47e 2019-10-04 stsp if (err)
3609 4e99b47e 2019-10-04 stsp return err;
3610 4e99b47e 2019-10-04 stsp
3611 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3612 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3613 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3614 4e99b47e 2019-10-04 stsp if (err)
3615 4e99b47e 2019-10-04 stsp return err;
3616 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3617 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3618 4e99b47e 2019-10-04 stsp }
3619 4e99b47e 2019-10-04 stsp }
3620 4e99b47e 2019-10-04 stsp
3621 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3622 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3623 4e759de4 2019-06-26 stsp if (err)
3624 4e759de4 2019-06-26 stsp return err;
3625 4e759de4 2019-06-26 stsp
3626 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3627 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3628 4e759de4 2019-06-26 stsp
3629 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3630 4e759de4 2019-06-26 stsp return NULL;
3631 4e759de4 2019-06-26 stsp }
3632 4e759de4 2019-06-26 stsp
3633 4e759de4 2019-06-26 stsp static const struct got_error *
3634 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3635 45cd4e47 2019-08-25 stsp const char *branch_name)
3636 4e759de4 2019-06-26 stsp {
3637 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3638 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3639 4e759de4 2019-06-26 stsp char *refname;
3640 4e759de4 2019-06-26 stsp
3641 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3642 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3643 4e759de4 2019-06-26 stsp
3644 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3645 4e759de4 2019-06-26 stsp if (err)
3646 4e759de4 2019-06-26 stsp goto done;
3647 4e759de4 2019-06-26 stsp
3648 45cd4e47 2019-08-25 stsp if (worktree &&
3649 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3650 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3651 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3652 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3653 45cd4e47 2019-08-25 stsp goto done;
3654 45cd4e47 2019-08-25 stsp }
3655 45cd4e47 2019-08-25 stsp
3656 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3657 4e759de4 2019-06-26 stsp done:
3658 45cd4e47 2019-08-25 stsp if (ref)
3659 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3660 4e759de4 2019-06-26 stsp free(refname);
3661 4e759de4 2019-06-26 stsp return err;
3662 4e759de4 2019-06-26 stsp }
3663 4e759de4 2019-06-26 stsp
3664 4e759de4 2019-06-26 stsp static const struct got_error *
3665 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3666 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3667 4e759de4 2019-06-26 stsp {
3668 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3669 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3670 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3671 d3f84d51 2019-07-11 stsp
3672 d3f84d51 2019-07-11 stsp /*
3673 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3674 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3675 d3f84d51 2019-07-11 stsp * an unintended typo.
3676 d3f84d51 2019-07-11 stsp */
3677 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3678 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3679 4e759de4 2019-06-26 stsp
3680 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3681 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3682 4e759de4 2019-06-26 stsp goto done;
3683 4e759de4 2019-06-26 stsp }
3684 4e759de4 2019-06-26 stsp
3685 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3686 4e759de4 2019-06-26 stsp if (err == NULL) {
3687 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3688 4e759de4 2019-06-26 stsp goto done;
3689 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3690 4e759de4 2019-06-26 stsp goto done;
3691 4e759de4 2019-06-26 stsp
3692 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3693 4e759de4 2019-06-26 stsp if (err)
3694 4e759de4 2019-06-26 stsp goto done;
3695 4e759de4 2019-06-26 stsp
3696 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3697 d0eebce4 2019-03-11 stsp done:
3698 4e759de4 2019-06-26 stsp if (ref)
3699 4e759de4 2019-06-26 stsp got_ref_close(ref);
3700 4e759de4 2019-06-26 stsp free(base_refname);
3701 4e759de4 2019-06-26 stsp free(refname);
3702 4e759de4 2019-06-26 stsp return err;
3703 4e759de4 2019-06-26 stsp }
3704 4e759de4 2019-06-26 stsp
3705 4e759de4 2019-06-26 stsp static const struct got_error *
3706 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3707 4e759de4 2019-06-26 stsp {
3708 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3709 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3710 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3711 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3712 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3713 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3714 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3715 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3716 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3717 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3718 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3719 4e759de4 2019-06-26 stsp
3720 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3721 da76fce2 2020-02-24 stsp
3722 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3723 4e759de4 2019-06-26 stsp switch (ch) {
3724 a74f7e83 2019-11-10 stsp case 'c':
3725 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3726 a74f7e83 2019-11-10 stsp break;
3727 4e759de4 2019-06-26 stsp case 'd':
3728 4e759de4 2019-06-26 stsp delref = optarg;
3729 4e759de4 2019-06-26 stsp break;
3730 4e759de4 2019-06-26 stsp case 'r':
3731 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3732 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3733 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3734 9ba1d308 2019-10-21 stsp optarg);
3735 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3736 4e759de4 2019-06-26 stsp break;
3737 4e759de4 2019-06-26 stsp case 'l':
3738 4e759de4 2019-06-26 stsp do_list = 1;
3739 da76fce2 2020-02-24 stsp break;
3740 da76fce2 2020-02-24 stsp case 'n':
3741 da76fce2 2020-02-24 stsp do_update = 0;
3742 4e759de4 2019-06-26 stsp break;
3743 4e759de4 2019-06-26 stsp default:
3744 4e759de4 2019-06-26 stsp usage_branch();
3745 4e759de4 2019-06-26 stsp /* NOTREACHED */
3746 4e759de4 2019-06-26 stsp }
3747 4e759de4 2019-06-26 stsp }
3748 4e759de4 2019-06-26 stsp
3749 4e759de4 2019-06-26 stsp if (do_list && delref)
3750 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3751 4e759de4 2019-06-26 stsp
3752 4e759de4 2019-06-26 stsp argc -= optind;
3753 4e759de4 2019-06-26 stsp argv += optind;
3754 ad89fa31 2019-10-04 stsp
3755 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3756 ad89fa31 2019-10-04 stsp do_show = 1;
3757 4e759de4 2019-06-26 stsp
3758 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3759 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3760 a74f7e83 2019-11-10 stsp
3761 4e759de4 2019-06-26 stsp if (do_list || delref) {
3762 4e759de4 2019-06-26 stsp if (argc > 0)
3763 4e759de4 2019-06-26 stsp usage_branch();
3764 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3765 4e759de4 2019-06-26 stsp usage_branch();
3766 4e759de4 2019-06-26 stsp
3767 4e759de4 2019-06-26 stsp #ifndef PROFILE
3768 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3769 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3770 4e759de4 2019-06-26 stsp NULL) == -1)
3771 4e759de4 2019-06-26 stsp err(1, "pledge");
3772 4e759de4 2019-06-26 stsp } else {
3773 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3774 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3775 4e759de4 2019-06-26 stsp err(1, "pledge");
3776 4e759de4 2019-06-26 stsp }
3777 4e759de4 2019-06-26 stsp #endif
3778 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3779 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3780 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3781 4e759de4 2019-06-26 stsp goto done;
3782 4e759de4 2019-06-26 stsp }
3783 4e759de4 2019-06-26 stsp
3784 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3785 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3786 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3787 4e759de4 2019-06-26 stsp goto done;
3788 4e759de4 2019-06-26 stsp else
3789 4e759de4 2019-06-26 stsp error = NULL;
3790 4e759de4 2019-06-26 stsp if (worktree) {
3791 4e759de4 2019-06-26 stsp repo_path =
3792 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3793 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3794 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3795 4e759de4 2019-06-26 stsp if (error)
3796 4e759de4 2019-06-26 stsp goto done;
3797 4e759de4 2019-06-26 stsp } else {
3798 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3799 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3800 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3801 4e759de4 2019-06-26 stsp goto done;
3802 4e759de4 2019-06-26 stsp }
3803 4e759de4 2019-06-26 stsp }
3804 4e759de4 2019-06-26 stsp }
3805 4e759de4 2019-06-26 stsp
3806 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3807 4e759de4 2019-06-26 stsp if (error != NULL)
3808 4e759de4 2019-06-26 stsp goto done;
3809 4e759de4 2019-06-26 stsp
3810 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3811 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3812 4e759de4 2019-06-26 stsp if (error)
3813 4e759de4 2019-06-26 stsp goto done;
3814 4e759de4 2019-06-26 stsp
3815 ad89fa31 2019-10-04 stsp if (do_show)
3816 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3817 ad89fa31 2019-10-04 stsp else if (do_list)
3818 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3819 4e759de4 2019-06-26 stsp else if (delref)
3820 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3821 4e759de4 2019-06-26 stsp else {
3822 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3823 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3824 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3825 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3826 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3827 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3828 a74f7e83 2019-11-10 stsp if (error)
3829 a74f7e83 2019-11-10 stsp goto done;
3830 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3831 da76fce2 2020-02-24 stsp if (error)
3832 da76fce2 2020-02-24 stsp goto done;
3833 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3834 da76fce2 2020-02-24 stsp int did_something = 0;
3835 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3836 da76fce2 2020-02-24 stsp
3837 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3838 da76fce2 2020-02-24 stsp if (error)
3839 da76fce2 2020-02-24 stsp goto done;
3840 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3841 da76fce2 2020-02-24 stsp worktree);
3842 da76fce2 2020-02-24 stsp if (error)
3843 da76fce2 2020-02-24 stsp goto done;
3844 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3845 da76fce2 2020-02-24 stsp == -1) {
3846 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3847 da76fce2 2020-02-24 stsp goto done;
3848 da76fce2 2020-02-24 stsp }
3849 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3850 da76fce2 2020-02-24 stsp free(branch_refname);
3851 da76fce2 2020-02-24 stsp if (error)
3852 da76fce2 2020-02-24 stsp goto done;
3853 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3854 da76fce2 2020-02-24 stsp repo);
3855 da76fce2 2020-02-24 stsp if (error)
3856 da76fce2 2020-02-24 stsp goto done;
3857 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3858 da76fce2 2020-02-24 stsp commit_id);
3859 da76fce2 2020-02-24 stsp if (error)
3860 da76fce2 2020-02-24 stsp goto done;
3861 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3862 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3863 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3864 da76fce2 2020-02-24 stsp if (error)
3865 da76fce2 2020-02-24 stsp goto done;
3866 da76fce2 2020-02-24 stsp if (did_something)
3867 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3868 da76fce2 2020-02-24 stsp }
3869 4e759de4 2019-06-26 stsp }
3870 4e759de4 2019-06-26 stsp done:
3871 da76fce2 2020-02-24 stsp if (ref)
3872 da76fce2 2020-02-24 stsp got_ref_close(ref);
3873 d0eebce4 2019-03-11 stsp if (repo)
3874 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3875 d0eebce4 2019-03-11 stsp if (worktree)
3876 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3877 d0eebce4 2019-03-11 stsp free(cwd);
3878 d0eebce4 2019-03-11 stsp free(repo_path);
3879 da76fce2 2020-02-24 stsp free(commit_id);
3880 da76fce2 2020-02-24 stsp free(commit_id_str);
3881 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3882 da76fce2 2020-02-24 stsp free((char *)pe->path);
3883 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3884 d00136be 2019-03-26 stsp return error;
3885 d00136be 2019-03-26 stsp }
3886 d00136be 2019-03-26 stsp
3887 8e7bd50a 2019-08-22 stsp
3888 d00136be 2019-03-26 stsp __dead static void
3889 8e7bd50a 2019-08-22 stsp usage_tag(void)
3890 8e7bd50a 2019-08-22 stsp {
3891 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3892 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3893 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3894 8e7bd50a 2019-08-22 stsp exit(1);
3895 b8bad2ba 2019-08-23 stsp }
3896 b8bad2ba 2019-08-23 stsp
3897 b8bad2ba 2019-08-23 stsp #if 0
3898 b8bad2ba 2019-08-23 stsp static const struct got_error *
3899 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3900 b8bad2ba 2019-08-23 stsp {
3901 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3902 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3903 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3904 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3905 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3906 b8bad2ba 2019-08-23 stsp
3907 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3908 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3909 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3910 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3911 b8bad2ba 2019-08-23 stsp if (err)
3912 b8bad2ba 2019-08-23 stsp return err;
3913 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3914 b8bad2ba 2019-08-23 stsp continue;
3915 b8bad2ba 2019-08-23 stsp } else {
3916 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3917 b8bad2ba 2019-08-23 stsp if (err)
3918 b8bad2ba 2019-08-23 stsp break;
3919 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3920 b8bad2ba 2019-08-23 stsp free(re_id);
3921 b8bad2ba 2019-08-23 stsp if (err)
3922 b8bad2ba 2019-08-23 stsp break;
3923 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3924 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3925 b8bad2ba 2019-08-23 stsp }
3926 b8bad2ba 2019-08-23 stsp
3927 b8bad2ba 2019-08-23 stsp while (se) {
3928 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3929 b8bad2ba 2019-08-23 stsp if (err)
3930 b8bad2ba 2019-08-23 stsp break;
3931 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3932 b8bad2ba 2019-08-23 stsp free(se_id);
3933 b8bad2ba 2019-08-23 stsp if (err)
3934 b8bad2ba 2019-08-23 stsp break;
3935 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3936 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3937 b8bad2ba 2019-08-23 stsp
3938 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3939 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3940 b8bad2ba 2019-08-23 stsp if (err)
3941 b8bad2ba 2019-08-23 stsp return err;
3942 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3943 b8bad2ba 2019-08-23 stsp break;
3944 b8bad2ba 2019-08-23 stsp }
3945 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3946 b8bad2ba 2019-08-23 stsp continue;
3947 b8bad2ba 2019-08-23 stsp }
3948 b8bad2ba 2019-08-23 stsp }
3949 b8bad2ba 2019-08-23 stsp done:
3950 b8bad2ba 2019-08-23 stsp return err;
3951 8e7bd50a 2019-08-22 stsp }
3952 b8bad2ba 2019-08-23 stsp #endif
3953 b8bad2ba 2019-08-23 stsp
3954 b8bad2ba 2019-08-23 stsp static const struct got_error *
3955 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3956 8e7bd50a 2019-08-22 stsp {
3957 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3958 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3959 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3960 8e7bd50a 2019-08-22 stsp
3961 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3962 8e7bd50a 2019-08-22 stsp
3963 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3964 8e7bd50a 2019-08-22 stsp if (err)
3965 8e7bd50a 2019-08-22 stsp return err;
3966 8e7bd50a 2019-08-22 stsp
3967 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3968 8e7bd50a 2019-08-22 stsp const char *refname;
3969 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3970 8e7bd50a 2019-08-22 stsp char datebuf[26];
3971 d4efa91b 2020-01-14 stsp const char *tagger;
3972 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3973 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3974 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3975 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3976 8e7bd50a 2019-08-22 stsp
3977 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3978 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3979 8e7bd50a 2019-08-22 stsp continue;
3980 8e7bd50a 2019-08-22 stsp refname += 10;
3981 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3982 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3983 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3984 8e7bd50a 2019-08-22 stsp break;
3985 8e7bd50a 2019-08-22 stsp }
3986 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3987 8e7bd50a 2019-08-22 stsp free(refstr);
3988 8e7bd50a 2019-08-22 stsp
3989 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3990 8e7bd50a 2019-08-22 stsp if (err)
3991 8e7bd50a 2019-08-22 stsp break;
3992 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3993 d4efa91b 2020-01-14 stsp if (err) {
3994 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3995 d4efa91b 2020-01-14 stsp free(id);
3996 d4efa91b 2020-01-14 stsp break;
3997 d4efa91b 2020-01-14 stsp }
3998 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3999 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4000 d4efa91b 2020-01-14 stsp if (err) {
4001 d4efa91b 2020-01-14 stsp free(id);
4002 d4efa91b 2020-01-14 stsp break;
4003 d4efa91b 2020-01-14 stsp }
4004 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4005 d4efa91b 2020-01-14 stsp tagger_time =
4006 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4007 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4008 d4efa91b 2020-01-14 stsp free(id);
4009 d4efa91b 2020-01-14 stsp if (err)
4010 d4efa91b 2020-01-14 stsp break;
4011 d4efa91b 2020-01-14 stsp } else {
4012 d4efa91b 2020-01-14 stsp free(id);
4013 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4014 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4015 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4016 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4017 d4efa91b 2020-01-14 stsp if (err)
4018 d4efa91b 2020-01-14 stsp break;
4019 d4efa91b 2020-01-14 stsp }
4020 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4021 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4022 2417344c 2019-08-23 stsp if (datestr)
4023 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4024 d4efa91b 2020-01-14 stsp if (commit)
4025 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4026 d4efa91b 2020-01-14 stsp else {
4027 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4028 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4029 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4030 d4efa91b 2020-01-14 stsp id_str);
4031 d4efa91b 2020-01-14 stsp break;
4032 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4033 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4034 d4efa91b 2020-01-14 stsp id_str);
4035 d4efa91b 2020-01-14 stsp break;
4036 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4037 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4038 d4efa91b 2020-01-14 stsp id_str);
4039 d4efa91b 2020-01-14 stsp break;
4040 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4041 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4042 d4efa91b 2020-01-14 stsp id_str);
4043 d4efa91b 2020-01-14 stsp break;
4044 d4efa91b 2020-01-14 stsp default:
4045 d4efa91b 2020-01-14 stsp break;
4046 d4efa91b 2020-01-14 stsp }
4047 8e7bd50a 2019-08-22 stsp }
4048 8e7bd50a 2019-08-22 stsp free(id_str);
4049 d4efa91b 2020-01-14 stsp if (commit) {
4050 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4051 d4efa91b 2020-01-14 stsp if (err)
4052 d4efa91b 2020-01-14 stsp break;
4053 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4054 d4efa91b 2020-01-14 stsp } else {
4055 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4056 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4057 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4058 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4059 d4efa91b 2020-01-14 stsp break;
4060 d4efa91b 2020-01-14 stsp }
4061 8e7bd50a 2019-08-22 stsp }
4062 8e7bd50a 2019-08-22 stsp
4063 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4064 8e7bd50a 2019-08-22 stsp do {
4065 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4066 8e7bd50a 2019-08-22 stsp if (line)
4067 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4068 8e7bd50a 2019-08-22 stsp } while (line);
4069 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4070 8e7bd50a 2019-08-22 stsp }
4071 8e7bd50a 2019-08-22 stsp
4072 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4073 8e7bd50a 2019-08-22 stsp return NULL;
4074 8e7bd50a 2019-08-22 stsp }
4075 8e7bd50a 2019-08-22 stsp
4076 8e7bd50a 2019-08-22 stsp static const struct got_error *
4077 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4078 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4079 8e7bd50a 2019-08-22 stsp {
4080 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4081 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4082 f372d5cd 2019-10-21 stsp char *editor = NULL;
4083 8e7bd50a 2019-08-22 stsp int fd = -1;
4084 8e7bd50a 2019-08-22 stsp
4085 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4086 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4087 8e7bd50a 2019-08-22 stsp goto done;
4088 8e7bd50a 2019-08-22 stsp }
4089 8e7bd50a 2019-08-22 stsp
4090 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4091 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4092 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4093 8e7bd50a 2019-08-22 stsp goto done;
4094 8e7bd50a 2019-08-22 stsp }
4095 8e7bd50a 2019-08-22 stsp
4096 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4097 8e7bd50a 2019-08-22 stsp if (err)
4098 8e7bd50a 2019-08-22 stsp goto done;
4099 8e7bd50a 2019-08-22 stsp
4100 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4101 8e7bd50a 2019-08-22 stsp close(fd);
4102 8e7bd50a 2019-08-22 stsp
4103 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4104 8e7bd50a 2019-08-22 stsp if (err)
4105 8e7bd50a 2019-08-22 stsp goto done;
4106 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4107 8e7bd50a 2019-08-22 stsp done:
4108 8e7bd50a 2019-08-22 stsp free(initial_content);
4109 8e7bd50a 2019-08-22 stsp free(template);
4110 8e7bd50a 2019-08-22 stsp free(editor);
4111 8e7bd50a 2019-08-22 stsp
4112 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4113 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4114 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4115 8e7bd50a 2019-08-22 stsp if (err) {
4116 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4117 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4118 8e7bd50a 2019-08-22 stsp }
4119 8e7bd50a 2019-08-22 stsp }
4120 8e7bd50a 2019-08-22 stsp return err;
4121 8e7bd50a 2019-08-22 stsp }
4122 8e7bd50a 2019-08-22 stsp
4123 8e7bd50a 2019-08-22 stsp static const struct got_error *
4124 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4125 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4126 8e7bd50a 2019-08-22 stsp {
4127 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4128 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4129 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4130 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4131 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4132 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4133 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4134 8e7bd50a 2019-08-22 stsp
4135 8e7bd50a 2019-08-22 stsp /*
4136 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4137 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4138 8e7bd50a 2019-08-22 stsp * an unintended typo.
4139 8e7bd50a 2019-08-22 stsp */
4140 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4141 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4142 8e7bd50a 2019-08-22 stsp
4143 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4144 8e7bd50a 2019-08-22 stsp if (err)
4145 8e7bd50a 2019-08-22 stsp return err;
4146 8e7bd50a 2019-08-22 stsp
4147 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4148 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4149 8e7bd50a 2019-08-22 stsp if (err)
4150 8e7bd50a 2019-08-22 stsp goto done;
4151 8e7bd50a 2019-08-22 stsp
4152 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4153 8e7bd50a 2019-08-22 stsp if (err)
4154 8e7bd50a 2019-08-22 stsp goto done;
4155 8e7bd50a 2019-08-22 stsp
4156 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4157 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4158 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4159 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4160 8e7bd50a 2019-08-22 stsp goto done;
4161 8e7bd50a 2019-08-22 stsp }
4162 8e7bd50a 2019-08-22 stsp tag_name += 10;
4163 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4164 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4165 8e7bd50a 2019-08-22 stsp goto done;
4166 8e7bd50a 2019-08-22 stsp }
4167 8e7bd50a 2019-08-22 stsp
4168 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4169 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4170 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4171 8e7bd50a 2019-08-22 stsp goto done;
4172 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4173 8e7bd50a 2019-08-22 stsp goto done;
4174 8e7bd50a 2019-08-22 stsp
4175 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4176 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4177 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4178 f372d5cd 2019-10-21 stsp if (err) {
4179 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4180 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4181 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4182 8e7bd50a 2019-08-22 stsp goto done;
4183 f372d5cd 2019-10-21 stsp }
4184 8e7bd50a 2019-08-22 stsp }
4185 8e7bd50a 2019-08-22 stsp
4186 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4187 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4188 f372d5cd 2019-10-21 stsp if (err) {
4189 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4190 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4191 8e7bd50a 2019-08-22 stsp goto done;
4192 f372d5cd 2019-10-21 stsp }
4193 8e7bd50a 2019-08-22 stsp
4194 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4195 f372d5cd 2019-10-21 stsp if (err) {
4196 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4197 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4198 8e7bd50a 2019-08-22 stsp goto done;
4199 f372d5cd 2019-10-21 stsp }
4200 8e7bd50a 2019-08-22 stsp
4201 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4202 f372d5cd 2019-10-21 stsp if (err) {
4203 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4204 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4205 f372d5cd 2019-10-21 stsp goto done;
4206 f372d5cd 2019-10-21 stsp }
4207 8e7bd50a 2019-08-22 stsp
4208 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4209 f372d5cd 2019-10-21 stsp if (err) {
4210 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4211 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4212 f372d5cd 2019-10-21 stsp goto done;
4213 8e7bd50a 2019-08-22 stsp }
4214 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4215 8e7bd50a 2019-08-22 stsp done:
4216 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4217 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4218 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4219 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4220 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4221 f372d5cd 2019-10-21 stsp free(tag_id_str);
4222 8e7bd50a 2019-08-22 stsp if (ref)
4223 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4224 8e7bd50a 2019-08-22 stsp free(commit_id);
4225 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4226 8e7bd50a 2019-08-22 stsp free(refname);
4227 8e7bd50a 2019-08-22 stsp free(tagmsg);
4228 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4229 aba9c984 2019-09-08 stsp free(tagger);
4230 8e7bd50a 2019-08-22 stsp return err;
4231 8e7bd50a 2019-08-22 stsp }
4232 8e7bd50a 2019-08-22 stsp
4233 8e7bd50a 2019-08-22 stsp static const struct got_error *
4234 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4235 8e7bd50a 2019-08-22 stsp {
4236 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4237 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4238 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4239 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4240 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4241 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4242 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4243 8e7bd50a 2019-08-22 stsp
4244 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4245 8e7bd50a 2019-08-22 stsp switch (ch) {
4246 80106605 2020-02-24 stsp case 'c':
4247 80106605 2020-02-24 stsp commit_id_arg = optarg;
4248 80106605 2020-02-24 stsp break;
4249 8e7bd50a 2019-08-22 stsp case 'm':
4250 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4251 8e7bd50a 2019-08-22 stsp break;
4252 8e7bd50a 2019-08-22 stsp case 'r':
4253 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4254 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4255 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4256 9ba1d308 2019-10-21 stsp optarg);
4257 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4258 8e7bd50a 2019-08-22 stsp break;
4259 8e7bd50a 2019-08-22 stsp case 'l':
4260 8e7bd50a 2019-08-22 stsp do_list = 1;
4261 8e7bd50a 2019-08-22 stsp break;
4262 8e7bd50a 2019-08-22 stsp default:
4263 8e7bd50a 2019-08-22 stsp usage_tag();
4264 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4265 8e7bd50a 2019-08-22 stsp }
4266 8e7bd50a 2019-08-22 stsp }
4267 8e7bd50a 2019-08-22 stsp
4268 8e7bd50a 2019-08-22 stsp argc -= optind;
4269 8e7bd50a 2019-08-22 stsp argv += optind;
4270 8e7bd50a 2019-08-22 stsp
4271 8e7bd50a 2019-08-22 stsp if (do_list) {
4272 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4273 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4274 8e7bd50a 2019-08-22 stsp if (tagmsg)
4275 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4276 8e7bd50a 2019-08-22 stsp if (argc > 0)
4277 8e7bd50a 2019-08-22 stsp usage_tag();
4278 80106605 2020-02-24 stsp } else if (argc != 1)
4279 8e7bd50a 2019-08-22 stsp usage_tag();
4280 80106605 2020-02-24 stsp
4281 a2887370 2019-08-23 stsp tag_name = argv[0];
4282 8e7bd50a 2019-08-22 stsp
4283 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4284 8e7bd50a 2019-08-22 stsp if (do_list) {
4285 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4286 8e7bd50a 2019-08-22 stsp NULL) == -1)
4287 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4288 8e7bd50a 2019-08-22 stsp } else {
4289 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4290 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4291 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4292 8e7bd50a 2019-08-22 stsp }
4293 8e7bd50a 2019-08-22 stsp #endif
4294 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4295 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4296 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4297 8e7bd50a 2019-08-22 stsp goto done;
4298 8e7bd50a 2019-08-22 stsp }
4299 8e7bd50a 2019-08-22 stsp
4300 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4301 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4302 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4303 8e7bd50a 2019-08-22 stsp goto done;
4304 8e7bd50a 2019-08-22 stsp else
4305 8e7bd50a 2019-08-22 stsp error = NULL;
4306 8e7bd50a 2019-08-22 stsp if (worktree) {
4307 8e7bd50a 2019-08-22 stsp repo_path =
4308 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4309 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4310 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4311 8e7bd50a 2019-08-22 stsp if (error)
4312 8e7bd50a 2019-08-22 stsp goto done;
4313 8e7bd50a 2019-08-22 stsp } else {
4314 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4315 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4316 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4317 8e7bd50a 2019-08-22 stsp goto done;
4318 8e7bd50a 2019-08-22 stsp }
4319 8e7bd50a 2019-08-22 stsp }
4320 8e7bd50a 2019-08-22 stsp }
4321 8e7bd50a 2019-08-22 stsp
4322 8e7bd50a 2019-08-22 stsp if (do_list) {
4323 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4324 c9956ddf 2019-09-08 stsp if (error != NULL)
4325 c9956ddf 2019-09-08 stsp goto done;
4326 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4327 8e7bd50a 2019-08-22 stsp if (error)
4328 8e7bd50a 2019-08-22 stsp goto done;
4329 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4330 8e7bd50a 2019-08-22 stsp } else {
4331 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4332 c9956ddf 2019-09-08 stsp if (error)
4333 c9956ddf 2019-09-08 stsp goto done;
4334 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4335 c9956ddf 2019-09-08 stsp if (error != NULL)
4336 c9956ddf 2019-09-08 stsp goto done;
4337 c9956ddf 2019-09-08 stsp
4338 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4339 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4340 8e7bd50a 2019-08-22 stsp if (error)
4341 8e7bd50a 2019-08-22 stsp goto done;
4342 8e7bd50a 2019-08-22 stsp }
4343 8e7bd50a 2019-08-22 stsp
4344 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4345 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4346 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4347 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4348 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4349 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4350 8e7bd50a 2019-08-22 stsp if (error)
4351 8e7bd50a 2019-08-22 stsp goto done;
4352 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4353 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4354 8e7bd50a 2019-08-22 stsp if (error)
4355 8e7bd50a 2019-08-22 stsp goto done;
4356 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4357 8e7bd50a 2019-08-22 stsp free(commit_id);
4358 8e7bd50a 2019-08-22 stsp if (error)
4359 8e7bd50a 2019-08-22 stsp goto done;
4360 8e7bd50a 2019-08-22 stsp }
4361 8e7bd50a 2019-08-22 stsp
4362 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4363 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4364 8e7bd50a 2019-08-22 stsp }
4365 8e7bd50a 2019-08-22 stsp done:
4366 8e7bd50a 2019-08-22 stsp if (repo)
4367 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4368 8e7bd50a 2019-08-22 stsp if (worktree)
4369 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4370 8e7bd50a 2019-08-22 stsp free(cwd);
4371 8e7bd50a 2019-08-22 stsp free(repo_path);
4372 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4373 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4374 8e7bd50a 2019-08-22 stsp return error;
4375 8e7bd50a 2019-08-22 stsp }
4376 8e7bd50a 2019-08-22 stsp
4377 8e7bd50a 2019-08-22 stsp __dead static void
4378 d00136be 2019-03-26 stsp usage_add(void)
4379 d00136be 2019-03-26 stsp {
4380 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4381 022fae89 2019-12-06 tracey getprogname());
4382 d00136be 2019-03-26 stsp exit(1);
4383 d00136be 2019-03-26 stsp }
4384 d00136be 2019-03-26 stsp
4385 d00136be 2019-03-26 stsp static const struct got_error *
4386 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4387 4e68cba3 2019-11-23 stsp {
4388 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4389 4e68cba3 2019-11-23 stsp path++;
4390 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4391 4e68cba3 2019-11-23 stsp return NULL;
4392 4e68cba3 2019-11-23 stsp }
4393 4e68cba3 2019-11-23 stsp
4394 4e68cba3 2019-11-23 stsp static const struct got_error *
4395 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4396 d00136be 2019-03-26 stsp {
4397 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4398 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4399 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4400 1dd54920 2019-05-11 stsp char *cwd = NULL;
4401 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4402 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4403 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4404 1dd54920 2019-05-11 stsp
4405 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4406 d00136be 2019-03-26 stsp
4407 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4408 d00136be 2019-03-26 stsp switch (ch) {
4409 022fae89 2019-12-06 tracey case 'I':
4410 022fae89 2019-12-06 tracey no_ignores = 1;
4411 022fae89 2019-12-06 tracey break;
4412 4e68cba3 2019-11-23 stsp case 'R':
4413 4e68cba3 2019-11-23 stsp can_recurse = 1;
4414 4e68cba3 2019-11-23 stsp break;
4415 d00136be 2019-03-26 stsp default:
4416 d00136be 2019-03-26 stsp usage_add();
4417 d00136be 2019-03-26 stsp /* NOTREACHED */
4418 d00136be 2019-03-26 stsp }
4419 d00136be 2019-03-26 stsp }
4420 d00136be 2019-03-26 stsp
4421 d00136be 2019-03-26 stsp argc -= optind;
4422 d00136be 2019-03-26 stsp argv += optind;
4423 d00136be 2019-03-26 stsp
4424 43012d58 2019-07-14 stsp #ifndef PROFILE
4425 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4426 43012d58 2019-07-14 stsp NULL) == -1)
4427 43012d58 2019-07-14 stsp err(1, "pledge");
4428 43012d58 2019-07-14 stsp #endif
4429 723c305c 2019-05-11 jcs if (argc < 1)
4430 d00136be 2019-03-26 stsp usage_add();
4431 d00136be 2019-03-26 stsp
4432 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4433 d00136be 2019-03-26 stsp if (cwd == NULL) {
4434 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4435 d00136be 2019-03-26 stsp goto done;
4436 d00136be 2019-03-26 stsp }
4437 723c305c 2019-05-11 jcs
4438 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4439 d00136be 2019-03-26 stsp if (error)
4440 d00136be 2019-03-26 stsp goto done;
4441 d00136be 2019-03-26 stsp
4442 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4443 c9956ddf 2019-09-08 stsp NULL);
4444 031a5338 2019-03-26 stsp if (error != NULL)
4445 031a5338 2019-03-26 stsp goto done;
4446 031a5338 2019-03-26 stsp
4447 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4448 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4449 d00136be 2019-03-26 stsp if (error)
4450 d00136be 2019-03-26 stsp goto done;
4451 d00136be 2019-03-26 stsp
4452 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4453 6d022e97 2019-08-04 stsp if (error)
4454 022fae89 2019-12-06 tracey goto done;
4455 022fae89 2019-12-06 tracey
4456 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4457 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4458 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4459 6d022e97 2019-08-04 stsp goto done;
4460 723c305c 2019-05-11 jcs
4461 022fae89 2019-12-06 tracey }
4462 022fae89 2019-12-06 tracey
4463 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4464 4e68cba3 2019-11-23 stsp char *ondisk_path;
4465 4e68cba3 2019-11-23 stsp struct stat sb;
4466 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4467 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4468 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4469 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4470 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4471 4e68cba3 2019-11-23 stsp goto done;
4472 4e68cba3 2019-11-23 stsp }
4473 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4474 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4475 4e68cba3 2019-11-23 stsp free(ondisk_path);
4476 4e68cba3 2019-11-23 stsp continue;
4477 4e68cba3 2019-11-23 stsp }
4478 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4479 4e68cba3 2019-11-23 stsp ondisk_path);
4480 4e68cba3 2019-11-23 stsp free(ondisk_path);
4481 4e68cba3 2019-11-23 stsp goto done;
4482 4e68cba3 2019-11-23 stsp }
4483 4e68cba3 2019-11-23 stsp free(ondisk_path);
4484 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4485 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4486 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4487 4e68cba3 2019-11-23 stsp goto done;
4488 4e68cba3 2019-11-23 stsp }
4489 4e68cba3 2019-11-23 stsp }
4490 4e68cba3 2019-11-23 stsp }
4491 022fae89 2019-12-06 tracey
4492 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4493 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4494 d00136be 2019-03-26 stsp done:
4495 031a5338 2019-03-26 stsp if (repo)
4496 031a5338 2019-03-26 stsp got_repo_close(repo);
4497 d00136be 2019-03-26 stsp if (worktree)
4498 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4499 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4500 1dd54920 2019-05-11 stsp free((char *)pe->path);
4501 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4502 2ec1f75b 2019-03-26 stsp free(cwd);
4503 2ec1f75b 2019-03-26 stsp return error;
4504 2ec1f75b 2019-03-26 stsp }
4505 2ec1f75b 2019-03-26 stsp
4506 2ec1f75b 2019-03-26 stsp __dead static void
4507 648e4ef7 2019-07-09 stsp usage_remove(void)
4508 2ec1f75b 2019-03-26 stsp {
4509 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4510 f2a9dc41 2019-12-13 tracey getprogname());
4511 2ec1f75b 2019-03-26 stsp exit(1);
4512 2a06fe5f 2019-08-24 stsp }
4513 2a06fe5f 2019-08-24 stsp
4514 2a06fe5f 2019-08-24 stsp static const struct got_error *
4515 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4516 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4517 2a06fe5f 2019-08-24 stsp {
4518 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4519 f2a9dc41 2019-12-13 tracey path++;
4520 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4521 2a06fe5f 2019-08-24 stsp return NULL;
4522 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4523 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4524 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4525 2a06fe5f 2019-08-24 stsp return NULL;
4526 2ec1f75b 2019-03-26 stsp }
4527 2ec1f75b 2019-03-26 stsp
4528 2ec1f75b 2019-03-26 stsp static const struct got_error *
4529 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4530 2ec1f75b 2019-03-26 stsp {
4531 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4532 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4533 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4534 17ed4618 2019-06-02 stsp char *cwd = NULL;
4535 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4536 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4537 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4538 2ec1f75b 2019-03-26 stsp
4539 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4540 17ed4618 2019-06-02 stsp
4541 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4542 2ec1f75b 2019-03-26 stsp switch (ch) {
4543 2ec1f75b 2019-03-26 stsp case 'f':
4544 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4545 2ec1f75b 2019-03-26 stsp break;
4546 70e3e7f5 2019-12-13 tracey case 'k':
4547 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4548 70e3e7f5 2019-12-13 tracey break;
4549 f2a9dc41 2019-12-13 tracey case 'R':
4550 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4551 f2a9dc41 2019-12-13 tracey break;
4552 2ec1f75b 2019-03-26 stsp default:
4553 f2a9dc41 2019-12-13 tracey usage_remove();
4554 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4555 2ec1f75b 2019-03-26 stsp }
4556 2ec1f75b 2019-03-26 stsp }
4557 2ec1f75b 2019-03-26 stsp
4558 2ec1f75b 2019-03-26 stsp argc -= optind;
4559 2ec1f75b 2019-03-26 stsp argv += optind;
4560 2ec1f75b 2019-03-26 stsp
4561 43012d58 2019-07-14 stsp #ifndef PROFILE
4562 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4563 43012d58 2019-07-14 stsp NULL) == -1)
4564 43012d58 2019-07-14 stsp err(1, "pledge");
4565 43012d58 2019-07-14 stsp #endif
4566 17ed4618 2019-06-02 stsp if (argc < 1)
4567 648e4ef7 2019-07-09 stsp usage_remove();
4568 2ec1f75b 2019-03-26 stsp
4569 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4570 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4571 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4572 2ec1f75b 2019-03-26 stsp goto done;
4573 2ec1f75b 2019-03-26 stsp }
4574 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4575 2ec1f75b 2019-03-26 stsp if (error)
4576 2ec1f75b 2019-03-26 stsp goto done;
4577 2ec1f75b 2019-03-26 stsp
4578 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4579 c9956ddf 2019-09-08 stsp NULL);
4580 2af4a041 2019-05-11 jcs if (error)
4581 2ec1f75b 2019-03-26 stsp goto done;
4582 2ec1f75b 2019-03-26 stsp
4583 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4584 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4585 2ec1f75b 2019-03-26 stsp if (error)
4586 2ec1f75b 2019-03-26 stsp goto done;
4587 2ec1f75b 2019-03-26 stsp
4588 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4589 6d022e97 2019-08-04 stsp if (error)
4590 6d022e97 2019-08-04 stsp goto done;
4591 17ed4618 2019-06-02 stsp
4592 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4593 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4594 f2a9dc41 2019-12-13 tracey struct stat sb;
4595 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4596 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4597 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4598 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4599 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4600 f2a9dc41 2019-12-13 tracey goto done;
4601 f2a9dc41 2019-12-13 tracey }
4602 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4603 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4604 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4605 f2a9dc41 2019-12-13 tracey continue;
4606 f2a9dc41 2019-12-13 tracey }
4607 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4608 f2a9dc41 2019-12-13 tracey ondisk_path);
4609 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4610 f2a9dc41 2019-12-13 tracey goto done;
4611 f2a9dc41 2019-12-13 tracey }
4612 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4613 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4614 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4615 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4616 f2a9dc41 2019-12-13 tracey goto done;
4617 f2a9dc41 2019-12-13 tracey }
4618 f2a9dc41 2019-12-13 tracey }
4619 f2a9dc41 2019-12-13 tracey }
4620 f2a9dc41 2019-12-13 tracey
4621 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4622 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4623 a129376b 2019-03-28 stsp done:
4624 a129376b 2019-03-28 stsp if (repo)
4625 a129376b 2019-03-28 stsp got_repo_close(repo);
4626 a129376b 2019-03-28 stsp if (worktree)
4627 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4628 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4629 17ed4618 2019-06-02 stsp free((char *)pe->path);
4630 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4631 a129376b 2019-03-28 stsp free(cwd);
4632 a129376b 2019-03-28 stsp return error;
4633 a129376b 2019-03-28 stsp }
4634 a129376b 2019-03-28 stsp
4635 a129376b 2019-03-28 stsp __dead static void
4636 a129376b 2019-03-28 stsp usage_revert(void)
4637 a129376b 2019-03-28 stsp {
4638 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4639 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4640 a129376b 2019-03-28 stsp exit(1);
4641 a129376b 2019-03-28 stsp }
4642 a129376b 2019-03-28 stsp
4643 1ee397ad 2019-07-12 stsp static const struct got_error *
4644 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4645 a129376b 2019-03-28 stsp {
4646 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4647 fb9704af 2020-01-27 stsp return NULL;
4648 fb9704af 2020-01-27 stsp
4649 a129376b 2019-03-28 stsp while (path[0] == '/')
4650 a129376b 2019-03-28 stsp path++;
4651 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4652 33aa809d 2019-08-08 stsp return NULL;
4653 33aa809d 2019-08-08 stsp }
4654 33aa809d 2019-08-08 stsp
4655 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4656 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4657 33aa809d 2019-08-08 stsp const char *action;
4658 33aa809d 2019-08-08 stsp };
4659 33aa809d 2019-08-08 stsp
4660 33aa809d 2019-08-08 stsp static const struct got_error *
4661 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4662 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4663 33aa809d 2019-08-08 stsp {
4664 33aa809d 2019-08-08 stsp char *line = NULL;
4665 33aa809d 2019-08-08 stsp size_t linesize = 0;
4666 33aa809d 2019-08-08 stsp ssize_t linelen;
4667 33aa809d 2019-08-08 stsp
4668 33aa809d 2019-08-08 stsp switch (status) {
4669 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4670 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4671 33aa809d 2019-08-08 stsp break;
4672 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4673 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4674 33aa809d 2019-08-08 stsp break;
4675 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4676 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4677 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4678 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4679 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4680 33aa809d 2019-08-08 stsp printf("%s", line);
4681 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4682 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4683 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4684 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4685 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4686 33aa809d 2019-08-08 stsp break;
4687 33aa809d 2019-08-08 stsp default:
4688 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4689 33aa809d 2019-08-08 stsp }
4690 33aa809d 2019-08-08 stsp
4691 33aa809d 2019-08-08 stsp return NULL;
4692 33aa809d 2019-08-08 stsp }
4693 33aa809d 2019-08-08 stsp
4694 33aa809d 2019-08-08 stsp static const struct got_error *
4695 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4696 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4697 33aa809d 2019-08-08 stsp {
4698 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4699 33aa809d 2019-08-08 stsp char *line = NULL;
4700 33aa809d 2019-08-08 stsp size_t linesize = 0;
4701 33aa809d 2019-08-08 stsp ssize_t linelen;
4702 33aa809d 2019-08-08 stsp int resp = ' ';
4703 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4704 33aa809d 2019-08-08 stsp
4705 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4706 33aa809d 2019-08-08 stsp
4707 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4708 33aa809d 2019-08-08 stsp char *nl;
4709 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4710 33aa809d 2019-08-08 stsp a->action);
4711 33aa809d 2019-08-08 stsp if (err)
4712 33aa809d 2019-08-08 stsp return err;
4713 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4714 33aa809d 2019-08-08 stsp if (linelen == -1) {
4715 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4716 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4717 33aa809d 2019-08-08 stsp return NULL;
4718 33aa809d 2019-08-08 stsp }
4719 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4720 33aa809d 2019-08-08 stsp if (nl)
4721 33aa809d 2019-08-08 stsp *nl = '\0';
4722 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4723 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4724 33aa809d 2019-08-08 stsp printf("y\n");
4725 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4726 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4727 33aa809d 2019-08-08 stsp printf("n\n");
4728 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4729 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4730 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4731 33aa809d 2019-08-08 stsp printf("q\n");
4732 33aa809d 2019-08-08 stsp } else
4733 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4734 33aa809d 2019-08-08 stsp free(line);
4735 33aa809d 2019-08-08 stsp return NULL;
4736 33aa809d 2019-08-08 stsp }
4737 33aa809d 2019-08-08 stsp
4738 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4739 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4740 33aa809d 2019-08-08 stsp a->action);
4741 33aa809d 2019-08-08 stsp if (err)
4742 33aa809d 2019-08-08 stsp return err;
4743 33aa809d 2019-08-08 stsp resp = getchar();
4744 33aa809d 2019-08-08 stsp if (resp == '\n')
4745 33aa809d 2019-08-08 stsp resp = getchar();
4746 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4747 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4748 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4749 33aa809d 2019-08-08 stsp resp = ' ';
4750 33aa809d 2019-08-08 stsp }
4751 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4752 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4753 33aa809d 2019-08-08 stsp resp = ' ';
4754 33aa809d 2019-08-08 stsp }
4755 33aa809d 2019-08-08 stsp }
4756 33aa809d 2019-08-08 stsp
4757 33aa809d 2019-08-08 stsp if (resp == 'y')
4758 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4759 33aa809d 2019-08-08 stsp else if (resp == 'n')
4760 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4761 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4762 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4763 33aa809d 2019-08-08 stsp
4764 1ee397ad 2019-07-12 stsp return NULL;
4765 a129376b 2019-03-28 stsp }
4766 a129376b 2019-03-28 stsp
4767 33aa809d 2019-08-08 stsp
4768 a129376b 2019-03-28 stsp static const struct got_error *
4769 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4770 a129376b 2019-03-28 stsp {
4771 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4772 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4773 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4774 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4775 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4776 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4777 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4778 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4779 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4780 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4781 a129376b 2019-03-28 stsp
4782 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4783 e20a8b6f 2019-06-04 stsp
4784 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4785 a129376b 2019-03-28 stsp switch (ch) {
4786 33aa809d 2019-08-08 stsp case 'p':
4787 33aa809d 2019-08-08 stsp pflag = 1;
4788 33aa809d 2019-08-08 stsp break;
4789 33aa809d 2019-08-08 stsp case 'F':
4790 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4791 33aa809d 2019-08-08 stsp break;
4792 0f6d7415 2019-08-08 stsp case 'R':
4793 0f6d7415 2019-08-08 stsp can_recurse = 1;
4794 0f6d7415 2019-08-08 stsp break;
4795 a129376b 2019-03-28 stsp default:
4796 a129376b 2019-03-28 stsp usage_revert();
4797 a129376b 2019-03-28 stsp /* NOTREACHED */
4798 a129376b 2019-03-28 stsp }
4799 a129376b 2019-03-28 stsp }
4800 a129376b 2019-03-28 stsp
4801 a129376b 2019-03-28 stsp argc -= optind;
4802 a129376b 2019-03-28 stsp argv += optind;
4803 a129376b 2019-03-28 stsp
4804 43012d58 2019-07-14 stsp #ifndef PROFILE
4805 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4806 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4807 43012d58 2019-07-14 stsp err(1, "pledge");
4808 43012d58 2019-07-14 stsp #endif
4809 e20a8b6f 2019-06-04 stsp if (argc < 1)
4810 a129376b 2019-03-28 stsp usage_revert();
4811 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4812 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4813 a129376b 2019-03-28 stsp
4814 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4815 a129376b 2019-03-28 stsp if (cwd == NULL) {
4816 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4817 a129376b 2019-03-28 stsp goto done;
4818 a129376b 2019-03-28 stsp }
4819 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4820 2ec1f75b 2019-03-26 stsp if (error)
4821 2ec1f75b 2019-03-26 stsp goto done;
4822 a129376b 2019-03-28 stsp
4823 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4824 c9956ddf 2019-09-08 stsp NULL);
4825 a129376b 2019-03-28 stsp if (error != NULL)
4826 a129376b 2019-03-28 stsp goto done;
4827 a129376b 2019-03-28 stsp
4828 33aa809d 2019-08-08 stsp if (patch_script_path) {
4829 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4830 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4831 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4832 33aa809d 2019-08-08 stsp patch_script_path);
4833 33aa809d 2019-08-08 stsp goto done;
4834 33aa809d 2019-08-08 stsp }
4835 33aa809d 2019-08-08 stsp }
4836 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4837 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4838 a129376b 2019-03-28 stsp if (error)
4839 a129376b 2019-03-28 stsp goto done;
4840 a129376b 2019-03-28 stsp
4841 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4842 6d022e97 2019-08-04 stsp if (error)
4843 6d022e97 2019-08-04 stsp goto done;
4844 00db391e 2019-08-03 stsp
4845 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4846 0f6d7415 2019-08-08 stsp char *ondisk_path;
4847 0f6d7415 2019-08-08 stsp struct stat sb;
4848 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4849 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4850 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4851 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4852 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4853 0f6d7415 2019-08-08 stsp goto done;
4854 0f6d7415 2019-08-08 stsp }
4855 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4856 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4857 0f6d7415 2019-08-08 stsp free(ondisk_path);
4858 0f6d7415 2019-08-08 stsp continue;
4859 0f6d7415 2019-08-08 stsp }
4860 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4861 0f6d7415 2019-08-08 stsp ondisk_path);
4862 0f6d7415 2019-08-08 stsp free(ondisk_path);
4863 0f6d7415 2019-08-08 stsp goto done;
4864 0f6d7415 2019-08-08 stsp }
4865 0f6d7415 2019-08-08 stsp free(ondisk_path);
4866 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4867 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4868 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4869 0f6d7415 2019-08-08 stsp goto done;
4870 0f6d7415 2019-08-08 stsp }
4871 0f6d7415 2019-08-08 stsp }
4872 0f6d7415 2019-08-08 stsp }
4873 0f6d7415 2019-08-08 stsp
4874 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4875 33aa809d 2019-08-08 stsp cpa.action = "revert";
4876 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4877 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4878 2ec1f75b 2019-03-26 stsp done:
4879 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4880 33aa809d 2019-08-08 stsp error == NULL)
4881 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4882 2ec1f75b 2019-03-26 stsp if (repo)
4883 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4884 2ec1f75b 2019-03-26 stsp if (worktree)
4885 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4886 2ec1f75b 2019-03-26 stsp free(path);
4887 d00136be 2019-03-26 stsp free(cwd);
4888 6bad629b 2019-02-04 stsp return error;
4889 c4296144 2019-05-09 stsp }
4890 c4296144 2019-05-09 stsp
4891 c4296144 2019-05-09 stsp __dead static void
4892 c4296144 2019-05-09 stsp usage_commit(void)
4893 c4296144 2019-05-09 stsp {
4894 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4895 5c1e53bc 2019-07-28 stsp getprogname());
4896 c4296144 2019-05-09 stsp exit(1);
4897 33ad4cbe 2019-05-12 jcs }
4898 33ad4cbe 2019-05-12 jcs
4899 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4900 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4901 e2ba3d07 2019-05-13 stsp const char *editor;
4902 e0870e44 2019-05-13 stsp const char *worktree_path;
4903 76d98825 2019-06-03 stsp const char *branch_name;
4904 314a6357 2019-05-13 stsp const char *repo_path;
4905 e0870e44 2019-05-13 stsp char *logmsg_path;
4906 e2ba3d07 2019-05-13 stsp
4907 e2ba3d07 2019-05-13 stsp };
4908 e0870e44 2019-05-13 stsp
4909 33ad4cbe 2019-05-12 jcs static const struct got_error *
4910 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4911 33ad4cbe 2019-05-12 jcs void *arg)
4912 33ad4cbe 2019-05-12 jcs {
4913 76d98825 2019-06-03 stsp char *initial_content = NULL;
4914 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4915 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4916 e0870e44 2019-05-13 stsp char *template = NULL;
4917 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4918 3ce1b845 2019-07-15 stsp int fd;
4919 33ad4cbe 2019-05-12 jcs size_t len;
4920 33ad4cbe 2019-05-12 jcs
4921 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4922 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4923 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4924 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4925 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4926 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4927 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4928 33ad4cbe 2019-05-12 jcs return NULL;
4929 33ad4cbe 2019-05-12 jcs }
4930 33ad4cbe 2019-05-12 jcs
4931 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4932 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4933 76d98825 2019-06-03 stsp
4934 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4935 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4936 76d98825 2019-06-03 stsp a->branch_name) == -1)
4937 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4938 e0870e44 2019-05-13 stsp
4939 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4940 793c30b5 2019-05-13 stsp if (err)
4941 e0870e44 2019-05-13 stsp goto done;
4942 33ad4cbe 2019-05-12 jcs
4943 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4944 33ad4cbe 2019-05-12 jcs
4945 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4946 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4947 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4948 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4949 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4950 33ad4cbe 2019-05-12 jcs }
4951 33ad4cbe 2019-05-12 jcs close(fd);
4952 33ad4cbe 2019-05-12 jcs
4953 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4954 33ad4cbe 2019-05-12 jcs done:
4955 76d98825 2019-06-03 stsp free(initial_content);
4956 e0870e44 2019-05-13 stsp free(template);
4957 314a6357 2019-05-13 stsp
4958 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4959 3ce1b845 2019-07-15 stsp if (err == NULL) {
4960 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4961 3ce1b845 2019-07-15 stsp if (err) {
4962 3ce1b845 2019-07-15 stsp free(*logmsg);
4963 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4964 3ce1b845 2019-07-15 stsp }
4965 3ce1b845 2019-07-15 stsp }
4966 33ad4cbe 2019-05-12 jcs return err;
4967 6bad629b 2019-02-04 stsp }
4968 c4296144 2019-05-09 stsp
4969 c4296144 2019-05-09 stsp static const struct got_error *
4970 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4971 c4296144 2019-05-09 stsp {
4972 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4973 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4974 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4975 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4976 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4977 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4978 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4979 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4980 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4981 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4982 5c1e53bc 2019-07-28 stsp
4983 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4984 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4985 c4296144 2019-05-09 stsp
4986 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4987 c4296144 2019-05-09 stsp switch (ch) {
4988 c4296144 2019-05-09 stsp case 'm':
4989 c4296144 2019-05-09 stsp logmsg = optarg;
4990 c4296144 2019-05-09 stsp break;
4991 c4296144 2019-05-09 stsp default:
4992 c4296144 2019-05-09 stsp usage_commit();
4993 c4296144 2019-05-09 stsp /* NOTREACHED */
4994 c4296144 2019-05-09 stsp }
4995 c4296144 2019-05-09 stsp }
4996 c4296144 2019-05-09 stsp
4997 c4296144 2019-05-09 stsp argc -= optind;
4998 c4296144 2019-05-09 stsp argv += optind;
4999 c4296144 2019-05-09 stsp
5000 43012d58 2019-07-14 stsp #ifndef PROFILE
5001 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5002 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5003 43012d58 2019-07-14 stsp err(1, "pledge");
5004 43012d58 2019-07-14 stsp #endif
5005 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5006 c4296144 2019-05-09 stsp if (cwd == NULL) {
5007 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5008 c4296144 2019-05-09 stsp goto done;
5009 c4296144 2019-05-09 stsp }
5010 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5011 7d5807f4 2019-07-11 stsp if (error)
5012 7d5807f4 2019-07-11 stsp goto done;
5013 7d5807f4 2019-07-11 stsp
5014 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5015 c4296144 2019-05-09 stsp if (error)
5016 a698f62e 2019-07-25 stsp goto done;
5017 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5018 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5019 c4296144 2019-05-09 stsp goto done;
5020 a698f62e 2019-07-25 stsp }
5021 5c1e53bc 2019-07-28 stsp
5022 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5023 916f288c 2019-07-30 stsp worktree);
5024 5c1e53bc 2019-07-28 stsp if (error)
5025 5c1e53bc 2019-07-28 stsp goto done;
5026 c4296144 2019-05-09 stsp
5027 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5028 c9956ddf 2019-09-08 stsp if (error)
5029 c9956ddf 2019-09-08 stsp goto done;
5030 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5031 c9956ddf 2019-09-08 stsp gitconfig_path);
5032 c4296144 2019-05-09 stsp if (error != NULL)
5033 c4296144 2019-05-09 stsp goto done;
5034 c4296144 2019-05-09 stsp
5035 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5036 aba9c984 2019-09-08 stsp if (error)
5037 aba9c984 2019-09-08 stsp return error;
5038 aba9c984 2019-09-08 stsp
5039 314a6357 2019-05-13 stsp /*
5040 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5041 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5042 314a6357 2019-05-13 stsp */
5043 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5044 314a6357 2019-05-13 stsp error = get_editor(&editor);
5045 314a6357 2019-05-13 stsp else
5046 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5047 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5048 c4296144 2019-05-09 stsp if (error)
5049 c4296144 2019-05-09 stsp goto done;
5050 c4296144 2019-05-09 stsp
5051 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5052 087fb88c 2019-08-04 stsp if (error)
5053 087fb88c 2019-08-04 stsp goto done;
5054 087fb88c 2019-08-04 stsp
5055 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5056 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5057 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5058 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5059 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5060 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5061 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5062 916f288c 2019-07-30 stsp goto done;
5063 916f288c 2019-07-30 stsp }
5064 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5065 916f288c 2019-07-30 stsp }
5066 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5067 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5068 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5069 e0870e44 2019-05-13 stsp if (error) {
5070 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5071 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5072 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5073 c4296144 2019-05-09 stsp goto done;
5074 e0870e44 2019-05-13 stsp }
5075 c4296144 2019-05-09 stsp
5076 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5077 c4296144 2019-05-09 stsp if (error)
5078 c4296144 2019-05-09 stsp goto done;
5079 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5080 c4296144 2019-05-09 stsp done:
5081 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5082 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5083 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5084 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5085 7266f21f 2019-10-21 stsp error == NULL)
5086 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5087 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5088 c4296144 2019-05-09 stsp if (repo)
5089 c4296144 2019-05-09 stsp got_repo_close(repo);
5090 c4296144 2019-05-09 stsp if (worktree)
5091 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5092 c4296144 2019-05-09 stsp free(cwd);
5093 c4296144 2019-05-09 stsp free(id_str);
5094 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5095 e2ba3d07 2019-05-13 stsp free(editor);
5096 aba9c984 2019-09-08 stsp free(author);
5097 234035bc 2019-06-01 stsp return error;
5098 234035bc 2019-06-01 stsp }
5099 234035bc 2019-06-01 stsp
5100 234035bc 2019-06-01 stsp __dead static void
5101 234035bc 2019-06-01 stsp usage_cherrypick(void)
5102 234035bc 2019-06-01 stsp {
5103 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5104 234035bc 2019-06-01 stsp exit(1);
5105 234035bc 2019-06-01 stsp }
5106 234035bc 2019-06-01 stsp
5107 234035bc 2019-06-01 stsp static const struct got_error *
5108 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5109 234035bc 2019-06-01 stsp {
5110 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5111 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5112 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5113 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5114 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5115 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5116 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5117 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5118 234035bc 2019-06-01 stsp int ch, did_something = 0;
5119 234035bc 2019-06-01 stsp
5120 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5121 234035bc 2019-06-01 stsp switch (ch) {
5122 234035bc 2019-06-01 stsp default:
5123 234035bc 2019-06-01 stsp usage_cherrypick();
5124 234035bc 2019-06-01 stsp /* NOTREACHED */
5125 234035bc 2019-06-01 stsp }
5126 234035bc 2019-06-01 stsp }
5127 234035bc 2019-06-01 stsp
5128 234035bc 2019-06-01 stsp argc -= optind;
5129 234035bc 2019-06-01 stsp argv += optind;
5130 234035bc 2019-06-01 stsp
5131 43012d58 2019-07-14 stsp #ifndef PROFILE
5132 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5133 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5134 43012d58 2019-07-14 stsp err(1, "pledge");
5135 43012d58 2019-07-14 stsp #endif
5136 234035bc 2019-06-01 stsp if (argc != 1)
5137 234035bc 2019-06-01 stsp usage_cherrypick();
5138 234035bc 2019-06-01 stsp
5139 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5140 234035bc 2019-06-01 stsp if (cwd == NULL) {
5141 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5142 234035bc 2019-06-01 stsp goto done;
5143 234035bc 2019-06-01 stsp }
5144 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5145 234035bc 2019-06-01 stsp if (error)
5146 234035bc 2019-06-01 stsp goto done;
5147 234035bc 2019-06-01 stsp
5148 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5149 c9956ddf 2019-09-08 stsp NULL);
5150 234035bc 2019-06-01 stsp if (error != NULL)
5151 234035bc 2019-06-01 stsp goto done;
5152 234035bc 2019-06-01 stsp
5153 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5154 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5155 234035bc 2019-06-01 stsp if (error)
5156 234035bc 2019-06-01 stsp goto done;
5157 234035bc 2019-06-01 stsp
5158 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5159 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5160 234035bc 2019-06-01 stsp if (error != NULL) {
5161 234035bc 2019-06-01 stsp struct got_reference *ref;
5162 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5163 234035bc 2019-06-01 stsp goto done;
5164 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5165 234035bc 2019-06-01 stsp if (error != NULL)
5166 234035bc 2019-06-01 stsp goto done;
5167 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5168 234035bc 2019-06-01 stsp got_ref_close(ref);
5169 234035bc 2019-06-01 stsp if (error != NULL)
5170 234035bc 2019-06-01 stsp goto done;
5171 234035bc 2019-06-01 stsp }
5172 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5173 234035bc 2019-06-01 stsp if (error)
5174 234035bc 2019-06-01 stsp goto done;
5175 234035bc 2019-06-01 stsp
5176 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5177 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5178 234035bc 2019-06-01 stsp if (error != NULL)
5179 234035bc 2019-06-01 stsp goto done;
5180 234035bc 2019-06-01 stsp
5181 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5182 234035bc 2019-06-01 stsp if (error) {
5183 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5184 234035bc 2019-06-01 stsp goto done;
5185 234035bc 2019-06-01 stsp error = NULL;
5186 234035bc 2019-06-01 stsp } else {
5187 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5188 234035bc 2019-06-01 stsp goto done;
5189 234035bc 2019-06-01 stsp }
5190 234035bc 2019-06-01 stsp
5191 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5192 234035bc 2019-06-01 stsp if (error)
5193 234035bc 2019-06-01 stsp goto done;
5194 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5195 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5196 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5197 03415a1a 2019-06-02 stsp NULL);
5198 234035bc 2019-06-01 stsp if (error != NULL)
5199 234035bc 2019-06-01 stsp goto done;
5200 234035bc 2019-06-01 stsp
5201 234035bc 2019-06-01 stsp if (did_something)
5202 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5203 234035bc 2019-06-01 stsp done:
5204 234035bc 2019-06-01 stsp if (commit)
5205 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5206 234035bc 2019-06-01 stsp free(commit_id_str);
5207 234035bc 2019-06-01 stsp if (head_ref)
5208 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5209 234035bc 2019-06-01 stsp if (worktree)
5210 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5211 234035bc 2019-06-01 stsp if (repo)
5212 234035bc 2019-06-01 stsp got_repo_close(repo);
5213 c4296144 2019-05-09 stsp return error;
5214 c4296144 2019-05-09 stsp }
5215 5ef14e63 2019-06-02 stsp
5216 5ef14e63 2019-06-02 stsp __dead static void
5217 5ef14e63 2019-06-02 stsp usage_backout(void)
5218 5ef14e63 2019-06-02 stsp {
5219 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5220 5ef14e63 2019-06-02 stsp exit(1);
5221 5ef14e63 2019-06-02 stsp }
5222 5ef14e63 2019-06-02 stsp
5223 5ef14e63 2019-06-02 stsp static const struct got_error *
5224 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5225 5ef14e63 2019-06-02 stsp {
5226 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5227 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5228 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5229 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5230 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5231 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5232 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5233 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5234 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5235 5ef14e63 2019-06-02 stsp
5236 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5237 5ef14e63 2019-06-02 stsp switch (ch) {
5238 5ef14e63 2019-06-02 stsp default:
5239 5ef14e63 2019-06-02 stsp usage_backout();
5240 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5241 5ef14e63 2019-06-02 stsp }
5242 5ef14e63 2019-06-02 stsp }
5243 5ef14e63 2019-06-02 stsp
5244 5ef14e63 2019-06-02 stsp argc -= optind;
5245 5ef14e63 2019-06-02 stsp argv += optind;
5246 5ef14e63 2019-06-02 stsp
5247 43012d58 2019-07-14 stsp #ifndef PROFILE
5248 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5249 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5250 43012d58 2019-07-14 stsp err(1, "pledge");
5251 43012d58 2019-07-14 stsp #endif
5252 5ef14e63 2019-06-02 stsp if (argc != 1)
5253 5ef14e63 2019-06-02 stsp usage_backout();
5254 5ef14e63 2019-06-02 stsp
5255 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5256 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5257 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5258 5ef14e63 2019-06-02 stsp goto done;
5259 5ef14e63 2019-06-02 stsp }
5260 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5261 5ef14e63 2019-06-02 stsp if (error)
5262 5ef14e63 2019-06-02 stsp goto done;
5263 5ef14e63 2019-06-02 stsp
5264 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5265 c9956ddf 2019-09-08 stsp NULL);
5266 5ef14e63 2019-06-02 stsp if (error != NULL)
5267 5ef14e63 2019-06-02 stsp goto done;
5268 5ef14e63 2019-06-02 stsp
5269 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5270 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5271 5ef14e63 2019-06-02 stsp if (error)
5272 5ef14e63 2019-06-02 stsp goto done;
5273 5ef14e63 2019-06-02 stsp
5274 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5275 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5276 5ef14e63 2019-06-02 stsp if (error != NULL) {
5277 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5278 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5279 5ef14e63 2019-06-02 stsp goto done;
5280 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5281 5ef14e63 2019-06-02 stsp if (error != NULL)
5282 5ef14e63 2019-06-02 stsp goto done;
5283 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5284 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5285 5ef14e63 2019-06-02 stsp if (error != NULL)
5286 5ef14e63 2019-06-02 stsp goto done;
5287 5ef14e63 2019-06-02 stsp }
5288 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5289 5ef14e63 2019-06-02 stsp if (error)
5290 5ef14e63 2019-06-02 stsp goto done;
5291 5ef14e63 2019-06-02 stsp
5292 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5293 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5294 5ef14e63 2019-06-02 stsp if (error != NULL)
5295 5ef14e63 2019-06-02 stsp goto done;
5296 5ef14e63 2019-06-02 stsp
5297 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5298 5ef14e63 2019-06-02 stsp if (error)
5299 5ef14e63 2019-06-02 stsp goto done;
5300 5ef14e63 2019-06-02 stsp
5301 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5302 5ef14e63 2019-06-02 stsp if (error)
5303 5ef14e63 2019-06-02 stsp goto done;
5304 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5305 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5306 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5307 5ef14e63 2019-06-02 stsp goto done;
5308 5ef14e63 2019-06-02 stsp }
5309 5ef14e63 2019-06-02 stsp
5310 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5311 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5312 5ef14e63 2019-06-02 stsp if (error != NULL)
5313 5ef14e63 2019-06-02 stsp goto done;
5314 5ef14e63 2019-06-02 stsp
5315 5ef14e63 2019-06-02 stsp if (did_something)
5316 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5317 5ef14e63 2019-06-02 stsp done:
5318 5ef14e63 2019-06-02 stsp if (commit)
5319 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5320 5ef14e63 2019-06-02 stsp free(commit_id_str);
5321 5ef14e63 2019-06-02 stsp if (head_ref)
5322 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5323 818c7501 2019-07-11 stsp if (worktree)
5324 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5325 818c7501 2019-07-11 stsp if (repo)
5326 818c7501 2019-07-11 stsp got_repo_close(repo);
5327 818c7501 2019-07-11 stsp return error;
5328 818c7501 2019-07-11 stsp }
5329 818c7501 2019-07-11 stsp
5330 818c7501 2019-07-11 stsp __dead static void
5331 818c7501 2019-07-11 stsp usage_rebase(void)
5332 818c7501 2019-07-11 stsp {
5333 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5334 af54c8f8 2019-07-11 stsp getprogname());
5335 818c7501 2019-07-11 stsp exit(1);
5336 0ebf8283 2019-07-24 stsp }
5337 0ebf8283 2019-07-24 stsp
5338 0ebf8283 2019-07-24 stsp void
5339 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5340 0ebf8283 2019-07-24 stsp {
5341 0ebf8283 2019-07-24 stsp char *nl;
5342 0ebf8283 2019-07-24 stsp size_t len;
5343 0ebf8283 2019-07-24 stsp
5344 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5345 0ebf8283 2019-07-24 stsp if (len > limit)
5346 0ebf8283 2019-07-24 stsp len = limit;
5347 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5348 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5349 0ebf8283 2019-07-24 stsp if (nl)
5350 0ebf8283 2019-07-24 stsp *nl = '\0';
5351 0ebf8283 2019-07-24 stsp }
5352 0ebf8283 2019-07-24 stsp
5353 0ebf8283 2019-07-24 stsp static const struct got_error *
5354 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5355 0ebf8283 2019-07-24 stsp {
5356 5943eee2 2019-08-13 stsp const struct got_error *err;
5357 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5358 5943eee2 2019-08-13 stsp const char *s;
5359 0ebf8283 2019-07-24 stsp
5360 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5361 5943eee2 2019-08-13 stsp if (err)
5362 5943eee2 2019-08-13 stsp return err;
5363 0ebf8283 2019-07-24 stsp
5364 5943eee2 2019-08-13 stsp s = logmsg0;
5365 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5366 5943eee2 2019-08-13 stsp s++;
5367 5943eee2 2019-08-13 stsp
5368 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5369 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5370 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5371 5943eee2 2019-08-13 stsp goto done;
5372 5943eee2 2019-08-13 stsp }
5373 0ebf8283 2019-07-24 stsp
5374 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5375 5943eee2 2019-08-13 stsp done:
5376 5943eee2 2019-08-13 stsp free(logmsg0);
5377 a0ea4fc0 2020-02-28 stsp return err;
5378 a0ea4fc0 2020-02-28 stsp }
5379 a0ea4fc0 2020-02-28 stsp
5380 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5381 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5382 a0ea4fc0 2020-02-28 stsp {
5383 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5384 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5385 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5386 a0ea4fc0 2020-02-28 stsp
5387 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5388 a0ea4fc0 2020-02-28 stsp if (err)
5389 a0ea4fc0 2020-02-28 stsp return err;
5390 a0ea4fc0 2020-02-28 stsp
5391 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5392 a0ea4fc0 2020-02-28 stsp if (err)
5393 a0ea4fc0 2020-02-28 stsp goto done;
5394 a0ea4fc0 2020-02-28 stsp
5395 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5396 a0ea4fc0 2020-02-28 stsp
5397 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5398 a0ea4fc0 2020-02-28 stsp if (err)
5399 a0ea4fc0 2020-02-28 stsp goto done;
5400 a0ea4fc0 2020-02-28 stsp
5401 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5402 a0ea4fc0 2020-02-28 stsp done:
5403 a0ea4fc0 2020-02-28 stsp free(id_str);
5404 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5405 a0ea4fc0 2020-02-28 stsp free(logmsg);
5406 5943eee2 2019-08-13 stsp return err;
5407 818c7501 2019-07-11 stsp }
5408 818c7501 2019-07-11 stsp
5409 818c7501 2019-07-11 stsp static const struct got_error *
5410 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5411 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5412 818c7501 2019-07-11 stsp {
5413 818c7501 2019-07-11 stsp const struct got_error *err;
5414 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5415 818c7501 2019-07-11 stsp
5416 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5417 818c7501 2019-07-11 stsp if (err)
5418 818c7501 2019-07-11 stsp goto done;
5419 818c7501 2019-07-11 stsp
5420 ff0d2220 2019-07-11 stsp if (new_id) {
5421 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5422 ff0d2220 2019-07-11 stsp if (err)
5423 ff0d2220 2019-07-11 stsp goto done;
5424 ff0d2220 2019-07-11 stsp }
5425 818c7501 2019-07-11 stsp
5426 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5427 ff0d2220 2019-07-11 stsp if (new_id_str)
5428 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5429 0ebf8283 2019-07-24 stsp
5430 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5431 0ebf8283 2019-07-24 stsp if (err)
5432 0ebf8283 2019-07-24 stsp goto done;
5433 0ebf8283 2019-07-24 stsp
5434 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5435 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5436 818c7501 2019-07-11 stsp done:
5437 818c7501 2019-07-11 stsp free(old_id_str);
5438 818c7501 2019-07-11 stsp free(new_id_str);
5439 272a1371 2020-02-28 stsp free(logmsg);
5440 818c7501 2019-07-11 stsp return err;
5441 818c7501 2019-07-11 stsp }
5442 818c7501 2019-07-11 stsp
5443 1ee397ad 2019-07-12 stsp static const struct got_error *
5444 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5445 818c7501 2019-07-11 stsp {
5446 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5447 818c7501 2019-07-11 stsp
5448 818c7501 2019-07-11 stsp while (path[0] == '/')
5449 818c7501 2019-07-11 stsp path++;
5450 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5451 818c7501 2019-07-11 stsp
5452 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5453 1ee397ad 2019-07-12 stsp return NULL;
5454 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5455 818c7501 2019-07-11 stsp *rebase_status = status;
5456 1ee397ad 2019-07-12 stsp return NULL;
5457 818c7501 2019-07-11 stsp }
5458 818c7501 2019-07-11 stsp
5459 818c7501 2019-07-11 stsp static const struct got_error *
5460 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5461 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5462 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5463 818c7501 2019-07-11 stsp {
5464 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5465 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5466 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5467 818c7501 2019-07-11 stsp }
5468 818c7501 2019-07-11 stsp
5469 818c7501 2019-07-11 stsp static const struct got_error *
5470 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5471 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5472 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5473 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5474 ff0d2220 2019-07-11 stsp {
5475 ff0d2220 2019-07-11 stsp const struct got_error *error;
5476 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5477 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5478 ff0d2220 2019-07-11 stsp
5479 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5480 ff0d2220 2019-07-11 stsp if (error)
5481 ff0d2220 2019-07-11 stsp return error;
5482 ff0d2220 2019-07-11 stsp
5483 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5484 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5485 ff0d2220 2019-07-11 stsp if (error) {
5486 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5487 ff0d2220 2019-07-11 stsp goto done;
5488 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5489 ff0d2220 2019-07-11 stsp } else {
5490 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5491 ff0d2220 2019-07-11 stsp free(new_commit_id);
5492 ff0d2220 2019-07-11 stsp }
5493 ff0d2220 2019-07-11 stsp done:
5494 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5495 ff0d2220 2019-07-11 stsp return error;
5496 64c6d990 2019-07-11 stsp }
5497 64c6d990 2019-07-11 stsp
5498 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5499 64c6d990 2019-07-11 stsp const char *path_prefix;
5500 64c6d990 2019-07-11 stsp size_t len;
5501 8ca9bd68 2019-07-25 stsp int errcode;
5502 64c6d990 2019-07-11 stsp };
5503 64c6d990 2019-07-11 stsp
5504 64c6d990 2019-07-11 stsp static const struct got_error *
5505 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5506 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5507 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5508 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5509 64c6d990 2019-07-11 stsp {
5510 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5511 64c6d990 2019-07-11 stsp
5512 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5513 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5514 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5515 64c6d990 2019-07-11 stsp
5516 64c6d990 2019-07-11 stsp return NULL;
5517 64c6d990 2019-07-11 stsp }
5518 64c6d990 2019-07-11 stsp
5519 64c6d990 2019-07-11 stsp static const struct got_error *
5520 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5521 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5522 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5523 64c6d990 2019-07-11 stsp {
5524 64c6d990 2019-07-11 stsp const struct got_error *err;
5525 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5526 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5527 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5528 64c6d990 2019-07-11 stsp
5529 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5530 64c6d990 2019-07-11 stsp return NULL;
5531 64c6d990 2019-07-11 stsp
5532 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5533 64c6d990 2019-07-11 stsp if (err)
5534 64c6d990 2019-07-11 stsp goto done;
5535 64c6d990 2019-07-11 stsp
5536 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5537 64c6d990 2019-07-11 stsp if (err)
5538 64c6d990 2019-07-11 stsp goto done;
5539 64c6d990 2019-07-11 stsp
5540 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5541 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5542 64c6d990 2019-07-11 stsp if (err)
5543 64c6d990 2019-07-11 stsp goto done;
5544 64c6d990 2019-07-11 stsp
5545 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5546 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5547 64c6d990 2019-07-11 stsp if (err)
5548 64c6d990 2019-07-11 stsp goto done;
5549 64c6d990 2019-07-11 stsp
5550 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5551 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5552 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5553 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5554 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5555 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5556 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5557 64c6d990 2019-07-11 stsp done:
5558 64c6d990 2019-07-11 stsp if (tree1)
5559 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5560 64c6d990 2019-07-11 stsp if (tree2)
5561 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5562 64c6d990 2019-07-11 stsp if (commit)
5563 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5564 64c6d990 2019-07-11 stsp if (parent_commit)
5565 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5566 64c6d990 2019-07-11 stsp return err;
5567 ff0d2220 2019-07-11 stsp }
5568 ff0d2220 2019-07-11 stsp
5569 ff0d2220 2019-07-11 stsp static const struct got_error *
5570 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5571 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5572 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5573 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5574 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5575 af61c510 2019-07-19 stsp {
5576 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5577 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5578 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5579 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5580 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5581 af61c510 2019-07-19 stsp
5582 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5583 af61c510 2019-07-19 stsp if (err)
5584 af61c510 2019-07-19 stsp return err;
5585 af61c510 2019-07-19 stsp
5586 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5587 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5588 af61c510 2019-07-19 stsp if (err)
5589 af61c510 2019-07-19 stsp goto done;
5590 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5591 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5592 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5593 af61c510 2019-07-19 stsp if (err) {
5594 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5595 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5596 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5597 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5598 af61c510 2019-07-19 stsp "been reached?!?");
5599 ee780d5c 2020-01-04 stsp }
5600 ee780d5c 2020-01-04 stsp goto done;
5601 af61c510 2019-07-19 stsp } else {
5602 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5603 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5604 af61c510 2019-07-19 stsp if (err)
5605 af61c510 2019-07-19 stsp goto done;
5606 af61c510 2019-07-19 stsp
5607 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5608 af61c510 2019-07-19 stsp if (err)
5609 af61c510 2019-07-19 stsp goto done;
5610 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5611 af61c510 2019-07-19 stsp commit_id = parent_id;
5612 af61c510 2019-07-19 stsp }
5613 af61c510 2019-07-19 stsp }
5614 af61c510 2019-07-19 stsp done:
5615 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5616 af61c510 2019-07-19 stsp return err;
5617 af61c510 2019-07-19 stsp }
5618 af61c510 2019-07-19 stsp
5619 af61c510 2019-07-19 stsp static const struct got_error *
5620 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5621 818c7501 2019-07-11 stsp {
5622 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5623 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5624 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5625 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5626 818c7501 2019-07-11 stsp char *cwd = NULL;
5627 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5628 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5629 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5630 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5631 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5632 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5633 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5634 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5635 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5636 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5637 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5638 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5639 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5640 818c7501 2019-07-11 stsp
5641 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5642 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5643 818c7501 2019-07-11 stsp
5644 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5645 818c7501 2019-07-11 stsp switch (ch) {
5646 818c7501 2019-07-11 stsp case 'a':
5647 818c7501 2019-07-11 stsp abort_rebase = 1;
5648 818c7501 2019-07-11 stsp break;
5649 818c7501 2019-07-11 stsp case 'c':
5650 818c7501 2019-07-11 stsp continue_rebase = 1;
5651 818c7501 2019-07-11 stsp break;
5652 818c7501 2019-07-11 stsp default:
5653 818c7501 2019-07-11 stsp usage_rebase();
5654 818c7501 2019-07-11 stsp /* NOTREACHED */
5655 818c7501 2019-07-11 stsp }
5656 818c7501 2019-07-11 stsp }
5657 818c7501 2019-07-11 stsp
5658 818c7501 2019-07-11 stsp argc -= optind;
5659 818c7501 2019-07-11 stsp argv += optind;
5660 818c7501 2019-07-11 stsp
5661 43012d58 2019-07-14 stsp #ifndef PROFILE
5662 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5664 43012d58 2019-07-14 stsp err(1, "pledge");
5665 43012d58 2019-07-14 stsp #endif
5666 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5667 818c7501 2019-07-11 stsp usage_rebase();
5668 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5669 818c7501 2019-07-11 stsp if (argc != 0)
5670 818c7501 2019-07-11 stsp usage_rebase();
5671 818c7501 2019-07-11 stsp } else if (argc != 1)
5672 818c7501 2019-07-11 stsp usage_rebase();
5673 818c7501 2019-07-11 stsp
5674 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5675 818c7501 2019-07-11 stsp if (cwd == NULL) {
5676 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5677 818c7501 2019-07-11 stsp goto done;
5678 818c7501 2019-07-11 stsp }
5679 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5680 818c7501 2019-07-11 stsp if (error)
5681 818c7501 2019-07-11 stsp goto done;
5682 818c7501 2019-07-11 stsp
5683 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5684 c9956ddf 2019-09-08 stsp NULL);
5685 818c7501 2019-07-11 stsp if (error != NULL)
5686 818c7501 2019-07-11 stsp goto done;
5687 818c7501 2019-07-11 stsp
5688 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5689 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5690 7ef62c4e 2020-02-24 stsp if (error)
5691 7ef62c4e 2020-02-24 stsp goto done;
5692 7ef62c4e 2020-02-24 stsp
5693 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5694 7ef62c4e 2020-02-24 stsp worktree);
5695 818c7501 2019-07-11 stsp if (error)
5696 7ef62c4e 2020-02-24 stsp goto done;
5697 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5698 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5699 818c7501 2019-07-11 stsp goto done;
5700 7ef62c4e 2020-02-24 stsp }
5701 818c7501 2019-07-11 stsp
5702 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5703 818c7501 2019-07-11 stsp if (error)
5704 818c7501 2019-07-11 stsp goto done;
5705 818c7501 2019-07-11 stsp
5706 f6794adc 2019-07-23 stsp if (abort_rebase) {
5707 818c7501 2019-07-11 stsp int did_something;
5708 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5709 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5710 f6794adc 2019-07-23 stsp goto done;
5711 f6794adc 2019-07-23 stsp }
5712 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5713 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5714 3e3a69f1 2019-07-25 stsp worktree, repo);
5715 818c7501 2019-07-11 stsp if (error)
5716 818c7501 2019-07-11 stsp goto done;
5717 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5718 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5719 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5720 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5721 818c7501 2019-07-11 stsp if (error)
5722 818c7501 2019-07-11 stsp goto done;
5723 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5724 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5725 818c7501 2019-07-11 stsp }
5726 818c7501 2019-07-11 stsp
5727 818c7501 2019-07-11 stsp if (continue_rebase) {
5728 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5729 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5730 f6794adc 2019-07-23 stsp goto done;
5731 f6794adc 2019-07-23 stsp }
5732 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5733 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5734 3e3a69f1 2019-07-25 stsp worktree, repo);
5735 818c7501 2019-07-11 stsp if (error)
5736 818c7501 2019-07-11 stsp goto done;
5737 818c7501 2019-07-11 stsp
5738 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5739 01757395 2019-07-12 stsp resume_commit_id, repo);
5740 818c7501 2019-07-11 stsp if (error)
5741 818c7501 2019-07-11 stsp goto done;
5742 818c7501 2019-07-11 stsp
5743 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5744 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5745 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5746 818c7501 2019-07-11 stsp goto done;
5747 818c7501 2019-07-11 stsp }
5748 818c7501 2019-07-11 stsp } else {
5749 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5750 818c7501 2019-07-11 stsp if (error != NULL)
5751 818c7501 2019-07-11 stsp goto done;
5752 ff0d2220 2019-07-11 stsp }
5753 818c7501 2019-07-11 stsp
5754 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5755 ff0d2220 2019-07-11 stsp if (error)
5756 ff0d2220 2019-07-11 stsp goto done;
5757 ff0d2220 2019-07-11 stsp
5758 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5759 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5760 a51a74b3 2019-07-27 stsp
5761 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5762 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5763 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5764 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5765 818c7501 2019-07-11 stsp if (error)
5766 818c7501 2019-07-11 stsp goto done;
5767 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5768 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5769 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5770 818c7501 2019-07-11 stsp "with work tree's branch");
5771 818c7501 2019-07-11 stsp goto done;
5772 818c7501 2019-07-11 stsp }
5773 818c7501 2019-07-11 stsp
5774 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5775 a51a74b3 2019-07-27 stsp if (error) {
5776 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5777 a51a74b3 2019-07-27 stsp goto done;
5778 a51a74b3 2019-07-27 stsp error = NULL;
5779 a51a74b3 2019-07-27 stsp } else {
5780 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5781 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5782 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5783 a51a74b3 2019-07-27 stsp goto done;
5784 a51a74b3 2019-07-27 stsp }
5785 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5786 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5787 818c7501 2019-07-11 stsp if (error)
5788 818c7501 2019-07-11 stsp goto done;
5789 818c7501 2019-07-11 stsp }
5790 818c7501 2019-07-11 stsp
5791 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5792 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5793 818c7501 2019-07-11 stsp if (error)
5794 818c7501 2019-07-11 stsp goto done;
5795 818c7501 2019-07-11 stsp
5796 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5797 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5798 fc66b545 2019-08-12 stsp if (pid == NULL) {
5799 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5800 fc66b545 2019-08-12 stsp int did_something;
5801 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5802 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5803 fc66b545 2019-08-12 stsp &did_something);
5804 fc66b545 2019-08-12 stsp if (error)
5805 fc66b545 2019-08-12 stsp goto done;
5806 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5807 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5808 fc66b545 2019-08-12 stsp }
5809 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5810 fc66b545 2019-08-12 stsp goto done;
5811 fc66b545 2019-08-12 stsp }
5812 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5813 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5814 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5815 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5816 818c7501 2019-07-11 stsp commit = NULL;
5817 818c7501 2019-07-11 stsp if (error)
5818 818c7501 2019-07-11 stsp goto done;
5819 64c6d990 2019-07-11 stsp
5820 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5821 38b0338b 2019-11-29 stsp if (continue_rebase) {
5822 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5823 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5824 38b0338b 2019-11-29 stsp goto done;
5825 38b0338b 2019-11-29 stsp } else {
5826 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5827 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5828 38b0338b 2019-11-29 stsp char *id_str;
5829 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5830 38b0338b 2019-11-29 stsp new_base_branch);
5831 38b0338b 2019-11-29 stsp if (error)
5832 38b0338b 2019-11-29 stsp goto done;
5833 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5834 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5835 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5836 38b0338b 2019-11-29 stsp free(id_str);
5837 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5838 38b0338b 2019-11-29 stsp new_head_commit_id);
5839 38b0338b 2019-11-29 stsp if (error)
5840 38b0338b 2019-11-29 stsp goto done;
5841 38b0338b 2019-11-29 stsp }
5842 818c7501 2019-07-11 stsp }
5843 818c7501 2019-07-11 stsp
5844 818c7501 2019-07-11 stsp pid = NULL;
5845 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5846 818c7501 2019-07-11 stsp commit_id = qid->id;
5847 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5848 818c7501 2019-07-11 stsp pid = qid;
5849 818c7501 2019-07-11 stsp
5850 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5851 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5852 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5853 818c7501 2019-07-11 stsp if (error)
5854 818c7501 2019-07-11 stsp goto done;
5855 818c7501 2019-07-11 stsp
5856 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5857 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5858 a0ea4fc0 2020-02-28 stsp if (error)
5859 a0ea4fc0 2020-02-28 stsp goto done;
5860 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5861 818c7501 2019-07-11 stsp break;
5862 01757395 2019-07-12 stsp }
5863 818c7501 2019-07-11 stsp
5864 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5865 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5866 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5867 818c7501 2019-07-11 stsp if (error)
5868 818c7501 2019-07-11 stsp goto done;
5869 818c7501 2019-07-11 stsp }
5870 818c7501 2019-07-11 stsp
5871 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5872 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5873 818c7501 2019-07-11 stsp if (error)
5874 818c7501 2019-07-11 stsp goto done;
5875 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5876 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5877 818c7501 2019-07-11 stsp } else
5878 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5879 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5880 818c7501 2019-07-11 stsp done:
5881 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5882 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5883 818c7501 2019-07-11 stsp free(resume_commit_id);
5884 818c7501 2019-07-11 stsp free(yca_id);
5885 818c7501 2019-07-11 stsp if (commit)
5886 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5887 818c7501 2019-07-11 stsp if (branch)
5888 818c7501 2019-07-11 stsp got_ref_close(branch);
5889 818c7501 2019-07-11 stsp if (new_base_branch)
5890 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5891 818c7501 2019-07-11 stsp if (tmp_branch)
5892 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5893 5ef14e63 2019-06-02 stsp if (worktree)
5894 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5895 5ef14e63 2019-06-02 stsp if (repo)
5896 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5897 5ef14e63 2019-06-02 stsp return error;
5898 0ebf8283 2019-07-24 stsp }
5899 0ebf8283 2019-07-24 stsp
5900 0ebf8283 2019-07-24 stsp __dead static void
5901 0ebf8283 2019-07-24 stsp usage_histedit(void)
5902 0ebf8283 2019-07-24 stsp {
5903 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5904 0ebf8283 2019-07-24 stsp getprogname());
5905 0ebf8283 2019-07-24 stsp exit(1);
5906 0ebf8283 2019-07-24 stsp }
5907 0ebf8283 2019-07-24 stsp
5908 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5909 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5910 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5911 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5912 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5913 0ebf8283 2019-07-24 stsp
5914 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5915 0ebf8283 2019-07-24 stsp unsigned char code;
5916 0ebf8283 2019-07-24 stsp const char *name;
5917 0ebf8283 2019-07-24 stsp const char *desc;
5918 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5919 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5920 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5921 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5922 82997472 2020-01-29 stsp "be used" },
5923 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5924 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5925 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5926 0ebf8283 2019-07-24 stsp };
5927 0ebf8283 2019-07-24 stsp
5928 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5929 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5930 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5931 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5932 0ebf8283 2019-07-24 stsp char *logmsg;
5933 0ebf8283 2019-07-24 stsp };
5934 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5935 0ebf8283 2019-07-24 stsp
5936 0ebf8283 2019-07-24 stsp static const struct got_error *
5937 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5938 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5939 0ebf8283 2019-07-24 stsp {
5940 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5941 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5942 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5943 8138f3e1 2019-08-11 stsp int n;
5944 0ebf8283 2019-07-24 stsp
5945 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5946 0ebf8283 2019-07-24 stsp if (err)
5947 0ebf8283 2019-07-24 stsp goto done;
5948 0ebf8283 2019-07-24 stsp
5949 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5950 0ebf8283 2019-07-24 stsp if (err)
5951 0ebf8283 2019-07-24 stsp goto done;
5952 0ebf8283 2019-07-24 stsp
5953 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5954 0ebf8283 2019-07-24 stsp if (err)
5955 0ebf8283 2019-07-24 stsp goto done;
5956 0ebf8283 2019-07-24 stsp
5957 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5958 0ebf8283 2019-07-24 stsp if (n < 0)
5959 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5960 0ebf8283 2019-07-24 stsp done:
5961 0ebf8283 2019-07-24 stsp if (commit)
5962 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5963 0ebf8283 2019-07-24 stsp free(id_str);
5964 0ebf8283 2019-07-24 stsp free(logmsg);
5965 0ebf8283 2019-07-24 stsp return err;
5966 0ebf8283 2019-07-24 stsp }
5967 0ebf8283 2019-07-24 stsp
5968 0ebf8283 2019-07-24 stsp static const struct got_error *
5969 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5970 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5971 0ebf8283 2019-07-24 stsp {
5972 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5973 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5974 0ebf8283 2019-07-24 stsp
5975 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5976 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5977 0ebf8283 2019-07-24 stsp
5978 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5979 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5980 0ebf8283 2019-07-24 stsp f, repo);
5981 0ebf8283 2019-07-24 stsp if (err)
5982 0ebf8283 2019-07-24 stsp break;
5983 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5984 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5985 083957f4 2020-02-24 stsp if (n < 0) {
5986 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5987 083957f4 2020-02-24 stsp break;
5988 083957f4 2020-02-24 stsp }
5989 083957f4 2020-02-24 stsp }
5990 0ebf8283 2019-07-24 stsp }
5991 0ebf8283 2019-07-24 stsp
5992 0ebf8283 2019-07-24 stsp return err;
5993 0ebf8283 2019-07-24 stsp }
5994 0ebf8283 2019-07-24 stsp
5995 0ebf8283 2019-07-24 stsp static const struct got_error *
5996 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5997 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5998 0ebf8283 2019-07-24 stsp {
5999 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6000 0ebf8283 2019-07-24 stsp int n, i;
6001 514f2ffe 2020-01-29 stsp char *id_str;
6002 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6003 514f2ffe 2020-01-29 stsp
6004 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6005 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6006 514f2ffe 2020-01-29 stsp if (err)
6007 514f2ffe 2020-01-29 stsp return err;
6008 514f2ffe 2020-01-29 stsp
6009 514f2ffe 2020-01-29 stsp n = fprintf(f,
6010 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6011 514f2ffe 2020-01-29 stsp "# commit %s\n"
6012 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6013 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6014 514f2ffe 2020-01-29 stsp if (n < 0) {
6015 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6016 514f2ffe 2020-01-29 stsp goto done;
6017 514f2ffe 2020-01-29 stsp }
6018 0ebf8283 2019-07-24 stsp
6019 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6020 514f2ffe 2020-01-29 stsp if (n < 0) {
6021 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6022 514f2ffe 2020-01-29 stsp goto done;
6023 514f2ffe 2020-01-29 stsp }
6024 0ebf8283 2019-07-24 stsp
6025 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6026 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6027 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6028 0ebf8283 2019-07-24 stsp cmd->desc);
6029 0ebf8283 2019-07-24 stsp if (n < 0) {
6030 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6031 0ebf8283 2019-07-24 stsp break;
6032 0ebf8283 2019-07-24 stsp }
6033 0ebf8283 2019-07-24 stsp }
6034 514f2ffe 2020-01-29 stsp done:
6035 514f2ffe 2020-01-29 stsp free(id_str);
6036 0ebf8283 2019-07-24 stsp return err;
6037 0ebf8283 2019-07-24 stsp }
6038 0ebf8283 2019-07-24 stsp
6039 0ebf8283 2019-07-24 stsp static const struct got_error *
6040 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6041 0ebf8283 2019-07-24 stsp {
6042 0ebf8283 2019-07-24 stsp static char msg[42];
6043 0ebf8283 2019-07-24 stsp int ret;
6044 0ebf8283 2019-07-24 stsp
6045 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6046 0ebf8283 2019-07-24 stsp lineno);
6047 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6048 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6049 0ebf8283 2019-07-24 stsp
6050 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6051 0ebf8283 2019-07-24 stsp }
6052 0ebf8283 2019-07-24 stsp
6053 0ebf8283 2019-07-24 stsp static const struct got_error *
6054 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6055 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6056 0ebf8283 2019-07-24 stsp {
6057 0ebf8283 2019-07-24 stsp const struct got_error *err;
6058 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6059 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6060 0ebf8283 2019-07-24 stsp
6061 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6062 0ebf8283 2019-07-24 stsp if (err)
6063 0ebf8283 2019-07-24 stsp return err;
6064 0ebf8283 2019-07-24 stsp
6065 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6066 0ebf8283 2019-07-24 stsp if (err)
6067 0ebf8283 2019-07-24 stsp goto done;
6068 0ebf8283 2019-07-24 stsp
6069 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6070 5943eee2 2019-08-13 stsp if (err)
6071 5943eee2 2019-08-13 stsp goto done;
6072 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6073 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6074 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6075 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6076 0ebf8283 2019-07-24 stsp }
6077 0ebf8283 2019-07-24 stsp done:
6078 0ebf8283 2019-07-24 stsp if (folded_commit)
6079 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6080 0ebf8283 2019-07-24 stsp free(id_str);
6081 5943eee2 2019-08-13 stsp free(folded_logmsg);
6082 0ebf8283 2019-07-24 stsp return err;
6083 0ebf8283 2019-07-24 stsp }
6084 0ebf8283 2019-07-24 stsp
6085 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6086 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6087 0ebf8283 2019-07-24 stsp {
6088 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6089 0ebf8283 2019-07-24 stsp
6090 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6091 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6092 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6093 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6094 3f9de99f 2019-07-24 stsp folded = prev;
6095 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6096 0ebf8283 2019-07-24 stsp }
6097 0ebf8283 2019-07-24 stsp
6098 0ebf8283 2019-07-24 stsp return folded;
6099 0ebf8283 2019-07-24 stsp }
6100 0ebf8283 2019-07-24 stsp
6101 0ebf8283 2019-07-24 stsp static const struct got_error *
6102 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6103 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6104 0ebf8283 2019-07-24 stsp {
6105 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6106 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6107 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6108 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6109 0ebf8283 2019-07-24 stsp int fd;
6110 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6111 0ebf8283 2019-07-24 stsp
6112 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6113 0ebf8283 2019-07-24 stsp if (err)
6114 0ebf8283 2019-07-24 stsp return err;
6115 0ebf8283 2019-07-24 stsp
6116 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6117 0ebf8283 2019-07-24 stsp if (folded) {
6118 0ebf8283 2019-07-24 stsp while (folded != hle) {
6119 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6120 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6121 3f9de99f 2019-07-24 stsp continue;
6122 3f9de99f 2019-07-24 stsp }
6123 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6124 0ebf8283 2019-07-24 stsp logmsg, repo);
6125 0ebf8283 2019-07-24 stsp if (err)
6126 0ebf8283 2019-07-24 stsp goto done;
6127 0ebf8283 2019-07-24 stsp free(logmsg);
6128 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6129 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6130 0ebf8283 2019-07-24 stsp }
6131 0ebf8283 2019-07-24 stsp }
6132 0ebf8283 2019-07-24 stsp
6133 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6134 0ebf8283 2019-07-24 stsp if (err)
6135 0ebf8283 2019-07-24 stsp goto done;
6136 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6137 5943eee2 2019-08-13 stsp if (err)
6138 5943eee2 2019-08-13 stsp goto done;
6139 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6140 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6141 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6142 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6143 0ebf8283 2019-07-24 stsp goto done;
6144 0ebf8283 2019-07-24 stsp }
6145 0ebf8283 2019-07-24 stsp free(logmsg);
6146 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6147 0ebf8283 2019-07-24 stsp
6148 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6149 0ebf8283 2019-07-24 stsp if (err)
6150 0ebf8283 2019-07-24 stsp goto done;
6151 0ebf8283 2019-07-24 stsp
6152 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6153 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6154 0ebf8283 2019-07-24 stsp if (err)
6155 0ebf8283 2019-07-24 stsp goto done;
6156 0ebf8283 2019-07-24 stsp
6157 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6158 0ebf8283 2019-07-24 stsp close(fd);
6159 0ebf8283 2019-07-24 stsp
6160 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6161 0ebf8283 2019-07-24 stsp if (err)
6162 0ebf8283 2019-07-24 stsp goto done;
6163 0ebf8283 2019-07-24 stsp
6164 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6165 0ebf8283 2019-07-24 stsp if (err) {
6166 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6167 0ebf8283 2019-07-24 stsp goto done;
6168 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6169 0ebf8283 2019-07-24 stsp }
6170 0ebf8283 2019-07-24 stsp done:
6171 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6172 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6173 0ebf8283 2019-07-24 stsp free(logmsg_path);
6174 0ebf8283 2019-07-24 stsp free(logmsg);
6175 5943eee2 2019-08-13 stsp free(orig_logmsg);
6176 0ebf8283 2019-07-24 stsp free(editor);
6177 0ebf8283 2019-07-24 stsp if (commit)
6178 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6179 0ebf8283 2019-07-24 stsp return err;
6180 5ef14e63 2019-06-02 stsp }
6181 0ebf8283 2019-07-24 stsp
6182 0ebf8283 2019-07-24 stsp static const struct got_error *
6183 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6184 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6185 0ebf8283 2019-07-24 stsp {
6186 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6187 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6188 0ebf8283 2019-07-24 stsp size_t size;
6189 0ebf8283 2019-07-24 stsp ssize_t len;
6190 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6191 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6192 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6193 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6194 0ebf8283 2019-07-24 stsp
6195 0ebf8283 2019-07-24 stsp for (;;) {
6196 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6197 0ebf8283 2019-07-24 stsp if (len == -1) {
6198 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6199 0ebf8283 2019-07-24 stsp if (feof(f))
6200 0ebf8283 2019-07-24 stsp break;
6201 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6202 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6203 0ebf8283 2019-07-24 stsp break;
6204 0ebf8283 2019-07-24 stsp }
6205 0ebf8283 2019-07-24 stsp lineno++;
6206 0ebf8283 2019-07-24 stsp p = line;
6207 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6208 0ebf8283 2019-07-24 stsp p++;
6209 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6210 0ebf8283 2019-07-24 stsp free(line);
6211 0ebf8283 2019-07-24 stsp line = NULL;
6212 0ebf8283 2019-07-24 stsp continue;
6213 0ebf8283 2019-07-24 stsp }
6214 0ebf8283 2019-07-24 stsp cmd = NULL;
6215 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6216 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6217 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6218 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6219 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6220 0ebf8283 2019-07-24 stsp break;
6221 0ebf8283 2019-07-24 stsp }
6222 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6223 0ebf8283 2019-07-24 stsp p++;
6224 0ebf8283 2019-07-24 stsp break;
6225 0ebf8283 2019-07-24 stsp }
6226 0ebf8283 2019-07-24 stsp }
6227 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6228 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6229 0ebf8283 2019-07-24 stsp break;
6230 0ebf8283 2019-07-24 stsp }
6231 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6232 0ebf8283 2019-07-24 stsp p++;
6233 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6234 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6235 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6236 0ebf8283 2019-07-24 stsp break;
6237 0ebf8283 2019-07-24 stsp }
6238 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6239 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6240 0ebf8283 2019-07-24 stsp if (err)
6241 0ebf8283 2019-07-24 stsp break;
6242 0ebf8283 2019-07-24 stsp } else {
6243 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6244 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6245 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6246 0ebf8283 2019-07-24 stsp break;
6247 0ebf8283 2019-07-24 stsp }
6248 0ebf8283 2019-07-24 stsp }
6249 0ebf8283 2019-07-24 stsp free(line);
6250 0ebf8283 2019-07-24 stsp line = NULL;
6251 0ebf8283 2019-07-24 stsp continue;
6252 0ebf8283 2019-07-24 stsp } else {
6253 0ebf8283 2019-07-24 stsp end = p;
6254 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6255 0ebf8283 2019-07-24 stsp end++;
6256 0ebf8283 2019-07-24 stsp *end = '\0';
6257 0ebf8283 2019-07-24 stsp
6258 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6259 0ebf8283 2019-07-24 stsp if (err) {
6260 0ebf8283 2019-07-24 stsp /* override error code */
6261 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6262 0ebf8283 2019-07-24 stsp break;
6263 0ebf8283 2019-07-24 stsp }
6264 0ebf8283 2019-07-24 stsp }
6265 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6266 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6267 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6268 0ebf8283 2019-07-24 stsp break;
6269 0ebf8283 2019-07-24 stsp }
6270 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6271 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6272 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6273 0ebf8283 2019-07-24 stsp commit_id = NULL;
6274 0ebf8283 2019-07-24 stsp free(line);
6275 0ebf8283 2019-07-24 stsp line = NULL;
6276 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6277 0ebf8283 2019-07-24 stsp }
6278 0ebf8283 2019-07-24 stsp
6279 0ebf8283 2019-07-24 stsp free(line);
6280 0ebf8283 2019-07-24 stsp free(commit_id);
6281 0ebf8283 2019-07-24 stsp return err;
6282 0ebf8283 2019-07-24 stsp }
6283 0ebf8283 2019-07-24 stsp
6284 0ebf8283 2019-07-24 stsp static const struct got_error *
6285 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6286 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6287 bfce7f83 2019-07-27 stsp {
6288 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6289 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6290 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6291 5b87815e 2020-03-05 stsp static char msg[92];
6292 bfce7f83 2019-07-27 stsp char *id_str;
6293 bfce7f83 2019-07-27 stsp
6294 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6295 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6296 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6297 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6298 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6299 5b87815e 2020-03-05 stsp
6300 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6301 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6302 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6303 5b87815e 2020-03-05 stsp if (hle == hle2)
6304 5b87815e 2020-03-05 stsp continue;
6305 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6306 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6307 5b87815e 2020-03-05 stsp continue;
6308 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6309 5b87815e 2020-03-05 stsp if (err)
6310 5b87815e 2020-03-05 stsp return err;
6311 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6312 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6313 5b87815e 2020-03-05 stsp free(id_str);
6314 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6315 5b87815e 2020-03-05 stsp }
6316 5b87815e 2020-03-05 stsp }
6317 bfce7f83 2019-07-27 stsp
6318 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6319 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6320 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6321 bfce7f83 2019-07-27 stsp break;
6322 bfce7f83 2019-07-27 stsp }
6323 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6324 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6325 bfce7f83 2019-07-27 stsp if (err)
6326 bfce7f83 2019-07-27 stsp return err;
6327 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6328 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6329 bfce7f83 2019-07-27 stsp free(id_str);
6330 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6331 bfce7f83 2019-07-27 stsp }
6332 bfce7f83 2019-07-27 stsp }
6333 bfce7f83 2019-07-27 stsp
6334 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6335 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6336 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6337 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6338 bfce7f83 2019-07-27 stsp
6339 bfce7f83 2019-07-27 stsp return NULL;
6340 bfce7f83 2019-07-27 stsp }
6341 bfce7f83 2019-07-27 stsp
6342 bfce7f83 2019-07-27 stsp static const struct got_error *
6343 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6344 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6345 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6346 0ebf8283 2019-07-24 stsp {
6347 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6348 0ebf8283 2019-07-24 stsp char *editor;
6349 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6350 0ebf8283 2019-07-24 stsp
6351 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6352 0ebf8283 2019-07-24 stsp if (err)
6353 0ebf8283 2019-07-24 stsp return err;
6354 0ebf8283 2019-07-24 stsp
6355 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6356 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6357 0ebf8283 2019-07-24 stsp goto done;
6358 0ebf8283 2019-07-24 stsp }
6359 0ebf8283 2019-07-24 stsp
6360 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6361 0ebf8283 2019-07-24 stsp if (f == NULL) {
6362 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6363 0ebf8283 2019-07-24 stsp goto done;
6364 0ebf8283 2019-07-24 stsp }
6365 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6366 bfce7f83 2019-07-27 stsp if (err)
6367 bfce7f83 2019-07-27 stsp goto done;
6368 bfce7f83 2019-07-27 stsp
6369 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6370 0ebf8283 2019-07-24 stsp done:
6371 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6372 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6373 0ebf8283 2019-07-24 stsp free(editor);
6374 0ebf8283 2019-07-24 stsp return err;
6375 0ebf8283 2019-07-24 stsp }
6376 0ebf8283 2019-07-24 stsp
6377 0ebf8283 2019-07-24 stsp static const struct got_error *
6378 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6379 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6380 514f2ffe 2020-01-29 stsp struct got_repository *);
6381 0ebf8283 2019-07-24 stsp
6382 0ebf8283 2019-07-24 stsp static const struct got_error *
6383 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6384 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6385 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6386 0ebf8283 2019-07-24 stsp {
6387 0ebf8283 2019-07-24 stsp const struct got_error *err;
6388 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6389 0ebf8283 2019-07-24 stsp char *path = NULL;
6390 0ebf8283 2019-07-24 stsp
6391 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6392 0ebf8283 2019-07-24 stsp if (err)
6393 0ebf8283 2019-07-24 stsp return err;
6394 0ebf8283 2019-07-24 stsp
6395 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6396 0ebf8283 2019-07-24 stsp if (err)
6397 0ebf8283 2019-07-24 stsp goto done;
6398 0ebf8283 2019-07-24 stsp
6399 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6400 0ebf8283 2019-07-24 stsp if (err)
6401 0ebf8283 2019-07-24 stsp goto done;
6402 0ebf8283 2019-07-24 stsp
6403 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6404 083957f4 2020-02-24 stsp rewind(f);
6405 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6406 083957f4 2020-02-24 stsp } else {
6407 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6408 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6409 0ebf8283 2019-07-24 stsp goto done;
6410 083957f4 2020-02-24 stsp }
6411 083957f4 2020-02-24 stsp f = NULL;
6412 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6413 083957f4 2020-02-24 stsp if (err) {
6414 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6415 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6416 083957f4 2020-02-24 stsp goto done;
6417 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6418 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6419 083957f4 2020-02-24 stsp }
6420 0ebf8283 2019-07-24 stsp }
6421 0ebf8283 2019-07-24 stsp done:
6422 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6423 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6424 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6425 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6426 0ebf8283 2019-07-24 stsp free(path);
6427 0ebf8283 2019-07-24 stsp return err;
6428 0ebf8283 2019-07-24 stsp }
6429 0ebf8283 2019-07-24 stsp
6430 0ebf8283 2019-07-24 stsp static const struct got_error *
6431 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6432 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6433 0ebf8283 2019-07-24 stsp {
6434 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6435 0ebf8283 2019-07-24 stsp char *path = NULL;
6436 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6437 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6438 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6439 0ebf8283 2019-07-24 stsp
6440 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6441 0ebf8283 2019-07-24 stsp if (err)
6442 0ebf8283 2019-07-24 stsp return err;
6443 0ebf8283 2019-07-24 stsp
6444 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6445 0ebf8283 2019-07-24 stsp if (f == NULL) {
6446 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6447 0ebf8283 2019-07-24 stsp goto done;
6448 0ebf8283 2019-07-24 stsp }
6449 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6450 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6451 0ebf8283 2019-07-24 stsp repo);
6452 0ebf8283 2019-07-24 stsp if (err)
6453 0ebf8283 2019-07-24 stsp break;
6454 0ebf8283 2019-07-24 stsp
6455 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6456 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6457 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6458 0ebf8283 2019-07-24 stsp if (n < 0) {
6459 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6460 0ebf8283 2019-07-24 stsp break;
6461 0ebf8283 2019-07-24 stsp }
6462 0ebf8283 2019-07-24 stsp }
6463 0ebf8283 2019-07-24 stsp }
6464 0ebf8283 2019-07-24 stsp done:
6465 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6466 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6467 0ebf8283 2019-07-24 stsp free(path);
6468 0ebf8283 2019-07-24 stsp if (commit)
6469 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6470 0ebf8283 2019-07-24 stsp return err;
6471 0ebf8283 2019-07-24 stsp }
6472 0ebf8283 2019-07-24 stsp
6473 bfce7f83 2019-07-27 stsp void
6474 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6475 bfce7f83 2019-07-27 stsp {
6476 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6477 bfce7f83 2019-07-27 stsp
6478 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6479 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6480 bfce7f83 2019-07-27 stsp free(hle);
6481 bfce7f83 2019-07-27 stsp }
6482 bfce7f83 2019-07-27 stsp }
6483 bfce7f83 2019-07-27 stsp
6484 0ebf8283 2019-07-24 stsp static const struct got_error *
6485 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6486 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6487 0ebf8283 2019-07-24 stsp {
6488 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6489 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6490 0ebf8283 2019-07-24 stsp
6491 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6492 0ebf8283 2019-07-24 stsp if (f == NULL) {
6493 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6494 0ebf8283 2019-07-24 stsp goto done;
6495 0ebf8283 2019-07-24 stsp }
6496 0ebf8283 2019-07-24 stsp
6497 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6498 0ebf8283 2019-07-24 stsp done:
6499 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6500 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6501 0ebf8283 2019-07-24 stsp return err;
6502 0ebf8283 2019-07-24 stsp }
6503 0ebf8283 2019-07-24 stsp
6504 0ebf8283 2019-07-24 stsp static const struct got_error *
6505 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6506 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6507 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6508 0ebf8283 2019-07-24 stsp {
6509 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6510 0ebf8283 2019-07-24 stsp int resp = ' ';
6511 0ebf8283 2019-07-24 stsp
6512 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6513 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6514 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6515 0ebf8283 2019-07-24 stsp resp = getchar();
6516 a61a4414 2019-08-07 stsp if (resp == '\n')
6517 a61a4414 2019-08-07 stsp resp = getchar();
6518 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6519 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6520 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6521 bfce7f83 2019-07-27 stsp repo);
6522 426ebf2e 2019-07-25 stsp if (err) {
6523 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6524 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6525 426ebf2e 2019-07-25 stsp break;
6526 bfce7f83 2019-07-27 stsp prev_err = err;
6527 426ebf2e 2019-07-25 stsp resp = ' ';
6528 426ebf2e 2019-07-25 stsp continue;
6529 426ebf2e 2019-07-25 stsp }
6530 bfce7f83 2019-07-27 stsp break;
6531 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6532 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6533 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6534 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6535 426ebf2e 2019-07-25 stsp if (err) {
6536 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6537 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6538 426ebf2e 2019-07-25 stsp break;
6539 bfce7f83 2019-07-27 stsp prev_err = err;
6540 426ebf2e 2019-07-25 stsp resp = ' ';
6541 426ebf2e 2019-07-25 stsp continue;
6542 426ebf2e 2019-07-25 stsp }
6543 bfce7f83 2019-07-27 stsp break;
6544 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6545 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6546 0ebf8283 2019-07-24 stsp break;
6547 426ebf2e 2019-07-25 stsp } else
6548 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6549 0ebf8283 2019-07-24 stsp }
6550 0ebf8283 2019-07-24 stsp
6551 0ebf8283 2019-07-24 stsp return err;
6552 0ebf8283 2019-07-24 stsp }
6553 0ebf8283 2019-07-24 stsp
6554 0ebf8283 2019-07-24 stsp static const struct got_error *
6555 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6556 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6557 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6558 0ebf8283 2019-07-24 stsp {
6559 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6560 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6561 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6562 3e3a69f1 2019-07-25 stsp branch, repo);
6563 0ebf8283 2019-07-24 stsp }
6564 0ebf8283 2019-07-24 stsp
6565 0ebf8283 2019-07-24 stsp static const struct got_error *
6566 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6567 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6568 0ebf8283 2019-07-24 stsp {
6569 0ebf8283 2019-07-24 stsp const struct got_error *err;
6570 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6571 0ebf8283 2019-07-24 stsp
6572 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6573 0ebf8283 2019-07-24 stsp if (err)
6574 0ebf8283 2019-07-24 stsp goto done;
6575 0ebf8283 2019-07-24 stsp
6576 0ebf8283 2019-07-24 stsp if (new_id) {
6577 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6578 0ebf8283 2019-07-24 stsp if (err)
6579 0ebf8283 2019-07-24 stsp goto done;
6580 0ebf8283 2019-07-24 stsp }
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6583 0ebf8283 2019-07-24 stsp if (new_id_str)
6584 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6585 0ebf8283 2019-07-24 stsp
6586 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6587 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6588 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6589 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6590 0ebf8283 2019-07-24 stsp goto done;
6591 0ebf8283 2019-07-24 stsp }
6592 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6593 0ebf8283 2019-07-24 stsp } else {
6594 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6595 0ebf8283 2019-07-24 stsp if (err)
6596 0ebf8283 2019-07-24 stsp goto done;
6597 0ebf8283 2019-07-24 stsp }
6598 0ebf8283 2019-07-24 stsp
6599 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6600 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6601 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6602 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6603 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6604 0ebf8283 2019-07-24 stsp break;
6605 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6606 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6607 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6608 0ebf8283 2019-07-24 stsp logmsg);
6609 0ebf8283 2019-07-24 stsp break;
6610 0ebf8283 2019-07-24 stsp default:
6611 0ebf8283 2019-07-24 stsp break;
6612 0ebf8283 2019-07-24 stsp }
6613 0ebf8283 2019-07-24 stsp done:
6614 0ebf8283 2019-07-24 stsp free(old_id_str);
6615 0ebf8283 2019-07-24 stsp free(new_id_str);
6616 0ebf8283 2019-07-24 stsp return err;
6617 0ebf8283 2019-07-24 stsp }
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp static const struct got_error *
6620 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6621 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6622 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6623 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6624 0ebf8283 2019-07-24 stsp {
6625 0ebf8283 2019-07-24 stsp const struct got_error *err;
6626 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6627 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6628 0ebf8283 2019-07-24 stsp
6629 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6630 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6631 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6632 0ebf8283 2019-07-24 stsp if (err)
6633 0ebf8283 2019-07-24 stsp return err;
6634 0ebf8283 2019-07-24 stsp }
6635 0ebf8283 2019-07-24 stsp
6636 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6637 0ebf8283 2019-07-24 stsp if (err)
6638 0ebf8283 2019-07-24 stsp return err;
6639 0ebf8283 2019-07-24 stsp
6640 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6641 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6642 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6643 0ebf8283 2019-07-24 stsp if (err) {
6644 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6645 0ebf8283 2019-07-24 stsp goto done;
6646 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6647 0ebf8283 2019-07-24 stsp } else {
6648 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6649 0ebf8283 2019-07-24 stsp free(new_commit_id);
6650 0ebf8283 2019-07-24 stsp }
6651 0ebf8283 2019-07-24 stsp done:
6652 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6653 0ebf8283 2019-07-24 stsp return err;
6654 0ebf8283 2019-07-24 stsp }
6655 0ebf8283 2019-07-24 stsp
6656 0ebf8283 2019-07-24 stsp static const struct got_error *
6657 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6658 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6659 0ebf8283 2019-07-24 stsp {
6660 0ebf8283 2019-07-24 stsp const struct got_error *error;
6661 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6662 0ebf8283 2019-07-24 stsp
6663 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6664 0ebf8283 2019-07-24 stsp repo);
6665 0ebf8283 2019-07-24 stsp if (error)
6666 0ebf8283 2019-07-24 stsp return error;
6667 0ebf8283 2019-07-24 stsp
6668 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6669 0ebf8283 2019-07-24 stsp if (error)
6670 0ebf8283 2019-07-24 stsp return error;
6671 0ebf8283 2019-07-24 stsp
6672 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6673 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6674 0ebf8283 2019-07-24 stsp return error;
6675 ab20a43a 2020-01-29 stsp }
6676 ab20a43a 2020-01-29 stsp
6677 ab20a43a 2020-01-29 stsp static const struct got_error *
6678 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6679 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6680 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6681 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6682 ab20a43a 2020-01-29 stsp {
6683 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6684 ab20a43a 2020-01-29 stsp
6685 ab20a43a 2020-01-29 stsp switch (status) {
6686 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6687 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6688 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6689 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6690 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6691 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6692 ab20a43a 2020-01-29 stsp default:
6693 ab20a43a 2020-01-29 stsp break;
6694 ab20a43a 2020-01-29 stsp }
6695 ab20a43a 2020-01-29 stsp
6696 ab20a43a 2020-01-29 stsp switch (staged_status) {
6697 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6698 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6699 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6700 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6701 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6702 ab20a43a 2020-01-29 stsp default:
6703 ab20a43a 2020-01-29 stsp break;
6704 ab20a43a 2020-01-29 stsp }
6705 ab20a43a 2020-01-29 stsp
6706 ab20a43a 2020-01-29 stsp return NULL;
6707 0ebf8283 2019-07-24 stsp }
6708 0ebf8283 2019-07-24 stsp
6709 0ebf8283 2019-07-24 stsp static const struct got_error *
6710 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6711 0ebf8283 2019-07-24 stsp {
6712 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6713 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6714 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6715 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6716 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6717 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6718 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6719 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6720 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6721 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6722 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6723 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6724 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6725 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6726 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6727 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6728 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6729 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6730 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6731 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6732 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6733 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6734 0ebf8283 2019-07-24 stsp
6735 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6736 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6737 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6738 0ebf8283 2019-07-24 stsp
6739 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6740 0ebf8283 2019-07-24 stsp switch (ch) {
6741 0ebf8283 2019-07-24 stsp case 'a':
6742 0ebf8283 2019-07-24 stsp abort_edit = 1;
6743 0ebf8283 2019-07-24 stsp break;
6744 0ebf8283 2019-07-24 stsp case 'c':
6745 0ebf8283 2019-07-24 stsp continue_edit = 1;
6746 0ebf8283 2019-07-24 stsp break;
6747 0ebf8283 2019-07-24 stsp case 'F':
6748 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6749 0ebf8283 2019-07-24 stsp break;
6750 083957f4 2020-02-24 stsp case 'm':
6751 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6752 083957f4 2020-02-24 stsp break;
6753 0ebf8283 2019-07-24 stsp default:
6754 0ebf8283 2019-07-24 stsp usage_histedit();
6755 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6756 0ebf8283 2019-07-24 stsp }
6757 0ebf8283 2019-07-24 stsp }
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp argc -= optind;
6760 0ebf8283 2019-07-24 stsp argv += optind;
6761 0ebf8283 2019-07-24 stsp
6762 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6763 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6764 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6765 0ebf8283 2019-07-24 stsp err(1, "pledge");
6766 0ebf8283 2019-07-24 stsp #endif
6767 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6768 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6769 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6770 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6771 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6772 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6773 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6774 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6775 0ebf8283 2019-07-24 stsp if (argc != 0)
6776 0ebf8283 2019-07-24 stsp usage_histedit();
6777 0ebf8283 2019-07-24 stsp
6778 0ebf8283 2019-07-24 stsp /*
6779 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6780 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6781 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6782 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6783 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6784 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6785 0ebf8283 2019-07-24 stsp */
6786 0ebf8283 2019-07-24 stsp
6787 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6788 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6789 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6790 0ebf8283 2019-07-24 stsp goto done;
6791 0ebf8283 2019-07-24 stsp }
6792 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6793 0ebf8283 2019-07-24 stsp if (error)
6794 0ebf8283 2019-07-24 stsp goto done;
6795 0ebf8283 2019-07-24 stsp
6796 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6797 c9956ddf 2019-09-08 stsp NULL);
6798 0ebf8283 2019-07-24 stsp if (error != NULL)
6799 0ebf8283 2019-07-24 stsp goto done;
6800 0ebf8283 2019-07-24 stsp
6801 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6802 0ebf8283 2019-07-24 stsp if (error)
6803 0ebf8283 2019-07-24 stsp goto done;
6804 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6805 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6806 0ebf8283 2019-07-24 stsp goto done;
6807 0ebf8283 2019-07-24 stsp }
6808 0ebf8283 2019-07-24 stsp
6809 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6810 0ebf8283 2019-07-24 stsp if (error)
6811 0ebf8283 2019-07-24 stsp goto done;
6812 0ebf8283 2019-07-24 stsp
6813 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6814 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6815 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6816 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6817 083957f4 2020-02-24 stsp "before the -m option can be used");
6818 083957f4 2020-02-24 stsp goto done;
6819 083957f4 2020-02-24 stsp }
6820 083957f4 2020-02-24 stsp
6821 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6822 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6823 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6824 3e3a69f1 2019-07-25 stsp worktree, repo);
6825 0ebf8283 2019-07-24 stsp if (error)
6826 0ebf8283 2019-07-24 stsp goto done;
6827 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6828 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6829 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6830 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6831 0ebf8283 2019-07-24 stsp if (error)
6832 0ebf8283 2019-07-24 stsp goto done;
6833 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6834 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6835 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6836 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6837 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6838 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp }
6840 0ebf8283 2019-07-24 stsp
6841 0ebf8283 2019-07-24 stsp if (continue_edit) {
6842 0ebf8283 2019-07-24 stsp char *path;
6843 0ebf8283 2019-07-24 stsp
6844 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6845 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6846 0ebf8283 2019-07-24 stsp goto done;
6847 0ebf8283 2019-07-24 stsp }
6848 0ebf8283 2019-07-24 stsp
6849 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6850 0ebf8283 2019-07-24 stsp if (error)
6851 0ebf8283 2019-07-24 stsp goto done;
6852 0ebf8283 2019-07-24 stsp
6853 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6854 0ebf8283 2019-07-24 stsp free(path);
6855 0ebf8283 2019-07-24 stsp if (error)
6856 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp
6858 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6859 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6860 3e3a69f1 2019-07-25 stsp worktree, repo);
6861 0ebf8283 2019-07-24 stsp if (error)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6865 0ebf8283 2019-07-24 stsp if (error)
6866 0ebf8283 2019-07-24 stsp goto done;
6867 0ebf8283 2019-07-24 stsp
6868 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6869 0ebf8283 2019-07-24 stsp head_commit_id);
6870 0ebf8283 2019-07-24 stsp if (error)
6871 0ebf8283 2019-07-24 stsp goto done;
6872 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6873 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6874 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6875 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6876 3aac7cf7 2019-07-25 stsp goto done;
6877 3aac7cf7 2019-07-25 stsp }
6878 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6879 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6880 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6881 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6882 0ebf8283 2019-07-24 stsp commit = NULL;
6883 0ebf8283 2019-07-24 stsp if (error)
6884 0ebf8283 2019-07-24 stsp goto done;
6885 0ebf8283 2019-07-24 stsp } else {
6886 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6887 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6888 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp }
6890 0ebf8283 2019-07-24 stsp
6891 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6892 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6893 0ebf8283 2019-07-24 stsp if (error != NULL)
6894 0ebf8283 2019-07-24 stsp goto done;
6895 0ebf8283 2019-07-24 stsp
6896 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6897 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6898 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6899 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6900 c7d20a3f 2019-07-30 stsp goto done;
6901 c7d20a3f 2019-07-30 stsp }
6902 c7d20a3f 2019-07-30 stsp
6903 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6904 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6905 bfce7f83 2019-07-27 stsp branch = NULL;
6906 0ebf8283 2019-07-24 stsp if (error)
6907 0ebf8283 2019-07-24 stsp goto done;
6908 0ebf8283 2019-07-24 stsp
6909 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6910 0ebf8283 2019-07-24 stsp head_commit_id);
6911 0ebf8283 2019-07-24 stsp if (error)
6912 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6914 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6915 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6916 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6917 3aac7cf7 2019-07-25 stsp goto done;
6918 3aac7cf7 2019-07-25 stsp }
6919 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6920 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6921 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6922 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6923 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6924 0ebf8283 2019-07-24 stsp commit = NULL;
6925 0ebf8283 2019-07-24 stsp if (error)
6926 0ebf8283 2019-07-24 stsp goto done;
6927 0ebf8283 2019-07-24 stsp
6928 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6929 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6930 c5996fff 2020-01-29 stsp goto done;
6931 c5996fff 2020-01-29 stsp }
6932 c5996fff 2020-01-29 stsp
6933 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6934 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6935 bfce7f83 2019-07-27 stsp if (error)
6936 bfce7f83 2019-07-27 stsp goto done;
6937 bfce7f83 2019-07-27 stsp
6938 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6939 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6940 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6941 6ba2bea2 2019-07-27 stsp if (error) {
6942 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6943 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6944 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6945 0ebf8283 2019-07-24 stsp goto done;
6946 6ba2bea2 2019-07-27 stsp }
6947 0ebf8283 2019-07-24 stsp } else {
6948 514f2ffe 2020-01-29 stsp const char *branch_name;
6949 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6950 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6951 514f2ffe 2020-01-29 stsp branch_name += 11;
6952 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6953 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6954 6ba2bea2 2019-07-27 stsp if (error) {
6955 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6956 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6957 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6958 0ebf8283 2019-07-24 stsp goto done;
6959 6ba2bea2 2019-07-27 stsp }
6960 0ebf8283 2019-07-24 stsp
6961 0ebf8283 2019-07-24 stsp }
6962 0ebf8283 2019-07-24 stsp
6963 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6964 0ebf8283 2019-07-24 stsp repo);
6965 6ba2bea2 2019-07-27 stsp if (error) {
6966 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6967 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6968 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6969 0ebf8283 2019-07-24 stsp goto done;
6970 6ba2bea2 2019-07-27 stsp }
6971 0ebf8283 2019-07-24 stsp
6972 0ebf8283 2019-07-24 stsp }
6973 0ebf8283 2019-07-24 stsp
6974 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6975 4ec14e60 2019-08-28 hiltjo if (error)
6976 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp
6978 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6979 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6980 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6981 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6982 0ebf8283 2019-07-24 stsp continue;
6983 0ebf8283 2019-07-24 stsp
6984 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6985 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6986 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6987 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6988 0ebf8283 2019-07-24 stsp repo);
6989 ab20a43a 2020-01-29 stsp if (error)
6990 ab20a43a 2020-01-29 stsp goto done;
6991 0ebf8283 2019-07-24 stsp } else {
6992 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6993 ab20a43a 2020-01-29 stsp int have_changes = 0;
6994 ab20a43a 2020-01-29 stsp
6995 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6996 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6997 ab20a43a 2020-01-29 stsp if (error)
6998 ab20a43a 2020-01-29 stsp goto done;
6999 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7000 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7001 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7002 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7003 ab20a43a 2020-01-29 stsp if (error) {
7004 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7005 ab20a43a 2020-01-29 stsp goto done;
7006 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7007 ab20a43a 2020-01-29 stsp goto done;
7008 ab20a43a 2020-01-29 stsp }
7009 ab20a43a 2020-01-29 stsp if (have_changes) {
7010 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7011 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7012 ab20a43a 2020-01-29 stsp if (error)
7013 ab20a43a 2020-01-29 stsp goto done;
7014 ab20a43a 2020-01-29 stsp } else {
7015 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7016 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7017 ab20a43a 2020-01-29 stsp if (error)
7018 ab20a43a 2020-01-29 stsp goto done;
7019 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7020 ab20a43a 2020-01-29 stsp hle, NULL);
7021 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7022 ab20a43a 2020-01-29 stsp commit = NULL;
7023 ab20a43a 2020-01-29 stsp if (error)
7024 ab20a43a 2020-01-29 stsp goto done;
7025 ab20a43a 2020-01-29 stsp }
7026 0ebf8283 2019-07-24 stsp }
7027 0ebf8283 2019-07-24 stsp continue;
7028 0ebf8283 2019-07-24 stsp }
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7031 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7032 0ebf8283 2019-07-24 stsp if (error)
7033 0ebf8283 2019-07-24 stsp goto done;
7034 0ebf8283 2019-07-24 stsp continue;
7035 0ebf8283 2019-07-24 stsp }
7036 0ebf8283 2019-07-24 stsp
7037 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7038 0ebf8283 2019-07-24 stsp hle->commit_id);
7039 0ebf8283 2019-07-24 stsp if (error)
7040 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7042 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7045 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7046 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7047 0ebf8283 2019-07-24 stsp if (error)
7048 0ebf8283 2019-07-24 stsp goto done;
7049 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7050 0ebf8283 2019-07-24 stsp commit = NULL;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7053 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7054 a0ea4fc0 2020-02-28 stsp repo);
7055 a0ea4fc0 2020-02-28 stsp if (error)
7056 a0ea4fc0 2020-02-28 stsp goto done;
7057 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7058 0ebf8283 2019-07-24 stsp break;
7059 0ebf8283 2019-07-24 stsp }
7060 0ebf8283 2019-07-24 stsp
7061 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7062 0ebf8283 2019-07-24 stsp char *id_str;
7063 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7064 0ebf8283 2019-07-24 stsp if (error)
7065 0ebf8283 2019-07-24 stsp goto done;
7066 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7067 0ebf8283 2019-07-24 stsp id_str);
7068 0ebf8283 2019-07-24 stsp free(id_str);
7069 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7070 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7071 3e3a69f1 2019-07-25 stsp fileindex);
7072 0ebf8283 2019-07-24 stsp goto done;
7073 0ebf8283 2019-07-24 stsp }
7074 0ebf8283 2019-07-24 stsp
7075 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7076 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7077 0ebf8283 2019-07-24 stsp if (error)
7078 0ebf8283 2019-07-24 stsp goto done;
7079 0ebf8283 2019-07-24 stsp continue;
7080 0ebf8283 2019-07-24 stsp }
7081 0ebf8283 2019-07-24 stsp
7082 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7083 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7084 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7085 0ebf8283 2019-07-24 stsp if (error)
7086 0ebf8283 2019-07-24 stsp goto done;
7087 0ebf8283 2019-07-24 stsp }
7088 0ebf8283 2019-07-24 stsp
7089 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7090 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7091 0ebf8283 2019-07-24 stsp if (error)
7092 0ebf8283 2019-07-24 stsp goto done;
7093 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7094 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7095 0ebf8283 2019-07-24 stsp } else
7096 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7097 3e3a69f1 2019-07-25 stsp branch, repo);
7098 0ebf8283 2019-07-24 stsp done:
7099 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7100 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7101 0ebf8283 2019-07-24 stsp free(head_commit_id);
7102 0ebf8283 2019-07-24 stsp free(base_commit_id);
7103 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7104 0ebf8283 2019-07-24 stsp if (commit)
7105 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7106 0ebf8283 2019-07-24 stsp if (branch)
7107 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7108 0ebf8283 2019-07-24 stsp if (tmp_branch)
7109 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7110 0ebf8283 2019-07-24 stsp if (worktree)
7111 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7112 2822a352 2019-10-15 stsp if (repo)
7113 2822a352 2019-10-15 stsp got_repo_close(repo);
7114 2822a352 2019-10-15 stsp return error;
7115 2822a352 2019-10-15 stsp }
7116 2822a352 2019-10-15 stsp
7117 2822a352 2019-10-15 stsp __dead static void
7118 2822a352 2019-10-15 stsp usage_integrate(void)
7119 2822a352 2019-10-15 stsp {
7120 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7121 2822a352 2019-10-15 stsp exit(1);
7122 2822a352 2019-10-15 stsp }
7123 2822a352 2019-10-15 stsp
7124 2822a352 2019-10-15 stsp static const struct got_error *
7125 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7126 2822a352 2019-10-15 stsp {
7127 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7128 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7129 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7130 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7131 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7132 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7133 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7134 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7135 2822a352 2019-10-15 stsp int ch, did_something = 0;
7136 2822a352 2019-10-15 stsp
7137 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7138 2822a352 2019-10-15 stsp switch (ch) {
7139 2822a352 2019-10-15 stsp default:
7140 2822a352 2019-10-15 stsp usage_integrate();
7141 2822a352 2019-10-15 stsp /* NOTREACHED */
7142 2822a352 2019-10-15 stsp }
7143 2822a352 2019-10-15 stsp }
7144 2822a352 2019-10-15 stsp
7145 2822a352 2019-10-15 stsp argc -= optind;
7146 2822a352 2019-10-15 stsp argv += optind;
7147 2822a352 2019-10-15 stsp
7148 2822a352 2019-10-15 stsp if (argc != 1)
7149 2822a352 2019-10-15 stsp usage_integrate();
7150 2822a352 2019-10-15 stsp branch_arg = argv[0];
7151 2822a352 2019-10-15 stsp
7152 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7153 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7154 2822a352 2019-10-15 stsp err(1, "pledge");
7155 2822a352 2019-10-15 stsp
7156 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7157 2822a352 2019-10-15 stsp if (cwd == NULL) {
7158 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7159 2822a352 2019-10-15 stsp goto done;
7160 2822a352 2019-10-15 stsp }
7161 2822a352 2019-10-15 stsp
7162 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7163 2822a352 2019-10-15 stsp if (error)
7164 2822a352 2019-10-15 stsp goto done;
7165 2822a352 2019-10-15 stsp
7166 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7167 2822a352 2019-10-15 stsp if (error)
7168 2822a352 2019-10-15 stsp goto done;
7169 2822a352 2019-10-15 stsp
7170 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7171 2822a352 2019-10-15 stsp NULL);
7172 2822a352 2019-10-15 stsp if (error != NULL)
7173 2822a352 2019-10-15 stsp goto done;
7174 2822a352 2019-10-15 stsp
7175 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7176 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7177 2822a352 2019-10-15 stsp if (error)
7178 2822a352 2019-10-15 stsp goto done;
7179 2822a352 2019-10-15 stsp
7180 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7181 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7182 2822a352 2019-10-15 stsp goto done;
7183 2822a352 2019-10-15 stsp }
7184 2822a352 2019-10-15 stsp
7185 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7186 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7187 2822a352 2019-10-15 stsp if (error)
7188 2822a352 2019-10-15 stsp goto done;
7189 2822a352 2019-10-15 stsp
7190 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7191 2822a352 2019-10-15 stsp if (refname == NULL) {
7192 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7193 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7194 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7195 2822a352 2019-10-15 stsp goto done;
7196 2822a352 2019-10-15 stsp }
7197 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7198 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7199 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7200 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7201 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7202 2822a352 2019-10-15 stsp goto done;
7203 2822a352 2019-10-15 stsp }
7204 2822a352 2019-10-15 stsp
7205 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7206 2822a352 2019-10-15 stsp if (error)
7207 2822a352 2019-10-15 stsp goto done;
7208 2822a352 2019-10-15 stsp
7209 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7210 2822a352 2019-10-15 stsp if (error)
7211 2822a352 2019-10-15 stsp goto done;
7212 2822a352 2019-10-15 stsp
7213 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7214 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7215 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7216 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7217 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7218 2822a352 2019-10-15 stsp goto done;
7219 2822a352 2019-10-15 stsp }
7220 2822a352 2019-10-15 stsp
7221 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7222 2822a352 2019-10-15 stsp if (error) {
7223 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7224 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7225 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7226 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
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_integrate_continue(worktree, fileindex, repo,
7231 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7232 2822a352 2019-10-15 stsp check_cancelled, NULL);
7233 2822a352 2019-10-15 stsp if (error)
7234 2822a352 2019-10-15 stsp goto done;
7235 2822a352 2019-10-15 stsp
7236 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7237 2822a352 2019-10-15 stsp done:
7238 715dc77e 2019-08-03 stsp if (repo)
7239 715dc77e 2019-08-03 stsp got_repo_close(repo);
7240 2822a352 2019-10-15 stsp if (worktree)
7241 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7242 2822a352 2019-10-15 stsp free(cwd);
7243 2822a352 2019-10-15 stsp free(base_commit_id);
7244 2822a352 2019-10-15 stsp free(commit_id);
7245 2822a352 2019-10-15 stsp free(refname);
7246 2822a352 2019-10-15 stsp free(base_refname);
7247 715dc77e 2019-08-03 stsp return error;
7248 715dc77e 2019-08-03 stsp }
7249 715dc77e 2019-08-03 stsp
7250 715dc77e 2019-08-03 stsp __dead static void
7251 715dc77e 2019-08-03 stsp usage_stage(void)
7252 715dc77e 2019-08-03 stsp {
7253 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7254 f3055044 2019-08-07 stsp "[file-path ...]\n",
7255 715dc77e 2019-08-03 stsp getprogname());
7256 715dc77e 2019-08-03 stsp exit(1);
7257 715dc77e 2019-08-03 stsp }
7258 715dc77e 2019-08-03 stsp
7259 715dc77e 2019-08-03 stsp static const struct got_error *
7260 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7261 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7262 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7263 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7264 408b4ebc 2019-08-03 stsp {
7265 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7266 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7267 408b4ebc 2019-08-03 stsp
7268 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7269 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7270 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7271 408b4ebc 2019-08-03 stsp return NULL;
7272 408b4ebc 2019-08-03 stsp
7273 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7274 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7275 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7276 408b4ebc 2019-08-03 stsp else
7277 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7278 408b4ebc 2019-08-03 stsp if (err)
7279 408b4ebc 2019-08-03 stsp return err;
7280 408b4ebc 2019-08-03 stsp
7281 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7282 408b4ebc 2019-08-03 stsp free(id_str);
7283 dc424a06 2019-08-07 stsp return NULL;
7284 dc424a06 2019-08-07 stsp }
7285 2e1f37b0 2019-08-08 stsp
7286 dc424a06 2019-08-07 stsp static const struct got_error *
7287 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7288 715dc77e 2019-08-03 stsp {
7289 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7290 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7291 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7292 715dc77e 2019-08-03 stsp char *cwd = NULL;
7293 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7294 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7295 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7296 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7297 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7298 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7299 715dc77e 2019-08-03 stsp
7300 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7301 715dc77e 2019-08-03 stsp
7302 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7303 715dc77e 2019-08-03 stsp switch (ch) {
7304 408b4ebc 2019-08-03 stsp case 'l':
7305 408b4ebc 2019-08-03 stsp list_stage = 1;
7306 408b4ebc 2019-08-03 stsp break;
7307 dc424a06 2019-08-07 stsp case 'p':
7308 dc424a06 2019-08-07 stsp pflag = 1;
7309 dc424a06 2019-08-07 stsp break;
7310 dc424a06 2019-08-07 stsp case 'F':
7311 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7312 dc424a06 2019-08-07 stsp break;
7313 715dc77e 2019-08-03 stsp default:
7314 715dc77e 2019-08-03 stsp usage_stage();
7315 715dc77e 2019-08-03 stsp /* NOTREACHED */
7316 715dc77e 2019-08-03 stsp }
7317 715dc77e 2019-08-03 stsp }
7318 715dc77e 2019-08-03 stsp
7319 715dc77e 2019-08-03 stsp argc -= optind;
7320 715dc77e 2019-08-03 stsp argv += optind;
7321 715dc77e 2019-08-03 stsp
7322 715dc77e 2019-08-03 stsp #ifndef PROFILE
7323 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7324 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7325 715dc77e 2019-08-03 stsp err(1, "pledge");
7326 715dc77e 2019-08-03 stsp #endif
7327 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7328 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7329 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7330 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7331 715dc77e 2019-08-03 stsp
7332 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7333 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7334 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7335 715dc77e 2019-08-03 stsp goto done;
7336 715dc77e 2019-08-03 stsp }
7337 715dc77e 2019-08-03 stsp
7338 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7339 715dc77e 2019-08-03 stsp if (error)
7340 715dc77e 2019-08-03 stsp goto done;
7341 715dc77e 2019-08-03 stsp
7342 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7343 c9956ddf 2019-09-08 stsp NULL);
7344 715dc77e 2019-08-03 stsp if (error != NULL)
7345 715dc77e 2019-08-03 stsp goto done;
7346 715dc77e 2019-08-03 stsp
7347 dc424a06 2019-08-07 stsp if (patch_script_path) {
7348 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7349 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7350 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7351 dc424a06 2019-08-07 stsp patch_script_path);
7352 dc424a06 2019-08-07 stsp goto done;
7353 dc424a06 2019-08-07 stsp }
7354 dc424a06 2019-08-07 stsp }
7355 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7356 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7357 715dc77e 2019-08-03 stsp if (error)
7358 715dc77e 2019-08-03 stsp goto done;
7359 715dc77e 2019-08-03 stsp
7360 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7361 6d022e97 2019-08-04 stsp if (error)
7362 6d022e97 2019-08-04 stsp goto done;
7363 715dc77e 2019-08-03 stsp
7364 6d022e97 2019-08-04 stsp if (list_stage)
7365 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7366 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7367 2e1f37b0 2019-08-08 stsp else {
7368 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7369 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7370 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7371 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7372 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7373 2e1f37b0 2019-08-08 stsp }
7374 ad493afc 2019-08-03 stsp done:
7375 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7376 2e1f37b0 2019-08-08 stsp error == NULL)
7377 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7378 ad493afc 2019-08-03 stsp if (repo)
7379 ad493afc 2019-08-03 stsp got_repo_close(repo);
7380 ad493afc 2019-08-03 stsp if (worktree)
7381 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7382 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7383 ad493afc 2019-08-03 stsp free((char *)pe->path);
7384 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7385 ad493afc 2019-08-03 stsp free(cwd);
7386 ad493afc 2019-08-03 stsp return error;
7387 ad493afc 2019-08-03 stsp }
7388 ad493afc 2019-08-03 stsp
7389 ad493afc 2019-08-03 stsp __dead static void
7390 ad493afc 2019-08-03 stsp usage_unstage(void)
7391 ad493afc 2019-08-03 stsp {
7392 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7393 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7394 ad493afc 2019-08-03 stsp getprogname());
7395 ad493afc 2019-08-03 stsp exit(1);
7396 ad493afc 2019-08-03 stsp }
7397 ad493afc 2019-08-03 stsp
7398 ad493afc 2019-08-03 stsp
7399 ad493afc 2019-08-03 stsp static const struct got_error *
7400 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7401 ad493afc 2019-08-03 stsp {
7402 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7403 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7404 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7405 ad493afc 2019-08-03 stsp char *cwd = NULL;
7406 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7407 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7408 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7409 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7410 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7411 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7412 ad493afc 2019-08-03 stsp
7413 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7414 ad493afc 2019-08-03 stsp
7415 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7416 ad493afc 2019-08-03 stsp switch (ch) {
7417 2e1f37b0 2019-08-08 stsp case 'p':
7418 2e1f37b0 2019-08-08 stsp pflag = 1;
7419 2e1f37b0 2019-08-08 stsp break;
7420 2e1f37b0 2019-08-08 stsp case 'F':
7421 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7422 2e1f37b0 2019-08-08 stsp break;
7423 ad493afc 2019-08-03 stsp default:
7424 ad493afc 2019-08-03 stsp usage_unstage();
7425 ad493afc 2019-08-03 stsp /* NOTREACHED */
7426 ad493afc 2019-08-03 stsp }
7427 ad493afc 2019-08-03 stsp }
7428 ad493afc 2019-08-03 stsp
7429 ad493afc 2019-08-03 stsp argc -= optind;
7430 ad493afc 2019-08-03 stsp argv += optind;
7431 ad493afc 2019-08-03 stsp
7432 ad493afc 2019-08-03 stsp #ifndef PROFILE
7433 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7434 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7435 ad493afc 2019-08-03 stsp err(1, "pledge");
7436 ad493afc 2019-08-03 stsp #endif
7437 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7438 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7439 2e1f37b0 2019-08-08 stsp
7440 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7441 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7442 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7443 ad493afc 2019-08-03 stsp goto done;
7444 ad493afc 2019-08-03 stsp }
7445 ad493afc 2019-08-03 stsp
7446 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7447 ad493afc 2019-08-03 stsp if (error)
7448 ad493afc 2019-08-03 stsp goto done;
7449 ad493afc 2019-08-03 stsp
7450 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7451 c9956ddf 2019-09-08 stsp NULL);
7452 ad493afc 2019-08-03 stsp if (error != NULL)
7453 ad493afc 2019-08-03 stsp goto done;
7454 ad493afc 2019-08-03 stsp
7455 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7456 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7457 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7458 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7459 2e1f37b0 2019-08-08 stsp patch_script_path);
7460 2e1f37b0 2019-08-08 stsp goto done;
7461 2e1f37b0 2019-08-08 stsp }
7462 2e1f37b0 2019-08-08 stsp }
7463 2e1f37b0 2019-08-08 stsp
7464 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7465 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7466 ad493afc 2019-08-03 stsp if (error)
7467 ad493afc 2019-08-03 stsp goto done;
7468 ad493afc 2019-08-03 stsp
7469 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7470 ad493afc 2019-08-03 stsp if (error)
7471 ad493afc 2019-08-03 stsp goto done;
7472 ad493afc 2019-08-03 stsp
7473 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7474 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7475 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7476 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7477 715dc77e 2019-08-03 stsp done:
7478 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7479 2e1f37b0 2019-08-08 stsp error == NULL)
7480 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7481 0ebf8283 2019-07-24 stsp if (repo)
7482 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7483 715dc77e 2019-08-03 stsp if (worktree)
7484 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7485 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7486 715dc77e 2019-08-03 stsp free((char *)pe->path);
7487 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7488 715dc77e 2019-08-03 stsp free(cwd);
7489 01073a5d 2019-08-22 stsp return error;
7490 01073a5d 2019-08-22 stsp }
7491 01073a5d 2019-08-22 stsp
7492 01073a5d 2019-08-22 stsp __dead static void
7493 01073a5d 2019-08-22 stsp usage_cat(void)
7494 01073a5d 2019-08-22 stsp {
7495 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7496 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7497 01073a5d 2019-08-22 stsp exit(1);
7498 01073a5d 2019-08-22 stsp }
7499 01073a5d 2019-08-22 stsp
7500 01073a5d 2019-08-22 stsp static const struct got_error *
7501 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7502 01073a5d 2019-08-22 stsp {
7503 01073a5d 2019-08-22 stsp const struct got_error *err;
7504 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7505 01073a5d 2019-08-22 stsp
7506 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7507 01073a5d 2019-08-22 stsp if (err)
7508 01073a5d 2019-08-22 stsp return err;
7509 01073a5d 2019-08-22 stsp
7510 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7511 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7512 01073a5d 2019-08-22 stsp return err;
7513 01073a5d 2019-08-22 stsp }
7514 01073a5d 2019-08-22 stsp
7515 01073a5d 2019-08-22 stsp static const struct got_error *
7516 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7517 01073a5d 2019-08-22 stsp {
7518 01073a5d 2019-08-22 stsp const struct got_error *err;
7519 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7520 56e0773d 2019-11-28 stsp int nentries, i;
7521 01073a5d 2019-08-22 stsp
7522 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7523 01073a5d 2019-08-22 stsp if (err)
7524 01073a5d 2019-08-22 stsp return err;
7525 01073a5d 2019-08-22 stsp
7526 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7527 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7528 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7529 01073a5d 2019-08-22 stsp char *id_str;
7530 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7531 01073a5d 2019-08-22 stsp break;
7532 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7533 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7534 01073a5d 2019-08-22 stsp if (err)
7535 01073a5d 2019-08-22 stsp break;
7536 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7537 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7538 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7539 01073a5d 2019-08-22 stsp free(id_str);
7540 01073a5d 2019-08-22 stsp }
7541 01073a5d 2019-08-22 stsp
7542 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7543 01073a5d 2019-08-22 stsp return err;
7544 01073a5d 2019-08-22 stsp }
7545 01073a5d 2019-08-22 stsp
7546 01073a5d 2019-08-22 stsp static const struct got_error *
7547 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7548 01073a5d 2019-08-22 stsp {
7549 01073a5d 2019-08-22 stsp const struct got_error *err;
7550 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7551 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7552 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7553 01073a5d 2019-08-22 stsp char *id_str = NULL;
7554 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7555 01073a5d 2019-08-22 stsp
7556 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7557 01073a5d 2019-08-22 stsp if (err)
7558 01073a5d 2019-08-22 stsp return err;
7559 01073a5d 2019-08-22 stsp
7560 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7561 01073a5d 2019-08-22 stsp if (err)
7562 01073a5d 2019-08-22 stsp goto done;
7563 01073a5d 2019-08-22 stsp
7564 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7565 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7566 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7567 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7568 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7569 01073a5d 2019-08-22 stsp char *pid_str;
7570 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7571 01073a5d 2019-08-22 stsp if (err)
7572 01073a5d 2019-08-22 stsp goto done;
7573 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7574 01073a5d 2019-08-22 stsp free(pid_str);
7575 01073a5d 2019-08-22 stsp }
7576 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7577 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7578 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7579 01073a5d 2019-08-22 stsp
7580 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7581 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7582 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7583 01073a5d 2019-08-22 stsp
7584 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7585 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7586 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7587 01073a5d 2019-08-22 stsp done:
7588 01073a5d 2019-08-22 stsp free(id_str);
7589 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7590 01073a5d 2019-08-22 stsp return err;
7591 01073a5d 2019-08-22 stsp }
7592 01073a5d 2019-08-22 stsp
7593 01073a5d 2019-08-22 stsp static const struct got_error *
7594 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7595 01073a5d 2019-08-22 stsp {
7596 01073a5d 2019-08-22 stsp const struct got_error *err;
7597 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7598 01073a5d 2019-08-22 stsp char *id_str = NULL;
7599 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7600 01073a5d 2019-08-22 stsp
7601 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7602 01073a5d 2019-08-22 stsp if (err)
7603 01073a5d 2019-08-22 stsp return err;
7604 01073a5d 2019-08-22 stsp
7605 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7606 01073a5d 2019-08-22 stsp if (err)
7607 01073a5d 2019-08-22 stsp goto done;
7608 01073a5d 2019-08-22 stsp
7609 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7610 e15d5241 2019-08-22 stsp
7611 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7612 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7613 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7614 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7615 01073a5d 2019-08-22 stsp break;
7616 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7617 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7618 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7619 01073a5d 2019-08-22 stsp break;
7620 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7621 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7622 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7623 01073a5d 2019-08-22 stsp break;
7624 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7625 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7626 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7627 01073a5d 2019-08-22 stsp break;
7628 01073a5d 2019-08-22 stsp default:
7629 01073a5d 2019-08-22 stsp break;
7630 01073a5d 2019-08-22 stsp }
7631 01073a5d 2019-08-22 stsp
7632 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7633 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7634 e15d5241 2019-08-22 stsp
7635 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7636 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7637 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7638 01073a5d 2019-08-22 stsp
7639 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7640 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7641 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7642 01073a5d 2019-08-22 stsp done:
7643 01073a5d 2019-08-22 stsp free(id_str);
7644 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7645 01073a5d 2019-08-22 stsp return err;
7646 01073a5d 2019-08-22 stsp }
7647 01073a5d 2019-08-22 stsp
7648 01073a5d 2019-08-22 stsp static const struct got_error *
7649 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7650 01073a5d 2019-08-22 stsp {
7651 01073a5d 2019-08-22 stsp const struct got_error *error;
7652 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7653 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7654 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7655 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7656 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7657 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7658 01073a5d 2019-08-22 stsp
7659 01073a5d 2019-08-22 stsp #ifndef PROFILE
7660 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7661 01073a5d 2019-08-22 stsp NULL) == -1)
7662 01073a5d 2019-08-22 stsp err(1, "pledge");
7663 01073a5d 2019-08-22 stsp #endif
7664 01073a5d 2019-08-22 stsp
7665 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7666 01073a5d 2019-08-22 stsp switch (ch) {
7667 896e9b6f 2019-08-26 stsp case 'c':
7668 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7669 896e9b6f 2019-08-26 stsp break;
7670 01073a5d 2019-08-22 stsp case 'r':
7671 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7672 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7673 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7674 9ba1d308 2019-10-21 stsp optarg);
7675 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7676 01073a5d 2019-08-22 stsp break;
7677 896e9b6f 2019-08-26 stsp case 'P':
7678 896e9b6f 2019-08-26 stsp force_path = 1;
7679 896e9b6f 2019-08-26 stsp break;
7680 01073a5d 2019-08-22 stsp default:
7681 01073a5d 2019-08-22 stsp usage_cat();
7682 01073a5d 2019-08-22 stsp /* NOTREACHED */
7683 01073a5d 2019-08-22 stsp }
7684 01073a5d 2019-08-22 stsp }
7685 01073a5d 2019-08-22 stsp
7686 01073a5d 2019-08-22 stsp argc -= optind;
7687 01073a5d 2019-08-22 stsp argv += optind;
7688 01073a5d 2019-08-22 stsp
7689 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7690 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7691 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7692 01073a5d 2019-08-22 stsp goto done;
7693 01073a5d 2019-08-22 stsp }
7694 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7695 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7696 01073a5d 2019-08-22 stsp goto done;
7697 01073a5d 2019-08-22 stsp if (worktree) {
7698 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7699 01073a5d 2019-08-22 stsp repo_path = strdup(
7700 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7701 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7702 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7703 01073a5d 2019-08-22 stsp goto done;
7704 01073a5d 2019-08-22 stsp }
7705 01073a5d 2019-08-22 stsp }
7706 01073a5d 2019-08-22 stsp }
7707 01073a5d 2019-08-22 stsp
7708 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7709 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7710 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7711 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7712 01073a5d 2019-08-22 stsp }
7713 01073a5d 2019-08-22 stsp
7714 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7715 01073a5d 2019-08-22 stsp free(repo_path);
7716 01073a5d 2019-08-22 stsp if (error != NULL)
7717 01073a5d 2019-08-22 stsp goto done;
7718 01073a5d 2019-08-22 stsp
7719 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7720 896e9b6f 2019-08-26 stsp if (error)
7721 896e9b6f 2019-08-26 stsp goto done;
7722 896e9b6f 2019-08-26 stsp
7723 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7724 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7725 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7726 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7727 01073a5d 2019-08-22 stsp if (error)
7728 01073a5d 2019-08-22 stsp goto done;
7729 01073a5d 2019-08-22 stsp
7730 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7731 896e9b6f 2019-08-26 stsp if (force_path) {
7732 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7733 896e9b6f 2019-08-26 stsp argv[i]);
7734 896e9b6f 2019-08-26 stsp if (error)
7735 896e9b6f 2019-08-26 stsp break;
7736 896e9b6f 2019-08-26 stsp } else {
7737 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7738 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7739 896e9b6f 2019-08-26 stsp if (error) {
7740 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7741 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7742 896e9b6f 2019-08-26 stsp break;
7743 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7744 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7745 896e9b6f 2019-08-26 stsp if (error)
7746 896e9b6f 2019-08-26 stsp break;
7747 896e9b6f 2019-08-26 stsp }
7748 896e9b6f 2019-08-26 stsp }
7749 01073a5d 2019-08-22 stsp
7750 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7751 01073a5d 2019-08-22 stsp if (error)
7752 01073a5d 2019-08-22 stsp break;
7753 01073a5d 2019-08-22 stsp
7754 01073a5d 2019-08-22 stsp switch (obj_type) {
7755 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7756 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7757 01073a5d 2019-08-22 stsp break;
7758 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7759 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7760 01073a5d 2019-08-22 stsp break;
7761 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7762 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7763 01073a5d 2019-08-22 stsp break;
7764 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7765 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7766 01073a5d 2019-08-22 stsp break;
7767 01073a5d 2019-08-22 stsp default:
7768 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7769 01073a5d 2019-08-22 stsp break;
7770 01073a5d 2019-08-22 stsp }
7771 01073a5d 2019-08-22 stsp if (error)
7772 01073a5d 2019-08-22 stsp break;
7773 01073a5d 2019-08-22 stsp free(label);
7774 01073a5d 2019-08-22 stsp label = NULL;
7775 01073a5d 2019-08-22 stsp free(id);
7776 01073a5d 2019-08-22 stsp id = NULL;
7777 01073a5d 2019-08-22 stsp }
7778 01073a5d 2019-08-22 stsp done:
7779 01073a5d 2019-08-22 stsp free(label);
7780 01073a5d 2019-08-22 stsp free(id);
7781 896e9b6f 2019-08-26 stsp free(commit_id);
7782 01073a5d 2019-08-22 stsp if (worktree)
7783 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7784 01073a5d 2019-08-22 stsp if (repo) {
7785 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7786 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7787 01073a5d 2019-08-22 stsp if (error == NULL)
7788 01073a5d 2019-08-22 stsp error = repo_error;
7789 01073a5d 2019-08-22 stsp }
7790 0ebf8283 2019-07-24 stsp return error;
7791 0ebf8283 2019-07-24 stsp }