Commit 8aa93786da2fe8d0c2714dcda5c6a0d4676b08c1

Stefan Sperling 2019-08-22T18:04:32

make 'got cat' output look more like raw object files

diff --git a/got/got.c b/got/got.c
index fac5ad8..9c6dc3c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -6025,7 +6025,6 @@ cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
 	struct got_object_qid *pid;
 	char *id_str = NULL;
 	const char *logmsg = NULL;
-	int i;
 
 	err = got_object_open_as_commit(&commit, repo, id);
 	if (err)
@@ -6035,31 +6034,28 @@ cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
 	if (err)
 		goto done;
 
-	fprintf(outfile, "tree: %s\n", id_str);
+	fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
 	parent_ids = got_object_commit_get_parent_ids(commit);
-	fprintf(outfile, "parents: %d\n",
+	fprintf(outfile, "numparents %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);
+		fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
 		free(pid_str);
 	}
-	fprintf(outfile, "author: %s\n",
-	    got_object_commit_get_author(commit));
-	fprintf(outfile, "author-time: %lld\n",
+	fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
+	    got_object_commit_get_author(commit),
 	    got_object_commit_get_author_time(commit));
 
-	fprintf(outfile, "committer: %s\n",
-	    got_object_commit_get_author(commit));
-	fprintf(outfile, "committer-time: %lld\n",
+	fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
+	    got_object_commit_get_author(commit),
 	    got_object_commit_get_committer_time(commit));
 
 	logmsg = got_object_commit_get_logmsg_raw(commit);
-	fprintf(outfile, "log-message: %zd bytes\n", strlen(logmsg));
+	fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
 	fprintf(outfile, "%s", logmsg);
 done:
 	free(id_str);
@@ -6083,33 +6079,36 @@ cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
 	if (err)
 		goto done;
 
-	fprintf(outfile, "tag-name: %s\n", got_object_tag_get_name(tag));
+	fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
+	    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");
+		fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
+		    GOT_OBJ_LABEL_BLOB);
 		break;
 	case GOT_OBJ_TYPE_TREE:
-		fprintf(outfile, "tagged-object-type: tree\n");
+		fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
+		    GOT_OBJ_LABEL_TREE);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
-		fprintf(outfile, "tagged-object-type: commit\n");
+		fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
+		    GOT_OBJ_LABEL_COMMIT);
 		break;
 	case GOT_OBJ_TYPE_TAG:
-		fprintf(outfile, "tagged-object-type: tag\n");
+		fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
+		    GOT_OBJ_LABEL_TAG);
 		break;
 	default:
 		break;
 	}
-	fprintf(outfile, "tagged-object: %s\n", id_str);
+	fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, 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));
+	fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
+	    got_object_tag_get_tagger(tag),
+	    got_object_tag_get_tagger_time(tag));
 
 	tagmsg = got_object_tag_get_message(tag);
-	fprintf(outfile, "tag-message: %zd bytes\n", strlen(tagmsg));
+	fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
 	fprintf(outfile, "%s", tagmsg);
 done:
 	free(id_str);
diff --git a/include/got_object.h b/include/got_object.h
index 37d13ae..b0bf900 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -57,6 +57,25 @@ void got_object_id_queue_free(struct got_object_id_queue *);
 #define GOT_OBJ_TYPE_OFFSET_DELTA	6
 #define GOT_OBJ_TYPE_REF_DELTA		7
 
+/*
+ * Labels used in object data.
+ */
+
+#define GOT_OBJ_LABEL_COMMIT	"commit"
+#define GOT_OBJ_LABEL_TREE	"tree"
+#define GOT_OBJ_LABEL_BLOB	"blob"
+#define GOT_OBJ_LABEL_TAG	"tag"
+
+#define GOT_COMMIT_LABEL_TREE		"tree "
+#define GOT_COMMIT_LABEL_PARENT		"parent "
+#define GOT_COMMIT_LABEL_AUTHOR		"author "
+#define GOT_COMMIT_LABEL_COMMITTER	"committer "
+
+#define GOT_TAG_LABEL_OBJECT		"object "
+#define GOT_TAG_LABEL_TYPE		"type "
+#define GOT_TAG_LABEL_TAG		"tag "
+#define GOT_TAG_LABEL_TAGGER		"tagger "
+
 struct got_repository;
 
 /*
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index a69c317..f09d092 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -75,21 +75,6 @@ struct got_tag_object {
 	int refcnt;		/* > 0 if open and/or cached */
 };
 
-#define GOT_OBJ_LABEL_COMMIT	"commit"
-#define GOT_OBJ_LABEL_TREE	"tree"
-#define GOT_OBJ_LABEL_BLOB	"blob"
-#define GOT_OBJ_LABEL_TAG	"tag"
-
-#define GOT_COMMIT_LABEL_TREE		"tree "
-#define GOT_COMMIT_LABEL_PARENT		"parent "
-#define GOT_COMMIT_LABEL_AUTHOR		"author "
-#define GOT_COMMIT_LABEL_COMMITTER	"committer "
-
-#define GOT_TAG_LABEL_OBJECT		"object "
-#define GOT_TAG_LABEL_TYPE		"type "
-#define GOT_TAG_LABEL_TAG		"tag "
-#define GOT_TAG_LABEL_TAGGER		"tagger "
-
 struct got_object_id *got_object_get_id(struct got_object *);
 const struct got_error *got_object_get_id_str(char **, struct got_object *);
 const struct got_error *got_object_get_path(char **, struct got_object_id *,
diff --git a/regress/cmdline/cat.sh b/regress/cmdline/cat.sh
index 280f27a..ff28334 100755
--- a/regress/cmdline/cat.sh
+++ b/regress/cmdline/cat.sh
@@ -47,15 +47,13 @@ function test_cat_basic {
 	fi
 
 	# cat commit
-	echo -n "tree: " > $testroot/stdout.expected
+	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
+	echo "numparents 0" >> $testroot/stdout.expected
+	echo "author $GOT_AUTHOR $author_time +0000" >> $testroot/stdout.expected
+	echo "committer Flan Hacker <flan_hacker@openbsd.org> $author_time +0000" >> $testroot/stdout.expected
+	echo "messagelen 22" >> $testroot/stdout.expected
 	printf "\nadding the test tree\n" >> $testroot/stdout.expected
 
 	got cat -r $testroot/repo $commit_id > $testroot/stdout