Git fork
at reftables-rust 129 lines 3.4 kB view raw
1# git-gui about git-gui dialog 2# Copyright (C) 2006, 2007 Shawn Pearce 3 4proc find_ssh_key {} { 5 foreach name { 6 ~/.ssh/id_dsa.pub ~/.ssh/id_ecdsa.pub ~/.ssh/id_ed25519.pub 7 ~/.ssh/id_rsa.pub ~/.ssh/identity.pub 8 } { 9 if {[file exists $name]} { 10 set fh [safe_open_file $name r] 11 set cont [read $fh] 12 close $fh 13 return [list $name $cont] 14 } 15 } 16 17 return {} 18} 19 20proc do_ssh_key {} { 21 global sshkey_title sshkey_fd 22 23 set w .sshkey_dialog 24 if {[winfo exists $w]} { 25 raise $w 26 return 27 } 28 29 Dialog $w 30 wm transient $w . 31 32 set finfo [find_ssh_key] 33 if {$finfo eq {}} { 34 set sshkey_title [mc "No keys found."] 35 set gen_state normal 36 } else { 37 set sshkey_title [mc "Found a public key in: %s" [lindex $finfo 0]] 38 set gen_state disabled 39 } 40 41 ttk::frame $w.header 42 ttk::label $w.header.lbl -textvariable sshkey_title -anchor w 43 ttk::button $w.header.gen -text [mc "Generate Key"] \ 44 -command [list make_ssh_key $w] -state $gen_state 45 pack $w.header.lbl -side left -expand 1 -fill x 46 pack $w.header.gen -side right 47 pack $w.header -fill x -pady 5 -padx 5 48 49 text $w.contents -width 60 -height 10 -wrap char -relief sunken 50 pack $w.contents -fill both -expand 1 51 set clr [ttk::style lookup . -selectbackground] 52 $w.contents configure -inactiveselectbackground $clr 53 54 ttk::frame $w.buttons 55 ttk::button $w.buttons.close -text [mc Close] \ 56 -default active -command [list destroy $w] 57 pack $w.buttons.close -side right 58 ttk::button $w.buttons.copy -text [mc "Copy To Clipboard"] \ 59 -command [list tk_textCopy $w.contents] 60 pack $w.buttons.copy -side left 61 pack $w.buttons -side bottom -fill x -pady 5 -padx 5 62 63 if {$finfo ne {}} { 64 $w.contents insert end [lindex $finfo 1] sel 65 } 66 $w.contents configure -state disabled 67 68 bind $w <Visibility> "grab $w; focus $w.buttons.close" 69 bind $w <Key-Escape> "destroy $w" 70 bind $w <Key-Return> "destroy $w" 71 bind $w <Destroy> kill_sshkey 72 wm title $w [mc "Your OpenSSH Public Key"] 73 tk::PlaceWindow $w widget . 74 tkwait window $w 75} 76 77proc make_ssh_key {w} { 78 global sshkey_title sshkey_output sshkey_fd 79 80 set sshkey_title [mc "Generating..."] 81 $w.header.gen configure -state disabled 82 83 set cmdline [list [shellpath] -c \ 84 {echo | ssh-keygen -q -t rsa -f ~/.ssh/id_rsa 2>&1}] 85 86 if {[catch { set sshkey_fd [safe_open_command $cmdline] } err]} { 87 error_popup [mc "Could not start ssh-keygen:\n\n%s" $err] 88 return 89 } 90 91 set sshkey_output {} 92 fconfigure $sshkey_fd -blocking 0 93 fileevent $sshkey_fd readable [list read_sshkey_output $sshkey_fd $w] 94} 95 96proc kill_sshkey {} { 97 global sshkey_fd 98 if {![info exists sshkey_fd]} return 99 catch { kill_file_process $sshkey_fd } 100 catch { close $sshkey_fd } 101} 102 103proc read_sshkey_output {fd w} { 104 global sshkey_fd sshkey_output sshkey_title 105 106 set sshkey_output "$sshkey_output[read $fd]" 107 if {![eof $fd]} return 108 109 fconfigure $fd -blocking 1 110 unset sshkey_fd 111 112 $w.contents configure -state normal 113 if {[catch {close $fd} err]} { 114 set sshkey_title [mc "Generation failed."] 115 $w.contents insert end $err 116 $w.contents insert end "\n" 117 $w.contents insert end $sshkey_output 118 } else { 119 set finfo [find_ssh_key] 120 if {$finfo eq {}} { 121 set sshkey_title [mc "Generation succeeded, but no keys found."] 122 $w.contents insert end $sshkey_output 123 } else { 124 set sshkey_title [mc "Your key is in: %s" [lindex $finfo 0]] 125 $w.contents insert end [lindex $finfo 1] sel 126 } 127 } 128 $w.contents configure -state disable 129}