@@ -34,57 +34,8 @@ type FileChange struct {
3434 NewContent string
3535}
3636
37- // initializeGitClone initializes git's configuration, clones a given repository, and sets up the given branch.
38- // Returns the repository path, a cleanup function which should be deferred, and an error, if any occurred.
39- func initializeGit (t * testing.T , opts * Opts ) (string , func (), error ) {
40- tmpdir := fs .NewDir (t , t .Name ())
41- fixPwd := env .ChangeWorkingDir (t , tmpdir .Path ())
42- cleanupFunc := func () {
43- fixPwd ()
44- if os .Getenv ("TEST_NOCLEANUP" ) == "" {
45- tmpdir .Remove ()
46- }
47- }
48- path := tmpdir .Path ()
49-
50- var err error
51-
52- if _ , err = git .RunGit (path , "init" ); err != nil {
53- return "" , func () {}, err
54- }
55- if _ , err = git .RunGit (path , "config" , "user.name" , "OpenShift Pipelines E2E test" ); err != nil {
56- return "" , func () {}, err
57- }
58- if _ , err = git .RunGit (path , "config" , "user.email" , "e2e-pipeline@redhat.com" ); err != nil {
59- return "" , func () {}, err
60- }
61-
62- if _ , err = git .RunGit (path , "remote" , "add" , "-f" , "origin" , opts .GitURL ); err != nil {
63- return "" , func () {}, err
64- }
65-
66- if _ , err = git .RunGit (path , "fetch" , "-a" , "origin" ); err != nil {
67- return "" , func () {}, err
68- }
69-
70- if strings .HasPrefix (opts .TargetRefName , "refs/tags" ) {
71- _ , err = git .RunGit (path , "reset" , "--hard" , "origin/" + opts .BaseRefName )
72- } else {
73- if opts .NoCheckOutFromBase {
74- // Create a new branch without the base reference,
75- // which can be helpful for testing when you only want to add specific requested files
76- _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName )
77- } else {
78- // checkout new branch from base branch
79- _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName , "origin/" + opts .BaseRefName )
80- }
81- }
82- return path , cleanupFunc , err
83- }
84-
8537// gitPushPullRetry tries to push the files to the repo, if it fails it will try to rebase and push again.
86- // Returns the sha of the commit pushed.
87- func gitPushPullRetry (t * testing.T , opts * Opts , path string ) string {
38+ func gitPushPullRetry (t * testing.T , opts * Opts , path string ) {
8839 // use a loop to try multiple times in case of error
8940 var err error
9041 count := 0
@@ -97,12 +48,7 @@ func gitPushPullRetry(t *testing.T, opts *Opts, path string) string {
9748 opts .Log .Infof ("Pushed files to repo %s branch %s" , opts .WebURL , opts .TargetRefName )
9849 // trying to avoid the multiple events at the time of creation we have a sync
9950 time .Sleep (5 * time .Second )
100-
101- // get sha
102- sha , err := git .RunGit (path , "rev-parse" , "HEAD" )
103- assert .NilError (t , err )
104-
105- return sha
51+ return
10652 }
10753 if strings .Contains (err .Error (), "non-fast-forward" ) {
10854 _ , err = git .RunGit (path , "fetch" , "-a" , "origin" )
@@ -123,8 +69,40 @@ func gitPushPullRetry(t *testing.T, opts *Opts, path string) string {
12369}
12470
12571func PushFilesToRefGit (t * testing.T , opts * Opts , entries map [string ]string ) string {
126- path , cleanupFunc , err := initializeGit (t , opts )
127- defer cleanupFunc ()
72+ tmpdir := fs .NewDir (t , t .Name ())
73+ defer (func () {
74+ if os .Getenv ("TEST_NOCLEANUP" ) == "" {
75+ tmpdir .Remove ()
76+ }
77+ })()
78+ defer env .ChangeWorkingDir (t , tmpdir .Path ())()
79+ path := tmpdir .Path ()
80+ _ , err := git .RunGit (path , "init" )
81+ assert .NilError (t , err )
82+
83+ _ , err = git .RunGit (path , "config" , "user.name" , "OpenShift Pipelines E2E test" )
84+ assert .NilError (t , err )
85+ _ , err = git .RunGit (path , "config" , "user.email" , "e2e-pipeline@redhat.com" )
86+ assert .NilError (t , err )
87+
88+ _ , err = git .RunGit (path , "remote" , "add" , "-f" , "origin" , opts .GitURL )
89+ assert .NilError (t , err )
90+
91+ _ , err = git .RunGit (path , "fetch" , "-a" , "origin" )
92+ assert .NilError (t , err )
93+
94+ if strings .HasPrefix (opts .TargetRefName , "refs/tags" ) {
95+ _ , err = git .RunGit (path , "reset" , "--hard" , "origin/" + opts .BaseRefName )
96+ } else {
97+ if opts .NoCheckOutFromBase {
98+ // Create a new branch without the base reference,
99+ // which can be helpful for testing when you only want to add specific requested files
100+ _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName )
101+ } else {
102+ // checkout new branch from base branch
103+ _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName , "origin/" + opts .BaseRefName )
104+ }
105+ }
128106 assert .NilError (t , err )
129107
130108 for filename , content := range entries {
@@ -147,12 +125,42 @@ func PushFilesToRefGit(t *testing.T, opts *Opts, entries map[string]string) stri
147125 assert .NilError (t , err )
148126 }
149127
150- return gitPushPullRetry (t , opts , path )
128+ // get sha
129+ sha , err := git .RunGit (path , "rev-parse" , "HEAD" )
130+ assert .NilError (t , err )
131+
132+ gitPushPullRetry (t , opts , path )
133+ return sha
151134}
152135
153136func ChangeFilesRefGit (t * testing.T , opts * Opts , fileChanges []FileChange ) {
154- path , cleanupFunc , err := initializeGit (t , opts )
155- defer cleanupFunc ()
137+ tmpdir := fs .NewDir (t , t .Name ())
138+ defer (func () {
139+ if os .Getenv ("TEST_NOCLEANUP" ) == "" {
140+ tmpdir .Remove ()
141+ }
142+ })()
143+ defer env .ChangeWorkingDir (t , tmpdir .Path ())()
144+ path := tmpdir .Path ()
145+ _ , err := git .RunGit (path , "init" )
146+ assert .NilError (t , err )
147+
148+ _ , err = git .RunGit (path , "config" , "user.name" , "OpenShift Pipelines E2E test" )
149+ assert .NilError (t , err )
150+ _ , err = git .RunGit (path , "config" , "user.email" , "e2e-pipeline@redhat.com" )
151+ assert .NilError (t , err )
152+
153+ _ , err = git .RunGit (path , "remote" , "add" , "-f" , "origin" , opts .GitURL )
154+ assert .NilError (t , err )
155+
156+ _ , err = git .RunGit (path , "fetch" , "-a" , "origin" )
157+ assert .NilError (t , err )
158+
159+ if strings .HasPrefix (opts .TargetRefName , "refs/tags" ) {
160+ _ , err = git .RunGit (path , "reset" , "--hard" , "origin/" + opts .BaseRefName )
161+ } else {
162+ _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName , "origin/" + opts .BaseRefName )
163+ }
156164 assert .NilError (t , err )
157165
158166 for _ , fileChange := range fileChanges {
0 commit comments