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"
35 #ifndef nitems
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 #endif
39 struct cmd {
40 const char *cmd_name;
41 const struct got_error *(*cmd_main)(int, char *[]);
42 void (*cmd_usage)(void);
43 const char *cmd_descr;
44 };
46 __dead void usage(void);
47 __dead void usage_checkout(void);
48 __dead void usage_log(void);
50 const struct got_error* cmd_checkout(int, char *[]);
51 const struct got_error* cmd_log(int, char *[]);
52 const struct got_error* cmd_status(int, char *[]);
54 struct cmd got_commands[] = {
55 { "checkout", cmd_checkout, usage_checkout,
56 "check out a new work tree from a repository" },
57 { "log", cmd_log, usage_log,
58 "show repository history" },
59 #ifdef notyet
60 { "status", cmd_status, usage_status,
61 "show modification status of files" },
62 #endif
63 };
65 int
66 main(int argc, char *argv[])
67 {
68 struct cmd *cmd;
69 unsigned int i;
70 int ch;
71 int hflag = 0;
73 setlocale(LC_ALL, "");
75 while ((ch = getopt(argc, argv, "h")) != -1) {
76 switch (ch) {
77 case 'h':
78 hflag = 1;
79 break;
80 default:
81 usage();
82 /* NOTREACHED */
83 }
84 }
86 argc -= optind;
87 argv += optind;
89 if (argc <= 0)
90 usage();
92 for (i = 0; i < nitems(got_commands); i++) {
93 const struct got_error *error;
95 cmd = &got_commands[i];
97 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
98 continue;
100 if (hflag)
101 got_commands[i].cmd_usage();
103 error = got_commands[i].cmd_main(argc, argv);
104 if (error) {
105 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
106 return 1;
109 return 0;
112 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
113 return 1;
116 __dead void
117 usage(void)
119 int i;
121 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
122 "Available commands:\n", getprogname());
123 for (i = 0; i < nitems(got_commands); i++) {
124 struct cmd *cmd = &got_commands[i];
125 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
127 exit(1);
130 __dead void
131 usage_checkout(void)
133 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
134 "[worktree-path]\n", getprogname());
135 exit(1);
138 static void
139 checkout_progress(void *arg, const char *path)
141 char *worktree_path = arg;
143 while (path[0] == '/')
144 path++;
146 printf("A %s/%s\n", worktree_path, path);
149 const struct got_error *
150 cmd_checkout(int argc, char *argv[])
152 const struct got_error *error = NULL;
153 struct got_repository *repo = NULL;
154 struct got_reference *head_ref = NULL;
155 struct got_worktree *worktree = NULL;
156 char *repo_path = NULL;
157 char *worktree_path = NULL;
158 const char *path_prefix = "";
159 int ch;
161 optind = 0;
162 while ((ch = getopt(argc, argv, "p:")) != -1) {
163 switch (ch) {
164 case 'p':
165 path_prefix = optarg;
166 break;
167 default:
168 usage();
169 /* NOTREACHED */
173 argc -= optind;
174 argv += optind;
176 #ifndef PROFILE
177 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
178 err(1, "pledge");
179 #endif
180 if (argc == 1) {
181 char *cwd, *base, *dotgit;
182 repo_path = argv[0];
183 cwd = getcwd(NULL, 0);
184 if (cwd == NULL)
185 err(1, "getcwd");
186 base = basename(repo_path);
187 if (base == NULL)
188 err(1, "basename");
189 dotgit = strstr(base, ".git");
190 if (dotgit)
191 *dotgit = '\0';
192 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
193 free(cwd);
194 return got_error(GOT_ERR_NO_MEM);
196 free(cwd);
197 } else if (argc == 2) {
198 repo_path = argv[0];
199 worktree_path = strdup(argv[1]);
200 if (worktree_path == NULL)
201 return got_error(GOT_ERR_NO_MEM);
202 } else
203 usage_checkout();
205 error = got_repo_open(&repo, repo_path);
206 if (error != NULL)
207 goto done;
208 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
209 if (error != NULL)
210 goto done;
212 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
213 if (error != NULL)
214 goto done;
216 error = got_worktree_open(&worktree, worktree_path);
217 if (error != NULL)
218 goto done;
220 error = got_worktree_checkout_files(worktree, head_ref, repo,
221 checkout_progress, worktree_path);
222 if (error != NULL)
223 goto done;
225 printf("checked out %s\n", worktree_path);
227 done:
228 free(worktree_path);
229 return error;
232 static const struct got_error *
233 print_commit_object(struct got_object *, struct got_object_id *,
234 struct got_repository *);
236 static const struct got_error *
237 print_parent_commits(struct got_commit_object *commit,
238 struct got_repository *repo)
240 struct got_parent_id *pid;
241 const struct got_error *err = NULL;
242 struct got_object *obj;
244 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
245 err = got_object_open(&obj, repo, pid->id);
246 if (err != NULL)
247 return err;
248 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
249 return got_error(GOT_ERR_OBJ_TYPE);
250 err = print_commit_object(obj, pid->id, repo);
251 got_object_close(obj);
254 return err;
257 static const struct got_error *
258 print_commit_object(struct got_object *obj, struct got_object_id *id,
259 struct got_repository *repo)
261 struct got_commit_object *commit;
262 char *buf;
263 const struct got_error *err;
265 err = got_object_commit_open(&commit, repo, obj);
266 if (err)
267 return err;
269 err = got_object_id_str(&buf, id);
270 if (err)
271 return err;
273 printf("-----------------------------------------------\n");
274 printf("commit: %s\n", buf);
275 printf("Author: %s\n", commit->author);
276 printf("\n%s\n", commit->logmsg);
278 free(buf);
280 err = print_parent_commits(commit, repo);
281 got_object_commit_close(commit);
282 return err;
285 __dead void
286 usage_log(void)
288 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
289 exit(1);
292 const struct got_error *
293 cmd_log(int argc, char *argv[])
295 const struct got_error *error;
296 struct got_repository *repo;
297 struct got_reference *head_ref;
298 struct got_object_id *id;
299 struct got_object *obj;
300 char *repo_path = NULL;
302 #ifndef PROFILE
303 if (pledge("stdio rpath wpath cpath", NULL) == -1)
304 err(1, "pledge");
305 #endif
306 if (argc == 1) {
307 repo_path = getcwd(NULL, 0);
308 if (repo_path == NULL)
309 err(1, "getcwd");
310 } else if (argc == 2)
311 repo_path = argv[1];
312 else
313 usage_log();
315 error = got_repo_open(&repo, repo_path);
316 if (error != NULL)
317 return error;
318 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
319 if (error != NULL)
320 return error;
321 error = got_ref_resolve(&id, repo, head_ref);
322 if (error != NULL)
323 return error;
325 error = got_object_open(&obj, repo, id);
326 if (error != NULL)
327 return error;
328 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
329 error = print_commit_object(obj, id, repo);
330 if (error)
331 return error;
332 } else
333 return got_error(GOT_ERR_OBJ_TYPE);
334 got_object_close(obj);
335 free(id);
336 got_ref_close(head_ref);
337 got_repo_close(repo);
338 return NULL;
341 #ifdef notyet
342 const struct got_error *
343 cmd_status(int argc __unused, char *argv[] __unused)
345 git_repository *repo = NULL;
346 git_status_list *status;
347 git_status_options statusopts;
348 size_t i;
350 git_libgit2_init();
352 if (git_repository_open_ext(&repo, ".", 0, NULL))
353 errx(1, "git_repository_open: %s", giterr_last()->message);
355 if (git_repository_is_bare(repo))
356 errx(1, "bar repository");
358 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
359 errx(1, "git_status_init_options: %s", giterr_last()->message);
361 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
362 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
363 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
364 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
366 if (git_status_list_new(&status, repo, &statusopts))
367 errx(1, "git_status_list_new: %s", giterr_last()->message);
369 for (i = 0; i < git_status_list_entrycount(status); i++) {
370 const git_status_entry *se;
372 se = git_status_byindex(status, i);
373 switch (se->status) {
374 case GIT_STATUS_WT_NEW:
375 printf("? %s\n", se->index_to_workdir->new_file.path);
376 break;
377 case GIT_STATUS_WT_MODIFIED:
378 printf("M %s\n", se->index_to_workdir->new_file.path);
379 break;
380 case GIT_STATUS_WT_DELETED:
381 printf("R %s\n", se->index_to_workdir->new_file.path);
382 break;
383 case GIT_STATUS_WT_RENAMED:
384 printf("m %s -> %s\n",
385 se->index_to_workdir->old_file.path,
386 se->index_to_workdir->new_file.path);
387 break;
388 case GIT_STATUS_WT_TYPECHANGE:
389 printf("t %s\n", se->index_to_workdir->new_file.path);
390 break;
391 case GIT_STATUS_INDEX_NEW:
392 printf("A %s\n", se->head_to_index->new_file.path);
393 break;
394 case GIT_STATUS_INDEX_MODIFIED:
395 printf("M %s\n", se->head_to_index->old_file.path);
396 break;
397 case GIT_STATUS_INDEX_DELETED:
398 printf("R %s\n", se->head_to_index->old_file.path);
399 break;
400 case GIT_STATUS_INDEX_RENAMED:
401 printf("m %s -> %s\n",
402 se->head_to_index->old_file.path,
403 se->head_to_index->new_file.path);
404 break;
405 case GIT_STATUS_INDEX_TYPECHANGE:
406 printf("t %s\n", se->head_to_index->old_file.path);
407 break;
408 case GIT_STATUS_CURRENT:
409 default:
410 break;
414 git_status_list_free(status);
415 git_repository_free(repo);
416 git_libgit2_shutdown();
418 return 0;
420 #endif