git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Han-Wen Nienhuys <hanwen@xs4all.nl>
To: git@vger.kernel.org
Subject: [PATCH] Add builtin dump command, to query a repository using a pipe.
Date: Fri, 25 Jan 2008 16:24:34 -0800	[thread overview]
Message-ID: <fnduk3$d0c$1@ger.gmane.org> (raw)


This command prints either contents or metadata on stdout for SHA1s or
symbolic object names supplied to its stdin, so you can extract many
pieces of data of the repository using a single subprocess.

Documentation will follow once the code is approved.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
---
 Makefile       |    1 +
 builtin-dump.c |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 builtin.h      |    1 +
 git.c          |    1 +
 4 files changed, 103 insertions(+), 0 deletions(-)
 create mode 100644 builtin-dump.c

diff --git a/Makefile b/Makefile
index 21c80e6..63a26a7 100644
--- a/Makefile
+++ b/Makefile
@@ -339,6 +339,7 @@ BUILTIN_OBJS = \
 	builtin-diff-files.o \
 	builtin-diff-index.o \
 	builtin-diff-tree.o \
+	builtin-dump.o \
 	builtin-fast-export.o \
 	builtin-fetch.o \
 	builtin-fetch-pack.o \
diff --git a/builtin-dump.c b/builtin-dump.c
new file mode 100644
index 0000000..813c7ab
--- /dev/null
+++ b/builtin-dump.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2008 Google Inc.
+ * Author: hanwen@google.com
+ * 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "cache.h"
+#include "exec_cmd.h"
+#include "object.h"
+#include "builtin.h"
+
+/*
+  format:
+
+  'show' SP type SP objname LF
+  =>
+  'len' number LF
+  contents LF
+    
+  'metadata' SP objname LF
+  =>
+  type SP size SP sha1 LF
+  
+  for non-existent objects, 'metadata' prints
+
+  'none' SP 0 SP null-hash LF
+  
+*/
+
+typedef unsigned char sha1_t[20];
+
+void print_metadata(struct strbuf command_buf) {
+	char *obj_name = strchr(command_buf.buf, ' ') + 1;
+	sha1_t sha1 = {0};
+	const char *type_name = "none";
+	unsigned long size = 0;
+	char header[200];
+
+	if (!get_sha1(obj_name, sha1)) {
+		enum object_type type = sha1_object_info(sha1, &size);
+		type_name = type ? typename(type) : "none";
+	}
+	sprintf(header, "%s %lu %s\n", type_name, size, sha1_to_hex(sha1));
+	write_or_die(1, header, strlen(header));  
+}
+
+
+void print_show(struct strbuf command_buf) {
+	char *type = strchr(command_buf.buf, ' ') + 1;
+	char *obj_name;
+	sha1_t sha1;
+	unsigned long size;
+	char header[200];
+	void *buf;
+	
+	if (!type)
+		die("commmand format error: %s\n", command_buf.buf);
+  
+	obj_name = strchr(type, ' ');
+	if (!obj_name)
+		die("commmand format error: %s\n", command_buf.buf);
+	*obj_name = '\0';
+	obj_name++;
+  
+	if (get_sha1(obj_name, sha1))
+		die("not a valid object name: %s", obj_name);
+  
+	buf = read_object_with_reference(sha1, type, &size, NULL);
+	sprintf(header, "len %lu\n", size);
+	write_or_die(1, header, strlen(header));
+	write_or_die(1, buf, size);
+	write_or_die(1, "\n", 1);
+}
+
+
+int cmd_dump(int argc, const char **argv, const char *prefix) {
+	struct strbuf command_buf = STRBUF_INIT;
+	while (strbuf_getline(&command_buf, stdin, '\n') != EOF) {
+		if (!prefixcmp(command_buf.buf, "metadata "))
+			print_metadata(command_buf);
+		else if (!prefixcmp(command_buf.buf, "print "))
+			print_show(command_buf);
+		else
+			die("unknown command %s", command_buf.buf);
+	}
+	return 0;
+}
diff --git a/builtin.h b/builtin.h
index cb675c4..df873f9 100644
--- a/builtin.h
+++ b/builtin.h
@@ -33,6 +33,7 @@ extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
 extern int cmd_diff(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
+extern int cmd_dump(int argc, const char **argv, const char *prefix);
 extern int cmd_fast_export(int argc, const char **argv, const char *prefix);
 extern int cmd_fetch(int argc, const char **argv, const char *prefix);
 extern int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
diff --git a/git.c b/git.c
index 15fec89..853dbc1 100644
--- a/git.c
+++ b/git.c
@@ -303,6 +303,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "diff-files", cmd_diff_files },
 		{ "diff-index", cmd_diff_index, RUN_SETUP },
 		{ "diff-tree", cmd_diff_tree, RUN_SETUP },
+		{ "dump", cmd_dump, RUN_SETUP },
 		{ "fast-export", cmd_fast_export, RUN_SETUP },
 		{ "fetch", cmd_fetch, RUN_SETUP },
 		{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
-- 
1.5.3.5.643.g40e25

             reply	other threads:[~2008-01-26  0:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-26  0:24 Han-Wen Nienhuys [this message]
2008-01-26  9:35 ` [PATCH] Add builtin dump command, to query a repository using a pipe Junio C Hamano
2008-01-26  9:46   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='fnduk3$d0c$1@ger.gmane.org' \
    --to=hanwen@xs4all.nl \
    --cc=git@vger.kernel.org \
    --cc=hanwen@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).