Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 f42b1b34 2018-03-12 stsp
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 5c860e29 2018-03-12 stsp #include <stdio.h>
27 5c860e29 2018-03-12 stsp #include <stdlib.h>
28 5c860e29 2018-03-12 stsp #include <string.h>
29 5c860e29 2018-03-12 stsp #include <unistd.h>
30 c09a553d 2018-03-12 stsp #include <libgen.h>
31 c0768b0f 2018-06-10 stsp #include <time.h>
32 5c860e29 2018-03-12 stsp
33 f42b1b34 2018-03-12 stsp #include "got_error.h"
34 f42b1b34 2018-03-12 stsp #include "got_object.h"
35 5261c201 2018-04-01 stsp #include "got_reference.h"
36 f42b1b34 2018-03-12 stsp #include "got_repository.h"
37 c09a553d 2018-03-12 stsp #include "got_worktree.h"
38 79109fed 2018-03-27 stsp #include "got_diff.h"
39 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
40 404c43c4 2018-06-21 stsp #include "got_blame.h"
41 5c860e29 2018-03-12 stsp
42 5c860e29 2018-03-12 stsp #ifndef nitems
43 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 5c860e29 2018-03-12 stsp #endif
45 5c860e29 2018-03-12 stsp
46 5c860e29 2018-03-12 stsp struct cmd {
47 5c860e29 2018-03-12 stsp const char *cmd_name;
48 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
49 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
50 46a0db7d 2018-03-12 stsp const char *cmd_descr;
51 5c860e29 2018-03-12 stsp };
52 5c860e29 2018-03-12 stsp
53 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
54 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
55 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
56 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
57 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
58 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
59 5c860e29 2018-03-12 stsp
60 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
61 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
62 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
63 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
64 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
65 4ed7e80c 2018-05-20 stsp #ifdef notyet
66 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
67 4ed7e80c 2018-05-20 stsp #endif
68 5c860e29 2018-03-12 stsp
69 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
70 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
71 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
72 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
73 1b6b95a8 2018-03-12 stsp "show repository history" },
74 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
75 b00d56cd 2018-04-01 stsp "compare files and directories" },
76 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
77 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
78 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
79 5de5890b 2018-10-18 stsp " list files and directories in repository" },
80 f42b1b34 2018-03-12 stsp #ifdef notyet
81 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
82 1b6b95a8 2018-03-12 stsp "show modification status of files" },
83 f42b1b34 2018-03-12 stsp #endif
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 5c860e29 2018-03-12 stsp int
87 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
88 5c860e29 2018-03-12 stsp {
89 5c860e29 2018-03-12 stsp struct cmd *cmd;
90 5c860e29 2018-03-12 stsp unsigned int i;
91 5c860e29 2018-03-12 stsp int ch;
92 1b6b95a8 2018-03-12 stsp int hflag = 0;
93 5c860e29 2018-03-12 stsp
94 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
95 5c860e29 2018-03-12 stsp
96 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
97 5c860e29 2018-03-12 stsp switch (ch) {
98 1b6b95a8 2018-03-12 stsp case 'h':
99 1b6b95a8 2018-03-12 stsp hflag = 1;
100 1b6b95a8 2018-03-12 stsp break;
101 5c860e29 2018-03-12 stsp default:
102 5c860e29 2018-03-12 stsp usage();
103 5c860e29 2018-03-12 stsp /* NOTREACHED */
104 5c860e29 2018-03-12 stsp }
105 5c860e29 2018-03-12 stsp }
106 5c860e29 2018-03-12 stsp
107 5c860e29 2018-03-12 stsp argc -= optind;
108 5c860e29 2018-03-12 stsp argv += optind;
109 1e70621d 2018-03-27 stsp optind = 0;
110 5c860e29 2018-03-12 stsp
111 5c860e29 2018-03-12 stsp if (argc <= 0)
112 5c860e29 2018-03-12 stsp usage();
113 5c860e29 2018-03-12 stsp
114 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
115 d7d4f210 2018-03-12 stsp const struct got_error *error;
116 d7d4f210 2018-03-12 stsp
117 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
118 5c860e29 2018-03-12 stsp
119 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
120 5c860e29 2018-03-12 stsp continue;
121 5c860e29 2018-03-12 stsp
122 1b6b95a8 2018-03-12 stsp if (hflag)
123 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
124 1b6b95a8 2018-03-12 stsp
125 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
126 d7d4f210 2018-03-12 stsp if (error) {
127 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
128 d7d4f210 2018-03-12 stsp return 1;
129 d7d4f210 2018-03-12 stsp }
130 d7d4f210 2018-03-12 stsp
131 d7d4f210 2018-03-12 stsp return 0;
132 5c860e29 2018-03-12 stsp }
133 5c860e29 2018-03-12 stsp
134 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
135 5c860e29 2018-03-12 stsp return 1;
136 5c860e29 2018-03-12 stsp }
137 5c860e29 2018-03-12 stsp
138 4ed7e80c 2018-05-20 stsp __dead static void
139 5c860e29 2018-03-12 stsp usage(void)
140 5c860e29 2018-03-12 stsp {
141 46a0db7d 2018-03-12 stsp int i;
142 46a0db7d 2018-03-12 stsp
143 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
144 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
145 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
146 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
147 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
148 46a0db7d 2018-03-12 stsp }
149 5c860e29 2018-03-12 stsp exit(1);
150 5c860e29 2018-03-12 stsp }
151 5c860e29 2018-03-12 stsp
152 4ed7e80c 2018-05-20 stsp __dead static void
153 c09a553d 2018-03-12 stsp usage_checkout(void)
154 c09a553d 2018-03-12 stsp {
155 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
156 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
157 c09a553d 2018-03-12 stsp exit(1);
158 92a684f4 2018-03-12 stsp }
159 92a684f4 2018-03-12 stsp
160 92a684f4 2018-03-12 stsp static void
161 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
162 92a684f4 2018-03-12 stsp {
163 92a684f4 2018-03-12 stsp char *worktree_path = arg;
164 92a684f4 2018-03-12 stsp
165 92a684f4 2018-03-12 stsp while (path[0] == '/')
166 92a684f4 2018-03-12 stsp path++;
167 92a684f4 2018-03-12 stsp
168 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
169 c09a553d 2018-03-12 stsp }
170 c09a553d 2018-03-12 stsp
171 4ed7e80c 2018-05-20 stsp static const struct got_error *
172 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
173 c09a553d 2018-03-12 stsp {
174 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
175 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
176 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
177 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
178 c09a553d 2018-03-12 stsp char *repo_path = NULL;
179 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
180 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
181 0bb8a95e 2018-03-12 stsp int ch;
182 c09a553d 2018-03-12 stsp
183 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
184 0bb8a95e 2018-03-12 stsp switch (ch) {
185 0bb8a95e 2018-03-12 stsp case 'p':
186 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
187 0bb8a95e 2018-03-12 stsp break;
188 0bb8a95e 2018-03-12 stsp default:
189 0bb8a95e 2018-03-12 stsp usage();
190 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
191 0bb8a95e 2018-03-12 stsp }
192 0bb8a95e 2018-03-12 stsp }
193 0bb8a95e 2018-03-12 stsp
194 0bb8a95e 2018-03-12 stsp argc -= optind;
195 0bb8a95e 2018-03-12 stsp argv += optind;
196 0bb8a95e 2018-03-12 stsp
197 6715a751 2018-03-16 stsp #ifndef PROFILE
198 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
199 ad242220 2018-09-08 stsp == -1)
200 c09a553d 2018-03-12 stsp err(1, "pledge");
201 6715a751 2018-03-16 stsp #endif
202 0bb8a95e 2018-03-12 stsp if (argc == 1) {
203 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
204 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
205 76089277 2018-04-01 stsp if (repo_path == NULL)
206 76089277 2018-04-01 stsp return got_error_from_errno();
207 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
208 76089277 2018-04-01 stsp if (cwd == NULL) {
209 76089277 2018-04-01 stsp error = got_error_from_errno();
210 76089277 2018-04-01 stsp goto done;
211 76089277 2018-04-01 stsp }
212 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
213 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
214 5d7c1dab 2018-04-01 stsp else
215 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
216 76089277 2018-04-01 stsp if (base == NULL) {
217 76089277 2018-04-01 stsp error = got_error_from_errno();
218 76089277 2018-04-01 stsp goto done;
219 76089277 2018-04-01 stsp }
220 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
221 c09a553d 2018-03-12 stsp if (dotgit)
222 c09a553d 2018-03-12 stsp *dotgit = '\0';
223 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
224 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
225 c09a553d 2018-03-12 stsp free(cwd);
226 76089277 2018-04-01 stsp goto done;
227 c09a553d 2018-03-12 stsp }
228 c09a553d 2018-03-12 stsp free(cwd);
229 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
230 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
231 76089277 2018-04-01 stsp if (repo_path == NULL) {
232 76089277 2018-04-01 stsp error = got_error_from_errno();
233 76089277 2018-04-01 stsp goto done;
234 76089277 2018-04-01 stsp }
235 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
236 76089277 2018-04-01 stsp if (worktree_path == NULL) {
237 76089277 2018-04-01 stsp error = got_error_from_errno();
238 76089277 2018-04-01 stsp goto done;
239 76089277 2018-04-01 stsp }
240 c09a553d 2018-03-12 stsp } else
241 c09a553d 2018-03-12 stsp usage_checkout();
242 c09a553d 2018-03-12 stsp
243 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
244 c09a553d 2018-03-12 stsp if (error != NULL)
245 c09a553d 2018-03-12 stsp goto done;
246 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
247 c09a553d 2018-03-12 stsp if (error != NULL)
248 c09a553d 2018-03-12 stsp goto done;
249 c09a553d 2018-03-12 stsp
250 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
251 c09a553d 2018-03-12 stsp if (error != NULL)
252 c09a553d 2018-03-12 stsp goto done;
253 c09a553d 2018-03-12 stsp
254 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
255 c09a553d 2018-03-12 stsp if (error != NULL)
256 c09a553d 2018-03-12 stsp goto done;
257 c09a553d 2018-03-12 stsp
258 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
259 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
260 c09a553d 2018-03-12 stsp if (error != NULL)
261 c09a553d 2018-03-12 stsp goto done;
262 c09a553d 2018-03-12 stsp
263 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
264 c09a553d 2018-03-12 stsp
265 c09a553d 2018-03-12 stsp done:
266 76089277 2018-04-01 stsp free(repo_path);
267 c09a553d 2018-03-12 stsp free(worktree_path);
268 c09a553d 2018-03-12 stsp return error;
269 c09a553d 2018-03-12 stsp }
270 c09a553d 2018-03-12 stsp
271 f42b1b34 2018-03-12 stsp static const struct got_error *
272 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
273 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
274 5c860e29 2018-03-12 stsp {
275 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
276 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
277 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
278 79109fed 2018-03-27 stsp
279 117e771c 2018-07-23 stsp err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
280 79109fed 2018-03-27 stsp if (err)
281 79109fed 2018-03-27 stsp return err;
282 79109fed 2018-03-27 stsp
283 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
284 79f35eb3 2018-06-11 stsp if (qid != NULL) {
285 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
286 79109fed 2018-03-27 stsp
287 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
288 79109fed 2018-03-27 stsp if (err)
289 79109fed 2018-03-27 stsp return err;
290 79109fed 2018-03-27 stsp
291 117e771c 2018-07-23 stsp err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
292 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
293 79109fed 2018-03-27 stsp if (err)
294 79109fed 2018-03-27 stsp return err;
295 79109fed 2018-03-27 stsp }
296 79109fed 2018-03-27 stsp
297 c0cc5c62 2018-10-18 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
298 79109fed 2018-03-27 stsp if (tree1)
299 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
300 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
301 79109fed 2018-03-27 stsp return err;
302 79109fed 2018-03-27 stsp }
303 79109fed 2018-03-27 stsp
304 4bb494d5 2018-06-16 stsp static char *
305 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
306 6c281f94 2018-06-11 stsp {
307 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
308 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
309 6c281f94 2018-06-11 stsp if (p)
310 6c281f94 2018-06-11 stsp *p = '\0';
311 6c281f94 2018-06-11 stsp return s;
312 6c281f94 2018-06-11 stsp }
313 6c281f94 2018-06-11 stsp
314 79109fed 2018-03-27 stsp static const struct got_error *
315 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
316 c0cc5c62 2018-10-18 stsp struct got_repository *repo, int show_patch, int diff_context)
317 79109fed 2018-03-27 stsp {
318 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
319 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
320 6c281f94 2018-06-11 stsp char datebuf[26];
321 5c860e29 2018-03-12 stsp
322 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
323 8bf5b3c9 2018-03-17 stsp if (err)
324 8bf5b3c9 2018-03-17 stsp return err;
325 788c352e 2018-06-16 stsp
326 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
327 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
328 832c249c 2018-06-10 stsp free(id_str);
329 0dcf3e9c 2018-09-15 stsp printf("from: %s\n", commit->author);
330 ccb26ccd 2018-11-05 stsp datestr = get_datestr(&commit->committer_time, datebuf);
331 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
332 dab5fe87 2018-09-14 stsp if (strcmp(commit->author, commit->committer) != 0)
333 0dcf3e9c 2018-09-15 stsp printf("via: %s\n", commit->committer);
334 3fe1abad 2018-06-10 stsp if (commit->nparents > 1) {
335 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
336 3fe1abad 2018-06-10 stsp int n = 1;
337 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
338 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
339 a0603db2 2018-06-10 stsp if (err)
340 a0603db2 2018-06-10 stsp return err;
341 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
342 a0603db2 2018-06-10 stsp free(id_str);
343 a0603db2 2018-06-10 stsp }
344 a0603db2 2018-06-10 stsp }
345 832c249c 2018-06-10 stsp
346 621015ac 2018-07-23 stsp logmsg0 = strdup(commit->logmsg);
347 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
348 832c249c 2018-06-10 stsp return got_error_from_errno();
349 8bf5b3c9 2018-03-17 stsp
350 621015ac 2018-07-23 stsp logmsg = logmsg0;
351 832c249c 2018-06-10 stsp do {
352 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
353 832c249c 2018-06-10 stsp if (line)
354 832c249c 2018-06-10 stsp printf(" %s\n", line);
355 832c249c 2018-06-10 stsp } while (line);
356 621015ac 2018-07-23 stsp free(logmsg0);
357 832c249c 2018-06-10 stsp
358 971751ac 2018-03-27 stsp if (show_patch) {
359 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
360 971751ac 2018-03-27 stsp if (err == 0)
361 971751ac 2018-03-27 stsp printf("\n");
362 971751ac 2018-03-27 stsp }
363 07862c20 2018-09-15 stsp
364 d82de447 2018-09-15 stsp fflush(stdout);
365 07862c20 2018-09-15 stsp return err;
366 f42b1b34 2018-03-12 stsp }
367 5c860e29 2018-03-12 stsp
368 f42b1b34 2018-03-12 stsp static const struct got_error *
369 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
370 c0cc5c62 2018-10-18 stsp struct got_repository *repo, char *path, int show_patch, int diff_context,
371 c0cc5c62 2018-10-18 stsp int limit, int first_parent_traversal)
372 f42b1b34 2018-03-12 stsp {
373 f42b1b34 2018-03-12 stsp const struct got_error *err;
374 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
375 372ccdbb 2018-06-10 stsp
376 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
377 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
378 f42b1b34 2018-03-12 stsp if (err)
379 f42b1b34 2018-03-12 stsp return err;
380 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
381 372ccdbb 2018-06-10 stsp if (err)
382 fcc85cad 2018-07-22 stsp goto done;
383 31cedeaf 2018-09-15 stsp while (1) {
384 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
385 372ccdbb 2018-06-10 stsp struct got_object_id *id;
386 8bf5b3c9 2018-03-17 stsp
387 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
388 372ccdbb 2018-06-10 stsp if (err) {
389 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
390 9ba79e04 2018-06-11 stsp err = NULL;
391 9ba79e04 2018-06-11 stsp break;
392 9ba79e04 2018-06-11 stsp }
393 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
394 372ccdbb 2018-06-10 stsp break;
395 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
396 372ccdbb 2018-06-10 stsp if (err)
397 372ccdbb 2018-06-10 stsp break;
398 372ccdbb 2018-06-10 stsp else
399 372ccdbb 2018-06-10 stsp continue;
400 7e665116 2018-04-02 stsp }
401 b43fbaa0 2018-06-11 stsp if (id == NULL)
402 7e665116 2018-04-02 stsp break;
403 b43fbaa0 2018-06-11 stsp
404 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
405 b43fbaa0 2018-06-11 stsp if (err)
406 fcc85cad 2018-07-22 stsp break;
407 c0cc5c62 2018-10-18 stsp err = print_commit(commit, id, repo, show_patch, diff_context);
408 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
409 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
410 7e665116 2018-04-02 stsp break;
411 31cedeaf 2018-09-15 stsp }
412 fcc85cad 2018-07-22 stsp done:
413 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
414 f42b1b34 2018-03-12 stsp return err;
415 f42b1b34 2018-03-12 stsp }
416 5c860e29 2018-03-12 stsp
417 4ed7e80c 2018-05-20 stsp __dead static void
418 6f3d1eb0 2018-03-12 stsp usage_log(void)
419 6f3d1eb0 2018-03-12 stsp {
420 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
421 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
422 6f3d1eb0 2018-03-12 stsp exit(1);
423 6f3d1eb0 2018-03-12 stsp }
424 6f3d1eb0 2018-03-12 stsp
425 4ed7e80c 2018-05-20 stsp static const struct got_error *
426 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
427 f42b1b34 2018-03-12 stsp {
428 f42b1b34 2018-03-12 stsp const struct got_error *error;
429 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
430 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
431 d1f2edc9 2018-06-13 stsp struct got_object *obj = NULL;
432 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
433 d142fc45 2018-04-01 stsp char *start_commit = NULL;
434 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
435 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
436 64a96a6d 2018-04-01 stsp const char *errstr;
437 5c860e29 2018-03-12 stsp
438 6715a751 2018-03-16 stsp #ifndef PROFILE
439 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
440 ad242220 2018-09-08 stsp == -1)
441 f42b1b34 2018-03-12 stsp err(1, "pledge");
442 6715a751 2018-03-16 stsp #endif
443 79109fed 2018-03-27 stsp
444 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
445 79109fed 2018-03-27 stsp switch (ch) {
446 79109fed 2018-03-27 stsp case 'p':
447 79109fed 2018-03-27 stsp show_patch = 1;
448 d142fc45 2018-04-01 stsp break;
449 d142fc45 2018-04-01 stsp case 'c':
450 d142fc45 2018-04-01 stsp start_commit = optarg;
451 64a96a6d 2018-04-01 stsp break;
452 c0cc5c62 2018-10-18 stsp case 'C':
453 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
454 4a8520aa 2018-10-18 stsp &errstr);
455 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
456 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
457 c0cc5c62 2018-10-18 stsp break;
458 64a96a6d 2018-04-01 stsp case 'l':
459 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
460 64a96a6d 2018-04-01 stsp if (errstr != NULL)
461 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
462 79109fed 2018-03-27 stsp break;
463 0ed6ed4c 2018-06-13 stsp case 'f':
464 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
465 a0603db2 2018-06-10 stsp break;
466 04ca23f4 2018-07-16 stsp case 'r':
467 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
468 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
469 04ca23f4 2018-07-16 stsp err(1, "-r option");
470 04ca23f4 2018-07-16 stsp break;
471 79109fed 2018-03-27 stsp default:
472 79109fed 2018-03-27 stsp usage();
473 79109fed 2018-03-27 stsp /* NOTREACHED */
474 79109fed 2018-03-27 stsp }
475 79109fed 2018-03-27 stsp }
476 79109fed 2018-03-27 stsp
477 79109fed 2018-03-27 stsp argc -= optind;
478 79109fed 2018-03-27 stsp argv += optind;
479 79109fed 2018-03-27 stsp
480 04ca23f4 2018-07-16 stsp if (argc == 0)
481 04ca23f4 2018-07-16 stsp path = strdup("");
482 04ca23f4 2018-07-16 stsp else if (argc == 1)
483 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
484 04ca23f4 2018-07-16 stsp else
485 6f3d1eb0 2018-03-12 stsp usage_log();
486 04ca23f4 2018-07-16 stsp if (path == NULL)
487 04ca23f4 2018-07-16 stsp return got_error_from_errno();
488 f42b1b34 2018-03-12 stsp
489 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
490 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
491 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
492 04ca23f4 2018-07-16 stsp goto done;
493 04ca23f4 2018-07-16 stsp }
494 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
495 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
496 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
497 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
498 04ca23f4 2018-07-16 stsp goto done;
499 04ca23f4 2018-07-16 stsp }
500 04ca23f4 2018-07-16 stsp }
501 04ca23f4 2018-07-16 stsp
502 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
503 d7d4f210 2018-03-12 stsp if (error != NULL)
504 04ca23f4 2018-07-16 stsp goto done;
505 f42b1b34 2018-03-12 stsp
506 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
507 3235492e 2018-04-01 stsp struct got_reference *head_ref;
508 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
509 3235492e 2018-04-01 stsp if (error != NULL)
510 3235492e 2018-04-01 stsp return error;
511 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
512 3235492e 2018-04-01 stsp got_ref_close(head_ref);
513 3235492e 2018-04-01 stsp if (error != NULL)
514 3235492e 2018-04-01 stsp return error;
515 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
516 3235492e 2018-04-01 stsp } else {
517 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
518 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
519 3235492e 2018-04-01 stsp if (error == NULL) {
520 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
521 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
522 d1f2edc9 2018-06-13 stsp if (error != NULL)
523 d1f2edc9 2018-06-13 stsp return error;
524 d1f2edc9 2018-06-13 stsp error = got_object_open(&obj, repo, id);
525 d1f2edc9 2018-06-13 stsp if (error != NULL)
526 d1f2edc9 2018-06-13 stsp return error;
527 d1f2edc9 2018-06-13 stsp }
528 d1f2edc9 2018-06-13 stsp if (obj == NULL) {
529 d1f2edc9 2018-06-13 stsp error = got_object_open_by_id_str(&obj, repo,
530 d1f2edc9 2018-06-13 stsp start_commit);
531 d1f2edc9 2018-06-13 stsp if (error != NULL)
532 d1f2edc9 2018-06-13 stsp return error;
533 6402fb3c 2018-09-15 stsp id = got_object_id_dup(got_object_get_id(obj));
534 3235492e 2018-04-01 stsp if (id == NULL)
535 3235492e 2018-04-01 stsp error = got_error_from_errno();
536 3235492e 2018-04-01 stsp }
537 3235492e 2018-04-01 stsp }
538 d7d4f210 2018-03-12 stsp if (error != NULL)
539 04ca23f4 2018-07-16 stsp goto done;
540 04ca23f4 2018-07-16 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
541 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
542 04ca23f4 2018-07-16 stsp goto done;
543 04ca23f4 2018-07-16 stsp }
544 04ca23f4 2018-07-16 stsp
545 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
546 04ca23f4 2018-07-16 stsp if (error != NULL)
547 04ca23f4 2018-07-16 stsp goto done;
548 04ca23f4 2018-07-16 stsp if (in_repo_path) {
549 04ca23f4 2018-07-16 stsp free(path);
550 04ca23f4 2018-07-16 stsp path = in_repo_path;
551 04ca23f4 2018-07-16 stsp }
552 04ca23f4 2018-07-16 stsp
553 04ca23f4 2018-07-16 stsp error = print_commits(obj, id, repo, path, show_patch,
554 c0cc5c62 2018-10-18 stsp diff_context, limit, first_parent_traversal);
555 04ca23f4 2018-07-16 stsp done:
556 04ca23f4 2018-07-16 stsp free(path);
557 04ca23f4 2018-07-16 stsp free(repo_path);
558 04ca23f4 2018-07-16 stsp free(cwd);
559 04ca23f4 2018-07-16 stsp if (obj)
560 04ca23f4 2018-07-16 stsp got_object_close(obj);
561 f42b1b34 2018-03-12 stsp free(id);
562 ad242220 2018-09-08 stsp if (repo) {
563 ad242220 2018-09-08 stsp const struct got_error *repo_error;
564 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
565 ad242220 2018-09-08 stsp if (error == NULL)
566 ad242220 2018-09-08 stsp error = repo_error;
567 ad242220 2018-09-08 stsp }
568 8bf5b3c9 2018-03-17 stsp return error;
569 5c860e29 2018-03-12 stsp }
570 5c860e29 2018-03-12 stsp
571 4ed7e80c 2018-05-20 stsp __dead static void
572 3f8b7d6a 2018-04-01 stsp usage_diff(void)
573 3f8b7d6a 2018-04-01 stsp {
574 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
575 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
576 3f8b7d6a 2018-04-01 stsp exit(1);
577 b00d56cd 2018-04-01 stsp }
578 b00d56cd 2018-04-01 stsp
579 4ed7e80c 2018-05-20 stsp static const struct got_error *
580 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
581 b00d56cd 2018-04-01 stsp {
582 b00d56cd 2018-04-01 stsp const struct got_error *error;
583 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
584 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
585 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
586 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
587 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
588 c0cc5c62 2018-10-18 stsp const char *errstr;
589 b00d56cd 2018-04-01 stsp
590 b00d56cd 2018-04-01 stsp #ifndef PROFILE
591 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
592 ad242220 2018-09-08 stsp == -1)
593 b00d56cd 2018-04-01 stsp err(1, "pledge");
594 b00d56cd 2018-04-01 stsp #endif
595 b00d56cd 2018-04-01 stsp
596 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
597 b00d56cd 2018-04-01 stsp switch (ch) {
598 c0cc5c62 2018-10-18 stsp case 'C':
599 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
600 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
601 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
602 c0cc5c62 2018-10-18 stsp break;
603 b00d56cd 2018-04-01 stsp default:
604 b00d56cd 2018-04-01 stsp usage();
605 b00d56cd 2018-04-01 stsp /* NOTREACHED */
606 b00d56cd 2018-04-01 stsp }
607 b00d56cd 2018-04-01 stsp }
608 b00d56cd 2018-04-01 stsp
609 b00d56cd 2018-04-01 stsp argc -= optind;
610 b00d56cd 2018-04-01 stsp argv += optind;
611 b00d56cd 2018-04-01 stsp
612 b00d56cd 2018-04-01 stsp if (argc == 0) {
613 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
614 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
615 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
616 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
617 e1e3f570 2018-04-01 stsp return got_error_from_errno();
618 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
619 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
620 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
621 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
622 76089277 2018-04-01 stsp if (repo_path == NULL)
623 76089277 2018-04-01 stsp return got_error_from_errno();
624 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
625 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
626 b00d56cd 2018-04-01 stsp } else
627 b00d56cd 2018-04-01 stsp usage_diff();
628 b00d56cd 2018-04-01 stsp
629 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
630 76089277 2018-04-01 stsp free(repo_path);
631 b00d56cd 2018-04-01 stsp if (error != NULL)
632 b00d56cd 2018-04-01 stsp goto done;
633 b00d56cd 2018-04-01 stsp
634 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
635 6402fb3c 2018-09-15 stsp if (error)
636 b00d56cd 2018-04-01 stsp goto done;
637 b00d56cd 2018-04-01 stsp
638 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
639 6402fb3c 2018-09-15 stsp if (error)
640 b00d56cd 2018-04-01 stsp goto done;
641 b00d56cd 2018-04-01 stsp
642 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
643 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
644 b00d56cd 2018-04-01 stsp goto done;
645 b00d56cd 2018-04-01 stsp }
646 b00d56cd 2018-04-01 stsp
647 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
648 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
649 c0cc5c62 2018-10-18 stsp error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
650 c0cc5c62 2018-10-18 stsp diff_context, repo, stdout);
651 b00d56cd 2018-04-01 stsp break;
652 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
653 c0cc5c62 2018-10-18 stsp error = got_diff_objects_as_trees(obj1, obj2, "", "",
654 c0cc5c62 2018-10-18 stsp diff_context, repo, stdout);
655 b00d56cd 2018-04-01 stsp break;
656 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
657 c0cc5c62 2018-10-18 stsp error = got_diff_objects_as_commits(obj1, obj2, diff_context,
658 c0cc5c62 2018-10-18 stsp repo, stdout);
659 b00d56cd 2018-04-01 stsp break;
660 b00d56cd 2018-04-01 stsp default:
661 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
662 b00d56cd 2018-04-01 stsp }
663 b00d56cd 2018-04-01 stsp
664 b00d56cd 2018-04-01 stsp done:
665 b00d56cd 2018-04-01 stsp if (obj1)
666 b00d56cd 2018-04-01 stsp got_object_close(obj1);
667 b00d56cd 2018-04-01 stsp if (obj2)
668 b00d56cd 2018-04-01 stsp got_object_close(obj2);
669 ad242220 2018-09-08 stsp if (repo) {
670 ad242220 2018-09-08 stsp const struct got_error *repo_error;
671 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
672 ad242220 2018-09-08 stsp if (error == NULL)
673 ad242220 2018-09-08 stsp error = repo_error;
674 ad242220 2018-09-08 stsp }
675 b00d56cd 2018-04-01 stsp return error;
676 404c43c4 2018-06-21 stsp }
677 404c43c4 2018-06-21 stsp
678 404c43c4 2018-06-21 stsp __dead static void
679 404c43c4 2018-06-21 stsp usage_blame(void)
680 404c43c4 2018-06-21 stsp {
681 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
682 404c43c4 2018-06-21 stsp getprogname());
683 404c43c4 2018-06-21 stsp exit(1);
684 b00d56cd 2018-04-01 stsp }
685 b00d56cd 2018-04-01 stsp
686 404c43c4 2018-06-21 stsp static const struct got_error *
687 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
688 404c43c4 2018-06-21 stsp {
689 404c43c4 2018-06-21 stsp const struct got_error *error;
690 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
691 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
692 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
693 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
694 404c43c4 2018-06-21 stsp int ch;
695 404c43c4 2018-06-21 stsp
696 404c43c4 2018-06-21 stsp #ifndef PROFILE
697 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
698 ad242220 2018-09-08 stsp == -1)
699 404c43c4 2018-06-21 stsp err(1, "pledge");
700 404c43c4 2018-06-21 stsp #endif
701 404c43c4 2018-06-21 stsp
702 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
703 404c43c4 2018-06-21 stsp switch (ch) {
704 404c43c4 2018-06-21 stsp case 'c':
705 404c43c4 2018-06-21 stsp commit_id_str = optarg;
706 404c43c4 2018-06-21 stsp break;
707 66bea077 2018-08-02 stsp case 'r':
708 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
709 66bea077 2018-08-02 stsp if (repo_path == NULL)
710 66bea077 2018-08-02 stsp err(1, "-r option");
711 66bea077 2018-08-02 stsp break;
712 404c43c4 2018-06-21 stsp default:
713 404c43c4 2018-06-21 stsp usage();
714 404c43c4 2018-06-21 stsp /* NOTREACHED */
715 404c43c4 2018-06-21 stsp }
716 404c43c4 2018-06-21 stsp }
717 404c43c4 2018-06-21 stsp
718 404c43c4 2018-06-21 stsp argc -= optind;
719 404c43c4 2018-06-21 stsp argv += optind;
720 404c43c4 2018-06-21 stsp
721 a39318fd 2018-08-02 stsp if (argc == 1)
722 404c43c4 2018-06-21 stsp path = argv[0];
723 a39318fd 2018-08-02 stsp else
724 404c43c4 2018-06-21 stsp usage_blame();
725 404c43c4 2018-06-21 stsp
726 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
727 66bea077 2018-08-02 stsp if (cwd == NULL) {
728 66bea077 2018-08-02 stsp error = got_error_from_errno();
729 66bea077 2018-08-02 stsp goto done;
730 66bea077 2018-08-02 stsp }
731 66bea077 2018-08-02 stsp if (repo_path == NULL) {
732 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
733 66bea077 2018-08-02 stsp if (repo_path == NULL) {
734 66bea077 2018-08-02 stsp error = got_error_from_errno();
735 66bea077 2018-08-02 stsp goto done;
736 66bea077 2018-08-02 stsp }
737 66bea077 2018-08-02 stsp }
738 66bea077 2018-08-02 stsp
739 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
740 404c43c4 2018-06-21 stsp if (error != NULL)
741 404c43c4 2018-06-21 stsp goto done;
742 404c43c4 2018-06-21 stsp
743 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
744 66bea077 2018-08-02 stsp if (error != NULL)
745 66bea077 2018-08-02 stsp goto done;
746 66bea077 2018-08-02 stsp
747 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
748 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
749 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
750 404c43c4 2018-06-21 stsp if (error != NULL)
751 66bea077 2018-08-02 stsp goto done;
752 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
753 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
754 404c43c4 2018-06-21 stsp if (error != NULL)
755 66bea077 2018-08-02 stsp goto done;
756 404c43c4 2018-06-21 stsp } else {
757 404c43c4 2018-06-21 stsp struct got_object *obj;
758 404c43c4 2018-06-21 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
759 404c43c4 2018-06-21 stsp if (error != NULL)
760 66bea077 2018-08-02 stsp goto done;
761 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
762 404c43c4 2018-06-21 stsp if (commit_id == NULL)
763 404c43c4 2018-06-21 stsp error = got_error_from_errno();
764 404c43c4 2018-06-21 stsp got_object_close(obj);
765 404c43c4 2018-06-21 stsp }
766 404c43c4 2018-06-21 stsp
767 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
768 404c43c4 2018-06-21 stsp done:
769 66bea077 2018-08-02 stsp free(in_repo_path);
770 66bea077 2018-08-02 stsp free(repo_path);
771 66bea077 2018-08-02 stsp free(cwd);
772 404c43c4 2018-06-21 stsp free(commit_id);
773 ad242220 2018-09-08 stsp if (repo) {
774 ad242220 2018-09-08 stsp const struct got_error *repo_error;
775 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
776 ad242220 2018-09-08 stsp if (error == NULL)
777 ad242220 2018-09-08 stsp error = repo_error;
778 ad242220 2018-09-08 stsp }
779 404c43c4 2018-06-21 stsp return error;
780 5de5890b 2018-10-18 stsp }
781 5de5890b 2018-10-18 stsp
782 5de5890b 2018-10-18 stsp __dead static void
783 5de5890b 2018-10-18 stsp usage_tree(void)
784 5de5890b 2018-10-18 stsp {
785 5de5890b 2018-10-18 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
786 5de5890b 2018-10-18 stsp getprogname());
787 5de5890b 2018-10-18 stsp exit(1);
788 5de5890b 2018-10-18 stsp }
789 5de5890b 2018-10-18 stsp
790 5de5890b 2018-10-18 stsp
791 5de5890b 2018-10-18 stsp static const struct got_error *
792 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
793 5de5890b 2018-10-18 stsp int show_ids, struct got_repository *repo)
794 5de5890b 2018-10-18 stsp {
795 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
796 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
797 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
798 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
799 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
800 5de5890b 2018-10-18 stsp
801 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
802 5de5890b 2018-10-18 stsp if (err)
803 5de5890b 2018-10-18 stsp goto done;
804 5de5890b 2018-10-18 stsp
805 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
806 5de5890b 2018-10-18 stsp if (err)
807 5de5890b 2018-10-18 stsp goto done;
808 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
809 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
810 5de5890b 2018-10-18 stsp while (te) {
811 5de5890b 2018-10-18 stsp char *id = NULL;
812 5de5890b 2018-10-18 stsp if (show_ids) {
813 5de5890b 2018-10-18 stsp char *id_str;
814 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
815 5de5890b 2018-10-18 stsp if (err)
816 5de5890b 2018-10-18 stsp goto done;
817 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
818 5de5890b 2018-10-18 stsp err = got_error_from_errno();
819 5de5890b 2018-10-18 stsp free(id_str);
820 5de5890b 2018-10-18 stsp goto done;
821 5de5890b 2018-10-18 stsp }
822 5de5890b 2018-10-18 stsp free(id_str);
823 5de5890b 2018-10-18 stsp }
824 5de5890b 2018-10-18 stsp printf("%s%s%s\n", id ? id : "",
825 5de5890b 2018-10-18 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
826 5de5890b 2018-10-18 stsp te = SIMPLEQ_NEXT(te, entry);
827 5de5890b 2018-10-18 stsp free(id);
828 5de5890b 2018-10-18 stsp }
829 5de5890b 2018-10-18 stsp done:
830 5de5890b 2018-10-18 stsp if (tree)
831 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
832 5de5890b 2018-10-18 stsp free(tree_id);
833 5de5890b 2018-10-18 stsp return err;
834 404c43c4 2018-06-21 stsp }
835 404c43c4 2018-06-21 stsp
836 5de5890b 2018-10-18 stsp static const struct got_error *
837 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
838 5de5890b 2018-10-18 stsp {
839 5de5890b 2018-10-18 stsp const struct got_error *error;
840 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
841 5de5890b 2018-10-18 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
842 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
843 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
844 5de5890b 2018-10-18 stsp int show_ids = 0;
845 5de5890b 2018-10-18 stsp int ch;
846 5de5890b 2018-10-18 stsp
847 5de5890b 2018-10-18 stsp #ifndef PROFILE
848 5de5890b 2018-10-18 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
849 5de5890b 2018-10-18 stsp == -1)
850 5de5890b 2018-10-18 stsp err(1, "pledge");
851 5de5890b 2018-10-18 stsp #endif
852 5de5890b 2018-10-18 stsp
853 5de5890b 2018-10-18 stsp while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
854 5de5890b 2018-10-18 stsp switch (ch) {
855 5de5890b 2018-10-18 stsp case 'c':
856 5de5890b 2018-10-18 stsp commit_id_str = optarg;
857 5de5890b 2018-10-18 stsp break;
858 5de5890b 2018-10-18 stsp case 'r':
859 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
860 5de5890b 2018-10-18 stsp if (repo_path == NULL)
861 5de5890b 2018-10-18 stsp err(1, "-r option");
862 5de5890b 2018-10-18 stsp break;
863 5de5890b 2018-10-18 stsp case 'i':
864 5de5890b 2018-10-18 stsp show_ids = 1;
865 5de5890b 2018-10-18 stsp break;
866 5de5890b 2018-10-18 stsp default:
867 5de5890b 2018-10-18 stsp usage();
868 5de5890b 2018-10-18 stsp /* NOTREACHED */
869 5de5890b 2018-10-18 stsp }
870 5de5890b 2018-10-18 stsp }
871 5de5890b 2018-10-18 stsp
872 5de5890b 2018-10-18 stsp argc -= optind;
873 5de5890b 2018-10-18 stsp argv += optind;
874 5de5890b 2018-10-18 stsp
875 5de5890b 2018-10-18 stsp if (argc == 1)
876 5de5890b 2018-10-18 stsp path = argv[0];
877 5de5890b 2018-10-18 stsp else if (argc > 1)
878 5de5890b 2018-10-18 stsp usage_tree();
879 5de5890b 2018-10-18 stsp else
880 5de5890b 2018-10-18 stsp path = "/";
881 5de5890b 2018-10-18 stsp
882 5de5890b 2018-10-18 stsp cwd = getcwd(NULL, 0);
883 5de5890b 2018-10-18 stsp if (cwd == NULL) {
884 5de5890b 2018-10-18 stsp error = got_error_from_errno();
885 5de5890b 2018-10-18 stsp goto done;
886 5de5890b 2018-10-18 stsp }
887 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
888 5de5890b 2018-10-18 stsp repo_path = strdup(cwd);
889 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
890 5de5890b 2018-10-18 stsp error = got_error_from_errno();
891 5de5890b 2018-10-18 stsp goto done;
892 5de5890b 2018-10-18 stsp }
893 5de5890b 2018-10-18 stsp }
894 5de5890b 2018-10-18 stsp
895 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
896 5de5890b 2018-10-18 stsp if (error != NULL)
897 5de5890b 2018-10-18 stsp goto done;
898 5de5890b 2018-10-18 stsp
899 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
900 5de5890b 2018-10-18 stsp if (error != NULL)
901 5de5890b 2018-10-18 stsp goto done;
902 5de5890b 2018-10-18 stsp
903 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
904 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
905 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
906 5de5890b 2018-10-18 stsp if (error != NULL)
907 5de5890b 2018-10-18 stsp goto done;
908 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
909 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
910 5de5890b 2018-10-18 stsp if (error != NULL)
911 5de5890b 2018-10-18 stsp goto done;
912 5de5890b 2018-10-18 stsp } else {
913 5de5890b 2018-10-18 stsp struct got_object *obj;
914 5de5890b 2018-10-18 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
915 5de5890b 2018-10-18 stsp if (error != NULL)
916 5de5890b 2018-10-18 stsp goto done;
917 5de5890b 2018-10-18 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
918 5de5890b 2018-10-18 stsp if (commit_id == NULL)
919 5de5890b 2018-10-18 stsp error = got_error_from_errno();
920 5de5890b 2018-10-18 stsp got_object_close(obj);
921 5de5890b 2018-10-18 stsp }
922 5de5890b 2018-10-18 stsp
923 5de5890b 2018-10-18 stsp error = print_tree(in_repo_path, commit_id, show_ids, repo);
924 5de5890b 2018-10-18 stsp done:
925 5de5890b 2018-10-18 stsp free(in_repo_path);
926 5de5890b 2018-10-18 stsp free(repo_path);
927 5de5890b 2018-10-18 stsp free(cwd);
928 5de5890b 2018-10-18 stsp free(commit_id);
929 5de5890b 2018-10-18 stsp if (repo) {
930 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
931 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
932 5de5890b 2018-10-18 stsp if (error == NULL)
933 5de5890b 2018-10-18 stsp error = repo_error;
934 5de5890b 2018-10-18 stsp }
935 5de5890b 2018-10-18 stsp return error;
936 5de5890b 2018-10-18 stsp }
937 5de5890b 2018-10-18 stsp
938 f42b1b34 2018-03-12 stsp #ifdef notyet
939 4ed7e80c 2018-05-20 stsp static const struct got_error *
940 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
941 5c860e29 2018-03-12 stsp {
942 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
943 5c860e29 2018-03-12 stsp git_status_list *status;
944 5c860e29 2018-03-12 stsp git_status_options statusopts;
945 5c860e29 2018-03-12 stsp size_t i;
946 5c860e29 2018-03-12 stsp
947 5c860e29 2018-03-12 stsp git_libgit2_init();
948 5c860e29 2018-03-12 stsp
949 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
950 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
951 5c860e29 2018-03-12 stsp
952 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
953 5c860e29 2018-03-12 stsp errx(1, "bar repository");
954 5c860e29 2018-03-12 stsp
955 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
956 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
957 5c860e29 2018-03-12 stsp
958 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
959 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
960 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
961 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
962 5c860e29 2018-03-12 stsp
963 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
964 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
965 5c860e29 2018-03-12 stsp
966 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
967 5c860e29 2018-03-12 stsp const git_status_entry *se;
968 5c860e29 2018-03-12 stsp
969 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
970 5c860e29 2018-03-12 stsp switch (se->status) {
971 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
972 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
973 5c860e29 2018-03-12 stsp break;
974 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
975 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
976 5c860e29 2018-03-12 stsp break;
977 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
978 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
979 5c860e29 2018-03-12 stsp break;
980 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
981 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
982 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
983 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
984 5c860e29 2018-03-12 stsp break;
985 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
986 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
987 5c860e29 2018-03-12 stsp break;
988 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
989 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
990 5c860e29 2018-03-12 stsp break;
991 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
992 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
993 5c860e29 2018-03-12 stsp break;
994 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
995 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
996 5c860e29 2018-03-12 stsp break;
997 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
998 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
999 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
1000 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
1001 5c860e29 2018-03-12 stsp break;
1002 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
1003 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
1004 5c860e29 2018-03-12 stsp break;
1005 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
1006 5c860e29 2018-03-12 stsp default:
1007 5c860e29 2018-03-12 stsp break;
1008 5c860e29 2018-03-12 stsp }
1009 5c860e29 2018-03-12 stsp }
1010 5c860e29 2018-03-12 stsp
1011 5c860e29 2018-03-12 stsp git_status_list_free(status);
1012 5c860e29 2018-03-12 stsp git_repository_free(repo);
1013 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
1014 5c860e29 2018-03-12 stsp
1015 5c860e29 2018-03-12 stsp return 0;
1016 5c860e29 2018-03-12 stsp }
1017 f42b1b34 2018-03-12 stsp #endif