From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: git@vger.kernel.org, gitster@pobox.com, pasky@suse.cz
Subject: [PATCH] git mv: try harder to keep index entries intact
Date: Fri, 1 Aug 2008 18:49:57 +0200 (CEST) [thread overview]
Message-ID: <alpine.DEB.1.00.0808011847560.9611@pacific.mpi-cbg.de.mpi-cbg.de> (raw)
Some filesystems change the ctime during a rename(), for technical
reasons. Since this is the only change, the contents need not be
rehashed. So just update the ctime after renaming the entry.
This change requires rename_index_entry_at() to return the new
position.
As git-mv assumes that it runs in a non-bare repository, and is
marked as such in the cmd_struct in git.c, the changes do not have
to be guarded against running in a bare repository.
To test this properly, you need to run t7001 with the environment
variable TEST_CTIME_WITH_SLEEP set non-empty, since there is no way
to manipulate the ctime directly; we have to sleep.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
builtin-mv.c | 15 +++++++++++++--
cache.h | 2 +-
read-cache.c | 4 ++--
t/t7001-mv.sh | 10 ++++++++--
4 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/builtin-mv.c b/builtin-mv.c
index 4f65b5a..166a019 100644
--- a/builtin-mv.c
+++ b/builtin-mv.c
@@ -214,8 +214,19 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
pos = cache_name_pos(src, strlen(src));
assert(pos >= 0);
- if (!show_only)
- rename_cache_entry_at(pos, dst);
+ if (!show_only) {
+ struct stat st;
+ pos = rename_cache_entry_at(pos, dst);
+
+ /*
+ * Renaming can update the ctime. Do not force
+ * a complete rehash just because of that.
+ */
+ if (!lstat(dst, &st))
+ active_cache[pos]->ce_ctime = st.st_ctime;
+ else if (!ignore_errors)
+ die ("Could not stat '%s'", dst);
+ }
}
if (active_cache_changed) {
diff --git a/cache.h b/cache.h
index 8155ab8..4b6876b 100644
--- a/cache.h
+++ b/cache.h
@@ -371,7 +371,7 @@ extern int index_name_pos(const struct index_state *, const char *name, int name
#define ADD_CACHE_JUST_APPEND 8 /* Append only; tree.c::read_tree() */
extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
-extern void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
+extern int rename_index_entry_at(struct index_state *, int pos, const char *new_name);
extern int remove_index_entry_at(struct index_state *, int pos);
extern int remove_file_from_index(struct index_state *, const char *path);
#define ADD_CACHE_VERBOSE 1
diff --git a/read-cache.c b/read-cache.c
index c5aa5bc..4454686 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -43,7 +43,7 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache
istate->cache_changed = 1;
}
-void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
+int rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
{
struct cache_entry *old = istate->cache[nr], *new;
int namelen = strlen(new_name);
@@ -56,7 +56,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n
cache_tree_invalidate_path(istate->cache_tree, old->name);
remove_index_entry_at(istate, nr);
- add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
+ return add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
}
/*
diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
index 910a28c..5f6cee5 100755
--- a/t/t7001-mv.sh
+++ b/t/t7001-mv.sh
@@ -180,12 +180,15 @@ test_expect_success 'git mv should overwrite symlink to a file' '
echo 1 >moved &&
ln -s moved symlink &&
git add moved symlink &&
+ if test ! -z "$TEST_CTIME_WITH_SLEEP"
+ then
+ sleep 1
+ fi &&
test_must_fail git mv moved symlink &&
git mv -f moved symlink &&
! test -e moved &&
test -f symlink &&
test "$(cat symlink)" = 1 &&
- git update-index --refresh &&
git diff-files --quiet
'
@@ -199,11 +202,14 @@ test_expect_success 'git mv should overwrite file with a symlink' '
echo 1 >moved &&
ln -s moved symlink &&
git add moved symlink &&
+ if test ! -z "$TEST_CTIME_WITH_SLEEP"
+ then
+ sleep 1
+ fi &&
test_must_fail git mv symlink moved &&
git mv -f symlink moved &&
! test -e symlink &&
test -h moved &&
- git update-index --refresh &&
git diff-files --quiet
'
--
1.6.0.rc1.55.g69db8
reply other threads:[~2008-08-01 16:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.1.00.0808011847560.9611@pacific.mpi-cbg.de.mpi-cbg.de \
--to=johannes.schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pasky@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).