]>
Commit | Line | Data |
---|---|---|
ba6485e0 PB |
1 | # git-gui remote adding support |
2 | # Copyright (C) 2008 Petr Baudis | |
3 | ||
4 | class remote_add { | |
5 | ||
6 | field w ; # widget path | |
7 | field w_name ; # new remote name widget | |
8 | field w_loc ; # new remote location widget | |
9 | ||
10 | field name {}; # name of the remote the user has chosen | |
11 | field location {}; # location of the remote the user has chosen | |
12 | ||
13 | field opt_action fetch; # action to do after registering the remote locally | |
14 | ||
15 | constructor dialog {} { | |
c80d7be5 | 16 | global repo_config use_ttk NS |
ba6485e0 | 17 | |
c80d7be5 PT |
18 | make_dialog top w |
19 | wm withdraw $top | |
a3d97afa | 20 | wm title $top [mc "%s (%s): Add Remote" [appname] [reponame]] |
ba6485e0 PB |
21 | if {$top ne {.}} { |
22 | wm geometry $top "+[winfo rootx .]+[winfo rooty .]" | |
23 | } | |
24 | ||
c80d7be5 PT |
25 | ${NS}::label $w.header -text [mc "Add New Remote"] \ |
26 | -font font_uibold -anchor center | |
ba6485e0 PB |
27 | pack $w.header -side top -fill x |
28 | ||
c80d7be5 PT |
29 | ${NS}::frame $w.buttons |
30 | ${NS}::button $w.buttons.create -text [mc Add] \ | |
ba6485e0 PB |
31 | -default active \ |
32 | -command [cb _add] | |
33 | pack $w.buttons.create -side right | |
c80d7be5 | 34 | ${NS}::button $w.buttons.cancel -text [mc Cancel] \ |
ba6485e0 PB |
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 | |
38 | ||
c80d7be5 | 39 | ${NS}::labelframe $w.desc -text [mc "Remote Details"] |
ba6485e0 | 40 | |
c80d7be5 | 41 | ${NS}::label $w.desc.name_l -text [mc "Name:"] |
ba6485e0 | 42 | set w_name $w.desc.name_t |
c80d7be5 | 43 | ${NS}::entry $w_name \ |
ba6485e0 PB |
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} | |
49 | ||
c80d7be5 | 50 | ${NS}::label $w.desc.loc_l -text [mc "Location:"] |
ba6485e0 | 51 | set w_loc $w.desc.loc_t |
c80d7be5 | 52 | ${NS}::entry $w_loc \ |
ba6485e0 PB |
53 | -width 40 \ |
54 | -textvariable @location | |
55 | grid $w.desc.loc_l $w_loc -sticky we -padx {0 5} | |
56 | ||
57 | grid columnconfigure $w.desc 1 -weight 1 | |
58 | pack $w.desc -anchor nw -fill x -pady 5 -padx 5 | |
59 | ||
c80d7be5 | 60 | ${NS}::labelframe $w.action -text [mc "Further Action"] |
ba6485e0 | 61 | |
c80d7be5 | 62 | ${NS}::radiobutton $w.action.fetch \ |
ba6485e0 PB |
63 | -text [mc "Fetch Immediately"] \ |
64 | -value fetch \ | |
65 | -variable @opt_action | |
66 | pack $w.action.fetch -anchor nw | |
67 | ||
c80d7be5 | 68 | ${NS}::radiobutton $w.action.push \ |
ba6485e0 PB |
69 | -text [mc "Initialize Remote Repository and Push"] \ |
70 | -value push \ | |
71 | -variable @opt_action | |
72 | pack $w.action.push -anchor nw | |
73 | ||
c80d7be5 | 74 | ${NS}::radiobutton $w.action.none \ |
ba6485e0 PB |
75 | -text [mc "Do Nothing Else Now"] \ |
76 | -value none \ | |
77 | -variable @opt_action | |
78 | pack $w.action.none -anchor nw | |
79 | ||
80 | grid columnconfigure $w.action 1 -weight 1 | |
81 | pack $w.action -anchor nw -fill x -pady 5 -padx 5 | |
82 | ||
83 | bind $w <Visibility> [cb _visible] | |
84 | bind $w <Key-Escape> [list destroy $w] | |
85 | bind $w <Key-Return> [cb _add]\;break | |
c80d7be5 | 86 | wm deiconify $top |
ba6485e0 PB |
87 | tkwait window $w |
88 | } | |
89 | ||
90 | method _add {} { | |
91 | global repo_config env | |
92 | global M1B | |
93 | ||
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 | } | |
104 | ||
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 | } | |
117 | ||
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 | } | |
128 | ||
129 | switch -- $opt_action { | |
130 | fetch { | |
131 | set c [console::new \ | |
0b32cab9 PB |
132 | [mc "fetch %s" $name] \ |
133 | [mc "Fetching the %s" $name]] | |
134 | console::exec $c [list git fetch $name] | |
ba6485e0 PB |
135 | } |
136 | push { | |
137 | set cmds [list] | |
138 | ||
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 | } | |
adcbd431 | 146 | lappend cmds [list exec $ssh $host mkdir -p $location && git --git-dir=$path init --bare] |
ba6485e0 | 147 | } elseif { ! [regexp {://} $location xx] } { |
adcbd431 | 148 | lappend cmds [list exec mkdir -p $location] |
ba6485e0 PB |
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 | } | |
160 | ||
161 | set c [console::new \ | |
162 | [mc "push %s" $name] \ | |
163 | [mc "Setting up the %s (at %s)" $name $location]] | |
164 | ||
165 | lappend cmds [list exec git push -v --all $name] | |
166 | console::chain $c $cmds | |
167 | } | |
168 | none { | |
169 | } | |
170 | } | |
171 | ||
172 | destroy $w | |
173 | } | |
174 | ||
175 | method _validate_name {d S} { | |
176 | if {$d == 1} { | |
177 | if {[regexp {[~^:?*\[\0- ]} $S]} { | |
178 | return 0 | |
179 | } | |
180 | } | |
181 | return 1 | |
182 | } | |
183 | ||
184 | method _visible {} { | |
185 | grab $w | |
186 | $w_name icursor end | |
187 | focus $w_name | |
188 | } | |
189 | ||
190 | } |