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