# [PATCH 1/2] fast-import: don't allow to tag empty branch

5 messages from 2011-09-22 to 2011-09-22. Participants: Dmitry Ivankov, Sverre Rabbelier.
Thread: https://gitlist.dev/t/28462

## Dmitry Ivankov, 2011-09-22 19:47

Subject: [PATCH 0/2] fast-import: empty/reset branch bugs
Message-ID: <1316720825-32552-1-git-send-email-divanorama@gmail.com>
URL: https://gitlist.dev/e/1316720825-32552-1-git-send-email-divanorama%40gmail.com

```
fast-import uses null_sha1 for empty branches. It doesn't make a 
null_sha1 parent nor writes out a branch with null_sha1 head. But
for notemodify and tag commands there is no check for null_sha1
and so bad tag/notes are produced instead of a error message.

Dmitry Ivankov (2):
  fast-import: don't allow to tag empty branch
  fast-import: don't allow to note on empty branch

 fast-import.c          |    4 ++++
 t/t9300-fast-import.sh |   29 +++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)

-- 
1.7.3.4

```

## Dmitry Ivankov, 2011-09-22 19:47

Subject: [PATCH 1/2] fast-import: don't allow to tag empty branch
Message-ID: <1316720825-32552-2-git-send-email-divanorama@gmail.com>
URL: https://gitlist.dev/e/1316720825-32552-2-git-send-email-divanorama%40gmail.com
In-Reply-To: <1316720825-32552-1-git-send-email-divanorama@gmail.com>

```
'reset' command makes fast-import start a branch from scratch. It's name
is kept in lookup table but it's sha1 is null_sha1 (special value).
'tag' command can be used to tag a branch by it's name. lookup_branch()
is used it that case and it doesn't check for null_sha1. So fast-import
writes a tag for null_sha1 object instead of giving a error.

Add a check to deny tagging an empty branch and add a corresponding test.

Signed-off-by: Dmitry Ivankov <divanorama@gmail.com>
---
 fast-import.c          |    2 ++
 t/t9300-fast-import.sh |   12 ++++++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index 742e7da..c44cc11 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -2717,6 +2717,8 @@ static void parse_new_tag(void)
 	from = strchr(command_buf.buf, ' ') + 1;
 	s = lookup_branch(from);
 	if (s) {
+		if (is_null_sha1(s->sha1))
+			die("Can't tag an empty branch.");
 		hashcpy(sha1, s->sha1);
 		type = OBJ_COMMIT;
 	} else if (*from == ':') {
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 1a6c066..0b97d7a 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -820,6 +820,18 @@ test_expect_success \
 	'test 1 = `git rev-list J | wc -l` &&
 	 test 0 = `git ls-tree J | wc -l`'
 
+cat >input <<INPUT_END
+reset refs/heads/J2
+
+tag wrong_tag
+from refs/heads/J2
+data <<EOF
+Tag branch that was reset.
+EOF
+INPUT_END
+test_expect_success \
+	'J: tag must fail on empty branch' \
+	'test_must_fail git fast-import <input'
 ###
 ### series K
 ###
-- 
1.7.3.4

```

## Dmitry Ivankov, 2011-09-22 19:47

Subject: [PATCH 2/2] fast-import: don't allow to note on empty branch
Message-ID: <1316720825-32552-3-git-send-email-divanorama@gmail.com>
URL: https://gitlist.dev/e/1316720825-32552-3-git-send-email-divanorama%40gmail.com
In-Reply-To: <1316720825-32552-1-git-send-email-divanorama@gmail.com>

```
'reset' command makes fast-import start a branch from scratch. It's name
is kept in lookup table but it's sha1 is null_sha1 (special value).
'notemodify' command can be used to add a note on branch head given it's
name. lookup_branch() is used it that case and it doesn't check for
null_sha1. So fast-import writes a note for null_sha1 object instead of
giving a error.

Add a check to deny adding a note on empty branch and add a corresponding
test.

Signed-off-by: Dmitry Ivankov <divanorama@gmail.com>
---
 fast-import.c          |    2 ++
 t/t9300-fast-import.sh |   17 +++++++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index c44cc11..a8a3ad1 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -2416,6 +2416,8 @@ static void note_change_n(struct branch *b, unsigned char old_fanout)
 	/* <committish> */
 	s = lookup_branch(p);
 	if (s) {
+		if (is_null_sha1(s->sha1))
+			die("Can't add a note on empty branch.");
 		hashcpy(commit_sha1, s->sha1);
 	} else if (*p == ':') {
 		uintmax_t commit_mark = strtoumax(p + 1, NULL, 10);
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 0b97d7a..bd32b91 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -1987,6 +1987,23 @@ test_expect_success \
 	'Q: verify second note for second commit' \
 	'git cat-file blob refs/notes/foobar:$commit2 >actual && test_cmp expect actual'
 
+cat >input <<EOF
+reset refs/heads/Q0
+
+commit refs/heads/note-Q0
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+Note for an empty branch.
+COMMIT
+
+N inline refs/heads/Q0
+data <<NOTE
+some note
+NOTE
+EOF
+test_expect_success \
+	'Q: deny note on empty branch' \
+	'test_must_fail git fast-import <input'
 ###
 ### series R (feature and option)
 ###
-- 
1.7.3.4

```

## Sverre Rabbelier, 2011-09-22 22:28

Subject: Re: [PATCH 1/2] fast-import: don't allow to tag empty branch
Message-ID: <CAGdFq_jxDD3_MLWAsv5WtCoSFXDm-EBB0=28aH-MqBdbGhpC3Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAGdFq_jxDD3_MLWAsv5WtCoSFXDm-EBB0%3D28aH-MqBdbGhpC3Q%40mail.gmail.com
In-Reply-To: <1316720825-32552-2-git-send-email-divanorama@gmail.com>

```
Heya,

On Thu, Sep 22, 2011 at 21:47, Dmitry Ivankov <divanorama@gmail.com> wrote:
> 'reset' command makes fast-import start a branch from scratch. It's name
> is kept in lookup table but it's sha1 is null_sha1 (special value).
> 'tag' command can be used to tag a branch by it's name. lookup_branch()
> is used it that case and it doesn't check for null_sha1. So fast-import
> writes a tag for null_sha1 object instead of giving a error.
>
> Add a check to deny tagging an empty branch and add a corresponding test.

Makes sense to me.

-- 
Cheers,

Sverre Rabbelier

```

## Sverre Rabbelier, 2011-09-22 22:29

Subject: Re: [PATCH 2/2] fast-import: don't allow to note on empty branch
Message-ID: <CAGdFq_jDvWveWm5cOXA9QQC+PmaEvwmEs7PmjoACmQXxkdT62g@mail.gmail.com>
URL: https://gitlist.dev/e/CAGdFq_jDvWveWm5cOXA9QQC%2BPmaEvwmEs7PmjoACmQXxkdT62g%40mail.gmail.com
In-Reply-To: <1316720825-32552-3-git-send-email-divanorama@gmail.com>

```
Heya,

On Thu, Sep 22, 2011 at 21:47, Dmitry Ivankov <divanorama@gmail.com> wrote:
> 'reset' command makes fast-import start a branch from scratch. It's name
> is kept in lookup table but it's sha1 is null_sha1 (special value).
> 'notemodify' command can be used to add a note on branch head given it's
> name. lookup_branch() is used it that case and it doesn't check for
> null_sha1. So fast-import writes a note for null_sha1 object instead of
> giving a error.
>
> Add a check to deny adding a note on empty branch and add a corresponding
> test.

Makes sense as well.

-- 
Cheers,

Sverre Rabbelier

```
