Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 {} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 16 | global repo_config use_ttk NS |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 17 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 18 | make_dialog top w |
| 19 | wm withdraw $top |
Vasco Almeida | a3d97af | 2016-05-08 10:52:57 +0000 | [diff] [blame] | 20 | wm title $top [mc "%s (%s): Add Remote" [appname] [reponame]] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 21 | if {$top ne {.}} { |
| 22 | wm geometry $top "+[winfo rootx .]+[winfo rooty .]" |
| 23 | } |
| 24 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 25 | ${NS}::label $w.header -text [mc "Add New Remote"] \ |
| 26 | -font font_uibold -anchor center |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 27 | pack $w.header -side top -fill x |
| 28 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 29 | ${NS}::frame $w.buttons |
| 30 | ${NS}::button $w.buttons.create -text [mc Add] \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 31 | -default active \ |
| 32 | -command [cb _add] |
| 33 | pack $w.buttons.create -side right |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 34 | ${NS}::button $w.buttons.cancel -text [mc Cancel] \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 39 | ${NS}::labelframe $w.desc -text [mc "Remote Details"] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 40 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 41 | ${NS}::label $w.desc.name_l -text [mc "Name:"] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 42 | set w_name $w.desc.name_t |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 43 | ${NS}::entry $w_name \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 50 | ${NS}::label $w.desc.loc_l -text [mc "Location:"] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 51 | set w_loc $w.desc.loc_t |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 52 | ${NS}::entry $w_loc \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 60 | ${NS}::labelframe $w.action -text [mc "Further Action"] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 61 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 62 | ${NS}::radiobutton $w.action.fetch \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 63 | -text [mc "Fetch Immediately"] \ |
| 64 | -value fetch \ |
| 65 | -variable @opt_action |
| 66 | pack $w.action.fetch -anchor nw |
| 67 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 68 | ${NS}::radiobutton $w.action.push \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 69 | -text [mc "Initialize Remote Repository and Push"] \ |
| 70 | -value push \ |
| 71 | -variable @opt_action |
| 72 | pack $w.action.push -anchor nw |
| 73 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 74 | ${NS}::radiobutton $w.action.none \ |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 86 | wm deiconify $top |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 \ |
Petr Baudis | 0b32cab | 2008-09-25 01:39:13 +0200 | [diff] [blame] | 132 | [mc "fetch %s" $name] \ |
| 133 | [mc "Fetching the %s" $name]] |
| 134 | console::exec $c [list git fetch $name] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 | } |
Petr Baudis | adcbd43 | 2008-09-24 22:44:03 +0200 | [diff] [blame] | 146 | lappend cmds [list exec $ssh $host mkdir -p $location && git --git-dir=$path init --bare] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 147 | } elseif { ! [regexp {://} $location xx] } { |
Petr Baudis | adcbd43 | 2008-09-24 22:44:03 +0200 | [diff] [blame] | 148 | lappend cmds [list exec mkdir -p $location] |
Petr Baudis | ba6485e | 2008-09-24 22:44:01 +0200 | [diff] [blame] | 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 | } |