git-gui / lib / choose_repository.tclon commit Merge branch 'jn/less-reconfigure' into maint (378e5e4)
   1# git-gui Git repository chooser
   2# Copyright (C) 2007 Shawn Pearce
   3
   4class choose_repository {
   5
   6field top
   7field w
   8field w_body      ; # Widget holding the center content
   9field w_next      ; # Next button
  10field w_quit      ; # Quit button
  11field o_cons      ; # Console object (if active)
  12field w_types     ; # List of type buttons in clone
  13field w_recentlist ; # Listbox containing recent repositories
  14field w_localpath  ; # Entry widget bound to local_path
  15
  16field done              0 ; # Finished picking the repository?
  17field local_path       {} ; # Where this repository is locally
  18field origin_url       {} ; # Where we are cloning from
  19field origin_name  origin ; # What we shall call 'origin'
  20field clone_type hardlink ; # Type of clone to construct
  21field readtree_err        ; # Error output from read-tree (if any)
  22field sorted_recent       ; # recent repositories (sorted)
  23
  24constructor pick {} {
  25        global M1T M1B use_ttk NS
  26
  27        make_dialog top w
  28        wm title $top [mc "Git Gui"]
  29
  30        if {$top eq {.}} {
  31                menu $w.mbar -tearoff 0
  32                $top configure -menu $w.mbar
  33
  34                set m_repo $w.mbar.repository
  35                $w.mbar add cascade \
  36                        -label [mc Repository] \
  37                        -menu $m_repo
  38                menu $m_repo
  39
  40                if {[is_MacOSX]} {
  41                        $w.mbar add cascade -label Apple -menu .mbar.apple
  42                        menu $w.mbar.apple
  43                        $w.mbar.apple add command \
  44                                -label [mc "About %s" [appname]] \
  45                                -command do_about
  46                        $w.mbar.apple add command \
  47                                -label [mc "Show SSH Key"] \
  48                                -command do_ssh_key
  49                } else {
  50                        $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
  51                        menu $w.mbar.help
  52                        $w.mbar.help add command \
  53                                -label [mc "About %s" [appname]] \
  54                                -command do_about
  55                        $w.mbar.help add command \
  56                                -label [mc "Show SSH Key"] \
  57                                -command do_ssh_key
  58                }
  59
  60                wm protocol $top WM_DELETE_WINDOW exit
  61                bind $top <$M1B-q> exit
  62                bind $top <$M1B-Q> exit
  63                bind $top <Key-Escape> exit
  64        } else {
  65                wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
  66                bind $top <Key-Escape> [list destroy $top]
  67                set m_repo {}
  68        }
  69
  70        pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
  71
  72        set w_body $w.body
  73        set opts $w_body.options
  74        ${NS}::frame $w_body
  75        text $opts \
  76                -cursor $::cursor_ptr \
  77                -relief flat \
  78                -background [get_bg_color $w_body] \
  79                -wrap none \
  80                -spacing1 5 \
  81                -width 50 \
  82                -height 3
  83        pack $opts -anchor w -fill x
  84
  85        $opts tag conf link_new -foreground blue -underline 1
  86        $opts tag bind link_new <1> [cb _next new]
  87        $opts insert end [mc "Create New Repository"] link_new
  88        $opts insert end "\n"
  89        if {$m_repo ne {}} {
  90                $m_repo add command \
  91                        -command [cb _next new] \
  92                        -accelerator $M1T-N \
  93                        -label [mc "New..."]
  94                bind $top <$M1B-n> [cb _next new]
  95                bind $top <$M1B-N> [cb _next new]
  96        }
  97
  98        $opts tag conf link_clone -foreground blue -underline 1
  99        $opts tag bind link_clone <1> [cb _next clone]
 100        $opts insert end [mc "Clone Existing Repository"] link_clone
 101        $opts insert end "\n"
 102        if {$m_repo ne {}} {
 103                if {[tk windowingsystem] eq "win32"} {
 104                        set key L
 105                } else {
 106                        set key C
 107                }
 108                $m_repo add command \
 109                        -command [cb _next clone] \
 110                        -accelerator $M1T-$key \
 111                        -label [mc "Clone..."]
 112                bind $top <$M1B-[string tolower $key]> [cb _next clone]
 113                bind $top <$M1B-[string toupper $key]> [cb _next clone]
 114        }
 115
 116        $opts tag conf link_open -foreground blue -underline 1
 117        $opts tag bind link_open <1> [cb _next open]
 118        $opts insert end [mc "Open Existing Repository"] link_open
 119        $opts insert end "\n"
 120        if {$m_repo ne {}} {
 121                $m_repo add command \
 122                        -command [cb _next open] \
 123                        -accelerator $M1T-O \
 124                        -label [mc "Open..."]
 125                bind $top <$M1B-o> [cb _next open]
 126                bind $top <$M1B-O> [cb _next open]
 127        }
 128
 129        $opts conf -state disabled
 130
 131        set sorted_recent [_get_recentrepos]
 132        if {[llength $sorted_recent] > 0} {
 133                if {$m_repo ne {}} {
 134                        $m_repo add separator
 135                        $m_repo add command \
 136                                -state disabled \
 137                                -label [mc "Recent Repositories"]
 138                }
 139
 140                ${NS}::label $w_body.space
 141                ${NS}::label $w_body.recentlabel \
 142                        -anchor w \
 143                        -text [mc "Open Recent Repository:"]
 144                set w_recentlist $w_body.recentlist
 145                text $w_recentlist \
 146                        -cursor $::cursor_ptr \
 147                        -relief flat \
 148                        -background [get_bg_color $w_body.recentlabel] \
 149                        -wrap none \
 150                        -width 50 \
 151                        -height 10
 152                $w_recentlist tag conf link \
 153                        -foreground blue \
 154                        -underline 1
 155                set home $::env(HOME)
 156                if {[is_Cygwin]} {
 157                        set home [exec cygpath --windows --absolute $home]
 158                }
 159                set home "[file normalize $home]/"
 160                set hlen [string length $home]
 161                foreach p $sorted_recent {
 162                        set path $p
 163                        if {[string equal -length $hlen $home $p]} {
 164                                set p "~/[string range $p $hlen end]"
 165                        }
 166                        regsub -all "\n" $p "\\n" p
 167                        $w_recentlist insert end $p link
 168                        $w_recentlist insert end "\n"
 169
 170                        if {$m_repo ne {}} {
 171                                $m_repo add command \
 172                                        -command [cb _open_recent_path $path] \
 173                                        -label "    $p"
 174                        }
 175                }
 176                $w_recentlist conf -state disabled
 177                $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
 178                pack $w_body.space -anchor w -fill x
 179                pack $w_body.recentlabel -anchor w -fill x
 180                pack $w_recentlist -anchor w -fill x
 181        }
 182        pack $w_body -fill x -padx 10 -pady 10
 183
 184        ${NS}::frame $w.buttons
 185        set w_next $w.buttons.next
 186        set w_quit $w.buttons.quit
 187        ${NS}::button $w_quit \
 188                -text [mc "Quit"] \
 189                -command exit
 190        pack $w_quit -side right -padx 5
 191        pack $w.buttons -side bottom -fill x -padx 10 -pady 10
 192
 193        if {$m_repo ne {}} {
 194                $m_repo add separator
 195                $m_repo add command \
 196                        -label [mc Quit] \
 197                        -command exit \
 198                        -accelerator $M1T-Q
 199        }
 200
 201        bind $top <Return> [cb _invoke_next]
 202        bind $top <Visibility> "
 203                [cb _center]
 204                grab $top
 205                focus $top
 206                bind $top <Visibility> {}
 207        "
 208        wm deiconify $top
 209        tkwait variable @done
 210
 211        grab release $top
 212        if {$top eq {.}} {
 213                eval destroy [winfo children $top]
 214        }
 215}
 216
 217method _center {} {
 218        set nx [winfo reqwidth $top]
 219        set ny [winfo reqheight $top]
 220        set rx [expr {([winfo screenwidth  $top] - $nx) / 3}]
 221        set ry [expr {([winfo screenheight $top] - $ny) / 3}]
 222        wm geometry $top [format {+%d+%d} $rx $ry]
 223}
 224
 225method _invoke_next {} {
 226        if {[winfo exists $w_next]} {
 227                uplevel #0 [$w_next cget -command]
 228        }
 229}
 230
 231proc _get_recentrepos {} {
 232        set recent [list]
 233        foreach p [get_config gui.recentrepo] {
 234                if {[_is_git [file join $p .git]]} {
 235                        lappend recent $p
 236                } else {
 237                        _unset_recentrepo $p
 238                }
 239        }
 240        return [lsort $recent]
 241}
 242
 243proc _unset_recentrepo {p} {
 244        regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
 245        git config --global --unset gui.recentrepo "^$p\$"
 246        load_config 1
 247}
 248
 249proc _append_recentrepos {path} {
 250        set path [file normalize $path]
 251        set recent [get_config gui.recentrepo]
 252
 253        if {[lindex $recent end] eq $path} {
 254                return
 255        }
 256
 257        set i [lsearch $recent $path]
 258        if {$i >= 0} {
 259                _unset_recentrepo $path
 260                set recent [lreplace $recent $i $i]
 261        }
 262
 263        lappend recent $path
 264        git config --global --add gui.recentrepo $path
 265        load_config 1
 266
 267        while {[llength $recent] > 10} {
 268                _unset_recentrepo [lindex $recent 0]
 269                set recent [lrange $recent 1 end]
 270        }
 271}
 272
 273method _open_recent {xy} {
 274        set id [lindex [split [$w_recentlist index @$xy] .] 0]
 275        set local_path [lindex $sorted_recent [expr {$id - 1}]]
 276        _do_open2 $this
 277}
 278
 279method _open_recent_path {p} {
 280        set local_path $p
 281        _do_open2 $this
 282}
 283
 284method _next {action} {
 285        global NS
 286        destroy $w_body
 287        if {![winfo exists $w_next]} {
 288                ${NS}::button $w_next -default active
 289                pack $w_next -side right -padx 5 -before $w_quit
 290        }
 291        _do_$action $this
 292}
 293
 294method _write_local_path {args} {
 295        if {$local_path eq {}} {
 296                $w_next conf -state disabled
 297        } else {
 298                $w_next conf -state normal
 299        }
 300}
 301
 302method _git_init {} {
 303        if {[catch {file mkdir $local_path} err]} {
 304                error_popup [strcat \
 305                        [mc "Failed to create repository %s:" $local_path] \
 306                        "\n\n$err"]
 307                return 0
 308        }
 309
 310        if {[catch {cd $local_path} err]} {
 311                error_popup [strcat \
 312                        [mc "Failed to create repository %s:" $local_path] \
 313                        "\n\n$err"]
 314                return 0
 315        }
 316
 317        if {[catch {git init} err]} {
 318                error_popup [strcat \
 319                        [mc "Failed to create repository %s:" $local_path] \
 320                        "\n\n$err"]
 321                return 0
 322        }
 323
 324        _append_recentrepos [pwd]
 325        set ::_gitdir .git
 326        set ::_prefix {}
 327        return 1
 328}
 329
 330proc _is_git {path} {
 331        if {[file exists [file join $path HEAD]]
 332         && [file exists [file join $path objects]]
 333         && [file exists [file join $path config]]} {
 334                return 1
 335        }
 336        if {[is_Cygwin]} {
 337                if {[file exists [file join $path HEAD]]
 338                 && [file exists [file join $path objects.lnk]]
 339                 && [file exists [file join $path config.lnk]]} {
 340                        return 1
 341                }
 342        }
 343        return 0
 344}
 345
 346proc _objdir {path} {
 347        set objdir [file join $path .git objects]
 348        if {[file isdirectory $objdir]} {
 349                return $objdir
 350        }
 351
 352        set objdir [file join $path objects]
 353        if {[file isdirectory $objdir]} {
 354                return $objdir
 355        }
 356
 357        if {[is_Cygwin]} {
 358                set objdir [file join $path .git objects.lnk]
 359                if {[file isfile $objdir]} {
 360                        return [win32_read_lnk $objdir]
 361                }
 362
 363                set objdir [file join $path objects.lnk]
 364                if {[file isfile $objdir]} {
 365                        return [win32_read_lnk $objdir]
 366                }
 367        }
 368
 369        return {}
 370}
 371
 372######################################################################
 373##
 374## Create New Repository
 375
 376method _do_new {} {
 377        global use_ttk NS
 378        $w_next conf \
 379                -state disabled \
 380                -command [cb _do_new2] \
 381                -text [mc "Create"]
 382
 383        ${NS}::frame $w_body
 384        ${NS}::label $w_body.h \
 385                -font font_uibold -anchor center \
 386                -text [mc "Create New Repository"]
 387        pack $w_body.h -side top -fill x -pady 10
 388        pack $w_body -fill x -padx 10
 389
 390        ${NS}::frame $w_body.where
 391        ${NS}::label $w_body.where.l -text [mc "Directory:"]
 392        ${NS}::entry $w_body.where.t \
 393                -textvariable @local_path \
 394                -width 50
 395        ${NS}::button $w_body.where.b \
 396                -text [mc "Browse"] \
 397                -command [cb _new_local_path]
 398        set w_localpath $w_body.where.t
 399
 400        grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
 401        pack $w_body.where -fill x
 402
 403        grid columnconfigure $w_body.where 1 -weight 1
 404
 405        trace add variable @local_path write [cb _write_local_path]
 406        bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
 407        update
 408        focus $w_body.where.t
 409}
 410
 411method _new_local_path {} {
 412        if {$local_path ne {}} {
 413                set p [file dirname $local_path]
 414        } else {
 415                set p [pwd]
 416        }
 417
 418        set p [tk_chooseDirectory \
 419                -initialdir $p \
 420                -parent $top \
 421                -title [mc "Git Repository"] \
 422                -mustexist false]
 423        if {$p eq {}} return
 424
 425        set p [file normalize $p]
 426        if {![_new_ok $p]} {
 427                return
 428        }
 429        set local_path $p
 430        $w_localpath icursor end
 431}
 432
 433method _do_new2 {} {
 434        if {![_new_ok $local_path]} {
 435                return
 436        }
 437        if {![_git_init $this]} {
 438                return
 439        }
 440        set done 1
 441}
 442
 443proc _new_ok {p} {
 444        if {[file isdirectory $p]} {
 445                if {[_is_git [file join $p .git]]} {
 446                        error_popup [mc "Directory %s already exists." $p]
 447                        return 0
 448                }
 449        } elseif {[file exists $p]} {
 450                error_popup [mc "File %s already exists." $p]
 451                return 0
 452        }
 453        return 1
 454}
 455
 456######################################################################
 457##
 458## Clone Existing Repository
 459
 460method _do_clone {} {
 461        global use_ttk NS
 462        $w_next conf \
 463                -state disabled \
 464                -command [cb _do_clone2] \
 465                -text [mc "Clone"]
 466
 467        ${NS}::frame $w_body
 468        ${NS}::label $w_body.h \
 469                -font font_uibold -anchor center \
 470                -text [mc "Clone Existing Repository"]
 471        pack $w_body.h -side top -fill x -pady 10
 472        pack $w_body -fill x -padx 10
 473
 474        set args $w_body.args
 475        ${NS}::frame $w_body.args
 476        pack $args -fill both
 477
 478        ${NS}::label $args.origin_l -text [mc "Source Location:"]
 479        ${NS}::entry $args.origin_t \
 480                -textvariable @origin_url \
 481                -width 50
 482        ${NS}::button $args.origin_b \
 483                -text [mc "Browse"] \
 484                -command [cb _open_origin]
 485        grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
 486
 487        ${NS}::label $args.where_l -text [mc "Target Directory:"]
 488        ${NS}::entry $args.where_t \
 489                -textvariable @local_path \
 490                -width 50
 491        ${NS}::button $args.where_b \
 492                -text [mc "Browse"] \
 493                -command [cb _new_local_path]
 494        grid $args.where_l $args.where_t $args.where_b -sticky ew
 495        set w_localpath $args.where_t
 496
 497        ${NS}::label $args.type_l -text [mc "Clone Type:"]
 498        ${NS}::frame $args.type_f
 499        set w_types [list]
 500        lappend w_types [${NS}::radiobutton $args.type_f.hardlink \
 501                -state disabled \
 502                -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
 503                -variable @clone_type \
 504                -value hardlink]
 505        lappend w_types [${NS}::radiobutton $args.type_f.full \
 506                -state disabled \
 507                -text [mc "Full Copy (Slower, Redundant Backup)"] \
 508                -variable @clone_type \
 509                -value full]
 510        lappend w_types [${NS}::radiobutton $args.type_f.shared \
 511                -state disabled \
 512                -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
 513                -variable @clone_type \
 514                -value shared]
 515        foreach r $w_types {
 516                pack $r -anchor w
 517        }
 518        grid $args.type_l $args.type_f -sticky new
 519
 520        grid columnconfigure $args 1 -weight 1
 521
 522        trace add variable @local_path write [cb _update_clone]
 523        trace add variable @origin_url write [cb _update_clone]
 524        bind $w_body.h <Destroy> "
 525                [list trace remove variable @local_path write [cb _update_clone]]
 526                [list trace remove variable @origin_url write [cb _update_clone]]
 527        "
 528        update
 529        focus $args.origin_t
 530}
 531
 532method _open_origin {} {
 533        if {$origin_url ne {} && [file isdirectory $origin_url]} {
 534                set p $origin_url
 535        } else {
 536                set p [pwd]
 537        }
 538
 539        set p [tk_chooseDirectory \
 540                -initialdir $p \
 541                -parent $top \
 542                -title [mc "Git Repository"] \
 543                -mustexist true]
 544        if {$p eq {}} return
 545
 546        set p [file normalize $p]
 547        if {![_is_git [file join $p .git]] && ![_is_git $p]} {
 548                error_popup [mc "Not a Git repository: %s" [file tail $p]]
 549                return
 550        }
 551        set origin_url $p
 552}
 553
 554method _update_clone {args} {
 555        if {$local_path ne {} && $origin_url ne {}} {
 556                $w_next conf -state normal
 557        } else {
 558                $w_next conf -state disabled
 559        }
 560
 561        if {$origin_url ne {} &&
 562                (  [_is_git [file join $origin_url .git]]
 563                || [_is_git $origin_url])} {
 564                set e normal
 565                if {[[lindex $w_types 0] cget -state] eq {disabled}} {
 566                        set clone_type hardlink
 567                }
 568        } else {
 569                set e disabled
 570                set clone_type full
 571        }
 572
 573        foreach r $w_types {
 574                $r conf -state $e
 575        }
 576}
 577
 578method _do_clone2 {} {
 579        if {[file isdirectory $origin_url]} {
 580                set origin_url [file normalize $origin_url]
 581        }
 582
 583        if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
 584                error_popup [mc "Standard only available for local repository."]
 585                return
 586        }
 587        if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
 588                error_popup [mc "Shared only available for local repository."]
 589                return
 590        }
 591
 592        if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
 593                set objdir [_objdir $origin_url]
 594                if {$objdir eq {}} {
 595                        error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
 596                        return
 597                }
 598        }
 599
 600        set giturl $origin_url
 601        if {[is_Cygwin] && [file isdirectory $giturl]} {
 602                set giturl [exec cygpath --unix --absolute $giturl]
 603                if {$clone_type eq {shared}} {
 604                        set objdir [exec cygpath --unix --absolute $objdir]
 605                }
 606        }
 607
 608        if {[file exists $local_path]} {
 609                error_popup [mc "Location %s already exists." $local_path]
 610                return
 611        }
 612
 613        if {![_git_init $this]} return
 614        set local_path [pwd]
 615
 616        if {[catch {
 617                        git config remote.$origin_name.url $giturl
 618                        git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
 619                } err]} {
 620                error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
 621                return
 622        }
 623
 624        destroy $w_body $w_next
 625
 626        switch -exact -- $clone_type {
 627        hardlink {
 628                set o_cons [status_bar::two_line $w_body]
 629                pack $w_body -fill x -padx 10 -pady 10
 630
 631                $o_cons start \
 632                        [mc "Counting objects"] \
 633                        [mc "buckets"]
 634                update
 635
 636                if {[file exists [file join $objdir info alternates]]} {
 637                        set pwd [pwd]
 638                        if {[catch {
 639                                file mkdir [gitdir objects info]
 640                                set f_in [open [file join $objdir info alternates] r]
 641                                set f_cp [open [gitdir objects info alternates] w]
 642                                fconfigure $f_in -translation binary -encoding binary
 643                                fconfigure $f_cp -translation binary -encoding binary
 644                                cd $objdir
 645                                while {[gets $f_in line] >= 0} {
 646                                        if {[is_Cygwin]} {
 647                                                puts $f_cp [exec cygpath --unix --absolute $line]
 648                                        } else {
 649                                                puts $f_cp [file normalize $line]
 650                                        }
 651                                }
 652                                close $f_in
 653                                close $f_cp
 654                                cd $pwd
 655                        } err]} {
 656                                catch {cd $pwd}
 657                                _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
 658                                return
 659                        }
 660                }
 661
 662                set tolink  [list]
 663                set buckets [glob \
 664                        -tails \
 665                        -nocomplain \
 666                        -directory [file join $objdir] ??]
 667                set bcnt [expr {[llength $buckets] + 2}]
 668                set bcur 1
 669                $o_cons update $bcur $bcnt
 670                update
 671
 672                file mkdir [file join .git objects pack]
 673                foreach i [glob -tails -nocomplain \
 674                        -directory [file join $objdir pack] *] {
 675                        lappend tolink [file join pack $i]
 676                }
 677                $o_cons update [incr bcur] $bcnt
 678                update
 679
 680                foreach i $buckets {
 681                        file mkdir [file join .git objects $i]
 682                        foreach j [glob -tails -nocomplain \
 683                                -directory [file join $objdir $i] *] {
 684                                lappend tolink [file join $i $j]
 685                        }
 686                        $o_cons update [incr bcur] $bcnt
 687                        update
 688                }
 689                $o_cons stop
 690
 691                if {$tolink eq {}} {
 692                        info_popup [strcat \
 693                                [mc "Nothing to clone from %s." $origin_url] \
 694                                "\n" \
 695                                [mc "The 'master' branch has not been initialized."] \
 696                                ]
 697                        destroy $w_body
 698                        set done 1
 699                        return
 700                }
 701
 702                set i [lindex $tolink 0]
 703                if {[catch {
 704                                file link -hard \
 705                                        [file join .git objects $i] \
 706                                        [file join $objdir $i]
 707                        } err]} {
 708                        info_popup [mc "Hardlinks are unavailable.  Falling back to copying."]
 709                        set i [_copy_files $this $objdir $tolink]
 710                } else {
 711                        set i [_link_files $this $objdir [lrange $tolink 1 end]]
 712                }
 713                if {!$i} return
 714
 715                destroy $w_body
 716        }
 717        full {
 718                set o_cons [console::embed \
 719                        $w_body \
 720                        [mc "Cloning from %s" $origin_url]]
 721                pack $w_body -fill both -expand 1 -padx 10
 722                $o_cons exec \
 723                        [list git fetch --no-tags -k $origin_name] \
 724                        [cb _do_clone_tags]
 725        }
 726        shared {
 727                set fd [open [gitdir objects info alternates] w]
 728                fconfigure $fd -translation binary
 729                puts $fd $objdir
 730                close $fd
 731        }
 732        }
 733
 734        if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
 735                if {![_clone_refs $this]} return
 736                set pwd [pwd]
 737                if {[catch {
 738                                cd $origin_url
 739                                set HEAD [git rev-parse --verify HEAD^0]
 740                        } err]} {
 741                        _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
 742                        return 0
 743                }
 744                cd $pwd
 745                _do_clone_checkout $this $HEAD
 746        }
 747}
 748
 749method _copy_files {objdir tocopy} {
 750        $o_cons start \
 751                [mc "Copying objects"] \
 752                [mc "KiB"]
 753        set tot 0
 754        set cmp 0
 755        foreach p $tocopy {
 756                incr tot [file size [file join $objdir $p]]
 757        }
 758        foreach p $tocopy {
 759                if {[catch {
 760                                set f_in [open [file join $objdir $p] r]
 761                                set f_cp [open [file join .git objects $p] w]
 762                                fconfigure $f_in -translation binary -encoding binary
 763                                fconfigure $f_cp -translation binary -encoding binary
 764
 765                                while {![eof $f_in]} {
 766                                        incr cmp [fcopy $f_in $f_cp -size 16384]
 767                                        $o_cons update \
 768                                                [expr {$cmp / 1024}] \
 769                                                [expr {$tot / 1024}]
 770                                        update
 771                                }
 772
 773                                close $f_in
 774                                close $f_cp
 775                        } err]} {
 776                        _clone_failed $this [mc "Unable to copy object: %s" $err]
 777                        return 0
 778                }
 779        }
 780        return 1
 781}
 782
 783method _link_files {objdir tolink} {
 784        set total [llength $tolink]
 785        $o_cons start \
 786                [mc "Linking objects"] \
 787                [mc "objects"]
 788        for {set i 0} {$i < $total} {} {
 789                set p [lindex $tolink $i]
 790                if {[catch {
 791                                file link -hard \
 792                                        [file join .git objects $p] \
 793                                        [file join $objdir $p]
 794                        } err]} {
 795                        _clone_failed $this [mc "Unable to hardlink object: %s" $err]
 796                        return 0
 797                }
 798
 799                incr i
 800                if {$i % 5 == 0} {
 801                        $o_cons update $i $total
 802                        update
 803                }
 804        }
 805        return 1
 806}
 807
 808method _clone_refs {} {
 809        set pwd [pwd]
 810        if {[catch {cd $origin_url} err]} {
 811                error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
 812                return 0
 813        }
 814        set fd_in [git_read for-each-ref \
 815                --tcl \
 816                {--format=list %(refname) %(objectname) %(*objectname)}]
 817        cd $pwd
 818
 819        set fd [open [gitdir packed-refs] w]
 820        fconfigure $fd -translation binary
 821        puts $fd "# pack-refs with: peeled"
 822        while {[gets $fd_in line] >= 0} {
 823                set line [eval $line]
 824                set refn [lindex $line 0]
 825                set robj [lindex $line 1]
 826                set tobj [lindex $line 2]
 827
 828                if {[regsub ^refs/heads/ $refn \
 829                        "refs/remotes/$origin_name/" refn]} {
 830                        puts $fd "$robj $refn"
 831                } elseif {[string match refs/tags/* $refn]} {
 832                        puts $fd "$robj $refn"
 833                        if {$tobj ne {}} {
 834                                puts $fd "^$tobj"
 835                        }
 836                }
 837        }
 838        close $fd_in
 839        close $fd
 840        return 1
 841}
 842
 843method _do_clone_tags {ok} {
 844        if {$ok} {
 845                $o_cons exec \
 846                        [list git fetch --tags -k $origin_name] \
 847                        [cb _do_clone_HEAD]
 848        } else {
 849                $o_cons done $ok
 850                _clone_failed $this [mc "Cannot fetch branches and objects.  See console output for details."]
 851        }
 852}
 853
 854method _do_clone_HEAD {ok} {
 855        if {$ok} {
 856                $o_cons exec \
 857                        [list git fetch $origin_name HEAD] \
 858                        [cb _do_clone_full_end]
 859        } else {
 860                $o_cons done $ok
 861                _clone_failed $this [mc "Cannot fetch tags.  See console output for details."]
 862        }
 863}
 864
 865method _do_clone_full_end {ok} {
 866        $o_cons done $ok
 867
 868        if {$ok} {
 869                destroy $w_body
 870
 871                set HEAD {}
 872                if {[file exists [gitdir FETCH_HEAD]]} {
 873                        set fd [open [gitdir FETCH_HEAD] r]
 874                        while {[gets $fd line] >= 0} {
 875                                if {[regexp "^(.{40})\t\t" $line line HEAD]} {
 876                                        break
 877                                }
 878                        }
 879                        close $fd
 880                }
 881
 882                catch {git pack-refs}
 883                _do_clone_checkout $this $HEAD
 884        } else {
 885                _clone_failed $this [mc "Cannot determine HEAD.  See console output for details."]
 886        }
 887}
 888
 889method _clone_failed {{why {}}} {
 890        if {[catch {file delete -force $local_path} err]} {
 891                set why [strcat \
 892                        $why \
 893                        "\n\n" \
 894                        [mc "Unable to cleanup %s" $local_path] \
 895                        "\n\n" \
 896                        $err]
 897        }
 898        if {$why ne {}} {
 899                update
 900                error_popup [strcat [mc "Clone failed."] "\n" $why]
 901        }
 902}
 903
 904method _do_clone_checkout {HEAD} {
 905        if {$HEAD eq {}} {
 906                info_popup [strcat \
 907                        [mc "No default branch obtained."] \
 908                        "\n" \
 909                        [mc "The 'master' branch has not been initialized."] \
 910                        ]
 911                set done 1
 912                return
 913        }
 914        if {[catch {
 915                        git update-ref HEAD $HEAD^0
 916                } err]} {
 917                info_popup [strcat \
 918                        [mc "Cannot resolve %s as a commit." $HEAD^0] \
 919                        "\n  $err" \
 920                        "\n" \
 921                        [mc "The 'master' branch has not been initialized."] \
 922                        ]
 923                set done 1
 924                return
 925        }
 926
 927        set o_cons [status_bar::two_line $w_body]
 928        pack $w_body -fill x -padx 10 -pady 10
 929        $o_cons start \
 930                [mc "Creating working directory"] \
 931                [mc "files"]
 932
 933        set readtree_err {}
 934        set fd [git_read --stderr read-tree \
 935                -m \
 936                -u \
 937                -v \
 938                HEAD \
 939                HEAD \
 940                ]
 941        fconfigure $fd -blocking 0 -translation binary
 942        fileevent $fd readable [cb _readtree_wait $fd]
 943}
 944
 945method _readtree_wait {fd} {
 946        set buf [read $fd]
 947        $o_cons update_meter $buf
 948        append readtree_err $buf
 949
 950        fconfigure $fd -blocking 1
 951        if {![eof $fd]} {
 952                fconfigure $fd -blocking 0
 953                return
 954        }
 955
 956        if {[catch {close $fd}]} {
 957                set err $readtree_err
 958                regsub {^fatal: } $err {} err
 959                error_popup [strcat \
 960                        [mc "Initial file checkout failed."] \
 961                        "\n\n$err"]
 962                return
 963        }
 964
 965        # -- Run the post-checkout hook.
 966        #
 967        set fd_ph [githook_read post-checkout [string repeat 0 40] \
 968                [git rev-parse HEAD] 1]
 969        if {$fd_ph ne {}} {
 970                global pch_error
 971                set pch_error {}
 972                fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
 973                fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
 974        } else {
 975                set done 1
 976        }
 977}
 978
 979method _postcheckout_wait {fd_ph} {
 980        global pch_error
 981
 982        append pch_error [read $fd_ph]
 983        fconfigure $fd_ph -blocking 1
 984        if {[eof $fd_ph]} {
 985                if {[catch {close $fd_ph}]} {
 986                        hook_failed_popup post-checkout $pch_error 0
 987                }
 988                unset pch_error
 989                set done 1
 990                return
 991        }
 992        fconfigure $fd_ph -blocking 0
 993}
 994
 995######################################################################
 996##
 997## Open Existing Repository
 998
 999method _do_open {} {
1000        global NS
1001        $w_next conf \
1002                -state disabled \
1003                -command [cb _do_open2] \
1004                -text [mc "Open"]
1005
1006        ${NS}::frame $w_body
1007        ${NS}::label $w_body.h \
1008                -font font_uibold -anchor center \
1009                -text [mc "Open Existing Repository"]
1010        pack $w_body.h -side top -fill x -pady 10
1011        pack $w_body -fill x -padx 10
1012
1013        ${NS}::frame $w_body.where
1014        ${NS}::label $w_body.where.l -text [mc "Repository:"]
1015        ${NS}::entry $w_body.where.t \
1016                -textvariable @local_path \
1017                -width 50
1018        ${NS}::button $w_body.where.b \
1019                -text [mc "Browse"] \
1020                -command [cb _open_local_path]
1021
1022        grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1023        pack $w_body.where -fill x
1024
1025        grid columnconfigure $w_body.where 1 -weight 1
1026
1027        trace add variable @local_path write [cb _write_local_path]
1028        bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1029        update
1030        focus $w_body.where.t
1031}
1032
1033method _open_local_path {} {
1034        if {$local_path ne {}} {
1035                set p $local_path
1036        } else {
1037                set p [pwd]
1038        }
1039
1040        set p [tk_chooseDirectory \
1041                -initialdir $p \
1042                -parent $top \
1043                -title [mc "Git Repository"] \
1044                -mustexist true]
1045        if {$p eq {}} return
1046
1047        set p [file normalize $p]
1048        if {![_is_git [file join $p .git]]} {
1049                error_popup [mc "Not a Git repository: %s" [file tail $p]]
1050                return
1051        }
1052        set local_path $p
1053}
1054
1055method _do_open2 {} {
1056        if {![_is_git [file join $local_path .git]]} {
1057                error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1058                return
1059        }
1060
1061        if {[catch {cd $local_path} err]} {
1062                error_popup [strcat \
1063                        [mc "Failed to open repository %s:" $local_path] \
1064                        "\n\n$err"]
1065                return
1066        }
1067
1068        _append_recentrepos [pwd]
1069        set ::_gitdir .git
1070        set ::_prefix {}
1071        set done 1
1072}
1073
1074}