lib / transport.tclon commit git gui: keep selected branch when remote is changed in push dialog (9e34e62)
   1# git-gui transport (fetch/push) support
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4proc fetch_from {remote} {
   5        set w [console::new \
   6                [mc "fetch %s" $remote] \
   7                [mc "Fetching new changes from %s" $remote]]
   8        set cmds [list]
   9        lappend cmds [list exec git fetch $remote]
  10        if {[is_config_true gui.pruneduringfetch]} {
  11                lappend cmds [list exec git remote prune $remote]
  12        }
  13        console::chain $w $cmds
  14}
  15
  16proc prune_from {remote} {
  17        set w [console::new \
  18                [mc "remote prune %s" $remote] \
  19                [mc "Pruning tracking branches deleted from %s" $remote]]
  20        console::exec $w [list git remote prune $remote]
  21}
  22
  23proc push_to {remote} {
  24        set w [console::new \
  25                [mc "push %s" $remote] \
  26                [mc "Pushing changes to %s" $remote]]
  27        set cmd [list git push]
  28        lappend cmd -v
  29        lappend cmd $remote
  30        console::exec $w $cmd
  31}
  32
  33proc start_push_anywhere_action {w} {
  34        global push_urltype push_remote push_url push_thin push_tags
  35        global push_force
  36        global repo_config
  37
  38        set is_mirror 0
  39        set r_url {}
  40        switch -- $push_urltype {
  41        remote {
  42                set r_url $push_remote
  43                catch {set is_mirror $repo_config(remote.$push_remote.mirror)}
  44        }
  45        url {set r_url $push_url}
  46        }
  47        if {$r_url eq {}} return
  48
  49        set cmd [list git push]
  50        lappend cmd -v
  51        if {$push_thin} {
  52                lappend cmd --thin
  53        }
  54        if {$push_force} {
  55                lappend cmd --force
  56        }
  57        if {$push_tags} {
  58                lappend cmd --tags
  59        }
  60        lappend cmd $r_url
  61        if {$is_mirror} {
  62                set cons [console::new \
  63                        [mc "push %s" $r_url] \
  64                        [mc "Mirroring to %s" $r_url]]
  65        } else {
  66                set cnt 0
  67                foreach i [$w.source.l curselection] {
  68                        set b [$w.source.l get $i]
  69                        lappend cmd "refs/heads/$b:refs/heads/$b"
  70                        incr cnt
  71                }
  72                if {$cnt == 0} {
  73                        return
  74                } elseif {$cnt == 1} {
  75                        set unit branch
  76                } else {
  77                        set unit branches
  78                }
  79
  80                set cons [console::new \
  81                        [mc "push %s" $r_url] \
  82                        [mc "Pushing %s %s to %s" $cnt $unit $r_url]]
  83        }
  84        console::exec $cons $cmd
  85        destroy $w
  86}
  87
  88trace add variable push_remote write \
  89        [list radio_selector push_urltype remote]
  90
  91proc do_push_anywhere {} {
  92        global all_remotes current_branch
  93        global push_urltype push_remote push_url push_thin push_tags
  94        global push_force use_ttk NS
  95
  96        set w .push_setup
  97        toplevel $w
  98        wm withdraw $w
  99        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
 100        pave_toplevel $w
 101
 102        ${NS}::label $w.header -text [mc "Push Branches"] \
 103                -font font_uibold -anchor center
 104        pack $w.header -side top -fill x
 105
 106        ${NS}::frame $w.buttons
 107        ${NS}::button $w.buttons.create -text [mc Push] \
 108                -default active \
 109                -command [list start_push_anywhere_action $w]
 110        pack $w.buttons.create -side right
 111        ${NS}::button $w.buttons.cancel -text [mc "Cancel"] \
 112                -default normal \
 113                -command [list destroy $w]
 114        pack $w.buttons.cancel -side right -padx 5
 115        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 116
 117        ${NS}::labelframe $w.source -text [mc "Source Branches"]
 118        slistbox $w.source.l \
 119                -height 10 \
 120                -width 70 \
 121                -selectmode extended
 122        foreach h [load_all_heads] {
 123                $w.source.l insert end $h
 124                if {$h eq $current_branch} {
 125                        $w.source.l select set end
 126                }
 127        }
 128        pack $w.source.l -side left -fill both -expand 1
 129        pack $w.source -fill both -expand 1 -pady 5 -padx 5
 130
 131        ${NS}::labelframe $w.dest -text [mc "Destination Repository"]
 132        if {$all_remotes ne {}} {
 133                ${NS}::radiobutton $w.dest.remote_r \
 134                        -text [mc "Remote:"] \
 135                        -value remote \
 136                        -variable push_urltype
 137                if {$use_ttk} {
 138                        ttk::combobox $w.dest.remote_m -state readonly \
 139                                -exportselection false \
 140                                -textvariable push_remote \
 141                                -values $all_remotes
 142                } else {
 143                        eval tk_optionMenu $w.dest.remote_m push_remote $all_remotes
 144                }
 145                grid $w.dest.remote_r $w.dest.remote_m -sticky w
 146                if {[lsearch -sorted -exact $all_remotes origin] != -1} {
 147                        set push_remote origin
 148                } else {
 149                        set push_remote [lindex $all_remotes 0]
 150                }
 151                set push_urltype remote
 152        } else {
 153                set push_urltype url
 154        }
 155        ${NS}::radiobutton $w.dest.url_r \
 156                -text [mc "Arbitrary Location:"] \
 157                -value url \
 158                -variable push_urltype
 159        ${NS}::entry $w.dest.url_t \
 160                -width 50 \
 161                -textvariable push_url \
 162                -validate key \
 163                -validatecommand {
 164                        if {%d == 1 && [regexp {\s} %S]} {return 0}
 165                        if {%d == 1 && [string length %S] > 0} {
 166                                set push_urltype url
 167                        }
 168                        return 1
 169                }
 170        grid $w.dest.url_r $w.dest.url_t -sticky we -padx {0 5}
 171        grid columnconfigure $w.dest 1 -weight 1
 172        pack $w.dest -anchor nw -fill x -pady 5 -padx 5
 173
 174        ${NS}::labelframe $w.options -text [mc "Transfer Options"]
 175        ${NS}::checkbutton $w.options.force \
 176                -text [mc "Force overwrite existing branch (may discard changes)"] \
 177                -variable push_force
 178        grid $w.options.force -columnspan 2 -sticky w
 179        ${NS}::checkbutton $w.options.thin \
 180                -text [mc "Use thin pack (for slow network connections)"] \
 181                -variable push_thin
 182        grid $w.options.thin -columnspan 2 -sticky w
 183        ${NS}::checkbutton $w.options.tags \
 184                -text [mc "Include tags"] \
 185                -variable push_tags
 186        grid $w.options.tags -columnspan 2 -sticky w
 187        grid columnconfigure $w.options 1 -weight 1
 188        pack $w.options -anchor nw -fill x -pady 5 -padx 5
 189
 190        set push_url {}
 191        set push_force 0
 192        set push_thin 0
 193        set push_tags 0
 194
 195        bind $w <Visibility> "grab $w; focus $w.buttons.create"
 196        bind $w <Key-Escape> "destroy $w"
 197        bind $w <Key-Return> [list start_push_anywhere_action $w]
 198        wm title $w [append "[appname] ([reponame]): " [mc "Push"]]
 199        wm deiconify $w
 200        tkwait window $w
 201}