Browse Source
* cc/replace: t6050: check pushing something based on a replaced commit Documentation: add documentation for "git replace" Add git-replace to .gitignore builtin-replace: use "usage_msg_opt" to give better error messages parse-options: add new function "usage_msg_opt" builtin-replace: teach "git replace" to actually replace Add new "git replace" command environment: add global variable to disable replacement mktag: call "check_sha1_signature" with the replacement sha1 replace_object: add a test case object: call "check_sha1_signature" with the replacement sha1 sha1_file: add a "read_sha1_file_repl" function replace_object: add mechanism to replace objects found in "refs/replace/" refs: add a "for_each_replace_ref" functionmaint
Junio C Hamano
16 years ago
23 changed files with 610 additions and 13 deletions
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
git-replace(1) |
||||
============== |
||||
|
||||
NAME |
||||
---- |
||||
git-replace - Create, list, delete refs to replace objects |
||||
|
||||
SYNOPSIS |
||||
-------- |
||||
[verse] |
||||
'git replace' [-f] <object> <replacement> |
||||
'git replace' -d <object>... |
||||
'git replace' -l [<pattern>] |
||||
|
||||
DESCRIPTION |
||||
----------- |
||||
Adds a 'replace' reference in `.git/refs/replace/` |
||||
|
||||
The name of the 'replace' reference is the SHA1 of the object that is |
||||
replaced. The content of the replace reference is the SHA1 of the |
||||
replacement object. |
||||
|
||||
Unless `-f` is given, the replace reference must not yet exist in |
||||
`.git/refs/replace/` directory. |
||||
|
||||
OPTIONS |
||||
------- |
||||
-f:: |
||||
If an existing replace ref for the same object exists, it will |
||||
be overwritten (instead of failing). |
||||
|
||||
-d:: |
||||
Delete existing replace refs for the given objects. |
||||
|
||||
-l <pattern>:: |
||||
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 |
||||
refs. |
||||
|
||||
BUGS |
||||
---- |
||||
Comparing blobs or trees that have been replaced with those that |
||||
replace them will not work properly. And using 'git reset --hard' to |
||||
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). |
||||
|
||||
SEE ALSO |
||||
-------- |
||||
linkgit:git-tag[1] |
||||
linkgit:git-branch[1] |
||||
|
||||
Author |
||||
------ |
||||
Written by Christian Couder <chriscool@tuxfamily.org> and Junio C |
||||
Hamano <gitster@pobox.com>, based on 'git tag' by Kristian Hogsberg |
||||
<krh@redhat.com> and Carlos Rica <jasampler@gmail.com>. |
||||
|
||||
Documentation |
||||
-------------- |
||||
Documentation by Christian Couder <chriscool@tuxfamily.org> and the |
||||
git-list <git@vger.kernel.org>, based on 'git tag' documentation. |
||||
|
||||
GIT |
||||
--- |
||||
Part of the linkgit:git[1] suite |
@ -0,0 +1,159 @@
@@ -0,0 +1,159 @@
|
||||
/* |
||||
* Builtin "git replace" |
||||
* |
||||
* Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org> |
||||
* |
||||
* Based on builtin-tag.c by Kristian Høgsberg <krh@redhat.com> |
||||
* and Carlos Rica <jasampler@gmail.com> that was itself based on |
||||
* git-tag.sh and mktag.c by Linus Torvalds. |
||||
*/ |
||||
|
||||
#include "cache.h" |
||||
#include "builtin.h" |
||||
#include "refs.h" |
||||
#include "parse-options.h" |
||||
|
||||
static const char * const git_replace_usage[] = { |
||||
"git replace [-f] <object> <replacement>", |
||||
"git replace -d <object>...", |
||||
"git replace -l [<pattern>]", |
||||
NULL |
||||
}; |
||||
|
||||
static int show_reference(const char *refname, const unsigned char *sha1, |
||||
int flag, void *cb_data) |
||||
{ |
||||
const char *pattern = cb_data; |
||||
|
||||
if (!fnmatch(pattern, refname, 0)) |
||||
printf("%s\n", refname); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int list_replace_refs(const char *pattern) |
||||
{ |
||||
if (pattern == NULL) |
||||
pattern = "*"; |
||||
|
||||
for_each_replace_ref(show_reference, (void *) pattern); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
typedef int (*each_replace_name_fn)(const char *name, const char *ref, |
||||
const unsigned char *sha1); |
||||
|
||||
static int for_each_replace_name(const char **argv, each_replace_name_fn fn) |
||||
{ |
||||
const char **p; |
||||
char ref[PATH_MAX]; |
||||
int had_error = 0; |
||||
unsigned char sha1[20]; |
||||
|
||||
for (p = argv; *p; p++) { |
||||
if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p) |
||||
>= sizeof(ref)) { |
||||
error("replace ref name too long: %.*s...", 50, *p); |
||||
had_error = 1; |
||||
continue; |
||||
} |
||||
if (!resolve_ref(ref, sha1, 1, NULL)) { |
||||
error("replace ref '%s' not found.", *p); |
||||
had_error = 1; |
||||
continue; |
||||
} |
||||
if (fn(*p, ref, sha1)) |
||||
had_error = 1; |
||||
} |
||||
return had_error; |
||||
} |
||||
|
||||
static int delete_replace_ref(const char *name, const char *ref, |
||||
const unsigned char *sha1) |
||||
{ |
||||
if (delete_ref(ref, sha1, 0)) |
||||
return 1; |
||||
printf("Deleted replace ref '%s'\n", name); |
||||
return 0; |
||||
} |
||||
|
||||
static int replace_object(const char *object_ref, const char *replace_ref, |
||||
int force) |
||||
{ |
||||
unsigned char object[20], prev[20], repl[20]; |
||||
char ref[PATH_MAX]; |
||||
struct ref_lock *lock; |
||||
|
||||
if (get_sha1(object_ref, object)) |
||||
die("Failed to resolve '%s' as a valid ref.", object_ref); |
||||
if (get_sha1(replace_ref, repl)) |
||||
die("Failed to resolve '%s' as a valid ref.", replace_ref); |
||||
|
||||
if (snprintf(ref, sizeof(ref), |
||||
"refs/replace/%s", |
||||
sha1_to_hex(object)) > sizeof(ref) - 1) |
||||
die("replace ref name too long: %.*s...", 50, ref); |
||||
if (check_ref_format(ref)) |
||||
die("'%s' is not a valid ref name.", ref); |
||||
|
||||
if (!resolve_ref(ref, prev, 1, NULL)) |
||||
hashclr(prev); |
||||
else if (!force) |
||||
die("replace ref '%s' already exists", ref); |
||||
|
||||
lock = lock_any_ref_for_update(ref, prev, 0); |
||||
if (!lock) |
||||
die("%s: cannot lock the ref", ref); |
||||
if (write_ref_sha1(lock, repl, NULL) < 0) |
||||
die("%s: cannot update the ref", ref); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
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, "list replace refs"), |
||||
OPT_BOOLEAN('d', NULL, &delete, "delete replace refs"), |
||||
OPT_BOOLEAN('f', NULL, &force, "replace the ref if it exists"), |
||||
OPT_END() |
||||
}; |
||||
|
||||
argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0); |
||||
|
||||
if (list && delete) |
||||
usage_msg_opt("-l and -d cannot be used together", |
||||
git_replace_usage, options); |
||||
|
||||
if (force && (list || delete)) |
||||
usage_msg_opt("-f cannot be used with -d or -l", |
||||
git_replace_usage, options); |
||||
|
||||
/* Delete refs */ |
||||
if (delete) { |
||||
if (argc < 1) |
||||
usage_msg_opt("-d needs at least one argument", |
||||
git_replace_usage, options); |
||||
return for_each_replace_name(argv, delete_replace_ref); |
||||
} |
||||
|
||||
/* Replace object */ |
||||
if (!list && argc) { |
||||
if (argc != 2) |
||||
usage_msg_opt("bad number of arguments", |
||||
git_replace_usage, options); |
||||
return replace_object(argv[0], argv[1], force); |
||||
} |
||||
|
||||
/* List refs, even if "list" is not set */ |
||||
if (argc > 1) |
||||
usage_msg_opt("only one pattern can be given with -l", |
||||
git_replace_usage, options); |
||||
if (force) |
||||
usage_msg_opt("-f needs some arguments", |
||||
git_replace_usage, options); |
||||
|
||||
return list_replace_refs(argv[0]); |
||||
} |
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
#include "cache.h" |
||||
#include "sha1-lookup.h" |
||||
#include "refs.h" |
||||
|
||||
static struct replace_object { |
||||
unsigned char sha1[2][20]; |
||||
} **replace_object; |
||||
|
||||
static int replace_object_alloc, replace_object_nr; |
||||
|
||||
static const unsigned char *replace_sha1_access(size_t index, void *table) |
||||
{ |
||||
struct replace_object **replace = table; |
||||
return replace[index]->sha1[0]; |
||||
} |
||||
|
||||
static int replace_object_pos(const unsigned char *sha1) |
||||
{ |
||||
return sha1_pos(sha1, replace_object, replace_object_nr, |
||||
replace_sha1_access); |
||||
} |
||||
|
||||
static int register_replace_object(struct replace_object *replace, |
||||
int ignore_dups) |
||||
{ |
||||
int pos = replace_object_pos(replace->sha1[0]); |
||||
|
||||
if (0 <= pos) { |
||||
if (ignore_dups) |
||||
free(replace); |
||||
else { |
||||
free(replace_object[pos]); |
||||
replace_object[pos] = replace; |
||||
} |
||||
return 1; |
||||
} |
||||
pos = -pos - 1; |
||||
if (replace_object_alloc <= ++replace_object_nr) { |
||||
replace_object_alloc = alloc_nr(replace_object_alloc); |
||||
replace_object = xrealloc(replace_object, |
||||
sizeof(*replace_object) * |
||||
replace_object_alloc); |
||||
} |
||||
if (pos < replace_object_nr) |
||||
memmove(replace_object + pos + 1, |
||||
replace_object + pos, |
||||
(replace_object_nr - pos - 1) * |
||||
sizeof(*replace_object)); |
||||
replace_object[pos] = replace; |
||||
return 0; |
||||
} |
||||
|
||||
static int register_replace_ref(const char *refname, |
||||
const unsigned char *sha1, |
||||
int flag, void *cb_data) |
||||
{ |
||||
/* Get sha1 from refname */ |
||||
const char *slash = strrchr(refname, '/'); |
||||
const char *hash = slash ? slash + 1 : refname; |
||||
struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj)); |
||||
|
||||
if (strlen(hash) != 40 || get_sha1_hex(hash, repl_obj->sha1[0])) { |
||||
free(repl_obj); |
||||
warning("bad replace ref name: %s", refname); |
||||
return 0; |
||||
} |
||||
|
||||
/* Copy sha1 from the read ref */ |
||||
hashcpy(repl_obj->sha1[1], sha1); |
||||
|
||||
/* Register new object */ |
||||
if (register_replace_object(repl_obj, 1)) |
||||
die("duplicate replace ref: %s", refname); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static void prepare_replace_object(void) |
||||
{ |
||||
static int replace_object_prepared; |
||||
|
||||
if (replace_object_prepared) |
||||
return; |
||||
|
||||
for_each_replace_ref(register_replace_ref, NULL); |
||||
replace_object_prepared = 1; |
||||
} |
||||
|
||||
/* We allow "recursive" replacement. Only within reason, though */ |
||||
#define MAXREPLACEDEPTH 5 |
||||
|
||||
const unsigned char *lookup_replace_object(const unsigned char *sha1) |
||||
{ |
||||
int pos, depth = MAXREPLACEDEPTH; |
||||
const unsigned char *cur = sha1; |
||||
|
||||
if (!read_replace_refs) |
||||
return sha1; |
||||
|
||||
prepare_replace_object(); |
||||
|
||||
/* Try to recursively replace the object */ |
||||
do { |
||||
if (--depth < 0) |
||||
die("replace depth too high for object %s", |
||||
sha1_to_hex(sha1)); |
||||
|
||||
pos = replace_object_pos(cur); |
||||
if (0 <= pos) |
||||
cur = replace_object[pos]->sha1[1]; |
||||
} while (0 <= pos); |
||||
|
||||
return cur; |
||||
} |
@ -0,0 +1,200 @@
@@ -0,0 +1,200 @@
|
||||
#!/bin/sh |
||||
# |
||||
# Copyright (c) 2008 Christian Couder |
||||
# |
||||
test_description='Tests replace refs functionality' |
||||
|
||||
exec </dev/null |
||||
|
||||
. ./test-lib.sh |
||||
|
||||
add_and_commit_file() |
||||
{ |
||||
_file="$1" |
||||
_msg="$2" |
||||
|
||||
git add $_file || return $? |
||||
test_tick || return $? |
||||
git commit --quiet -m "$_file: $_msg" |
||||
} |
||||
|
||||
HASH1= |
||||
HASH2= |
||||
HASH3= |
||||
HASH4= |
||||
HASH5= |
||||
HASH6= |
||||
HASH7= |
||||
|
||||
test_expect_success 'set up buggy branch' ' |
||||
echo "line 1" >> hello && |
||||
echo "line 2" >> hello && |
||||
echo "line 3" >> hello && |
||||
echo "line 4" >> hello && |
||||
add_and_commit_file hello "4 lines" && |
||||
HASH1=$(git rev-parse --verify HEAD) && |
||||
echo "line BUG" >> hello && |
||||
echo "line 6" >> hello && |
||||
echo "line 7" >> hello && |
||||
echo "line 8" >> hello && |
||||
add_and_commit_file hello "4 more lines with a BUG" && |
||||
HASH2=$(git rev-parse --verify HEAD) && |
||||
echo "line 9" >> hello && |
||||
echo "line 10" >> hello && |
||||
add_and_commit_file hello "2 more lines" && |
||||
HASH3=$(git rev-parse --verify HEAD) && |
||||
echo "line 11" >> hello && |
||||
add_and_commit_file hello "1 more line" && |
||||
HASH4=$(git rev-parse --verify HEAD) && |
||||
sed -e "s/BUG/5/" hello > hello.new && |
||||
mv hello.new hello && |
||||
add_and_commit_file hello "BUG fixed" && |
||||
HASH5=$(git rev-parse --verify HEAD) && |
||||
echo "line 12" >> hello && |
||||
echo "line 13" >> hello && |
||||
add_and_commit_file hello "2 more lines" && |
||||
HASH6=$(git rev-parse --verify HEAD) |
||||
echo "line 14" >> hello && |
||||
echo "line 15" >> hello && |
||||
echo "line 16" >> hello && |
||||
add_and_commit_file hello "again 3 more lines" && |
||||
HASH7=$(git rev-parse --verify HEAD) |
||||
' |
||||
|
||||
test_expect_success 'replace the author' ' |
||||
git cat-file commit $HASH2 | grep "author A U Thor" && |
||||
R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) && |
||||
git cat-file commit $R | grep "author O Thor" && |
||||
git update-ref refs/replace/$HASH2 $R && |
||||
git show HEAD~5 | grep "O Thor" && |
||||
git show $HASH2 | grep "O Thor" |
||||
' |
||||
|
||||
cat >tag.sig <<EOF |
||||
object $HASH2 |
||||
type commit |
||||
tag mytag |
||||
tagger T A Gger <> 0 +0000 |
||||
|
||||
EOF |
||||
|
||||
test_expect_success 'tag replaced commit' ' |
||||
git mktag <tag.sig >.git/refs/tags/mytag 2>message |
||||
' |
||||
|
||||
test_expect_success '"git fsck" works' ' |
||||
git fsck master > fsck_master.out && |
||||
grep "dangling commit $R" fsck_master.out && |
||||
grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out && |
||||
test -z "$(git fsck)" |
||||
' |
||||
|
||||
test_expect_success 'repack, clone and fetch work' ' |
||||
git repack -a -d && |
||||
git clone --no-hardlinks . clone_dir && |
||||
cd clone_dir && |
||||
git show HEAD~5 | grep "A U Thor" && |
||||
git show $HASH2 | grep "A U Thor" && |
||||
git cat-file commit $R && |
||||
git repack -a -d && |
||||
test_must_fail git cat-file commit $R && |
||||
git fetch ../ "refs/replace/*:refs/replace/*" && |
||||
git show HEAD~5 | grep "O Thor" && |
||||
git show $HASH2 | grep "O Thor" && |
||||
git cat-file commit $R && |
||||
cd .. |
||||
' |
||||
|
||||
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_must_fail git replace -d $R && |
||||
test_must_fail git replace -d && |
||||
test_must_fail git replace -l -d $HASH2 && |
||||
git replace -d $HASH2 && |
||||
git show $HASH2 | grep "A U Thor" && |
||||
test -z "$(git replace -l)" |
||||
' |
||||
|
||||
test_expect_success '"git replace" replacing' ' |
||||
git replace $HASH2 $R && |
||||
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 "$HASH2" = "$(git replace)" |
||||
' |
||||
|
||||
# This creates a side branch where the bug in H2 |
||||
# does not appear because P2 is created by applying |
||||
# H2 and squashing H5 into it. |
||||
# P3, P4 and P6 are created by cherry-picking H3, H4 |
||||
# and H6 respectively. |
||||
# |
||||
# At this point, we should have the following: |
||||
# |
||||
# P2--P3--P4--P6 |
||||
# / |
||||
# H1-H2-H3-H4-H5-H6-H7 |
||||
# |
||||
# Then we replace H6 with P6. |
||||
# |
||||
test_expect_success 'create parallel branch without the bug' ' |
||||
git replace -d $HASH2 && |
||||
git show $HASH2 | grep "A U Thor" && |
||||
git checkout $HASH1 && |
||||
git cherry-pick $HASH2 && |
||||
git show $HASH5 | git apply && |
||||
git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello && |
||||
PARA2=$(git rev-parse --verify HEAD) && |
||||
git cherry-pick $HASH3 && |
||||
PARA3=$(git rev-parse --verify HEAD) && |
||||
git cherry-pick $HASH4 && |
||||
PARA4=$(git rev-parse --verify HEAD) && |
||||
git cherry-pick $HASH6 && |
||||
PARA6=$(git rev-parse --verify HEAD) && |
||||
git replace $HASH6 $PARA6 && |
||||
git checkout master && |
||||
cur=$(git rev-parse --verify HEAD) && |
||||
test "$cur" = "$HASH7" && |
||||
git log --pretty=oneline | grep $PARA2 && |
||||
git remote add cloned ./clone_dir |
||||
' |
||||
|
||||
test_expect_success 'push to cloned repo' ' |
||||
git push cloned $HASH6^:refs/heads/parallel && |
||||
cd clone_dir && |
||||
git checkout parallel && |
||||
git log --pretty=oneline | grep $PARA2 && |
||||
cd .. |
||||
' |
||||
|
||||
test_expect_success 'push branch with replacement' ' |
||||
git cat-file commit $PARA3 | grep "author A U Thor" && |
||||
S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) && |
||||
git cat-file commit $S | grep "author O Thor" && |
||||
git replace $PARA3 $S && |
||||
git show $HASH6~2 | grep "O Thor" && |
||||
git show $PARA3 | grep "O Thor" && |
||||
git push cloned $HASH6^:refs/heads/parallel2 && |
||||
cd clone_dir && |
||||
git checkout parallel2 && |
||||
git log --pretty=oneline | grep $PARA3 && |
||||
git show $PARA3 | grep "A U Thor" && |
||||
cd .. |
||||
' |
||||
|
||||
test_expect_success 'fetch branch with replacement' ' |
||||
git branch tofetch $HASH6 && |
||||
cd clone_dir && |
||||
git fetch origin refs/heads/tofetch:refs/heads/parallel3 |
||||
git log --pretty=oneline parallel3 | grep $PARA3 |
||||
git show $PARA3 | grep "A U Thor" |
||||
cd .. |
||||
' |
||||
|
||||
# |
||||
# |
||||
test_done |
Loading…
Reference in new issue