From: Linus Torvalds <torvalds@osdl.org>
To: Junio C Hamano <junkio@cox.net>, Git Mailing List <git@vger.kernel.org>
Subject: [RFC] get_sha1() shorthands for blob/tree objects
Date: Tue, 18 Apr 2006 16:45:16 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.64.0604181627101.3701@g5.osdl.org> (raw)
[ NOTE! The reason I put "RFC" in the subject rather than "PATCH" is that
I'm not 100% sure this isn't just a "shiny object" of mine rather than a
really useful thing to do. What do people think? Have you ever wanted to
access individual files in some random revision? Do you think this is
useful? I think it's cool and _may_ be useful, but I'm not going to
really push this patch. Consider it a throw-away patch unless somebody
else finds it intriguing enough.. ]
This is a fairly straightforward patch to allow "get_sha1()" to also have
shorthands for tree and blob objects.
The syntax is very simple and intuitive: you can specify a tree or a blob
by simply specifying <revision>:<path>, and get_sha1() will do the SHA1
lookup from the tree for you.
You can currently do it with "git ls-tree <rev> <path>" and parsing the
output, but that's actually pretty awkward.
With this, you can do something like
git cat-file blob v1.2.4:Makefile
to get the contents of "Makefile" at revision v1.2.4.
Now, this isn't necessarily something you really need all that often, but
the concept itself is actually pretty powerful. We could, for example,
allow things like
git diff v0.99.6:git-commit-script..v1.3.0:git-commit.sh
to see the difference between two arbitrary files in two arbitrary
revisions. To do that, the only thing we'd have to do is to make
git-diff-tree accept two blobs to diff, in addition to the two trees it
now expects.
[ IOW, don't get me wrong: the get_sha1() parsing is just the first step,
and does _not_ allow that "git diff" syntax to work yet. It parses the
object names fine, but git-diff-tree will currently exit with a "fatal:
unable to read source tree" error message because the objects aren't
tree objects ]
Linus
---
diff --git a/sha1_name.c b/sha1_name.c
index 4f92e12..0cd1139 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -3,6 +3,7 @@ #include "tag.h"
#include "commit.h"
#include "tree.h"
#include "blob.h"
+#include "diff.h"
static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
{
@@ -449,12 +450,76 @@ static int get_sha1_1(const char *name,
return get_short_sha1(name, len, sha1, 0);
}
+static int get_tree_entry(const unsigned char *, const char *, unsigned char *);
+
+static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result)
+{
+ int namelen = strlen(name);
+ while (t->size) {
+ const char *entry;
+ const unsigned char *sha1;
+ int entrylen, cmp;
+ unsigned mode;
+
+ sha1 = tree_entry_extract(t, &entry, &mode);
+ update_tree_entry(t);
+ entrylen = strlen(entry);
+ if (entrylen > namelen)
+ continue;
+ cmp = memcmp(name, entry, entrylen);
+ if (cmp > 0)
+ continue;
+ if (cmp < 0)
+ break;
+ if (entrylen == namelen) {
+ memcpy(result, sha1, 20);
+ return 0;
+ }
+ if (name[entrylen] != '/')
+ continue;
+ if (!S_ISDIR(mode))
+ break;
+ if (++entrylen == namelen) {
+ memcpy(result, sha1, 20);
+ return 0;
+ }
+ return get_tree_entry(sha1, name + entrylen, result);
+ }
+ return -1;
+}
+
+static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1)
+{
+ int retval;
+ void *tree;
+ struct tree_desc t;
+
+ tree = read_object_with_reference(tree_sha1, tree_type, &t.size, NULL);
+ if (!tree)
+ return -1;
+ t.buf = tree;
+ retval = find_tree_entry(&t, name, sha1);
+ free(tree);
+ return retval;
+}
+
/*
* This is like "get_sha1_basic()", except it allows "sha1 expressions",
* notably "xyz^" for "parent of xyz"
*/
int get_sha1(const char *name, unsigned char *sha1)
{
+ int ret;
+
prepare_alt_odb();
- return get_sha1_1(name, strlen(name), sha1);
+ ret = get_sha1_1(name, strlen(name), sha1);
+ if (ret < 0) {
+ const char *cp = strchr(name, ':');
+ if (cp) {
+ unsigned char tree_sha1[20];
+ if (!get_sha1_1(name, cp-name, tree_sha1))
+ return get_tree_entry(tree_sha1, cp+1, sha1);
+ }
+ }
+ return ret;
}
next reply other threads:[~2006-04-18 23:45 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-04-18 23:45 Linus Torvalds [this message]
2006-04-19 0:14 ` [RFC] get_sha1() shorthands for blob/tree objects Martin Langhoff
2006-04-19 0:21 ` Shawn Pearce
2006-04-19 1:20 ` Ray Lehtiniemi
2006-04-19 0:27 ` Junio C Hamano
2006-04-19 0:44 ` Linus Torvalds
2006-04-19 0:47 ` Linus Torvalds
2006-04-19 8:15 ` Andreas Ericsson
2006-04-19 14:44 ` Linus Torvalds
2006-04-19 0:56 ` Junio C Hamano
2006-04-19 1:16 ` Linus Torvalds
2006-04-19 1:19 ` Linus Torvalds
2006-04-19 1:30 ` Junio C Hamano
2006-04-19 1:43 ` Linus Torvalds
2006-04-19 4:02 ` Junio C Hamano
2006-04-19 4:14 ` Linus Torvalds
2006-04-19 21:49 ` Junio C Hamano
2006-04-19 21:57 ` Linus Torvalds
2006-04-19 3:51 ` Martin Langhoff
2006-04-19 3:58 ` Linus Torvalds
2006-04-19 4:04 ` Linus Torvalds
2006-04-22 0:49 ` [RFC] get_sha1(): :path and :[0-3]:path to extract from index Junio C Hamano
2006-04-25 8:37 ` Uwe Zeisberger
2006-04-25 8: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=Pine.LNX.4.64.0604181627101.3701@g5.osdl.org \
--to=torvalds@osdl.org \
--cc=git@vger.kernel.org \
--cc=junkio@cox.net \
/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).