@@ -67,6 +67,55 @@ func getApplicationAndOrgIDFromDir(dir string) (string, string, error) {
6767 return appResp .ApplicationID , appResp .OrganizationID , nil
6868}
6969
70+ // getPreferredCloneURL returns the preferred clone URL based on SSH availability
71+ func getPreferredCloneURL (sshURL , httpsURL string ) (url string , method string , err error ) {
72+ if utils .CanUseSSH () && sshURL != "" {
73+ return sshURL , "SSH" , nil
74+ }
75+ if httpsURL != "" {
76+ return httpsURL , "HTTPS" , nil
77+ }
78+ return "" , "" , fmt .Errorf ("no valid clone method available" )
79+ }
80+
81+ // ensureGitRepository ensures a directory is a properly configured git repository.
82+ // If the directory doesn't exist, it clones the repo.
83+ // If it exists but isn't a git repo, it initializes git and sets origin.
84+ // If it exists and is a git repo, it ensures origin is set correctly and pulls.
85+ // Returns the working directory path and any error.
86+ func ensureGitRepository (cmd * cobra.Command , targetDir , sshURL , httpsURL string ) error {
87+ cloneURL , cloneMethod , err := getPreferredCloneURL (sshURL , httpsURL )
88+ if err != nil {
89+ return err
90+ }
91+
92+ // Check if directory exists
93+ if _ , err := os .Stat (targetDir ); os .IsNotExist (err ) {
94+ // Directory doesn't exist - clone fresh
95+ cmd .Printf ("Cloning repository to '%s' using %s...\n " , targetDir , cloneMethod )
96+ return git .Clone (cloneURL , targetDir )
97+ }
98+
99+ // Directory exists - check if it's a git repo
100+ if ! git .IsGitRepositoryDir (targetDir ) {
101+ // Not a git repo - initialize and set origin
102+ cmd .Printf ("Directory '%s' exists but is not a git repository. Initializing...\n " , targetDir )
103+ if err := git .InitRepository (targetDir ); err != nil {
104+ return errors .WrapError ("failed to initialize git repository" , err )
105+ }
106+ }
107+
108+ // Ensure origin is set correctly
109+ cmd .Printf ("Ensuring git origin is configured correctly...\n " )
110+ if err := git .SetRemoteURL (targetDir , "origin" , cloneURL ); err != nil {
111+ return errors .WrapError ("failed to set git origin" , err )
112+ }
113+
114+ // Pull latest changes
115+ cmd .Printf ("Pulling latest changes...\n " )
116+ return git .Pull (targetDir )
117+ }
118+
70119// cloneRepository clones a repository using SSH or HTTPS based on availability
71120// Returns the clone method used ("SSH" or "HTTPS") and any error
72121func cloneRepository (sshURL , httpsURL , targetDir string ) (string , error ) {
@@ -125,8 +174,8 @@ func isGitAuthError(err error) bool {
125174 return false
126175}
127176
128- // pullOrCloneWithRetries retries a git clone or pull operation with exponential backoff
129- func pullOrCloneWithRetries (cmd * cobra.Command , workingDir , sshURL , httpsURL string ) error {
177+ // ensureGitRepositoryWithRetries retries ensureGitRepository with exponential backoff
178+ func ensureGitRepositoryWithRetries (cmd * cobra.Command , workingDir , sshURL , httpsURL string ) error {
130179 maxRetries := 3
131180 baseDelay := 200 * time .Millisecond
132181
@@ -136,17 +185,7 @@ func pullOrCloneWithRetries(cmd *cobra.Command, workingDir, sshURL, httpsURL str
136185 time .Sleep (delay )
137186 }
138187
139- var err error
140- if _ , statErr := os .Stat (workingDir ); statErr == nil {
141- // Directory exists, pull
142- cmd .Printf ("Pulling latest changes...\n " )
143- err = git .Pull (workingDir )
144- } else {
145- // Directory doesn't exist, clone
146- cmd .Printf ("Cloning repository...\n " )
147- _ , err = cloneRepository (sshURL , httpsURL , workingDir )
148- }
149-
188+ err := ensureGitRepository (cmd , workingDir , sshURL , httpsURL )
150189 if err == nil {
151190 return nil
152191 }
0 commit comments