git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/7] Git.pm: Introduce fast get_object() method

From
Petr Baudis <pasky@suse.cz>
Date
Jun 25, 2006, 01:54 UTC
Message-ID
<20060625015432.29906.31058.stgit@machine.or.cz>
In-Reply-To
<20060625015421.29906.50002.stgit@machine.or.cz>

Direct .xs routine. Note that it does not work 100% correctly when you juggle multiple repository objects, but it is not that bad either. The trouble is that we might reuse packs information for another Git project; that is not an issue since Git depends on uniqueness of SHA1 ids so if we have found the object somewhere else, it is nevertheless going to be the same object. It merely makes object existence detection through this method unreliable; it is duly noted in the documentation.

At least that's how I see it, I hope I didn't overlook any other potential problem. I tested it for memory leaks and it appears to be doing ok.

Signed-off-by: Petr Baudis <pasky@suse.cz>
---
 perl/Git.pm |   17 +++++++++++++++++
 perl/Git.xs |   24 ++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index c490e0c..cdae1fe 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -571,6 +571,23 @@ sub ident_person {
 }
 
 
+=item get_object ( TYPE, SHA1 )
+
+Return contents of the given object in a scalar string. If the object has
+not been found, undef is returned; however, do not rely on this! Currently,
+if you use multiple repositories at once, get_object() on one repository
+_might_ return the object even though it exists only in another repository.
+(But do not rely on this behaviour either.)
+
+The method must be called on a repository instance.
+
+Implementation of this method is very fast; no external command calls
+are involved. That's why it is broken, too. ;-)
+
+=cut
+
+# Implemented in Git.xs.
+
 
 =item hash_object ( TYPE, FILENAME )
 
diff --git a/perl/Git.xs b/perl/Git.xs
index 4e85d69..e841e4a 100644
--- a/perl/Git.xs
+++ b/perl/Git.xs
@@ -118,6 +118,30 @@ CODE:
 	free((char **) argv);
 }
 
+
+SV *
+xs_get_object(type, id)
+	char *type;
+	char *id;
+CODE:
+{
+	unsigned char sha1[20];
+	unsigned long size;
+	void *buf;
+
+	if (strlen(id) != 40 || get_sha1_hex(id, sha1) < 0)
+		XSRETURN_UNDEF;
+
+	buf = read_sha1_file(sha1, type, &size);
+	if (!buf)
+		XSRETURN_UNDEF;
+	RETVAL = newSVpvn(buf, size);
+	free(buf);
+}
+OUTPUT:
+	RETVAL
+
+
 char *
 xs_hash_object(type, file)
 	char *type;
Previous: Petr BaudisNext: Petr Baudis
Message 6 of 11 in “Git.pm: Introduce ident() and ident_person() methods”
  1. 1/7 Git.pm: Introduce ident() and ident_person() methodsPetr Baudis, Jun 25, 2006
  2. 2/7 Git.pm: Try to support ActiveState output pipePetr Baudis, Jun 25, 2006
  3. 3/7 Git.pm: Swap hash_object() parametersPetr Baudis, Jun 25, 2006
  4. 4/7 Git.pm: Fix Git->repository("/somewhere/totally/elsewhere")Petr Baudis, Jun 25, 2006
  5. 5/7 Make it possible to set up libgit directly (instead of from the environment)Petr Baudis, Jun 25, 2006
  6. 6/7 Git.pm: Introduce fast get_object() methodPetr Baudis, Jun 25, 2006
  7. 7/7 Convert git-annotate to use Git.pmPetr Baudis, Jun 25, 2006
  8. Ryan AndersonJun 25, 2006
  9. Petr BaudisJun 25, 2006
  10. Petr BaudisJun 25, 2006
  11. Junio C HamanoJun 25, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.