which would mean that corresponding tree - old or new - is empty.
As followup patches will show, that functionality was already needed in
several places of Git codebase, but there, we were preparing empty
tree_desc objects by hand, with some code duplication.
For handling sha1 = NULL case, let's reuse fill_tree_descriptor() which
returns just empty tree_desc in that case.
Signed-off-by: Kirill Smelkov <redacted>
---
tree-diff.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
@@ -287,14 +287,10 @@ int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const chaunsignedlongsize1,size2;intretval;-tree1=read_object_with_reference(old,tree_type,&size1,NULL);-if(!tree1)-die("unable to read source tree (%s)",sha1_to_hex(old));-tree2=read_object_with_reference(new,tree_type,&size2,NULL);-if(!tree2)-die("unable to read destination tree (%s)",sha1_to_hex(new));-init_tree_desc(&t1,tree1,size1);-init_tree_desc(&t2,tree2,size2);+tree1=fill_tree_descriptor(&t1,old);+tree2=fill_tree_descriptor(&t2,new);+size1=t1.size;+size2=t2.size;retval=diff_tree(&t1,&t2,base,opt);if(!*base&&DIFF_OPT_TST(opt,FOLLOW_RENAMES)&&diff_might_be_rename()){init_tree_desc(&t1,tree1,size1);
Now since diff_tree_sha1 understands NULL for both old and new, we could
indicate an empty tree for root commit by providing just NULL for old
sha1.
Signed-off-by: Kirill Smelkov <redacted>
---
tree-diff.c | 15 +--------------
1 file changed, 1 insertion(+), 14 deletions(-)
Since diff_tree_sha1() can now accept empty trees via NULL sha1, we
could just call it without manually reading trees into tree_desc and
duplicating code.
Cc: Thomas Rast <redacted>
Signed-off-by: Kirill Smelkov <redacted>
---
line-log.c | 26 ++------------------------
1 file changed, 2 insertions(+), 24 deletions(-)
Since diff_tree_sha1() can now accept empty trees via NULL sha1, we
could just call it without manually reading trees into tree_desc and
duplicating code.
Besides, that
if (!tree)
return 0;
looked suspect - we were saying an invalid tree != empty tree, but maybe it is
better to just say the tree is invalid here, which is what diff_tree_sha1()
does for such case.
Signed-off-by: Kirill Smelkov <redacted>
---
revision.c | 12 +-----------
1 file changed, 1 insertion(+), 11 deletions(-)
From: Jeff King <hidden> Date: 2016-06-15 22:59:50
On Wed, Feb 05, 2014 at 08:57:12PM +0400, Kirill Smelkov wrote:
Since diff_tree_sha1() can now accept empty trees via NULL sha1, we
could just call it without manually reading trees into tree_desc and
duplicating code.
Besides, that
if (!tree)
return 0;
looked suspect - we were saying an invalid tree != empty tree, but maybe it is
better to just say the tree is invalid here, which is what diff_tree_sha1()
does for such case.
I think that is sensible. The assertion that "invalid != empty" is
probably sane, because we handle the empty tree as internal magic. But I
do not see any reason we should be hitting this code path regularly with
an invalid tree, short of repository corruption, so in practice I don't
think it matters.
This does introduce a die() where there was not one previously, and that
can make things harder to diagnose/debug in a corrupted repository. But
it looks like this is limited to the history-simplification code, and I
suspect that it is not commonly used in the case of corruption.
So I think the patch looks fine.
-Peff