aboutsummaryrefslogtreecommitdiffstats
path: root/services/mirror
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-01-19 23:26:57 +0000
committerGitHub <noreply@github.com>2022-01-19 23:26:57 +0000
commit5cb0c9aa0d7eed087055b1efca79628957207d36 (patch)
treed117a514e1f17e5f6bfcda1be273f6a971112663 /services/mirror
parent4563148a61ba892e8f2bb66342f00a950bcd5315 (diff)
downloadgitea-5cb0c9aa0d7eed087055b1efca79628957207d36.tar.gz
gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.zip
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'services/mirror')
-rw-r--r--services/mirror/mirror_pull.go24
-rw-r--r--services/mirror/mirror_push.go14
2 files changed, 19 insertions, 19 deletions
diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go
index da2221d915..b9333c77f3 100644
--- a/services/mirror/mirror_pull.go
+++ b/services/mirror/mirror_pull.go
@@ -30,30 +30,30 @@ import (
const gitShortEmptySha = "0000000"
// UpdateAddress writes new address to Git repository and database
-func UpdateAddress(m *repo_model.Mirror, addr string) error {
+func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error {
remoteName := m.GetRemoteName()
repoPath := m.Repo.RepoPath()
// Remove old remote
- _, err := git.NewCommand("remote", "rm", remoteName).RunInDir(repoPath)
+ _, err := git.NewCommandContext(ctx, "remote", "rm", remoteName).RunInDir(repoPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return err
}
- _, err = git.NewCommand("remote", "add", remoteName, "--mirror=fetch", addr).RunInDir(repoPath)
+ _, err = git.NewCommandContext(ctx, "remote", "add", remoteName, "--mirror=fetch", addr).RunInDir(repoPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return err
}
if m.Repo.HasWiki() {
wikiPath := m.Repo.WikiPath()
- wikiRemotePath := repo_module.WikiRemoteURL(addr)
+ wikiRemotePath := repo_module.WikiRemoteURL(ctx, addr)
// Remove old remote of wiki
- _, err := git.NewCommand("remote", "rm", remoteName).RunInDir(wikiPath)
+ _, err := git.NewCommandContext(ctx, "remote", "rm", remoteName).RunInDir(wikiPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return err
}
- _, err = git.NewCommand("remote", "add", remoteName, "--mirror=fetch", wikiRemotePath).RunInDir(wikiPath)
+ _, err = git.NewCommandContext(ctx, "remote", "add", remoteName, "--mirror=fetch", wikiRemotePath).RunInDir(wikiPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return err
}
@@ -250,7 +250,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
}
output := stderrBuilder.String()
- gitRepo, err := git.OpenRepository(repoPath)
+ gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
if err != nil {
log.Error("OpenRepository: %v", err)
return nil, false
@@ -337,7 +337,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
}
log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo)
- branches, _, err := git.GetBranchesByPath(m.Repo.RepoPath(), 0, 0)
+ branches, _, err := git.GetBranchesByPath(ctx, m.Repo.RepoPath(), 0, 0)
if err != nil {
log.Error("GetBranches: %v", err)
return nil, false
@@ -427,7 +427,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
OldCommitID: git.EmptySHA,
NewCommitID: commitID,
}, repo_module.NewPushCommits())
- notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
+ notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName, commitID)
continue
}
@@ -438,12 +438,12 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
}
// Push commits
- oldCommitID, err := git.GetFullCommitID(gitRepo.Path, result.oldCommitID)
+ oldCommitID, err := git.GetFullCommitID(gitRepo.Ctx, gitRepo.Path, result.oldCommitID)
if err != nil {
log.Error("GetFullCommitID [%d]: %v", m.RepoID, err)
continue
}
- newCommitID, err := git.GetFullCommitID(gitRepo.Path, result.newCommitID)
+ newCommitID, err := git.GetFullCommitID(gitRepo.Ctx, gitRepo.Path, result.newCommitID)
if err != nil {
log.Error("GetFullCommitID [%d]: %v", m.RepoID, err)
continue
@@ -470,7 +470,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
// Get latest commit date and update to current repository updated time
- commitDate, err := git.GetLatestCommitTime(m.Repo.RepoPath())
+ commitDate, err := git.GetLatestCommitTime(ctx, m.Repo.RepoPath())
if err != nil {
log.Error("GetLatestCommitDate [%d]: %v", m.RepoID, err)
return false
diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go
index e1c395ea74..9895f0c995 100644
--- a/services/mirror/mirror_push.go
+++ b/services/mirror/mirror_push.go
@@ -26,15 +26,15 @@ import (
var stripExitStatus = regexp.MustCompile(`exit status \d+ - `)
// AddPushMirrorRemote registers the push mirror remote.
-func AddPushMirrorRemote(m *repo_model.PushMirror, addr string) error {
+func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error {
addRemoteAndConfig := func(addr, path string) error {
- if _, err := git.NewCommand("remote", "add", "--mirror=push", m.RemoteName, addr).RunInDir(path); err != nil {
+ if _, err := git.NewCommandContext(ctx, "remote", "add", "--mirror=push", m.RemoteName, addr).RunInDir(path); err != nil {
return err
}
- if _, err := git.NewCommand("config", "--add", "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*").RunInDir(path); err != nil {
+ if _, err := git.NewCommandContext(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*").RunInDir(path); err != nil {
return err
}
- if _, err := git.NewCommand("config", "--add", "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*").RunInDir(path); err != nil {
+ if _, err := git.NewCommandContext(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*").RunInDir(path); err != nil {
return err
}
return nil
@@ -45,7 +45,7 @@ func AddPushMirrorRemote(m *repo_model.PushMirror, addr string) error {
}
if m.Repo.HasWiki() {
- wikiRemoteURL := repository.WikiRemoteURL(addr)
+ wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
if len(wikiRemoteURL) > 0 {
if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil {
return err
@@ -57,8 +57,8 @@ func AddPushMirrorRemote(m *repo_model.PushMirror, addr string) error {
}
// RemovePushMirrorRemote removes the push mirror remote.
-func RemovePushMirrorRemote(m *repo_model.PushMirror) error {
- cmd := git.NewCommand("remote", "rm", m.RemoteName)
+func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error {
+ cmd := git.NewCommandContext(ctx, "remote", "rm", m.RemoteName)
if _, err := cmd.RunInDir(m.Repo.RepoPath()); err != nil {
return err