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