Skip to content

Commit 80f165a

Browse files
committed
Merge branch 'cc/replace-with-the-same-type'
* cc/replace-with-the-same-type: Doc: 'replace' merge and non-merge commits t6050-replace: use some long option names replace: allow long option names Documentation/replace: add Creating Replacement Objects section t6050-replace: add test to clean up all the replace refs t6050-replace: test that objects are of the same type Documentation/replace: state that objects must be of the same type replace: forbid replacing an object with one of a different type
2 parents d0c7890 + 9074925 commit 80f165a

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

Documentation/git-replace.txt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ The name of the 'replace' reference is the SHA-1 of the object that is
2020
replaced. The content of the 'replace' reference is the SHA-1 of the
2121
replacement object.
2222

23+
The replaced object and the replacement object must be of the same type.
24+
This restriction can be bypassed using `-f`.
25+
2326
Unless `-f` is given, the 'replace' reference must not yet exist.
2427

28+
There is no other restriction on the replaced and replacement objects.
29+
Merge commits can be replaced by non-merge commits and vice versa.
30+
2531
Replacement references will be used by default by all Git commands
2632
except those doing reachability traversal (prune, pack transfer and
2733
fsck).
@@ -49,18 +55,34 @@ achieve the same effect as the `--no-replace-objects` option.
4955
OPTIONS
5056
-------
5157
-f::
58+
--force::
5259
If an existing replace ref for the same object exists, it will
5360
be overwritten (instead of failing).
5461

5562
-d::
63+
--delete::
5664
Delete existing replace refs for the given objects.
5765

5866
-l <pattern>::
67+
--list <pattern>::
5968
List replace refs for objects that match the given pattern (or
6069
all if no pattern is given).
6170
Typing "git replace" without arguments, also lists all replace
6271
refs.
6372

73+
CREATING REPLACEMENT OBJECTS
74+
----------------------------
75+
76+
linkgit:git-filter-branch[1], linkgit:git-hash-object[1] and
77+
linkgit:git-rebase[1], among other git commands, can be used to create
78+
replacement objects from existing objects.
79+
80+
If you want to replace many blobs, trees or commits that are part of a
81+
string of commits, you may just want to create a replacement string of
82+
commits and then only replace the commit at the tip of the target
83+
string of commits with the commit at the tip of the replacement string
84+
of commits.
85+
6486
BUGS
6587
----
6688
Comparing blobs or trees that have been replaced with those that
@@ -69,12 +91,13 @@ go back to a replaced commit will move the branch to the replacement
6991
commit instead of the replaced commit.
7092

7193
There may be other problems when using 'git rev-list' related to
72-
pending objects. And of course things may break if an object of one
73-
type is replaced by an object of another type (for example a blob
74-
replaced by a commit).
94+
pending objects.
7595

7696
SEE ALSO
7797
--------
98+
linkgit:git-hash-object[1]
99+
linkgit:git-filter-branch[1]
100+
linkgit:git-rebase[1]
78101
linkgit:git-tag[1]
79102
linkgit:git-branch[1]
80103
linkgit:git[1]

builtin/replace.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,
8585
int force)
8686
{
8787
unsigned char object[20], prev[20], repl[20];
88+
enum object_type obj_type, repl_type;
8889
char ref[PATH_MAX];
8990
struct ref_lock *lock;
9091

@@ -100,6 +101,15 @@ static int replace_object(const char *object_ref, const char *replace_ref,
100101
if (check_refname_format(ref, 0))
101102
die("'%s' is not a valid ref name.", ref);
102103

104+
obj_type = sha1_object_info(object, NULL);
105+
repl_type = sha1_object_info(repl, NULL);
106+
if (!force && obj_type != repl_type)
107+
die("Objects must be of the same type.\n"
108+
"'%s' points to a replaced object of type '%s'\n"
109+
"while '%s' points to a replacement object of type '%s'.",
110+
object_ref, typename(obj_type),
111+
replace_ref, typename(repl_type));
112+
103113
if (read_ref(ref, prev))
104114
hashclr(prev);
105115
else if (!force)
@@ -118,9 +128,9 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
118128
{
119129
int list = 0, delete = 0, force = 0;
120130
struct option options[] = {
121-
OPT_BOOL('l', NULL, &list, N_("list replace refs")),
122-
OPT_BOOL('d', NULL, &delete, N_("delete replace refs")),
123-
OPT_BOOL('f', NULL, &force, N_("replace the ref if it exists")),
131+
OPT_BOOL('l', "list", &list, N_("list replace refs")),
132+
OPT_BOOL('d', "delete", &delete, N_("delete replace refs")),
133+
OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
124134
OPT_END()
125135
};
126136

t/t6050-replace.sh

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ test_expect_success '"git replace" listing and deleting' '
122122
test "$HASH2" = "$(git replace -l)" &&
123123
test "$HASH2" = "$(git replace)" &&
124124
aa=${HASH2%??????????????????????????????????????} &&
125-
test "$HASH2" = "$(git replace -l "$aa*")" &&
125+
test "$HASH2" = "$(git replace --list "$aa*")" &&
126126
test_must_fail git replace -d $R &&
127-
test_must_fail git replace -d &&
127+
test_must_fail git replace --delete &&
128128
test_must_fail git replace -l -d $HASH2 &&
129129
git replace -d $HASH2 &&
130130
git show $HASH2 | grep "A U Thor" &&
@@ -147,7 +147,7 @@ test_expect_success '"git replace" resolves sha1' '
147147
git show $HASH2 | grep "O Thor" &&
148148
test_must_fail git replace $HASH2 $R &&
149149
git replace -f $HASH2 $R &&
150-
test_must_fail git replace -f &&
150+
test_must_fail git replace --force &&
151151
test "$HASH2" = "$(git replace)"
152152
'
153153

@@ -263,4 +263,23 @@ test_expect_success 'not just commits' '
263263
test_cmp file.replaced file
264264
'
265265

266+
test_expect_success 'replaced and replacement objects must be of the same type' '
267+
test_must_fail git replace mytag $HASH1 &&
268+
test_must_fail git replace HEAD^{tree} HEAD~1 &&
269+
BLOB=$(git rev-parse :file) &&
270+
test_must_fail git replace HEAD^ $BLOB
271+
'
272+
273+
test_expect_success '-f option bypasses the type check' '
274+
git replace -f mytag $HASH1 &&
275+
git replace --force HEAD^{tree} HEAD~1 &&
276+
git replace -f HEAD^ $BLOB
277+
'
278+
279+
test_expect_success 'replace ref cleanup' '
280+
test -n "$(git replace)" &&
281+
git replace -d $(git replace) &&
282+
test -z "$(git replace)"
283+
'
284+
266285
test_done

0 commit comments

Comments
 (0)