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>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_refs.h"
31 #include "got_repository.h"
33 #ifndef nitems
34 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
35 #endif
37 struct cmd {
38 const char *cmd_name;
39 int (*cmd_main)(int, char *[]);
40 void (*cmd_usage)(void);
41 const char *cmd_descr;
42 };
44 __dead void usage(void);
45 __dead void usage_log(void);
47 int cmd_log(int, char *[]);
48 int cmd_status(int, char *[]);
50 struct cmd got_commands[] = {
51 { "log", cmd_log, usage_log,
52 "show repository history" },
53 #ifdef notyet
54 { "status", cmd_status, usage_status,
55 "show modification status of files" },
56 #endif
57 };
59 int
60 main(int argc, char *argv[])
61 {
62 struct cmd *cmd;
63 unsigned int i;
64 int ch;
65 int hflag = 0;
67 setlocale(LC_ALL, "");
69 if (pledge("stdio rpath wpath cpath", NULL) == -1)
70 err(1, "pledge");
72 while ((ch = getopt(argc, argv, "h")) != -1) {
73 switch (ch) {
74 case 'h':
75 hflag = 1;
76 break;
77 default:
78 usage();
79 /* NOTREACHED */
80 }
81 }
83 argc -= optind;
84 argv += optind;
86 if (argc <= 0)
87 usage();
89 for (i = 0; i < nitems(got_commands); i++) {
90 cmd = &got_commands[i];
92 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
93 continue;
95 if (hflag)
96 got_commands[i].cmd_usage();
98 return got_commands[i].cmd_main(argc, argv);
99 /* NOTREACHED */
102 fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
103 return 1;
106 __dead void
107 usage(void)
109 int i;
111 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\nAvailable commands:\n",
112 getprogname());
113 for (i = 0; i < nitems(got_commands); i++) {
114 struct cmd *cmd = &got_commands[i];
115 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
117 exit(1);
120 static const struct got_error *
121 print_commit_object(struct got_object *, struct got_object_id *,
122 struct got_repository *);
124 static const struct got_error *
125 print_parent_commits(struct got_commit_object *commit,
126 struct got_repository *repo)
128 struct got_parent_id *pid;
129 const struct got_error *err = NULL;
130 struct got_object *obj;
132 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
133 err = got_object_open(&obj, repo, pid->id);
134 if (err != NULL)
135 return err;
136 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
137 return got_error(GOT_ERR_OBJ_TYPE);
138 err = print_commit_object(obj, pid->id, repo);
139 got_object_close(obj);
142 return err;
145 static const struct got_error *
146 print_commit_object(struct got_object *obj, struct got_object_id *id,
147 struct got_repository *repo)
149 struct got_commit_object *commit;
150 char *buf;
151 const struct got_error *err;
153 err = got_object_commit_open(&commit, repo, obj);
154 if (err)
155 return err;
157 err = got_object_id_str(&buf, id);
158 if (err)
159 return err;
161 printf("-----------------------------------------------\n");
162 printf("commit: %s\n", buf);
163 printf("Author: %s\n", commit->author);
164 printf("\n%s\n", commit->logmsg);
166 free(buf);
168 err = print_parent_commits(commit, repo);
169 got_object_commit_close(commit);
170 return err;
173 __dead void
174 usage_log(void)
176 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
177 exit(1);
180 int
181 cmd_log(int argc, char *argv[])
183 const struct got_error *error;
184 struct got_repository *repo;
185 struct got_reference *head_ref;
186 struct got_object_id *id;
187 struct got_object *obj;
188 char *repo_path = NULL;
190 if (pledge("stdio rpath wpath cpath", NULL) == -1)
191 err(1, "pledge");
193 if (argc == 1) {
194 repo_path = getcwd(NULL, 0);
195 if (repo_path == NULL)
196 err(1, "getcwd");
197 } else if (argc == 2)
198 repo_path = argv[1];
199 else
200 usage_log();
202 error = got_repo_open(&repo, repo_path);
203 if (error != NULL || repo == NULL)
204 return 1;
205 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
206 if (error != NULL || head_ref == NULL)
207 return 1;
208 error = got_ref_resolve(&id, repo, head_ref);
209 if (error != NULL || head_ref == NULL)
210 return 1;
212 error = got_object_open(&obj, repo, id);
213 if (error != NULL || obj == NULL)
214 return 1;
215 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
216 error = print_commit_object(obj, id, repo);
217 if (error)
218 return 1;
219 } else
220 return 1;
221 got_object_close(obj);
222 free(id);
223 got_ref_close(head_ref);
224 got_repo_close(repo);
225 return 0;
228 #ifdef notyet
229 int
230 cmd_status(int argc __unused, char *argv[] __unused)
232 git_repository *repo = NULL;
233 git_status_list *status;
234 git_status_options statusopts;
235 size_t i;
237 git_libgit2_init();
239 if (git_repository_open_ext(&repo, ".", 0, NULL))
240 errx(1, "git_repository_open: %s", giterr_last()->message);
242 if (git_repository_is_bare(repo))
243 errx(1, "bar repository");
245 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
246 errx(1, "git_status_init_options: %s", giterr_last()->message);
248 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
249 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
250 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
251 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
253 if (git_status_list_new(&status, repo, &statusopts))
254 errx(1, "git_status_list_new: %s", giterr_last()->message);
256 for (i = 0; i < git_status_list_entrycount(status); i++) {
257 const git_status_entry *se;
259 se = git_status_byindex(status, i);
260 switch (se->status) {
261 case GIT_STATUS_WT_NEW:
262 printf("? %s\n", se->index_to_workdir->new_file.path);
263 break;
264 case GIT_STATUS_WT_MODIFIED:
265 printf("M %s\n", se->index_to_workdir->new_file.path);
266 break;
267 case GIT_STATUS_WT_DELETED:
268 printf("R %s\n", se->index_to_workdir->new_file.path);
269 break;
270 case GIT_STATUS_WT_RENAMED:
271 printf("m %s -> %s\n",
272 se->index_to_workdir->old_file.path,
273 se->index_to_workdir->new_file.path);
274 break;
275 case GIT_STATUS_WT_TYPECHANGE:
276 printf("t %s\n", se->index_to_workdir->new_file.path);
277 break;
278 case GIT_STATUS_INDEX_NEW:
279 printf("A %s\n", se->head_to_index->new_file.path);
280 break;
281 case GIT_STATUS_INDEX_MODIFIED:
282 printf("M %s\n", se->head_to_index->old_file.path);
283 break;
284 case GIT_STATUS_INDEX_DELETED:
285 printf("R %s\n", se->head_to_index->old_file.path);
286 break;
287 case GIT_STATUS_INDEX_RENAMED:
288 printf("m %s -> %s\n",
289 se->head_to_index->old_file.path,
290 se->head_to_index->new_file.path);
291 break;
292 case GIT_STATUS_INDEX_TYPECHANGE:
293 printf("t %s\n", se->head_to_index->old_file.path);
294 break;
295 case GIT_STATUS_CURRENT:
296 default:
297 break;
301 git_status_list_free(status);
302 git_repository_free(repo);
303 git_libgit2_shutdown();
305 return 0;
307 #endif