Skip to content

Commit 45b3b12

Browse files
ebiedermgitster
authored andcommitted
object: factor out parse_mode out of fast-import and tree-walk into in object.h
builtin/fast-import.c and tree-walk.c have almost identical version of get_mode. The two functions started out the same but have diverged slightly. The version in fast-import changed mode to a uint16_t to save memory. The version in tree-walk started erroring if no mode was present. As far as I can tell both of these changes are valid for both of the callers, so add the both changes and place the common parsing helper in object.h Rename the helper from get_mode to parse_mode so it does not conflict with another helper named get_mode in diff-no-index.c This will be used shortly in a new helper decode_tree_entry_raw which is used to compute cmpatibility objects as part of the sha256 transition. Signed-off-by: "Eric W. Biederman" <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 095261a commit 45b3b12

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

builtin/fast-import.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,20 +1235,6 @@ static void *gfi_unpack_entry(
12351235
return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep);
12361236
}
12371237

1238-
static const char *get_mode(const char *str, uint16_t *modep)
1239-
{
1240-
unsigned char c;
1241-
uint16_t mode = 0;
1242-
1243-
while ((c = *str++) != ' ') {
1244-
if (c < '0' || c > '7')
1245-
return NULL;
1246-
mode = (mode << 3) + (c - '0');
1247-
}
1248-
*modep = mode;
1249-
return str;
1250-
}
1251-
12521238
static void load_tree(struct tree_entry *root)
12531239
{
12541240
struct object_id *oid = &root->versions[1].oid;
@@ -1286,7 +1272,7 @@ static void load_tree(struct tree_entry *root)
12861272
t->entries[t->entry_count++] = e;
12871273

12881274
e->tree = NULL;
1289-
c = get_mode(c, &e->versions[1].mode);
1275+
c = parse_mode(c, &e->versions[1].mode);
12901276
if (!c)
12911277
die("Corrupt mode in %s", oid_to_hex(oid));
12921278
e->versions[0].mode = e->versions[1].mode;
@@ -2275,7 +2261,7 @@ static void file_change_m(const char *p, struct branch *b)
22752261
struct object_id oid;
22762262
uint16_t mode, inline_data = 0;
22772263

2278-
p = get_mode(p, &mode);
2264+
p = parse_mode(p, &mode);
22792265
if (!p)
22802266
die("Corrupt mode: %s", command_buf.buf);
22812267
switch (mode) {

object.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ void *create_object(struct repository *r, const struct object_id *oid, void *obj
190190

191191
void *object_as_type(struct object *obj, enum object_type type, int quiet);
192192

193+
194+
static inline const char *parse_mode(const char *str, uint16_t *modep)
195+
{
196+
unsigned char c;
197+
unsigned int mode = 0;
198+
199+
if (*str == ' ')
200+
return NULL;
201+
202+
while ((c = *str++) != ' ') {
203+
if (c < '0' || c > '7')
204+
return NULL;
205+
mode = (mode << 3) + (c - '0');
206+
}
207+
*modep = mode;
208+
return str;
209+
}
210+
193211
/*
194212
* Returns the object, having parsed it to find out what it is.
195213
*

tree-walk.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,19 @@
1010
#include "pathspec.h"
1111
#include "json-writer.h"
1212

13-
static const char *get_mode(const char *str, unsigned int *modep)
14-
{
15-
unsigned char c;
16-
unsigned int mode = 0;
17-
18-
if (*str == ' ')
19-
return NULL;
20-
21-
while ((c = *str++) != ' ') {
22-
if (c < '0' || c > '7')
23-
return NULL;
24-
mode = (mode << 3) + (c - '0');
25-
}
26-
*modep = mode;
27-
return str;
28-
}
29-
3013
static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
3114
{
3215
const char *path;
33-
unsigned int mode, len;
16+
unsigned int len;
17+
uint16_t mode;
3418
const unsigned hashsz = the_hash_algo->rawsz;
3519

3620
if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
3721
strbuf_addstr(err, _("too-short tree object"));
3822
return -1;
3923
}
4024

41-
path = get_mode(buf, &mode);
25+
path = parse_mode(buf, &mode);
4226
if (!path) {
4327
strbuf_addstr(err, _("malformed mode in tree entry"));
4428
return -1;

0 commit comments

Comments
 (0)