crendential-store: use timeout when locking file

When holding the lock for rewriting the credential file, use a timeout to avoid race conditions when the credentials file needs to be updated in parallel. An example would be doing `fetch --all` on a repository with several remotes that need credentials, using parallel fetching. The timeout can be configured using "credentialStore.lockTimeoutMS", defaulting to 1 second. Signed-off-by: Simão Afonso <simao.afonso@powertools-tech.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Simão Afonso committed Nov 25, 2020 at 18:31 UTC df7f915fb6fb28ea5a1e3103251c251e53a80b1b
2 files changed +12 -2
Documentation/config/credential.txt
+6
@@ -28,3 +28,9 @@ credential.<url>.*::
28
29 credentialCache.ignoreSIGHUP::
30 Tell git-credential-cache--daemon to ignore SIGHUP, instead of quitting.
31 +
32 +credentialStore.lockTimeoutMS::
33 + The length of time, in milliseconds, for git-credential-store to retry
34 + when trying to lock the credentials file. Value 0 means not to retry at
35 + all; -1 means to try indefinitely. Default is 1000 (i.e., retry for
36 + 1s).
builtin/credential-store.c
+6 -2
@@ -1,4 +1,5 @@
1 #include "builtin.h"
2 +#include "config.h"
3 #include "lockfile.h"
4 #include "credential.h"
5 #include "string-list.h"
@@ -58,8 +59,11 @@ static void print_line(struct strbuf *buf)
59 static void rewrite_credential_file(const char *fn, struct credential *c,
60 struct strbuf *extra)
61 {
61 - if (hold_lock_file_for_update(&credential_lock, fn, 0) < 0)
62 - die_errno("unable to get credential storage lock");
62 + int timeout_ms = 1000;
63 +
64 + git_config_get_int("credentialstore.locktimeoutms", &timeout_ms);
65 + if (hold_lock_file_for_update_timeout(&credential_lock, fn, 0, timeout_ms) < 0)
66 + die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms);
67 if (extra)
68 print_line(extra);
69 parse_credential_file(fn, c, NULL, print_line);