-
Notifications
You must be signed in to change notification settings - Fork 160
perf: speed up modified file lookup in large repositories #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -222,6 +222,61 @@ _forgit_list_files() { | |||||||||||||||||||||
| git ls-files -z "$@" "$rootdir" | tr '\0' '\n' | uniq | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| _forgit_rewrite_repo_paths_for_cwd() { | ||||||||||||||||||||||
| local cwd mode rootdir separator | ||||||||||||||||||||||
| rootdir=$1 | ||||||||||||||||||||||
| mode=${2:-paths} | ||||||||||||||||||||||
| separator=${3-} | ||||||||||||||||||||||
| cwd=$(pwd -P) | ||||||||||||||||||||||
|
Comment on lines
+226
to
+230
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'd put the definition and assignment of the local variables in the same order. I find it easier to read this way.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| perl -MCwd=realpath -MFile::Spec -e ' | ||||||||||||||||||||||
| use strict; | ||||||||||||||||||||||
| use warnings; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| my ($rootdir, $cwd, $mode, $separator) = @ARGV; | ||||||||||||||||||||||
| my $normalized_rootdir = realpath($rootdir); | ||||||||||||||||||||||
| my $normalized_cwd = realpath($cwd); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| while (my $line = <STDIN>) { | ||||||||||||||||||||||
| chomp $line; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| my ($status, $repo_path); | ||||||||||||||||||||||
| if ($mode eq q{status_entries}) { | ||||||||||||||||||||||
| next unless $line =~ /^(..[^[:space:]]*)( )(.*)$/; | ||||||||||||||||||||||
| ($status, $repo_path) = ($1, $3); | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| next if $line eq q{}; | ||||||||||||||||||||||
| $repo_path = $line; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| my $absolute_path = "$normalized_rootdir/$repo_path"; | ||||||||||||||||||||||
| my $display_path = File::Spec->abs2rel(realpath($absolute_path) // $absolute_path, $normalized_cwd); | ||||||||||||||||||||||
| $display_path = q{.} if $display_path eq q{}; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if ($mode eq q{status_entries}) { | ||||||||||||||||||||||
| print "[$status] ${display_path}${separator}${normalized_rootdir}/${repo_path}\n"; | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| print "$display_path\n"; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ' "$rootdir" "$cwd" "$mode" "$separator" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change was largely just pulled from |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| _forgit_list_modified_files() { | ||||||||||||||||||||||
| # on large repos with many untracked files, git ls-files | ||||||||||||||||||||||
| # is substantially slower than git diff | ||||||||||||||||||||||
| # Wherever possible (when we don't care about untracked files) | ||||||||||||||||||||||
| # use this command over _forgit_list_files | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # See note on _forgit_list_files() about -z and other options | ||||||||||||||||||||||
| local rootdir | ||||||||||||||||||||||
| rootdir=$(git rev-parse --show-toplevel) | ||||||||||||||||||||||
| git -C "$rootdir" diff --name-only -z | | ||||||||||||||||||||||
| tr '\0' '\n' | | ||||||||||||||||||||||
| uniq | | ||||||||||||||||||||||
| _forgit_rewrite_repo_paths_for_cwd "$rootdir" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| _forgit_list_staged_files() { | ||||||||||||||||||||||
| local up | ||||||||||||||||||||||
| up="$(git rev-parse --show-cdup)" | ||||||||||||||||||||||
|
|
@@ -291,32 +346,12 @@ _forgit_restore_untracked_color() { | |||||||||||||||||||||
| # 1. the human-readable status line shown in fzf | ||||||||||||||||||||||
| # 2. the absolute-path payload used by preview/edit/add actions | ||||||||||||||||||||||
| _forgit_build_status_entries() { | ||||||||||||||||||||||
| local cwd rootdir | ||||||||||||||||||||||
| local rootdir | ||||||||||||||||||||||
| rootdir=$1 | ||||||||||||||||||||||
| cwd=$(pwd -P) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Use a single Perl process so path normalization stays portable while the | ||||||||||||||||||||||
| # full add-list transformation still runs as one batch pipeline stage. | ||||||||||||||||||||||
| perl -MCwd=realpath -MFile::Spec -e ' | ||||||||||||||||||||||
| use strict; | ||||||||||||||||||||||
| use warnings; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| my ($rootdir, $cwd, $separator) = @ARGV; | ||||||||||||||||||||||
| my $normalized_rootdir = realpath($rootdir); | ||||||||||||||||||||||
| my $normalized_cwd = realpath($cwd); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| while (my $line = <STDIN>) { | ||||||||||||||||||||||
| chomp $line; | ||||||||||||||||||||||
| next unless $line =~ /^(..[^[:space:]]*)( )(.*)$/; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| my ($status, $repo_path) = ($1, $3); | ||||||||||||||||||||||
| my $absolute_path = "$normalized_rootdir/$repo_path"; | ||||||||||||||||||||||
| my $display_path = File::Spec->abs2rel(realpath($absolute_path) // $absolute_path, $normalized_cwd); | ||||||||||||||||||||||
| $display_path = "." if $display_path eq q{}; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| print "[$status] ${display_path}${separator}${normalized_rootdir}/${repo_path}\n"; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ' "$rootdir" "$cwd" "$_ffsep" | ||||||||||||||||||||||
| _forgit_rewrite_repo_paths_for_cwd "$rootdir" status_entries "$_ffsep" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| _forgit_is_submodule() { | ||||||||||||||||||||||
|
|
@@ -739,7 +774,7 @@ _forgit_restore() { | |||||||||||||||||||||
| if [[ $worktree == true || $staged != true ]]; then | ||||||||||||||||||||||
| while IFS='' read -r file; do | ||||||||||||||||||||||
| candidates+=("$file") | ||||||||||||||||||||||
| done < <(_forgit_list_files --modified) | ||||||||||||||||||||||
| done < <(_forgit_list_modified_files) | ||||||||||||||||||||||
| fi | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [[ ${#candidates[@]} -eq 0 ]] && echo "Nothing to restore." && return 1 | ||||||||||||||||||||||
|
|
@@ -1093,7 +1128,7 @@ _forgit_checkout_file() { | |||||||||||||||||||||
| _forgit_git_checkout_file "$@" | ||||||||||||||||||||||
| return $? | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| [[ $(_forgit_list_files --modified | wc -l) -eq 0 ]] && echo 'Nothing to checkout.' && return 1 | ||||||||||||||||||||||
| [[ $(_forgit_list_modified_files | wc -l) -eq 0 ]] && echo 'Nothing to checkout.' && return 1 | ||||||||||||||||||||||
| opts=" | ||||||||||||||||||||||
| $FORGIT_FZF_DEFAULT_OPTS | ||||||||||||||||||||||
| -m -0 | ||||||||||||||||||||||
|
|
@@ -1103,7 +1138,7 @@ _forgit_checkout_file() { | |||||||||||||||||||||
| files=() | ||||||||||||||||||||||
| while IFS='' read -r file; do | ||||||||||||||||||||||
| files+=("$file") | ||||||||||||||||||||||
| done < <(_forgit_list_files --modified | | ||||||||||||||||||||||
| done < <(_forgit_list_modified_files | | ||||||||||||||||||||||
| FZF_DEFAULT_OPTS="$opts" fzf) | ||||||||||||||||||||||
| [[ ${#files[@]} -gt 0 ]] && _forgit_git_checkout_file "$@" "${files[@]}" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the more common version to do this because it also expands to an empty string if the variable is declared but is null. At least that's what I found when looking it up here.