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" <ebiederm@xmission.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric W. Biederman committed Oct 1, 2023 at 21:40 UTC 45b3b1214154975ac03c818838e3d12571f57d1e
3 files changed +23 -35
builtin/fast-import.c
+2 -16
@@ -1235,20 +1235,6 @@ static void *gfi_unpack_entry(
1235 return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep);
1236 }
1237
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 -
1238 static void load_tree(struct tree_entry *root)
1239 {
1240 struct object_id *oid = &root->versions[1].oid;
@@ -1286,7 +1272,7 @@ static void load_tree(struct tree_entry *root)
1272 t->entries[t->entry_count++] = e;
1273
1274 e->tree = NULL;
1289 - c = get_mode(c, &e->versions[1].mode);
1275 + c = parse_mode(c, &e->versions[1].mode);
1276 if (!c)
1277 die("Corrupt mode in %s", oid_to_hex(oid));
1278 e->versions[0].mode = e->versions[1].mode;
@@ -2275,7 +2261,7 @@ static void file_change_m(const char *p, struct branch *b)
2261 struct object_id oid;
2262 uint16_t mode, inline_data = 0;
2263
2278 - p = get_mode(p, &mode);
2264 + p = parse_mode(p, &mode);
2265 if (!p)
2266 die("Corrupt mode: %s", command_buf.buf);
2267 switch (mode) {
object.h
+18
@@ -190,6 +190,24 @@ void *create_object(struct repository *r, const struct object_id *oid, void *obj
190
191 void *object_as_type(struct object *obj, enum object_type type, int quiet);
192
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 +
211 /*
212 * Returns the object, having parsed it to find out what it is.
213 *
tree-walk.c
+3 -19
@@ -10,27 +10,11 @@
10 #include "pathspec.h"
11 #include "json-writer.h"
12
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 -
13 static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
14 {
15 const char *path;
33 - unsigned int mode, len;
16 + unsigned int len;
17 + uint16_t mode;
18 const unsigned hashsz = the_hash_algo->rawsz;
19
20 if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
@@ -38,7 +22,7 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned l
22 return -1;
23 }
24
41 - path = get_mode(buf, &mode);
25 + path = parse_mode(buf, &mode);
26 if (!path) {
27 strbuf_addstr(err, _("malformed mode in tree entry"));
28 return -1;