您可以指定 git 获取的超时时间吗?

Can you specify a timeout to git fetch?

我在 python 中编写了一些 git 命令,如果没有连接,有时 git fetch 会挂起数小时。有没有办法让它超时并报告失败?

不可以,您需要使用包装器使命令超时。

我需要 change/set 超时:

$ time git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out

real    2m11.684s
user    0m0.002s
sys 0m0.005s

(2 分 11 秒可能取决于我的 sysctl 设置)

因为这是 Whosebug,我不得不按如下方式修补 git(而不是使用 timeout command/wrapper)以获得指定超时的这两种变体: ~/.gitconfig 指定超时(以秒为单位):

[http]
  connecttimeout = 10

所以,

$ time git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Connection timed out after 10009 milliseconds

real    0m10.031s
user    0m0.000s
sys 0m0.004s

以及一种通过 env 覆盖 ~/.gitconfig 指定超时的方法。变量:

$ time GIT_HTTP_CONNECT_TIMEOUT=2 git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Connection timed out after 2001 milliseconds

real    0m2.023s
user    0m0.000s
sys 0m0.004s

有问题的补丁(应用在最新的 git master commit 4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a 日期:Wed Sep 18 11:55:13 2019 -0700)是:

diff --git a/http.c b/http.c
index 938b9e55af..c46737e48d 100644
--- a/http.c
+++ b/http.c
@@ -83,6 +83,7 @@ static const char *ssl_pinnedkey;
 static const char *ssl_cainfo;
 static long curl_low_speed_limit = -1;
 static long curl_low_speed_time = -1;
+static long curl_connecttimeout = -1; // in seconds, see man 3 CURLOPT_CONNECTTIMEOUT, default(for me, depending on sysctl setting probably!) is 2min10sec
 static int curl_ftp_no_epsv;
 static const char *curl_http_proxy;
 static const char *http_proxy_authmethod;
@@ -354,6 +355,10 @@ static int http_options(const char *var, const char *value, void *cb)
        curl_low_speed_time = (long)git_config_int(var, value);
        return 0;
    }
+   if (!strcmp("http.connecttimeout", var)) { // overriden by env var GIT_HTTP_CONNECT_TIMEOUT
+       curl_connecttimeout = (long)git_config_int(var, value);
+       return 0;
+   }

    if (!strcmp("http.noepsv", var)) {
        curl_ftp_no_epsv = git_config_bool(var, value);
@@ -935,6 +940,11 @@ static CURL *get_curl_handle(void)
        curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
                 curl_low_speed_time);
    }
+  if (curl_connecttimeout >= 0) {
+    //-1 or any negative means don't set a timeout which means (for me, depending on sysctl settings, no doubt) 2min10sec eg. 130sec and quits like: fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
+    //0 means set timeout to 0 which practically doesn't set a timeout and is the same as using a negative value! aka 2min10sec and quits like for -1 (see above)
+    curl_easy_setopt(result, CURLOPT_CONNECTTIMEOUT, curl_connecttimeout); //10L = timeout in 10 seconds instead of 2min10s eg. `git fetch` when: fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out   --- real  2m10.809s  --- because git.xfce.org is down/not responding to ping! see $ man 3 CURLOPT_CONNECTTIMEOUT Setting a timeout quits like: fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
+  }

    curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
 #if LIBCURL_VERSION_NUM >= 0x071301
@@ -1061,6 +1071,7 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
 {
    char *low_speed_limit;
    char *low_speed_time;
+   char *connecttimeout;
    char *normalized_url;
    struct urlmatch_config config = { STRING_LIST_INIT_DUP };

@@ -1151,6 +1162,9 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
    low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
    if (low_speed_time != NULL)
        curl_low_speed_time = strtol(low_speed_time, NULL, 10);
+  connecttimeout = getenv("GIT_HTTP_CONNECT_TIMEOUT"); // man 3 CURLOPT_CONNECTTIMEOUT ; can also be set in ~/.gitconfig as http.connecttimeout aka [http]\n\tconnecttimeout=10
+  if (connecttimeout != NULL)
+    curl_connecttimeout = strtol(connecttimeout, NULL, 10); //10 is base

    if (curl_ssl_verify == -1)
        curl_ssl_verify = 1;
@@ -1903,6 +1917,7 @@ static int http_request(const char *url,
    curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
    curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);

+
    ret = run_one_slot(slot, &results);

    if (options && options->content_type) {

额外内容:
覆盖 ~/.gitconfig-set 超时并将超时设置为 0(这意味着没有超时,因为当 CURLOPT_CONNECTTIMEOUT 设置为 0 时 libcurl 的行为方式)或者根本不设置任何超时(这当前意味着相同:没有超时,因为 git 没有 touch/set CURLOPT_CONNECTTIMEOUT),这些值中的任何一个都可以工作:0 或任何负值,即

$ time GIT_HTTP_CONNECT_TIMEOUT=0 git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out

real    2m11.255s
user    0m0.000s
sys 0m0.004s

$ time GIT_HTTP_CONNECT_TIMEOUT=-1 git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out

real    2m8.710s
user    0m0.000s
sys 0m0.006s

在 archlinux 上测试:
local/curl7.66.0-1
local/git2.23.0.r256.g4c86140027-1