From 759ae8ba1ee3fe7f4e55722cf7886b9e4394ae2f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 5 Oct 2015 20:52:01 +0200 Subject: [PATCH 1/4] Demonstrate a Windows file locking issue with `git clone --dissociate` On Windows, dissociating from a reference can fail very easily due to pack files that are still in use when they want to be removed. Signed-off-by: Johannes Schindelin --- t/t5700-clone-reference.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/t/t5700-clone-reference.sh b/t/t5700-clone-reference.sh index ef1779f5ca..f7cec85e5b 100755 --- a/t/t5700-clone-reference.sh +++ b/t/t5700-clone-reference.sh @@ -188,5 +188,26 @@ test_expect_success 'clone and dissociate from reference' ' test_must_fail git -C R fsck && git -C S fsck ' +test_expect_failure MINGW 'clone, dissociate from partial reference and repack' ' + rm -fr P Q R && + git init P && + ( + cd P && + test_commit one && + git repack && + test_commit two && + git repack + ) && + git clone --bare P Q && + ( + cd P && + git checkout -b second && + test_commit three && + git repack + ) && + git clone --bare --dissociate --reference=P Q R && + ls R/objects/pack/*.pack >packs.txt && + test_line_count = 1 packs.txt +' test_done From 821d6ed120381b87eb5f0d653fd976c964529dcc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 5 Oct 2015 21:04:06 +0200 Subject: [PATCH 2/4] Consolidate code to close a pack's file descriptor There was a lot of repeated code to close the file descriptor of a given pack. Let's just refactor this code into a single function. Signed-off-by: Johannes Schindelin --- sha1_file.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/sha1_file.c b/sha1_file.c index d295a3225a..5d75a327bd 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -786,6 +786,18 @@ void close_pack_windows(struct packed_git *p) } } +static int close_pack_fd(struct packed_git *p) +{ + if (p->pack_fd < 0) + return 0; + + close(p->pack_fd); + pack_open_fds--; + p->pack_fd = -1; + + return 1; +} + /* * The LRU pack is the one with the oldest MRU window, preferring packs * with no used windows, or the oldest mtime if it has no windows allocated. @@ -853,12 +865,8 @@ static int close_one_pack(void) find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse); } - if (lru_p) { - close(lru_p->pack_fd); - pack_open_fds--; - lru_p->pack_fd = -1; - return 1; - } + if (lru_p) + return close_pack_fd(lru_p); return 0; } @@ -898,12 +906,7 @@ void free_pack_by_name(const char *pack_name) p = *pp; if (strcmp(pack_name, p->pack_name) == 0) { clear_delta_base_cache(); - close_pack_windows(p); - if (p->pack_fd != -1) { - close(p->pack_fd); - pack_open_fds--; - } - close_pack_index(p); + close_pack(p); free(p->bad_object_sha1); *pp = p->next; if (last_found_pack == p) @@ -1037,11 +1040,7 @@ static int open_packed_git(struct packed_git *p) { if (!open_packed_git_1(p)) return 0; - if (p->pack_fd != -1) { - close(p->pack_fd); - pack_open_fds--; - p->pack_fd = -1; - } + close_pack_fd(p); return -1; } @@ -1107,11 +1106,8 @@ unsigned char *use_pack(struct packed_git *p, p->pack_name, strerror(errno)); if (!win->offset && win->len == p->pack_size - && !p->do_not_close) { - close(p->pack_fd); - pack_open_fds--; - p->pack_fd = -1; - } + && !p->do_not_close) + close_pack_fd(p); pack_mmap_calls++; pack_open_windows++; if (pack_mapped > peak_pack_mapped) From 02f6850fd499a59ead914a878dcf3df99361ae46 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 5 Oct 2015 21:09:39 +0200 Subject: [PATCH 3/4] Add a function to release all packs On Windows, files that are in use cannot be removed or renamed. That means that we have to release pack files when we are about to, say, repack them. Let's introduce a convenient function to close them pack files. Signed-off-by: Johannes Schindelin --- cache.h | 1 + sha1_file.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/cache.h b/cache.h index 79066e57dc..5a9813b771 100644 --- a/cache.h +++ b/cache.h @@ -1275,6 +1275,7 @@ extern void close_pack_index(struct packed_git *); extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *); extern void close_pack_windows(struct packed_git *); +extern void close_all_packs(void); extern void unuse_pack(struct pack_window **); extern void free_pack_by_name(const char *); extern void clear_delta_base_cache(void); diff --git a/sha1_file.c b/sha1_file.c index 5d75a327bd..fe823fec3f 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -798,6 +798,22 @@ static int close_pack_fd(struct packed_git *p) return 1; } +static void close_pack(struct packed_git *p) +{ + close_pack_windows(p); + close_pack_fd(p); + close_pack_index(p); +} + +void close_all_packs(void) +{ + struct packed_git *p; + + for (p = packed_git; p; p = p->next) + close_pack(p); +} + + /* * The LRU pack is the one with the oldest MRU window, preferring packs * with no used windows, or the oldest mtime if it has no windows allocated. From b4dc11b6c8014652ad0ce35bbe801c26997706a0 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 28 Sep 2015 21:12:31 +0200 Subject: [PATCH 4/4] clone --dissociate: avoid locking pack files When `git clone` is asked to dissociate the repository from the reference repository whose objects were used, it is quite possible that the pack files need to be repacked. In that case, the pack files need to be deleted that were originally hard-links to the reference repository's pack files. On platforms where a file cannot be deleted if another process still holds a handle on it, we therefore need to take pains to release all pack files and indexes before dissociating. This fixes https://github.com/git-for-windows/git/issues/446 The test case to demonstrate the breakage technically does not need to be run on Linux or MacOSX. It won't hurth, either, though. Signed-off-by: Johannes Schindelin --- builtin/clone.c | 4 +++- t/t5700-clone-reference.sh | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index 578da85254..cc896e22d1 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -1064,8 +1064,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix) transport_unlock_pack(transport); transport_disconnect(transport); - if (option_dissociate) + if (option_dissociate) { + close_all_packs(); dissociate_from_references(); + } junk_mode = JUNK_LEAVE_REPO; err = checkout(); diff --git a/t/t5700-clone-reference.sh b/t/t5700-clone-reference.sh index f7cec85e5b..2250ef4fe2 100755 --- a/t/t5700-clone-reference.sh +++ b/t/t5700-clone-reference.sh @@ -188,7 +188,7 @@ test_expect_success 'clone and dissociate from reference' ' test_must_fail git -C R fsck && git -C S fsck ' -test_expect_failure MINGW 'clone, dissociate from partial reference and repack' ' +test_expect_success 'clone, dissociate from partial reference and repack' ' rm -fr P Q R && git init P && (