initial 'got cat' implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
diff --git a/got/got.1 b/got/got.1
index 3caa70a..5cc0297 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1084,6 +1084,27 @@ file instead of prompting interactively.
.It Cm ug
Short alias for
.Cm unstage .
+.It Cm cat Oo Fl r Ar repository-path Oc Ar object ...
+Parse and print contents of specified objects to standard output
+in a line-based text format.
+Treat each argument as a reference, a tag name, or an object ID SHA1 hash.
+References will be resolved to an object ID.
+Tag names will resolved to a tag object.
+An abbreviated hash argument will be expanded to a full SHA1 hash
+automatically, provided the abbreviation is unique.
+.Pp
+The options for
+.Cm got cat
+are as follows:
+.Bl -tag -width Ds
+.It Fl r Ar repository-path
+Use the repository at the specified path.
+If not specified, assume the repository is located at or above the current
+working directory.
+If this directory is a
+.Nm
+work tree, use the repository path associated with this work tree.
+.El
.El
.Sh ENVIRONMENT
.Bl -tag -width GOT_AUTHOR
diff --git a/got/got.c b/got/got.c
index 9062a44..0b3586f 100644
--- a/got/got.c
+++ b/got/got.c
@@ -98,6 +98,7 @@ __dead static void usage_rebase(void);
__dead static void usage_histedit(void);
__dead static void usage_stage(void);
__dead static void usage_unstage(void);
+__dead static void usage_cat(void);
static const struct got_error* cmd_init(int, char *[]);
static const struct got_error* cmd_import(int, char *[]);
@@ -120,6 +121,7 @@ static const struct got_error* cmd_rebase(int, char *[]);
static const struct got_error* cmd_histedit(int, char *[]);
static const struct got_error* cmd_stage(int, char *[]);
static const struct got_error* cmd_unstage(int, char *[]);
+static const struct got_error* cmd_cat(int, char *[]);
static struct got_cmd got_commands[] = {
{ "init", cmd_init, usage_init, "in" },
@@ -143,6 +145,7 @@ static struct got_cmd got_commands[] = {
{ "histedit", cmd_histedit, usage_histedit, "he" },
{ "stage", cmd_stage, usage_stage, "sg" },
{ "unstage", cmd_unstage, usage_unstage, "ug" },
+ { "cat", cmd_cat, usage_cat, "" },
};
static void
@@ -1905,7 +1908,8 @@ done:
static const struct got_error *
match_object_id(struct got_object_id **id, char **label,
- const char *id_str, int obj_type, struct got_repository *repo)
+ const char *id_str, int obj_type, int resolve_tags,
+ struct got_repository *repo)
{
const struct got_error *err;
struct got_tag_object *tag;
@@ -1914,18 +1918,25 @@ match_object_id(struct got_object_id **id, char **label,
*id = NULL;
*label = NULL;
- err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
- if (err == NULL) {
- *id = got_object_id_dup(got_object_tag_get_object_id(tag));
- if (*id == NULL)
- err = got_error_from_errno("got_object_id_dup");
- if (asprintf(label, "refs/tags/%s",
- got_object_tag_get_name(tag)) == -1)
- err = got_error_from_errno("asprintf");
- got_object_tag_close(tag);
- return err;
- } else if (err->code != GOT_ERR_NO_OBJ)
- return err;
+ if (resolve_tags) {
+ err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
+ repo);
+ if (err == NULL) {
+ *id = got_object_id_dup(
+ got_object_tag_get_object_id(tag));
+ if (*id == NULL)
+ err = got_error_from_errno("got_object_id_dup");
+ else if (asprintf(label, "refs/tags/%s",
+ got_object_tag_get_name(tag)) == -1) {
+ err = got_error_from_errno("asprintf");
+ free(id);
+ *id = NULL;
+ }
+ got_object_tag_close(tag);
+ return err;
+ } else if (err->code != GOT_ERR_NO_OBJ)
+ return err;
+ }
err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
if (err) {
@@ -2095,11 +2106,13 @@ cmd_diff(int argc, char *argv[])
goto done;
}
- error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
+ error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
+ repo);
if (error)
goto done;
- error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
+ error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
+ repo);
if (error)
goto done;
@@ -5949,3 +5962,274 @@ done:
free(cwd);
return error;
}
+
+__dead static void
+usage_cat(void)
+{
+ fprintf(stderr, "usage: %s cat [-r repository ] object1 "
+ "[object2 ...]\n", getprogname());
+ exit(1);
+}
+
+static const struct got_error *
+cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
+{
+ const struct got_error *err;
+ struct got_blob_object *blob;
+
+ err = got_object_open_as_blob(&blob, repo, id, 8192);
+ if (err)
+ return err;
+
+ err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
+ got_object_blob_close(blob);
+ return err;
+}
+
+static const struct got_error *
+cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
+{
+ const struct got_error *err;
+ struct got_tree_object *tree;
+ const struct got_tree_entries *entries;
+ struct got_tree_entry *te;
+
+ err = got_object_open_as_tree(&tree, repo, id);
+ if (err)
+ return err;
+
+ entries = got_object_tree_get_entries(tree);
+ te = SIMPLEQ_FIRST(&entries->head);
+ while (te) {
+ char *id_str;
+ if (sigint_received || sigpipe_received)
+ break;
+ err = got_object_id_str(&id_str, te->id);
+ if (err)
+ break;
+ fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
+ free(id_str);
+ te = SIMPLEQ_NEXT(te, entry);
+ }
+
+ got_object_tree_close(tree);
+ return err;
+}
+
+static const struct got_error *
+cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
+{
+ const struct got_error *err;
+ struct got_commit_object *commit;
+ const struct got_object_id_queue *parent_ids;
+ struct got_object_qid *pid;
+ char *id_str = NULL;
+ char *logmsg = NULL;
+ int i;
+
+ err = got_object_open_as_commit(&commit, repo, id);
+ if (err)
+ return err;
+
+ err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
+ if (err)
+ goto done;
+
+ fprintf(outfile, "tree: %s\n", id_str);
+ parent_ids = got_object_commit_get_parent_ids(commit);
+ fprintf(outfile, "parents: %d\n",
+ got_object_commit_get_nparents(commit));
+ i = 1;
+ SIMPLEQ_FOREACH(pid, parent_ids, entry) {
+ char *pid_str;
+ err = got_object_id_str(&pid_str, pid->id);
+ if (err)
+ goto done;
+ fprintf(outfile, "parent %d: %s\n", i++, pid_str);
+ free(pid_str);
+ }
+ fprintf(outfile, "author: %s\n",
+ got_object_commit_get_author(commit));
+ fprintf(outfile, "author-time: %lld\n",
+ got_object_commit_get_author_time(commit));
+
+ fprintf(outfile, "committer: %s\n",
+ got_object_commit_get_author(commit));
+ fprintf(outfile, "committer-time: %lld\n",
+ got_object_commit_get_committer_time(commit));
+
+ err = got_object_commit_get_logmsg(&logmsg, commit);
+ fprintf(outfile, "log-message: %zd bytes\n", strlen(logmsg));
+ fprintf(outfile, "%s", logmsg);
+done:
+ free(id_str);
+ free(logmsg);
+ got_object_commit_close(commit);
+ return err;
+}
+
+static const struct got_error *
+cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
+{
+ const struct got_error *err;
+ struct got_tag_object *tag;
+ char *id_str = NULL;
+ const char *tagmsg = NULL;
+
+ err = got_object_open_as_tag(&tag, repo, id);
+ if (err)
+ return err;
+
+ err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
+ if (err)
+ goto done;
+
+ fprintf(outfile, "tag-name: %s\n", got_object_tag_get_name(tag));
+ switch (got_object_tag_get_object_type(tag)) {
+ case GOT_OBJ_TYPE_BLOB:
+ fprintf(outfile, "tagged-object-type: blob\n");
+ break;
+ case GOT_OBJ_TYPE_TREE:
+ fprintf(outfile, "tagged-object-type: tree\n");
+ break;
+ case GOT_OBJ_TYPE_COMMIT:
+ fprintf(outfile, "tagged-object-type: commit\n");
+ break;
+ case GOT_OBJ_TYPE_TAG:
+ fprintf(outfile, "tagged-object-type: tag\n");
+ break;
+ default:
+ break;
+ }
+ fprintf(outfile, "tagged-object: %s\n", id_str);
+
+ fprintf(outfile, "tagger: %s\n",
+ got_object_tag_get_tagger(tag));
+ fprintf(outfile, "tagger-time: %lld %lld\n",
+ got_object_tag_get_tagger_time(tag),
+ got_object_tag_get_tagger_gmtoff(tag));
+
+ tagmsg = got_object_tag_get_message(tag);
+ fprintf(outfile, "tag-message: %zd bytes\n", strlen(tagmsg));
+ fprintf(outfile, "%s", tagmsg);
+done:
+ free(id_str);
+ got_object_tag_close(tag);
+ return err;
+}
+
+static const struct got_error *
+cmd_cat(int argc, char *argv[])
+{
+ const struct got_error *error;
+ struct got_repository *repo = NULL;
+ struct got_worktree *worktree = NULL;
+ char *cwd = NULL, *repo_path = NULL, *label = NULL;
+ struct got_object_id *id = NULL;
+ int ch, obj_type, i;
+
+#ifndef PROFILE
+ if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
+ NULL) == -1)
+ err(1, "pledge");
+#endif
+
+ while ((ch = getopt(argc, argv, "r:")) != -1) {
+ switch (ch) {
+ case 'r':
+ repo_path = realpath(optarg, NULL);
+ if (repo_path == NULL)
+ err(1, "-r option");
+ got_path_strip_trailing_slashes(repo_path);
+ break;
+ default:
+ usage_cat();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL) {
+ error = got_error_from_errno("getcwd");
+ goto done;
+ }
+ error = got_worktree_open(&worktree, cwd);
+ if (error && error->code != GOT_ERR_NOT_WORKTREE)
+ goto done;
+ if (worktree) {
+ if (repo_path == NULL) {
+ repo_path = strdup(
+ got_worktree_get_repo_path(worktree));
+ if (repo_path == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
+ }
+ }
+
+ if (repo_path == NULL) {
+ repo_path = getcwd(NULL, 0);
+ if (repo_path == NULL)
+ return got_error_from_errno("getcwd");
+ }
+
+ error = got_repo_open(&repo, repo_path);
+ free(repo_path);
+ if (error != NULL)
+ goto done;
+
+ error = apply_unveil(got_repo_get_path(repo), 1, NULL);
+ if (error)
+ goto done;
+
+ for (i = 0; i < argc; i++) {
+ error = match_object_id(&id, &label, argv[i],
+ GOT_OBJ_TYPE_ANY, 0, repo);
+ if (error)
+ break;
+
+ error = got_object_get_type(&obj_type, repo, id);
+ if (error)
+ break;
+
+ switch (obj_type) {
+ case GOT_OBJ_TYPE_BLOB:
+ error = cat_blob(id, repo, stdout);
+ break;
+ case GOT_OBJ_TYPE_TREE:
+ error = cat_tree(id, repo, stdout);
+ break;
+ case GOT_OBJ_TYPE_COMMIT:
+ error = cat_commit(id, repo, stdout);
+ break;
+ case GOT_OBJ_TYPE_TAG:
+ error = cat_tag(id, repo, stdout);
+ break;
+ default:
+ error = got_error(GOT_ERR_OBJ_TYPE);
+ break;
+ }
+ if (error)
+ break;
+ free(label);
+ label = NULL;
+ free(id);
+ id = NULL;
+ }
+
+done:
+ free(label);
+ free(id);
+ if (worktree)
+ got_worktree_close(worktree);
+ if (repo) {
+ const struct got_error *repo_error;
+ repo_error = got_repo_close(repo);
+ if (error == NULL)
+ error = repo_error;
+ }
+ return error;
+}
diff --git a/include/got_object.h b/include/got_object.h
index 5504da8..ceae59c 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -214,7 +214,7 @@ const struct got_error *got_object_blob_dump_to_file(size_t *, int *,
/*
* Attempt to open a tag object in a repository.
- * The caller must dispose of the tree with got_object_tag_close().
+ * The caller must dispose of the tree with got_tag_object_close().
*/
const struct got_error *got_object_open_as_tag(struct got_tag_object **,
struct got_repository *, struct got_object_id *);
@@ -234,5 +234,18 @@ int got_object_tag_get_object_type(struct got_tag_object *);
*/
struct got_object_id *got_object_tag_get_object_id(struct got_tag_object *);
+
+/* Get the timestamp of the tag. */
+time_t got_object_tag_get_tagger_time(struct got_tag_object *);
+
+/* Get the tag's timestamp's GMT offset. */
+time_t got_object_tag_get_tagger_gmtoff(struct got_tag_object *);
+
+/* Get the author of the tag. */
+const char *got_object_tag_get_tagger(struct got_tag_object *);
+
+/* Get the tag message associated with the tag. */
+const char *got_object_tag_get_message(struct got_tag_object *);
+
const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
const char *);
diff --git a/lib/object.c b/lib/object.c
index a47efb0..72be8b6 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1424,6 +1424,30 @@ got_object_tag_get_object_id(struct got_tag_object *tag)
return &tag->id;
}
+time_t
+got_object_tag_get_tagger_time(struct got_tag_object *tag)
+{
+ return tag->tagger_time;
+}
+
+time_t
+got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
+{
+ return tag->tagger_gmtoff;
+}
+
+const char *
+got_object_tag_get_tagger(struct got_tag_object *tag)
+{
+ return tag->tagger;
+}
+
+const char *
+got_object_tag_get_message(struct got_tag_object *tag)
+{
+ return tag->tagmsg;
+}
+
static struct got_tree_entry *
find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
{
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index c7f5765..010a6bb 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -1,5 +1,5 @@
REGRESS_TARGETS=checkout update status log add rm diff blame branch ref commit \
- revert cherrypick backout rebase import histedit stage unstage
+ revert cherrypick backout rebase import histedit stage unstage cat
NOOBJ=Yes
checkout:
@@ -59,4 +59,6 @@ stage:
unstage:
./unstage.sh
+cat:
+ ./cat.sh
.include <bsd.regress.mk>
diff --git a/regress/cmdline/cat.sh b/regress/cmdline/cat.sh
new file mode 100755
index 0000000..280f27a
--- /dev/null
+++ b/regress/cmdline/cat.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+function test_cat_basic {
+ local testroot=`test_init cat_basic`
+ local commit_id=`git_show_head $testroot/repo`
+ local author_time=`git_show_author_time $testroot/repo`
+ local alpha_id=`got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1`
+ local gamma_id=`got tree -r $testroot/repo -i | grep 'gamma/$' | cut -d' ' -f 1`
+ local delta_id=`got tree -r $testroot/repo -i gamma | grep 'delta$' | cut -d' ' -f 1`
+
+ # cat blob
+ echo "alpha" > $testroot/stdout.expected
+ got cat -r $testroot/repo $alpha_id > $testroot/stdout
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # cat tree
+ echo "$delta_id 0100644 delta" > $testroot/stdout.expected
+ got cat -r $testroot/repo $gamma_id > $testroot/stdout
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # cat commit
+ echo -n "tree: " > $testroot/stdout.expected
+ git_show_tree $testroot/repo >> $testroot/stdout.expected
+ echo >> $testroot/stdout.expected
+ echo "parents: 0" >> $testroot/stdout.expected
+ echo "author: $GOT_AUTHOR" >> $testroot/stdout.expected
+ echo "author-time: $author_time" >> $testroot/stdout.expected
+ echo "committer: Flan Hacker <flan_hacker@openbsd.org>" >> $testroot/stdout.expected
+ echo "committer-time: $author_time" >> $testroot/stdout.expected
+ echo "log-message: 22 bytes" >> $testroot/stdout.expected
+ printf "\nadding the test tree\n" >> $testroot/stdout.expected
+
+ got cat -r $testroot/repo $commit_id > $testroot/stdout
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # TODO: test cat tag
+
+ test_done "$testroot" "$ret"
+
+}
+
+run_test test_cat_basic
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index 5c3679e..346b80d 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -52,7 +52,8 @@ function git_show_head
function git_show_author_time
{
local repo="$1"
- (cd $repo && git show --no-patch --pretty='format:%at')
+ local object="$2"
+ (cd $repo && git show --no-patch --pretty='format:%at' $object)
}
function git_show_parent_commit