From 547e7e9cf0c99b0a5d84611498a52689a068de76 Mon Sep 17 00:00:00 2001 From: Alex Dergachev Date: Mon, 8 Dec 2014 22:23:05 +0000 Subject: [PATCH] Avoid repoContr#show 404 if "Fetch changesets automatically"; fixes #6 --- app/models/repository/git_remote.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/models/repository/git_remote.rb b/app/models/repository/git_remote.rb index 3e5fb5a..ae18f42 100644 --- a/app/models/repository/git_remote.rb +++ b/app/models/repository/git_remote.rb @@ -36,14 +36,32 @@ class Repository::GitRemote < Repository::Git return p[:host] end - # hook into Repository.fetch_changesets to also run 'git fetch' + # Hook into Repository.fetch_changesets to also run 'git fetch'. def fetch_changesets + # ensure we don't fetch twice during the same request + return if @already_fetched + @already_fetched = true + puts "Calling fetch changesets on #{clone_path}" # runs git fetch self.fetch super end + # Override default_branch to fetch, otherwise caching problems in + # find_project_repository prevent Repository::Git#fetch_changesets from running. + # + # Ideally this would only be run for RepositoriesController#show. + def default_branch + if self.branches == [] && self.project.active? && Setting.autofetch_changesets? + # git_adapter#branches caches @branches incorrectly, reset it + scm.instance_variable_set :@branches, nil + # NB: fetch_changesets is idemptotent during a given request, so OK to call it 2x + self.fetch_changesets + end + super + end + # called in before_validate handler, sets form errors def initialize_clone # avoids crash in RepositoriesController#destroy