git-gui: use git-clone

git-gui clones a repository by invoking git-plumbing commands, in proc do_clone, rather than using git-clone. The justification was that the low-level commands are guaranteed to provide a stable interface, while the higher level commands such as git-clone may not be stable. This approach requires git-gui to continually evolve by mirroring new features in git itself, which has not happened, while the user interface in git-clone has proven very stable. Also, git-gui does directly call many other non-plumbing commands in git's repertoire. do_clone's last significant functionality change was in 2015, and updates are required for shallow clones, the reftable backend, cloning from linked worktrees, and perhaps other features and bugs. For instance, I had reports of git-gui failing to correctly clone repositories prior to 2015, resulting in essentially the patch given here. The only significant work was supporting .gitfile linked worktrees unknown to do_clone, but supported by git-clone, and none regarding the interface to git-clone itself. That interface is clearly stable enough to not be a problem. Supporting new use-cases with this requires exposing new options in the clone dialog, then passing flags to git-clone. This avoids updating do_clone to understand those options, reducing the maintenance burdens. So, teach git-gui to use git-clone. This change is in one patch as there is no obvious incremental path to migration. The existing dialog / options / status screen are unchanged, the known user-visible changes are that cloning from a working directory linked by a gitfile now works, there is no auto-fallback to a full copy when cloning linked workdirs and worktrees (meaning git-clone fails unless a full or shared copy is selected), and messages displayed are from git-clone. Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>

Mark Levedahl committed Feb 9, 2024 at 18:07 UTC 6ff8d68ec1adf170aa57630989fde092d18e02de
2 files changed +34 -435
git-gui.sh
+3
@@ -1215,6 +1215,9 @@ if {[catch {
1215 load_config 1
1216 apply_config
1217 choose_repository::pick
1218 + if {![file isdirectory $_gitdir]} {
1219 + exit 1
1220 + }
1221 set picked 1
1222 }
1223
lib/choose_repository.tcl
+31 -435
@@ -10,22 +10,12 @@ field w_next ; # Next button
10 field w_quit ; # Quit button
11 field o_cons ; # Console object (if active)
12
13 -# Status mega-widget instance during _do_clone2 (used by _copy_files and
14 -# _link_files). Widget is destroyed before _do_clone2 calls
15 -# _do_clone_checkout
16 -field o_status
17 -
18 -# Operation displayed by status mega-widget during _do_clone_checkout =>
19 -# _readtree_wait => _postcheckout_wait => _do_clone_submodules =>
20 -# _do_validate_submodule_cloning. The status mega-widget is a different
21 -# instance than that stored in $o_status in earlier operations.
22 -field o_status_op
23 -
13 field w_types ; # List of type buttons in clone
14 field w_recentlist ; # Listbox containing recent repositories
15 field w_localpath ; # Entry widget bound to local_path
16
17 field done 0 ; # Finished picking the repository?
18 +field clone_ok false ; # clone succeeeded
19 field local_path {} ; # Where this repository is locally
20 field origin_url {} ; # Where we are cloning from
21 field origin_name origin ; # What we shall call 'origin'
@@ -353,20 +343,6 @@ proc _is_git {path {outdir_var ""}} {
343 return 1
344 }
345
356 -proc _objdir {path} {
357 - set objdir [file join $path .git objects]
358 - if {[file isdirectory $objdir]} {
359 - return $objdir
360 - }
361 -
362 - set objdir [file join $path objects]
363 - if {[file isdirectory $objdir]} {
364 - return $objdir
365 - }
366 -
367 - return {}
368 -}
369 -
346 ######################################################################
347 ##
348 ## Create New Repository
@@ -592,14 +568,6 @@ method _do_clone2 {} {
568 return
569 }
570
595 - if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
596 - set objdir [_objdir $origin_url]
597 - if {$objdir eq {}} {
598 - error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
599 - return
600 - }
601 - }
602 -
571 set giturl $origin_url
572
573 if {[file exists $local_path]} {
@@ -607,434 +575,62 @@ method _do_clone2 {} {
575 return
576 }
577
610 - if {![_git_init $this]} return
611 - set local_path [pwd]
612 -
613 - if {[catch {
614 - git config remote.$origin_name.url $giturl
615 - git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
616 - } err]} {
617 - error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
618 - return
578 + set clone_options {--progress}
579 + if {$recursive} {
580 + append clone_options { --recurse-submodules}
581 }
582
583 destroy $w_body $w_next
584
585 switch -exact -- $clone_type {
624 - hardlink {
625 - set o_status [status_bar::two_line $w_body]
626 - pack $w_body -fill x -padx 10 -pady 10
627 -
628 - set status_op [$o_status start \
629 - [mc "Counting objects"] \
630 - [mc "buckets"]]
631 - update
632 -
633 - if {[file exists [file join $objdir info alternates]]} {
634 - set pwd [pwd]
635 - if {[catch {
636 - file mkdir [gitdir objects info]
637 - set f_in [safe_open_file [file join $objdir info alternates] r]
638 - set f_cp [safe_open_file [gitdir objects info alternates] w]
639 - fconfigure $f_in -translation binary -encoding binary
640 - fconfigure $f_cp -translation binary -encoding binary
641 - cd $objdir
642 - while {[gets $f_in line] >= 0} {
643 - puts $f_cp [file normalize $line]
644 - }
645 - close $f_in
646 - close $f_cp
647 - cd $pwd
648 - } err]} {
649 - catch {cd $pwd}
650 - _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
651 - $status_op stop
652 - return
653 - }
586 + full {
587 + append clone_options { --no-hardlinks --no-local}
588 }
655 -
656 - set tolink [list]
657 - set buckets [glob \
658 - -tails \
659 - -nocomplain \
660 - -directory [file join $objdir] ??]
661 - set bcnt [expr {[llength $buckets] + 2}]
662 - set bcur 1
663 - $status_op update $bcur $bcnt
664 - update
665 -
666 - file mkdir [file join .git objects pack]
667 - foreach i [glob -tails -nocomplain \
668 - -directory [file join $objdir pack] *] {
669 - lappend tolink [file join pack $i]
670 - }
671 - $status_op update [incr bcur] $bcnt
672 - update
673 -
674 - foreach i $buckets {
675 - file mkdir [file join .git objects $i]
676 - foreach j [glob -tails -nocomplain \
677 - -directory [file join $objdir $i] *] {
678 - lappend tolink [file join $i $j]
679 - }
680 - $status_op update [incr bcur] $bcnt
681 - update
682 - }
683 - $status_op stop
684 -
685 - if {$tolink eq {}} {
686 - info_popup [strcat \
687 - [mc "Nothing to clone from %s." $origin_url] \
688 - "\n" \
689 - [mc "The 'master' branch has not been initialized."] \
690 - ]
691 - destroy $w_body
692 - set done 1
693 - return
694 - }
695 -
696 - set i [lindex $tolink 0]
697 - if {[catch {
698 - file link -hard \
699 - [file join .git objects $i] \
700 - [file join $objdir $i]
701 - } err]} {
702 - info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
703 - set i [_copy_files $this $objdir $tolink]
704 - } else {
705 - set i [_link_files $this $objdir [lrange $tolink 1 end]]
589 + shared {
590 + append clone_options { --shared}
591 }
707 - if {!$i} return
708 -
709 - destroy $w_body
710 -
711 - set o_status {}
592 }
713 - full {
593 +
594 + if {[catch {
595 set o_cons [console::embed \
596 $w_body \
597 [mc "Cloning from %s" $origin_url]]
598 pack $w_body -fill both -expand 1 -padx 10
599 $o_cons exec \
719 - [list git fetch --no-tags -k $origin_name] \
720 - [cb _do_clone_tags]
721 - }
722 - shared {
723 - set fd [safe_open_file [gitdir objects info alternates] w]
724 - fconfigure $fd -translation binary
725 - puts $fd $objdir
726 - close $fd
727 - }
728 - }
729 -
730 - if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
731 - if {![_clone_refs $this]} return
732 - set pwd [pwd]
733 - if {[catch {
734 - cd $origin_url
735 - set HEAD [git rev-parse --verify HEAD^0]
736 - } err]} {
737 - _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
738 - return 0
739 - }
740 - cd $pwd
741 - _do_clone_checkout $this $HEAD
742 - }
743 -}
744 -
745 -method _copy_files {objdir tocopy} {
746 - set status_op [$o_status start \
747 - [mc "Copying objects"] \
748 - [mc "KiB"]]
749 - set tot 0
750 - set cmp 0
751 - foreach p $tocopy {
752 - incr tot [file size [file join $objdir $p]]
753 - }
754 - foreach p $tocopy {
755 - if {[catch {
756 - set f_in [safe_open_file [file join $objdir $p] r]
757 - set f_cp [safe_open_file [file join .git objects $p] w]
758 - fconfigure $f_in -translation binary -encoding binary
759 - fconfigure $f_cp -translation binary -encoding binary
760 -
761 - while {![eof $f_in]} {
762 - incr cmp [fcopy $f_in $f_cp -size 16384]
763 - $status_op update \
764 - [expr {$cmp / 1024}] \
765 - [expr {$tot / 1024}]
766 - update
767 - }
768 -
769 - close $f_in
770 - close $f_cp
771 - } err]} {
772 - _clone_failed $this [mc "Unable to copy object: %s" $err]
773 - $status_op stop
774 - return 0
775 - }
776 - }
777 - $status_op stop
778 - return 1
779 -}
780 -
781 -method _link_files {objdir tolink} {
782 - set total [llength $tolink]
783 - set status_op [$o_status start \
784 - [mc "Linking objects"] \
785 - [mc "objects"]]
786 - for {set i 0} {$i < $total} {} {
787 - set p [lindex $tolink $i]
788 - if {[catch {
789 - file link -hard \
790 - [file join .git objects $p] \
791 - [file join $objdir $p]
792 - } err]} {
793 - _clone_failed $this [mc "Unable to hardlink object: %s" $err]
794 - $status_op stop
795 - return 0
796 - }
797 -
798 - incr i
799 - if {$i % 5 == 0} {
800 - $status_op update $i $total
801 - update
802 - }
803 - }
804 - $status_op stop
805 - return 1
806 -}
807 -
808 -method _clone_refs {} {
809 - set pwd [pwd]
810 - if {[catch {cd $origin_url} err]} {
811 - error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
812 - return 0
813 - }
814 - set fd_in [git_read [list for-each-ref \
815 - --tcl \
816 - {--format=list %(refname) %(objectname) %(*objectname)}]]
817 - cd $pwd
818 -
819 - set fd [safe_open_file [gitdir packed-refs] w]
820 - fconfigure $fd -translation binary
821 - puts $fd "# pack-refs with: peeled"
822 - while {[gets $fd_in line] >= 0} {
823 - set line [eval $line]
824 - set refn [lindex $line 0]
825 - set robj [lindex $line 1]
826 - set tobj [lindex $line 2]
827 -
828 - if {[regsub ^refs/heads/ $refn \
829 - "refs/remotes/$origin_name/" refn]} {
830 - puts $fd "$robj $refn"
831 - } elseif {[string match refs/tags/* $refn]} {
832 - puts $fd "$robj $refn"
833 - if {$tobj ne {}} {
834 - puts $fd "^$tobj"
835 - }
836 - }
837 - }
838 - close $fd_in
839 - close $fd
840 - return 1
841 -}
842 -
843 -method _do_clone_tags {ok} {
844 - if {$ok} {
845 - $o_cons exec \
846 - [list git fetch --tags -k $origin_name] \
847 - [cb _do_clone_HEAD]
848 - } else {
849 - $o_cons done $ok
850 - _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
600 + [list git clone {*}$clone_options $origin_url $local_path] \
601 + [cb _do_clone2_done]
602 + } err]} {
603 + error_popup [strcat [mc "Clone failed."] "\n" $err]
604 + return
605 }
852 -}
606
854 -method _do_clone_HEAD {ok} {
855 - if {$ok} {
856 - $o_cons exec \
857 - [list git fetch $origin_name HEAD] \
858 - [cb _do_clone_full_end]
859 - } else {
860 - $o_cons done $ok
861 - _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
607 + tkwait variable @done
608 + if {!$clone_ok} {
609 + error_popup [mc "Clone failed."]
610 + return
611 }
612 }
613
865 -method _do_clone_full_end {ok} {
614 +method _do_clone2_done {ok} {
615 $o_cons done $ok
867 -
616 if {$ok} {
869 - destroy $w_body
870 -
871 - set HEAD {}
872 - if {[file exists [gitdir FETCH_HEAD]]} {
873 - set fd [safe_open_file [gitdir FETCH_HEAD] r]
874 - while {[gets $fd line] >= 0} {
875 - if {[regexp "^(.{40})\t\t" $line line HEAD]} {
876 - break
877 - }
878 - }
879 - close $fd
880 - }
881 -
882 - catch {git pack-refs}
883 - _do_clone_checkout $this $HEAD
884 - } else {
885 - _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
886 - }
887 -}
888 -
889 -method _clone_failed {{why {}}} {
890 - if {[catch {file delete -force $local_path} err]} {
891 - set why [strcat \
892 - $why \
893 - "\n\n" \
894 - [mc "Unable to cleanup %s" $local_path] \
895 - "\n\n" \
896 - $err]
897 - }
898 - if {$why ne {}} {
899 - update
900 - error_popup [strcat [mc "Clone failed."] "\n" $why]
901 - }
902 -}
903 -
904 -method _do_clone_checkout {HEAD} {
905 - if {$HEAD eq {}} {
906 - info_popup [strcat \
907 - [mc "No default branch obtained."] \
908 - "\n" \
909 - [mc "The 'master' branch has not been initialized."] \
910 - ]
911 - set done 1
912 - return
913 - }
914 - if {[catch {
915 - git update-ref HEAD $HEAD^0
617 + if {[catch {
618 + cd $local_path
619 + set ::_gitdir .git
620 + set ::_prefix {}
621 + _append_recentrepos [pwd]
622 } err]} {
917 - info_popup [strcat \
918 - [mc "Cannot resolve %s as a commit." $HEAD^0] \
919 - "\n $err" \
920 - "\n" \
921 - [mc "The 'master' branch has not been initialized."] \
922 - ]
923 - set done 1
924 - return
925 - }
926 -
927 - set status [status_bar::two_line $w_body]
928 - pack $w_body -fill x -padx 10 -pady 10
929 -
930 - # We start the status operation here.
931 - #
932 - # This function calls _readtree_wait as a callback.
933 - #
934 - # _readtree_wait in turn either calls _do_clone_submodules directly,
935 - # or calls _postcheckout_wait as a callback which then calls
936 - # _do_clone_submodules.
937 - #
938 - # _do_clone_submodules calls _do_validate_submodule_cloning.
939 - #
940 - # _do_validate_submodule_cloning stops the status operation.
941 - #
942 - # There are no other calls into this chain from other code.
943 -
944 - set o_status_op [$status start \
945 - [mc "Creating working directory"] \
946 - [mc "files"]]
947 -
948 - set readtree_err {}
949 - set fd [git_read [list read-tree \
950 - -m \
951 - -u \
952 - -v \
953 - HEAD \
954 - HEAD \
955 - ] \
956 - [list 2>@1]]
957 - fconfigure $fd -blocking 0 -translation binary
958 - fileevent $fd readable [cb _readtree_wait $fd]
959 -}
960 -
961 -method _readtree_wait {fd} {
962 - set buf [read $fd]
963 - $o_status_op update_meter $buf
964 - append readtree_err $buf
965 -
966 - fconfigure $fd -blocking 1
967 - if {![eof $fd]} {
968 - fconfigure $fd -blocking 0
969 - return
970 - }
971 -
972 - if {[catch {close $fd}]} {
973 - set err $readtree_err
974 - regsub {^fatal: } $err {} err
975 - error_popup [strcat \
976 - [mc "Initial file checkout failed."] \
977 - "\n\n$err"]
978 - return
979 - }
980 -
981 - # -- Run the post-checkout hook.
982 - #
983 - set fd_ph [githook_read post-checkout [string repeat 0 40] \
984 - [git rev-parse HEAD] 1]
985 - if {$fd_ph ne {}} {
986 - global pch_error
987 - set pch_error {}
988 - fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
989 - fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
990 - } else {
991 - _do_clone_submodules $this
992 - }
993 -}
994 -
995 -method _postcheckout_wait {fd_ph} {
996 - global pch_error
997 -
998 - append pch_error [read $fd_ph]
999 - fconfigure $fd_ph -blocking 1
1000 - if {[eof $fd_ph]} {
1001 - if {[catch {close $fd_ph}]} {
1002 - hook_failed_popup post-checkout $pch_error 0
623 + set ok 0
624 }
1004 - unset pch_error
1005 - _do_clone_submodules $this
1006 - return
625 }
1008 - fconfigure $fd_ph -blocking 0
1009 -}
1010 -
1011 -method _do_clone_submodules {} {
1012 - if {$recursive eq {true}} {
1013 - $o_status_op stop
1014 - set o_status_op {}
1015 -
1016 - destroy $w_body
1017 -
1018 - set o_cons [console::embed \
1019 - $w_body \
1020 - [mc "Cloning submodules"]]
1021 - pack $w_body -fill both -expand 1 -padx 10
1022 - $o_cons exec \
1023 - [list git submodule update --init --recursive] \
1024 - [cb _do_validate_submodule_cloning]
1025 - } else {
1026 - set done 1
626 + if {!$ok} {
627 + set ::_gitdir {}
628 + set ::_prefix {}
629 }
630 + set clone_ok $ok
631 + set done 1
632 }
633
1030 -method _do_validate_submodule_cloning {ok} {
1031 - if {$ok} {
1032 - $o_cons done $ok
1033 - set done 1
1034 - } else {
1035 - _clone_failed $this [mc "Cannot clone submodules."]
1036 - }
1037 -}
634
635 ######################################################################
636 ##