git-gui / lib / choose_repository.tclon commit Merge branch 'vd/doc-unpack-objects' into maint (5022b58)
   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                set pos -before
 290                if {[tk windowingsystem] eq "win32"} { set pos -after }
 291                pack $w_next -side right -padx 5 $pos $w_quit
 292        }
 293        _do_$action $this
 294}
 295
 296method _write_local_path {args} {
 297        if {$local_path eq {}} {
 298                $w_next conf -state disabled
 299        } else {
 300                $w_next conf -state normal
 301        }
 302}
 303
 304method _git_init {} {
 305        if {[catch {file mkdir $local_path} err]} {
 306                error_popup [strcat \
 307                        [mc "Failed to create repository %s:" $local_path] \
 308                        "\n\n$err"]
 309                return 0
 310        }
 311
 312        if {[catch {cd $local_path} err]} {
 313                error_popup [strcat \
 314                        [mc "Failed to create repository %s:" $local_path] \
 315                        "\n\n$err"]
 316                return 0
 317        }
 318
 319        if {[catch {git init} err]} {
 320                error_popup [strcat \
 321                        [mc "Failed to create repository %s:" $local_path] \
 322                        "\n\n$err"]
 323                return 0
 324        }
 325
 326        _append_recentrepos [pwd]
 327        set ::_gitdir .git
 328        set ::_prefix {}
 329        return 1
 330}
 331
 332proc _is_git {path} {
 333        if {[file exists [file join $path HEAD]]
 334         && [file exists [file join $path objects]]
 335         && [file exists [file join $path config]]} {
 336                return 1
 337        }
 338        if {[is_Cygwin]} {
 339                if {[file exists [file join $path HEAD]]
 340                 && [file exists [file join $path objects.lnk]]
 341                 && [file exists [file join $path config.lnk]]} {
 342                        return 1
 343                }
 344        }
 345        return 0
 346}
 347
 348proc _objdir {path} {
 349        set objdir [file join $path .git objects]
 350        if {[file isdirectory $objdir]} {
 351                return $objdir
 352        }
 353
 354        set objdir [file join $path objects]
 355        if {[file isdirectory $objdir]} {
 356                return $objdir
 357        }
 358
 359        if {[is_Cygwin]} {
 360                set objdir [file join $path .git objects.lnk]
 361                if {[file isfile $objdir]} {
 362                        return [win32_read_lnk $objdir]
 363                }
 364
 365                set objdir [file join $path objects.lnk]
 366                if {[file isfile $objdir]} {
 367                        return [win32_read_lnk $objdir]
 368                }
 369        }
 370
 371        return {}
 372}
 373
 374######################################################################
 375##
 376## Create New Repository
 377
 378method _do_new {} {
 379        global use_ttk NS
 380        $w_next conf \
 381                -state disabled \
 382                -command [cb _do_new2] \
 383                -text [mc "Create"]
 384
 385        ${NS}::frame $w_body
 386        ${NS}::label $w_body.h \
 387                -font font_uibold -anchor center \
 388                -text [mc "Create New Repository"]
 389        pack $w_body.h -side top -fill x -pady 10
 390        pack $w_body -fill x -padx 10
 391
 392        ${NS}::frame $w_body.where
 393        ${NS}::label $w_body.where.l -text [mc "Directory:"]
 394        ${NS}::entry $w_body.where.t \
 395                -textvariable @local_path \
 396                -width 50
 397        ${NS}::button $w_body.where.b \
 398                -text [mc "Browse"] \
 399                -command [cb _new_local_path]
 400        set w_localpath $w_body.where.t
 401
 402        grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
 403        pack $w_body.where -fill x
 404
 405        grid columnconfigure $w_body.where 1 -weight 1
 406
 407        trace add variable @local_path write [cb _write_local_path]
 408        bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
 409        update
 410        focus $w_body.where.t
 411}
 412
 413method _new_local_path {} {
 414        if {$local_path ne {}} {
 415                set p [file dirname $local_path]
 416        } else {
 417                set p [pwd]
 418        }
 419
 420        set p [tk_chooseDirectory \
 421                -initialdir $p \
 422                -parent $top \
 423                -title [mc "Git Repository"] \
 424                -mustexist false]
 425        if {$p eq {}} return
 426
 427        set p [file normalize $p]
 428        if {![_new_ok $p]} {
 429                return
 430        }
 431        set local_path $p
 432        $w_localpath icursor end
 433}
 434
 435method _do_new2 {} {
 436        if {![_new_ok $local_path]} {
 437                return
 438        }
 439        if {![_git_init $this]} {
 440                return
 441        }
 442        set done 1
 443}
 444
 445proc _new_ok {p} {
 446        if {[file isdirectory $p]} {
 447                if {[_is_git [file join $p .git]]} {
 448                        error_popup [mc "Directory %s already exists." $p]
 449                        return 0
 450                }
 451        } elseif {[file exists $p]} {
 452                error_popup [mc "File %s already exists." $p]
 453                return 0
 454        }
 455        return 1
 456}
 457
 458######################################################################
 459##
 460## Clone Existing Repository
 461
 462method _do_clone {} {
 463        global use_ttk NS
 464        $w_next conf \
 465                -state disabled \
 466                -command [cb _do_clone2] \
 467                -text [mc "Clone"]
 468
 469        ${NS}::frame $w_body
 470        ${NS}::label $w_body.h \
 471                -font font_uibold -anchor center \
 472                -text [mc "Clone Existing Repository"]
 473        pack $w_body.h -side top -fill x -pady 10
 474        pack $w_body -fill x -padx 10
 475
 476        set args $w_body.args
 477        ${NS}::frame $w_body.args
 478        pack $args -fill both
 479
 480        ${NS}::label $args.origin_l -text [mc "Source Location:"]
 481        ${NS}::entry $args.origin_t \
 482                -textvariable @origin_url \
 483                -width 50
 484        ${NS}::button $args.origin_b \
 485                -text [mc "Browse"] \
 486                -command [cb _open_origin]
 487        grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
 488
 489        ${NS}::label $args.where_l -text [mc "Target Directory:"]
 490        ${NS}::entry $args.where_t \
 491                -textvariable @local_path \
 492                -width 50
 493        ${NS}::button $args.where_b \
 494                -text [mc "Browse"] \
 495                -command [cb _new_local_path]
 496        grid $args.where_l $args.where_t $args.where_b -sticky ew
 497        set w_localpath $args.where_t
 498
 499        ${NS}::label $args.type_l -text [mc "Clone Type:"]
 500        ${NS}::frame $args.type_f
 501        set w_types [list]
 502        lappend w_types [${NS}::radiobutton $args.type_f.hardlink \
 503                -state disabled \
 504                -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
 505                -variable @clone_type \
 506                -value hardlink]
 507        lappend w_types [${NS}::radiobutton $args.type_f.full \
 508                -state disabled \
 509                -text [mc "Full Copy (Slower, Redundant Backup)"] \
 510                -variable @clone_type \
 511                -value full]
 512        lappend w_types [${NS}::radiobutton $args.type_f.shared \
 513                -state disabled \
 514                -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
 515                -variable @clone_type \
 516                -value shared]
 517        foreach r $w_types {
 518                pack $r -anchor w
 519        }
 520        grid $args.type_l $args.type_f -sticky new
 521
 522        grid columnconfigure $args 1 -weight 1
 523
 524        trace add variable @local_path write [cb _update_clone]
 525        trace add variable @origin_url write [cb _update_clone]
 526        bind $w_body.h <Destroy> "
 527                [list trace remove variable @local_path write [cb _update_clone]]
 528                [list trace remove variable @origin_url write [cb _update_clone]]
 529        "
 530        update
 531        focus $args.origin_t
 532}
 533
 534method _open_origin {} {
 535        if {$origin_url ne {} && [file isdirectory $origin_url]} {
 536                set p $origin_url
 537        } else {
 538                set p [pwd]
 539        }
 540
 541        set p [tk_chooseDirectory \
 542                -initialdir $p \
 543                -parent $top \
 544                -title [mc "Git Repository"] \
 545                -mustexist true]
 546        if {$p eq {}} return
 547
 548        set p [file normalize $p]
 549        if {![_is_git [file join $p .git]] && ![_is_git $p]} {
 550                error_popup [mc "Not a Git repository: %s" [file tail $p]]
 551                return
 552        }
 553        set origin_url $p
 554}
 555
 556method _update_clone {args} {
 557        if {$local_path ne {} && $origin_url ne {}} {
 558                $w_next conf -state normal
 559        } else {
 560                $w_next conf -state disabled
 561        }
 562
 563        if {$origin_url ne {} &&
 564                (  [_is_git [file join $origin_url .git]]
 565                || [_is_git $origin_url])} {
 566                set e normal
 567                if {[[lindex $w_types 0] cget -state] eq {disabled}} {
 568                        set clone_type hardlink
 569                }
 570        } else {
 571                set e disabled
 572                set clone_type full
 573        }
 574
 575        foreach r $w_types {
 576                $r conf -state $e
 577        }
 578}
 579
 580method _do_clone2 {} {
 581        if {[file isdirectory $origin_url]} {
 582                set origin_url [file normalize $origin_url]
 583        }
 584
 585        if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
 586                error_popup [mc "Standard only available for local repository."]
 587                return
 588        }
 589        if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
 590                error_popup [mc "Shared only available for local repository."]
 591                return
 592        }
 593
 594        if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
 595                set objdir [_objdir $origin_url]
 596                if {$objdir eq {}} {
 597                        error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
 598                        return
 599                }
 600        }
 601
 602        set giturl $origin_url
 603        if {[is_Cygwin] && [file isdirectory $giturl]} {
 604                set giturl [exec cygpath --unix --absolute $giturl]
 605                if {$clone_type eq {shared}} {
 606                        set objdir [exec cygpath --unix --absolute $objdir]
 607                }
 608        }
 609
 610        if {[file exists $local_path]} {
 611                error_popup [mc "Location %s already exists." $local_path]
 612                return
 613        }
 614
 615        if {![_git_init $this]} return
 616        set local_path [pwd]
 617
 618        if {[catch {
 619                        git config remote.$origin_name.url $giturl
 620                        git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
 621                } err]} {
 622                error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
 623                return
 624        }
 625
 626        destroy $w_body $w_next
 627
 628        switch -exact -- $clone_type {
 629        hardlink {
 630                set o_cons [status_bar::two_line $w_body]
 631                pack $w_body -fill x -padx 10 -pady 10
 632
 633                $o_cons start \
 634                        [mc "Counting objects"] \
 635                        [mc "buckets"]
 636                update
 637
 638                if {[file exists [file join $objdir info alternates]]} {
 639                        set pwd [pwd]
 640                        if {[catch {
 641                                file mkdir [gitdir objects info]
 642                                set f_in [open [file join $objdir info alternates] r]
 643                                set f_cp [open [gitdir objects info alternates] w]
 644                                fconfigure $f_in -translation binary -encoding binary
 645                                fconfigure $f_cp -translation binary -encoding binary
 646                                cd $objdir
 647                                while {[gets $f_in line] >= 0} {
 648                                        if {[is_Cygwin]} {
 649                                                puts $f_cp [exec cygpath --unix --absolute $line]
 650                                        } else {
 651                                                puts $f_cp [file normalize $line]
 652                                        }
 653                                }
 654                                close $f_in
 655                                close $f_cp
 656                                cd $pwd
 657                        } err]} {
 658                                catch {cd $pwd}
 659                                _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
 660                                return
 661                        }
 662                }
 663
 664                set tolink  [list]
 665                set buckets [glob \
 666                        -tails \
 667                        -nocomplain \
 668                        -directory [file join $objdir] ??]
 669                set bcnt [expr {[llength $buckets] + 2}]
 670                set bcur 1
 671                $o_cons update $bcur $bcnt
 672                update
 673
 674                file mkdir [file join .git objects pack]
 675                foreach i [glob -tails -nocomplain \
 676                        -directory [file join $objdir pack] *] {
 677                        lappend tolink [file join pack $i]
 678                }
 679                $o_cons update [incr bcur] $bcnt
 680                update
 681
 682                foreach i $buckets {
 683                        file mkdir [file join .git objects $i]
 684                        foreach j [glob -tails -nocomplain \
 685                                -directory [file join $objdir $i] *] {
 686                                lappend tolink [file join $i $j]
 687                        }
 688                        $o_cons update [incr bcur] $bcnt
 689                        update
 690                }
 691                $o_cons stop
 692
 693                if {$tolink eq {}} {
 694                        info_popup [strcat \
 695                                [mc "Nothing to clone from %s." $origin_url] \
 696                                "\n" \
 697                                [mc "The 'master' branch has not been initialized."] \
 698                                ]
 699                        destroy $w_body
 700                        set done 1
 701                        return
 702                }
 703
 704                set i [lindex $tolink 0]
 705                if {[catch {
 706                                file link -hard \
 707                                        [file join .git objects $i] \
 708                                        [file join $objdir $i]
 709                        } err]} {
 710                        info_popup [mc "Hardlinks are unavailable.  Falling back to copying."]
 711                        set i [_copy_files $this $objdir $tolink]
 712                } else {
 713                        set i [_link_files $this $objdir [lrange $tolink 1 end]]
 714                }
 715                if {!$i} return
 716
 717                destroy $w_body
 718        }
 719        full {
 720                set o_cons [console::embed \
 721                        $w_body \
 722                        [mc "Cloning from %s" $origin_url]]
 723                pack $w_body -fill both -expand 1 -padx 10
 724                $o_cons exec \
 725                        [list git fetch --no-tags -k $origin_name] \
 726                        [cb _do_clone_tags]
 727        }
 728        shared {
 729                set fd [open [gitdir objects info alternates] w]
 730                fconfigure $fd -translation binary
 731                puts $fd $objdir
 732                close $fd
 733        }
 734        }
 735
 736        if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
 737                if {![_clone_refs $this]} return
 738                set pwd [pwd]
 739                if {[catch {
 740                                cd $origin_url
 741                                set HEAD [git rev-parse --verify HEAD^0]
 742                        } err]} {
 743                        _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
 744                        return 0
 745                }
 746                cd $pwd
 747                _do_clone_checkout $this $HEAD
 748        }
 749}
 750
 751method _copy_files {objdir tocopy} {
 752        $o_cons start \
 753                [mc "Copying objects"] \
 754                [mc "KiB"]
 755        set tot 0
 756        set cmp 0
 757        foreach p $tocopy {
 758                incr tot [file size [file join $objdir $p]]
 759        }
 760        foreach p $tocopy {
 761                if {[catch {
 762                                set f_in [open [file join $objdir $p] r]
 763                                set f_cp [open [file join .git objects $p] w]
 764                                fconfigure $f_in -translation binary -encoding binary
 765                                fconfigure $f_cp -translation binary -encoding binary
 766
 767                                while {![eof $f_in]} {
 768                                        incr cmp [fcopy $f_in $f_cp -size 16384]
 769                                        $o_cons update \
 770                                                [expr {$cmp / 1024}] \
 771                                                [expr {$tot / 1024}]
 772                                        update
 773                                }
 774
 775                                close $f_in
 776                                close $f_cp
 777                        } err]} {
 778                        _clone_failed $this [mc "Unable to copy object: %s" $err]
 779                        return 0
 780                }
 781        }
 782        return 1
 783}
 784
 785method _link_files {objdir tolink} {
 786        set total [llength $tolink]
 787        $o_cons start \
 788                [mc "Linking objects"] \
 789                [mc "objects"]
 790        for {set i 0} {$i < $total} {} {
 791                set p [lindex $tolink $i]
 792                if {[catch {
 793                                file link -hard \
 794                                        [file join .git objects $p] \
 795                                        [file join $objdir $p]
 796                        } err]} {
 797                        _clone_failed $this [mc "Unable to hardlink object: %s" $err]
 798                        return 0
 799                }
 800
 801                incr i
 802                if {$i % 5 == 0} {
 803                        $o_cons update $i $total
 804                        update
 805                }
 806        }
 807        return 1
 808}
 809
 810method _clone_refs {} {
 811        set pwd [pwd]
 812        if {[catch {cd $origin_url} err]} {
 813                error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
 814                return 0
 815        }
 816        set fd_in [git_read for-each-ref \
 817                --tcl \
 818                {--format=list %(refname) %(objectname) %(*objectname)}]
 819        cd $pwd
 820
 821        set fd [open [gitdir packed-refs] w]
 822        fconfigure $fd -translation binary
 823        puts $fd "# pack-refs with: peeled"
 824        while {[gets $fd_in line] >= 0} {
 825                set line [eval $line]
 826                set refn [lindex $line 0]
 827                set robj [lindex $line 1]
 828                set tobj [lindex $line 2]
 829
 830                if {[regsub ^refs/heads/ $refn \
 831                        "refs/remotes/$origin_name/" refn]} {
 832                        puts $fd "$robj $refn"
 833                } elseif {[string match refs/tags/* $refn]} {
 834                        puts $fd "$robj $refn"
 835                        if {$tobj ne {}} {
 836                                puts $fd "^$tobj"
 837                        }
 838                }
 839        }
 840        close $fd_in
 841        close $fd
 842        return 1
 843}
 844
 845method _do_clone_tags {ok} {
 846        if {$ok} {
 847                $o_cons exec \
 848                        [list git fetch --tags -k $origin_name] \
 849                        [cb _do_clone_HEAD]
 850        } else {
 851                $o_cons done $ok
 852                _clone_failed $this [mc "Cannot fetch branches and objects.  See console output for details."]
 853        }
 854}
 855
 856method _do_clone_HEAD {ok} {
 857        if {$ok} {
 858                $o_cons exec \
 859                        [list git fetch $origin_name HEAD] \
 860                        [cb _do_clone_full_end]
 861        } else {
 862                $o_cons done $ok
 863                _clone_failed $this [mc "Cannot fetch tags.  See console output for details."]
 864        }
 865}
 866
 867method _do_clone_full_end {ok} {
 868        $o_cons done $ok
 869
 870        if {$ok} {
 871                destroy $w_body
 872
 873                set HEAD {}
 874                if {[file exists [gitdir FETCH_HEAD]]} {
 875                        set fd [open [gitdir FETCH_HEAD] r]
 876                        while {[gets $fd line] >= 0} {
 877                                if {[regexp "^(.{40})\t\t" $line line HEAD]} {
 878                                        break
 879                                }
 880                        }
 881                        close $fd
 882                }
 883
 884                catch {git pack-refs}
 885                _do_clone_checkout $this $HEAD
 886        } else {
 887                _clone_failed $this [mc "Cannot determine HEAD.  See console output for details."]
 888        }
 889}
 890
 891method _clone_failed {{why {}}} {
 892        if {[catch {file delete -force $local_path} err]} {
 893                set why [strcat \
 894                        $why \
 895                        "\n\n" \
 896                        [mc "Unable to cleanup %s" $local_path] \
 897                        "\n\n" \
 898                        $err]
 899        }
 900        if {$why ne {}} {
 901                update
 902                error_popup [strcat [mc "Clone failed."] "\n" $why]
 903        }
 904}
 905
 906method _do_clone_checkout {HEAD} {
 907        if {$HEAD eq {}} {
 908                info_popup [strcat \
 909                        [mc "No default branch obtained."] \
 910                        "\n" \
 911                        [mc "The 'master' branch has not been initialized."] \
 912                        ]
 913                set done 1
 914                return
 915        }
 916        if {[catch {
 917                        git update-ref HEAD $HEAD^0
 918                } err]} {
 919                info_popup [strcat \
 920                        [mc "Cannot resolve %s as a commit." $HEAD^0] \
 921                        "\n  $err" \
 922                        "\n" \
 923                        [mc "The 'master' branch has not been initialized."] \
 924                        ]
 925                set done 1
 926                return
 927        }
 928
 929        set o_cons [status_bar::two_line $w_body]
 930        pack $w_body -fill x -padx 10 -pady 10
 931        $o_cons start \
 932                [mc "Creating working directory"] \
 933                [mc "files"]
 934
 935        set readtree_err {}
 936        set fd [git_read --stderr read-tree \
 937                -m \
 938                -u \
 939                -v \
 940                HEAD \
 941                HEAD \
 942                ]
 943        fconfigure $fd -blocking 0 -translation binary
 944        fileevent $fd readable [cb _readtree_wait $fd]
 945}
 946
 947method _readtree_wait {fd} {
 948        set buf [read $fd]
 949        $o_cons update_meter $buf
 950        append readtree_err $buf
 951
 952        fconfigure $fd -blocking 1
 953        if {![eof $fd]} {
 954                fconfigure $fd -blocking 0
 955                return
 956        }
 957
 958        if {[catch {close $fd}]} {
 959                set err $readtree_err
 960                regsub {^fatal: } $err {} err
 961                error_popup [strcat \
 962                        [mc "Initial file checkout failed."] \
 963                        "\n\n$err"]
 964                return
 965        }
 966
 967        # -- Run the post-checkout hook.
 968        #
 969        set fd_ph [githook_read post-checkout [string repeat 0 40] \
 970                [git rev-parse HEAD] 1]
 971        if {$fd_ph ne {}} {
 972                global pch_error
 973                set pch_error {}
 974                fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
 975                fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
 976        } else {
 977                set done 1
 978        }
 979}
 980
 981method _postcheckout_wait {fd_ph} {
 982        global pch_error
 983
 984        append pch_error [read $fd_ph]
 985        fconfigure $fd_ph -blocking 1
 986        if {[eof $fd_ph]} {
 987                if {[catch {close $fd_ph}]} {
 988                        hook_failed_popup post-checkout $pch_error 0
 989                }
 990                unset pch_error
 991                set done 1
 992                return
 993        }
 994        fconfigure $fd_ph -blocking 0
 995}
 996
 997######################################################################
 998##
 999## Open Existing Repository
1000
1001method _do_open {} {
1002        global NS
1003        $w_next conf \
1004                -state disabled \
1005                -command [cb _do_open2] \
1006                -text [mc "Open"]
1007
1008        ${NS}::frame $w_body
1009        ${NS}::label $w_body.h \
1010                -font font_uibold -anchor center \
1011                -text [mc "Open Existing Repository"]
1012        pack $w_body.h -side top -fill x -pady 10
1013        pack $w_body -fill x -padx 10
1014
1015        ${NS}::frame $w_body.where
1016        ${NS}::label $w_body.where.l -text [mc "Repository:"]
1017        ${NS}::entry $w_body.where.t \
1018                -textvariable @local_path \
1019                -width 50
1020        ${NS}::button $w_body.where.b \
1021                -text [mc "Browse"] \
1022                -command [cb _open_local_path]
1023
1024        grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1025        pack $w_body.where -fill x
1026
1027        grid columnconfigure $w_body.where 1 -weight 1
1028
1029        trace add variable @local_path write [cb _write_local_path]
1030        bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1031        update
1032        focus $w_body.where.t
1033}
1034
1035method _open_local_path {} {
1036        if {$local_path ne {}} {
1037                set p $local_path
1038        } else {
1039                set p [pwd]
1040        }
1041
1042        set p [tk_chooseDirectory \
1043                -initialdir $p \
1044                -parent $top \
1045                -title [mc "Git Repository"] \
1046                -mustexist true]
1047        if {$p eq {}} return
1048
1049        set p [file normalize $p]
1050        if {![_is_git [file join $p .git]]} {
1051                error_popup [mc "Not a Git repository: %s" [file tail $p]]
1052                return
1053        }
1054        set local_path $p
1055}
1056
1057method _do_open2 {} {
1058        if {![_is_git [file join $local_path .git]]} {
1059                error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1060                return
1061        }
1062
1063        if {[catch {cd $local_path} err]} {
1064                error_popup [strcat \
1065                        [mc "Failed to open repository %s:" $local_path] \
1066                        "\n\n$err"]
1067                return
1068        }
1069
1070        _append_recentrepos [pwd]
1071        set ::_gitdir .git
1072        set ::_prefix {}
1073        set done 1
1074}
1075
1076}