From 277336a5e0341a5ae06fc330834dfeefe5e85cec Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:53 +0200 Subject: replace: forbid replacing an object with one of a different type 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. If -f option, which means '--force', is used, we can allow an object to be replaced with one of a different type, as the user should know what (s)he is doing. If one object is replaced with one of a different type, the only way to keep the history valid is to also replace all the other objects that point to the replaced object. That's 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 Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/builtin/replace.c b/builtin/replace.c index 59d3115..95736d9 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 (!force && obj_type != repl_type) + 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'.", + object_ref, typename(obj_type), + replace_ref, typename(repl_type)); + if (read_ref(ref, prev)) hashclr(prev); else if (!force) -- cgit v0.10.2-6-g49f6 From 160df71ef53b0ad645391ec7d5525eda33871d09 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:54 +0200 Subject: Documentation/replace: state that objects must be of the same type A previous patch ensures that both the replaced and the replacement objects passed to git replace must be of the same type, except if -f option is used. While at it state that there is no other restriction on both objects. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt index e0b4057..d198006 100644 --- a/Documentation/git-replace.txt +++ b/Documentation/git-replace.txt @@ -20,8 +20,13 @@ 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. +This restriction can be bypassed using `-f`. + Unless `-f` is given, the 'replace' reference must not yet exist. +There is no other restriction on the replaced and replacement objects. + Replacement references will be used by default by all Git commands except those doing reachability traversal (prune, pack transfer and fsck). @@ -69,9 +74,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 -------- -- cgit v0.10.2-6-g49f6 From 3e625c8fec8a2ec49352e8f0a3862ccbfca11d3a Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:55 +0200 Subject: t6050-replace: test that objects are of the same type and that the -f option bypasses the type check Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh index decdc33..09bad98 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 && + test_must_fail git replace HEAD^{tree} HEAD~1 && + BLOB=$(git rev-parse :file) && + test_must_fail git replace HEAD^ $BLOB +' + +test_expect_success '-f option bypasses the type check' ' + git replace -f mytag $HASH1 && + git replace -f HEAD^{tree} HEAD~1 && + git replace -f HEAD^ $BLOB +' + test_done -- cgit v0.10.2-6-g49f6 From 11aec9556b1facd6272495690bd61995bd83547c Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:56 +0200 Subject: t6050-replace: add test to clean up all the replace refs Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh index 09bad98..09a2b49 100755 --- a/t/t6050-replace.sh +++ b/t/t6050-replace.sh @@ -276,4 +276,10 @@ test_expect_success '-f option bypasses the type check' ' git replace -f HEAD^ $BLOB ' +test_expect_success 'replace ref cleanup' ' + test -n "$(git replace)" && + git replace -d $(git replace) && + test -z "$(git replace)" +' + test_done -- cgit v0.10.2-6-g49f6 From b8fcce1e7f7baafba8b6ba0b4e9bec2f27893f43 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:57 +0200 Subject: Documentation/replace: add Creating Replacement Objects section There were no hints in the documentation about how to create replacement objects. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt index d198006..a2bd2ee 100644 --- a/Documentation/git-replace.txt +++ b/Documentation/git-replace.txt @@ -66,6 +66,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 @@ -78,6 +91,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] -- cgit v0.10.2-6-g49f6 From ed0ff80984d37f9f40b1bf209bc86ea2fa8f3783 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:58 +0200 Subject: replace: allow long option names It is now standard practice in Git to have both short and long option names. So let's give a long option name to the git replace options too. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt index a2bd2ee..414000e 100644 --- a/Documentation/git-replace.txt +++ b/Documentation/git-replace.txt @@ -54,13 +54,16 @@ achieve the same effect as the `--no-replace-objects` option. OPTIONS ------- -f:: +--force:: If an existing replace ref for the same object exists, it will be overwritten (instead of failing). -d:: +--delete:: Delete existing replace refs for the given objects. -l :: +--list :: List replace refs for objects that match the given pattern (or all if no pattern is given). Typing "git replace" without arguments, also lists all replace diff --git a/builtin/replace.c b/builtin/replace.c index 95736d9..d4d1b75 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -128,9 +128,9 @@ int cmd_replace(int argc, const char **argv, const char *prefix) { int list = 0, delete = 0, force = 0; struct option options[] = { - OPT_BOOLEAN('l', NULL, &list, N_("list replace refs")), - OPT_BOOLEAN('d', NULL, &delete, N_("delete replace refs")), - OPT_BOOLEAN('f', NULL, &force, N_("replace the ref if it exists")), + OPT_BOOLEAN('l', "list", &list, N_("list replace refs")), + OPT_BOOLEAN('d', "delete", &delete, N_("delete replace refs")), + OPT_BOOLEAN('f', "force", &force, N_("replace the ref if it exists")), OPT_END() }; -- cgit v0.10.2-6-g49f6 From b1ecd8cfdfc46bcb3d2c06cf0dbaf6d1ebe41e3c Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Fri, 6 Sep 2013 07:10:59 +0200 Subject: t6050-replace: use some long option names So that they are tested a little bit too. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh index 09a2b49..7d47984 100755 --- a/t/t6050-replace.sh +++ b/t/t6050-replace.sh @@ -122,9 +122,9 @@ test_expect_success '"git replace" listing and deleting' ' test "$HASH2" = "$(git replace -l)" && test "$HASH2" = "$(git replace)" && aa=${HASH2%??????????????????????????????????????} && - test "$HASH2" = "$(git replace -l "$aa*")" && + test "$HASH2" = "$(git replace --list "$aa*")" && test_must_fail git replace -d $R && - test_must_fail git replace -d && + test_must_fail git replace --delete && test_must_fail git replace -l -d $HASH2 && git replace -d $HASH2 && git show $HASH2 | grep "A U Thor" && @@ -147,7 +147,7 @@ test_expect_success '"git replace" resolves sha1' ' git show $HASH2 | grep "O Thor" && test_must_fail git replace $HASH2 $R && git replace -f $HASH2 $R && - test_must_fail git replace -f && + test_must_fail git replace --force && test "$HASH2" = "$(git replace)" ' @@ -272,7 +272,7 @@ test_expect_success 'replaced and replacement objects must be of the same type' test_expect_success '-f option bypasses the type check' ' git replace -f mytag $HASH1 && - git replace -f HEAD^{tree} HEAD~1 && + git replace --force HEAD^{tree} HEAD~1 && git replace -f HEAD^ $BLOB ' -- cgit v0.10.2-6-g49f6 From 907492534118a88d071676e78fe4bd1427189a37 Mon Sep 17 00:00:00 2001 From: Philip Oakley Date: Sun, 8 Sep 2013 13:10:44 +0100 Subject: Doc: 'replace' merge and non-merge commits Merges are often treated as special case objects so tell users that they are not special here. Signed-off-by: Philip Oakley Signed-off-by: Junio C Hamano diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt index 414000e..f373ab4 100644 --- a/Documentation/git-replace.txt +++ b/Documentation/git-replace.txt @@ -26,6 +26,7 @@ This restriction can be bypassed using `-f`. Unless `-f` is given, the 'replace' reference must not yet exist. There is no other restriction on the replaced and replacement objects. +Merge commits can be replaced by non-merge commits and vice versa. Replacement references will be used by default by all Git commands except those doing reachability traversal (prune, pack transfer and -- cgit v0.10.2-6-g49f6