Proposed patch for mktree [0/3]
Here is a series of 3 patches to git-mktree in addition to the recent work done on pu.
The first is just small fixes after the work on the '--missing' option. The second patch adds a '--batch' option to mktree (similar to '--batch' in cat-file) that allows multiple tree objects to be created interactively by a single process.
Finally there are some improvements to the validation of the type of object identified by the tree entry sha1 (previously the sha1 was only used to check for existence, not type).
-josh
Josh Micich (3):
--missing option for mktree: re-added strbuf_release(&p_uq), Updated man page added --batch option to mk-tree
improved validation of entry type in mktree
Documentation/git-mktree.txt | 16 ++++++++-- builtin-mktree.c | 63 +++++++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 19 deletions(-)
PATCH 1/3 '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man page
Re-added call to strbuf_release(&p_uq) that got lost in earlier changes. Updated mktree_usage msg.
Updated man page to explain new '--missing' option. Also clarified sorting behaviour.
--- Documentation/git-mktree.txt | 11 ++++++++--- builtin-mktree.c | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/git-mktree.txt +8 -3
@@ -8,18 +8,23 @@ git-mktree - Build a tree-object from ls-tree formatted text SYNOPSIS -------- -'git mktree' [-z] +'git mktree' [-z] [--missing] DESCRIPTION ----------- -Reads standard input in non-recursive `ls-tree` output format, -and creates a tree object. The object name of the tree object +Reads standard input in non-recursive `ls-tree` output format, and creates +a tree object. The order of the tree entries is normalised by mktree so +pre-sorting the input is not required. The object name of the tree object built is written to the standard output. OPTIONS ------- -z:: Read the NUL-terminated `ls-tree -z` output instead. +--missing:: + Allow missing objects. The default behaviour (without this option) + is to verify that each tree entry's sha1 identifies an existing + object. Author ------
builtin-mktree.c +1 -1
@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1) }static const char *mktree_usage[] = { -"git mktree [-z]" , +"git mktree [-z] [--missing]" ,NULL };
@@ -112,6 +112,9 @@ static void mktree_line(char *buf, size_t len, int
line_termination, int allow_m
if (unquote_c_style(&p_uq, path, NULL))
die("invalid quoting");
path = strbuf_detach(&p_uq, NULL);
+ append_to_tree(mode, sha1, path);
+ strbuf_release(&p_uq);
+ return;
}
append_to_tree(mode, sha1, path);
}
Signature · 1 line
1.6.3.165.g2cce5.dirty
Re: [PATCH 1/3] '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man page
Documentation/git-mktree.txt git-mktree - Build a tree-object from ls-tree formatted text · line 18
Quoted 8 lines · Josh Micich
DESCRIPTION
-----------Documentation/git-mktree.txt +3
-2-Reads standard input in non-recursive `ls-tree` output format, -and creates a tree object. The object name of the tree object +Reads standard input in non-recursive `ls-tree` output format, and creates +a tree object. The order of the tree entries is normalised by mktree so +pre-sorting the input is not required. The object name of the tree object built is written to the standard output.
Thanks ;-)
Documentation/git-mktree.txt git-mktree - Build a tree-object from ls-tree formatted text · line 27
Quoted 8 lines
OPTIONS
-------
-z::Documentation/git-mktree.txt +4
-0Read the NUL-terminated `ls-tree -z` output instead. +--missing:: + Allow missing objects. The default behaviour (without this option) + is to verify that each tree entry's sha1 identifies an existing + object.
I'd suggest adding "... except for gitlink entries (aka "submodules") that are always allowed to be missing" here.
Review
Quoted 11 lines
(quoted diff) +0
-0@@ -112,6 +112,9 @@ static void mktree_line(char *buf, size_t len, int
line_termination, intallow_m(quoted diff) +3
-0if (unquote_c_style(&p_uq, path, NULL)) die("invalid quoting"); path = strbuf_detach(&p_uq, NULL); + append_to_tree(mode, sha1, path); + strbuf_release(&p_uq); + return; } append_to_tree(mode, sha1, path); }
Ehh, why? detach already detaches the allocated buffer from strbuf and there is nothing to clean up by strbuf_release().
Re: [PATCH 1/3] '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man page
Quoted 11 lines · Junio C Hamano
Quoted 7 lines
path =
strbuf_detach(&p_uq, NULL);
+append_to_tree(mode, sha1, path);
+strbuf_release(&p_uq);
+ return;
}
append_to_tree(mode, sha1, path);
}Ehh, why? detach already detaches the allocated buffer from strbuf and there is nothing to clean up by
strbuf_release().
My bad. While re-basing my work to fe0bb5, the strbuf_release() call went missing, but I didn't notice you had also changed:
- path = p_uq.buf;
+ path = strbuf_detach(&p_uq, NULL);
That is much nicer
PATCH 2/3 added --batch option to mktree
In reply to Josh MicichThis option enables creation of many tree objects with a single process
--- Documentation/git-mktree.txt | 7 ++++++- builtin-mktree.c | 36 +++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 8 deletions(-)
Documentation/git-mktree.txt +6 -1
@@ -8,7 +8,7 @@ git-mktree - Build a tree-object from ls-tree formatted text SYNOPSIS -------- -'git mktree' [-z] [--missing] +'git mktree' [-z] [--missing] [--batch] DESCRIPTION -----------
@@ -25,6 +25,11 @@ OPTIONS Allow missing objects. The default behaviour (without this option) is to verify that each tree entry's sha1 identifies an existing object. +--batch:: + Allow building of more than one tree object before exiting. Each + tree is separated by as single blank line. The final new-line is + optional. Note - if the '-z' option is used, lines are terminated + with NUL. Author ------
builtin-mktree.c +1 -1
@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1) }static const char *mktree_usage[] = { -"git mktree [-z] [--missing]" , +"git mktree [-z] [--missing] [--batch]" ,NULL };
@@ -125,20 +125,42 @@ int cmd_mktree(int ac, const char **av, const char
*prefix)
unsigned char sha1[20];
int line_termination = '\n';
int allow_missing = 0;
+ int is_batch_mode = 0;
+
const struct option option[] = {
OPT_SET_INT('z', NULL, &line_termination, "input is NUL
terminated", '\0'),
OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing
objects", 1),
+ OPT_SET_INT( 0 , "batch", &is_batch_mode, "interactively create
more than one tree", 1),
OPT_END()
};
ac = parse_options(ac, av, option, mktree_usage, 0);
- while (strbuf_getline(&sb, stdin, line_termination) != EOF)
- mktree_line(sb.buf, sb.len, line_termination, allow_missing);
-
+ int got_eof = 0;
+ while (!got_eof) {
+ while (1) {
+ if (strbuf_getline(&sb, stdin, line_termination) ==
EOF) {
+ got_eof = 1;
+ break;
+ }
+ if (sb.buf[0] == '\0') {
+ // empty lines denote tree boundaries in batch
mode
+ if (is_batch_mode) {
+ break;
+ }
+ die("input format error: (blank line only valid
in batch mode)");
+ }
+ mktree_line(sb.buf, sb.len, line_termination,allow_missing);
+ }
+ if (is_batch_mode && got_eof && used < 1) {
+ // allow input to finish with a new-line (or not)
+ } else {
+ write_tree(sha1);
+ puts(sha1_to_hex(sha1));
+ fflush(stdout);
+ }
+ used=0; // reset tree entry buffer for re-use in batch mode
+ }
strbuf_release(&sb);
-
- write_tree(sha1);
- puts(sha1_to_hex(sha1));
exit(0);
}
Signature · 1 line
1.6.3.165.g2cce5.dirty
Re: [PATCH 2/3] added --batch option to mktree
Quoted 1 line · Josh Micich
This option enables creation of many tree objects with a single process
... which is desirable in what way? how does this justify the cost of maintenance? what is it used for?
Documentation/git-mktree.txt OPTIONS · line 32
Quoted 11 lines
DESCRIPTION
-----------Documentation/git-mktree.txt +5
-0@@ -25,6 +25,11 @@ OPTIONS Allow missing objects. The default behaviour (without this option) is to verify that each tree entry's sha1 identifies an existing object. +--batch:: + Allow building of more than one tree object before exiting. Each + tree is separated by as single blank line. The final new-line is + optional. Note - if the '-z' option is used, lines are terminated + with NUL.
I think you want an blank line before this.
Quoted 16 lines
(quoted diff) +0
-0Author ------builtin-mktree.c +1
-1@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1) }static const char *mktree_usage[] = { -"git mktree [-z] [--missing]" , +"git mktree [-z] [--missing] [--batch]" ,NULL };
builtin-mktree.c int cmd_mktree(int ac, const char **av, const char · line 125
Quoted 1 line
builtin-mktree.c +0
-0@@ -125,20 +125,42 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
Linewrapped and would not apply.
builtin-mktree.c int cmd_mktree(int ac, const char **av, const char · line 137
Quoted 12 lines
builtin-mktree.c +3
-0unsigned char sha1[20]; int line_termination = '\n'; int allow_missing = 0; + int is_batch_mode = 0; + const struct option option[] = { OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'), OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1), + OPT_SET_INT( 0 , "batch", &is_batch_mode, "interactively create more than one tree", 1),
What do you mean by "interactively"? Does anybody type from the standard input?
builtin-mktree.c int cmd_mktree(int ac, const char **av, const char · line 140
Quoted 6 lines
builtin-mktree.c +1
-3ac =parse_options (ac, av, option, mktree_usage,0 ); -while (strbuf_getline (&sb, stdin, line_termination) !=EOF ) -mktree_line (sb.buf, sb.len, line_termination, allow_missing); - +int got_eof =0 ;
Decl-after-statement.
Quoted 10 lines
builtin-mktree.c +8
-0+while (!got_eof) { +while (1 ) { +if (strbuf_getline (&sb, stdin, line_termination) ==EOF ) { + got_eof =1 ; +break ; + } +if (sb.buf[0 ] =='\0' ) { +// empty lines denote tree boundaries in batch mode
No C++ comment please;
builtin-mktree.c
Quoted 3 lines
builtin-mktree.c +3
-0+if (is_batch_mode) { +break ; + }
Lose excess {} pair;
builtin-mktree.c
Quoted 2 lines
builtin-mktree.c +2
-0+if (is_batch_mode && got_eof && used <1 ) { +// allow input to finish with a new-line (or not)
Style: have an explicit ";" for an empty statement.
But more importantly, what does this comment mean? Why do you want to be loose in input format validation?
builtin-mktree.c
Quoted 6 lines
builtin-mktree.c +6
-0+ }else { +write_tree (sha1); +puts (sha1_to_hex (sha1)); +fflush (stdout); + } + used=0 ;// reset tree entry buffer for re-use in batch mode
used = 0; /* .... */
Re: [PATCH 2/3] added --batch option to mktree
In reply to a message not in the archiveQuoted 6 lines · Josh Micich
Quoted 4 lines · Junio C Hamano
Quoted 1 line
This option enables creation of many tree objects with a single process
... which is desirable in what way? how does this justify the cost of maintenance? what is it used for?
Quoted 7 lines
I have been writing a few of my own utilities on top of basic git commands and got mild performance gains from using the existing '--batch' option with "git cat-file" instead of executing new "git cat-file -p <sha>" processes for each object I wish to read. Later I was writing a utility for mirroring another SCM (creates many trees), and I faced a similar performance issue with "git mktree". After adding the '--batch' option, I experienced about a 30% speed increase.
"An operation that some people may want to do but the current toolset is not very good at is _this_, and this patch rectifies that. _Here_ is a number to back up that claim." is a perfect way to justify your new feature. Why not have that in the proposed commit log message ;-)?
Quoted 11 lines
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (is_batch_mode && got_eof && used < 1) { + // allow input to finish with a new-line (or not)Style: have an explicit ";" for an empty statement.
But more importantly, what does this comment mean? Why do you want to be loose in input format validation?
I agree with your implied suggestion that tight input validation is better. I was actually trying to keep consistent with the way I believe mktree works today. The final \n is optional as far as I can tell.
Ok.
Re: [PATCH 2/3] added --batch option to mktree
In reply to Junio C HamanoQuoted 4 lines · Junio C Hamano
Quoted 1 line
This option enables creation of many tree objects with a single process
... which is desirable in what way? how does this justify the cost of maintenance? what is it used for?
I have been writing a few of my own utilities on top of basic git commands and got mild performance gains from using the existing '--batch' option with "git cat-file" instead of executing new "git cat-file -p <sha>" processes for each object I wish to read. Later I was writing a utility for mirroring another SCM (creates many trees), and I faced a similar performance issue with "git mktree". After adding the '--batch' option, I experienced about a 30% speed increase.
If you find this logic persuasive, I can fix all the other issues and re-submit this patch.
Quoted 2 lines
Quoted 1 line
+--batch::
I think you want an blank line before this.
right
Quoted 4 lines
Review int cmd_mktree(int ac, const char **av, const char · line 125
Quoted 2 lines
(quoted diff) +0
-0@@ -125,20 +125,42 @@ int cmd_mktree(int ac, const char **av, const char *prefix)Linewrapped and would not apply.
sorry - gmane web interface did that. BTW - does git have a coding standard for maximum line length?
Quoted 2 lines
What do you mean by "interactively"? Does anybody type from the standard input?
I guess I was trying to stress the point that the created tree object ids are output immediately as the complete text for each tree is received. Other (bad) applications I have seen wait for EOF on stdin before sending anything to stdout, and this is exactly what I had avoided in the impl. Perhaps you could suggest better phrasing.
Quoted 5 lines
Decl-after-statement.
No C++ comment please;
Lose excess {} pair;
Style: have an explicit ";" for an empty statement. used = 0; /* .... */
sorry - bad habits. gotta work out how to get my compiler to warn me about these
Quoted 7 lines
Review int cmd_mktree(int ac, const char **av, const char · line 127
Quoted 2 lines
(quoted diff) +2
-0+ if (is_batch_mode && got_eof && used < 1) { + // allow input to finish with a new-line (or not)Style: have an explicit ";" for an empty statement.
But more importantly, what does this comment mean? Why do you want to be loose in input format validation?
I agree with your implied suggestion that tight input validation is better. I was actually trying to keep consistent with the way I believe mktree works today. The final \n is optional as far as I can tell.
Re: [PATCH 2/3] added --batch option to mktree
Heya,
Quoted 2 lines · Josh Micich
If you find this logic persuasive, I can fix all the other issues and re-submit this patch.
I suspect Junio's point was more in the gist of "you should explain this in your commit message". So if you do resend your patch, include the above explanation (or at least the mention of the 30% speedup) in your commit message.
Signature · 3 lines
Cheers, Sverre Rabbelier
Re: [PATCH 2/3] added --batch option to mktree
This option works in a similar way to the '--batch' option of 'git cat-file'. It enables creation of many tree objects with a single process.
The change was motivated by performance considerations in applications that need to create many tree objects. A non-rigorous test showed tree creation times improved from (roughly) 200ms to 50ms.
--- Documentation/git-mktree.txt | 8 +++++++- builtin-mktree.c | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 8 deletions(-)
Documentation/git-mktree.txt +7 -1
@@ -8,7 +8,7 @@ git-mktree - Build a tree-object from ls-tree formatted text SYNOPSIS -------- -'git mktree' [-z] [--missing] +'git mktree' [-z] [--missing] [--batch] DESCRIPTION -----------
@@ -28,6 +28,12 @@ OPTIONS object. This option has no effect on the treatment of gitlink entries (aka "submodules") which are always allowed to be missing. +--batch:: + Allow building of more than one tree object before exiting. Each + tree is separated by as single blank line. The final new-line is + optional. Note - if the '-z' option is used, lines are terminated + with NUL. + Author ------ Written by Junio C Hamano <[email protected]>
builtin-mktree.c +1 -1
@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1) }static const char *mktree_usage[] = { -"git mktree [-z] [--missing]" , +"git mktree [-z] [--missing] [--batch]" ,NULL };
@@ -122,20 +122,46 @@ int cmd_mktree(int ac, const char **av, const
char *prefix)
unsigned char sha1[20];
int line_termination = '\n';
int allow_missing = 0;
+ int is_batch_mode = 0;
+ int got_eof = 0;
+
const struct option option[] = {
OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'), OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1),
+ OPT_SET_INT( 0 , "batch", &is_batch_mode, "allow creation of more
than one tree", 1),
OPT_END()
};
ac = parse_options(ac, av, option, mktree_usage, 0);
- while (strbuf_getline(&sb, stdin, line_termination) != EOF)
- mktree_line(sb.buf, sb.len, line_termination, allow_missing);
-
+ while (!got_eof) {
+ while (1) {
+ if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
+ got_eof = 1;
+ break;
+ }
+ if (sb.buf[0] == '\0') {
+ /* empty lines denote tree boundaries in batch mode */
+ if (is_batch_mode)
+ break;
+ die("input format error: (blank line only valid in batch mode)");
+ }
+ mktree_line(sb.buf, sb.len, line_termination, allow_missing);
+ }
+ if (is_batch_mode && got_eof && used < 1) {
+ /*
+ * Execution gets here if the last tree entry is terminated with a
+ * new-line. The final new-line has been made optional to be
+ * consistent with the original non-batch behaviour of mktree.
+ */
+ ; /* skip creating an empty tree */
+ } else {
+ write_tree(sha1);
+ puts(sha1_to_hex(sha1));
+ fflush(stdout);
+ }
+ used=0; /* reset tree entry buffer for re-use in batch mode */
+ }
strbuf_release(&sb);
-
- write_tree(sha1);
- puts(sha1_to_hex(sha1));
exit(0);
}
Signature · 1 line
1.6.3.1.181.gfc9b3
PATCH 3/3 improved validation of entry type in mktree
In reply to Josh MicichPreviously mktree would accept tree entries which had a mismatch between the declared type and the actual type of object identified by the sha.
--- builtin-mktree.c | 34 +++++++++++++++++++++------------- 1 files changed, 21 insertions(+), 13 deletions(-)
builtin-mktree.c +0 -0
@@ -71,7 +71,7 @@ static void mktree_line(char *buf, size_t len, int
line_termination, int allow_m
{
char *ptr, *ntr;
unsigned mode;
- enum object_type type;
+ enum object_type mode_type;
char *path;
unsigned char sha1[20];
@@ -94,29 +94,37 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
if (S_ISGITLINK(mode))
allow_missing = 1;
- if (!allow_missing)
- type = sha1_object_info(sha1, NULL);
- else
- type = object_type(mode);
-
- if (type < 0)
- die("object %s unavailable", sha1_to_hex(sha1));
*ntr++ = 0; /* now at the beginning of SHA1 */
- if (type != type_from_string(ptr))
- die("object type %s mismatch (%s)", ptr, typename(type));
path = ntr + 41; /* at the beginning of name */
+ struct strbuf p_uq = STRBUF_INIT;
if (line_termination && path[0] == '"') {
- struct strbuf p_uq = STRBUF_INIT;
if (unquote_c_style(&p_uq, path, NULL))
die("invalid quoting");
path = strbuf_detach(&p_uq, NULL); append_to_tree(mode, sha1, path);
- strbuf_release(&p_uq);
- return;
}
+
+ mode_type = object_type(mode);
+ if (mode_type != type_from_string(ptr)) {
+ die("entry '%s' object type (%s) doesn't match mode type (%s)",
path, ptr, typename(mode_type));
+ }
+
+ enum object_type obj_type = sha1_object_info(sha1, NULL);
+ if (obj_type < 0) {
+ if (!allow_missing) {
+ die("entry '%s' object %s is unavailable", path,sha1_to_hex(sha1));
+ }
+ } else {
+ if (obj_type != mode_type) {
+ die("entry '%s' object %s is a %s but specified type
was (%s)",
+ path, sha1_to_hex(sha1), typename(obj_type),typename(mode_type));
+ }
+ }
+
append_to_tree(mode, sha1, path);
+ strbuf_release(&p_uq); // safe to call on unused empty buffer
}
int cmd_mktree(int ac, const char **av, const char *prefix)
Signature · 1 line
1.6.3.165.g2cce5.dirty
Re: [PATCH 3/3] improved validation of entry type in mktree
builtin-mktree.c
Quoted 11 lines · Josh Micich
builtin-mktree.c +0
-0@@ -71,7 +71,7 @@ static void mktree_line(char *buf, size_t len, int
line_termination, intallow_m
{builtin-mktree.c +1
-1char *ptr, *ntr;unsigned mode; -enum object_type type; +enum object_type mode_type;
Why?
Quoted 11 lines
builtin-mktree.c +0
-0@@ -94,29 +94,37 @@ static void mktree_line(char *buf, size_t len, int
line_termination, intallow_mbuiltin-mktree.c +0
-6if (S_ISGITLINK (mode)) allow_missing =1 ; -if (!allow_missing) - type =sha1_object_info (sha1,NULL ); -else - type =object_type (mode); - -if (type <0 )
builtin-mktree.c
Quoted 10 lines
builtin-mktree.c +1
-4-die ("object %s unavailable" ,sha1_to_hex (sha1)); *ntr++ =0 ;/* now at the beginning of SHA1 */ -if (type !=type_from_string (ptr)) -die ("object type %s mismatch (%s)" , ptr,typename (type)); path = ntr +41 ;/* at the beginning of name */ +struct strbuf p_uq =STRBUF_INIT ;if (line_termination && path[0 ] =='"' ) { -struct strbuf p_uq =STRBUF_INIT ;
Why make its lifetime longer even though you do not use it outside of this block?
builtin-mktree.c
Quoted 13 lines
...
builtin-mktree.c +4
-0} + + mode_type =object_type (mode); +if (mode_type !=type_from_string (ptr)) { + die("entry '%s' object type (%s) doesn't match mode type (%s)",path, ptr,
typename(mode_type));builtin-mktree.c +5
-0+ } + +enum object_type obj_type =sha1_object_info (sha1,NULL ); +if (obj_type <0 ) { +if (!allow_missing) {
This is the other way around; when allow_missing is given you shouldn't even consult the object database to read it.
Re: [PATCH 3/3] improved validation of entry type in mktree
Quoted 6 lines · Junio C Hamano
Quoted 3 lines
+ struct strbuf
p_uq=STRBUF_INIT;
if (line_termination&& path[0] == '"') {
- struct strbufp_uq=STRBUF_INIT;Why make its lifetime longer even though you do not use it outside of this block?
Sorry, no need. Similar confusion cleared up in patch 1.
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ enum object_type obj_type = sha1_object_info(sha1, NULL); + if (obj_type < 0) { + if (!allow_missing) {This is the other way around; when
allow_missingis given you shouldn't even consult the object database to read it.
I think mktree should verify the object types for objects that are available.
For example (given that 4b825d is empty tree and e69de2 is empty blob), mktree should reject the following entries:
100644 blob 4b825dc642cb6eb9a060e54bf8d69288fbee4904 foo
040000 tree e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 bar
I don't think the '--missing' option should cause mktree to accept a tree which is clearly invalid.
Furthermore even with '--missing', a tree entry like this should be rejected:
160000 commit e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 foo
Re: [PATCH 3/3] improved validation of entry type in mktree
Previously mktree would accept tree entries which had a mismatch between the declared type and the actual type of object identified by the sha.
--- builtin-mktree.c | 43 +++++++++++++++++++++++++++++++++---------- 1 files changed, 33 insertions(+), 10 deletions(-)
builtin-mktree.c +0 -0
@@ -71,7 +71,8 @@ static void mktree_line(char *buf, size_t len, int
line_termination, int allow_m
{
char *ptr, *ntr;
unsigned mode;
- enum object_type type;
+ enum object_type mode_type; /* object type derived from mode */
+ enum object_type obj_type; /* object type derived from sha */
char *path;
unsigned char sha1[20];
@@ -94,17 +95,8 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
if (S_ISGITLINK(mode))
allow_missing = 1;
- if (!allow_missing)
- type = sha1_object_info(sha1, NULL);
- else
- type = object_type(mode);
-
- if (type < 0)
- die("object %s unavailable", sha1_to_hex(sha1));
*ntr++ = 0; /* now at the beginning of SHA1 */
- if (type != type_from_string(ptr))
- die("object type %s mismatch (%s)", ptr, typename(type));
path = ntr + 41; /* at the beginning of name */ if (line_termination && path[0] == '"') {
@@ -113,6 +105,37 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
die("invalid quoting");
path = strbuf_detach(&p_uq, NULL);
}
+
+ /*
+ * Object type is redundantly derivable three ways.
+ * These should all agree.
+ */
+ mode_type = object_type(mode);
+ if (mode_type != type_from_string(ptr)) {
+ die("entry '%s' object type (%s) doesn't match mode type (%s)",
+ path, ptr, typename(mode_type));
+ }
+
+ /* Check the type of object identified by sha1 */
+ obj_type = sha1_object_info(sha1, NULL);
+ if (obj_type < 0) {
+ if (allow_missing) {
+ ; /* no problem - missing objects are presumed to be of the right type */
+ } else {
+ die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1));
+ }
+ } else {
+ if (obj_type != mode_type) {
+ /*
+ * The object exists but is of the wrong type.
+ * This is a problem regardless of allow_missing
+ * because the new tree entry will never be correct.
+ */
+ die("entry '%s' object %s is a %s but specified type was (%s)",
+ path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type));
+ }
+ }
+
append_to_tree(mode, sha1, path);
}
Signature · 1 line
1.6.3.1.181.gfc9b3.dirty
Re: [PATCH 3/3] improved validation of entry type in mktree
In reply to Josh MicichQuoted 2 lines · Josh Micich
Furthermore even with '--missing', a tree entry like this should be rejected:
160000 commite69de29bb2d1d6434b8b29ae775ad8c2e48c5391foo
But with submodules you might not _have_ e69de29b in object database to check its type!
Signature · 3 lines
Jakub Narebski Warsaw, Poland ShadeHawk on #git
Re: [PATCH 3/3] improved validation of entry type in mktree
Quoted 4 lines · Jakub Narebski <jnareb <at> gmail.com> writes:
Quoted 1 line · Josh Micich
Furthermore even with '--missing', a tree entry like this should be
rejected:
Quoted 4 lines
Quoted 1 line
160000 commit
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391fooBut with submodules you might not _have_
e69de29bin object database to check its type!
The code already accounted for that case. If the tree entry is a GIT_LINK ("160000 commit ..."), it is not mandatory for the referenced object to exist locally. But if the object _does_ exist, the type should match. I guess I should have prefixed that example with "assuming object e69de29b (empty tree) is present in the local database".