@@ -167,6 +167,30 @@ func plumbCommand(cmd *exec.Cmd, channel ssh.Channel, sidechannel io.Writer) *sy
167167
168168var createLock sync.Mutex
169169
170+ // initRepo create a directory and init a new Git repo
171+ func initRepo (repoPath , gitHome string , c cookoo.Context ) (bool , error ) {
172+ log .Infof (c , "Creating new directory at %s" , repoPath )
173+ // Create directory
174+ if err := os .MkdirAll (repoPath , 0755 ); err != nil {
175+ log .Warnf (c , "Failed to create repository: %s" , err )
176+ return false , err
177+ }
178+ cmd := exec .Command ("git" , "init" , "--bare" )
179+ cmd .Dir = repoPath
180+ if out , err := cmd .CombinedOutput (); err != nil {
181+ log .Warnf (c , "git init output: %s" , out )
182+ return false , err
183+ }
184+
185+ hook , err := prereceiveHook (map [string ]string {"GitHome" : gitHome })
186+ if err != nil {
187+ return true , err
188+ }
189+ ioutil .WriteFile (filepath .Join (repoPath , "hooks" , "pre-receive" ), hook , 0755 )
190+
191+ return true , nil
192+ }
193+
170194// createRepo creates a new Git repo if it is not present already.
171195//
172196// Largely inspired by gitreceived from Flynn.
@@ -177,37 +201,30 @@ func createRepo(c cookoo.Context, repoPath, gitHome string) (bool, error) {
177201 createLock .Lock ()
178202 defer createLock .Unlock ()
179203
180- if fi , err := os .Stat (repoPath ); err == nil && fi .IsDir () {
181- // Nothing to do.
182- log .Infof (c , "Directory %s already exists." , repoPath )
183- return false , nil
184- } else if os .IsNotExist (err ) {
185-
186- log .Infof (c , "Creating new directory at %s" , repoPath )
187- // Create directory
188- if err := os .MkdirAll (repoPath , 0755 ); err != nil {
189- log .Warnf (c , "Failed to create repository: %s" , err )
190- return false , err
191- }
192- cmd := exec .Command ("git" , "init" , "--bare" )
193- cmd .Dir = repoPath
194- if out , err := cmd .CombinedOutput (); err != nil {
195- log .Warnf (c , "git init output: %s" , out )
196- return false , err
197- }
198-
199- hook , err := prereceiveHook (map [string ]string {"GitHome" : gitHome })
200- if err != nil {
201- return true , err
204+ if fi , err := os .Stat (repoPath ); err == nil {
205+ if fi .IsDir () {
206+ configPath := filepath .Join (repoPath , "config" )
207+ if _ , cerr := os .Stat (configPath ); cerr == nil {
208+ log .Debugf (c , "Directory '%s' already exists." , repoPath )
209+ return true , nil
210+ } else {
211+ log .Warnf (c , "No config file found at `%s`; removing it and recreating." , repoPath )
212+ if err := os .RemoveAll (repoPath ); err != nil {
213+ return false , fmt .Errorf ("Unable to remove path '%s': %s" , repoPath , err )
214+ }
215+ }
216+ } else {
217+ log .Warnf (c , "Path '%s' is not a directory; removing it and recreating." , repoPath )
218+ if err := os .RemoveAll (repoPath ); err != nil {
219+ return false , fmt .Errorf ("Unable to remove path '%s': %s" , repoPath , err )
220+ }
202221 }
203- ioutil .WriteFile (filepath .Join (repoPath , "hooks" , "pre-receive" ), hook , 0755 )
204-
205- return true , nil
206- } else if err == nil {
207- return false , errors .New ("Expected directory, found file." )
222+ } else if os .IsNotExist (err ) {
223+ log .Debugf (c , "Unable to get stat for path '%s': %s ." , repoPath , err )
208224 } else {
209225 return false , err
210226 }
227+ return initRepo (repoPath , gitHome , c )
211228}
212229
213230//prereceiveHook templates a pre-receive hook for Git.
0 commit comments