Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libgen.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_refs.h"
32 #include "got_repository.h"
33 #include "got_worktree.h"
34 #include "got_diff.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 #endif
40 struct cmd {
41 const char *cmd_name;
42 const struct got_error *(*cmd_main)(int, char *[]);
43 void (*cmd_usage)(void);
44 const char *cmd_descr;
45 };
47 __dead void usage(void);
48 __dead void usage_checkout(void);
49 __dead void usage_log(void);
51 const struct got_error* cmd_checkout(int, char *[]);
52 const struct got_error* cmd_log(int, char *[]);
53 const struct got_error* cmd_status(int, char *[]);
55 struct cmd got_commands[] = {
56 { "checkout", cmd_checkout, usage_checkout,
57 "check out a new work tree from a repository" },
58 { "log", cmd_log, usage_log,
59 "show repository history" },
60 #ifdef notyet
61 { "status", cmd_status, usage_status,
62 "show modification status of files" },
63 #endif
64 };
66 int
67 main(int argc, char *argv[])
68 {
69 struct cmd *cmd;
70 unsigned int i;
71 int ch;
72 int hflag = 0;
74 setlocale(LC_ALL, "");
76 while ((ch = getopt(argc, argv, "h")) != -1) {
77 switch (ch) {
78 case 'h':
79 hflag = 1;
80 break;
81 default:
82 usage();
83 /* NOTREACHED */
84 }
85 }
87 argc -= optind;
88 argv += optind;
89 optind = 0;
91 if (argc <= 0)
92 usage();
94 for (i = 0; i < nitems(got_commands); i++) {
95 const struct got_error *error;
97 cmd = &got_commands[i];
99 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
100 continue;
102 if (hflag)
103 got_commands[i].cmd_usage();
105 error = got_commands[i].cmd_main(argc, argv);
106 if (error) {
107 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
108 return 1;
111 return 0;
114 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
115 return 1;
118 __dead void
119 usage(void)
121 int i;
123 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
124 "Available commands:\n", getprogname());
125 for (i = 0; i < nitems(got_commands); i++) {
126 struct cmd *cmd = &got_commands[i];
127 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
129 exit(1);
132 __dead void
133 usage_checkout(void)
135 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
136 "[worktree-path]\n", getprogname());
137 exit(1);
140 static void
141 checkout_progress(void *arg, const char *path)
143 char *worktree_path = arg;
145 while (path[0] == '/')
146 path++;
148 printf("A %s/%s\n", worktree_path, path);
151 const struct got_error *
152 cmd_checkout(int argc, char *argv[])
154 const struct got_error *error = NULL;
155 struct got_repository *repo = NULL;
156 struct got_reference *head_ref = NULL;
157 struct got_worktree *worktree = NULL;
158 char *repo_path = NULL;
159 char *worktree_path = NULL;
160 const char *path_prefix = "";
161 int ch;
163 while ((ch = getopt(argc, argv, "p:")) != -1) {
164 switch (ch) {
165 case 'p':
166 path_prefix = optarg;
167 break;
168 default:
169 usage();
170 /* NOTREACHED */
174 argc -= optind;
175 argv += optind;
177 #ifndef PROFILE
178 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
179 err(1, "pledge");
180 #endif
181 if (argc == 1) {
182 char *cwd, *base, *dotgit;
183 repo_path = argv[0];
184 cwd = getcwd(NULL, 0);
185 if (cwd == NULL)
186 err(1, "getcwd");
187 base = basename(repo_path);
188 if (base == NULL)
189 err(1, "basename");
190 dotgit = strstr(base, ".git");
191 if (dotgit)
192 *dotgit = '\0';
193 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 error = got_error_from_errno();
195 free(cwd);
196 return error;
198 free(cwd);
199 } else if (argc == 2) {
200 repo_path = argv[0];
201 worktree_path = strdup(argv[1]);
202 if (worktree_path == NULL)
203 return got_error_from_errno();
204 } else
205 usage_checkout();
207 error = got_repo_open(&repo, repo_path);
208 if (error != NULL)
209 goto done;
210 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
211 if (error != NULL)
212 goto done;
214 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
215 if (error != NULL)
216 goto done;
218 error = got_worktree_open(&worktree, worktree_path);
219 if (error != NULL)
220 goto done;
222 error = got_worktree_checkout_files(worktree, head_ref, repo,
223 checkout_progress, worktree_path);
224 if (error != NULL)
225 goto done;
227 printf("checked out %s\n", worktree_path);
229 done:
230 free(worktree_path);
231 return error;
234 static const struct got_error *
235 print_patch(struct got_commit_object *commit, struct got_object_id *id,
236 struct got_repository *repo)
238 const struct got_error *err = NULL;
239 struct got_tree_object *tree1 = NULL, *tree2;
240 struct got_object *obj;
241 struct got_parent_id *pid;
243 err = got_object_open(&obj, repo, commit->tree_id);
244 if (err)
245 return err;
247 err = got_object_tree_open(&tree2, repo, obj);
248 got_object_close(obj);
249 if (err)
250 return err;
252 pid = SIMPLEQ_FIRST(&commit->parent_ids);
253 if (pid != NULL) {
254 struct got_commit_object *pcommit;
256 err = got_object_open(&obj, repo, pid->id);
257 if (err)
258 return err;
260 err = got_object_commit_open(&pcommit, repo, obj);
261 got_object_close(obj);
262 if (err)
263 return err;
265 err = got_object_open(&obj, repo, pcommit->tree_id);
266 got_object_commit_close(pcommit);
267 if (err)
268 return err;
269 err = got_object_tree_open(&tree1, repo, obj);
270 got_object_close(obj);
271 if (err)
272 return err;
275 err = got_diff_tree(tree1, tree2, repo, stdout);
276 if (tree1)
277 got_object_tree_close(tree1);
278 got_object_tree_close(tree2);
279 return err;
282 static const struct got_error *
283 print_commit(struct got_commit_object *commit, struct got_object_id *id,
284 struct got_repository *repo, int show_patch)
286 const struct got_error *err = NULL;
287 char *buf;
289 err = got_object_id_str(&buf, id);
290 if (err)
291 return err;
293 printf("-----------------------------------------------\n");
294 printf("commit: %s\n", buf);
295 printf("Author: %s\n", commit->author);
296 printf("\n%s\n", commit->logmsg);
298 if (show_patch) {
299 err = print_patch(commit, id, repo);
300 if (err == 0)
301 printf("\n");
304 free(buf);
305 return err;
308 struct commit_queue_entry {
309 TAILQ_ENTRY(commit_queue_entry) entry;
310 struct got_object_id *id;
311 struct got_commit_object *commit;
312 };
314 static const struct got_error *
315 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
316 struct got_repository *repo, int show_patch)
318 const struct got_error *err;
319 struct got_commit_object *root_commit;
320 TAILQ_HEAD(, commit_queue_entry) commits;
321 struct commit_queue_entry *entry;
323 TAILQ_INIT(&commits);
325 err = got_object_commit_open(&root_commit, repo, root_obj);
326 if (err)
327 return err;
329 entry = calloc(1, sizeof(*entry));
330 if (entry == NULL)
331 return got_error_from_errno();
332 entry->id = got_object_id_dup(root_id);
333 if (entry->id == NULL) {
334 err = got_error_from_errno();
335 free(entry);
336 return err;
338 entry->commit = root_commit;
339 TAILQ_INSERT_HEAD(&commits, entry, entry);
341 while (!TAILQ_EMPTY(&commits)) {
342 struct got_parent_id *pid;
344 entry = TAILQ_FIRST(&commits);
345 err = print_commit(entry->commit, entry->id, repo, show_patch);
346 if (err)
347 break;
349 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
350 struct got_object *obj;
351 struct got_commit_object *pcommit;
352 struct commit_queue_entry *pentry;
354 err = got_object_open(&obj, repo, pid->id);
355 if (err)
356 break;
357 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
358 err = got_error(GOT_ERR_OBJ_TYPE);
359 break;
362 err = got_object_commit_open(&pcommit, repo, obj);
363 got_object_close(obj);
364 if (err)
365 break;
367 pentry = calloc(1, sizeof(*pentry));
368 if (pentry == NULL) {
369 err = got_error_from_errno();
370 got_object_commit_close(pcommit);
371 break;
373 pentry->id = got_object_id_dup(pid->id);
374 if (pentry->id == NULL) {
375 err = got_error_from_errno();
376 got_object_commit_close(pcommit);
377 break;
379 pentry->commit = pcommit;
380 TAILQ_INSERT_TAIL(&commits, pentry, entry);
383 TAILQ_REMOVE(&commits, entry, entry);
384 got_object_commit_close(entry->commit);
385 free(entry->id);
386 free(entry);
389 return err;
392 __dead void
393 usage_log(void)
395 fprintf(stderr, "usage: %s log [-p] [repository-path]\n",
396 getprogname());
397 exit(1);
400 const struct got_error *
401 cmd_log(int argc, char *argv[])
403 const struct got_error *error;
404 struct got_repository *repo;
405 struct got_reference *head_ref;
406 struct got_object_id *id;
407 struct got_object *obj;
408 char *repo_path = NULL;
409 int ch;
410 int show_patch = 0;
412 #ifndef PROFILE
413 if (pledge("stdio rpath wpath cpath", NULL) == -1)
414 err(1, "pledge");
415 #endif
417 while ((ch = getopt(argc, argv, "p")) != -1) {
418 switch (ch) {
419 case 'p':
420 show_patch = 1;
421 break;
422 default:
423 usage();
424 /* NOTREACHED */
428 argc -= optind;
429 argv += optind;
431 if (argc == 0) {
432 repo_path = getcwd(NULL, 0);
433 if (repo_path == NULL)
434 err(1, "getcwd");
435 } else if (argc == 1)
436 repo_path = argv[0];
437 else
438 usage_log();
440 error = got_repo_open(&repo, repo_path);
441 if (error != NULL)
442 return error;
443 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
444 if (error != NULL)
445 return error;
446 error = got_ref_resolve(&id, repo, head_ref);
447 if (error != NULL)
448 return error;
450 error = got_object_open(&obj, repo, id);
451 if (error != NULL)
452 return error;
453 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
454 error = print_commits(obj, id, repo, show_patch);
455 else
456 error = got_error(GOT_ERR_OBJ_TYPE);
457 got_object_close(obj);
458 free(id);
459 got_ref_close(head_ref);
460 got_repo_close(repo);
461 return error;
464 #ifdef notyet
465 const struct got_error *
466 cmd_status(int argc __unused, char *argv[] __unused)
468 git_repository *repo = NULL;
469 git_status_list *status;
470 git_status_options statusopts;
471 size_t i;
473 git_libgit2_init();
475 if (git_repository_open_ext(&repo, ".", 0, NULL))
476 errx(1, "git_repository_open: %s", giterr_last()->message);
478 if (git_repository_is_bare(repo))
479 errx(1, "bar repository");
481 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
482 errx(1, "git_status_init_options: %s", giterr_last()->message);
484 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
485 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
486 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
487 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
489 if (git_status_list_new(&status, repo, &statusopts))
490 errx(1, "git_status_list_new: %s", giterr_last()->message);
492 for (i = 0; i < git_status_list_entrycount(status); i++) {
493 const git_status_entry *se;
495 se = git_status_byindex(status, i);
496 switch (se->status) {
497 case GIT_STATUS_WT_NEW:
498 printf("? %s\n", se->index_to_workdir->new_file.path);
499 break;
500 case GIT_STATUS_WT_MODIFIED:
501 printf("M %s\n", se->index_to_workdir->new_file.path);
502 break;
503 case GIT_STATUS_WT_DELETED:
504 printf("R %s\n", se->index_to_workdir->new_file.path);
505 break;
506 case GIT_STATUS_WT_RENAMED:
507 printf("m %s -> %s\n",
508 se->index_to_workdir->old_file.path,
509 se->index_to_workdir->new_file.path);
510 break;
511 case GIT_STATUS_WT_TYPECHANGE:
512 printf("t %s\n", se->index_to_workdir->new_file.path);
513 break;
514 case GIT_STATUS_INDEX_NEW:
515 printf("A %s\n", se->head_to_index->new_file.path);
516 break;
517 case GIT_STATUS_INDEX_MODIFIED:
518 printf("M %s\n", se->head_to_index->old_file.path);
519 break;
520 case GIT_STATUS_INDEX_DELETED:
521 printf("R %s\n", se->head_to_index->old_file.path);
522 break;
523 case GIT_STATUS_INDEX_RENAMED:
524 printf("m %s -> %s\n",
525 se->head_to_index->old_file.path,
526 se->head_to_index->new_file.path);
527 break;
528 case GIT_STATUS_INDEX_TYPECHANGE:
529 printf("t %s\n", se->head_to_index->old_file.path);
530 break;
531 case GIT_STATUS_CURRENT:
532 default:
533 break;
537 git_status_list_free(status);
538 git_repository_free(repo);
539 git_libgit2_shutdown();
541 return 0;
543 #endif