@@ -32,34 +32,52 @@ func buildAuthURL(repoURL, username, password string) string {
3232 fmt .Sprintf ("http://%s:%s@" , username , password ), 1 )
3333}
3434
35- // InitializeRepoWithFunction clones an empty Gitea repo, initializes a function, and pushes it
35+ // InitializeRepoWithFunction creates a function project and pushes it to the Gitea repo
3636func InitializeRepoWithFunction (repoURL , username , password , language string ) (repoDir string , err error ) {
3737 repoDir = fmt .Sprintf ("%s/func-test-%s" , os .TempDir (), rand .String (10 ))
3838
3939 // Build authenticated URL
4040 authURL := buildAuthURL (repoURL , username , password )
4141
42- // Clone empty repo
43- cmd := exec .Command ("git " , "clone " , authURL , repoDir )
42+ // Initialize function (func init creates the directory)
43+ cmd := exec .Command ("func " , "init " , "-l" , language , repoDir )
4444 if _ , err = utils .Run (cmd ); err != nil {
45- return "" , fmt .Errorf ("failed to clone repo : %w" , err )
45+ return "" , fmt .Errorf ("failed to init function : %w" , err )
4646 }
4747
48- // Initialize function
49- cmd = exec .Command ("func" , "init" , "-l" , language )
50- cmd .Dir = repoDir
48+ // Initialize git repo
49+ cmd = exec .Command ("git" , "-C" , repoDir , "init" )
5150 if _ , err = utils .Run (cmd ); err != nil {
52- return "" , fmt .Errorf ("failed to init function: %w" , err )
51+ return "" , fmt .Errorf ("failed to git init: %w" , err )
52+ }
53+
54+ // Configure git user
55+ cmd = exec .Command ("git" , "-C" , repoDir , "config" , "user.name" , "Test User" )
56+ if _ , err = utils .Run (cmd ); err != nil {
57+ return "" , fmt .Errorf ("failed to set git user.name: %w" , err )
58+ }
59+ cmd = exec .Command ("git" , "-C" , repoDir , "config" , "user.email" , "test@example.com" )
60+ if _ , err = utils .Run (cmd ); err != nil {
61+ return "" , fmt .Errorf ("failed to set git user.email: %w" , err )
62+ }
63+
64+ // Add remote
65+ cmd = exec .Command ("git" , "-C" , repoDir , "remote" , "add" , "origin" , authURL )
66+ if _ , err = utils .Run (cmd ); err != nil {
67+ return "" , fmt .Errorf ("failed to add remote: %w" , err )
5368 }
5469
5570 // Commit and push
56- if err = exec .Command ("git" , "-C" , repoDir , "add" , "." ).Run (); err != nil {
71+ cmd = exec .Command ("git" , "-C" , repoDir , "add" , "." )
72+ if _ , err = utils .Run (cmd ); err != nil {
5773 return "" , fmt .Errorf ("failed to git add: %w" , err )
5874 }
59- if err = exec .Command ("git" , "-C" , repoDir , "commit" , "-m" , "Initial function" ).Run (); err != nil {
75+ cmd = exec .Command ("git" , "-C" , repoDir , "commit" , "-m" , "Initial function" )
76+ if _ , err = utils .Run (cmd ); err != nil {
6077 return "" , fmt .Errorf ("failed to git commit: %w" , err )
6178 }
62- if err = exec .Command ("git" , "-C" , repoDir , "push" ).Run (); err != nil {
79+ cmd = exec .Command ("git" , "-C" , repoDir , "push" , "-u" , "origin" , "main" )
80+ if _ , err = utils .Run (cmd ); err != nil {
6381 return "" , fmt .Errorf ("failed to push initial commit: %w" , err )
6482 }
6583
@@ -70,24 +88,28 @@ func InitializeRepoWithFunction(repoURL, username, password, language string) (r
7088// Requires at least one file to be specified
7189func CommitAndPush (repoDir string , msg string , file string , otherFiles ... string ) error {
7290 // Add first file
73- if err := exec .Command ("git" , "-C" , repoDir , "add" , file ).Run (); err != nil {
91+ cmd := exec .Command ("git" , "-C" , repoDir , "add" , file )
92+ if _ , err := utils .Run (cmd ); err != nil {
7493 return fmt .Errorf ("failed to git add %s: %w" , file , err )
7594 }
7695
7796 // Add other files if provided
7897 for _ , f := range otherFiles {
79- if err := exec .Command ("git" , "-C" , repoDir , "add" , f ).Run (); err != nil {
98+ cmd = exec .Command ("git" , "-C" , repoDir , "add" , f )
99+ if _ , err := utils .Run (cmd ); err != nil {
80100 return fmt .Errorf ("failed to git add %s: %w" , f , err )
81101 }
82102 }
83103
84104 // Commit
85- if err := exec .Command ("git" , "-C" , repoDir , "commit" , "-m" , msg ).Run (); err != nil {
105+ cmd = exec .Command ("git" , "-C" , repoDir , "commit" , "-m" , msg )
106+ if _ , err := utils .Run (cmd ); err != nil {
86107 return fmt .Errorf ("failed to git commit: %w" , err )
87108 }
88109
89110 // Push
90- if err := exec .Command ("git" , "-C" , repoDir , "push" ).Run (); err != nil {
111+ cmd = exec .Command ("git" , "-C" , repoDir , "push" )
112+ if _ , err := utils .Run (cmd ); err != nil {
91113 return fmt .Errorf ("failed to push: %w" , err )
92114 }
93115
0 commit comments