@@ -10,6 +10,7 @@ import (
10
10
"compress/gzip"
11
11
"context"
12
12
"crypto/sha256"
13
+ "errors"
13
14
"fmt"
14
15
"io"
15
16
"io/ioutil"
@@ -348,67 +349,70 @@ type FakeGerrit struct {
348
349
}
349
350
350
351
type FakeRepo struct {
351
- t * testing.T
352
- name string
353
- seq int
354
- history []string // oldest to newest
355
- content map [string ]map [string ]string
356
- tags map [string ]string
352
+ t * testing.T
353
+ name string
354
+ dir * GitDir
357
355
}
358
356
359
357
func NewFakeRepo (t * testing.T , name string ) * FakeRepo {
360
- return & FakeRepo {
361
- t : t ,
362
- name : name ,
363
- content : map [string ]map [string ]string {},
364
- tags : map [string ]string {},
358
+ if _ , err := exec .LookPath ("git" ); errors .Is (err , exec .ErrNotFound ) {
359
+ t .Skip ("test requires git" )
365
360
}
361
+
362
+ r := & FakeRepo {
363
+ t : t ,
364
+ name : name ,
365
+ dir : & GitDir {& Git {}, t .TempDir ()},
366
+ }
367
+ t .Cleanup (func () { r .dir .Close () })
368
+ r .runGit ("init" , "-b" , "master" )
369
+ r .runGit ("commit" , "--allow-empty" , "--allow-empty-message" , "-m" , "" )
370
+ return r
371
+ }
372
+
373
+ func (repo * FakeRepo ) runGit (args ... string ) []byte {
374
+ repo .t .Helper ()
375
+ out , err := repo .dir .RunCommand (context .Background (), args ... )
376
+ if err != nil {
377
+ repo .t .Fatal (err )
378
+ }
379
+ return out
366
380
}
367
381
368
382
func (repo * FakeRepo ) Commit (contents map [string ]string ) string {
369
- rev := fmt . Sprintf ( "%v~%v " , repo . name , repo . seq )
370
- repo . seq ++
383
+ return repo . CommitOnBranch ( "master " , contents )
384
+ }
371
385
372
- newContent := map [string ]string {}
373
- if len (repo .history ) != 0 {
374
- for k , v := range repo .content [repo .history [len (repo .history )- 1 ]] {
375
- newContent [k ] = v
376
- }
377
- }
386
+ func (repo * FakeRepo ) CommitOnBranch (branch string , contents map [string ]string ) string {
387
+ repo .runGit ("switch" , branch )
378
388
for k , v := range contents {
379
- newContent [k ] = v
389
+ full := filepath .Join (repo .dir .dir , k )
390
+ if err := os .MkdirAll (filepath .Dir (full ), 0777 ); err != nil {
391
+ repo .t .Fatal (err )
392
+ }
393
+ if err := os .WriteFile (full , []byte (v ), 0777 ); err != nil {
394
+ repo .t .Fatal (err )
395
+ }
380
396
}
381
- repo .content [rev ] = newContent
382
- repo .history = append (repo .history , rev )
383
- return rev
397
+ repo .runGit ("add" , "." )
398
+ repo .runGit ("commit" , "--allow-empty-message" , "-m" , "" )
399
+ return strings .TrimSpace (string (repo .runGit ("rev-parse" , "HEAD" )))
400
+ }
401
+
402
+ func (repo * FakeRepo ) History () []string {
403
+ return strings .Split (string (repo .runGit ("log" , "--format=%H" )), "\n " )
384
404
}
385
405
386
406
func (repo * FakeRepo ) Tag (tag , commit string ) {
387
- if _ , ok := repo .content [commit ]; ! ok {
388
- repo .t .Fatalf ("commit %q does not exist on repo %q" , commit , repo .name )
389
- }
390
- if _ , ok := repo .tags [tag ]; ok {
391
- repo .t .Fatalf ("tag %q already exists on repo %q" , commit , repo .name )
392
- }
393
- repo .tags [tag ] = commit
407
+ repo .runGit ("tag" , tag , commit )
394
408
}
395
409
396
- // GetRepoContent returns the content of repo based on the value of commit:
397
- // - commit is "master": return content of the most recent revision
398
- // - commit is tag: return content of the repo associating with the commit that the tag maps to
399
- // - commit is neither "master" or tag: return content of the repo associated with that commit
400
- func (repo * FakeRepo ) GetRepoContent (commit string ) (map [string ]string , error ) {
401
- rev := commit
402
- if commit == "master" {
403
- l := len (repo .history )
404
- if l == 0 {
405
- return nil , fmt .Errorf ("repo %v history is empty" , repo .name )
406
- }
407
- rev = repo .history [l - 1 ]
408
- } else if val , ok := repo .tags [commit ]; ok {
409
- rev = val
410
- }
411
- return repo .content [rev ], nil
410
+ func (repo * FakeRepo ) Branch (branch , commit string ) {
411
+ repo .runGit ("branch" , branch , commit )
412
+ }
413
+
414
+ func (repo * FakeRepo ) ReadFile (commit , file string ) ([]byte , error ) {
415
+ return repo .dir .RunCommand (context .Background (), "show" , commit + ":" + file )
412
416
}
413
417
414
418
var _ GerritClient = (* FakeGerrit )(nil )
@@ -434,55 +438,42 @@ func (g *FakeGerrit) ReadBranchHead(ctx context.Context, project, branch string)
434
438
if err != nil {
435
439
return "" , err
436
440
}
437
- return repo .history [len (repo .history )- 1 ], nil
441
+ out , err := repo .dir .RunCommand (ctx , "rev-parse" , "refs/heads/" + branch )
442
+ return strings .TrimSpace (string (out )), err
438
443
}
439
444
440
445
func (g * FakeGerrit ) ReadFile (ctx context.Context , project string , commit string , file string ) ([]byte , error ) {
441
446
repo , err := g .repo (project )
442
447
if err != nil {
443
448
return nil , err
444
449
}
445
- repoContent , err := repo .GetRepoContent (commit )
446
- if err != nil {
447
- return nil , err
448
- }
449
- fileContent := repoContent [file ]
450
- if fileContent == "" {
451
- return nil , fmt .Errorf ("commit/file not found %v at %v: %w" , file , commit , gerrit .ErrResourceNotExist )
452
- }
453
- return []byte (fileContent ), nil
450
+ return repo .ReadFile (commit , file )
454
451
}
455
452
456
453
func (g * FakeGerrit ) ListTags (ctx context.Context , project string ) ([]string , error ) {
457
454
repo , err := g .repo (project )
458
455
if err != nil {
459
456
return nil , err
460
457
}
461
- var tags []string
462
- for k := range repo .tags {
463
- tags = append (tags , k )
464
- }
465
- return tags , nil
458
+ out , err := repo .dir .RunCommand (ctx , "tag" , "-l" )
459
+ return strings .Split (strings .TrimSpace (string (out )), "\n " ), err
466
460
}
467
461
468
462
func (g * FakeGerrit ) GetTag (ctx context.Context , project string , tag string ) (gerrit.TagInfo , error ) {
469
463
repo , err := g .repo (project )
470
464
if err != nil {
471
465
return gerrit.TagInfo {}, err
472
466
}
473
- if commit , ok := repo .tags [tag ]; ok {
474
- return gerrit.TagInfo {Revision : commit }, nil
475
- } else {
476
- return gerrit.TagInfo {}, fmt .Errorf ("tag not found: %w" , gerrit .ErrResourceNotExist )
477
- }
467
+ out , err := repo .dir .RunCommand (ctx , "rev-parse" , "refs/tags/" + tag )
468
+ return gerrit.TagInfo {Revision : strings .TrimSpace (string (out ))}, err
478
469
}
479
470
480
471
func (g * FakeGerrit ) CreateAutoSubmitChange (_ * wf.TaskContext , input gerrit.ChangeInput , reviewers []string , contents map [string ]string ) (string , error ) {
481
472
repo , err := g .repo (input .Project )
482
473
if err != nil {
483
474
return "" , err
484
475
}
485
- commit := repo .Commit ( contents )
476
+ commit := repo .CommitOnBranch ( input . Branch , contents )
486
477
return "cl_" + commit , nil
487
478
}
488
479
@@ -504,9 +495,23 @@ func (g *FakeGerrit) GetCommitsInRefs(ctx context.Context, project string, commi
504
495
if err != nil {
505
496
return nil , err
506
497
}
498
+ refSet := map [string ]bool {}
499
+ for _ , ref := range refs {
500
+ refSet [ref ] = true
501
+ }
502
+
507
503
result := map [string ][]string {}
508
- for _ , commit := range repo .history {
509
- result [commit ] = []string {"master" }
504
+ for _ , commit := range commits {
505
+ out , err := repo .dir .RunCommand (ctx , "branch" , "--format=%(refname)" , "--contains=" + commit )
506
+ if err != nil {
507
+ return nil , err
508
+ }
509
+ for _ , branch := range strings .Split (strings .TrimSpace (string (out )), "\n " ) {
510
+ branch := strings .TrimSpace (branch )
511
+ if refSet [branch ] {
512
+ result [commit ] = append (result [commit ], branch )
513
+ }
514
+ }
510
515
}
511
516
return result , nil
512
517
}
@@ -527,12 +532,12 @@ func (g *FakeGerrit) serveHTTP(w http.ResponseWriter, r *http.Request) {
527
532
return
528
533
}
529
534
rev := strings .TrimSuffix (parts [3 ], ".tar.gz" )
530
- repoContent , err := repo .GetRepoContent ( rev )
535
+ archive , err := repo .dir . RunCommand ( r . Context (), "archive" , "--format=tgz" , rev )
531
536
if err != nil {
532
537
w .WriteHeader (http .StatusInternalServerError )
533
538
return
534
539
}
535
- ServeTarball ( "" , repoContent , w , r )
540
+ http . ServeContent ( w , r , parts [ 3 ], time . Now (), bytes . NewReader ( archive ) )
536
541
}
537
542
538
543
func (* FakeGerrit ) QueryChanges (_ context.Context , query string ) ([]* gerrit.ChangeInfo , error ) {
0 commit comments