@matheusbsbs / matheus-bagliano / commits / e3c6f240fd
Fork 0

git-fetch: use fetch--tool pick-rref to avoid local fetch from alternate

When we are fetching from a repository that is on a local filesystem, first check if we have all the objects that we are going to fetch available locally, by not just checking the tips of what we are fetching, but with a full reachability analysis to our existing refs. In such a case, we do not have to run git-fetch-pack which would send many needless objects. This is especially true when the other repository is an alternate of the current repository (e.g. perhaps the repository was created by running "git clone -l -s" from there). The useless objects transferred used to be discarded when they were expanded by git-unpack-objects called from git-fetch-pack, but recent git-fetch-pack prefers to keep the data it receives from the other end without exploding them into loose objects, resulting in a pack full of duplicated data when fetching from your own alternate. This also uses fetch--tool pick-rref on dumb transport side to remove a shell loop to do the same. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 5, 2007 at 03:22 UTC e3c6f240fd9c5bdeb33f2d47adc859f37935e2df
1 file changed +29 -13
git-fetch.sh
+29 -13
@@ -177,9 +177,33 @@ fetch_all_at_once () {
177 git-bundle unbundle "$remote" $rref ||
178 echo failed "$remote"
179 else
180 - git-fetch-pack --thin $exec $keep $shallow_depth \
181 - $quiet $no_progress "$remote" $rref ||
182 - echo failed "$remote"
180 + if test -d "$remote" &&
181 +
182 + # The remote might be our alternate. With
183 + # this optimization we will bypass fetch-pack
184 + # altogether, which means we cannot be doing
185 + # the shallow stuff at all.
186 + test ! -f "$GIT_DIR/shallow" &&
187 + test -z "$shallow_depth" &&
188 +
189 + # See if all of what we are going to fetch are
190 + # connected to our repository's tips, in which
191 + # case we do not have to do any fetch.
192 + theirs=$(git-fetch--tool -s pick-rref \
193 + "$rref" "$ls_remote_result") &&
194 +
195 + # This will barf when $theirs reach an object that
196 + # we do not have in our repository. Otherwise,
197 + # we already have everything the fetch would bring in.
198 + git-rev-list --objects $theirs --not --all \
199 + >/dev/null 2>/dev/null
200 + then
201 + git-fetch--tool pick-rref "$rref" "$ls_remote_result"
202 + else
203 + git-fetch-pack --thin $exec $keep $shallow_depth \
204 + $quiet $no_progress "$remote" $rref ||
205 + echo failed "$remote"
206 + fi
207 fi
208 ) |
209 (
@@ -239,16 +263,8 @@ fetch_per_ref () {
263 fi
264
265 # Find $remote_name from ls-remote output.
242 - head=$(
243 - IFS=' '
244 - echo "$ls_remote_result" |
245 - while read sha1 name
246 - do
247 - test "z$name" = "z$remote_name" || continue
248 - echo "$sha1"
249 - break
250 - done
251 - )
266 + head=$(git-fetch--tool -s pick-rref \
267 + "$remote_name" "$ls_remote_result")
268 expr "z$head" : "z$_x40\$" >/dev/null ||
269 die "No such ref $remote_name at $remote"
270 echo >&2 "Fetching $remote_name from $remote using $proto"