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