Skip to content

Commit

Permalink
Fix clean tmp dir (go-gitea#32360) (go-gitea#32593)
Browse files Browse the repository at this point in the history
Backport go-gitea#32360 

Try to fix go-gitea#31792 

Credit to @jeroenlaylo
Copied from
go-gitea#31792 (comment)

Co-authored-by: wxiaoguang <[email protected]>
  • Loading branch information
lunny and wxiaoguang authored Nov 22, 2024
1 parent 2b8b277 commit 073ba97
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions modules/git/repo_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,35 @@ func (repo *Repository) readTreeToIndex(id ObjectID, indexFilename ...string) er
}

// ReadTreeToTemporaryIndex reads a treeish to a temporary index file
func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename, tmpDir string, cancel context.CancelFunc, err error) {
func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (tmpIndexFilename, tmpDir string, cancel context.CancelFunc, err error) {
defer func() {
// if error happens and there is a cancel function, do clean up
if err != nil && cancel != nil {
cancel()
cancel = nil
}
}()

removeDirFn := func(dir string) func() { // it can't use the return value "tmpDir" directly because it is empty when error occurs
return func() {
if err := util.RemoveAll(dir); err != nil {
log.Error("failed to remove tmp index dir: %v", err)
}
}
}

tmpDir, err = os.MkdirTemp("", "index")
if err != nil {
return filename, tmpDir, cancel, err
return "", "", nil, err
}

filename = filepath.Join(tmpDir, ".tmp-index")
cancel = func() {
err := util.RemoveAll(tmpDir)
if err != nil {
log.Error("failed to remove tmp index file: %v", err)
}
}
err = repo.ReadTreeToIndex(treeish, filename)
tmpIndexFilename = filepath.Join(tmpDir, ".tmp-index")
cancel = removeDirFn(tmpDir)
err = repo.ReadTreeToIndex(treeish, tmpIndexFilename)
if err != nil {
defer cancel()
return "", "", func() {}, err
return "", "", cancel, err
}
return filename, tmpDir, cancel, err
return tmpIndexFilename, tmpDir, cancel, err
}

// EmptyIndex empties the index
Expand Down

0 comments on commit 073ba97

Please sign in to comment.