@@ -9,12 +9,10 @@ import (
9
9
"context"
10
10
"fmt"
11
11
"io"
12
- "io/ioutil"
13
12
"mime/multipart"
14
13
"os"
15
14
"os/exec"
16
15
"path"
17
- "path/filepath"
18
16
"strings"
19
17
"time"
20
18
@@ -398,43 +396,27 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
398
396
return nil
399
397
}
400
398
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 ()
419
403
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 )
423
405
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
427
409
428
410
stdout , err := cmd .StdoutPipe ()
429
411
if err != nil {
430
- return nil , fmt .Errorf ("StdoutPipe: %v" , err )
412
+ return nil , fmt .Errorf ("StdoutPipe: %v stderr %s " , err , stdErr . String () )
431
413
}
432
414
433
415
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 () )
435
417
}
436
418
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 )
438
420
defer process .GetManager ().Remove (pid )
439
421
440
422
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 *
449
431
return diff , nil
450
432
}
451
433
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
+
452
469
// ________ .__ __ ___________.__.__
453
470
// \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
454
471
// | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
0 commit comments