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