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