git: retry if http request failed

2020-08-25 05:53发布

问题:

In our Continuous Integration environment we do very heavy usage of git clone and git update.

Sometimes the network or the central git server is not reliable.

Is there a way to tell git to retry if the http request failed?

Example:

500 Internal Server Error while accessing https://example.com/repos/foo_bar/info/refs?service=git-upload-pack

回答1:

Is there a way to tell git to retry if the http request failed?

No git itself, it does not support natively that feature.

But interestingly enough, the idea of wrapping a git command in order to retry its execution has been done before: see "git-retry(1) Manual Page ", part of depot_tools, a collection of tools for dealing with Chromium development.

The shell wrapper git-retry calls the python script git_retry.py with the following options:

'-v', '--verbose', default=0,

Increase verbosity; can be specified multiple times

'-c', '--retry-count', default=GitRetry.DEFAULT_RETRY_COUNT (5),

Number of times to retry (default=5)

'-d', '--delay', default=GitRetry.DEFAULT_DELAY_SECS (3.0)

Specifies the amount of time (in seconds) to wait between successive retries (default=3 sec.). This can be zero.

'-D', '--delay-factor', default=2

The exponential factor to apply to delays in between successive failures (default=%default). If this is zero, delays will increase linearly. Set this to one to have a constant (non-increasing) delay.


Note: a git clone for a repo with submodule will always try to clone a submodule twice (one retry). See "Is there any way to continue Git clone from the point where it failed?".



回答2:

You could run a script like this instead of calling git directly.

#!/bin/bash

REALGIT=/usr/bin/git

RETRIES=3
DELAY=10
COUNT=1
while [ $COUNT -lt $RETRIES ]; do
  $REALGIT $*
  if [ $? -eq 0 ]; then
    RETRIES=0
    break
  fi
  let COUNT=$COUNT+1
  sleep $DELAY
done


回答3:

This is a wrapper around git commands, it recognizes when a command fails and retries

git retry [-v] [-c COUNT] [-d DELAY] [-e] — <git_subcommand>

More information on this can be found here.



回答4:

Run below command

while ! git push; do sleep 7; done



标签: git http