summaryrefslogtreecommitdiff
path: root/app/models/repository.rb
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2016-08-29 07:46:30 -0700
committerStan Hu <stanhu@gmail.com>2016-09-01 00:08:23 -0700
commitd326d3428da89b943bb5f1d4d396f21b3e999ff7 (patch)
tree218de7018f84bd040671f6fd87b4b760d363f63f /app/models/repository.rb
parent1c4e866348e0c782d2e5374209508785ac812c7a (diff)
downloadgitlab-ce-d326d3428da89b943bb5f1d4d396f21b3e999ff7.tar.gz
Optimize branch lookups and force a repository reload for Repository#find_branchsh-reload-find-branch
If `git gc` runs and `Repository` has an instance to `Rugged::Repository`, a bug in libgit2 may cause the instance to return a stale value or a missing branch. This change not only optimizes the branch lookup so we don't have to iterate through every branch, but it also works around the `git gc` issue by forcing a repository reload every time `Repository#find_branch` is called. See: https://github.com/libgit2/libgit2/issues/1534 Closes #15392, #21470
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r--app/models/repository.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 91bdafdac99..f891e8374d2 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -120,8 +120,21 @@ class Repository
commits
end
- def find_branch(name)
- raw_repository.branches.find { |branch| branch.name == name }
+ def find_branch(name, fresh_repo: true)
+ # Since the Repository object may have in-memory index changes, invalidating the memoized Repository object may
+ # cause unintended side effects. Because finding a branch is a read-only operation, we can safely instantiate
+ # a new repo here to ensure a consistent state to avoid a libgit2 bug where concurrent access (e.g. via git gc)
+ # may cause the branch to "disappear" erroneously or have the wrong SHA.
+ #
+ # See: https://github.com/libgit2/libgit2/issues/1534 and https://gitlab.com/gitlab-org/gitlab-ce/issues/15392
+ raw_repo =
+ if fresh_repo
+ Gitlab::Git::Repository.new(path_to_repo)
+ else
+ raw_repository
+ end
+
+ raw_repo.find_branch(name)
end
def find_tag(name)