From 5119602a99f46aaa7b3bb2a0f5085b1df46b54ec Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 18 Apr 2006 16:45:16 -0700 Subject: [PATCH 1/4] get_sha1() shorthands for blob/tree objects 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 :, and get_sha1() will do the SHA1 lookup from the tree for you. You can currently do it with "git ls-tree " 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. Signed-off-by: Junio C Hamano --- sha1_name.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/sha1_name.c b/sha1_name.c index 4f92e12a8df..0cd1139e060 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -3,6 +3,7 @@ #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, int len, unsigned char *sha1) 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; } From 041a7308de3e6af36c5a6cc3412b542f42314f3f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 19 Apr 2006 11:56:07 -0700 Subject: [PATCH 2/4] sha1_name.c: prepare to make get_tree_entry() reusable from others. Signed-off-by: Junio C Hamano --- sha1_name.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/sha1_name.c b/sha1_name.c index 0cd1139e060..35e8dfb9c7b 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -450,18 +450,17 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1) return get_short_sha1(name, len, sha1, 0); } -static int get_tree_entry(const unsigned char *, const char *, unsigned char *); +static int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *); -static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result) +static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode) { 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); + sha1 = tree_entry_extract(t, &entry, mode); update_tree_entry(t); entrylen = strlen(entry); if (entrylen > namelen) @@ -477,18 +476,18 @@ static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char } if (name[entrylen] != '/') continue; - if (!S_ISDIR(mode)) + if (!S_ISDIR(*mode)) break; if (++entrylen == namelen) { memcpy(result, sha1, 20); return 0; } - return get_tree_entry(sha1, name + entrylen, result); + return get_tree_entry(sha1, name + entrylen, result, mode); } return -1; } -static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1) +static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode) { int retval; void *tree; @@ -498,7 +497,7 @@ static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsi if (!tree) return -1; t.buf = tree; - retval = find_tree_entry(&t, name, sha1); + retval = find_tree_entry(&t, name, sha1, mode); free(tree); return retval; } @@ -510,6 +509,7 @@ static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsi int get_sha1(const char *name, unsigned char *sha1) { int ret; + unsigned unused; prepare_alt_odb(); ret = get_sha1_1(name, strlen(name), sha1); @@ -518,7 +518,8 @@ int get_sha1(const char *name, unsigned char *sha1) 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 get_tree_entry(tree_sha1, cp+1, sha1, + &unused); } } return ret; From f3ab49db1b88b753fc628d7b241f20f73950e162 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 19 Apr 2006 11:56:53 -0700 Subject: [PATCH 3/4] sha1_name.c: no need to include diff.h; tree-walk.h will do. Signed-off-by: Junio C Hamano --- sha1_name.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sha1_name.c b/sha1_name.c index 35e8dfb9c7b..7ad20b59f98 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -3,7 +3,7 @@ #include "commit.h" #include "tree.h" #include "blob.h" -#include "diff.h" +#include "tree-walk.h" static int find_short_object_filename(int len, const char *name, unsigned char *sha1) { From 4dcff634e6294bcd7a40c8a51762b675b35be237 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 19 Apr 2006 14:05:47 -0700 Subject: [PATCH 4/4] get_tree_entry(): make it available from tree-walk Signed-off-by: Junio C Hamano --- sha1_name.c | 52 ---------------------------------------------------- tree-walk.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ tree-walk.h | 2 ++ 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/sha1_name.c b/sha1_name.c index 7ad20b59f98..345935bb2be 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -450,58 +450,6 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1) return get_short_sha1(name, len, sha1, 0); } -static int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *); - -static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode) -{ - int namelen = strlen(name); - while (t->size) { - const char *entry; - const unsigned char *sha1; - int entrylen, cmp; - - 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, mode); - } - return -1; -} - -static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode) -{ - 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, mode); - free(tree); - return retval; -} - /* * This is like "get_sha1_basic()", except it allows "sha1 expressions", * notably "xyz^" for "parent of xyz" diff --git a/tree-walk.c b/tree-walk.c index bf8bfdfdf85..9f7abb7cb35 100644 --- a/tree-walk.c +++ b/tree-walk.c @@ -115,3 +115,53 @@ void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callb free(entry); } +static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode) +{ + int namelen = strlen(name); + while (t->size) { + const char *entry; + const unsigned char *sha1; + int entrylen, cmp; + + 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, mode); + } + return -1; +} + +int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode) +{ + 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, mode); + free(tree); + return retval; +} + diff --git a/tree-walk.h b/tree-walk.h index 76893e36c32..47438fe1c08 100644 --- a/tree-walk.h +++ b/tree-walk.h @@ -22,4 +22,6 @@ typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback); +int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *); + #endif