Skip to content

Commit ba01dc0

Browse files
committed
Use git plumbing for upload: go-gitea#5621 repo_editor.go: GetDiffPreview
1 parent 525f0b6 commit ba01dc0

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

models/repo_editor.go

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"mime/multipart"
1413
"os"
1514
"os/exec"
1615
"path"
17-
"path/filepath"
1816
"strings"
1917
"time"
2018

@@ -398,43 +396,27 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
398396
return nil
399397
}
400398

401-
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
402-
func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
403-
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
404-
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
405-
406-
if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
407-
return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
408-
} else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
409-
return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
410-
}
411-
412-
localPath := repo.LocalCopyPath()
413-
filePath := path.Join(localPath, treePath)
414-
dir := filepath.Dir(filePath)
415-
416-
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
417-
return nil, fmt.Errorf("Failed to create dir %s: %v", dir, err)
418-
}
399+
func (repo *Repository) diffIndex(repoPath string) (diff *Diff, err error) {
400+
timeout := 5 * time.Minute
401+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
402+
defer cancel()
419403

420-
if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
421-
return nil, fmt.Errorf("WriteFile: %v", err)
422-
}
404+
stdErr := new(bytes.Buffer)
423405

424-
cmd := exec.Command("git", "diff", treePath)
425-
cmd.Dir = localPath
426-
cmd.Stderr = os.Stderr
406+
cmd := exec.CommandContext(ctx, "git", "diff-index", "--cached", "-p", "HEAD")
407+
cmd.Dir = repoPath
408+
cmd.Stderr = stdErr
427409

428410
stdout, err := cmd.StdoutPipe()
429411
if err != nil {
430-
return nil, fmt.Errorf("StdoutPipe: %v", err)
412+
return nil, fmt.Errorf("StdoutPipe: %v stderr %s", err, stdErr.String())
431413
}
432414

433415
if err = cmd.Start(); err != nil {
434-
return nil, fmt.Errorf("Start: %v", err)
416+
return nil, fmt.Errorf("Start: %v stderr %s", err, stdErr.String())
435417
}
436418

437-
pid := process.GetManager().Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
419+
pid := process.GetManager().Add(fmt.Sprintf("diffIndex [repo_path: %s]", repo.RepoPath()), cmd)
438420
defer process.GetManager().Remove(pid)
439421

440422
diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
@@ -449,6 +431,41 @@ func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *
449431
return diff, nil
450432
}
451433

434+
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
435+
func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
436+
timeStr := com.ToStr(time.Now().Nanosecond()) // SHOULD USE SOMETHING UNIQUE
437+
tmpBasePath := path.Join(LocalCopyPath(), "upload-"+timeStr+".git")
438+
if err := os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm); err != nil {
439+
return nil, fmt.Errorf("Failed to create dir %s: %v", tmpBasePath, err)
440+
}
441+
442+
defer os.RemoveAll(path.Dir(tmpBasePath))
443+
444+
// Do a bare shared clone into tmpBasePath and
445+
// make HEAD to point to the branch tree
446+
if err := repo.bareClone(tmpBasePath, branch); err != nil {
447+
return nil, fmt.Errorf("GetDiffPreview: %v", err)
448+
}
449+
450+
// Set the default index
451+
if err := repo.setDefaultIndex(tmpBasePath); err != nil {
452+
return nil, fmt.Errorf("GetDiffPreview: %v", err)
453+
}
454+
455+
// Add the object to the database
456+
objectHash, err := repo.hashObject(tmpBasePath, strings.NewReader(content))
457+
if err != nil {
458+
return nil, fmt.Errorf("GetDiffPreview: %v", err)
459+
}
460+
461+
// Add the object to the index
462+
if err := repo.addObjectToIndex(tmpBasePath, "100666", objectHash, treePath); err != nil {
463+
return nil, fmt.Errorf("GetDiffPreview: %v", err)
464+
}
465+
466+
return repo.diffIndex(tmpBasePath)
467+
}
468+
452469
// ________ .__ __ ___________.__.__
453470
// \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
454471
// | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \

0 commit comments

Comments
 (0)