1 # git-gui remote adding support
2 # Copyright (C) 2008 Petr Baudis
4 class remote_add {
6 field w ; # widget path
7 field w_name ; # new remote name widget
8 field w_loc ; # new remote location widget
10 field name {}; # name of the remote the user has chosen
11 field location {}; # location of the remote the user has chosen
13 field opt_action fetch; # action to do after registering the remote locally
15 constructor dialog {} {
16 global repo_config use_ttk NS
18 make_dialog top w
19 wm withdraw $top
20 wm title $top [append "[appname] ([reponame]): " [mc "Add Remote"]]
21 if {$top ne {.}} {
22 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
23 }
25 ${NS}::label $w.header -text [mc "Add New Remote"] \
26 -font font_uibold -anchor center
27 pack $w.header -side top -fill x
29 ${NS}::frame $w.buttons
30 ${NS}::button $w.buttons.create -text [mc Add] \
31 -default active \
32 -command [cb _add]
33 pack $w.buttons.create -side right
34 ${NS}::button $w.buttons.cancel -text [mc Cancel] \
35 -command [list destroy $w]
36 pack $w.buttons.cancel -side right -padx 5
37 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
39 ${NS}::labelframe $w.desc -text [mc "Remote Details"]
41 ${NS}::label $w.desc.name_l -text [mc "Name:"]
42 set w_name $w.desc.name_t
43 ${NS}::entry $w_name \
44 -width 40 \
45 -textvariable @name \
46 -validate key \
47 -validatecommand [cb _validate_name %d %S]
48 grid $w.desc.name_l $w_name -sticky we -padx {0 5}
50 ${NS}::label $w.desc.loc_l -text [mc "Location:"]
51 set w_loc $w.desc.loc_t
52 ${NS}::entry $w_loc \
53 -width 40 \
54 -textvariable @location
55 grid $w.desc.loc_l $w_loc -sticky we -padx {0 5}
57 grid columnconfigure $w.desc 1 -weight 1
58 pack $w.desc -anchor nw -fill x -pady 5 -padx 5
60 ${NS}::labelframe $w.action -text [mc "Further Action"]
62 ${NS}::radiobutton $w.action.fetch \
63 -text [mc "Fetch Immediately"] \
64 -value fetch \
65 -variable @opt_action
66 pack $w.action.fetch -anchor nw
68 ${NS}::radiobutton $w.action.push \
69 -text [mc "Initialize Remote Repository and Push"] \
70 -value push \
71 -variable @opt_action
72 pack $w.action.push -anchor nw
74 ${NS}::radiobutton $w.action.none \
75 -text [mc "Do Nothing Else Now"] \
76 -value none \
77 -variable @opt_action
78 pack $w.action.none -anchor nw
80 grid columnconfigure $w.action 1 -weight 1
81 pack $w.action -anchor nw -fill x -pady 5 -padx 5
83 bind $w <Visibility> [cb _visible]
84 bind $w <Key-Escape> [list destroy $w]
85 bind $w <Key-Return> [cb _add]\;break
86 wm deiconify $top
87 tkwait window $w
88 }
90 method _add {} {
91 global repo_config env
92 global M1B
94 if {$name eq {}} {
95 tk_messageBox \
96 -icon error \
97 -type ok \
98 -title [wm title $w] \
99 -parent $w \
100 -message [mc "Please supply a remote name."]
101 focus $w_name
102 return
103 }
105 # XXX: We abuse check-ref-format here, but
106 # that should be ok.
107 if {[catch {git check-ref-format "remotes/$name"}]} {
108 tk_messageBox \
109 -icon error \
110 -type ok \
111 -title [wm title $w] \
112 -parent $w \
113 -message [mc "'%s' is not an acceptable remote name." $name]
114 focus $w_name
115 return
116 }
118 if {[catch {add_single_remote $name $location}]} {
119 tk_messageBox \
120 -icon error \
121 -type ok \
122 -title [wm title $w] \
123 -parent $w \
124 -message [mc "Failed to add remote '%s' of location '%s'." $name $location]
125 focus $w_name
126 return
127 }
129 switch -- $opt_action {
130 fetch {
131 set c [console::new \
132 [mc "fetch %s" $name] \
133 [mc "Fetching the %s" $name]]
134 console::exec $c [list git fetch $name]
135 }
136 push {
137 set cmds [list]
139 # Parse the location
140 if { [regexp {(?:git\+)?ssh://([^/]+)(/.+)} $location xx host path]
141 || [regexp {([^:][^:]+):(.+)} $location xx host path]} {
142 set ssh ssh
143 if {[info exists env(GIT_SSH)]} {
144 set ssh $env(GIT_SSH)
145 }
146 lappend cmds [list exec $ssh $host mkdir -p $location && git --git-dir=$path init --bare]
147 } elseif { ! [regexp {://} $location xx] } {
148 lappend cmds [list exec mkdir -p $location]
149 lappend cmds [list exec git --git-dir=$location init --bare]
150 } else {
151 tk_messageBox \
152 -icon error \
153 -type ok \
154 -title [wm title $w] \
155 -parent $w \
156 -message [mc "Do not know how to initialize repository at location '%s'." $location]
157 destroy $w
158 return
159 }
161 set c [console::new \
162 [mc "push %s" $name] \
163 [mc "Setting up the %s (at %s)" $name $location]]
165 lappend cmds [list exec git push -v --all $name]
166 console::chain $c $cmds
167 }
168 none {
169 }
170 }
172 destroy $w
173 }
175 method _validate_name {d S} {
176 if {$d == 1} {
177 if {[regexp {[~^:?*\[\0- ]} $S]} {
178 return 0
179 }
180 }
181 return 1
182 }
184 method _visible {} {
185 grab $w
186 $w_name icursor end
187 focus $w_name
188 }
190 }