Skip to content

Commit 573285e

Browse files
Clément Poulaingitster
authored andcommitted
sha1_name: add get_sha1_with_context()
Textconv is defined by the diff driver, which is associated with a pathname, not a blob. This fonction permits to know the context for the sha1 you're looking for, especially his pathname Signed-off-by: Clément Poulain <[email protected]> Signed-off-by: Diane Gasselin <[email protected]> Signed-off-by: Axel Bonnet <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 37d29e1 commit 573285e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

cache.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,12 +730,23 @@ static inline unsigned int hexval(unsigned char c)
730730
#define MINIMUM_ABBREV 4
731731
#define DEFAULT_ABBREV 7
732732

733+
struct object_context {
734+
unsigned char tree[20];
735+
char path[PATH_MAX];
736+
unsigned mode;
737+
};
738+
733739
extern int get_sha1(const char *str, unsigned char *sha1);
734740
extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
735741
static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
736742
{
737743
return get_sha1_with_mode_1(str, sha1, mode, 1, NULL);
738744
}
745+
extern int get_sha1_with_context_1(const char *name, unsigned char *sha1, struct object_context *orc, int gently, const char *prefix);
746+
static inline int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
747+
{
748+
return get_sha1_with_context_1(str, sha1, orc, 1, NULL);
749+
}
739750
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
740751
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
741752
extern int read_ref(const char *filename, unsigned char *sha1);

sha1_name.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,8 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
933933
*/
934934
int get_sha1(const char *name, unsigned char *sha1)
935935
{
936-
unsigned unused;
937-
return get_sha1_with_mode(name, sha1, &unused);
936+
struct object_context unused;
937+
return get_sha1_with_context(name, sha1, &unused);
938938
}
939939

940940
/* Must be called only when object_name:filename doesn't exist. */
@@ -1031,12 +1031,24 @@ static void diagnose_invalid_index_path(int stage,
10311031

10321032

10331033
int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode, int gently, const char *prefix)
1034+
{
1035+
struct object_context oc;
1036+
int ret;
1037+
ret = get_sha1_with_context_1(name, sha1, &oc, gently, prefix);
1038+
*mode = oc.mode;
1039+
return ret;
1040+
}
1041+
1042+
int get_sha1_with_context_1(const char *name, unsigned char *sha1,
1043+
struct object_context *oc,
1044+
int gently, const char *prefix)
10341045
{
10351046
int ret, bracket_depth;
10361047
int namelen = strlen(name);
10371048
const char *cp;
10381049

1039-
*mode = S_IFINVALID;
1050+
memset(oc, 0, sizeof(*oc));
1051+
oc->mode = S_IFINVALID;
10401052
ret = get_sha1_1(name, namelen, sha1);
10411053
if (!ret)
10421054
return ret;
@@ -1059,6 +1071,11 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
10591071
cp = name + 3;
10601072
}
10611073
namelen = namelen - (cp - name);
1074+
1075+
strncpy(oc->path, cp,
1076+
sizeof(oc->path));
1077+
oc->path[sizeof(oc->path)-1] = '\0';
1078+
10621079
if (!active_cache)
10631080
read_cache();
10641081
pos = cache_name_pos(cp, namelen);
@@ -1071,7 +1088,6 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
10711088
break;
10721089
if (ce_stage(ce) == stage) {
10731090
hashcpy(sha1, ce->sha1);
1074-
*mode = ce->ce_mode;
10751091
return 0;
10761092
}
10771093
pos++;
@@ -1098,12 +1114,17 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
10981114
}
10991115
if (!get_sha1_1(name, cp-name, tree_sha1)) {
11001116
const char *filename = cp+1;
1101-
ret = get_tree_entry(tree_sha1, filename, sha1, mode);
1117+
ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
11021118
if (!gently) {
11031119
diagnose_invalid_sha1_path(prefix, filename,
11041120
tree_sha1, object_name);
11051121
free(object_name);
11061122
}
1123+
hashcpy(oc->tree, tree_sha1);
1124+
strncpy(oc->path, filename,
1125+
sizeof(oc->path));
1126+
oc->path[sizeof(oc->path)-1] = '\0';
1127+
11071128
return ret;
11081129
} else {
11091130
if (!gently)

0 commit comments

Comments
 (0)