# [PATCH 0/5] Check replacement object type and minor updates

11 messages from 2013-08-25 to 2013-08-26. Participants: Christian Couder, Johannes Sixt.
Thread: https://gitlist.dev/t/34764

## Christian Couder, 2013-08-25 13:06

Subject: [PATCH 0/5] Check replacement object type and minor updates
Message-ID: <20130825125940.4681.70226.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825125940.4681.70226.chriscool%40tuxfamily.org

```
Here is a reroll of the patch forbiding replacing an bject with one
of a different type (1/5).
It is followed by a documentation update (2/5) and a test (3/5).
I also added for good mesure another test (4/5) I came up with while
developing the previous test, and another doc update related to what
we discussed about creating replacement objects (5/5).

Christian Couder (5):
  replace: forbid replacing an object with one of a different type
  Documentation/replace: state that objects must be of the same type
  t6050-replace: test that objects are of the same type
  t6050-replace: add test to clean up all the replace refs
  Documentation/replace: add Creating Replacement Objects section

 Documentation/git-replace.txt | 23 ++++++++++++++++++++---
 builtin/replace.c             | 10 ++++++++++
 t/t6050-replace.sh            | 19 +++++++++++++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)

-- 
1.8.4.rc1.24.g13dc82a

```

## Christian Couder, 2013-08-25 13:06

Subject: [PATCH 1/5] replace: forbid replacing an object with one of a different type
Message-ID: <20130825130609.4681.25786.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825130609.4681.25786.chriscool%40tuxfamily.org
In-Reply-To: <20130825125940.4681.70226.chriscool@tuxfamily.org>

```
Users replacing an object with one of a different type were not
prevented to do so, even if it was obvious, and stated in the doc,
that bad things would result from doing that.

To avoid mistakes, it is better to just forbid that though.

There is no case where one object can be replaced with one of a
different type while keeping the history valid, because:

* Annotated tags contain the type of the tagged object.

* The tree/parent lines in commits must be a tree and commits, resp.

* The object types referred to by trees are specified in the 'mode'
  field:
    100644 and 100755    blob
    160000               commit
    040000               tree
  (these are the only valid modes)

* Blobs don't point at anything.

The doc will be updated in a later patch.

Acked-by: Philip Oakley <philipoakley@iee.org>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/replace.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/builtin/replace.c b/builtin/replace.c
index 59d3115..bc6c245 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -85,6 +85,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,
 			  int force)
 {
 	unsigned char object[20], prev[20], repl[20];
+	enum object_type obj_type, repl_type;
 	char ref[PATH_MAX];
 	struct ref_lock *lock;
 
@@ -100,6 +101,15 @@ static int replace_object(const char *object_ref, const char *replace_ref,
 	if (check_refname_format(ref, 0))
 		die("'%s' is not a valid ref name.", ref);
 
+	obj_type = sha1_object_info(object, NULL);
+	repl_type = sha1_object_info(repl, NULL);
+	if (obj_type != repl_type)
+		die("Objects must be of the same type.\n"
+		    "Object ref '%s' is of type '%s'\n"
+		    "while replace ref '%s' is of type '%s'.",
+		    object_ref, typename(obj_type),
+		    replace_ref, typename(repl_type));
+
 	if (read_ref(ref, prev))
 		hashclr(prev);
 	else if (!force)
-- 
1.8.4.rc1.24.g13dc82a

```

## Christian Couder, 2013-08-25 13:06

Subject: [PATCH 2/5] Documentation/replace: state that objects must be of the same type
Message-ID: <20130825130609.4681.83578.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825130609.4681.83578.chriscool%40tuxfamily.org
In-Reply-To: <20130825125940.4681.70226.chriscool@tuxfamily.org>

```
A previous patch ensures that both the replaced and the replacement
objects passed to git replace must be of the same type.

While at it state that there is no other restriction on both objects.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Documentation/git-replace.txt | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt
index e0b4057..aa66d27 100644
--- a/Documentation/git-replace.txt
+++ b/Documentation/git-replace.txt
@@ -20,6 +20,9 @@ The name of the 'replace' reference is the SHA-1 of the object that is
 replaced. The content of the 'replace' reference is the SHA-1 of the
 replacement object.
 
+The replaced object and the replacement object must be of the same type.
+There is no other restriction on them.
+
 Unless `-f` is given, the 'replace' reference must not yet exist.
 
 Replacement references will be used by default by all Git commands
@@ -69,9 +72,7 @@ go back to a replaced commit will move the branch to the replacement
 commit instead of the replaced commit.
 
 There may be other problems when using 'git rev-list' related to
-pending objects. And of course things may break if an object of one
-type is replaced by an object of another type (for example a blob
-replaced by a commit).
+pending objects.
 
 SEE ALSO
 --------
-- 
1.8.4.rc1.24.g13dc82a

```

## Christian Couder, 2013-08-25 13:06

Subject: [PATCH 3/5] t6050-replace: test that objects are of the same type
Message-ID: <20130825130609.4681.58306.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825130609.4681.58306.chriscool%40tuxfamily.org
In-Reply-To: <20130825125940.4681.70226.chriscool@tuxfamily.org>

```
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t6050-replace.sh | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index decdc33..8f631ac 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -263,4 +263,17 @@ test_expect_success 'not just commits' '
 	test_cmp file.replaced file
 '
 
+test_expect_success 'replaced and replacement objects must be of the same type' '
+	test_must_fail git replace mytag $HASH1 2>err &&
+	grep "Object ref '\''mytag'\'' is of type '\''tag'\''" err &&
+	grep "replace ref '\''$HASH1'\'' is of type '\''commit'\''" err &&
+	test_must_fail git replace HEAD^{tree} HEAD~1 2>err &&
+	grep "Object ref '\''HEAD^{tree}'\'' is of type '\''tree'\''" err &&
+	grep "replace ref '\''HEAD~1'\'' is of type '\''commit'\''" err &&
+	BLOB=$(git rev-parse :file) &&
+	test_must_fail git replace HEAD^ $BLOB 2>err &&
+	grep "Object ref '\''HEAD^'\'' is of type '\''commit'\''" err &&
+	grep "replace ref '\''$BLOB'\'' is of type '\''blob'\''" err
+'
+
 test_done
-- 
1.8.4.rc1.24.g13dc82a

```

## Christian Couder, 2013-08-25 13:06

Subject: [PATCH 4/5] t6050-replace: add test to clean up all the replace refs
Message-ID: <20130825130609.4681.43592.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825130609.4681.43592.chriscool%40tuxfamily.org
In-Reply-To: <20130825125940.4681.70226.chriscool@tuxfamily.org>

```
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t6050-replace.sh | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index 8f631ac..4807689 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -276,4 +276,10 @@ test_expect_success 'replaced and replacement objects must be of the same type'
 	grep "replace ref '\''$BLOB'\'' is of type '\''blob'\''" err
 '
 
+test_expect_success 'replace ref cleanup' '
+	test -n "$(git replace)" &&
+	git replace -d $(git replace) &&
+	test -z "$(git replace)"
+'
+
 test_done
-- 
1.8.4.rc1.24.g13dc82a

```

## Christian Couder, 2013-08-25 13:06

Subject: [PATCH 5/5] Documentation/replace: add Creating Replacement Objects section
Message-ID: <20130825130609.4681.95932.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825130609.4681.95932.chriscool%40tuxfamily.org
In-Reply-To: <20130825125940.4681.70226.chriscool@tuxfamily.org>

```
There were no hints in the documentation about how to create
replacement objects.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Documentation/git-replace.txt | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt
index aa66d27..736b48c 100644
--- a/Documentation/git-replace.txt
+++ b/Documentation/git-replace.txt
@@ -64,6 +64,19 @@ OPTIONS
 	Typing "git replace" without arguments, also lists all replace
 	refs.
 
+CREATING REPLACEMENT OBJECTS
+----------------------------
+
+linkgit:git-filter-branch[1], linkgit:git-hash-object[1] and
+linkgit:git-rebase[1], among other git commands, can be used to create
+replacement objects from existing objects.
+
+If you want to replace many blobs, trees or commits that are part of a
+string of commits, you may just want to create a replacement string of
+commits and then only replace the commit at the tip of the target
+string of commits with the commit at the tip of the replacement string
+of commits.
+
 BUGS
 ----
 Comparing blobs or trees that have been replaced with those that
@@ -76,6 +89,9 @@ pending objects.
 
 SEE ALSO
 --------
+linkgit:git-hash-object[1]
+linkgit:git-filter-branch[1]
+linkgit:git-rebase[1]
 linkgit:git-tag[1]
 linkgit:git-branch[1]
 linkgit:git[1]
-- 
1.8.4.rc1.24.g13dc82a

```

## Johannes Sixt, 2013-08-25 18:35

Subject: Re: [PATCH 1/5] replace: forbid replacing an object with one of a different type
Message-ID: <521A4E83.9000400@kdbg.org>
URL: https://gitlist.dev/e/521A4E83.9000400%40kdbg.org
In-Reply-To: <20130825130609.4681.25786.chriscool@tuxfamily.org>

```
Am 25.08.2013 15:06, schrieb Christian Couder:
> @@ -100,6 +101,15 @@ static int replace_object(const char *object_ref, const char *replace_ref,
>  	if (check_refname_format(ref, 0))
>  		die("'%s' is not a valid ref name.", ref);
>  
> +	obj_type = sha1_object_info(object, NULL);
> +	repl_type = sha1_object_info(repl, NULL);
> +	if (obj_type != repl_type)
> +		die("Objects must be of the same type.\n"
> +		    "Object ref '%s' is of type '%s'\n"

Is it really an "Object ref", not just an "Object"?

> +		    "while replace ref '%s' is of type '%s'.",

And here:

		"while replacement object '%s' is of type '%s'.",

BTW, I appreciate your choice of where in the sentence the line breaks are.

> +		    object_ref, typename(obj_type),
> +		    replace_ref, typename(repl_type));
> +
>  	if (read_ref(ref, prev))
>  		hashclr(prev);
>  	else if (!force)

-- Hannes

```

## Christian Couder, 2013-08-25 19:44

Subject: Re: [PATCH 1/5] replace: forbid replacing an object with one of a different type
Message-ID: <20130825.214417.1308749312477487067.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130825.214417.1308749312477487067.chriscool%40tuxfamily.org
In-Reply-To: <521A4E83.9000400@kdbg.org>

```
From: Johannes Sixt <j6t@kdbg.org>

> Am 25.08.2013 15:06, schrieb Christian Couder:
>> @@ -100,6 +101,15 @@ static int replace_object(const char *object_ref, const char *replace_ref,
>>  	if (check_refname_format(ref, 0))
>>  		die("'%s' is not a valid ref name.", ref);
>>  
>> +	obj_type = sha1_object_info(object, NULL);
>> +	repl_type = sha1_object_info(repl, NULL);
>> +	if (obj_type != repl_type)
>> +		die("Objects must be of the same type.\n"
>> +		    "Object ref '%s' is of type '%s'\n"
> 
> Is it really an "Object ref", not just an "Object"?

Well, it is what is passed to the command line. It is then converted into an
hex sha1 using get_sha1() and then sha1_to_hex().

What about:

     		die("Objects must be of the same type.\n"
		    "'%s' points to a replaced object of type '%s'\n"
		    "while '%s' points to a replacement object of type '%s'.",

> BTW, I appreciate your choice of where in the sentence the line breaks are.

Thanks,
Christian.

```

## Johannes Sixt, 2013-08-25 20:06

Subject: Re: [PATCH 3/5] t6050-replace: test that objects are of the same type
Message-ID: <521A63B3.6040004@kdbg.org>
URL: https://gitlist.dev/e/521A63B3.6040004%40kdbg.org
In-Reply-To: <20130825130609.4681.58306.chriscool@tuxfamily.org>

```
Am 25.08.2013 15:06, schrieb Christian Couder:
> +test_expect_success 'replaced and replacement objects must be of the same type' '
> +	test_must_fail git replace mytag $HASH1 2>err &&
> +	grep "Object ref '\''mytag'\'' is of type '\''tag'\''" err &&

Uh, this hurts in the eye! Please write this as

	grep "Object ref .mytag. is of type .tag" err &&

-- Hannes

```

## Johannes Sixt, 2013-08-25 20:18

Subject: Re: [PATCH 1/5] replace: forbid replacing an object with one of a different type
Message-ID: <521A6684.7090103@kdbg.org>
URL: https://gitlist.dev/e/521A6684.7090103%40kdbg.org
In-Reply-To: <20130825.214417.1308749312477487067.chriscool@tuxfamily.org>

```
Am 25.08.2013 21:44, schrieb Christian Couder:
> What about:
> 
>      		die("Objects must be of the same type.\n"
> 		    "'%s' points to a replaced object of type '%s'\n"
> 		    "while '%s' points to a replacement object of type '%s'.",

Much better!

-- Hannes

```

## Christian Couder, 2013-08-26 07:19

Subject: Re: [PATCH 1/5] replace: forbid replacing an object with one of a different type
Message-ID: <20130826.091937.1157849584660055093.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20130826.091937.1157849584660055093.chriscool%40tuxfamily.org
In-Reply-To: <521A6684.7090103@kdbg.org>

```
From: Johannes Sixt <j6t@kdbg.org>

> Am 25.08.2013 21:44, schrieb Christian Couder:
>> What about:
>> 
>>      		die("Objects must be of the same type.\n"
>> 		    "'%s' points to a replaced object of type '%s'\n"
>> 		    "while '%s' points to a replacement object of type '%s'.",
> 
> Much better!

Ok, I will reroll with this change and the change you suggest in the test.

Thanks,
Christian.

```
