Skip to content

Commit d36a8f7

Browse files
committed
git-gui: Allow 'Create New Repository' on existing directories
Often users setup a few source files and get a project rolling before they create a Git repository for it. In such cases the core Git tools allow users to initialize a new repository by simply running `git init` at the desired root level directory. We need to allow the same situation in git-gui; if the user is trying to make a new repository we should let them do that to any location they chose. If the directory already exists and already has files contained within it we still should allow the user to create a repository there. However we still need to disallow creating a repository on top of an existing repository. Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent daaa958 commit d36a8f7

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

lib/choose_repository.tcl

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,6 @@ method _write_local_path {args} {
290290
}
291291

292292
method _git_init {} {
293-
if {[file exists $local_path]} {
294-
error_popup [mc "Location %s already exists." $local_path]
295-
return 0
296-
}
297-
298293
if {[catch {file mkdir $local_path} err]} {
299294
error_popup [strcat \
300295
[mc "Failed to create repository %s:" $local_path] \
@@ -417,41 +412,35 @@ method _new_local_path {} {
417412
if {$p eq {}} return
418413

419414
set p [file normalize $p]
420-
if {[file isdirectory $p]} {
421-
foreach i [glob \
422-
-directory $p \
423-
-tails \
424-
-nocomplain \
425-
* .*] {
426-
switch -- $i {
427-
. continue
428-
.. continue
429-
default {
430-
error_popup [mc "Directory %s already exists." $p]
431-
return
432-
}
433-
}
434-
}
435-
if {[catch {file delete $p} err]} {
436-
error_popup [strcat \
437-
[mc "Directory %s already exists." $p] \
438-
"\n\n$err"]
439-
return
440-
}
441-
} elseif {[file exists $p]} {
442-
error_popup [mc "File %s already exists." $p]
415+
if {![_new_ok $p]} {
443416
return
444417
}
445418
set local_path $p
446419
}
447420

448421
method _do_new2 {} {
422+
if {![_new_ok $local_path]} {
423+
return
424+
}
449425
if {![_git_init $this]} {
450426
return
451427
}
452428
set done 1
453429
}
454430

431+
proc _new_ok {p} {
432+
if {[file isdirectory $p]} {
433+
if {[_is_git [file join $p .git]]} {
434+
error_popup [mc "Directory %s already exists." $p]
435+
return 0
436+
}
437+
} elseif {[file exists $p]} {
438+
error_popup [mc "File %s already exists." $p]
439+
return 0
440+
}
441+
return 1
442+
}
443+
455444
######################################################################
456445
##
457446
## Clone Existing Repository
@@ -607,6 +596,11 @@ method _do_clone2 {} {
607596
}
608597
}
609598

599+
if {[file exists $local_path]} {
600+
error_popup [mc "Location %s already exists." $local_path]
601+
return
602+
}
603+
610604
if {![_git_init $this]} return
611605
set local_path [pwd]
612606

0 commit comments

Comments
 (0)