From e577f2fff38957316a2b6d584cad9bc7abaf732b Mon Sep 17 00:00:00 2001
From: Unknwon <joe2010xtmf@163.com>
Date: Thu, 4 Dec 2014 23:07:51 -0500
Subject: [PATCH] fix #706 and other mirror improve

---
 README.md      |  1 +
 README_ZH.md   |  1 +
 models/repo.go | 62 ++++++++++++++++++++++++++++++++------------------
 3 files changed, 42 insertions(+), 22 deletions(-)

diff --git a/README.md b/README.md
index 0501d7efba..716be12a07 100644
--- a/README.md
+++ b/README.md
@@ -26,6 +26,7 @@ The goal of this project is to make the easiest, fastest and most painless way t
 - See [Trello Board](https://trello.com/b/uxAoeLUl/gogs-go-git-service) to follow the develop team.
 - Try it before anything? Do it [online](https://try.gogs.io/Unknown/gogs) or go down to **Installation -> Install from binary** section!
 - Having troubles? Get help from [Troubleshooting](http://gogs.io/docs/intro/troubleshooting.md).
+- Want to help on localization? Check out [Crowdin](https://crowdin.com/project/gogs)!
 
 ## Features
 
diff --git a/README_ZH.md b/README_ZH.md
index 6facfeb802..c9982c9623 100644
--- a/README_ZH.md
+++ b/README_ZH.md
@@ -17,6 +17,7 @@ Gogs 的目标是打造一个最简单、最快速和最轻松的方式搭建自
 - 您可以到 [Trello Board](https://trello.com/b/uxAoeLUl/gogs-go-git-service) 跟随开发团队的脚步。
 - 想要先睹为快?通过 [在线体验](https://try.gogs.io/Unknown/gogs) 或查看 **安装部署 -> 二进制安装** 小节。
 - 使用过程中遇到问题?尝试从 [故障排查](http://gogs.io/docs/intro/troubleshooting.md) 页面获取帮助。
+- 希望帮助多国语言界面的翻译吗?请立即访问 [Crowdin](https://crowdin.com/project/gogs)!
 
 ## 功能特性
 
diff --git a/models/repo.go b/models/repo.go
index 5676a443ff..f2f810eb7e 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -308,28 +308,6 @@ func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) er
 	return nil
 }
 
-// MirrorUpdate checks and updates mirror repositories.
-func MirrorUpdate() {
-	if err := x.Iterate(new(Mirror), func(idx int, bean interface{}) error {
-		m := bean.(*Mirror)
-		if m.NextUpdate.After(time.Now()) {
-			return nil
-		}
-
-		repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git")
-		if _, stderr, err := process.ExecDir(10*time.Minute,
-			repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
-			"git", "remote", "update"); err != nil {
-			return errors.New("git remote update: " + stderr)
-		}
-
-		m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
-		return UpdateMirror(m)
-	}); err != nil {
-		log.Error(4, "repo.MirrorUpdate: %v", err)
-	}
-}
-
 // MigrateRepository migrates a existing repository from other project hosting.
 func MigrateRepository(u *User, name, desc string, private, mirror bool, url string) (*Repository, error) {
 	repo, err := CreateRepository(u, name, desc, "", "", private, mirror, false)
@@ -1173,8 +1151,48 @@ func DeleteRepositoryArchives() error {
 		})
 }
 
+var (
+	// Prevent duplicate tasks.
+	isMirrorUpdating = false
+	isGitFscking     = false
+)
+
+// MirrorUpdate checks and updates mirror repositories.
+func MirrorUpdate() {
+	if isMirrorUpdating {
+		return
+	}
+	isMirrorUpdating = true
+	defer func() { isMirrorUpdating = false }()
+
+	if err := x.Iterate(new(Mirror), func(idx int, bean interface{}) error {
+		m := bean.(*Mirror)
+		if m.NextUpdate.After(time.Now()) {
+			return nil
+		}
+
+		repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git")
+		if _, stderr, err := process.ExecDir(10*time.Minute,
+			repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
+			"git", "remote", "update"); err != nil {
+			return errors.New("git remote update: " + stderr)
+		}
+
+		m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
+		return UpdateMirror(m)
+	}); err != nil {
+		log.Error(4, "repo.MirrorUpdate: %v", err)
+	}
+}
+
 // GitFsck calls 'git fsck' to check repository health.
 func GitFsck() {
+	if isGitFscking {
+		return
+	}
+	isGitFscking = true
+	defer func() { isGitFscking = false }()
+
 	args := append([]string{"fsck"}, setting.GitFsckArgs...)
 	if err := x.Where("id > 0").Iterate(new(Repository),
 		func(idx int, bean interface{}) error {