77 "cmp"
88 "fmt"
99 "os"
10+ "path"
11+ "path/filepath"
1012
1113 "github.com/go-git/go-git/v5"
1214 "github.com/go-git/go-git/v5/config"
@@ -124,7 +126,7 @@ func latestCommitSHA(repoURL, branch string, auth transport.AuthMethod) (string,
124126 return "" , fmt .Errorf ("branch %q not found" , branch )
125127}
126128
127- func (r * RepoCache ) CheckoutRepo (sourceUrl , branch , commit , gitAuth string ) (string , string , string , string , error ) {
129+ func (r * RepoCache ) CheckoutRepo (sourceUrl , branch , commit , gitAuth string , isDev bool ) (string , string , string , string , error ) {
128130 gitAuth = cmp .Or (gitAuth , r .server .config .Security .DefaultGitAuth )
129131 authEntry , err := r .server .loadGitKey (gitAuth )
130132 if err != nil {
@@ -137,7 +139,8 @@ func (r *RepoCache) CheckoutRepo(sourceUrl, branch, commit, gitAuth string) (str
137139 return "" , "" , "" , "" , err
138140 }
139141
140- dir , ok := r .cache [Repo {repo , branch , commit , gitAuth }]
142+ repoKey := Repo {repo , branch , commit , gitAuth }
143+ dir , ok := r .cache [repoKey ]
141144 if ok {
142145 return dir .dir , folder , dir .commitMessage , dir .hash , nil
143146 }
@@ -150,7 +153,9 @@ func (r *RepoCache) CheckoutRepo(sourceUrl, branch, commit, gitAuth string) (str
150153 // No commit id specified, checkout specified branch
151154 cloneOptions .ReferenceName = plumbing .NewBranchReferenceName (branch )
152155 cloneOptions .SingleBranch = true
153- cloneOptions .Depth = 1
156+ if ! isDev {
157+ cloneOptions .Depth = 1
158+ }
154159 }
155160
156161 if gitAuth != "" {
@@ -162,9 +167,19 @@ func (r *RepoCache) CheckoutRepo(sourceUrl, branch, commit, gitAuth string) (str
162167 cloneOptions .Auth = auth
163168 }
164169
165- targetPath , err := os .MkdirTemp (r .rootDir , "repo_" )
166- if err != nil {
167- return "" , "" , "" , "" , err
170+ var targetPath string
171+ if isDev {
172+ // We don't have a previous dev checkout for this repo, create a new one
173+ repoName := filepath .Base (repo )
174+ targetPath = getUnusedRepoPath (os .ExpandEnv ("$OPENRUN_HOME/app_src/" ), repoName )
175+ if err := os .MkdirAll (targetPath , 0744 ); err != nil {
176+ return "" , "" , "" , "" , err
177+ }
178+ } else {
179+ targetPath , err = os .MkdirTemp (r .rootDir , "repo_" )
180+ if err != nil {
181+ return "" , "" , "" , "" , err
182+ }
168183 }
169184
170185 // Configure the repo to Clone
@@ -215,6 +230,20 @@ func (r *RepoCache) CheckoutRepo(sourceUrl, branch, commit, gitAuth string) (str
215230 return targetPath , folder , newCommit .Message , newCommit .Hash .String (), nil
216231}
217232
233+ func getUnusedRepoPath (targetDir , repoName string ) string {
234+ if _ , err := os .Stat (path .Join (targetDir , repoName )); os .IsNotExist (err ) {
235+ return path .Join (targetDir , repoName )
236+ }
237+ count := 2
238+ for {
239+ unusedName := fmt .Sprintf ("%s%d" , repoName , count )
240+ if _ , err := os .Stat (path .Join (targetDir , unusedName )); os .IsNotExist (err ) {
241+ return path .Join (targetDir , unusedName )
242+ }
243+ count ++
244+ }
245+ }
246+
218247func (r * RepoCache ) Cleanup () {
219248 if r .rootDir != "" {
220249 os .RemoveAll (r .rootDir )
0 commit comments