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>
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.
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.
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>
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>
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"
41 5c860e29 2018-03-12 stsp #ifndef nitems
42 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
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);
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 *[]);
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" },
82 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
84 5c860e29 2018-03-12 stsp struct cmd *cmd;
85 5c860e29 2018-03-12 stsp unsigned int i;
87 1b6b95a8 2018-03-12 stsp int hflag = 0;
89 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
91 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
92 5c860e29 2018-03-12 stsp switch (ch) {
98 5c860e29 2018-03-12 stsp /* NOTREACHED */
102 5c860e29 2018-03-12 stsp argc -= optind;
103 5c860e29 2018-03-12 stsp argv += optind;
104 1e70621d 2018-03-27 stsp optind = 0;
106 5c860e29 2018-03-12 stsp if (argc <= 0)
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;
112 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
114 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
118 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
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);
129 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
133 4ed7e80c 2018-05-20 stsp __dead static void
134 5c860e29 2018-03-12 stsp usage(void)
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);
147 4ed7e80c 2018-05-20 stsp __dead static void
148 c09a553d 2018-03-12 stsp usage_checkout(void)
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());
155 92a684f4 2018-03-12 stsp static void
156 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
158 92a684f4 2018-03-12 stsp char *worktree_path = arg;
160 92a684f4 2018-03-12 stsp while (path[0] == '/')
163 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
166 4ed7e80c 2018-05-20 stsp static const struct got_error *
167 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
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 = "";
178 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
179 0bb8a95e 2018-03-12 stsp switch (ch) {
181 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
185 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
189 0bb8a95e 2018-03-12 stsp argc -= optind;
190 0bb8a95e 2018-03-12 stsp argv += optind;
192 6715a751 2018-03-16 stsp #ifndef PROFILE
193 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
195 c09a553d 2018-03-12 stsp err(1, "pledge");
197 0bb8a95e 2018-03-12 stsp if (argc == 1) {
198 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
199 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
200 76089277 2018-04-01 stsp if (repo_path == NULL)
201 76089277 2018-04-01 stsp return got_error_from_errno();
202 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
203 76089277 2018-04-01 stsp if (cwd == NULL) {
204 76089277 2018-04-01 stsp error = got_error_from_errno();
207 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
208 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
210 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
211 76089277 2018-04-01 stsp if (base == NULL) {
212 76089277 2018-04-01 stsp error = got_error_from_errno();
215 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
216 c09a553d 2018-03-12 stsp if (dotgit)
217 c09a553d 2018-03-12 stsp *dotgit = '\0';
218 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
219 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
224 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
225 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
226 76089277 2018-04-01 stsp if (repo_path == NULL) {
227 76089277 2018-04-01 stsp error = got_error_from_errno();
230 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
231 76089277 2018-04-01 stsp if (worktree_path == NULL) {
232 76089277 2018-04-01 stsp error = got_error_from_errno();
236 c09a553d 2018-03-12 stsp usage_checkout();
238 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
239 c09a553d 2018-03-12 stsp if (error != NULL)
241 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
242 c09a553d 2018-03-12 stsp if (error != NULL)
245 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
246 c09a553d 2018-03-12 stsp if (error != NULL)
249 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
250 c09a553d 2018-03-12 stsp if (error != NULL)
253 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
254 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
255 c09a553d 2018-03-12 stsp if (error != NULL)
258 b65ae19a 2018-04-24 stsp printf("Checked out %s\n", worktree_path);
259 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
262 76089277 2018-04-01 stsp free(repo_path);
263 c09a553d 2018-03-12 stsp free(worktree_path);
264 c09a553d 2018-03-12 stsp return error;
267 f42b1b34 2018-03-12 stsp static const struct got_error *
268 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
269 f42b1b34 2018-03-12 stsp struct got_repository *repo)
271 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
272 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
273 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
275 117e771c 2018-07-23 stsp err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
277 79109fed 2018-03-27 stsp return err;
279 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
280 79f35eb3 2018-06-11 stsp if (qid != NULL) {
281 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
283 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
285 79109fed 2018-03-27 stsp return err;
287 117e771c 2018-07-23 stsp err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
288 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
290 79109fed 2018-03-27 stsp return err;
293 f6861a81 2018-09-13 stsp err = got_diff_tree(tree1, tree2, "", "", repo, stdout);
295 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
296 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
297 79109fed 2018-03-27 stsp return err;
300 4bb494d5 2018-06-16 stsp static char *
301 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
303 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
304 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
310 79109fed 2018-03-27 stsp static const struct got_error *
311 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
312 1fd6d7ea 2018-06-13 stsp struct got_repository *repo, int show_patch)
314 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
315 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
316 6c281f94 2018-06-11 stsp char datebuf[26];
317 788c352e 2018-06-16 stsp time_t author_time, committer_time;
319 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
321 8bf5b3c9 2018-03-17 stsp return err;
323 788c352e 2018-06-16 stsp author_time = mktime(&commit->tm_author);
324 788c352e 2018-06-16 stsp committer_time = mktime(&commit->tm_committer);
326 788c352e 2018-06-16 stsp /* This would express the date in committer's timezone. */
327 788c352e 2018-06-16 stsp author_time += commit->tm_author.tm_gmtoff;
328 788c352e 2018-06-16 stsp committer_time += commit->tm_committer.tm_gmtoff;
331 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
332 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
333 832c249c 2018-06-10 stsp free(id_str);
334 0dcf3e9c 2018-09-15 stsp printf("from: %s\n", commit->author);
335 dab5fe87 2018-09-14 stsp datestr = get_datestr(&committer_time, datebuf);
336 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
337 dab5fe87 2018-09-14 stsp if (strcmp(commit->author, commit->committer) != 0)
338 0dcf3e9c 2018-09-15 stsp printf("via: %s\n", commit->committer);
339 3fe1abad 2018-06-10 stsp if (commit->nparents > 1) {
340 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
342 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
343 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
345 a0603db2 2018-06-10 stsp return err;
346 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
347 a0603db2 2018-06-10 stsp free(id_str);
351 621015ac 2018-07-23 stsp logmsg0 = strdup(commit->logmsg);
352 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
353 832c249c 2018-06-10 stsp return got_error_from_errno();
355 621015ac 2018-07-23 stsp logmsg = logmsg0;
357 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
359 832c249c 2018-06-10 stsp printf(" %s\n", line);
360 832c249c 2018-06-10 stsp } while (line);
361 621015ac 2018-07-23 stsp free(logmsg0);
363 971751ac 2018-03-27 stsp if (show_patch) {
364 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
365 971751ac 2018-03-27 stsp if (err == 0)
366 971751ac 2018-03-27 stsp printf("\n");
369 d82de447 2018-09-15 stsp fflush(stdout);
370 07862c20 2018-09-15 stsp return err;
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)
378 f42b1b34 2018-03-12 stsp const struct got_error *err;
379 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
381 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
382 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
384 f42b1b34 2018-03-12 stsp return err;
385 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
388 31cedeaf 2018-09-15 stsp while (1) {
389 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
390 372ccdbb 2018-06-10 stsp struct got_object_id *id;
392 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
394 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
395 9ba79e04 2018-06-11 stsp err = NULL;
398 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
400 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
406 b43fbaa0 2018-06-11 stsp if (id == NULL)
409 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
412 1fd6d7ea 2018-06-13 stsp err = print_commit(commit, id, repo, show_patch);
413 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
414 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
418 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
419 f42b1b34 2018-03-12 stsp return err;
422 4ed7e80c 2018-05-20 stsp __dead static void
423 6f3d1eb0 2018-03-12 stsp usage_log(void)
425 6238ee32 2018-06-13 stsp fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
426 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
430 4ed7e80c 2018-05-20 stsp static const struct got_error *
431 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
433 f42b1b34 2018-03-12 stsp const struct got_error *error;
434 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
435 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
436 d1f2edc9 2018-06-13 stsp struct got_object *obj = NULL;
437 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
438 d142fc45 2018-04-01 stsp char *start_commit = NULL;
440 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
441 64a96a6d 2018-04-01 stsp const char *errstr;
443 6715a751 2018-03-16 stsp #ifndef PROFILE
444 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
446 f42b1b34 2018-03-12 stsp err(1, "pledge");
449 04ca23f4 2018-07-16 stsp while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
450 79109fed 2018-03-27 stsp switch (ch) {
452 79109fed 2018-03-27 stsp show_patch = 1;
455 d142fc45 2018-04-01 stsp start_commit = optarg;
458 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
459 64a96a6d 2018-04-01 stsp if (errstr != NULL)
460 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
463 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
466 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
467 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
468 04ca23f4 2018-07-16 stsp err(1, "-r option");
472 79109fed 2018-03-27 stsp /* NOTREACHED */
476 79109fed 2018-03-27 stsp argc -= optind;
477 79109fed 2018-03-27 stsp argv += optind;
479 04ca23f4 2018-07-16 stsp if (argc == 0)
480 04ca23f4 2018-07-16 stsp path = strdup("");
481 04ca23f4 2018-07-16 stsp else if (argc == 1)
482 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
484 6f3d1eb0 2018-03-12 stsp usage_log();
485 04ca23f4 2018-07-16 stsp if (path == NULL)
486 04ca23f4 2018-07-16 stsp return got_error_from_errno();
488 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
489 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
490 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
493 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
494 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
495 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
496 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
501 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
502 d7d4f210 2018-03-12 stsp if (error != NULL)
505 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
506 3235492e 2018-04-01 stsp struct got_reference *head_ref;
507 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
508 3235492e 2018-04-01 stsp if (error != NULL)
509 3235492e 2018-04-01 stsp return error;
510 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
511 3235492e 2018-04-01 stsp got_ref_close(head_ref);
512 3235492e 2018-04-01 stsp if (error != NULL)
513 3235492e 2018-04-01 stsp return error;
514 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
516 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
517 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
518 3235492e 2018-04-01 stsp if (error == NULL) {
519 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
520 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
521 d1f2edc9 2018-06-13 stsp if (error != NULL)
522 d1f2edc9 2018-06-13 stsp return error;
523 d1f2edc9 2018-06-13 stsp error = got_object_open(&obj, repo, id);
524 d1f2edc9 2018-06-13 stsp if (error != NULL)
525 d1f2edc9 2018-06-13 stsp return error;
527 d1f2edc9 2018-06-13 stsp if (obj == NULL) {
528 d1f2edc9 2018-06-13 stsp error = got_object_open_by_id_str(&obj, repo,
529 d1f2edc9 2018-06-13 stsp start_commit);
530 d1f2edc9 2018-06-13 stsp if (error != NULL)
531 d1f2edc9 2018-06-13 stsp return error;
532 6402fb3c 2018-09-15 stsp id = got_object_id_dup(got_object_get_id(obj));
533 3235492e 2018-04-01 stsp if (id == NULL)
534 3235492e 2018-04-01 stsp error = got_error_from_errno();
537 d7d4f210 2018-03-12 stsp if (error != NULL)
539 04ca23f4 2018-07-16 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
540 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
544 04ca23f4 2018-07-16 stsp error = got_repo_map_path(&in_repo_path, repo, path);
545 04ca23f4 2018-07-16 stsp if (error != NULL)
547 04ca23f4 2018-07-16 stsp if (in_repo_path) {
548 04ca23f4 2018-07-16 stsp free(path);
549 04ca23f4 2018-07-16 stsp path = in_repo_path;
552 04ca23f4 2018-07-16 stsp error = print_commits(obj, id, repo, path, show_patch,
553 04ca23f4 2018-07-16 stsp limit, first_parent_traversal);
555 04ca23f4 2018-07-16 stsp free(path);
556 04ca23f4 2018-07-16 stsp free(repo_path);
559 04ca23f4 2018-07-16 stsp got_object_close(obj);
561 ad242220 2018-09-08 stsp if (repo) {
562 ad242220 2018-09-08 stsp const struct got_error *repo_error;
563 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
564 ad242220 2018-09-08 stsp if (error == NULL)
565 ad242220 2018-09-08 stsp error = repo_error;
567 8bf5b3c9 2018-03-17 stsp return error;
570 4ed7e80c 2018-05-20 stsp __dead static void
571 3f8b7d6a 2018-04-01 stsp usage_diff(void)
573 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
574 3f8b7d6a 2018-04-01 stsp getprogname());
578 4ed7e80c 2018-05-20 stsp static const struct got_error *
579 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
581 b00d56cd 2018-04-01 stsp const struct got_error *error;
582 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
583 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
584 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
585 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
588 b00d56cd 2018-04-01 stsp #ifndef PROFILE
589 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
591 b00d56cd 2018-04-01 stsp err(1, "pledge");
594 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
595 b00d56cd 2018-04-01 stsp switch (ch) {
598 b00d56cd 2018-04-01 stsp /* NOTREACHED */
602 b00d56cd 2018-04-01 stsp argc -= optind;
603 b00d56cd 2018-04-01 stsp argv += optind;
605 b00d56cd 2018-04-01 stsp if (argc == 0) {
606 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
607 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
608 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
609 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
610 e1e3f570 2018-04-01 stsp return got_error_from_errno();
611 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
612 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
613 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
614 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
615 76089277 2018-04-01 stsp if (repo_path == NULL)
616 76089277 2018-04-01 stsp return got_error_from_errno();
617 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
618 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
620 b00d56cd 2018-04-01 stsp usage_diff();
622 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
623 76089277 2018-04-01 stsp free(repo_path);
624 b00d56cd 2018-04-01 stsp if (error != NULL)
627 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
631 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
635 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
636 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
640 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
641 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
642 f6861a81 2018-09-13 stsp error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
643 f6861a81 2018-09-13 stsp repo, stdout);
645 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
646 f6861a81 2018-09-13 stsp error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
649 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
650 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
653 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
658 b00d56cd 2018-04-01 stsp got_object_close(obj1);
660 b00d56cd 2018-04-01 stsp got_object_close(obj2);
661 ad242220 2018-09-08 stsp if (repo) {
662 ad242220 2018-09-08 stsp const struct got_error *repo_error;
663 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
664 ad242220 2018-09-08 stsp if (error == NULL)
665 ad242220 2018-09-08 stsp error = repo_error;
667 b00d56cd 2018-04-01 stsp return error;
670 404c43c4 2018-06-21 stsp __dead static void
671 404c43c4 2018-06-21 stsp usage_blame(void)
673 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
674 404c43c4 2018-06-21 stsp getprogname());
678 404c43c4 2018-06-21 stsp static const struct got_error *
679 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
681 404c43c4 2018-06-21 stsp const struct got_error *error;
682 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
683 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
684 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
685 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
688 404c43c4 2018-06-21 stsp #ifndef PROFILE
689 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
691 404c43c4 2018-06-21 stsp err(1, "pledge");
694 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
695 404c43c4 2018-06-21 stsp switch (ch) {
697 404c43c4 2018-06-21 stsp commit_id_str = optarg;
700 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
701 66bea077 2018-08-02 stsp if (repo_path == NULL)
702 66bea077 2018-08-02 stsp err(1, "-r option");
706 404c43c4 2018-06-21 stsp /* NOTREACHED */
710 404c43c4 2018-06-21 stsp argc -= optind;
711 404c43c4 2018-06-21 stsp argv += optind;
713 a39318fd 2018-08-02 stsp if (argc == 1)
714 404c43c4 2018-06-21 stsp path = argv[0];
716 404c43c4 2018-06-21 stsp usage_blame();
718 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
719 66bea077 2018-08-02 stsp if (cwd == NULL) {
720 66bea077 2018-08-02 stsp error = got_error_from_errno();
723 66bea077 2018-08-02 stsp if (repo_path == NULL) {
724 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
725 66bea077 2018-08-02 stsp if (repo_path == NULL) {
726 66bea077 2018-08-02 stsp error = got_error_from_errno();
731 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
732 404c43c4 2018-06-21 stsp if (error != NULL)
735 66bea077 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
736 66bea077 2018-08-02 stsp if (error != NULL)
739 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
740 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
741 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
742 404c43c4 2018-06-21 stsp if (error != NULL)
744 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
745 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
746 404c43c4 2018-06-21 stsp if (error != NULL)
749 404c43c4 2018-06-21 stsp struct got_object *obj;
750 404c43c4 2018-06-21 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
751 404c43c4 2018-06-21 stsp if (error != NULL)
753 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
754 404c43c4 2018-06-21 stsp if (commit_id == NULL)
755 404c43c4 2018-06-21 stsp error = got_error_from_errno();
756 404c43c4 2018-06-21 stsp got_object_close(obj);
759 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
761 66bea077 2018-08-02 stsp free(in_repo_path);
762 66bea077 2018-08-02 stsp free(repo_path);
764 404c43c4 2018-06-21 stsp free(commit_id);
765 ad242220 2018-09-08 stsp if (repo) {
766 ad242220 2018-09-08 stsp const struct got_error *repo_error;
767 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
768 ad242220 2018-09-08 stsp if (error == NULL)
769 ad242220 2018-09-08 stsp error = repo_error;
771 404c43c4 2018-06-21 stsp return error;
774 f42b1b34 2018-03-12 stsp #ifdef notyet
775 4ed7e80c 2018-05-20 stsp static const struct got_error *
776 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
778 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
779 5c860e29 2018-03-12 stsp git_status_list *status;
780 5c860e29 2018-03-12 stsp git_status_options statusopts;
783 5c860e29 2018-03-12 stsp git_libgit2_init();
785 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
786 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
788 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
789 5c860e29 2018-03-12 stsp errx(1, "bar repository");
791 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
792 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
794 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
795 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
796 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
797 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
799 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
800 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
802 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
803 5c860e29 2018-03-12 stsp const git_status_entry *se;
805 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
806 5c860e29 2018-03-12 stsp switch (se->status) {
807 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
808 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
810 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
811 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
813 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
814 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
816 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
817 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
818 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
819 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
821 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
822 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
824 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
825 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
827 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
828 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
830 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
831 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
833 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
834 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
835 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
836 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
838 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
839 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
841 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
847 5c860e29 2018-03-12 stsp git_status_list_free(status);
848 5c860e29 2018-03-12 stsp git_repository_free(repo);
849 5c860e29 2018-03-12 stsp git_libgit2_shutdown();