blob: 480a6b30d0a9c9aa4f667a618b0e852d637432dd [file] [log] [blame]
Petr Baudisba6485e2008-09-24 22:44:01 +02001# git-gui remote adding support
2# Copyright (C) 2008 Petr Baudis
3
4class remote_add {
5
6field w ; # widget path
7field w_name ; # new remote name widget
8field w_loc ; # new remote location widget
9
10field name {}; # name of the remote the user has chosen
11field location {}; # location of the remote the user has chosen
12
13field opt_action fetch; # action to do after registering the remote locally
14
15constructor dialog {} {
Pat Thoytsc80d7be2010-01-26 00:05:31 +000016 global repo_config use_ttk NS
Petr Baudisba6485e2008-09-24 22:44:01 +020017
Pat Thoytsc80d7be2010-01-26 00:05:31 +000018 make_dialog top w
19 wm withdraw $top
Vasco Almeidaa3d97af2016-05-08 10:52:57 +000020 wm title $top [mc "%s (%s): Add Remote" [appname] [reponame]]
Petr Baudisba6485e2008-09-24 22:44:01 +020021 if {$top ne {.}} {
22 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
23 }
24
Pat Thoytsc80d7be2010-01-26 00:05:31 +000025 ${NS}::label $w.header -text [mc "Add New Remote"] \
26 -font font_uibold -anchor center
Petr Baudisba6485e2008-09-24 22:44:01 +020027 pack $w.header -side top -fill x
28
Pat Thoytsc80d7be2010-01-26 00:05:31 +000029 ${NS}::frame $w.buttons
30 ${NS}::button $w.buttons.create -text [mc Add] \
Petr Baudisba6485e2008-09-24 22:44:01 +020031 -default active \
32 -command [cb _add]
33 pack $w.buttons.create -side right
Pat Thoytsc80d7be2010-01-26 00:05:31 +000034 ${NS}::button $w.buttons.cancel -text [mc Cancel] \
Petr Baudisba6485e2008-09-24 22:44:01 +020035 -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
Pat Thoytsc80d7be2010-01-26 00:05:31 +000039 ${NS}::labelframe $w.desc -text [mc "Remote Details"]
Petr Baudisba6485e2008-09-24 22:44:01 +020040
Pat Thoytsc80d7be2010-01-26 00:05:31 +000041 ${NS}::label $w.desc.name_l -text [mc "Name:"]
Petr Baudisba6485e2008-09-24 22:44:01 +020042 set w_name $w.desc.name_t
Pat Thoytsc80d7be2010-01-26 00:05:31 +000043 ${NS}::entry $w_name \
Petr Baudisba6485e2008-09-24 22:44:01 +020044 -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
Pat Thoytsc80d7be2010-01-26 00:05:31 +000050 ${NS}::label $w.desc.loc_l -text [mc "Location:"]
Petr Baudisba6485e2008-09-24 22:44:01 +020051 set w_loc $w.desc.loc_t
Pat Thoytsc80d7be2010-01-26 00:05:31 +000052 ${NS}::entry $w_loc \
Petr Baudisba6485e2008-09-24 22:44:01 +020053 -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
Pat Thoytsc80d7be2010-01-26 00:05:31 +000060 ${NS}::labelframe $w.action -text [mc "Further Action"]
Petr Baudisba6485e2008-09-24 22:44:01 +020061
Pat Thoytsc80d7be2010-01-26 00:05:31 +000062 ${NS}::radiobutton $w.action.fetch \
Petr Baudisba6485e2008-09-24 22:44:01 +020063 -text [mc "Fetch Immediately"] \
64 -value fetch \
65 -variable @opt_action
66 pack $w.action.fetch -anchor nw
67
Pat Thoytsc80d7be2010-01-26 00:05:31 +000068 ${NS}::radiobutton $w.action.push \
Petr Baudisba6485e2008-09-24 22:44:01 +020069 -text [mc "Initialize Remote Repository and Push"] \
70 -value push \
71 -variable @opt_action
72 pack $w.action.push -anchor nw
73
Pat Thoytsc80d7be2010-01-26 00:05:31 +000074 ${NS}::radiobutton $w.action.none \
Petr Baudisba6485e2008-09-24 22:44:01 +020075 -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
Pat Thoytsc80d7be2010-01-26 00:05:31 +000086 wm deiconify $top
Petr Baudisba6485e2008-09-24 22:44:01 +020087 tkwait window $w
88}
89
90method _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 \
Petr Baudis0b32cab2008-09-25 01:39:13 +0200132 [mc "fetch %s" $name] \
133 [mc "Fetching the %s" $name]]
134 console::exec $c [list git fetch $name]
Petr Baudisba6485e2008-09-24 22:44:01 +0200135 }
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 }
Petr Baudisadcbd432008-09-24 22:44:03 +0200146 lappend cmds [list exec $ssh $host mkdir -p $location && git --git-dir=$path init --bare]
Petr Baudisba6485e2008-09-24 22:44:01 +0200147 } elseif { ! [regexp {://} $location xx] } {
Petr Baudisadcbd432008-09-24 22:44:03 +0200148 lappend cmds [list exec mkdir -p $location]
Petr Baudisba6485e2008-09-24 22:44:01 +0200149 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
175method _validate_name {d S} {
176 if {$d == 1} {
177 if {[regexp {[~^:?*\[\0- ]} $S]} {
178 return 0
179 }
180 }
181 return 1
182}
183
184method _visible {} {
185 grab $w
186 $w_name icursor end
187 focus $w_name
188}
189
190}