From b8591e07fde134fc07daa75fb545c77448f0ae29 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:09:08 +0100 Subject: [PATCH 001/406] fixup! do_git_config_sequence(): fall back to git_dir if commondir is NULL In preparation for applying the latest iteration of bw/config-h that was submitted to the Git mailing list, let's revert the original iteration. Signed-off-by: Johannes Schindelin --- config.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/config.c b/config.c index eb67c9b9cc8cc9..3461993f0af665 100644 --- a/config.c +++ b/config.c @@ -1676,8 +1676,6 @@ static int do_git_config_sequence(const struct config_options *opts, if (opts->commondir) repo_config = mkpathdup("%s/config", opts->commondir); - else if (opts->git_dir) - repo_config = mkpathdup("%s/config", opts->git_dir); else repo_config = NULL; From 0154b9c065801f9be515d916fa51e6a85f200d77 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 13 Jul 2017 22:55:36 +0200 Subject: [PATCH 002/406] squash! do_git_config_sequence(): fall back to git_dir if commondir is NULL config: report a bug if git_dir exists without commondir This did happen at some stage, and was fixed relatively quickly. Make sure that we detect very quickly, too, should that happen again. Signed-off-by: Johannes Schindelin --- config.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.c b/config.c index 3461993f0af665..efe67560b39873 100644 --- a/config.c +++ b/config.c @@ -1676,6 +1676,8 @@ static int do_git_config_sequence(const struct config_options *opts, if (opts->commondir) repo_config = mkpathdup("%s/config", opts->commondir); + else if (opts->git_dir) + BUG("git_dir without commondir"); else repo_config = NULL; From 17719e683f919b4d07a84aa68b7768b78bae0fe3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:15:14 +0100 Subject: [PATCH 003/406] fixup! http: when using Secure Channel, ignore sslCAInfo by default Revert this in preparation for applying the latest iteration of the patch. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 8 -------- http.c | 19 +------------------ 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 56a110446915ea..e107f4c1e24370 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2249,14 +2249,6 @@ http.schannelCheckRevoke:: certificate. This option is ignored if cURL lacks support for setting the relevant SSL option at runtime. -http.schannelUseSSLCAInfo:: - As of cURL v7.60.0, the Secure Channel backend can use the - certificate bundle provided via `http.sslCAInfo`, but that would - override the Windows Certificate Store. Since this is not desirable - by default, Git will tell cURL not to use that bundle by default - when the `schannel` backend was configured via `http.sslBackend`, - unless `http.schannelUseSSLCAInfo` overrides this behavior. - http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index 0ebf8f77a6d20b..2ce2cd447bf779 100644 --- a/http.c +++ b/http.c @@ -158,12 +158,6 @@ static char *cached_accept_language; static char *http_ssl_backend; static int http_schannel_check_revoke = 1; -/* - * With the backend being set to `schannel`, setting sslCAinfo would override - * the Certificate Store in cURL v7.60.0 and later, which is not what we want - * by default. - */ -static int http_schannel_use_ssl_cainfo; size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { @@ -323,11 +317,6 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } - if (!strcmp("http.schannelusesslcainfo", var)) { - http_schannel_use_ssl_cainfo = git_config_bool(var, value); - return 0; - } - if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -880,13 +869,7 @@ static CURL *get_curl_handle(void) if (ssl_pinnedkey != NULL) curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey); #endif - if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) && - !http_schannel_use_ssl_cainfo) { - curl_easy_setopt(result, CURLOPT_CAINFO, NULL); -#if LIBCURL_VERSION_NUM >= 0x073400 - curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL); -#endif - } else if (ssl_cainfo != NULL) + if (ssl_cainfo != NULL) curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { From a36e5dca52a3780044668da3832e618ddf590496 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:15:41 +0100 Subject: [PATCH 004/406] fixup! http: add support for disabling SSL revocation checks in cURL Revert this in preparation for applying the latest iteration of the patch. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 8 -------- http.c | 17 ----------------- 2 files changed, 25 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index e107f4c1e24370..c569e728593027 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2241,14 +2241,6 @@ http.sslBackend:: This option is ignored if cURL lacks support for choosing the SSL backend at runtime. -http.schannelCheckRevoke:: - Used to enforce or disable certificate revocation checks in cURL - when http.sslBackend is set to "schannel". Defaults to `true` if - unset. Only necessary to disable this if Git consistently errors - and the message is about checking the revocation status of a - certificate. This option is ignored if cURL lacks support for - setting the relevant SSL option at runtime. - http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index 2ce2cd447bf779..fedfb2a2070020 100644 --- a/http.c +++ b/http.c @@ -157,8 +157,6 @@ static char *cached_accept_language; static char *http_ssl_backend; -static int http_schannel_check_revoke = 1; - size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { size_t size = eltsize * nmemb; @@ -312,11 +310,6 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } - if (!strcmp("http.schannelcheckrevoke", var)) { - http_schannel_check_revoke = git_config_bool(var, value); - return 0; - } - if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -818,16 +811,6 @@ static CURL *get_curl_handle(void) } #endif - if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) && - !http_schannel_check_revoke) { -#if LIBCURL_VERSION_NUM >= 0x072c00 - curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE); -#else - warning("CURLSSLOPT_NO_REVOKE not applied to curl SSL options because\n" - "your curl version is too old (>= 7.44.0)"); -#endif - } - if (http_proactive_auth) init_curl_http_auth(result); From 69eadfb8354a1fa1bff405794fe54a0a001a6bf6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:15:51 +0100 Subject: [PATCH 005/406] fixup! http: add support for selecting SSL backends at runtime Revert this in preparation for applying the latest iteration of the patch. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 5 ----- http.c | 35 ----------------------------------- 2 files changed, 40 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index c569e728593027..eb66a119753726 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2236,11 +2236,6 @@ http.sslCAPath:: with when fetching or pushing over HTTPS. Can be overridden by the `GIT_SSL_CAPATH` environment variable. -http.sslBackend:: - Name of the SSL backend to use (e.g. "openssl" or "schannel"). - This option is ignored if cURL lacks support for choosing the SSL - backend at runtime. - http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index fedfb2a2070020..4162860ee31636 100644 --- a/http.c +++ b/http.c @@ -155,8 +155,6 @@ static struct active_request_slot *active_queue_head; static char *cached_accept_language; -static char *http_ssl_backend; - size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { size_t size = eltsize * nmemb; @@ -304,12 +302,6 @@ static int http_options(const char *var, const char *value, void *cb) curl_ssl_try = git_config_bool(var, value); return 0; } - if (!strcmp("http.sslbackend", var)) { - free(http_ssl_backend); - http_ssl_backend = xstrdup_or_null(value); - return 0; - } - if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -1003,33 +995,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth) git_config(urlmatch_config_entry, &config); free(normalized_url); -#if LIBCURL_VERSION_NUM >= 0x073800 - if (http_ssl_backend) { - const curl_ssl_backend **backends; - struct strbuf buf = STRBUF_INIT; - int i; - - switch (curl_global_sslset(-1, http_ssl_backend, &backends)) { - case CURLSSLSET_UNKNOWN_BACKEND: - strbuf_addf(&buf, _("Unsupported SSL backend '%s'. " - "Supported SSL backends:"), - http_ssl_backend); - for (i = 0; backends[i]; i++) - strbuf_addf(&buf, "\n\t%s", backends[i]->name); - die("%s", buf.buf); - case CURLSSLSET_NO_BACKENDS: - die(_("Could not set SSL backend to '%s': " - "cURL was built without SSL backends"), - http_ssl_backend); - case CURLSSLSET_TOO_LATE: - die(_("Could not set SSL backend to '%s': already set"), - http_ssl_backend); - case CURLSSLSET_OK: - break; /* Okay! */ - } - } -#endif - if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) die("curl_global_init failed"); From 18dab43c05fd4aa7d5adb758aa7c36afd88a5297 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 2 Aug 2017 22:10:06 +0200 Subject: [PATCH 006/406] squash! http: add support for selecting SSL backends at runtime http: add support for selecting SSL backends at runtime As of version 7.56.0, curl supports being compiled with multiple SSL backends. This patch adds the Git side of that feature: by setting http.sslBackend to "openssl" or "schannel", Git for Windows can now choose the SSL backend at runtime. This comes in handy on Windows because Secure Channel ("schannel") is the native solution, accessing the Windows Credential Store, thereby allowing for enterprise-wide management of certificates. For historical reasons, Git for Windows needs to support OpenSSL still, as it has previously been the only supported SSL backend in Git for Windows for almost a decade. The patch has been carried in Git for Windows for over a year, and is considered mature. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 5 +++++ http.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Documentation/config.txt b/Documentation/config.txt index eb66a119753726..c569e728593027 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2236,6 +2236,11 @@ http.sslCAPath:: with when fetching or pushing over HTTPS. Can be overridden by the `GIT_SSL_CAPATH` environment variable. +http.sslBackend:: + Name of the SSL backend to use (e.g. "openssl" or "schannel"). + This option is ignored if cURL lacks support for choosing the SSL + backend at runtime. + http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index 4162860ee31636..fedfb2a2070020 100644 --- a/http.c +++ b/http.c @@ -155,6 +155,8 @@ static struct active_request_slot *active_queue_head; static char *cached_accept_language; +static char *http_ssl_backend; + size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { size_t size = eltsize * nmemb; @@ -302,6 +304,12 @@ static int http_options(const char *var, const char *value, void *cb) curl_ssl_try = git_config_bool(var, value); return 0; } + if (!strcmp("http.sslbackend", var)) { + free(http_ssl_backend); + http_ssl_backend = xstrdup_or_null(value); + return 0; + } + if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -995,6 +1003,33 @@ void http_init(struct remote *remote, const char *url, int proactive_auth) git_config(urlmatch_config_entry, &config); free(normalized_url); +#if LIBCURL_VERSION_NUM >= 0x073800 + if (http_ssl_backend) { + const curl_ssl_backend **backends; + struct strbuf buf = STRBUF_INIT; + int i; + + switch (curl_global_sslset(-1, http_ssl_backend, &backends)) { + case CURLSSLSET_UNKNOWN_BACKEND: + strbuf_addf(&buf, _("Unsupported SSL backend '%s'. " + "Supported SSL backends:"), + http_ssl_backend); + for (i = 0; backends[i]; i++) + strbuf_addf(&buf, "\n\t%s", backends[i]->name); + die("%s", buf.buf); + case CURLSSLSET_NO_BACKENDS: + die(_("Could not set SSL backend to '%s': " + "cURL was built without SSL backends"), + http_ssl_backend); + case CURLSSLSET_TOO_LATE: + die(_("Could not set SSL backend to '%s': already set"), + http_ssl_backend); + case CURLSSLSET_OK: + break; /* Okay! */ + } + } +#endif + if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) die("curl_global_init failed"); From a67fd95dafbc94b320c64244967950daf452ac04 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Tue, 23 Jan 2018 13:20:55 +1100 Subject: [PATCH 007/406] squash! http: add support for disabling SSL revocation checks in cURL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit http: add support for disabling SSL revocation checks in cURL This adds support for a new http.schannelCheckRevoke config value. This config value is only used if http.sslBackend is set to "schannel", which forces cURL to use the Windows Certificate Store when validating server certificates associated with a remote server. This config value should only be set to "false" if you are in an environment where revocation checks are blocked by the network, with no alternative options. This is only supported in cURL 7.44 or later. Note: originally, we wanted to call the config setting `http.schannel.checkRevoke`. This, however, does not work: the `http.*` config settings can be limited to specific URLs via `http..*` (and this feature would mistake `schannel` for a URL). Helped by Agustín Martín Barbero. Signed-off-by: Brendan Forster Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 8 ++++++++ http.c | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Documentation/config.txt b/Documentation/config.txt index c569e728593027..e107f4c1e24370 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2241,6 +2241,14 @@ http.sslBackend:: This option is ignored if cURL lacks support for choosing the SSL backend at runtime. +http.schannelCheckRevoke:: + Used to enforce or disable certificate revocation checks in cURL + when http.sslBackend is set to "schannel". Defaults to `true` if + unset. Only necessary to disable this if Git consistently errors + and the message is about checking the revocation status of a + certificate. This option is ignored if cURL lacks support for + setting the relevant SSL option at runtime. + http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index fedfb2a2070020..272584b16e5f7d 100644 --- a/http.c +++ b/http.c @@ -157,6 +157,8 @@ static char *cached_accept_language; static char *http_ssl_backend; +static int http_schannel_check_revoke = 1; + size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { size_t size = eltsize * nmemb; @@ -310,6 +312,11 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } + if (!strcmp("http.schannelcheckrevoke", var)) { + http_schannel_check_revoke = git_config_bool(var, value); + return 0; + } + if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -811,6 +818,16 @@ static CURL *get_curl_handle(void) } #endif + if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) && + !http_schannel_check_revoke) { +#if LIBCURL_VERSION_NUM >= 0x072c00 + curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE); +#else + warning("CURLSSLOPT_NO_REVOKE not applied to curl SSL options because\n" + "your curl version is too old (< 7.44.0)"); +#endif + } + if (http_proactive_auth) init_curl_http_auth(result); From 850406a924d2f8c4cadb661d746f16d6bbb71e17 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 14 Jun 2018 14:01:58 +0200 Subject: [PATCH 008/406] squash! http: when using Secure Channel, ignore sslCAInfo by default http: when using Secure Channel, ignore sslCAInfo by default As of cURL v7.60.0, the Secure Channel backend can use the certificate bundle provided via `http.sslCAInfo`, but that would override the Windows Certificate Store. Since this is not desirable by default, let's tell Git to not ask cURL to use that bundle by default when the `schannel` backend was configured via `http.sslBackend`, unless `http.schannelUseSSLCAInfo` overrides this behavior. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 8 ++++++++ http.c | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index e107f4c1e24370..56a110446915ea 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2249,6 +2249,14 @@ http.schannelCheckRevoke:: certificate. This option is ignored if cURL lacks support for setting the relevant SSL option at runtime. +http.schannelUseSSLCAInfo:: + As of cURL v7.60.0, the Secure Channel backend can use the + certificate bundle provided via `http.sslCAInfo`, but that would + override the Windows Certificate Store. Since this is not desirable + by default, Git will tell cURL not to use that bundle by default + when the `schannel` backend was configured via `http.sslBackend`, + unless `http.schannelUseSSLCAInfo` overrides this behavior. + http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index 272584b16e5f7d..43e75ac583b428 100644 --- a/http.c +++ b/http.c @@ -158,6 +158,12 @@ static char *cached_accept_language; static char *http_ssl_backend; static int http_schannel_check_revoke = 1; +/* + * With the backend being set to `schannel`, setting sslCAinfo would override + * the Certificate Store in cURL v7.60.0 and later, which is not what we want + * by default. + */ +static int http_schannel_use_ssl_cainfo; size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { @@ -317,6 +323,11 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } + if (!strcmp("http.schannelusesslcainfo", var)) { + http_schannel_use_ssl_cainfo = git_config_bool(var, value); + return 0; + } + if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -869,7 +880,13 @@ static CURL *get_curl_handle(void) if (ssl_pinnedkey != NULL) curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey); #endif - if (ssl_cainfo != NULL) + if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) && + !http_schannel_use_ssl_cainfo) { + curl_easy_setopt(result, CURLOPT_CAINFO, NULL); +#if LIBCURL_VERSION_NUM >= 0x073400 + curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL); +#endif + } else if (ssl_cainfo != NULL) curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { From 07100ba0e0a6042dcd8a40e7ee3cc60eee72d1ff Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:26:31 +0100 Subject: [PATCH 009/406] fixup! Remove old code and macro-ize implementation In preparation for applying the patch that actually made it to the Git mailing list, let's revert this. Signed-off-by: Johannes Schindelin --- compat/win32/pthread.c | 184 +++++++++++++++++++++++++++++++++++++++++ compat/win32/pthread.h | 43 ++++++---- 2 files changed, 209 insertions(+), 18 deletions(-) diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c index 2e7eead42cb008..27e6092781d854 100644 --- a/compat/win32/pthread.c +++ b/compat/win32/pthread.c @@ -56,3 +56,187 @@ pthread_t pthread_self(void) t.tid = GetCurrentThreadId(); return t; } + +#ifdef GIT_WIN_XP_SUPPORT + +int pthread_cond_init(pthread_cond_t *cond, const void *unused) +{ + cond->waiters = 0; + cond->was_broadcast = 0; + InitializeCriticalSection(&cond->waiters_lock); + + cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); + if (!cond->sema) + die("CreateSemaphore() failed"); + + cond->continue_broadcast = CreateEvent(NULL, /* security */ + FALSE, /* auto-reset */ + FALSE, /* not signaled */ + NULL); /* name */ + if (!cond->continue_broadcast) + die("CreateEvent() failed"); + + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) +{ + CloseHandle(cond->sema); + CloseHandle(cond->continue_broadcast); + DeleteCriticalSection(&cond->waiters_lock); + return 0; +} + +int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) +{ + int last_waiter; + + EnterCriticalSection(&cond->waiters_lock); + cond->waiters++; + LeaveCriticalSection(&cond->waiters_lock); + + /* + * Unlock external mutex and wait for signal. + * NOTE: we've held mutex locked long enough to increment + * waiters count above, so there's no problem with + * leaving mutex unlocked before we wait on semaphore. + */ + LeaveCriticalSection(mutex); + + /* let's wait - ignore return value */ + WaitForSingleObject(cond->sema, INFINITE); + + /* + * Decrease waiters count. If we are the last waiter, then we must + * notify the broadcasting thread that it can continue. + * But if we continued due to cond_signal, we do not have to do that + * because the signaling thread knows that only one waiter continued. + */ + EnterCriticalSection(&cond->waiters_lock); + cond->waiters--; + last_waiter = cond->was_broadcast && cond->waiters == 0; + LeaveCriticalSection(&cond->waiters_lock); + + if (last_waiter) { + /* + * cond_broadcast was issued while mutex was held. This means + * that all other waiters have continued, but are contending + * for the mutex at the end of this function because the + * broadcasting thread did not leave cond_broadcast, yet. + * (This is so that it can be sure that each waiter has + * consumed exactly one slice of the semaphor.) + * The last waiter must tell the broadcasting thread that it + * can go on. + */ + SetEvent(cond->continue_broadcast); + /* + * Now we go on to contend with all other waiters for + * the mutex. Auf in den Kampf! + */ + } + /* lock external mutex again */ + EnterCriticalSection(mutex); + + return 0; +} + +/* + * IMPORTANT: This implementation requires that pthread_cond_signal + * is called while the mutex is held that is used in the corresponding + * pthread_cond_wait calls! + */ +int pthread_cond_signal(pthread_cond_t *cond) +{ + int have_waiters; + + EnterCriticalSection(&cond->waiters_lock); + have_waiters = cond->waiters > 0; + LeaveCriticalSection(&cond->waiters_lock); + + /* + * Signal only when there are waiters + */ + if (have_waiters) + return ReleaseSemaphore(cond->sema, 1, NULL) ? + 0 : err_win_to_posix(GetLastError()); + else + return 0; +} + +/* + * DOUBLY IMPORTANT: This implementation requires that pthread_cond_broadcast + * is called while the mutex is held that is used in the corresponding + * pthread_cond_wait calls! + */ +int pthread_cond_broadcast(pthread_cond_t *cond) +{ + EnterCriticalSection(&cond->waiters_lock); + + if ((cond->was_broadcast = cond->waiters > 0)) { + /* wake up all waiters */ + ReleaseSemaphore(cond->sema, cond->waiters, NULL); + LeaveCriticalSection(&cond->waiters_lock); + /* + * At this point all waiters continue. Each one takes its + * slice of the semaphor. Now it's our turn to wait: Since + * the external mutex is held, no thread can leave cond_wait, + * yet. For this reason, we can be sure that no thread gets + * a chance to eat *more* than one slice. OTOH, it means + * that the last waiter must send us a wake-up. + */ + WaitForSingleObject(cond->continue_broadcast, INFINITE); + /* + * Since the external mutex is held, no thread can enter + * cond_wait, and, hence, it is safe to reset this flag + * without cond->waiters_lock held. + */ + cond->was_broadcast = 0; + } else { + LeaveCriticalSection(&cond->waiters_lock); + } + return 0; +} + +#else // GIT_WIN_XP_SUPPORT + +WINBASEAPI VOID WINAPI +InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI VOID WINAPI +WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI VOID WINAPI +WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI WINBOOL WINAPI +SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, + PCRITICAL_SECTION CriticalSection, + DWORD dwMilliseconds); + +int pthread_cond_init(pthread_cond_t *cond, const void *unused) +{ + InitializeConditionVariable(cond); + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) +{ + return 0; +} + +int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) +{ + SleepConditionVariableCS(cond, mutex, INFINITE); + return 0; +} + +int pthread_cond_signal(pthread_cond_t *cond) +{ + WakeConditionVariable(cond); + return 0; +} + +int pthread_cond_broadcast(pthread_cond_t *cond) +{ + WakeAllConditionVariable(cond); + return 0; +} + +#endif // GIT_WIN_XP_SUPPORT diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h index 11383eb5f4d823..10a9d45f07eadd 100644 --- a/compat/win32/pthread.h +++ b/compat/win32/pthread.h @@ -32,24 +32,31 @@ typedef int pthread_mutexattr_t; #define pthread_mutexattr_settype(a, t) 0 #define PTHREAD_MUTEX_RECURSIVE 0 -#define pthread_cond_t CONDITION_VARIABLE - -WINBASEAPI VOID WINAPI -InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI VOID WINAPI -WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI VOID WINAPI -WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI BOOL WINAPI -SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, - PCRITICAL_SECTION CriticalSection, - DWORD dwMilliseconds); - -#define pthread_cond_init(a,b) InitializeConditionVariable((a)) -#define pthread_cond_destroy(a) do {} while (0) -#define pthread_cond_wait(a,b) return_0(SleepConditionVariableCS((a), (b), INFINITE)) -#define pthread_cond_signal WakeConditionVariable -#define pthread_cond_broadcast WakeAllConditionVariable +#ifdef GIT_WIN_XP_SUPPORT +/* + * Implement simple condition variable for Windows threads, based on ACE + * implementation. + * + * See original implementation: http://bit.ly/1vkDjo + * ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html + * See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html + */ +typedef struct { + LONG waiters; + int was_broadcast; + CRITICAL_SECTION waiters_lock; + HANDLE sema; + HANDLE continue_broadcast; +} pthread_cond_t; +#else +typedef CONDITION_VARIABLE pthread_cond_t; +#endif + +extern int pthread_cond_init(pthread_cond_t *cond, const void *unused); +extern int pthread_cond_destroy(pthread_cond_t *cond); +extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex); +extern int pthread_cond_signal(pthread_cond_t *cond); +extern int pthread_cond_broadcast(pthread_cond_t *cond); /* * Simple thread creation implementation using pthread API From dba23ff7595efe917cacfdef32e2668d1033658a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:28:32 +0100 Subject: [PATCH 010/406] fixup! Format to 80 cols In preparation for applying the patch that actually made it to the Git mailing list, let's revert this. Signed-off-by: Johannes Schindelin --- compat/win32/pthread.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c index 27e6092781d854..80243b67cb8dc1 100644 --- a/compat/win32/pthread.c +++ b/compat/win32/pthread.c @@ -199,16 +199,10 @@ int pthread_cond_broadcast(pthread_cond_t *cond) #else // GIT_WIN_XP_SUPPORT -WINBASEAPI VOID WINAPI -InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI VOID WINAPI -WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI VOID WINAPI -WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI WINBOOL WINAPI -SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, - PCRITICAL_SECTION CriticalSection, - DWORD dwMilliseconds); +WINBASEAPI VOID WINAPI InitializeConditionVariable (PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI VOID WINAPI WakeConditionVariable (PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI VOID WINAPI WakeAllConditionVariable (PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI WINBOOL WINAPI SleepConditionVariableCS (PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds); int pthread_cond_init(pthread_cond_t *cond, const void *unused) { From f7ca946ce4ea503c4628900fa18ae105020ed273 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:28:49 +0100 Subject: [PATCH 011/406] fixup! Implement pthread_cond_t with Win32 CONDITION_VARIABLE In preparation for applying the patch that actually made it to the Git mailing list, let's revert this. Signed-off-by: Johannes Schindelin --- compat/win32/pthread.c | 40 ---------------------------------------- compat/win32/pthread.h | 4 ---- 2 files changed, 44 deletions(-) diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c index 80243b67cb8dc1..e18f5c6e2e55a4 100644 --- a/compat/win32/pthread.c +++ b/compat/win32/pthread.c @@ -57,8 +57,6 @@ pthread_t pthread_self(void) return t; } -#ifdef GIT_WIN_XP_SUPPORT - int pthread_cond_init(pthread_cond_t *cond, const void *unused) { cond->waiters = 0; @@ -196,41 +194,3 @@ int pthread_cond_broadcast(pthread_cond_t *cond) } return 0; } - -#else // GIT_WIN_XP_SUPPORT - -WINBASEAPI VOID WINAPI InitializeConditionVariable (PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI VOID WINAPI WakeConditionVariable (PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI VOID WINAPI WakeAllConditionVariable (PCONDITION_VARIABLE ConditionVariable); -WINBASEAPI WINBOOL WINAPI SleepConditionVariableCS (PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds); - -int pthread_cond_init(pthread_cond_t *cond, const void *unused) -{ - InitializeConditionVariable(cond); - return 0; -} - -int pthread_cond_destroy(pthread_cond_t *cond) -{ - return 0; -} - -int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) -{ - SleepConditionVariableCS(cond, mutex, INFINITE); - return 0; -} - -int pthread_cond_signal(pthread_cond_t *cond) -{ - WakeConditionVariable(cond); - return 0; -} - -int pthread_cond_broadcast(pthread_cond_t *cond) -{ - WakeAllConditionVariable(cond); - return 0; -} - -#endif // GIT_WIN_XP_SUPPORT diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h index 10a9d45f07eadd..1c164088fbb64d 100644 --- a/compat/win32/pthread.h +++ b/compat/win32/pthread.h @@ -32,7 +32,6 @@ typedef int pthread_mutexattr_t; #define pthread_mutexattr_settype(a, t) 0 #define PTHREAD_MUTEX_RECURSIVE 0 -#ifdef GIT_WIN_XP_SUPPORT /* * Implement simple condition variable for Windows threads, based on ACE * implementation. @@ -48,9 +47,6 @@ typedef struct { HANDLE sema; HANDLE continue_broadcast; } pthread_cond_t; -#else -typedef CONDITION_VARIABLE pthread_cond_t; -#endif extern int pthread_cond_init(pthread_cond_t *cond, const void *unused); extern int pthread_cond_destroy(pthread_cond_t *cond); From a0505d0a89b8e89f8854251d08ea484a5f539ea8 Mon Sep 17 00:00:00 2001 From: Loo Rong Jie Date: Thu, 22 Jun 2017 09:03:29 +0800 Subject: [PATCH 012/406] squash! Implement pthread_cond_t with Win32 CONDITION_VARIABLE win32: replace pthread_cond_*() with much simpler code The Win32 CONDITION_VARIABLE has better performance and is easier to maintain, as the code is a lot shorter now (the semantics of the CONDITION_VARIABLE matches the pthread_cond_t very well). Note: CONDITION_VARIABLE is not available in Windows XP and below, but the declared minimal Windows version required to build and run Git for Windows is Windows Vista (which is also beyond its end-of-life, but for less long than Windows XP), so that's okay. Signed-off-by: Loo Rong Jie Signed-off-by: Johannes Schindelin --- compat/win32/pthread.c | 138 ----------------------------------------- compat/win32/pthread.h | 28 +++------ 2 files changed, 7 insertions(+), 159 deletions(-) diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c index e18f5c6e2e55a4..2e7eead42cb008 100644 --- a/compat/win32/pthread.c +++ b/compat/win32/pthread.c @@ -56,141 +56,3 @@ pthread_t pthread_self(void) t.tid = GetCurrentThreadId(); return t; } - -int pthread_cond_init(pthread_cond_t *cond, const void *unused) -{ - cond->waiters = 0; - cond->was_broadcast = 0; - InitializeCriticalSection(&cond->waiters_lock); - - cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); - if (!cond->sema) - die("CreateSemaphore() failed"); - - cond->continue_broadcast = CreateEvent(NULL, /* security */ - FALSE, /* auto-reset */ - FALSE, /* not signaled */ - NULL); /* name */ - if (!cond->continue_broadcast) - die("CreateEvent() failed"); - - return 0; -} - -int pthread_cond_destroy(pthread_cond_t *cond) -{ - CloseHandle(cond->sema); - CloseHandle(cond->continue_broadcast); - DeleteCriticalSection(&cond->waiters_lock); - return 0; -} - -int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) -{ - int last_waiter; - - EnterCriticalSection(&cond->waiters_lock); - cond->waiters++; - LeaveCriticalSection(&cond->waiters_lock); - - /* - * Unlock external mutex and wait for signal. - * NOTE: we've held mutex locked long enough to increment - * waiters count above, so there's no problem with - * leaving mutex unlocked before we wait on semaphore. - */ - LeaveCriticalSection(mutex); - - /* let's wait - ignore return value */ - WaitForSingleObject(cond->sema, INFINITE); - - /* - * Decrease waiters count. If we are the last waiter, then we must - * notify the broadcasting thread that it can continue. - * But if we continued due to cond_signal, we do not have to do that - * because the signaling thread knows that only one waiter continued. - */ - EnterCriticalSection(&cond->waiters_lock); - cond->waiters--; - last_waiter = cond->was_broadcast && cond->waiters == 0; - LeaveCriticalSection(&cond->waiters_lock); - - if (last_waiter) { - /* - * cond_broadcast was issued while mutex was held. This means - * that all other waiters have continued, but are contending - * for the mutex at the end of this function because the - * broadcasting thread did not leave cond_broadcast, yet. - * (This is so that it can be sure that each waiter has - * consumed exactly one slice of the semaphor.) - * The last waiter must tell the broadcasting thread that it - * can go on. - */ - SetEvent(cond->continue_broadcast); - /* - * Now we go on to contend with all other waiters for - * the mutex. Auf in den Kampf! - */ - } - /* lock external mutex again */ - EnterCriticalSection(mutex); - - return 0; -} - -/* - * IMPORTANT: This implementation requires that pthread_cond_signal - * is called while the mutex is held that is used in the corresponding - * pthread_cond_wait calls! - */ -int pthread_cond_signal(pthread_cond_t *cond) -{ - int have_waiters; - - EnterCriticalSection(&cond->waiters_lock); - have_waiters = cond->waiters > 0; - LeaveCriticalSection(&cond->waiters_lock); - - /* - * Signal only when there are waiters - */ - if (have_waiters) - return ReleaseSemaphore(cond->sema, 1, NULL) ? - 0 : err_win_to_posix(GetLastError()); - else - return 0; -} - -/* - * DOUBLY IMPORTANT: This implementation requires that pthread_cond_broadcast - * is called while the mutex is held that is used in the corresponding - * pthread_cond_wait calls! - */ -int pthread_cond_broadcast(pthread_cond_t *cond) -{ - EnterCriticalSection(&cond->waiters_lock); - - if ((cond->was_broadcast = cond->waiters > 0)) { - /* wake up all waiters */ - ReleaseSemaphore(cond->sema, cond->waiters, NULL); - LeaveCriticalSection(&cond->waiters_lock); - /* - * At this point all waiters continue. Each one takes its - * slice of the semaphor. Now it's our turn to wait: Since - * the external mutex is held, no thread can leave cond_wait, - * yet. For this reason, we can be sure that no thread gets - * a chance to eat *more* than one slice. OTOH, it means - * that the last waiter must send us a wake-up. - */ - WaitForSingleObject(cond->continue_broadcast, INFINITE); - /* - * Since the external mutex is held, no thread can enter - * cond_wait, and, hence, it is safe to reset this flag - * without cond->waiters_lock held. - */ - cond->was_broadcast = 0; - } else { - LeaveCriticalSection(&cond->waiters_lock); - } - return 0; -} diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h index 1c164088fbb64d..c6cb8dd2190062 100644 --- a/compat/win32/pthread.h +++ b/compat/win32/pthread.h @@ -32,27 +32,13 @@ typedef int pthread_mutexattr_t; #define pthread_mutexattr_settype(a, t) 0 #define PTHREAD_MUTEX_RECURSIVE 0 -/* - * Implement simple condition variable for Windows threads, based on ACE - * implementation. - * - * See original implementation: http://bit.ly/1vkDjo - * ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html - * See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html - */ -typedef struct { - LONG waiters; - int was_broadcast; - CRITICAL_SECTION waiters_lock; - HANDLE sema; - HANDLE continue_broadcast; -} pthread_cond_t; - -extern int pthread_cond_init(pthread_cond_t *cond, const void *unused); -extern int pthread_cond_destroy(pthread_cond_t *cond); -extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex); -extern int pthread_cond_signal(pthread_cond_t *cond); -extern int pthread_cond_broadcast(pthread_cond_t *cond); +#define pthread_cond_t CONDITION_VARIABLE + +#define pthread_cond_init(a,b) InitializeConditionVariable((a)) +#define pthread_cond_destroy(a) do {} while (0) +#define pthread_cond_wait(a,b) return_0(SleepConditionVariableCS((a), (b), INFINITE)) +#define pthread_cond_signal WakeConditionVariable +#define pthread_cond_broadcast WakeAllConditionVariable /* * Simple thread creation implementation using pthread API From 2fb1d8b7cd5a49612f60dbc06fa736e46625f340 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:35:05 +0100 Subject: [PATCH 013/406] fixup! tests: explicitly use `git.exe` on Windows In preparation for applying the latest iteration that was submitted to the Git mailing list, let's revert this here patch. Signed-off-by: Johannes Schindelin --- Makefile | 1 - t/test-lib-functions.sh | 2 +- t/test-lib.sh | 13 ++++--------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 28d78688fa39fb..5a969f5830a410 100644 --- a/Makefile +++ b/Makefile @@ -2567,7 +2567,6 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+ @echo PAGER_ENV=\''$(subst ','\'',$(subst ','\'',$(PAGER_ENV)))'\' >>$@+ @echo DC_SHA1=\''$(subst ','\'',$(subst ','\'',$(DC_SHA1)))'\' >>$@+ - @echo X=\'$(X)\' >>$@+ ifdef TEST_OUTPUT_DIRECTORY @echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+ endif diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 01bbad2e19f9c2..849b14a7cf7be1 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -898,7 +898,7 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" init \ + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git" init \ "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?" mv .git/hooks .git/hooks-disabled diff --git a/t/test-lib.sh b/t/test-lib.sh index 5bef52d71a7997..1f361886852549 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -49,23 +49,18 @@ export ASAN_OPTIONS : ${LSAN_OPTIONS=abort_on_error=1} export LSAN_OPTIONS -if test ! -f "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS -then - echo >&2 'error: GIT-BUILD-OPTIONS missing (has Git been built?).' - exit 1 -fi -. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS -export PERL_PATH SHELL_PATH - ################################################################ # It appears that people try to run tests without building... -test -n "$GIT_TEST_INSTALLED" || "$GIT_BUILD_DIR/git$X" >/dev/null || +test -n "$GIT_TEST_INSTALLED" || "$GIT_BUILD_DIR/git" >/dev/null || if test $? != 1 then echo >&2 'error: you do not seem to have built git yet.' exit 1 fi +. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS +export PERL_PATH SHELL_PATH + # if --tee was passed, write the output not only to the terminal, but # additionally to the file test-results/$BASENAME.out, too. case "$GIT_TEST_TEE_STARTED, $* " in From 1f438ff32f985f2d42b0b83a095369ba89e10921 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:35:33 +0100 Subject: [PATCH 014/406] fixup! tests: do not require Git to be built when testing an installed Git In preparation for applying the latest iteration that was submitted to the Git mailing list, let's revert this here patch. Signed-off-by: Johannes Schindelin --- t/test-lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 1f361886852549..713eb718e68501 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -51,7 +51,7 @@ export LSAN_OPTIONS ################################################################ # It appears that people try to run tests without building... -test -n "$GIT_TEST_INSTALLED" || "$GIT_BUILD_DIR/git" >/dev/null || +"$GIT_BUILD_DIR/git" >/dev/null if test $? != 1 then echo >&2 'error: you do not seem to have built git yet.' From c1f55c4fcd25672611292079f4f5f8ec9d8dc420 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:35:39 +0100 Subject: [PATCH 015/406] fixup! t/lib-gettext: test installed git-sh-i18n if GIT_TEST_INSTALLED is set In preparation for applying the latest iteration that was submitted to the Git mailing list, let's revert this here patch. Signed-off-by: Johannes Schindelin --- t/lib-gettext.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/t/lib-gettext.sh b/t/lib-gettext.sh index 9eb160c997a0f4..eec757f104708d 100644 --- a/t/lib-gettext.sh +++ b/t/lib-gettext.sh @@ -10,12 +10,7 @@ GIT_TEXTDOMAINDIR="$GIT_BUILD_DIR/po/build/locale" GIT_PO_PATH="$GIT_BUILD_DIR/po" export GIT_TEXTDOMAINDIR GIT_PO_PATH -if test -n "$GIT_TEST_INSTALLED" -then - . "$(git --exec-path)"/git-sh-i18n -else - . "$GIT_BUILD_DIR"/git-sh-i18n -fi +. "$GIT_BUILD_DIR"/git-sh-i18n if test_have_prereq GETTEXT && ! test_have_prereq GETTEXT_POISON then From 69d262d0ec29619db289d16edf9d5a4f876e03f2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:35:42 +0100 Subject: [PATCH 016/406] fixup! tests: respect GIT_TEST_INSTALLED when initializing repositories In preparation for applying the latest iteration that was submitted to the Git mailing list, let's revert this here patch. Signed-off-by: Johannes Schindelin --- t/test-lib-functions.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 849b14a7cf7be1..4207af40777c69 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -898,8 +898,7 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git" init \ - "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || + "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?" mv .git/hooks .git/hooks-disabled ) || exit From d64d6debd2dc6b78423f42b6bd76bcf9140f478f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 19 Nov 2018 11:35:45 +0100 Subject: [PATCH 017/406] fixup! tests: fix GIT_TEST_INSTALLED's PATH to include t/helper/ In preparation for applying the latest iteration that was submitted to the Git mailing list, let's revert this here patch. Signed-off-by: Johannes Schindelin --- t/test-lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 713eb718e68501..44288cbb598435 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -926,7 +926,7 @@ elif test -n "$GIT_TEST_INSTALLED" then GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) || error "Cannot run git from $GIT_TEST_INSTALLED." - PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH + PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} else # normal case, use ../bin-wrappers only unless $with_dashes: git_bin_dir="$GIT_BUILD_DIR/bin-wrappers" From 7801f037afa954b5e89d58b780764fde7dac7982 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 21 Jul 2017 22:17:20 +0200 Subject: [PATCH 018/406] squash! tests: fix GIT_TEST_INSTALLED's PATH to include t/helper/ tests: fix GIT_TEST_INSTALLED's PATH to include t/helper/ We really need to be able to find the test helpers... Really. This change was forgotten when we moved the test helpers into t/helper/ Signed-off-by: Johannes Schindelin --- t/test-lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 44288cbb598435..713eb718e68501 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -926,7 +926,7 @@ elif test -n "$GIT_TEST_INSTALLED" then GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) || error "Cannot run git from $GIT_TEST_INSTALLED." - PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH + PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} else # normal case, use ../bin-wrappers only unless $with_dashes: git_bin_dir="$GIT_BUILD_DIR/bin-wrappers" From faa7cccbc900056113422ddd6d526c3ce582c6b8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 20 Jul 2017 00:09:44 +0200 Subject: [PATCH 019/406] squash! tests: respect GIT_TEST_INSTALLED when initializing repositories tests: respect GIT_TEST_INSTALLED when initializing repositories It really makes very, very little sense to use a different git executable than the one the caller indicated via setting the environment variable GIT_TEST_INSTALLED. Signed-off-by: Johannes Schindelin --- t/test-lib-functions.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 4207af40777c69..849b14a7cf7be1 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -898,7 +898,8 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git" init \ + "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?" mv .git/hooks .git/hooks-disabled ) || exit From fe37aea812d7f0c2dcd8b3875487b458560138a7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 18 Jul 2017 23:32:36 +0200 Subject: [PATCH 020/406] squash! t/lib-gettext: test installed git-sh-i18n if GIT_TEST_INSTALLED is set t/lib-gettext: test installed git-sh-i18n if GIT_TEST_INSTALLED is set It makes very, very little sense to test the built git-sh-i18n when the user asked specifically to test another one. Signed-off-by: Johannes Schindelin --- t/lib-gettext.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/t/lib-gettext.sh b/t/lib-gettext.sh index eec757f104708d..9eb160c997a0f4 100644 --- a/t/lib-gettext.sh +++ b/t/lib-gettext.sh @@ -10,7 +10,12 @@ GIT_TEXTDOMAINDIR="$GIT_BUILD_DIR/po/build/locale" GIT_PO_PATH="$GIT_BUILD_DIR/po" export GIT_TEXTDOMAINDIR GIT_PO_PATH -. "$GIT_BUILD_DIR"/git-sh-i18n +if test -n "$GIT_TEST_INSTALLED" +then + . "$(git --exec-path)"/git-sh-i18n +else + . "$GIT_BUILD_DIR"/git-sh-i18n +fi if test_have_prereq GETTEXT && ! test_have_prereq GETTEXT_POISON then From ce7b0f2e14c28877cc78229ea08fe88ced22d19f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 5 Aug 2017 00:18:33 +0200 Subject: [PATCH 021/406] squash! tests: do not require Git to be built when testing an installed Git tests: do not require Git to be built when testing an installed Git We really only need the test helpers to be built in the worktree in that case, but that is not what we test for. On the other hand it is a perfect opportunity to verify that `GIT_TEST_INSTALLED` points to a working Git. So let's test the appropriate Git executable. While at it, also adjust the error message in the `GIT_TEST_INSTALLED` case. This patch is best viewed with `-w --patience`. Helped-by: Jeff King Signed-off-by: Johannes Schindelin --- t/test-lib.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 713eb718e68501..9b688d7698518e 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -51,10 +51,15 @@ export LSAN_OPTIONS ################################################################ # It appears that people try to run tests without building... -"$GIT_BUILD_DIR/git" >/dev/null +"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git" >/dev/null if test $? != 1 then - echo >&2 'error: you do not seem to have built git yet.' + if test -n "$GIT_TEST_INSTALLED" + then + echo >&2 "error: there is no working Git at '$GIT_TEST_INSTALLED'" + else + echo >&2 'error: you do not seem to have built git yet.' + fi exit 1 fi From b094daa8750bb3ba014ab1d263d4362af31823cf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 3 Nov 2016 09:40:12 -0700 Subject: [PATCH 022/406] squash! tests: explicitly use `git.exe` on Windows tests: explicitly use `git.exe` on Windows On Windows, when we refer to `/an/absolute/path/to/git`, it magically resolves `git.exe` at that location. Except if something of the name `git` exists next to that `git.exe`. So if we call `$BUILD_DIR/git`, it will find `$BUILD_DIR/git.exe` *only* if there is not, say, a directory called `$BUILD_DIR/git`. Such a directory, however, exists in Git for Windows when building with Visual Studio (our Visual Studio project generator defaults to putting the build files into a directory whose name is the base name of the corresponding `.exe`). In the bin-wrappers/* scripts, we already take pains to use `git.exe` rather than `git`, as this could pick up the wrong thing on Windows (i.e. if there exists a `git` file or directory in the build directory). Now we do the same in the tests' start-up code. This also helps when testing an installed Git, as there might be even more likely some stray file or directory in the way. Note: the only way we can record whether the `.exe` suffix is by writing it to the `GIT-BUILD-OPTIONS` file and sourcing it at the beginning of `t/test-lib.sh`. This is not a requirement introduced by this patch, but we move the call to be able to use the `$X` variable that holds the file extension, if any. Note also: the many, many calls to `git this` and `git that` are unaffected, as the regular PATH search will find the `.exe` files on Windows (and not be confused by a directory of the name `git` that is in one of the directories listed in the `PATH` variable), while `/path/to/git` would not, per se, know that it is looking for an executable and happily prefer such a directory. Signed-off-by: Johannes Schindelin --- Makefile | 1 + t/test-lib-functions.sh | 2 +- t/test-lib.sh | 13 +++++++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 5a969f5830a410..28d78688fa39fb 100644 --- a/Makefile +++ b/Makefile @@ -2567,6 +2567,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+ @echo PAGER_ENV=\''$(subst ','\'',$(subst ','\'',$(PAGER_ENV)))'\' >>$@+ @echo DC_SHA1=\''$(subst ','\'',$(subst ','\'',$(DC_SHA1)))'\' >>$@+ + @echo X=\'$(X)\' >>$@+ ifdef TEST_OUTPUT_DIRECTORY @echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+ endif diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 849b14a7cf7be1..01bbad2e19f9c2 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -898,7 +898,7 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git" init \ + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" init \ "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?" mv .git/hooks .git/hooks-disabled diff --git a/t/test-lib.sh b/t/test-lib.sh index 9b688d7698518e..60469ec7b0ef5e 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -49,9 +49,17 @@ export ASAN_OPTIONS : ${LSAN_OPTIONS=abort_on_error=1} export LSAN_OPTIONS +if test ! -f "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS +then + echo >&2 'error: GIT-BUILD-OPTIONS missing (has Git been built?).' + exit 1 +fi +. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS +export PERL_PATH SHELL_PATH + ################################################################ # It appears that people try to run tests without building... -"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git" >/dev/null +"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null if test $? != 1 then if test -n "$GIT_TEST_INSTALLED" @@ -63,9 +71,6 @@ then exit 1 fi -. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS -export PERL_PATH SHELL_PATH - # if --tee was passed, write the output not only to the terminal, but # additionally to the file test-results/$BASENAME.out, too. case "$GIT_TEST_TEE_STARTED, $* " in From f3bd77d6e2924060523d99978238efa3188f3b27 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 12 Oct 2015 19:24:05 +0000 Subject: [PATCH 023/406] getpwuid(mingw): initialize the structure only once Signed-off-by: Johannes Schindelin --- compat/mingw.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..32fa6e7c1fa1ef 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1770,16 +1770,27 @@ int mingw_getpagesize(void) struct passwd *getpwuid(int uid) { + static unsigned initialized; static char user_name[100]; - static struct passwd p; + static struct passwd *p; + DWORD len; - DWORD len = sizeof(user_name); - if (!GetUserName(user_name, &len)) + if (initialized) + return p; + + len = sizeof(user_name); + if (!GetUserName(user_name, &len)) { + initialized = 1; return NULL; - p.pw_name = user_name; - p.pw_gecos = "unknown"; - p.pw_dir = NULL; - return &p; + } + + p = xmalloc(sizeof(*p)); + p->pw_name = user_name; + p->pw_gecos = "unknown"; + p->pw_dir = NULL; + + initialized = 1; + return p; } static HANDLE timer_event; From 8f39bea1918c683b8534559f3671e20a3a3a3f5b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 13 Oct 2015 10:05:53 +0000 Subject: [PATCH 024/406] getpwuid(mingw): provide a better default for the user name We do have the excellent GetUserInfoEx() function to obtain more detailed information of the current user (if the user is part of a Windows domain); Let's use it. Suggested by Lutz Roeder. To avoid the cost of loading Secur32.dll (even lazily, loading DLLs takes a non-neglibile amount of time), we use the established technique to load DLLs only when, and if, needed. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 32fa6e7c1fa1ef..90554712932f19 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -5,6 +5,7 @@ #include "../strbuf.h" #include "../run-command.h" #include "../cache.h" +#include "win32/lazyload.h" #define HCAST(type, handle) ((type)(intptr_t)handle) @@ -1768,6 +1769,33 @@ int mingw_getpagesize(void) return si.dwAllocationGranularity; } +/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */ +enum EXTENDED_NAME_FORMAT { + NameDisplay = 3, + NameUserPrincipal = 8 +}; + +static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type) +{ + DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW, + enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG); + static wchar_t wbuffer[1024]; + DWORD len; + + if (!INIT_PROC_ADDR(GetUserNameExW)) + return NULL; + + len = ARRAY_SIZE(wbuffer); + if (GetUserNameExW(type, wbuffer, &len)) { + char *converted = xmalloc((len *= 3)); + if (xwcstoutf(converted, wbuffer, len) >= 0) + return converted; + free(converted); + } + + return NULL; +} + struct passwd *getpwuid(int uid) { static unsigned initialized; @@ -1786,7 +1814,9 @@ struct passwd *getpwuid(int uid) p = xmalloc(sizeof(*p)); p->pw_name = user_name; - p->pw_gecos = "unknown"; + p->pw_gecos = get_extended_user_info(NameDisplay); + if (!p->pw_gecos) + p->pw_gecos = "unknown"; p->pw_dir = NULL; initialized = 1; From cdcd259c6bcfd14487b97c7f95b8014d4b84d21d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 13 Oct 2015 10:36:52 +0000 Subject: [PATCH 025/406] mingw: use domain information for default email When a user is registered in a Windows domain, it is really easy to obtain the email address. So let's do that. Suggested by Lutz Roeder. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 5 +++++ compat/mingw.h | 2 ++ git-compat-util.h | 4 ++++ ident.c | 3 +++ 4 files changed, 14 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 90554712932f19..45c8046c7cc34f 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1796,6 +1796,11 @@ static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type) return NULL; } +char *mingw_query_user_email(void) +{ + return get_extended_user_info(NameUserPrincipal); +} + struct passwd *getpwuid(int uid) { static unsigned initialized; diff --git a/compat/mingw.h b/compat/mingw.h index 571019d0bddcea..f31dcff2be1d60 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -424,6 +424,8 @@ static inline void convert_slashes(char *path) int mingw_offset_1st_component(const char *path); #define offset_1st_component mingw_offset_1st_component #define PATH_SEP ';' +extern char *mingw_query_user_email(void); +#define query_user_email mingw_query_user_email #if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800) #define PRIuMAX "I64u" #define PRId64 "I64d" diff --git a/git-compat-util.h b/git-compat-util.h index 5f2e90932f9990..71779cb0aeb6f3 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -382,6 +382,10 @@ static inline char *git_find_last_dir_sep(const char *path) #define find_last_dir_sep git_find_last_dir_sep #endif +#ifndef query_user_email +#define query_user_email() NULL +#endif + #if defined(__HP_cc) && (__HP_cc >= 61000) #define NORETURN __attribute__((noreturn)) #define NORETURN_PTR diff --git a/ident.c b/ident.c index 327abe557f5a66..33bcf40644cdf2 100644 --- a/ident.c +++ b/ident.c @@ -168,6 +168,9 @@ const char *ident_default_email(void) strbuf_addstr(&git_default_email, email); committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; author_ident_explicitly_given |= IDENT_MAIL_GIVEN; + } else if ((email = query_user_email()) && email[0]) { + strbuf_addstr(&git_default_email, email); + free((char *)email); } else copy_email(xgetpwuid_self(&default_email_is_bogus), &git_default_email, &default_email_is_bogus); From a64fdacb3c0cff5e3ae896ef89fec374789ed7cd Mon Sep 17 00:00:00 2001 From: Loo Rong Jie Date: Thu, 22 Jun 2017 09:03:29 +0800 Subject: [PATCH 026/406] win32: replace pthread_cond_*() with much simpler code The Win32 CONDITION_VARIABLE has better performance and is easier to maintain, as the code is a lot shorter now (the semantics of the CONDITION_VARIABLE matches the pthread_cond_t very well). Note: CONDITION_VARIABLE is not available in Windows XP and below, but the declared minimal Windows version required to build and run Git for Windows is Windows Vista (which is also beyond its end-of-life, but for less long than Windows XP), so that's okay. Signed-off-by: Loo Rong Jie Signed-off-by: Johannes Schindelin --- compat/win32/pthread.c | 138 ----------------------------------------- compat/win32/pthread.h | 28 +++------ 2 files changed, 7 insertions(+), 159 deletions(-) diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c index e18f5c6e2e55a4..2e7eead42cb008 100644 --- a/compat/win32/pthread.c +++ b/compat/win32/pthread.c @@ -56,141 +56,3 @@ pthread_t pthread_self(void) t.tid = GetCurrentThreadId(); return t; } - -int pthread_cond_init(pthread_cond_t *cond, const void *unused) -{ - cond->waiters = 0; - cond->was_broadcast = 0; - InitializeCriticalSection(&cond->waiters_lock); - - cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); - if (!cond->sema) - die("CreateSemaphore() failed"); - - cond->continue_broadcast = CreateEvent(NULL, /* security */ - FALSE, /* auto-reset */ - FALSE, /* not signaled */ - NULL); /* name */ - if (!cond->continue_broadcast) - die("CreateEvent() failed"); - - return 0; -} - -int pthread_cond_destroy(pthread_cond_t *cond) -{ - CloseHandle(cond->sema); - CloseHandle(cond->continue_broadcast); - DeleteCriticalSection(&cond->waiters_lock); - return 0; -} - -int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) -{ - int last_waiter; - - EnterCriticalSection(&cond->waiters_lock); - cond->waiters++; - LeaveCriticalSection(&cond->waiters_lock); - - /* - * Unlock external mutex and wait for signal. - * NOTE: we've held mutex locked long enough to increment - * waiters count above, so there's no problem with - * leaving mutex unlocked before we wait on semaphore. - */ - LeaveCriticalSection(mutex); - - /* let's wait - ignore return value */ - WaitForSingleObject(cond->sema, INFINITE); - - /* - * Decrease waiters count. If we are the last waiter, then we must - * notify the broadcasting thread that it can continue. - * But if we continued due to cond_signal, we do not have to do that - * because the signaling thread knows that only one waiter continued. - */ - EnterCriticalSection(&cond->waiters_lock); - cond->waiters--; - last_waiter = cond->was_broadcast && cond->waiters == 0; - LeaveCriticalSection(&cond->waiters_lock); - - if (last_waiter) { - /* - * cond_broadcast was issued while mutex was held. This means - * that all other waiters have continued, but are contending - * for the mutex at the end of this function because the - * broadcasting thread did not leave cond_broadcast, yet. - * (This is so that it can be sure that each waiter has - * consumed exactly one slice of the semaphor.) - * The last waiter must tell the broadcasting thread that it - * can go on. - */ - SetEvent(cond->continue_broadcast); - /* - * Now we go on to contend with all other waiters for - * the mutex. Auf in den Kampf! - */ - } - /* lock external mutex again */ - EnterCriticalSection(mutex); - - return 0; -} - -/* - * IMPORTANT: This implementation requires that pthread_cond_signal - * is called while the mutex is held that is used in the corresponding - * pthread_cond_wait calls! - */ -int pthread_cond_signal(pthread_cond_t *cond) -{ - int have_waiters; - - EnterCriticalSection(&cond->waiters_lock); - have_waiters = cond->waiters > 0; - LeaveCriticalSection(&cond->waiters_lock); - - /* - * Signal only when there are waiters - */ - if (have_waiters) - return ReleaseSemaphore(cond->sema, 1, NULL) ? - 0 : err_win_to_posix(GetLastError()); - else - return 0; -} - -/* - * DOUBLY IMPORTANT: This implementation requires that pthread_cond_broadcast - * is called while the mutex is held that is used in the corresponding - * pthread_cond_wait calls! - */ -int pthread_cond_broadcast(pthread_cond_t *cond) -{ - EnterCriticalSection(&cond->waiters_lock); - - if ((cond->was_broadcast = cond->waiters > 0)) { - /* wake up all waiters */ - ReleaseSemaphore(cond->sema, cond->waiters, NULL); - LeaveCriticalSection(&cond->waiters_lock); - /* - * At this point all waiters continue. Each one takes its - * slice of the semaphor. Now it's our turn to wait: Since - * the external mutex is held, no thread can leave cond_wait, - * yet. For this reason, we can be sure that no thread gets - * a chance to eat *more* than one slice. OTOH, it means - * that the last waiter must send us a wake-up. - */ - WaitForSingleObject(cond->continue_broadcast, INFINITE); - /* - * Since the external mutex is held, no thread can enter - * cond_wait, and, hence, it is safe to reset this flag - * without cond->waiters_lock held. - */ - cond->was_broadcast = 0; - } else { - LeaveCriticalSection(&cond->waiters_lock); - } - return 0; -} diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h index 1c164088fbb64d..c6cb8dd2190062 100644 --- a/compat/win32/pthread.h +++ b/compat/win32/pthread.h @@ -32,27 +32,13 @@ typedef int pthread_mutexattr_t; #define pthread_mutexattr_settype(a, t) 0 #define PTHREAD_MUTEX_RECURSIVE 0 -/* - * Implement simple condition variable for Windows threads, based on ACE - * implementation. - * - * See original implementation: http://bit.ly/1vkDjo - * ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html - * See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html - */ -typedef struct { - LONG waiters; - int was_broadcast; - CRITICAL_SECTION waiters_lock; - HANDLE sema; - HANDLE continue_broadcast; -} pthread_cond_t; - -extern int pthread_cond_init(pthread_cond_t *cond, const void *unused); -extern int pthread_cond_destroy(pthread_cond_t *cond); -extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex); -extern int pthread_cond_signal(pthread_cond_t *cond); -extern int pthread_cond_broadcast(pthread_cond_t *cond); +#define pthread_cond_t CONDITION_VARIABLE + +#define pthread_cond_init(a,b) InitializeConditionVariable((a)) +#define pthread_cond_destroy(a) do {} while (0) +#define pthread_cond_wait(a,b) return_0(SleepConditionVariableCS((a), (b), INFINITE)) +#define pthread_cond_signal WakeConditionVariable +#define pthread_cond_broadcast WakeAllConditionVariable /* * Simple thread creation implementation using pthread API From 4f57dd78878871fdfc3453da733f975bde7739cf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 30 Aug 2017 15:26:35 +0200 Subject: [PATCH 027/406] config: rename `dummy` parameter to `cb` in git_default_config() This is the convention elsewhere (and prepares for the case where we may need to pass callback data). Signed-off-by: Johannes Schindelin --- config.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.c b/config.c index 3461993f0af665..f2d89ae2e7c37a 100644 --- a/config.c +++ b/config.c @@ -1448,13 +1448,13 @@ static int git_default_mailmap_config(const char *var, const char *value) return 0; } -int git_default_config(const char *var, const char *value, void *dummy) +int git_default_config(const char *var, const char *value, void *cb) { if (starts_with(var, "core.")) return git_default_core_config(var, value); if (starts_with(var, "user.")) - return git_ident_config(var, value, dummy); + return git_ident_config(var, value, cb); if (starts_with(var, "i18n.")) return git_default_i18n_config(var, value); From c1130c1bad5dffd22ecaf277d45dac6ccfe3f909 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 7 Sep 2016 18:07:04 +0200 Subject: [PATCH 028/406] reset: support the experimental --stdin option Just like with other Git commands, this option makes it read the paths from the standard input. It comes in handy when resetting many, many paths at once and wildcards are not an option (e.g. when the paths are generated by a tool). Note: we first parse the entire list and perform the actual reset action only in a second phase. Not only does this make things simpler, it also helps performance, as do_diff_cache() traverses the index and the (sorted) pathspecs in simultaneously to avoid unnecessary lookups. This feature is marked experimental because it is still under review in the upstream Git project. Signed-off-by: Johannes Schindelin --- Documentation/git-reset.txt | 10 +++++++ builtin/reset.c | 53 ++++++++++++++++++++++++++++++++++++- t/t7108-reset-stdin.sh | 32 ++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100755 t/t7108-reset-stdin.sh diff --git a/Documentation/git-reset.txt b/Documentation/git-reset.txt index 1d697d996220b2..14312c6d324f31 100644 --- a/Documentation/git-reset.txt +++ b/Documentation/git-reset.txt @@ -10,6 +10,7 @@ SYNOPSIS [verse] 'git reset' [-q] [] [--] ... 'git reset' (--patch | -p) [] [--] [...] +EXPERIMENTAL: 'git reset' [-q] [--stdin [-z]] [] 'git reset' [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [] DESCRIPTION @@ -97,6 +98,15 @@ OPTIONS --quiet:: Be quiet, only report errors. +--stdin:: + EXPERIMENTAL: Instead of taking list of paths from the + command line, read list of paths from the standard input. + Paths are separated by LF (i.e. one path per line) by + default. + +-z:: + EXPERIMENTAL: Only meaningful with `--stdin`; paths are + separated with NUL character instead of LF. EXAMPLES -------- diff --git a/builtin/reset.c b/builtin/reset.c index 11cd0dcb8cc73a..b79e8406675283 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -24,10 +24,13 @@ #include "cache-tree.h" #include "submodule.h" #include "submodule-config.h" +#include "strbuf.h" +#include "quote.h" static const char * const git_reset_usage[] = { N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] []"), N_("git reset [-q] [] [--] ..."), + N_("EXPERIMENTAL: git reset [-q] [--stdin [-z]] []"), N_("git reset --patch [] [--] [...]"), NULL }; @@ -280,7 +283,9 @@ static int git_reset_config(const char *var, const char *value, void *cb) int cmd_reset(int argc, const char **argv, const char *prefix) { int reset_type = NONE, update_ref_status = 0, quiet = 0; - int patch_mode = 0, unborn; + int patch_mode = 0, nul_term_line = 0, read_from_stdin = 0, unborn; + char **stdin_paths = NULL; + int stdin_nr = 0, stdin_alloc = 0; const char *rev; struct object_id oid; struct pathspec pathspec; @@ -302,6 +307,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix) OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")), OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that removed paths will be added later")), + OPT_BOOL('z', NULL, &nul_term_line, + N_("EXPERIMENTAL: paths are separated with NUL character")), + OPT_BOOL(0, "stdin", &read_from_stdin, + N_("EXPERIMENTAL: read paths from ")), OPT_END() }; @@ -311,6 +320,42 @@ int cmd_reset(int argc, const char **argv, const char *prefix) PARSE_OPT_KEEP_DASHDASH); parse_args(&pathspec, argv, prefix, patch_mode, &rev); + if (read_from_stdin) { + strbuf_getline_fn getline_fn = nul_term_line ? + strbuf_getline_nul : strbuf_getline_lf; + int flags = PATHSPEC_PREFER_FULL; + struct strbuf buf = STRBUF_INIT; + struct strbuf unquoted = STRBUF_INIT; + + if (patch_mode) + die(_("--stdin is incompatible with --patch")); + + if (pathspec.nr) + die(_("--stdin is incompatible with path arguments")); + + while (getline_fn(&buf, stdin) != EOF) { + if (!nul_term_line && buf.buf[0] == '"') { + strbuf_reset(&unquoted); + if (unquote_c_style(&unquoted, buf.buf, NULL)) + die(_("line is badly quoted")); + strbuf_swap(&buf, &unquoted); + } + ALLOC_GROW(stdin_paths, stdin_nr + 1, stdin_alloc); + stdin_paths[stdin_nr++] = xstrdup(buf.buf); + strbuf_reset(&buf); + } + strbuf_release(&unquoted); + strbuf_release(&buf); + + ALLOC_GROW(stdin_paths, stdin_nr + 1, stdin_alloc); + stdin_paths[stdin_nr++] = NULL; + flags |= PATHSPEC_LITERAL_PATH; + parse_pathspec(&pathspec, 0, flags, prefix, + (const char **)stdin_paths); + + } else if (nul_term_line) + die(_("-z requires --stdin")); + unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid); if (unborn) { /* reset on unborn branch: treat as reset to empty tree */ @@ -401,5 +446,11 @@ int cmd_reset(int argc, const char **argv, const char *prefix) if (!pathspec.nr) remove_branch_state(); + if (stdin_paths) { + while (stdin_nr) + free(stdin_paths[--stdin_nr]); + free(stdin_paths); + } + return update_ref_status; } diff --git a/t/t7108-reset-stdin.sh b/t/t7108-reset-stdin.sh new file mode 100755 index 00000000000000..b7cbcbf869296c --- /dev/null +++ b/t/t7108-reset-stdin.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +test_description='reset --stdin' + +. ./test-lib.sh + +test_expect_success 'reset --stdin' ' + test_commit hello && + git rm hello.t && + test -z "$(git ls-files hello.t)" && + echo hello.t | git reset --stdin && + test hello.t = "$(git ls-files hello.t)" +' + +test_expect_success 'reset --stdin -z' ' + test_commit world && + git rm hello.t world.t && + test -z "$(git ls-files hello.t world.t)" && + printf world.tQworld.tQhello.tQ | q_to_nul | git reset --stdin -z && + printf "hello.t\nworld.t\n" >expect && + git ls-files >actual && + test_cmp expect actual +' + +test_expect_success '--stdin requires --mixed' ' + echo hello.t >list && + test_must_fail git reset --soft --stdin Date: Wed, 2 Nov 2016 13:51:20 -0400 Subject: [PATCH 029/406] cvsexportcommit: force crlf translation When using cvsnt + msys + git, it seems like the output of cvs status had \r\n in it, and caused the command to fail. This fixes that. Signed-off-by: Dustin Spicuzza --- git-cvsexportcommit.perl | 1 + 1 file changed, 1 insertion(+) diff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl index d13f02da95f3b9..fc00d5946af68e 100755 --- a/git-cvsexportcommit.perl +++ b/git-cvsexportcommit.perl @@ -431,6 +431,7 @@ END sub safe_pipe_capture { my @output; if (my $pid = open my $child, '-|') { + binmode($child, ":crlf"); @output = (<$child>); close $child or die join(' ',@_).": $! $?"; } else { From cb8cbcb79ee958e89c9b6fe0df2d9c554625f8df Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Sun, 24 Jul 2011 15:54:04 +0200 Subject: [PATCH 030/406] t9350: point out that refs are not updated correctly This happens only when the corresponding commits are not exported in the current fast-export run. This can happen either when the relevant commit is already marked, or when the commit is explicitly marked as UNINTERESTING with a negative ref by another argument. This breaks fast-export basec remote helpers. Signed-off-by: Sverre Rabbelier --- t/t9350-fast-export.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/t/t9350-fast-export.sh b/t/t9350-fast-export.sh index 6a392e87bcc177..6d6d5050a299b6 100755 --- a/t/t9350-fast-export.sh +++ b/t/t9350-fast-export.sh @@ -556,4 +556,15 @@ test_expect_success 'merge commit gets exported with --import-marks' ' ) ' +cat > expected << EOF +reset refs/heads/master +from $(git rev-parse master) + +EOF + +test_expect_failure 'refs are updated even if no commits need to be exported' ' + git fast-export master..master > actual && + test_cmp expected actual +' + test_done From 1bff9a634111c68083e284bac97f5e2e99089bd4 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 23 Jul 2011 15:55:26 +0200 Subject: [PATCH 031/406] fast-export: do not refer to non-existing marks When calling `git fast-export a..a b` when a and b refer to the same commit, nothing would be exported, and an incorrect reset line would be printed for b ('from :0'). Signed-off-by: Johannes Schindelin Signed-off-by: Sverre Rabbelier --- builtin/fast-export.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 9bd8a14b57b8e1..d10c9686f1238c 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -874,9 +874,20 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info) } } +static void handle_reset(const char *name, struct object *object) +{ + int mark = get_object_mark(object); + + if (mark) + printf("reset %s\nfrom :%d\n\n", name, + get_object_mark(object)); + else + printf("reset %s\nfrom %s\n\n", name, + oid_to_hex(&object->oid)); +} + static void handle_tags_and_duplicates(void) { - struct commit *commit; int i; for (i = extra_refs.nr - 1; i >= 0; i--) { @@ -890,9 +901,7 @@ static void handle_tags_and_duplicates(void) if (anonymize) name = anonymize_refname(name); /* create refs pointing to already seen commits */ - commit = (struct commit *)object; - printf("reset %s\nfrom :%d\n\n", name, - get_object_mark(&commit->object)); + handle_reset(name, object); show_progress(); break; } From f56cc0a2babc787f1af652a5d0c2b8ae1d407e29 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Sat, 28 Aug 2010 20:49:01 -0500 Subject: [PATCH 032/406] transport-helper: add trailing -- [PT: ensure we add an additional element to the argv array] --- transport-helper.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/transport-helper.c b/transport-helper.c index 143ca008c8f203..5b66c457ad8cc7 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -460,6 +460,8 @@ static int get_exporter(struct transport *transport, for (i = 0; i < revlist_args->nr; i++) argv_array_push(&fastexport->args, revlist_args->items[i].string); + argv_array_push(&fastexport->args, "--"); + fastexport->git_cmd = 1; return start_command(fastexport); } From 0e56a551727f0185ae5e764939d47cf5c120dc33 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Sun, 24 Jul 2011 00:06:00 +0200 Subject: [PATCH 033/406] remote-helper: check helper status after import/export Signed-off-by: Johannes Schindelin Signed-off-by: Sverre Rabbelier --- builtin/clone.c | 4 +++- t/t5801-remote-helpers.sh | 2 +- transport-helper.c | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index fd2c3ef0901460..2c7a1d973a85bc 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -1166,7 +1166,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix) } if (!is_local && !complete_refs_before_fetch) - transport_fetch_refs(transport, mapped_refs); + if (transport_fetch_refs(transport, mapped_refs)) + die(_("could not fetch refs from %s"), + transport->url); remote_head = find_ref_by_name(refs, "HEAD"); remote_head_points_at = diff --git a/t/t5801-remote-helpers.sh b/t/t5801-remote-helpers.sh index aaaa722ccab016..18cf138343523c 100755 --- a/t/t5801-remote-helpers.sh +++ b/t/t5801-remote-helpers.sh @@ -228,7 +228,7 @@ test_expect_success 'push update refs failure' ' echo "update fail" >>file && git commit -a -m "update fail" && git rev-parse --verify testgit/origin/heads/update >expect && - test_expect_code 1 env GIT_REMOTE_TESTGIT_FAILURE="non-fast forward" \ + test_must_fail env GIT_REMOTE_TESTGIT_FAILURE="non-fast forward" \ git push origin update && git rev-parse --verify testgit/origin/heads/update >actual && test_cmp expect actual diff --git a/transport-helper.c b/transport-helper.c index 5b66c457ad8cc7..b4f459e772ed1f 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -466,6 +466,19 @@ static int get_exporter(struct transport *transport, return start_command(fastexport); } +static void check_helper_status(struct helper_data *data) +{ + int pid, status; + + pid = waitpid(data->helper->pid, &status, WNOHANG); + if (pid < 0) + die("Could not retrieve status of remote helper '%s'", + data->name); + if (pid > 0 && WIFEXITED(status)) + die("Remote helper '%s' died with %d", + data->name, WEXITSTATUS(status)); +} + static int fetch_with_import(struct transport *transport, int nr_heads, struct ref **to_fetch) { @@ -502,6 +515,7 @@ static int fetch_with_import(struct transport *transport, if (finish_command(&fastimport)) die(_("error while running fast-import")); + check_helper_status(data); /* * The fast-import stream of a remote helper that advertises @@ -989,6 +1003,7 @@ static int push_refs_with_export(struct transport *transport, if (finish_command(&exporter)) die(_("error while running fast-export")); + check_helper_status(data); if (push_update_refs_status(data, remote_refs, flags)) return 1; From 9e61fd43aa847306ba71b533e2ab46c9b696849f Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Tue, 5 Jun 2012 10:24:11 -0400 Subject: [PATCH 034/406] Fix launching of externals from Unicode paths If Git were installed in a path containing non-ASCII characters, commands such as git-am and git-submodule, which are implemented as externals, would fail to launch with the following error: > fatal: 'am' appears to be a git command, but we were not > able to execute it. Maybe git-am is broken? This was due to lookup_prog not being Unicode-aware. It was somehow missed in 2ee5a1a14ad17ff35f0ad52390a27fbbc41258f3. Note that the only problem in this function was calling GetFileAttributes instead of GetFileAttributesW. The calls to access() were fine because access() is a macro which resolves to mingw_access, which already handles Unicode correctly. But I changed lookup_prog to use _waccess directly so that we only convert the path to UTF-16 once. Signed-off-by: Adam Roben --- compat/mingw.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..cc282be4d8e8f5 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -998,14 +998,20 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd, int isexe, int exe_only) { char path[MAX_PATH]; + wchar_t wpath[MAX_PATH]; snprintf(path, sizeof(path), "%.*s\\%s.exe", dirlen, dir, cmd); - if (!isexe && access(path, F_OK) == 0) + if (xutftowcs_path(wpath, path) < 0) + return NULL; + + if (!isexe && _waccess(wpath, F_OK) == 0) return xstrdup(path); path[strlen(path)-4] = '\0'; - if ((!exe_only || isexe) && access(path, F_OK) == 0) - if (!(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY)) + if ((!exe_only || isexe) && _waccess(wpath, F_OK) == 0) { + + if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) return xstrdup(path); + } return NULL; } From d9209bb4068521ddfd233f98d6c0f7f1aab2e759 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 12 Feb 2018 17:23:42 +0100 Subject: [PATCH 035/406] add --edit: truncate the patch file If there is already a .git/ADD_EDIT.patch file, we fail to truncate it properly, which could result in very funny errors. Let's just truncate it and not worry about it too much. Reported by J Wyman. Signed-off-by: Johannes Schindelin --- builtin/add.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/add.c b/builtin/add.c index 9916498a29bbd8..72baa72bc44b2c 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -239,7 +239,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix) rev.diffopt.output_format = DIFF_FORMAT_PATCH; rev.diffopt.use_color = 0; rev.diffopt.flags.ignore_dirty_submodules = 1; - out = open(file, O_CREAT | O_WRONLY, 0666); + out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (out < 0) die(_("Could not open '%s' for writing."), file); rev.diffopt.file = xfdopen(out, "w"); From 2c13b14359854be3311e23a5ebadb5e3aa52b4da Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 9 Apr 2012 13:04:35 -0500 Subject: [PATCH 036/406] Always auto-gc after calling a fast-import transport After importing anything with fast-import, we should always let the garbage collector do its job, since the objects are written to disk inefficiently. This brings down an initial import of http://selenic.com/hg from about 230 megabytes to about 14. In the future, we may want to make this configurable on a per-remote basis, or maybe teach fast-import about it in the first place. Signed-off-by: Johannes Schindelin --- transport-helper.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/transport-helper.c b/transport-helper.c index b4f459e772ed1f..7c15c41b58432d 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -16,6 +16,8 @@ #include "protocol.h" static int debug; +/* TODO: put somewhere sensible, e.g. git_transport_options? */ +static int auto_gc = 1; struct helper_data { const char *name; @@ -549,6 +551,12 @@ static int fetch_with_import(struct transport *transport, } } strbuf_release(&buf); + if (auto_gc) { + const char *argv_gc_auto[] = { + "gc", "--auto", "--quiet", NULL, + }; + run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); + } return 0; } From 6b11ffece9583727a51078cb456115b61e610ed8 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Tue, 5 Jun 2012 15:40:33 -0400 Subject: [PATCH 037/406] Make non-.exe externals work again 7ebac8cb94f3a06d3fbdde469414a1443ca45510 made launching of .exe externals work when installed in Unicode paths. But it broke launching of non-.exe externals, no matter where they were installed. We now correctly maintain the UTF-8 and UTF-16 paths in tandem in lookup_prog. This fixes t5526, among others. Signed-off-by: Adam Roben --- compat/mingw.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index cc282be4d8e8f5..a4ee9554efc679 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1006,11 +1006,12 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd, if (!isexe && _waccess(wpath, F_OK) == 0) return xstrdup(path); - path[strlen(path)-4] = '\0'; + wpath[wcslen(wpath)-4] = '\0'; if ((!exe_only || isexe) && _waccess(wpath, F_OK) == 0) { - - if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) + if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) { + path[strlen(path)-4] = '\0'; return xstrdup(path); + } } return NULL; } From 74d51985365f6ee5c18358aabe6d0c0cc200aeae Mon Sep 17 00:00:00 2001 From: Thomas Braun Date: Thu, 8 May 2014 21:43:24 +0200 Subject: [PATCH 038/406] Config option to disable side-band-64k for transport Since commit 0c499ea60f the send-pack builtin uses the side-band-64k capability if advertised by the server. Unfortunately this breaks pushing over the dump git protocol if used over a network connection. The detailed reasons for this breakage are (by courtesy of Jeff Preshing, quoted from ttps://groups.google.com/d/msg/msysgit/at8D7J-h7mw/eaLujILGUWoJ): ---------------------------------------------------------------------------- MinGW wraps Windows sockets in CRT file descriptors in order to mimic the functionality of POSIX sockets. This causes msvcrt.dll to treat sockets as Installable File System (IFS) handles, calling ReadFile, WriteFile, DuplicateHandle and CloseHandle on them. This approach works well in simple cases on recent versions of Windows, but does not support all usage patterns. In particular, using this approach, any attempt to read & write concurrently on the same socket (from one or more processes) will deadlock in a scenario where the read waits for a response from the server which is only invoked after the write. This is what send_pack currently attempts to do in the use_sideband codepath. ---------------------------------------------------------------------------- The new config option "sendpack.sideband" allows to override the side-band-64k capability of the server, and thus makes the dump git protocol work. Other transportation methods like ssh and http/https still benefit from the sideband channel, therefore the default value of "sendpack.sideband" is still true. Signed-off-by: Thomas Braun --- Documentation/config.txt | 6 ++++++ send-pack.c | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index eb66a119753726..ae611f38e2ffd9 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -3754,3 +3754,9 @@ worktree.guessRemote:: such a branch exists, it is checked out and set as "upstream" for the new branch. If no such match can be found, it falls back to creating a new branch from the current HEAD. + +sendpack.sideband:: + Allows to disable the side-band-64k capability for send-pack even + when it is advertised by the server. Makes it possible to work + around a limitation in the git for windows implementation together + with the dump git protocol. Defaults to true. diff --git a/send-pack.c b/send-pack.c index e920ca57df4dd0..bf544992573fb2 100644 --- a/send-pack.c +++ b/send-pack.c @@ -38,6 +38,16 @@ int option_parse_push_signed(const struct option *opt, die("bad %s argument: %s", opt->long_name, arg); } +static int config_use_sideband = 1; + +static int send_pack_config(const char *var, const char *value, void *unused) +{ + if (!strcmp("sendpack.sideband", var)) + config_use_sideband = git_config_bool(var, value); + + return 0; +} + static void feed_object(const struct object_id *oid, FILE *fh, int negative) { if (negative && !has_sha1_file(oid->hash)) @@ -392,6 +402,8 @@ int send_pack(struct send_pack_args *args, struct async demux; const char *push_cert_nonce = NULL; + git_config(send_pack_config, NULL); + /* Does the other end support the reporting? */ if (server_supports("report-status")) status_report = 1; @@ -399,7 +411,7 @@ int send_pack(struct send_pack_args *args, allow_deleting_refs = 1; if (server_supports("ofs-delta")) args->use_ofs_delta = 1; - if (server_supports("side-band-64k")) + if (config_use_sideband && server_supports("side-band-64k")) use_sideband = 1; if (server_supports("quiet")) quiet_supported = 1; From ae7985a3cb91500845b0935876c8c5c664678d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20D=C3=B6nmez?= Date: Sat, 16 Jan 2016 18:59:31 +0200 Subject: [PATCH 039/406] Don't let ld strip relocations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the first step for enabling ASLR (Address Space Layout Randomization) support. We want to enable ASLR for better protection against exploiting security holes in Git. The problem fixed by this commit is that `ld.exe` seems to be stripping relocations which in turn will break ASLR support. We just make sure it's not stripping the main executable entry. Signed-off-by: İsmail Dönmez Signed-off-by: Johannes Schindelin --- config.mak.uname | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.mak.uname b/config.mak.uname index e47af72e018fc3..a4b4eb6c846102 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -562,9 +562,11 @@ else prefix = /usr/ ifeq (MINGW32,$(MSYSTEM)) prefix = /mingw32 + BASIC_LDFLAGS += -Wl,--pic-executable,-e,_mainCRTStartup endif ifeq (MINGW64,$(MSYSTEM)) prefix = /mingw64 + BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup else COMPAT_CFLAGS += -D_USE_32BIT_TIME_T BASIC_LDFLAGS += -Wl,--large-address-aware From 0858bd23959ae5211457cfa48df61c2a17a6fe03 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 18 Sep 2015 16:57:33 +0200 Subject: [PATCH 040/406] mingw: ensure `getcwd()` reports the correct case When switching the current working directory, say, in PowerShell, it is quite possible to use a different capitalization than the one that is recorded on disk. While doing the same in `cmd.exe` adjusts the capitalization magically, that does not happen in PowerShell so that `getcwd()` returns the current directory in a different way than is recorded on disk. Typically this creates no problems except when you call git log . in a subdirectory called, say, "GIT/" but you switched to "Git/" and your `getcwd()` reports the latter, then Git won't understand that you wanted to see the history as per the `GIT/` subdirectory but it thinks you wanted to see the history of some directory that may have existed in the past (but actually never did). So let's be extra careful to adjust the capitalization of the current directory before working with it. Reported by a few PowerShell power users ;-) Signed-off-by: Johannes Schindelin --- compat/mingw.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..8e6bf79a1a93a5 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -887,8 +887,15 @@ struct tm *localtime_r(const time_t *timep, struct tm *result) char *mingw_getcwd(char *pointer, int len) { - wchar_t wpointer[MAX_PATH]; - if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer))) + wchar_t cwd[MAX_PATH], wpointer[MAX_PATH]; + DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd); + + if (!ret || ret >= ARRAY_SIZE(cwd)) { + errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError()); + return NULL; + } + ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer)); + if (!ret || ret >= ARRAY_SIZE(wpointer)) return NULL; if (xwcstoutf(pointer, wpointer, len) < 0) return NULL; From a55a801a1b50057f1473c15edd3977cf06cf7214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20D=C3=B6nmez?= Date: Sat, 16 Jan 2016 19:09:34 +0200 Subject: [PATCH 041/406] Enable DEP and ASLR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable DEP (Data Execution Prevention) and ASLR (Address Space Layout Randomization) support. This applies to both 32bit and 64bit builds and makes it substantially harder to exploit security holes in Git by offering a much more unpredictable attack surface. ASLR interferes with GDB's ability to set breakpoints. A similar issue holds true when compiling with -O2 (in which case single-stepping is messed up because GDB cannot map the code back to the original source code properly). Therefore we simply enable ASLR only when an optimization flag is present in the CFLAGS, using it as an indicator that the developer does not want to debug in GDB anyway. Signed-off-by: İsmail Dönmez Signed-off-by: Johannes Schindelin --- config.mak.uname | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config.mak.uname b/config.mak.uname index a4b4eb6c846102..3c00bd913303c5 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -560,6 +560,12 @@ else ifeq ($(shell expr "$(uname_R)" : '2\.'),2) # MSys2 prefix = /usr/ + # Enable DEP + BASIC_LDFLAGS += -Wl,--nxcompat + # Enable ASLR (unless debugging) + ifneq (,$(findstring -O,$(CFLAGS))) + BASIC_LDFLAGS += -Wl,--dynamicbase + endif ifeq (MINGW32,$(MSYSTEM)) prefix = /mingw32 BASIC_LDFLAGS += -Wl,--pic-executable,-e,_mainCRTStartup From 593f444d679a2a264b824a210d90c17aeaedcb8a Mon Sep 17 00:00:00 2001 From: yaras Date: Tue, 1 Mar 2016 16:12:23 +0100 Subject: [PATCH 042/406] Do not mask the username when reading credentials When user is asked for credentials there is no need to mask username, so PROMPT_ASKPASS flag on calling credential_ask_one for login is unnecessary. credential_ask_one internally uses git_prompt which in case of given flag PROMPT_ASKPASS uses masked input method instead of git_terminal_prompt, which does not mask user input. This fixes #675 Signed-off-by: yaras --- credential.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/credential.c b/credential.c index 62be651b03b55e..e9108a9e8a19c4 100644 --- a/credential.c +++ b/credential.c @@ -136,7 +136,9 @@ static void credential_getpass(struct credential *c) { if (!c->username) c->username = credential_ask_one("Username", c, - PROMPT_ASKPASS|PROMPT_ECHO); + (getenv("GIT_ASKPASS") ? + PROMPT_ASKPASS : 0) | + PROMPT_ECHO); if (!c->password) c->password = credential_ask_one("Password", c, PROMPT_ASKPASS); From a4109dfbe6fa4d64cad16e6c34cf071c23ce2e56 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 29 May 2017 13:59:31 +0200 Subject: [PATCH 043/406] setup_git_directory(): handle UNC paths correctly The first offset in a UNC path is not the host name, but the folder name after that. This fixes https://github.com/git-for-windows/git/issues/1181 Signed-off-by: Johannes Schindelin --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.c b/setup.c index b24c811c1c3129..0cd3c91c482d1c 100644 --- a/setup.c +++ b/setup.c @@ -892,7 +892,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; const char *gitdirenv; - int ceil_offset = -1, min_offset = has_dos_drive_prefix(dir->buf) ? 3 : 1; + int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); dev_t current_device = 0; int one_filesystem = 1; From 0dac7a8c6e419921fb9f219a9dbd2bf6bef322f3 Mon Sep 17 00:00:00 2001 From: Steve Hoelzer Date: Mon, 19 Dec 2016 11:10:09 -0600 Subject: [PATCH 044/406] poll: Use GetTickCount64 to avoid wraparound issues From Visual Studio 2015 Code Analysis: Warning C28159 Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days. Code that does not take that into account can loop indefinitely. GetTickCount64 operates on 64 bit values and does not have that problem. Signed-off-by: Steve Hoelzer --- compat/poll/poll.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compat/poll/poll.c b/compat/poll/poll.c index 7ed3fbbea13c99..8ccaeabcf725b4 100644 --- a/compat/poll/poll.c +++ b/compat/poll/poll.c @@ -449,7 +449,8 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout) static HANDLE hEvent; WSANETWORKEVENTS ev; HANDLE h, handle_array[FD_SETSIZE + 2]; - DWORD ret, wait_timeout, nhandles, start = 0, elapsed, orig_timeout = 0; + DWORD ret, wait_timeout, nhandles, elapsed, orig_timeout = 0; + ULONGLONG start = 0; fd_set rfds, wfds, xfds; BOOL poll_again; MSG msg; @@ -465,7 +466,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout) if (timeout != INFTIM) { orig_timeout = timeout; - start = GetTickCount(); + start = GetTickCount64(); } if (!hEvent) @@ -614,7 +615,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout) if (!rc && orig_timeout && timeout != INFTIM) { - elapsed = GetTickCount() - start; + elapsed = (DWORD)(GetTickCount64() - start); timeout = elapsed >= orig_timeout ? 0 : orig_timeout - elapsed; } From 80158c21a944abce25b824b54e2f63d9482adc6b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 13 Oct 2017 17:32:32 +0200 Subject: [PATCH 045/406] Fix .git/ discovery at the root of UNC shares A very common assumption in Git's source code base is that offset_1st_component() returns either 0 for relative paths, or 1 for absolute paths that start with a slash. In other words, the return value is either 0 or points just after the dir separator. This assumption is not fulfilled when calling offset_1st_component() e.g. on UNC paths on Windows, e.g. "//my-server/my-share". In this case, offset_1st_component() returns the length of the entire string (which is correct, because stripping the last "component" would not result in a valid directory), yet the return value still does not point just after a dir separator. This assumption is most prominently seen in the setup_git_directory_gently_1() function, where we want to append a ".git" component and simply assume that there is already a dir separator. In the UNC example given above, this assumption is incorrect. As a consequence, Git will fail to handle a worktree at the top of a UNC share correctly. Let's fix this by adding a dir separator specifically for that case: we found that there is no first component in the path and it does not end in a dir separator? Then add it. This fixes https://github.com/git-for-windows/git/issues/1320 Signed-off-by: Johannes Schindelin --- setup.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/setup.c b/setup.c index 0cd3c91c482d1c..c338697c9d97ae 100644 --- a/setup.c +++ b/setup.c @@ -920,6 +920,12 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, if (ceil_offset < 0) ceil_offset = min_offset - 2; + if (min_offset && min_offset == dir->len && + !is_dir_sep(dir->buf[min_offset - 1])) { + strbuf_addch(dir, '/'); + min_offset++; + } + /* * Test in the following order (relative to the dir): * - .git (file containing "gitdir: ") From c5c6163821114d17a3d8f94487fea637c7d7016c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 20 Dec 2016 17:18:05 +0100 Subject: [PATCH 046/406] poll: lazy-load GetTickCount64() This fixes the compilation, actually, as we still did not make the jump to post-Windows XP completely: we still compile with _WIN32_WINNT set to 0x0502 (which corresponds to Windows Server 2003 and is technically greater than Windows XP's 0x0501). However, GetTickCount64() is only available starting with Windows Vista/Windows Server 2008. Let's just lazy-load the function, which should also help Git for Windows contributors who want to reinstate Windows XP support. Signed-off-by: Johannes Schindelin --- compat/poll/poll.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compat/poll/poll.c b/compat/poll/poll.c index 8ccaeabcf725b4..88f5c19c55c8e7 100644 --- a/compat/poll/poll.c +++ b/compat/poll/poll.c @@ -266,6 +266,20 @@ win32_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents) return happened; } +#include +#include "compat/win32/lazyload.h" + +static ULONGLONG CompatGetTickCount64(void) +{ + DECLARE_PROC_ADDR(kernel32.dll, ULONGLONG, GetTickCount64, void); + + if (!INIT_PROC_ADDR(GetTickCount64)) + return (ULONGLONG)GetTickCount(); + + return GetTickCount64(); +} +#define GetTickCount64 CompatGetTickCount64 + #else /* !MinGW */ /* Convert select(2) returned fd_sets into poll(2) revents values. */ From 62986fe99bb6af6defbf987c7f86069ab1274985 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 11 Jan 2017 21:42:45 +0100 Subject: [PATCH 047/406] t5580: verify that alternates can be UNC paths On Windows, UNC paths are a very convenient way to share data, and alternates are all about sharing data. We fixed a bug where alternates specifying UNC paths were not handled properly, and it is high time that we add a regression test to ensure that this bug is not reintroduced. Signed-off-by: Johannes Schindelin --- t/t5580-clone-push-unc.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/t5580-clone-push-unc.sh b/t/t5580-clone-push-unc.sh index ba548df4a91824..265cd50526afe3 100755 --- a/t/t5580-clone-push-unc.sh +++ b/t/t5580-clone-push-unc.sh @@ -57,4 +57,16 @@ test_expect_success MINGW 'remote nick cannot contain backslashes' ' test_i18ngrep ! "unable to access" err ' +test_expect_success 'unc alternates' ' + tree="$(git rev-parse HEAD:)" && + mkdir test-unc-alternate && + ( + cd test-unc-alternate && + git init && + test_must_fail git show $tree && + echo "$UNCPATH/.git/objects" >.git/objects/info/alternates && + git show $tree + ) +' + test_done From 46d1fcef860852c2eeeb4fbffe0385bd237ab5c3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 2 Mar 2018 22:40:19 +0100 Subject: [PATCH 048/406] setup_git_directory(): handle UNC root paths correctly When working in the root directory of a file share (this is only possible in Git Bash and Powershell, but not in CMD), the current directory is reported without a trailing slash. This is different from Unix and standard Windows directories: both / and C:\ are reported with a trailing slash as current directories. If a Git worktree is located there, Git is not quite prepared for that: while it does manage to find the .git directory/file, it returns as length of the top-level directory's path *one more* than the length of the current directory, and setup_git_directory_gently() would then return an undefined string as prefix. In practice, this undefined string usually points to NUL bytes, and does not cause much harm. Under rare circumstances that are really involved to reproduce (and not reliably so), the reported prefix could be a suffix string of Git's exec path, though. A careful analysis determined that this bug is unlikely to be exploitable, therefore we mark this as a regular bug fix. Signed-off-by: Johannes Schindelin --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.c b/setup.c index c338697c9d97ae..19a3497d399e76 100644 --- a/setup.c +++ b/setup.c @@ -760,7 +760,7 @@ static const char *setup_discovered_git_dir(const char *gitdir, set_git_dir(gitdir); inside_git_dir = 0; inside_work_tree = 1; - if (offset == cwd->len) + if (offset >= cwd->len) return NULL; /* Make "offset" point past the '/' (already the case for root dirs) */ From ca11feb7ac2b0a0e74ff6a5025d6426f4a4a93bd Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Thu, 15 Dec 2016 11:34:39 -0500 Subject: [PATCH 049/406] diffcore-rename: speed up register_rename_src Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point. This is a performance optimization. This routine is called during run_diff_files in status and the caller is iterating over the sorted index, so we should expect to be able to append in the normal case. The existing insert logic is preserved so we don't have to assume that, but simply take advantage of it if possible. Signed-off-by: Jeff Hostetler --- diffcore-rename.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/diffcore-rename.c b/diffcore-rename.c index d775183c2fd1be..f55287d233a72d 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -82,6 +82,18 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p) first = 0; last = rename_src_nr; + + if (last > 0) { + struct diff_rename_src *src = &(rename_src[last-1]); + int cmp = strcmp(one->path, src->p->one->path); + if (!cmp) + return src; + if (cmp > 0) { + first = last; + goto append_it; + } + } + while (last > first) { int next = (last + first) >> 1; struct diff_rename_src *src = &(rename_src[next]); @@ -95,6 +107,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p) first = next+1; } +append_it: /* insert to make it at "first" */ ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc); rename_src_nr++; From ab544bb047acfe74e9160e520830d8a958f08629 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 6 Apr 2017 00:56:44 +0200 Subject: [PATCH 050/406] Move Windows-specific config settings into compat/mingw.c Signed-off-by: Johannes Schindelin --- setup.c | 6 +++--- t/t3700-add.sh | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/setup.c b/setup.c index b24c811c1c3129..caaea474e0ed33 100644 --- a/setup.c +++ b/setup.c @@ -39,7 +39,7 @@ static int abspath_part_inside_repo(char *path) off = offset_1st_component(path); /* check if work tree is already the prefix */ - if (wtlen <= len && !strncmp(path, work_tree, wtlen)) { + if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) { if (path[wtlen] == '/') { memmove(path, path + wtlen + 1, len - wtlen); return 0; @@ -59,7 +59,7 @@ static int abspath_part_inside_repo(char *path) path++; if (*path == '/') { *path = '\0'; - if (strcmp(real_path(path0), work_tree) == 0) { + if (fspathcmp(real_path(path0), work_tree) == 0) { memmove(path0, path + 1, len - (path - path0)); return 0; } @@ -68,7 +68,7 @@ static int abspath_part_inside_repo(char *path) } /* check whole path */ - if (strcmp(real_path(path0), work_tree) == 0) { + if (fspathcmp(real_path(path0), work_tree) == 0) { *path0 = '\0'; return 0; } diff --git a/t/t3700-add.sh b/t/t3700-add.sh index 37729ba2582119..8ee4fc70ad3453 100755 --- a/t/t3700-add.sh +++ b/t/t3700-add.sh @@ -402,4 +402,11 @@ test_expect_success 'all statuses changed in folder if . is given' ' test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0 ' +test_expect_success MINGW 'path is case-insensitive' ' + path="$(pwd)/BLUB" && + touch "$path" && + downcased="$(echo "$path" | tr A-Z a-z)" && + git add "$downcased" +' + test_done From 9658d7d08df439ec2691c52e4574c3200fb6d9b9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 18 Apr 2017 12:09:08 +0200 Subject: [PATCH 051/406] mingw: demonstrate a problem with certain absolute paths On Windows, there are several categories of absolute paths. One such category starts with a backslash and is implicitly relative to the drive associated with the current working directory. Example: c: git clone https://github.com/git-for-windows/git \G4W should clone into C:\G4W. There is currently a problem with that, in that mingw_mktemp() does not expect the _wmktemp() function to prefix the absolute path with the drive prefix, and as a consequence, the resulting path does not fit into the originally-passed string buffer. The symptom is a "Result too large" error. Reported by Juan Carlos Arevalo Baeza. Signed-off-by: Johannes Schindelin --- t/t5580-clone-push-unc.sh | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/t/t5580-clone-push-unc.sh b/t/t5580-clone-push-unc.sh index ba548df4a91824..c2b00822962b5b 100755 --- a/t/t5580-clone-push-unc.sh +++ b/t/t5580-clone-push-unc.sh @@ -17,14 +17,11 @@ fi UNCPATH="$(winpwd)" case "$UNCPATH" in [A-Z]:*) + WITHOUTDRIVE="${UNCPATH#?:}" # Use administrative share e.g. \\localhost\C$\git-sdk-64\usr\src\git # (we use forward slashes here because MSYS2 and Git accept them, and # they are easier on the eyes) - UNCPATH="//localhost/${UNCPATH%%:*}\$/${UNCPATH#?:}" - test -d "$UNCPATH" || { - skip_all='could not access administrative share; skipping' - test_done - } + UNCPATH="//localhost/${UNCPATH%%:*}\$$WITHOUTDRIVE" ;; *) skip_all='skipping UNC path tests, cannot determine current path as UNC' @@ -32,6 +29,18 @@ case "$UNCPATH" in ;; esac +test_expect_failure 'clone into absolute path lacking a drive prefix' ' + USINGBACKSLASHES="$(echo "$WITHOUTDRIVE"/without-drive-prefix | + tr / \\\\)" && + git clone . "$USINGBACKSLASHES" && + test -f without-drive-prefix/.git/HEAD +' + +test -d "$UNCPATH" || { + skip_all='could not access administrative share; skipping' + test_done +} + test_expect_success setup ' test_commit initial ' From 8df7701e20d5ec6b39a8cf17fc288207c41358d2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 18 Apr 2017 12:38:30 +0200 Subject: [PATCH 052/406] mingw: allow absolute paths without drive prefix When specifying an absolute path without a drive prefix, we convert that path internally. Let's make sure that we handle that case properly, too ;-) This fixes the command git clone https://github.com/git-for-windows/git \G4W Signed-off-by: Johannes Schindelin --- compat/mingw.c | 10 +++++++++- t/t5580-clone-push-unc.sh | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..eceadc171f2a44 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -818,11 +818,19 @@ unsigned int sleep (unsigned int seconds) char *mingw_mktemp(char *template) { wchar_t wtemplate[MAX_PATH]; + int offset = 0; + if (xutftowcs_path(wtemplate, template) < 0) return NULL; + + if (is_dir_sep(template[0]) && !is_dir_sep(template[1]) && + iswalpha(wtemplate[0]) && wtemplate[1] == L':') { + /* We have an absolute path missing the drive prefix */ + offset = 2; + } if (!_wmktemp(wtemplate)) return NULL; - if (xwcstoutf(template, wtemplate, strlen(template) + 1) < 0) + if (xwcstoutf(template, wtemplate + offset, strlen(template) + 1) < 0) return NULL; return template; } diff --git a/t/t5580-clone-push-unc.sh b/t/t5580-clone-push-unc.sh index c2b00822962b5b..17c38c33a5043b 100755 --- a/t/t5580-clone-push-unc.sh +++ b/t/t5580-clone-push-unc.sh @@ -29,7 +29,7 @@ case "$UNCPATH" in ;; esac -test_expect_failure 'clone into absolute path lacking a drive prefix' ' +test_expect_success 'clone into absolute path lacking a drive prefix' ' USINGBACKSLASHES="$(echo "$WITHOUTDRIVE"/without-drive-prefix | tr / \\\\)" && git clone . "$USINGBACKSLASHES" && From ffd9cdf8b2ad2ad1cf0aea3a29774a481282a867 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 8 Jun 2016 08:32:21 +0200 Subject: [PATCH 053/406] gc/repack: release packs when needed On Windows, files cannot be removed nor renamed if there are still handles held by a process. To remedy that, we introduced the close_all_packs() function. Earlier, we made sure that the packs are released just before `git gc` is spawned, in case that gc wants to remove no-longer needed packs. But this developer forgot that gc itself also needs to let go of packs, e.g. when consolidating all packs via the --aggressive option. Likewise, `git repack -d` wants to delete obsolete packs and therefore needs to close all pack handles, too. Signed-off-by: Johannes Schindelin --- builtin/gc.c | 4 +++- builtin/repack.c | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/builtin/gc.c b/builtin/gc.c index 57069442b0dc12..8a99ed2af2ba34 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -642,8 +642,10 @@ int cmd_gc(int argc, const char **argv, const char *prefix) report_garbage = report_pack_garbage; reprepare_packed_git(the_repository); - if (pack_garbage.nr > 0) + if (pack_garbage.nr > 0) { + close_all_packs(the_repository->objects); clean_pack_garbage(); + } if (gc_write_commit_graph) write_commit_graph_reachable(get_object_directory(), 0); diff --git a/builtin/repack.c b/builtin/repack.c index d5886039cc6656..5fdd19c9c4f95a 100644 --- a/builtin/repack.c +++ b/builtin/repack.c @@ -408,6 +408,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix) if (!names.nr && !po_args.quiet) printf("Nothing new to pack.\n"); + close_all_packs(the_repository->objects); + /* * Ok we have prepared all new packfiles. * First see if there are packs of the same name and if so From c4c89e5d7657d9c85d34a3911dd6a285e7b2e476 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 13 Jul 2017 22:55:36 +0200 Subject: [PATCH 054/406] config: report a bug if git_dir exists without commondir This did happen at some stage, and was fixed relatively quickly. Make sure that we detect very quickly, too, should that happen again. Signed-off-by: Johannes Schindelin --- config.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.c b/config.c index 3461993f0af665..efe67560b39873 100644 --- a/config.c +++ b/config.c @@ -1676,6 +1676,8 @@ static int do_git_config_sequence(const struct config_options *opts, if (opts->commondir) repo_config = mkpathdup("%s/config", opts->commondir); + else if (opts->git_dir) + BUG("git_dir without commondir"); else repo_config = NULL; From 245f9260032b90e31d69378e19d26b6d7d36e4f3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 2 Aug 2017 22:10:06 +0200 Subject: [PATCH 055/406] http: add support for selecting SSL backends at runtime As of version 7.56.0, curl supports being compiled with multiple SSL backends. This patch adds the Git side of that feature: by setting http.sslBackend to "openssl" or "schannel", Git for Windows can now choose the SSL backend at runtime. This comes in handy on Windows because Secure Channel ("schannel") is the native solution, accessing the Windows Credential Store, thereby allowing for enterprise-wide management of certificates. For historical reasons, Git for Windows needs to support OpenSSL still, as it has previously been the only supported SSL backend in Git for Windows for almost a decade. The patch has been carried in Git for Windows for over a year, and is considered mature. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 5 +++++ http.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Documentation/config.txt b/Documentation/config.txt index eb66a119753726..c569e728593027 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2236,6 +2236,11 @@ http.sslCAPath:: with when fetching or pushing over HTTPS. Can be overridden by the `GIT_SSL_CAPATH` environment variable. +http.sslBackend:: + Name of the SSL backend to use (e.g. "openssl" or "schannel"). + This option is ignored if cURL lacks support for choosing the SSL + backend at runtime. + http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index 4162860ee31636..fedfb2a2070020 100644 --- a/http.c +++ b/http.c @@ -155,6 +155,8 @@ static struct active_request_slot *active_queue_head; static char *cached_accept_language; +static char *http_ssl_backend; + size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { size_t size = eltsize * nmemb; @@ -302,6 +304,12 @@ static int http_options(const char *var, const char *value, void *cb) curl_ssl_try = git_config_bool(var, value); return 0; } + if (!strcmp("http.sslbackend", var)) { + free(http_ssl_backend); + http_ssl_backend = xstrdup_or_null(value); + return 0; + } + if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -995,6 +1003,33 @@ void http_init(struct remote *remote, const char *url, int proactive_auth) git_config(urlmatch_config_entry, &config); free(normalized_url); +#if LIBCURL_VERSION_NUM >= 0x073800 + if (http_ssl_backend) { + const curl_ssl_backend **backends; + struct strbuf buf = STRBUF_INIT; + int i; + + switch (curl_global_sslset(-1, http_ssl_backend, &backends)) { + case CURLSSLSET_UNKNOWN_BACKEND: + strbuf_addf(&buf, _("Unsupported SSL backend '%s'. " + "Supported SSL backends:"), + http_ssl_backend); + for (i = 0; backends[i]; i++) + strbuf_addf(&buf, "\n\t%s", backends[i]->name); + die("%s", buf.buf); + case CURLSSLSET_NO_BACKENDS: + die(_("Could not set SSL backend to '%s': " + "cURL was built without SSL backends"), + http_ssl_backend); + case CURLSSLSET_TOO_LATE: + die(_("Could not set SSL backend to '%s': already set"), + http_ssl_backend); + case CURLSSLSET_OK: + break; /* Okay! */ + } + } +#endif + if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) die("curl_global_init failed"); From 42d8cd3e71540088302d80dcdb03b3932a7535e5 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Tue, 23 Jan 2018 13:20:55 +1100 Subject: [PATCH 056/406] http: add support for disabling SSL revocation checks in cURL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds support for a new http.schannelCheckRevoke config value. This config value is only used if http.sslBackend is set to "schannel", which forces cURL to use the Windows Certificate Store when validating server certificates associated with a remote server. This config value should only be set to "false" if you are in an environment where revocation checks are blocked by the network, with no alternative options. This is only supported in cURL 7.44 or later. Note: originally, we wanted to call the config setting `http.schannel.checkRevoke`. This, however, does not work: the `http.*` config settings can be limited to specific URLs via `http..*` (and this feature would mistake `schannel` for a URL). Helped by Agustín Martín Barbero. Signed-off-by: Brendan Forster Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 8 ++++++++ http.c | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Documentation/config.txt b/Documentation/config.txt index c569e728593027..e107f4c1e24370 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2241,6 +2241,14 @@ http.sslBackend:: This option is ignored if cURL lacks support for choosing the SSL backend at runtime. +http.schannelCheckRevoke:: + Used to enforce or disable certificate revocation checks in cURL + when http.sslBackend is set to "schannel". Defaults to `true` if + unset. Only necessary to disable this if Git consistently errors + and the message is about checking the revocation status of a + certificate. This option is ignored if cURL lacks support for + setting the relevant SSL option at runtime. + http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index fedfb2a2070020..272584b16e5f7d 100644 --- a/http.c +++ b/http.c @@ -157,6 +157,8 @@ static char *cached_accept_language; static char *http_ssl_backend; +static int http_schannel_check_revoke = 1; + size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { size_t size = eltsize * nmemb; @@ -310,6 +312,11 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } + if (!strcmp("http.schannelcheckrevoke", var)) { + http_schannel_check_revoke = git_config_bool(var, value); + return 0; + } + if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -811,6 +818,16 @@ static CURL *get_curl_handle(void) } #endif + if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) && + !http_schannel_check_revoke) { +#if LIBCURL_VERSION_NUM >= 0x072c00 + curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE); +#else + warning("CURLSSLOPT_NO_REVOKE not applied to curl SSL options because\n" + "your curl version is too old (< 7.44.0)"); +#endif + } + if (http_proactive_auth) init_curl_http_auth(result); From f5ab67ada02980fdfe2c8a99ecfde46e4fbf2ac0 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 14 Jun 2018 14:01:58 +0200 Subject: [PATCH 057/406] http: when using Secure Channel, ignore sslCAInfo by default As of cURL v7.60.0, the Secure Channel backend can use the certificate bundle provided via `http.sslCAInfo`, but that would override the Windows Certificate Store. Since this is not desirable by default, let's tell Git to not ask cURL to use that bundle by default when the `schannel` backend was configured via `http.sslBackend`, unless `http.schannelUseSSLCAInfo` overrides this behavior. Signed-off-by: Johannes Schindelin --- Documentation/config.txt | 8 ++++++++ http.c | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index e107f4c1e24370..56a110446915ea 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2249,6 +2249,14 @@ http.schannelCheckRevoke:: certificate. This option is ignored if cURL lacks support for setting the relevant SSL option at runtime. +http.schannelUseSSLCAInfo:: + As of cURL v7.60.0, the Secure Channel backend can use the + certificate bundle provided via `http.sslCAInfo`, but that would + override the Windows Certificate Store. Since this is not desirable + by default, Git will tell cURL not to use that bundle by default + when the `schannel` backend was configured via `http.sslBackend`, + unless `http.schannelUseSSLCAInfo` overrides this behavior. + http.pinnedpubkey:: Public key of the https service. It may either be the filename of a PEM or DER encoded public key file or a string starting with diff --git a/http.c b/http.c index 272584b16e5f7d..43e75ac583b428 100644 --- a/http.c +++ b/http.c @@ -158,6 +158,12 @@ static char *cached_accept_language; static char *http_ssl_backend; static int http_schannel_check_revoke = 1; +/* + * With the backend being set to `schannel`, setting sslCAinfo would override + * the Certificate Store in cURL v7.60.0 and later, which is not what we want + * by default. + */ +static int http_schannel_use_ssl_cainfo; size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { @@ -317,6 +323,11 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } + if (!strcmp("http.schannelusesslcainfo", var)) { + http_schannel_use_ssl_cainfo = git_config_bool(var, value); + return 0; + } + if (!strcmp("http.minsessions", var)) { min_curl_sessions = git_config_int(var, value); #ifndef USE_CURL_MULTI @@ -869,7 +880,13 @@ static CURL *get_curl_handle(void) if (ssl_pinnedkey != NULL) curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey); #endif - if (ssl_cainfo != NULL) + if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) && + !http_schannel_use_ssl_cainfo) { + curl_easy_setopt(result, CURLOPT_CAINFO, NULL); +#if LIBCURL_VERSION_NUM >= 0x073400 + curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL); +#endif + } else if (ssl_cainfo != NULL) curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { From 20d22f43e35c513f02451343e2d0d6f714acc44d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 25 Jan 2018 15:03:05 +0100 Subject: [PATCH 058/406] compat/poll: prepare for targeting Windows Vista Windows Vista (and later) actually have a working poll(), but we still cannot use it because it only works on sockets. So let's detect when we are targeting Windows Vista and undefine those constants, and define `pollfd` so that we can declare our own pollfd struct. We also need to make sure that we override those constants *after* `winsock2.h` has been `#include`d (otherwise we would not really override those constants). Signed-off-by: Johannes Schindelin --- compat/poll/poll.c | 6 +++--- compat/poll/poll.h | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/compat/poll/poll.c b/compat/poll/poll.c index 7ed3fbbea13c99..ad5dcde439d3db 100644 --- a/compat/poll/poll.c +++ b/compat/poll/poll.c @@ -29,9 +29,6 @@ #include -/* Specification. */ -#include - #include #include #include @@ -55,6 +52,9 @@ # include #endif +/* Specification. */ +#include "poll.h" + #ifdef HAVE_SYS_IOCTL_H # include #endif diff --git a/compat/poll/poll.h b/compat/poll/poll.h index cd1995292a536f..1e1597360f4485 100644 --- a/compat/poll/poll.h +++ b/compat/poll/poll.h @@ -21,6 +21,21 @@ #ifndef _GL_POLL_H #define _GL_POLL_H +#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600 +/* Vista has its own, socket-only poll() */ +#undef POLLIN +#undef POLLPRI +#undef POLLOUT +#undef POLLERR +#undef POLLHUP +#undef POLLNVAL +#undef POLLRDNORM +#undef POLLRDBAND +#undef POLLWRNORM +#undef POLLWRBAND +#define pollfd compat_pollfd +#endif + /* fake a poll(2) environment */ #define POLLIN 0x0001 /* any readable data available */ #define POLLPRI 0x0002 /* OOB/Urgent readable data */ From e71358acdf125d702f4ec55b1898f5e7d19d95a1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 25 Jan 2018 14:19:52 +0100 Subject: [PATCH 059/406] mingw: set _WIN32_WINNT explicitly for Git for Windows Previously, we only ever declared a target Windows version if compiling with Visual C. Which meant that we were relying on the MinGW headers to guess which Windows version we want to target... Let's be explicit about it, in particular because we actually want to bump the target Windows version to Vista (which we will do in the next commit). Signed-off-by: Johannes Schindelin --- git-compat-util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-compat-util.h b/git-compat-util.h index 5f2e90932f9990..3ba93d9c155e6e 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -146,7 +146,7 @@ #define _SGI_SOURCE 1 #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */ -# if defined (_MSC_VER) && !defined(_WIN32_WINNT) +# if !defined(_WIN32_WINNT) # define _WIN32_WINNT 0x0502 # endif #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */ From f75b7e139ff43f35338a9de76d07074a4d5eebca Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 25 Jan 2018 14:21:51 +0100 Subject: [PATCH 060/406] mingw: bump the minimum Windows version to Vista Quite some time ago, a last plea to the XP users out there who want to see Windows XP support in Git for Windows, asking them to get engaged and help, vanished into the depths of the universe. It is time to codify the ascent by the "silent majority" of XP users, and mark the minimum Windows version required for Git for Windows as Windows Vista. This, incidentally, lets us use quite a few nice new APIs. This also means that we no longer need the inet_pton() and inet_ntop() emulation, and we no longer need to do the PROC_ADDR dance with the `CreateSymbolicLinkW()` function, either. Signed-off-by: Johannes Schindelin --- config.mak.uname | 4 ---- git-compat-util.h | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/config.mak.uname b/config.mak.uname index e47af72e018fc3..8acdeb71fdab3b 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -381,8 +381,6 @@ ifeq ($(uname_S),Windows) NO_PYTHON = YesPlease BLK_SHA1 = YesPlease ETAGS_TARGET = ETAGS - NO_INET_PTON = YesPlease - NO_INET_NTOP = YesPlease NO_POSIX_GOODIES = UnfortunatelyYes NATIVE_CRLF = YesPlease DEFAULT_HELP_FORMAT = html @@ -529,8 +527,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_REGEX = YesPlease NO_PYTHON = YesPlease ETAGS_TARGET = ETAGS - NO_INET_PTON = YesPlease - NO_INET_NTOP = YesPlease NO_POSIX_GOODIES = UnfortunatelyYes DEFAULT_HELP_FORMAT = html COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32 diff --git a/git-compat-util.h b/git-compat-util.h index 3ba93d9c155e6e..48c955541e2000 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -147,7 +147,7 @@ #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */ # if !defined(_WIN32_WINNT) -# define _WIN32_WINNT 0x0502 +# define _WIN32_WINNT 0x0600 # endif #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */ #include From 8130c748068760291ba2fa5cbfc27889fb2a1440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20B=C3=B6gershausen?= Date: Tue, 15 Aug 2017 08:55:38 +0200 Subject: [PATCH 061/406] mingw: support UNC in git clone file://server/share/repo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend the parser to accept file://server/share/repo in the way that Windows users expect it to be parsed who are used to referring to file shares by UNC paths of the form \\server\share\folder. [jes: tightened check to avoid handling file://C:/some/path as a UNC path.] This closes https://github.com/git-for-windows/git/issues/1264. Signed-off-by: Torsten Bögershausen Signed-off-by: Johannes Schindelin --- connect.c | 4 ++++ t/t5500-fetch-pack.sh | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index 94547e5056b008..a45e0e3dc91668 100644 --- a/connect.c +++ b/connect.c @@ -918,6 +918,10 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host, if (protocol == PROTO_LOCAL) path = end; + else if (protocol == PROTO_FILE && *host != '/' && + !has_dos_drive_prefix(host) && + offset_1st_component(host - 2) > 1) + path = host - 2; /* include the leading "//" */ else if (protocol == PROTO_FILE && has_dos_drive_prefix(end)) path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */ else diff --git a/t/t5500-fetch-pack.sh b/t/t5500-fetch-pack.sh index 1b5a4a6d380f37..aea02121447695 100755 --- a/t/t5500-fetch-pack.sh +++ b/t/t5500-fetch-pack.sh @@ -695,13 +695,22 @@ do # file with scheme for p in file do - test_expect_success "fetch-pack --diag-url $p://$h/$r" ' + test_expect_success !MINGW "fetch-pack --diag-url $p://$h/$r" ' check_prot_path $p://$h/$r $p "/$r" ' + test_expect_success MINGW "fetch-pack --diag-url $p://$h/$r" ' + check_prot_path $p://$h/$r $p "//$h/$r" + ' + test_expect_success MINGW "fetch-pack --diag-url $p:///$r" ' + check_prot_path $p:///$r $p "/$r" + ' # No "/~" -> "~" conversion for file - test_expect_success "fetch-pack --diag-url $p://$h/~$r" ' + test_expect_success !MINGW "fetch-pack --diag-url $p://$h/~$r" ' check_prot_path $p://$h/~$r $p "/~$r" ' + test_expect_success MINGW "fetch-pack --diag-url $p://$h/~$r" ' + check_prot_path $p://$h/~$r $p "//$h/~$r" + ' done # file without scheme for h in nohost nohost:12 [::1] [::1]:23 [ [:aa From cabd85d8ec87326f5be5817933a68ee255243ae7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 10 Jan 2017 23:14:20 +0100 Subject: [PATCH 062/406] mingw: use CreateHardLink directly The function CreateHardLink is available in all supported Windows versions (since Windows XP), so there is no more need to resolve it in runtime. Helped-by: Max Kirillov Signed-off-by: Johannes Schindelin --- compat/mingw.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..4e61ed4277aff5 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1937,24 +1937,12 @@ int mingw_raise(int sig) int link(const char *oldpath, const char *newpath) { - typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES); - static T create_hard_link = NULL; wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH]; if (xutftowcs_path(woldpath, oldpath) < 0 || xutftowcs_path(wnewpath, newpath) < 0) return -1; - if (!create_hard_link) { - create_hard_link = (T) GetProcAddress( - GetModuleHandle("kernel32.dll"), "CreateHardLinkW"); - if (!create_hard_link) - create_hard_link = (T)-1; - } - if (create_hard_link == (T)-1) { - errno = ENOSYS; - return -1; - } - if (!create_hard_link(wnewpath, woldpath, NULL)) { + if (!CreateHardLinkW(wnewpath, woldpath, NULL)) { errno = err_win_to_posix(GetLastError()); return -1; } From a4bb03be1df89a7ea9c38238aeaf283db232aa20 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 27 Sep 2018 21:40:25 +0200 Subject: [PATCH 063/406] Fix typo 'detla' -> 'delta' Signed-off-by: Johannes Schindelin --- pack-objects.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pack-objects.h b/pack-objects.h index 62806ccc39ea31..c34036166a20af 100644 --- a/pack-objects.h +++ b/pack-objects.h @@ -356,7 +356,7 @@ static inline unsigned long oe_delta_size(struct packing_data *pack, return e->delta_size_; /* - * pack->detla_size[] can't be NULL because oe_set_delta_size() + * pack->delta_size[] can't be NULL because oe_set_delta_size() * must have been called when a new delta is saved with * oe_set_delta(). * If oe_delta() returns NULL (i.e. default state, which means From 296128b6914e93f7b9f703161fd64c28e0c4c521 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 27 Sep 2018 22:23:22 +0200 Subject: [PATCH 064/406] pack-objects (mingw): demonstrate a segmentation fault with large deltas There is a problem in the way 9ac3f0e5b3e4 (pack-objects: fix performance issues on packing large deltas, 2018-07-22) initializes that mutex in the `packing_data` struct. The problem manifests in a segmentation fault on Windows, when a mutex (AKA critical section) is accessed without being initialized. (With pthreads, you apparently do not really have to initialize them?) This was reported in https://github.com/git-for-windows/git/issues/1839. Signed-off-by: Johannes Schindelin --- t/t5319-pack-large-objects.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 t/t5319-pack-large-objects.sh diff --git a/t/t5319-pack-large-objects.sh b/t/t5319-pack-large-objects.sh new file mode 100755 index 00000000000000..c36c66fbb43a73 --- /dev/null +++ b/t/t5319-pack-large-objects.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# +# Copyright (c) 2018 Johannes Schindelin +# + +test_description='git pack-object with "large" deltas + +' +. ./test-lib.sh +. "$TEST_DIRECTORY"/lib-pack.sh + +# Two similar-ish objects that we have computed deltas between. +A=01d7713666f4de822776c7622c10f1b07de280dc +B=e68fe8129b546b101aee9510c5328e7f21ca1d18 + +test_expect_success 'setup' ' + clear_packs && + { + pack_header 2 && + pack_obj $A $B && + pack_obj $B + } >ab.pack && + pack_trailer ab.pack && + git index-pack --stdin Date: Thu, 27 Sep 2018 22:23:22 +0200 Subject: [PATCH 065/406] pack-objects (mingw): initialize `packing_data` mutex in the correct spot In 9ac3f0e5b3e4 (pack-objects: fix performance issues on packing large deltas, 2018-07-22), a mutex was introduced that is used to guard the call to set the delta size. This commit even added code to initialize it, but at an incorrect spot: in `init_threaded_search()`, while the call to `oe_set_delta_size()` (and hence to `packing_data_lock()`) can happen in the call chain `check_object()` <- `get_object_details()` <- `prepare_pack()` <- `cmd_pack_objects()`, which is long before the `prepare_pack()` function calls `ll_find_deltas()` (which initializes the threaded search). Another tell-tale that the mutex was initialized in an incorrect spot is that the function to initialize it lives in builtin/, while the code that uses the mutex is defined in a libgit.a header file. Let's use a more appropriate function: `prepare_packing_data()`, which not only lives in libgit.a, but *has* to be called before the `packing_data` struct is used that contains that mutex. This fixes https://github.com/git-for-windows/git/issues/1839. Signed-off-by: Johannes Schindelin --- builtin/pack-objects.c | 1 - pack-objects.c | 3 +++ t/t5319-pack-large-objects.sh | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index d1144a8f7ef79f..29d48f38671116 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -2299,7 +2299,6 @@ static void init_threaded_search(void) pthread_mutex_init(&cache_mutex, NULL); pthread_mutex_init(&progress_mutex, NULL); pthread_cond_init(&progress_cond, NULL); - pthread_mutex_init(&to_pack.lock, NULL); old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads); } diff --git a/pack-objects.c b/pack-objects.c index 6ef87e5683aacd..f73f609884ca0d 100644 --- a/pack-objects.c +++ b/pack-objects.c @@ -148,6 +148,9 @@ void prepare_packing_data(struct packing_data *pdata) 1U << OE_SIZE_BITS); pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE", 1UL << OE_DELTA_SIZE_BITS); +#ifndef NO_PTHREADS + pthread_mutex_init(&pdata->lock, NULL); +#endif } struct object_entry *packlist_alloc(struct packing_data *pdata, diff --git a/t/t5319-pack-large-objects.sh b/t/t5319-pack-large-objects.sh index c36c66fbb43a73..a75eab87d36136 100755 --- a/t/t5319-pack-large-objects.sh +++ b/t/t5319-pack-large-objects.sh @@ -24,7 +24,7 @@ test_expect_success 'setup' ' git index-pack --stdin Date: Fri, 21 Jul 2017 22:17:20 +0200 Subject: [PATCH 066/406] tests: fix GIT_TEST_INSTALLED's PATH to include t/helper/ We really need to be able to find the test helpers... Really. This change was forgotten when we moved the test helpers into t/helper/ Signed-off-by: Johannes Schindelin --- t/test-lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 44288cbb598435..713eb718e68501 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -926,7 +926,7 @@ elif test -n "$GIT_TEST_INSTALLED" then GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) || error "Cannot run git from $GIT_TEST_INSTALLED." - PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH + PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} else # normal case, use ../bin-wrappers only unless $with_dashes: git_bin_dir="$GIT_BUILD_DIR/bin-wrappers" From a806c6366c0b00a8bad167f15750f83e9ecfb954 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 20 Jul 2017 00:09:44 +0200 Subject: [PATCH 067/406] tests: respect GIT_TEST_INSTALLED when initializing repositories It really makes very, very little sense to use a different git executable than the one the caller indicated via setting the environment variable GIT_TEST_INSTALLED. Signed-off-by: Johannes Schindelin --- t/test-lib-functions.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 4207af40777c69..849b14a7cf7be1 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -898,7 +898,8 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git" init \ + "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?" mv .git/hooks .git/hooks-disabled ) || exit From f38cdc731a1d930b6c10c2209973c929b18c6dd8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 18 Jul 2017 23:32:36 +0200 Subject: [PATCH 068/406] t/lib-gettext: test installed git-sh-i18n if GIT_TEST_INSTALLED is set It makes very, very little sense to test the built git-sh-i18n when the user asked specifically to test another one. Signed-off-by: Johannes Schindelin --- t/lib-gettext.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/t/lib-gettext.sh b/t/lib-gettext.sh index eec757f104708d..9eb160c997a0f4 100644 --- a/t/lib-gettext.sh +++ b/t/lib-gettext.sh @@ -10,7 +10,12 @@ GIT_TEXTDOMAINDIR="$GIT_BUILD_DIR/po/build/locale" GIT_PO_PATH="$GIT_BUILD_DIR/po" export GIT_TEXTDOMAINDIR GIT_PO_PATH -. "$GIT_BUILD_DIR"/git-sh-i18n +if test -n "$GIT_TEST_INSTALLED" +then + . "$(git --exec-path)"/git-sh-i18n +else + . "$GIT_BUILD_DIR"/git-sh-i18n +fi if test_have_prereq GETTEXT && ! test_have_prereq GETTEXT_POISON then From 39d95caa1242569d1b07411e29a2d40a36155740 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 5 Aug 2017 00:18:33 +0200 Subject: [PATCH 069/406] tests: do not require Git to be built when testing an installed Git We really only need the test helpers to be built in the worktree in that case, but that is not what we test for. On the other hand it is a perfect opportunity to verify that `GIT_TEST_INSTALLED` points to a working Git. So let's test the appropriate Git executable. While at it, also adjust the error message in the `GIT_TEST_INSTALLED` case. This patch is best viewed with `-w --patience`. Helped-by: Jeff King Signed-off-by: Johannes Schindelin --- t/test-lib.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 713eb718e68501..9b688d7698518e 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -51,10 +51,15 @@ export LSAN_OPTIONS ################################################################ # It appears that people try to run tests without building... -"$GIT_BUILD_DIR/git" >/dev/null +"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git" >/dev/null if test $? != 1 then - echo >&2 'error: you do not seem to have built git yet.' + if test -n "$GIT_TEST_INSTALLED" + then + echo >&2 "error: there is no working Git at '$GIT_TEST_INSTALLED'" + else + echo >&2 'error: you do not seem to have built git yet.' + fi exit 1 fi From 8651cf106418a8097fb00652abe6574a0b0a8141 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 3 Nov 2016 09:40:12 -0700 Subject: [PATCH 070/406] tests: explicitly use `git.exe` on Windows On Windows, when we refer to `/an/absolute/path/to/git`, it magically resolves `git.exe` at that location. Except if something of the name `git` exists next to that `git.exe`. So if we call `$BUILD_DIR/git`, it will find `$BUILD_DIR/git.exe` *only* if there is not, say, a directory called `$BUILD_DIR/git`. Such a directory, however, exists in Git for Windows when building with Visual Studio (our Visual Studio project generator defaults to putting the build files into a directory whose name is the base name of the corresponding `.exe`). In the bin-wrappers/* scripts, we already take pains to use `git.exe` rather than `git`, as this could pick up the wrong thing on Windows (i.e. if there exists a `git` file or directory in the build directory). Now we do the same in the tests' start-up code. This also helps when testing an installed Git, as there might be even more likely some stray file or directory in the way. Note: the only way we can record whether the `.exe` suffix is by writing it to the `GIT-BUILD-OPTIONS` file and sourcing it at the beginning of `t/test-lib.sh`. This is not a requirement introduced by this patch, but we move the call to be able to use the `$X` variable that holds the file extension, if any. Note also: the many, many calls to `git this` and `git that` are unaffected, as the regular PATH search will find the `.exe` files on Windows (and not be confused by a directory of the name `git` that is in one of the directories listed in the `PATH` variable), while `/path/to/git` would not, per se, know that it is looking for an executable and happily prefer such a directory. For the same reason, we need to handle test-tool.exe the same way. Signed-off-by: Johannes Schindelin --- Makefile | 1 + t/test-lib-functions.sh | 2 +- t/test-lib.sh | 15 ++++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5a969f5830a410..28d78688fa39fb 100644 --- a/Makefile +++ b/Makefile @@ -2567,6 +2567,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+ @echo PAGER_ENV=\''$(subst ','\'',$(subst ','\'',$(PAGER_ENV)))'\' >>$@+ @echo DC_SHA1=\''$(subst ','\'',$(subst ','\'',$(DC_SHA1)))'\' >>$@+ + @echo X=\'$(X)\' >>$@+ ifdef TEST_OUTPUT_DIRECTORY @echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+ endif diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 849b14a7cf7be1..01bbad2e19f9c2 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -898,7 +898,7 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git" init \ + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" init \ "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?" mv .git/hooks .git/hooks-disabled diff --git a/t/test-lib.sh b/t/test-lib.sh index 9b688d7698518e..425a49d555fddf 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -49,9 +49,17 @@ export ASAN_OPTIONS : ${LSAN_OPTIONS=abort_on_error=1} export LSAN_OPTIONS +if test ! -f "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS +then + echo >&2 'error: GIT-BUILD-OPTIONS missing (has Git been built?).' + exit 1 +fi +. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS +export PERL_PATH SHELL_PATH + ################################################################ # It appears that people try to run tests without building... -"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git" >/dev/null +"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null if test $? != 1 then if test -n "$GIT_TEST_INSTALLED" @@ -63,9 +71,6 @@ then exit 1 fi -. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS -export PERL_PATH SHELL_PATH - # if --tee was passed, write the output not only to the terminal, but # additionally to the file test-results/$BASENAME.out, too. case "$GIT_TEST_TEE_STARTED, $* " in @@ -971,7 +976,7 @@ test -d "$GIT_BUILD_DIR"/templates/blt || { error "You haven't built things yet, have you?" } -if ! test -x "$GIT_BUILD_DIR"/t/helper/test-tool +if ! test -x "$GIT_BUILD_DIR"/t/helper/test-tool$X then echo >&2 'You need to build test-tool:' echo >&2 'Run "make t/helper/test-tool" in the source (toplevel) directory' From eafcbabac1629af377e37cfd9af9c6950b4793f4 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 6 Jan 2017 17:39:50 +0100 Subject: [PATCH 071/406] mingw: load system libraries the recommended way When we access IPv6-related functions, we load the corresponding system library using the `LoadLibrary()` function, which is not the recommended way to load system libraries. In practice, it does not make a difference: the `ws2_32.dll` library containing the IPv6 functions is already loaded into memory, so LoadLibrary() simply reuses the already-loaded library. Still, recommended way is recommended way, so let's use that instead. While at it, also adjust the code in contrib/ that loads system libraries. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 3 ++- contrib/credential/wincred/git-credential-wincred.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..f2e67367b98715 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1547,7 +1547,8 @@ static void ensure_socket_initialization(void) WSAGetLastError()); for (name = libraries; *name; name++) { - ipv6_dll = LoadLibrary(*name); + ipv6_dll = LoadLibraryExA(*name, NULL, + LOAD_LIBRARY_SEARCH_SYSTEM32); if (!ipv6_dll) continue; diff --git a/contrib/credential/wincred/git-credential-wincred.c b/contrib/credential/wincred/git-credential-wincred.c index 86518cd93d9c58..5bdad41de1f834 100644 --- a/contrib/credential/wincred/git-credential-wincred.c +++ b/contrib/credential/wincred/git-credential-wincred.c @@ -75,7 +75,8 @@ static CredDeleteWT CredDeleteW; static void load_cred_funcs(void) { /* load DLLs */ - advapi = LoadLibrary("advapi32.dll"); + advapi = LoadLibraryExA("advapi32.dll", NULL, + LOAD_LIBRARY_SEARCH_SYSTEM32); if (!advapi) die("failed to load advapi32.dll"); From 4517b9f84b205f686a26423597cd5b8f4fba570f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 10 Jan 2017 21:42:08 +0100 Subject: [PATCH 072/406] Windows: force-recompile git.res for differing architectures When git.rc is compiled into git.res, the result is actually dependent on the architecture. That is, you cannot simply link a 32-bit git.res into a 64-bit git.exe. Therefore, to allow 32-bit and 64-bit builds in the same directory, we let git.res depend on GIT-PREFIX so that it gets recompiled when compiling for a different architecture (this works because the exec path changes based on the architecture: /mingw32/libexec/git-core for 32-bit and /mingw64/libexec/git-core for 64-bit). Signed-off-by: Johannes Schindelin --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5a969f5830a410..e1eb474acccdac 100644 --- a/Makefile +++ b/Makefile @@ -2087,7 +2087,7 @@ $(SCRIPT_LIB) : % : %.sh GIT-SCRIPT-DEFINES $(QUIET_GEN)$(cmd_munge_script) && \ mv $@+ $@ -git.res: git.rc GIT-VERSION-FILE +git.res: git.rc GIT-VERSION-FILE GIT-PREFIX $(QUIET_RC)$(RC) \ $(join -DMAJOR= -DMINOR= -DMICRO= -DPATCHLEVEL=, $(wordlist 1, 4, \ $(shell echo $(GIT_VERSION) 0 0 0 0 | tr '.a-zA-Z-' ' '))) \ From 5bee993475076dfeffcb26080174214423c10ade Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 11 Dec 2015 06:59:13 +0100 Subject: [PATCH 073/406] mingw: handle absolute paths in expand_user_path() On Windows, an absolute POSIX path needs to be turned into a Windows one. Signed-off-by: Johannes Schindelin --- path.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/path.c b/path.c index 34f0f98349a6ec..a72abf0e1fe9dc 100644 --- a/path.c +++ b/path.c @@ -11,6 +11,7 @@ #include "path.h" #include "packfile.h" #include "object-store.h" +#include "exec-cmd.h" static int get_st_mode_bits(const char *path, int *mode) { @@ -709,6 +710,10 @@ char *expand_user_path(const char *path, int real_home) if (path == NULL) goto return_null; +#ifdef __MINGW32__ + if (path[0] == '/') + return system_path(path + 1); +#endif if (path[0] == '~') { const char *first_slash = strchrnul(path, '/'); const char *username = path + 1; From bb8b52ff5b8a8e58cdebb198a28ea005e902fdd2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 2 Mar 2017 15:52:28 +0100 Subject: [PATCH 074/406] mingw: fix isatty() after dup2() We newly handle isatty() by special-casing the stdin/stdout/stderr file descriptors, caching the return value. However, we missed the case where dup2() overrides the respective file descriptor. That poses a problem e.g. where the `show` builtin asks for a pager very early, the `setup_pager()` function sets the pager depending on the return value of `isatty()` and then redirects stdout. Subsequently, `cmd_log_init_finish()` calls `setup_pager()` *again*. What should happen now is that `isatty()` reports that stdout is *not* a TTY and consequently stdout should be left alone. Let's override dup2() to handle this appropriately. This fixes https://github.com/git-for-windows/git/issues/1077 Signed-off-by: Johannes Schindelin --- compat/mingw.h | 3 +++ compat/winansi.c | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/compat/mingw.h b/compat/mingw.h index 571019d0bddcea..1af81d73f6897c 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -390,6 +390,9 @@ int mingw_raise(int sig); int winansi_isatty(int fd); #define isatty winansi_isatty +int winansi_dup2(int oldfd, int newfd); +#define dup2 winansi_dup2 + void winansi_init(void); HANDLE winansi_get_osfhandle(int fd); diff --git a/compat/winansi.c b/compat/winansi.c index a11a0f16d27647..f4f08237f9ed51 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -474,6 +474,18 @@ static void die_lasterr(const char *fmt, ...) va_end(params); } +#undef dup2 +int winansi_dup2(int oldfd, int newfd) +{ + int ret = dup2(oldfd, newfd); + + if (!ret && newfd >= 0 && newfd <= 2) + fd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ? + 0 : fd_is_interactive[oldfd]; + + return ret; +} + static HANDLE duplicate_handle(HANDLE hnd) { HANDLE hresult, hproc = GetCurrentProcess(); From c37c4633e8eaf1c7f2e279f0077d07c019dab336 Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Thu, 18 Feb 2010 18:27:27 +0100 Subject: [PATCH 075/406] Revert "git-gui: set GIT_DIR and GIT_WORK_TREE after setup" This reverts commit a9fa11fe5bd5978bb175b3b5663f6477a345d428. --- git-gui/git-gui.sh | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 6de74ce639cec9..d424f8f0b227fe 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -1325,9 +1325,6 @@ if {[lindex $_reponame end] eq {.git}} { set _reponame [lindex $_reponame end] } -set env(GIT_DIR) $_gitdir -set env(GIT_WORK_TREE) $_gitworktree - ###################################################################### ## ## global init @@ -2152,7 +2149,7 @@ set starting_gitk_msg [mc "Starting gitk... please wait..."] proc do_gitk {revs {is_submodule false}} { global current_diff_path file_states current_diff_side ui_index - global _gitdir _gitworktree + global _gitworktree # -- Always start gitk through whatever we were loaded with. This # lets us bypass using shell process on Windows systems. @@ -2164,12 +2161,19 @@ proc do_gitk {revs {is_submodule false}} { } else { global env + if {[info exists env(GIT_DIR)]} { + set old_GIT_DIR $env(GIT_DIR) + } else { + set old_GIT_DIR {} + } + set pwd [pwd] if {!$is_submodule} { if {![is_bare]} { cd $_gitworktree } + set env(GIT_DIR) [file normalize [gitdir]] } else { cd $current_diff_path if {$revs eq {--}} { @@ -2190,18 +2194,15 @@ proc do_gitk {revs {is_submodule false}} { } set revs $old_sha1...$new_sha1 } - # GIT_DIR and GIT_WORK_TREE for the submodule are not the ones - # we've been using for the main repository, so unset them. - # TODO we could make life easier (start up faster?) for gitk - # by setting these to the appropriate values to allow gitk - # to skip the heuristics to find their proper value - unset env(GIT_DIR) - unset env(GIT_WORK_TREE) + if {[info exists env(GIT_DIR)]} { + unset env(GIT_DIR) + } } eval exec $cmd $revs "--" "--" & - set env(GIT_DIR) $_gitdir - set env(GIT_WORK_TREE) $_gitworktree + if {$old_GIT_DIR ne {}} { + set env(GIT_DIR) $old_GIT_DIR + } cd $pwd ui_status $::starting_gitk_msg @@ -2222,20 +2223,22 @@ proc do_git_gui {} { error_popup [mc "Couldn't find git gui in PATH"] } else { global env - global _gitdir _gitworktree - # see note in do_gitk about unsetting these vars when - # running tools in a submodule - unset env(GIT_DIR) - unset env(GIT_WORK_TREE) + if {[info exists env(GIT_DIR)]} { + set old_GIT_DIR $env(GIT_DIR) + unset env(GIT_DIR) + } else { + set old_GIT_DIR {} + } set pwd [pwd] cd $current_diff_path eval exec $exe gui & - set env(GIT_DIR) $_gitdir - set env(GIT_WORK_TREE) $_gitworktree + if {$old_GIT_DIR ne {}} { + set env(GIT_DIR) $old_GIT_DIR + } cd $pwd ui_status $::starting_gitk_msg From 765f37cdd5501bdefb87e70714e6181e789d6ee1 Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Sun, 21 Feb 2010 21:05:04 +0100 Subject: [PATCH 076/406] git-gui: provide question helper for retry fallback on Windows Make use of the new environment variable GIT_ASK_YESNO to support the recently implemented fallback in case unlink, rename or rmdir fail for files in use on Windows. The added dialog will present a yes/no question to the the user which will currently be used by the windows compat layer to let the user retry a failed file operation. Signed-off-by: Heiko Voigt --- git-gui/Makefile | 2 ++ git-gui/git-gui--askyesno | 51 +++++++++++++++++++++++++++++++++++++++ git-gui/git-gui.sh | 3 +++ 3 files changed, 56 insertions(+) create mode 100755 git-gui/git-gui--askyesno diff --git a/git-gui/Makefile b/git-gui/Makefile index f10caedaa7a397..d529cab82014ba 100644 --- a/git-gui/Makefile +++ b/git-gui/Makefile @@ -293,6 +293,7 @@ install: all $(QUIET)$(INSTALL_D0)'$(DESTDIR_SQ)$(gitexecdir_SQ)' $(INSTALL_D1) $(QUIET)$(INSTALL_X0)git-gui $(INSTALL_X1) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(QUIET)$(INSTALL_X0)git-gui--askpass $(INSTALL_X1) '$(DESTDIR_SQ)$(gitexecdir_SQ)' + $(QUIET)$(INSTALL_X0)git-gui--askyesno $(INSTALL_X1) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(QUIET)$(foreach p,$(GITGUI_BUILT_INS), $(INSTALL_L0)'$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' $(INSTALL_L1)'$(DESTDIR_SQ)$(gitexecdir_SQ)/git-gui' $(INSTALL_L2)'$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' $(INSTALL_L3) &&) true ifdef GITGUI_WINDOWS_WRAPPER $(QUIET)$(INSTALL_R0)git-gui.tcl $(INSTALL_R1) '$(DESTDIR_SQ)$(gitexecdir_SQ)' @@ -311,6 +312,7 @@ uninstall: $(QUIET)$(CLEAN_DST) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(gitexecdir_SQ)'/git-gui $(REMOVE_F1) $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(gitexecdir_SQ)'/git-gui--askpass $(REMOVE_F1) + $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(gitexecdir_SQ)'/git-gui--askyesno $(REMOVE_F1) $(QUIET)$(foreach p,$(GITGUI_BUILT_INS), $(REMOVE_F0)'$(DESTDIR_SQ)$(gitexecdir_SQ)'/$p $(REMOVE_F1) &&) true ifdef GITGUI_WINDOWS_WRAPPER $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(gitexecdir_SQ)'/git-gui.tcl $(REMOVE_F1) diff --git a/git-gui/git-gui--askyesno b/git-gui/git-gui--askyesno new file mode 100755 index 00000000000000..2a6e6fd11122f5 --- /dev/null +++ b/git-gui/git-gui--askyesno @@ -0,0 +1,51 @@ +#!/bin/sh +# Tcl ignores the next line -*- tcl -*- \ +exec wish "$0" -- "$@" + +# This is an implementation of a simple yes no dialog +# which is injected into the git commandline by git gui +# in case a yesno question needs to be answered. + +set NS {} +set use_ttk [package vsatisfies [package provide Tk] 8.5] +if {$use_ttk} { + set NS ttk +} + +if {$argc < 1} { + puts stderr "Usage: $argv0 " + exit 1 +} else { + set prompt [join $argv " "] +} + +${NS}::frame .t +${NS}::label .t.m -text $prompt -justify center -width 40 +.t.m configure -wraplength 400 +pack .t.m -side top -fill x -padx 20 -pady 20 -expand 1 +pack .t -side top -fill x -ipadx 20 -ipady 20 -expand 1 + +${NS}::frame .b +${NS}::frame .b.left -width 200 +${NS}::button .b.yes -text Yes -command yes +${NS}::button .b.no -text No -command no + + +pack .b.left -side left -expand 1 -fill x +pack .b.yes -side left -expand 1 +pack .b.no -side right -expand 1 -ipadx 5 +pack .b -side bottom -fill x -ipadx 20 -ipady 15 + +bind . {exit 0} +bind . {exit 1} + +proc no {} { + exit 1 +} + +proc yes {} { + exit 0 +} + +wm title . "Question?" +tk::PlaceWindow . diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index d424f8f0b227fe..26c43ca7b119e8 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -1229,6 +1229,9 @@ set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}] if {![info exists env(SSH_ASKPASS)]} { set env(SSH_ASKPASS) [gitexec git-gui--askpass] } +if {![info exists env(GIT_ASK_YESNO)]} { + set env(GIT_ASK_YESNO) [gitexec git-gui--askyesno] +} ###################################################################### ## From 7f3f5b550d32b142a3ee49da2db770dd607c3cd2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 23 Jul 2010 18:06:05 +0200 Subject: [PATCH 077/406] git gui: set GIT_ASKPASS=git-gui--askpass if not set yet Signed-off-by: Johannes Schindelin --- git-gui/git-gui.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 26c43ca7b119e8..99d64e39decdb7 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -1229,6 +1229,9 @@ set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}] if {![info exists env(SSH_ASKPASS)]} { set env(SSH_ASKPASS) [gitexec git-gui--askpass] } +if {![info exists env(GIT_ASKPASS)]} { + set env(GIT_ASKPASS) [gitexec git-gui--askpass] +} if {![info exists env(GIT_ASK_YESNO)]} { set env(GIT_ASK_YESNO) [gitexec git-gui--askyesno] } From d5145b6e7cae0ca86ad35aa9dec48f0f722532e2 Mon Sep 17 00:00:00 2001 From: Thomas Klaeger Date: Sun, 18 Oct 2015 22:31:36 +0200 Subject: [PATCH 078/406] git-gui (Windows): use git-bash.exe if it is available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Git for Windows 2.x ships with an executable that starts the Git Bash with all the environment variables and what not properly set up. It is also adjusted according to the Terminal emulator option chosen when installing Git for Windows (while `bash.exe --login -i` would always launch with Windows' default console). So let's use that executable (usually C:\Program Files\Git\git-bash.exe) instead of `bash.exe --login -i` if its presence was detected. This fixes https://github.com/git-for-windows/git/issues/490 Signed-off-by: Thomas Kläger Signed-off-by: Johannes Schindelin --- git-gui/git-gui.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 99d64e39decdb7..9562cce698f80e 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -2715,10 +2715,18 @@ if {![is_bare]} { } if {[is_Windows]} { + # Use /git-bash.exe if available + set normalized [file normalize $::argv0] + regsub "/mingw../libexec/git-core/git-gui$" \ + $normalized "/git-bash.exe" cmdLine + if {$cmdLine != $normalized && [file exists $cmdLine]} { + set cmdLine [list "Git Bash" $cmdLine &] + } else { + set cmdLine [list "Git Bash" bash --login -l &] + } .mbar.repository add command \ -label [mc "Git Bash"] \ - -command {eval exec [auto_execok start] \ - [list "Git Bash" bash --login -l &]} + -command {eval exec [auto_execok start] $cmdLine} } if {[is_Windows] || ![is_bare]} { From b178a81b438ad586712c66949432895bdc4ec049 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 4 Oct 2018 14:46:00 +0200 Subject: [PATCH 079/406] respect core.hooksPath, falling back to .git/hooks Since v2.9.0, Git knows about the config variable core.hookspath that allows overriding the path to the directory containing the Git hooks. Since v2.10.0, the `--git-path` option respects that config variable, too, so we may just as well use that command. For Git versions older than v2.5.0 (which was the first version to support the `--git-path` option for the `rev-parse` command), we simply fall back to the previous code. This fixes https://github.com/git-for-windows/git/issues/1755 Initial-patch-by: Philipp Gortan Signed-off-by: Johannes Schindelin --- git-gui/git-gui.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 6de74ce639cec9..a32a9e6861a496 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -623,7 +623,11 @@ proc git_write {args} { } proc githook_read {hook_name args} { - set pchook [gitdir hooks $hook_name] + if {[package vcompare $::_git_version 2.5.0] >= 0} { + set pchook [git rev-parse --git-path "hooks/$hook_name"] + } else { + set pchook [gitdir hooks $hook_name] + } lappend args 2>@1 # On Windows [file executable] might lie so we need to ask From c0e476df1d37915464eae834b6134ce126ecf586 Mon Sep 17 00:00:00 2001 From: Karsten Blees Date: Sat, 4 Feb 2012 21:54:36 +0100 Subject: [PATCH 080/406] gitk: Unicode file name support Assumes file names in git tree objects are UTF-8 encoded. On most unix systems, the system encoding (and thus the TCL system encoding) will be UTF-8, so file names will be displayed correctly. On Windows, it is impossible to set the system encoding to UTF-8. Changing the TCL system encoding (via 'encoding system ...', e.g. in the startup code) is explicitly discouraged by the TCL docs. Change gitk functions dealing with file names to always convert from and to UTF-8. Signed-off-by: Karsten Blees Signed-off-by: Johannes Schindelin --- gitk-git/gitk | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index a14d7a16b2dd11..e2a7f089cb4a22 100755 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -7634,7 +7634,7 @@ proc gettreeline {gtf id} { if {[string index $fname 0] eq "\""} { set fname [lindex $fname 0] } - set fname [encoding convertfrom $fname] + set fname [encoding convertfrom utf-8 $fname] lappend treefilelist($id) $fname } if {![eof $gtf]} { @@ -7896,7 +7896,7 @@ proc gettreediffline {gdtf ids} { if {[string index $file 0] eq "\""} { set file [lindex $file 0] } - set file [encoding convertfrom $file] + set file [encoding convertfrom utf-8 $file] if {$file ne [lindex $treediff end]} { lappend treediff $file lappend sublist $file @@ -8041,7 +8041,7 @@ proc makediffhdr {fname ids} { global ctext curdiffstart treediffs diffencoding global ctext_file_names jump_to_here targetline diffline - set fname [encoding convertfrom $fname] + set fname [encoding convertfrom utf-8 $fname] set diffencoding [get_path_encoding $fname] set i [lsearch -exact $treediffs($ids) $fname] if {$i >= 0} { @@ -8103,7 +8103,7 @@ proc parseblobdiffline {ids line} { if {![string compare -length 5 "diff " $line]} { if {![regexp {^diff (--cc|--git) } $line m type]} { - set line [encoding convertfrom $line] + set line [encoding convertfrom utf-8 $line] $ctext insert end "$line\n" hunksep continue } @@ -8150,7 +8150,7 @@ proc parseblobdiffline {ids line} { makediffhdr $fname $ids } elseif {![string compare -length 16 "* Unmerged path " $line]} { - set fname [encoding convertfrom [string range $line 16 end]] + set fname [encoding convertfrom utf-8 [string range $line 16 end]] $ctext insert end "\n" set curdiffstart [$ctext index "end - 1c"] lappend ctext_file_names $fname @@ -8205,7 +8205,7 @@ proc parseblobdiffline {ids line} { if {[string index $fname 0] eq "\""} { set fname [lindex $fname 0] } - set fname [encoding convertfrom $fname] + set fname [encoding convertfrom utf-8 $fname] set i [lsearch -exact $treediffs($ids) $fname] if {$i >= 0} { setinlist difffilestart $i $curdiffstart @@ -8224,6 +8224,7 @@ proc parseblobdiffline {ids line} { set diffinhdr 0 return } + set line [encoding convertfrom utf-8 $line] $ctext insert end "$line\n" filesep } else { @@ -12161,7 +12162,7 @@ proc cache_gitattr {attr pathlist} { foreach row [split $rlist "\n"] { if {[regexp "(.*): $attr: (.*)" $row m path value]} { if {[string index $path 0] eq "\""} { - set path [encoding convertfrom [lindex $path 0]] + set path [encoding convertfrom utf-8 [lindex $path 0]] } set path_attr_cache($attr,$path) $value } From 27acc14fc3a2148173389019349d90d8717ac762 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 11 Aug 2009 02:22:33 +0200 Subject: [PATCH 081/406] gitk: work around the command line limit on Windows On Windows, there are dramatic problems when a command line grows beyond PATH_MAX, which is restricted to 8191 characters on XP and later (according to http://support.microsoft.com/kb/830473). Work around this by just cutting off the command line at that length (actually, at a space boundary) in the hope that only negative refs are chucked: gitk will then do unnecessary work, but that is still better than flashing the gitk window and exiting with exit status 5 (which no Windows user is able to make sense of). The first fix caused Tcl to fail to compile the regexp, see msysGit issue 427. Here is another fix without using regexp, and using a more relaxed command line length limit to fix the original issue 387. Signed-off-by: Sebastian Schuberth Signed-off-by: Pat Thoyts Signed-off-by: Johannes Schindelin --- gitk-git/gitk | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index e2a7f089cb4a22..a7cfa867f1575c 100755 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -10172,7 +10172,19 @@ proc getallcommits {} { } } if {$ids ne {}} { - set fd [open [concat $cmd $ids] r] + set cmd [concat $cmd $ids] + # The maximum command line length for the CreateProcess function is 32767 characters, see + # http://blogs.msdn.com/oldnewthing/archive/2003/12/10/56028.aspx + # Be a little conservative in case Tcl adds some more stuff to the command line we do not + # know about and truncate the command line at a SHA1-boundary below 32000 characters. + if {[tk windowingsystem] == "win32" && + [string length $cmd] > 32000} { + set ndx [string last " " $cmd 32000] + if {$ndx != -1} { + set cmd [string range $cmd 0 $ndx] + } + } + set fd [open $cmd r] fconfigure $fd -blocking 0 incr allcommits nowbusy allcommits From ec100945d337e8fbeeef82ada20e3dd8ba3cc5bf Mon Sep 17 00:00:00 2001 From: "Chris West (Faux)" Date: Mon, 26 Jul 2010 00:36:19 +0100 Subject: [PATCH 082/406] gitk: fix another invocation with an overly long command-line Signed-off-by: Sebastian Schuberth --- gitk-git/gitk | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index a7cfa867f1575c..3e0c9fca7ed4c5 100755 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -406,7 +406,7 @@ proc start_rev_list {view} { if {$revs eq {}} { return 0 } - set args [concat $vflags($view) $revs] + set args [limit_arg_length [concat $vflags($view) $revs]] } else { set args $vorigargs($view) } @@ -10172,18 +10172,7 @@ proc getallcommits {} { } } if {$ids ne {}} { - set cmd [concat $cmd $ids] - # The maximum command line length for the CreateProcess function is 32767 characters, see - # http://blogs.msdn.com/oldnewthing/archive/2003/12/10/56028.aspx - # Be a little conservative in case Tcl adds some more stuff to the command line we do not - # know about and truncate the command line at a SHA1-boundary below 32000 characters. - if {[tk windowingsystem] == "win32" && - [string length $cmd] > 32000} { - set ndx [string last " " $cmd 32000] - if {$ndx != -1} { - set cmd [string range $cmd 0 $ndx] - } - } + set cmd [limit_arg_length [concat $cmd $ids]] set fd [open $cmd r] fconfigure $fd -blocking 0 incr allcommits @@ -10194,6 +10183,21 @@ proc getallcommits {} { } } +# The maximum command line length for the CreateProcess function is 32767 characters, see +# http://blogs.msdn.com/oldnewthing/archive/2003/12/10/56028.aspx +# Be a little conservative in case Tcl adds some more stuff to the command line we do not +# know about and truncate the command line at a SHA1-boundary below 32000 characters. +proc limit_arg_length {cmd} { + if {[tk windowingsystem] == "win32" && + [string length $cmd] > 32000} { + set ndx [string last " " $cmd 32000] + if {$ndx != -1} { + return [string range $cmd 0 $ndx] + } + } + return $cmd +} + # Since most commits have 1 parent and 1 child, we group strings of # such commits into "arcs" joining branch/merge points (BMPs), which # are commits that either don't have 1 parent or don't have 1 child. From 9b45cd6a282c5c37cd6fbc4eadbff3e3ddb564c8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 20 Sep 2017 21:52:28 +0200 Subject: [PATCH 083/406] git-gui--askyesno: fix funny text wrapping The text wrapping seems to be aligned to the right side of the Yes button, leaving an awful lot of empty space. Let's try to counter this by using pixel units. Signed-off-by: Johannes Schindelin --- git-gui/git-gui--askyesno | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-gui/git-gui--askyesno b/git-gui/git-gui--askyesno index 2a6e6fd11122f5..cf9c990d0919b3 100755 --- a/git-gui/git-gui--askyesno +++ b/git-gui/git-gui--askyesno @@ -20,8 +20,8 @@ if {$argc < 1} { } ${NS}::frame .t -${NS}::label .t.m -text $prompt -justify center -width 40 -.t.m configure -wraplength 400 +${NS}::label .t.m -text $prompt -justify center -width 400px +.t.m configure -wraplength 400px pack .t.m -side top -fill x -padx 20 -pady 20 -expand 1 pack .t -side top -fill x -ipadx 20 -ipady 20 -expand 1 From 824ec3911ec0034e15cd63524574c3bb75dbf9b1 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Sun, 22 Jul 2012 23:19:24 +0200 Subject: [PATCH 084/406] gitk: Use an external icon file on Windows Git for Windows now ships with the new Git icon from git-scm.com. Use that icon file if it exists instead of the old procedurally drawn one. This patch was sent upstream but so far no decision on its inclusion was made, so commit it to our fork. Signed-off-by: Sebastian Schuberth --- gitk-git/gitk | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index 3e0c9fca7ed4c5..805e39f42b9864 100755 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -12208,7 +12208,6 @@ if { [info exists ::env(GITK_MSGSDIR)] } { set gitk_prefix [file dirname [file dirname [file normalize $argv0]]] set gitk_libdir [file join $gitk_prefix share gitk lib] set gitk_msgsdir [file join $gitk_libdir msgs] - unset gitk_prefix } ## Internationalization (i18n) through msgcat and gettext. See @@ -12563,28 +12562,32 @@ if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} { set worktree [exec git rev-parse --show-toplevel] setcoords makewindow -catch { - image create photo gitlogo -width 16 -height 16 - - image create photo gitlogominus -width 4 -height 2 - gitlogominus put #C00000 -to 0 0 4 2 - gitlogo copy gitlogominus -to 1 5 - gitlogo copy gitlogominus -to 6 5 - gitlogo copy gitlogominus -to 11 5 - image delete gitlogominus - - image create photo gitlogoplus -width 4 -height 4 - gitlogoplus put #008000 -to 1 0 3 4 - gitlogoplus put #008000 -to 0 1 4 3 - gitlogo copy gitlogoplus -to 1 9 - gitlogo copy gitlogoplus -to 6 9 - gitlogo copy gitlogoplus -to 11 9 - image delete gitlogoplus - - image create photo gitlogo32 -width 32 -height 32 - gitlogo32 copy gitlogo -zoom 2 2 - - wm iconphoto . -default gitlogo gitlogo32 +if {$::tcl_platform(platform) eq {windows} && [file exists $gitk_prefix/etc/git.ico]} { + wm iconbitmap . -default $gitk_prefix/etc/git.ico +} else { + catch { + image create photo gitlogo -width 16 -height 16 + + image create photo gitlogominus -width 4 -height 2 + gitlogominus put #C00000 -to 0 0 4 2 + gitlogo copy gitlogominus -to 1 5 + gitlogo copy gitlogominus -to 6 5 + gitlogo copy gitlogominus -to 11 5 + image delete gitlogominus + + image create photo gitlogoplus -width 4 -height 4 + gitlogoplus put #008000 -to 1 0 3 4 + gitlogoplus put #008000 -to 0 1 4 3 + gitlogo copy gitlogoplus -to 1 9 + gitlogo copy gitlogoplus -to 6 9 + gitlogo copy gitlogoplus -to 11 9 + image delete gitlogoplus + + image create photo gitlogo32 -width 32 -height 32 + gitlogo32 copy gitlogo -zoom 2 2 + + wm iconphoto . -default gitlogo gitlogo32 + } } # wait for the window to become visible tkwait visibility . From d522e6d19c2e322765e7782e4e011dc1a5b6e709 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 20 Sep 2017 21:53:45 +0200 Subject: [PATCH 085/406] git-gui--askyesno: allow overriding the window title "Question?" is maybe not the most informative thing to ask. In the absence of better information, it is the best we can do, of course. However, Git for Windows' auto updater just learned the trick to use git-gui--askyesno to ask the user whether to update now or not. And in this scripted scenario, we can easily pass a command-line option to change the window title. So let's support that with the new `--title ` option. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-gui/git-gui--askyesno | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/git-gui/git-gui--askyesno b/git-gui/git-gui--askyesno index cf9c990d0919b3..45b0260eff8145 100755 --- a/git-gui/git-gui--askyesno +++ b/git-gui/git-gui--askyesno @@ -12,10 +12,15 @@ if {$use_ttk} { set NS ttk } +set title "Question?" if {$argc < 1} { puts stderr "Usage: $argv0 <question>" exit 1 } else { + if {$argc > 2 && [lindex $argv 0] == "--title"} { + set title [lindex $argv 1] + set argv [lreplace $argv 0 1] + } set prompt [join $argv " "] } @@ -47,5 +52,5 @@ proc yes {} { exit 0 } -wm title . "Question?" +wm title . $title tk::PlaceWindow . From 4e2751b0860e0417e619fb54b58f565dd385ce74 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 16 Feb 2016 16:42:06 +0100 Subject: [PATCH 086/406] gitk: fix arrow keys in input fields with Tcl/Tk >= 8.6 Tcl/Tk 8.6 introduced new events for the cursor left/right keys and apparently changed the behavior of the previous event. Let's work around that by using the new events when we are running with Tcl/Tk 8.6 or later. This fixes https://github.com/git-for-windows/git/issues/495 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- gitk-git/gitk | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index 805e39f42b9864..8d7a6bb180264b 100755 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -2076,7 +2076,7 @@ proc makewindow {} { global headctxmenu progresscanv progressitem progresscoords statusw global fprogitem fprogcoord lastprogupdate progupdatepending global rprogitem rprogcoord rownumsel numcommits - global have_tk85 use_ttk NS + global have_tk85 have_tk86 use_ttk NS global git_version global worddiff @@ -2566,8 +2566,13 @@ proc makewindow {} { bind . <Key-Down> "selnextline 1" bind . <Shift-Key-Up> "dofind -1 0" bind . <Shift-Key-Down> "dofind 1 0" - bindkey <Key-Right> "goforw" - bindkey <Key-Left> "goback" + if {$have_tk86} { + bindkey <<NextChar>> "goforw" + bindkey <<PrevChar>> "goback" + } else { + bindkey <Key-Right> "goforw" + bindkey <Key-Left> "goback" + } bind . <Key-Prior> "selnextpage -1" bind . <Key-Next> "selnextpage 1" bind . <$M1B-Home> "allcanvs yview moveto 0.0" @@ -12498,6 +12503,7 @@ set nullid2 "0000000000000000000000000000000000000001" set nullfile "/dev/null" set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}] +set have_tk86 [expr {[package vcompare $tk_version "8.6"] >= 0}] if {![info exists have_ttk]} { set have_ttk [llength [info commands ::ttk::style]] } From 2c31f880ea35609fcebaf38b19fc8ea83a848628 Mon Sep 17 00:00:00 2001 From: Max Kirillov <max@max630.net> Date: Wed, 18 Jan 2017 21:01:09 +0200 Subject: [PATCH 087/406] git-gui: correctly restore GIT_DIR after invoking gitk git-gui tries to temporary set GIT_DIR for starting gitk and restore it back after they are started. But in case of GIT_DIR which was not set prior to invocation it is not unset after it. This affects commands which can be later started from that git gui, for example "Git Bash". Fix it. Signed-off-by: Max Kirillov <max@max630.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-gui/git-gui.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 99d64e39decdb7..46de3eab9dec60 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -2208,6 +2208,8 @@ proc do_gitk {revs {is_submodule false}} { if {$old_GIT_DIR ne {}} { set env(GIT_DIR) $old_GIT_DIR + } else { + unset env(GIT_DIR) } cd $pwd From 9951b649bfd4cf73a20fd2d9c8abed9f1e32cbf9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 20 Sep 2017 21:55:45 +0200 Subject: [PATCH 088/406] git-gui--askyesno (mingw): use Git for Windows' icon, if available For additional GUI goodness. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-gui/git-gui--askyesno | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/git-gui/git-gui--askyesno b/git-gui/git-gui--askyesno index 45b0260eff8145..c0c82e7cbd01d6 100755 --- a/git-gui/git-gui--askyesno +++ b/git-gui/git-gui--askyesno @@ -52,5 +52,17 @@ proc yes {} { exit 0 } +if {$::tcl_platform(platform) eq {windows}} { + set icopath [file dirname [file normalize $argv0]] + if {[file tail $icopath] eq {git-core}} { + set icopath [file dirname $icopath] + } + set icopath [file dirname $icopath] + set icopath [file join $icopath share git git-for-windows.ico] + if {[file exists $icopath]} { + wm iconbitmap . -default $icopath + } +} + wm title . $title tk::PlaceWindow . From 4d5a955a4ad29ea069c329f74144b23a6f725263 Mon Sep 17 00:00:00 2001 From: "James J. Raden" <james.raden@gmail.com> Date: Thu, 21 Jan 2016 12:07:47 -0500 Subject: [PATCH 089/406] gitk: make the "list references" default window width wider When using remotes (with git-flow especially), the remote reference names are almost always wordwrapped in the "list references" window because it's somewhat narrow by default. It's possible to resize it with a mouse, but it's annoying to have to do this every time, especially on Windows 10, where the window border seems to be only one (1) pixel wide, thus making the grabbing of the window border tricky. Signed-off-by: James J. Raden <james.raden@gmail.com> --- gitk-git/gitk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index 8d7a6bb180264b..d1d77d832ee0ed 100755 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -9988,7 +9988,7 @@ proc showrefs {} { text $top.list -background $bgcolor -foreground $fgcolor \ -selectbackground $selectbgcolor -font mainfont \ -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \ - -width 30 -height 20 -cursor $maincursor \ + -width 60 -height 20 -cursor $maincursor \ -spacing1 1 -spacing3 1 -state disabled $top.list tag configure highlight -background $selectbgcolor if {![lsearch -exact $bglist $top.list]} { From 5cff37b36917ed5fe62032f9218b48dd616d4237 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 22 Apr 2015 14:47:27 +0100 Subject: [PATCH 090/406] Windows: add support for a Windows-wide configuration Between the libgit2 and the Git for Windows project, there has been a discussion how we could share Git configuration to avoid duplication (or worse: skew). Earlier, libgit2 was nice enough to just re-use Git for Windows' C:\Program Files (x86)\Git\etc\gitconfig but with the upcoming Git for Windows 2.x, there would be more paths to search, as we will have 64-bit and 32-bit versions, and the corresponding config files will be in %PROGRAMFILES%\Git\mingw64\etc and ...\mingw32\etc, respectively. Worse: there are portable Git for Windows versions out there which live in totally unrelated directories, still. Therefore we came to a consensus to use `%PROGRAMDATA%\Git\config` as the location for shared Git settings that are of wider interest than just Git for Windows. Of course, the configuration in `%PROGRAMDATA%\Git\config` has the widest reach, therefore it must take the lowest precedence, i.e. Git for Windows can still override settings in its `etc/gitconfig` file. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Documentation/config.txt | 4 +++- Documentation/git-config.txt | 5 +++++ Documentation/git.txt | 3 ++- compat/mingw.c | 14 ++++++++++++++ compat/mingw.h | 2 ++ config.c | 13 ++++++++++--- git-compat-util.h | 4 ++++ 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index eb66a119753726..62a2e9cecff804 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -6,7 +6,9 @@ the Git commands' behavior. The `.git/config` file in each repository is used to store the configuration for that repository, and `$HOME/.gitconfig` is used to store a per-user configuration as fallback values for the `.git/config` file. The file `/etc/gitconfig` -can be used to store a system-wide default configuration. +can be used to store a system-wide default configuration. On Windows, +configuration can also be stored in `C:\ProgramData\Git\config`; This +file will be used also by libgit2-based software. The configuration variables are used by both the Git plumbing and the porcelains. The variables are divided into sections, wherein diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 8e240435bee8fb..4e4021a93ea503 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -281,6 +281,11 @@ $XDG_CONFIG_HOME/git/config:: $GIT_DIR/config:: Repository specific configuration file. +On Windows, as there is no central `/etc/` directory, there is yet another +config file, intended to contain settings for *all* Git-related software +running on the machine. Consequently, this config file takes an even lower +precedence than the `$(prefix)/etc/gitconfig` file. + If no further options are given, all reading options will read all of these files that are available. If the global or the system-wide configuration file are not available they will be ignored. If the repository configuration diff --git a/Documentation/git.txt b/Documentation/git.txt index dba7f0c18e33e7..f54d4d1080addd 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -567,7 +567,8 @@ for further details. `GIT_CONFIG_NOSYSTEM`:: Whether to skip reading settings from the system-wide - `$(prefix)/etc/gitconfig` file. This environment variable can + `$(prefix)/etc/gitconfig` file (and on Windows, also from the + `%PROGRAMDATA%\Git\config` file). This environment variable can be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a predictable environment for a picky script, or you can set it temporarily to avoid using a buggy `/etc/gitconfig` file while diff --git a/compat/mingw.c b/compat/mingw.c index 45c8046c7cc34f..665dcbdc2da7ce 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2362,3 +2362,17 @@ int uname(struct utsname *buf) "%u", (v >> 16) & 0x7fff); return 0; } + +const char *program_data_config(void) +{ + static struct strbuf path = STRBUF_INIT; + static unsigned initialized; + + if (!initialized) { + const char *env = mingw_getenv("PROGRAMDATA"); + if (env) + strbuf_addf(&path, "%s/Git/config", env); + initialized = 1; + } + return *path.buf ? path.buf : NULL; +} diff --git a/compat/mingw.h b/compat/mingw.h index f31dcff2be1d60..45667999ce7b03 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -426,6 +426,8 @@ int mingw_offset_1st_component(const char *path); #define PATH_SEP ';' extern char *mingw_query_user_email(void); #define query_user_email mingw_query_user_email +extern const char *program_data_config(void); +#define git_program_data_config program_data_config #if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800) #define PRIuMAX "I64u" #define PRId64 "I64d" diff --git a/config.c b/config.c index 3461993f0af665..0336fa99897823 100644 --- a/config.c +++ b/config.c @@ -1680,9 +1680,16 @@ static int do_git_config_sequence(const struct config_options *opts, repo_config = NULL; current_parsing_scope = CONFIG_SCOPE_SYSTEM; - if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) - ret += git_config_from_file(fn, git_etc_gitconfig(), - data); + if (git_config_system()) { + if (git_program_data_config() && + !access_or_die(git_program_data_config(), R_OK, 0)) + ret += git_config_from_file(fn, + git_program_data_config(), + data); + if (!access_or_die(git_etc_gitconfig(), R_OK, 0)) + ret += git_config_from_file(fn, git_etc_gitconfig(), + data); + } current_parsing_scope = CONFIG_SCOPE_GLOBAL; if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) diff --git a/git-compat-util.h b/git-compat-util.h index 71779cb0aeb6f3..03406f21a792cb 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -386,6 +386,10 @@ static inline char *git_find_last_dir_sep(const char *path) #define query_user_email() NULL #endif +#ifndef git_program_data_config +#define git_program_data_config() NULL +#endif + #if defined(__HP_cc) && (__HP_cc >= 61000) #define NORETURN __attribute__((noreturn)) #define NORETURN_PTR From 828cdc96e51facfcd28e0e53c91f4ff78ed0b97b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 16 Feb 2016 15:21:45 +0100 Subject: [PATCH 091/406] Clarify the location of the Windows-specific ProgramData config On Windows, there is no (single) `/etc/` directory. To address that, in conjunction with the libgit2 project, Git for Windows introduced yet another level of system-wide config files, located in C:\ProgramData (and the equivalent on Windows XP). Let's spell this out in the documentation. This closes https://github.com/git-for-windows/git/pull/470 (because there was no reaction in three months in that Pull Request). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Documentation/git-config.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 4e4021a93ea503..5fd9b865b8ee01 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -282,9 +282,11 @@ $GIT_DIR/config:: Repository specific configuration file. On Windows, as there is no central `/etc/` directory, there is yet another -config file, intended to contain settings for *all* Git-related software -running on the machine. Consequently, this config file takes an even lower -precedence than the `$(prefix)/etc/gitconfig` file. +config file (located at `$PROGRAMDATA/Git/config`), intended to contain +settings for *all* Git-related software running on the machine. Consequently, +this config file takes an even lower precedence than the +`$(prefix)/etc/gitconfig` file. Typically `$PROGRAMDATA` points to +`C:\ProgramData`. If no further options are given, all reading options will read all of these files that are available. If the global or the system-wide configuration From 79da41902fd4731e4eb569a0a3a7b76e6e77a273 Mon Sep 17 00:00:00 2001 From: Kelly Heller <kkheller@cedrus.com> Date: Wed, 27 May 2015 14:51:43 -0700 Subject: [PATCH 092/406] Allow `add -p` and `add -i` with a large number of files This fixes https://github.com/msysgit/git/issues/182. Inspired by Pull Request 218 using code from @PhilipDavis. [jes: simplified code quite a bit] Signed-off-by: Kelly Heller <kkheller@cedrus.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-add--interactive.perl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 20eb81cc92f947..aacc0f95f9265c 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -160,6 +160,24 @@ sub run_cmd_pipe { die "$^O does not support: @invalid\n" if @invalid; my @args = map { m/ /o ? "\"$_\"": $_ } @_; return qx{@args}; + } elsif (($^O eq 'MSWin32' || $^O eq 'msys') && (scalar @_ > 200) && + grep $_ eq '--', @_) { + use File::Temp qw(tempfile); + my ($fhargs, $filename) = + tempfile('git-args-XXXXXX', UNLINK => 1); + + my $cmd = 'cat '.$filename.' | xargs -0 -s 20000 '; + while ($_[0] ne '--') { + $cmd = $cmd . shift(@_) . ' '; + } + + shift(@_); + print $fhargs join("\0", @_); + close($fhargs); + + my $fh = undef; + open($fh, '-|', $cmd) or die; + return <$fh>; } else { my $fh = undef; open($fh, '-|', @_) or die; From 20a7e672fee257f33a2ff9d6643d41b667ab8062 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 16 Feb 2016 16:31:12 +0100 Subject: [PATCH 093/406] remove_dirs: do not swallow error when stat() failed Without an error message when stat() failed, e.g. `git clean` would abort without an error message, leaving the user quite puzzled. This fixes https://github.com/git-for-windows/git/issues/521 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/clean.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/clean.c b/builtin/clean.c index 8d9a7dc2064707..ba38c5ebece977 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -192,7 +192,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, strbuf_setlen(path, len); strbuf_addstr(path, e->d_name); if (lstat(path->buf, &st)) - ; /* fall thru */ + warning("Could not stat path '%s': %s", + path->buf, strerror(errno)); else if (S_ISDIR(st.st_mode)) { if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone)) ret = 1; From ca9ab290388d24a92aca5cd275cc2771e5f1f60d Mon Sep 17 00:00:00 2001 From: Andreas Heiduk <asheiduk@gmail.com> Date: Mon, 15 May 2017 23:39:51 +0200 Subject: [PATCH 094/406] Improve documentation for C:\ProgramData\Git\config Move the description for the additional Git for Windows configuration file into the right place, so that the following descriptions of the read priority also covers this file correctly. Also make it clear, what file `git config --system` selects. Signed-off-by: Andreas Heiduk <asheiduk@gmail.com> --- Documentation/git-config.txt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 5fd9b865b8ee01..3ca1dd7c605629 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -263,8 +263,16 @@ FILES If not set explicitly with `--file`, there are four files where 'git config' will search for configuration options: +$PROGRAMDATA/Git/config:: + (Windows-only) System-wide configuration file shared with other Git + implementations. Typically `$PROGRAMDATA` points to `C:\ProgramData`. + $(prefix)/etc/gitconfig:: System-wide configuration file. + (Windows-only) This file contains only the settings which are + specific for this installation of Git for Windows and which should + not be shared with other Git implementations like JGit, libgit2. + `--system` will select this file. $XDG_CONFIG_HOME/git/config:: Second user-specific configuration file. If $XDG_CONFIG_HOME is not set @@ -281,13 +289,6 @@ $XDG_CONFIG_HOME/git/config:: $GIT_DIR/config:: Repository specific configuration file. -On Windows, as there is no central `/etc/` directory, there is yet another -config file (located at `$PROGRAMDATA/Git/config`), intended to contain -settings for *all* Git-related software running on the machine. Consequently, -this config file takes an even lower precedence than the -`$(prefix)/etc/gitconfig` file. Typically `$PROGRAMDATA` points to -`C:\ProgramData`. - If no further options are given, all reading options will read all of these files that are available. If the global or the system-wide configuration file are not available they will be ignored. If the repository configuration From 96b2004bbcc5879eca1b9f97b3096a3bf66f7bff Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 12 Sep 2015 12:25:47 +0200 Subject: [PATCH 095/406] t3701: verify that we can add *lots* of files interactively Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t3701-add-interactive.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 609fbfdc317137..62b9bcd461b029 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -639,4 +639,25 @@ test_expect_success 'add -p patch editing works with pathological context lines' test_cmp expected-2 actual ' +test_expect_success EXPENSIVE 'add -i with a lot of files' ' + git reset --hard && + x160=0123456789012345678901234567890123456789 && + x160=$x160$x160$x160$x160 && + y= && + i=0 && + while test $i -le 200 + do + name=$(printf "%s%03d" $x160 $i) && + echo $name >$name && + git add -N $name && + y="${y}y$LF" && + i=$(($i+1)) || + break + done && + echo "$y" | git add -p -- . && + git diff --cached >staged && + test_line_count = 1407 staged && + git reset --hard +' + test_done From 27752d59bd631ad0e2d7222b0ecfc24cd975459d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 16 Feb 2016 16:36:10 +0100 Subject: [PATCH 096/406] t7300: `git clean -dfx` must show an error with long paths In particular on Windows, where the default maximum path length is quite small, but there are ways to circumvent that limit in many cases, it is very important that users be given an indication why their command failed because of too long paths when it did. This test case makes sure that a warning is issued that would have helped the user who reported Git for Windows' issue 521: https://github.com/git-for-windows/git/issues/521 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t7300-clean.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh index 7b36954d63d70e..aa08443f6ac86d 100755 --- a/t/t7300-clean.sh +++ b/t/t7300-clean.sh @@ -669,4 +669,15 @@ test_expect_success 'git clean -d skips untracked dirs containing ignored files' test_path_is_missing foo/b/bb ' +test_expect_success MINGW 'handle clean & core.longpaths = false nicely' ' + git config core.longpaths false && + test_when_finished git config --unset core.longpaths && + a50=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && + mkdir -p $a50$a50/$a50$a50/$a50$a50 && + touch $a50$a50/test.txt && + touch $a50$a50/$a50$a50/$a50$a50/test.txt && + test_must_fail git clean -xdf 2>.git/err && + grep "too long" .git/err +' + test_done From 5c7ce1ffed82363728e87749073e445dd3d1accb Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 12 Aug 2016 10:54:26 +0200 Subject: [PATCH 097/406] status: carry the --no-lock-index option for backwards-compatibility When a third-party tool periodically runs `git status` in order to keep track of the state of the working tree, it is a bad idea to lock the index: it might interfere with interactive commands executed by the user, e.g. when the user wants to commit files. Git for Windows introduced the `--no-lock-index` option a long time ago to fix that (it made it into Git for Windows v2.9.2(3)) by simply avoiding to write that file. The downside is that the periodic `git status` calls will be a little bit more wasteful because they may have to refresh the index repeatedly, only to throw away the updates when it exits. This cannot really be helped, though, as tools wanting to get a periodic update of the status have no way to predict when the user may want to lock the index herself. Sadly, a competing approach was submitted (by somebody who apparently has less work on their plate than this maintainer) that made it into v2.15.0 but is *different*: instead of a `git status`-only option, it is an option that comes *before* the Git command and is called differently, too. Let's give previous users a chance to upgrade to newer Git for Windows versions by handling the `--no-lock-index` option, still, though with a big fat warning. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Documentation/git-status.txt | 7 +++++++ builtin/commit.c | 10 ++++++++++ t/t7508-status.sh | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/Documentation/git-status.txt b/Documentation/git-status.txt index d9f422d5600af6..eec603fbfbe1cc 100644 --- a/Documentation/git-status.txt +++ b/Documentation/git-status.txt @@ -145,6 +145,13 @@ ignored, then the directory is not shown, but all contents are shown. threshold. See also linkgit:git-diff[1] `--find-renames`. +--no-lock-index:: +--lock-index:: + (DEPRECATED: use --no-optional-locks instead) + Specifies whether `git status` should try to lock the index and + update it afterwards if any changes were detected. Defaults to + `--lock-index`. + <pathspec>...:: See the 'pathspec' entry in linkgit:gitglossary[7]. diff --git a/builtin/commit.c b/builtin/commit.c index 0d9828e29ebe89..d7497ac03aeb9a 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1294,6 +1294,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) { static int no_renames = -1; static const char *rename_score_arg = (const char *)-1; + static int no_lock_index = 0; static struct wt_status s; int fd; struct object_id oid; @@ -1331,6 +1332,9 @@ int cmd_status(int argc, const char **argv, const char *prefix) { OPTION_CALLBACK, 'M', "find-renames", &rename_score_arg, N_("n"), N_("detect renames, optionally set similarity index"), PARSE_OPT_OPTARG, opt_parse_rename_score }, + OPT_BOOL(0, "no-lock-index", &no_lock_index, + N_("(DEPRECATED: use `git --no-optional-locks status` " + "instead) Do not lock the index")), OPT_END(), }; @@ -1344,6 +1348,12 @@ int cmd_status(int argc, const char **argv, const char *prefix) finalize_colopts(&s.colopts, -1); finalize_deferred_config(&s); + if (no_lock_index) { + warning("--no-lock-index is deprecated, use --no-optional-locks" + " instead"); + setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "false", 1); + } + handle_untracked_files_arg(&s); handle_ignored_arg(&s); diff --git a/t/t7508-status.sh b/t/t7508-status.sh index e1f11293e22990..9fd0ee22283273 100755 --- a/t/t7508-status.sh +++ b/t/t7508-status.sh @@ -1669,6 +1669,17 @@ test_expect_success '"Initial commit" should not be noted in commit template' ' test_i18ngrep ! "Initial commit" output ' +test_expect_success '--no-lock-index prevents index update and is deprecated' ' + test-tool chmtime =1234567890 .git/index && + git status --no-lock-index 2>err && + grep "no-lock-index is deprecated" err && + test-tool chmtime -v +0 .git/index >out && + grep ^1234567890 out && + git status && + test-tool chmtime -v +0 .git/index >out && + ! grep ^1234567890 out +' + test_expect_success '--no-optional-locks prevents index update' ' test-tool chmtime =1234567890 .git/index && git --no-optional-locks status && From 7f9f9aa4fa29e288ad5871ec0ad08abc94042dfd Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 9 Nov 2017 16:55:59 +0100 Subject: [PATCH 098/406] status: reinstate --show-ignored-directory as a deprecated option It was a bad idea to just remove that option from Git for Windows v2.15.0, as early users of that (still experimental) option would have been puzzled what they are supposed to do now. So let's reintroduce the flag, but make sure to show the user good advice how to fix this going forward. We'll remove this option in a more orderly fashion either in v2.16.0 or in v2.17.0. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/commit.c | 11 ++ t/t7522-status-show-ignored-directory.sh | 149 +++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 t/t7522-status-show-ignored-directory.sh diff --git a/builtin/commit.c b/builtin/commit.c index d7497ac03aeb9a..20c4bf28e45a25 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1295,6 +1295,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) static int no_renames = -1; static const char *rename_score_arg = (const char *)-1; static int no_lock_index = 0; + static int show_ignored_directory = 0; static struct wt_status s; int fd; struct object_id oid; @@ -1332,6 +1333,10 @@ int cmd_status(int argc, const char **argv, const char *prefix) { OPTION_CALLBACK, 'M', "find-renames", &rename_score_arg, N_("n"), N_("detect renames, optionally set similarity index"), PARSE_OPT_OPTARG, opt_parse_rename_score }, + OPT_BOOL(0, "show-ignored-directory", &show_ignored_directory, + N_("(DEPRECATED: use --ignore=matching instead) Only " + "show directories that match an ignore pattern " + "name.")), OPT_BOOL(0, "no-lock-index", &no_lock_index, N_("(DEPRECATED: use `git --no-optional-locks status` " "instead) Do not lock the index")), @@ -1354,6 +1359,12 @@ int cmd_status(int argc, const char **argv, const char *prefix) setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "false", 1); } + if (show_ignored_directory) { + warning("--show-ignored-directory was deprecated, use " + "--ignored=matching instead"); + ignored_arg = "matching"; + } + handle_untracked_files_arg(&s); handle_ignored_arg(&s); diff --git a/t/t7522-status-show-ignored-directory.sh b/t/t7522-status-show-ignored-directory.sh new file mode 100755 index 00000000000000..856c00e43f61ef --- /dev/null +++ b/t/t7522-status-show-ignored-directory.sh @@ -0,0 +1,149 @@ +#!/bin/sh +# +# + +test_description='git status collapse ignored' + +. ./test-lib.sh + + +cat >.gitignore <<\EOF +*.ign +ignored_dir/ +!*.unignore +EOF + +# commit initial ignore file +test_expect_success 'setup initial commit and ignore file' ' + git add . && + test_tick && + git commit -m "Initial commit" +' + +cat >expect <<\EOF +? expect +? output +! dir/ignored/ignored_1.ign +! dir/ignored/ignored_2.ign +! ignored/ignored_1.ign +! ignored/ignored_2.ign +EOF + +# Test status behavior on folder with ignored files +test_expect_success 'setup folder with ignored files' ' + mkdir -p ignored dir/ignored && + touch ignored/ignored_1.ign ignored/ignored_2.ign \ + dir/ignored/ignored_1.ign dir/ignored/ignored_2.ign +' + +test_expect_success 'Verify behavior of status on folders with ignored files' ' + test_when_finished "git clean -fdx" && + git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output && + test_i18ncmp expect output +' + +# Test status bahavior on folder with tracked and ignored files +cat >expect <<\EOF +? expect +? output +! dir/tracked_ignored/ignored_1.ign +! dir/tracked_ignored/ignored_2.ign +! tracked_ignored/ignored_1.ign +! tracked_ignored/ignored_2.ign +EOF + +test_expect_success 'setup folder with tracked & ignored files' ' + mkdir -p tracked_ignored dir/tracked_ignored && + touch tracked_ignored/tracked_1 tracked_ignored/tracked_2 \ + tracked_ignored/ignored_1.ign tracked_ignored/ignored_2.ign \ + dir/tracked_ignored/tracked_1 dir/tracked_ignored/tracked_2 \ + dir/tracked_ignored/ignored_1.ign dir/tracked_ignored/ignored_2.ign && + + git add tracked_ignored/tracked_1 tracked_ignored/tracked_2 \ + dir/tracked_ignored/tracked_1 dir/tracked_ignored/tracked_2 && + test_tick && + git commit -m "commit tracked files" +' + +test_expect_success 'Verify status on folder with tracked & ignored files' ' + test_when_finished "git clean -fdx && git reset HEAD~1 --hard" && + git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output && + test_i18ncmp expect output +' + + +# Test status behavior on folder with untracked and ignored files +cat >expect <<\EOF +? dir/untracked_ignored/untracked_1 +? dir/untracked_ignored/untracked_2 +? expect +? output +? untracked_ignored/untracked_1 +? untracked_ignored/untracked_2 +! dir/untracked_ignored/ignored_1.ign +! dir/untracked_ignored/ignored_2.ign +! untracked_ignored/ignored_1.ign +! untracked_ignored/ignored_2.ign +EOF + +test_expect_success 'setup folder with tracked & ignored files' ' + mkdir -p untracked_ignored dir/untracked_ignored && + touch untracked_ignored/untracked_1 untracked_ignored/untracked_2 \ + untracked_ignored/ignored_1.ign untracked_ignored/ignored_2.ign \ + dir/untracked_ignored/untracked_1 dir/untracked_ignored/untracked_2 \ + dir/untracked_ignored/ignored_1.ign dir/untracked_ignored/ignored_2.ign +' + +test_expect_success 'Verify status on folder with tracked & ignored files' ' + test_when_finished "git clean -fdx" && + git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output && + test_i18ncmp expect output +' + +# Test status behavior on ignored folder +cat >expect <<\EOF +? expect +? output +! ignored_dir/ +EOF + +test_expect_success 'setup folder with tracked & ignored files' ' + mkdir ignored_dir && + touch ignored_dir/ignored_1 ignored_dir/ignored_2 \ + ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign +' + +test_expect_success 'Verify status on folder with tracked & ignored files' ' + test_when_finished "git clean -fdx" && + git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output && + test_i18ncmp expect output +' + +# Test status behavior on ignored folder with tracked file +cat >expect <<\EOF +? expect +? output +! ignored_dir/ignored_1 +! ignored_dir/ignored_1.ign +! ignored_dir/ignored_2 +! ignored_dir/ignored_2.ign +EOF + +test_expect_success 'setup folder with tracked & ignored files' ' + mkdir ignored_dir && + touch ignored_dir/ignored_1 ignored_dir/ignored_2 \ + ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign \ + ignored_dir/tracked && + git add -f ignored_dir/tracked && + test_tick && + git commit -m "Force add file in ignored directory" +' + +test_expect_success 'Verify status on folder with tracked & ignored files' ' + test_when_finished "git clean -fdx && git reset HEAD~1 --hard" && + git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output && + test_i18ncmp expect output +' + +test_done + From c577a5859496acdba0a5972a07371fbe14c05bb8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 9 Nov 2017 18:00:38 +0100 Subject: [PATCH 099/406] status: verify that --show-ignored-directory prints a warning The option is deprecated now, and we better make sure that keeps saying so until we finally remove it. Suggested by Kevin Willford. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t7522-status-show-ignored-directory.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/t/t7522-status-show-ignored-directory.sh b/t/t7522-status-show-ignored-directory.sh index 856c00e43f61ef..af29f8bb4fab72 100755 --- a/t/t7522-status-show-ignored-directory.sh +++ b/t/t7522-status-show-ignored-directory.sh @@ -21,6 +21,7 @@ test_expect_success 'setup initial commit and ignore file' ' ' cat >expect <<\EOF +? err ? expect ? output ! dir/ignored/ignored_1.ign @@ -38,8 +39,9 @@ test_expect_success 'setup folder with ignored files' ' test_expect_success 'Verify behavior of status on folders with ignored files' ' test_when_finished "git clean -fdx" && - git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output && - test_i18ncmp expect output + git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output 2>err && + test_i18ncmp expect output && + grep "deprecated.*use --ignored=matching instead" err ' # Test status bahavior on folder with tracked and ignored files From b4220fce04a3cbe2e8c309ab8845fbd6fac631a5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 26 Jan 2018 16:34:59 +0100 Subject: [PATCH 100/406] mingw: demonstrate that all file handles are inherited by child processes When spawning child processes, we really should be careful which file handles we let them inherit. This is doubly important on Windows, where we cannot rename, delete, or modify files if there is still a file handle open. Sadly, we have to guard this test inside #ifdef WIN32: we need to use the value of the HANDLE directly, and that concept does not exist on Linux/Unix. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/helper/test-run-command.c | 47 +++++++++++++++++++++++++++++++++++++ t/t0061-run-command.sh | 4 ++++ 2 files changed, 51 insertions(+) diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index 2cc93bb69c522d..e1bc58b9569aa7 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -50,11 +50,58 @@ static int task_finished(int result, return 1; } +static int inherit_handle(const char *argv0) +{ + struct child_process cp = CHILD_PROCESS_INIT; + char path[PATH_MAX]; + int tmp; + + /* First, open an inheritable handle */ + xsnprintf(path, sizeof(path), "out-XXXXXX"); + tmp = xmkstemp(path); + + argv_array_pushl(&cp.args, + "test-tool", argv0, "inherited-handle-child", NULL); + cp.in = -1; + cp.no_stdout = cp.no_stderr = 1; + if (start_command(&cp) < 0) + die("Could not start child process"); + + /* Then close it, and try to delete it. */ + close(tmp); + if (unlink(path)) + die("Could not delete '%s'", path); + + if (close(cp.in) < 0 || finish_command(&cp) < 0) + die("Child did not finish"); + + return 0; +} + +static int inherit_handle_child(void) +{ + struct strbuf buf = STRBUF_INIT; + + if (strbuf_read(&buf, 0, 0) < 0) + die("Could not read stdin"); + printf("Received %s\n", buf.buf); + strbuf_release(&buf); + + return 0; +} + int cmd__run_command(int argc, const char **argv) { struct child_process proc = CHILD_PROCESS_INIT; int jobs; + if (argc < 2) + return 1; + if (!strcmp(argv[1], "inherited-handle")) + exit(inherit_handle(argv[0])); + if (!strcmp(argv[1], "inherited-handle-child")) + exit(inherit_handle_child()); + if (argc < 3) return 1; while (!strcmp(argv[1], "env")) { diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 3e131c5325e363..d800c0a4d1fd83 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -12,6 +12,10 @@ cat >hello-script <<-EOF cat hello-script EOF +test_expect_failure MINGW 'subprocess inherits only std handles' ' + test-tool run-command inherited-handle +' + test_expect_success 'start_command reports ENOENT' ' test-tool run-command start-command-ENOENT ./does-not-exist ' From faa912625a6cc23082b40e868b46103aae9c5a6d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 7 Feb 2018 13:50:03 +0100 Subject: [PATCH 101/406] mingw: work around incorrect standard handles For some reason, when being called via TortoiseGit the standard handles, or at least what is returned by _get_osfhandle(0) for standard input, can take on the value (HANDLE)-2 (which is not a legal value, according to the documentation). Even if this value is not documented anywhere, CreateProcess() seems to work fine without complaints if hStdInput set to this value. In contrast, the upcoming code to restrict which file handles get inherited by spawned processes would result in `ERROR_INVALID_PARAMETER` when including such handle values in the list. To help this, special-case the value (HANDLE)-2 returned by _get_osfhandle() and replace it with INVALID_HANDLE_VALUE, which will hopefully let the handle inheritance restriction work even when called from TortoiseGit. This fixes https://github.com/git-for-windows/git/issues/1481 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/winansi.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/compat/winansi.c b/compat/winansi.c index a11a0f16d27647..f5dcc8de70f797 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -639,10 +639,20 @@ void winansi_init(void) */ HANDLE winansi_get_osfhandle(int fd) { + HANDLE ret; + if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED)) return hconsole1; if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED)) return hconsole2; - return (HANDLE)_get_osfhandle(fd); + ret = (HANDLE)_get_osfhandle(fd); + + /* + * There are obviously circumstances under which _get_osfhandle() + * returns (HANDLE)-2. This is not documented anywhere, but that is so + * clearly an invalid handle value that we can just work around this + * and return the correct value for invalid handles. + */ + return ret == (HANDLE)-2 ? INVALID_HANDLE_VALUE : ret; } From f15d09a1f582b3d769a81432667f122cd99a74c7 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:07 -0700 Subject: [PATCH 102/406] builtin rebase: support --onto The `--onto` option is important, as it allows to rebase a range of commits onto a different base commit (which gave the command its odd name: "rebase"). This commit introduces options parsing so that different options can be added in future commits. Note: As this commit introduces to the parse_options() call (which "eats" argv[0]), the argc is now expected to be lower by one after this patch, compared to before this patch: argv[0] no longer refers to the command name, but to the first (non-option) command-line parameter. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index e695d8a430f155..742ed3149824b8 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -16,6 +16,16 @@ #include "cache-tree.h" #include "unpack-trees.h" #include "lockfile.h" +#include "parse-options.h" + +static char const * const builtin_rebase_usage[] = { + N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " + "[<upstream>] [<branch>]"), + N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " + "--root [<branch>]"), + N_("git rebase --continue | --abort | --skip | --edit-todo"), + NULL +}; static GIT_PATH_FUNC(apply_dir, "rebase-apply") static GIT_PATH_FUNC(merge_dir, "rebase-merge") @@ -301,6 +311,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) int ret, flags; struct strbuf msg = STRBUF_INIT; struct strbuf revisions = STRBUF_INIT; + struct option builtin_rebase_options[] = { + OPT_STRING(0, "onto", &options.onto_name, + N_("revision"), + N_("rebase onto given branch instead of upstream")), + OPT_END(), + }; /* * NEEDSWORK: Once the builtin rebase has been tested enough @@ -318,13 +334,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) BUG("sane_execvp() returned???"); } - if (argc != 2) - die(_("Usage: %s <base>"), argv[0]); + if (argc == 2 && !strcmp(argv[1], "-h")) + usage_with_options(builtin_rebase_usage, + builtin_rebase_options); + prefix = setup_git_directory(); trace_repo_setup(prefix); setup_work_tree(); git_config(git_default_config, NULL); + argc = parse_options(argc, argv, prefix, + builtin_rebase_options, + builtin_rebase_usage, 0); + + if (argc > 2) + usage_with_options(builtin_rebase_usage, + builtin_rebase_options); switch (options.type) { case REBASE_MERGE: @@ -343,10 +368,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } if (!options.root) { - if (argc < 2) + if (argc < 1) die("TODO: handle @{upstream}"); else { - options.upstream_name = argv[1]; + options.upstream_name = argv[0]; argc--; argv++; if (!strcmp(options.upstream_name, "-")) @@ -377,7 +402,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) * orig_head -- commit object name of tip of the branch before rebasing * head_name -- refs/heads/<that-branch> or "detached HEAD" */ - if (argc > 1) + if (argc > 0) die("TODO: handle switch_to"); else { /* Do not need to switch branches, we are already on it. */ From 97dbf36d65b085e3da427a0cabb3436aced87508 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:09 -0700 Subject: [PATCH 103/406] builtin rebase: support `git rebase --onto A...B` This commit implements support for an --onto argument that is actually a "symmetric range" i.e. `<rev1>...<rev2>`. The equivalent shell script version of the code offers two different error messages for the cases where there is no merge base vs more than one merge base. Though it would be nice to retain this distinction, dropping it makes it possible to simply use the `get_oid_mb()` function. Besides, it happens rarely in real-world scenarios. Therefore, in the interest of keeping the code less complex, let's just use that function, and live with an error message that does not distinguish between those two error conditions. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 742ed3149824b8..38c496dd10baea 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -17,6 +17,7 @@ #include "unpack-trees.h" #include "lockfile.h" #include "parse-options.h" +#include "commit.h" static char const * const builtin_rebase_usage[] = { N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " @@ -311,6 +312,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) int ret, flags; struct strbuf msg = STRBUF_INIT; struct strbuf revisions = STRBUF_INIT; + struct object_id merge_base; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -387,7 +389,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (!options.onto_name) options.onto_name = options.upstream_name; if (strstr(options.onto_name, "...")) { - die("TODO"); + if (get_oid_mb(options.onto_name, &merge_base) < 0) + die(_("'%s': need exactly one merge base"), + options.onto_name); + options.onto = lookup_commit_or_die(&merge_base, + options.onto_name); } else { options.onto = peel_committish(options.onto_name); if (!options.onto) From 8ae8bde018b7e957b4b7e4367f1bd066df9f8985 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:10 -0700 Subject: [PATCH 104/406] builtin rebase: handle the pre-rebase hook and --no-verify This commit converts the equivalent part of the shell script `git-legacy-rebase.sh` to run the pre-rebase hook (unless disabled), and to interrupt the rebase with error if the hook fails. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 38c496dd10baea..b79f9b0a9fe4e7 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -70,6 +70,7 @@ struct rebase_options { const char *state_dir; struct commit *upstream; const char *upstream_name; + const char *upstream_arg; char *head_name; struct object_id orig_head; struct commit *onto; @@ -310,6 +311,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) }; const char *branch_name; int ret, flags; + int ok_to_skip_pre_rebase = 0; struct strbuf msg = STRBUF_INIT; struct strbuf revisions = STRBUF_INIT; struct object_id merge_base; @@ -317,6 +319,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_STRING(0, "onto", &options.onto_name, N_("revision"), N_("rebase onto given branch instead of upstream")), + OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, + N_("allow pre-rebase hook to run")), OPT_END(), }; @@ -382,6 +386,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.upstream = peel_committish(options.upstream_name); if (!options.upstream) die(_("invalid upstream '%s'"), options.upstream_name); + options.upstream_arg = options.upstream_name; } else die("TODO: upstream for --root"); @@ -430,6 +435,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("Could not resolve HEAD to a revision")); } + /* If a hook exists, give it a chance to interrupt*/ + if (!ok_to_skip_pre_rebase && + run_hook_le(NULL, "pre-rebase", options.upstream_arg, + argc ? argv[0] : NULL, NULL)) + die(_("The pre-rebase hook refused to rebase.")); + strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) die(_("Could not detach HEAD")); From 6910ae7edbd1f3c281de4be92f2876713645f68d Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:12 -0700 Subject: [PATCH 105/406] builtin rebase: support --quiet This commit introduces a rebase option `--quiet`. While `--quiet` is commonly perceived as opposite to `--verbose`, this is not the case for the rebase command: both `--quiet` and `--verbose` default to `false` if neither `--quiet` nor `--verbose` is present. Despite the default being `false` for both verbose and quiet mode, passing the `--quiet` option will turn off verbose mode, and `--verbose` will turn off quiet mode. This patch introduces the `flags` bit field, with `REBASE_NO_QUIET` as first user (with many more to come). We do *not* use `REBASE_QUIET` here for an important reason: To keep the implementation simple, this commit introduces `--no-quiet` instead of `--quiet`, so that a single `OPT_NEGBIT()` can turn on quiet mode and turn off verbose and diffstat mode at the same time. Likewise, the companion commit which will introduce support for `--verbose` will have a single `OPT_BIT()` that turns off quiet mode and turns on verbose and diffstat mode at the same time. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index b79f9b0a9fe4e7..19fa4d3fc488c9 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -79,6 +79,10 @@ struct rebase_options { int root; struct commit *restrict_revision; int dont_finish_rebase; + enum { + REBASE_NO_QUIET = 1<<0, + } flags; + struct strbuf git_am_opt; }; /* Returns the filename prefixed by the state_dir */ @@ -159,6 +163,9 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "revisions", opts->revisions); add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? oid_to_hex(&opts->restrict_revision->object.oid) : NULL); + add_var(&script_snippet, "GIT_QUIET", + opts->flags & REBASE_NO_QUIET ? "" : "t"); + add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf); switch (opts->type) { case REBASE_AM: @@ -308,6 +315,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) { struct rebase_options options = { .type = REBASE_UNSPECIFIED, + .flags = REBASE_NO_QUIET, + .git_am_opt = STRBUF_INIT, }; const char *branch_name; int ret, flags; @@ -321,6 +330,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) N_("rebase onto given branch instead of upstream")), OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, N_("allow pre-rebase hook to run")), + OPT_NEGBIT('q', "quiet", &options.flags, + N_("be quiet. implies --no-stat"), + REBASE_NO_QUIET), OPT_END(), }; @@ -357,6 +369,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) usage_with_options(builtin_rebase_usage, builtin_rebase_options); + if (!(options.flags & REBASE_NO_QUIET)) + strbuf_addstr(&options.git_am_opt, " -q"); + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From 0627f7dc28cda640ae313443e154ccbb70d5c9db Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 7 Aug 2018 01:16:09 +0545 Subject: [PATCH 106/406] rebase: start implementing it as a builtin This commit imitates the strategy that was used to convert the difftool to a builtin. We start by renaming the shell script `git-rebase.sh` to `git-legacy-rebase.sh` and introduce a `builtin/rebase.c` that simply executes the shell script version, unless the config setting `rebase.useBuiltin` is set to `true`. The motivation behind this is to rewrite all the functionality of the shell script version in the aforementioned `rebase.c`, one by one and be able to conveniently test new features by configuring `rebase.useBuiltin`. In the original difftool conversion, if sane_execvp() that attempts to run the legacy scripted version returned with non-negative status, the command silently exited without doing anything with success, but sane_execvp() should not return with non-negative status in the first place, so we use die() to notice such an abnormal case. We intentionally avoid reading the config directly to avoid messing up the GIT_* environment variables when we need to fall back to exec()ing the shell script. The test of builtin rebase can be done by `git -c rebase.useBuiltin=true rebase ...` Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- .gitignore | 1 + Makefile | 3 +- builtin.h | 1 + builtin/rebase.c | 58 +++++++++++++++++++++++++++ git-rebase.sh => git-legacy-rebase.sh | 0 git.c | 6 +++ 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 builtin/rebase.c rename git-rebase.sh => git-legacy-rebase.sh (100%) diff --git a/.gitignore b/.gitignore index ffceea7d59fd21..9afa700ba128fc 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ /git-init-db /git-interpret-trailers /git-instaweb +/git-legacy-rebase /git-log /git-ls-files /git-ls-remote diff --git a/Makefile b/Makefile index 5a969f5830a410..7e503758a5b223 100644 --- a/Makefile +++ b/Makefile @@ -614,7 +614,7 @@ SCRIPT_SH += git-merge-one-file.sh SCRIPT_SH += git-merge-resolve.sh SCRIPT_SH += git-mergetool.sh SCRIPT_SH += git-quiltimport.sh -SCRIPT_SH += git-rebase.sh +SCRIPT_SH += git-legacy-rebase.sh SCRIPT_SH += git-remote-testgit.sh SCRIPT_SH += git-request-pull.sh SCRIPT_SH += git-stash.sh @@ -1073,6 +1073,7 @@ BUILTIN_OBJS += builtin/pull.o BUILTIN_OBJS += builtin/push.o BUILTIN_OBJS += builtin/range-diff.o BUILTIN_OBJS += builtin/read-tree.o +BUILTIN_OBJS += builtin/rebase.o BUILTIN_OBJS += builtin/rebase--helper.o BUILTIN_OBJS += builtin/receive-pack.o BUILTIN_OBJS += builtin/reflog.o diff --git a/builtin.h b/builtin.h index 99206df4bd43fc..db5767a39755ea 100644 --- a/builtin.h +++ b/builtin.h @@ -203,6 +203,7 @@ extern int cmd_pull(int argc, const char **argv, const char *prefix); extern int cmd_push(int argc, const char **argv, const char *prefix); extern int cmd_range_diff(int argc, const char **argv, const char *prefix); extern int cmd_read_tree(int argc, const char **argv, const char *prefix); +extern int cmd_rebase(int argc, const char **argv, const char *prefix); extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix); extern int cmd_receive_pack(int argc, const char **argv, const char *prefix); extern int cmd_reflog(int argc, const char **argv, const char *prefix); diff --git a/builtin/rebase.c b/builtin/rebase.c new file mode 100644 index 00000000000000..c44addb2a4c609 --- /dev/null +++ b/builtin/rebase.c @@ -0,0 +1,58 @@ +/* + * "git rebase" builtin command + * + * Copyright (c) 2018 Pratik Karki + */ + +#include "builtin.h" +#include "run-command.h" +#include "exec-cmd.h" +#include "argv-array.h" +#include "dir.h" + +static int use_builtin_rebase(void) +{ + struct child_process cp = CHILD_PROCESS_INIT; + struct strbuf out = STRBUF_INIT; + int ret; + + argv_array_pushl(&cp.args, + "config", "--bool", "rebase.usebuiltin", NULL); + cp.git_cmd = 1; + if (capture_command(&cp, &out, 6)) { + strbuf_release(&out); + return 0; + } + + strbuf_trim(&out); + ret = !strcmp("true", out.buf); + strbuf_release(&out); + return ret; +} + +int cmd_rebase(int argc, const char **argv, const char *prefix) +{ + /* + * NEEDSWORK: Once the builtin rebase has been tested enough + * and git-legacy-rebase.sh is retired to contrib/, this preamble + * can be removed. + */ + + if (!use_builtin_rebase()) { + const char *path = mkpath("%s/git-legacy-rebase", + git_exec_path()); + + if (sane_execvp(path, (char **)argv) < 0) + die_errno(_("could not exec %s"), path); + else + BUG("sane_execvp() returned???"); + } + + if (argc != 2) + die(_("Usage: %s <base>"), argv[0]); + prefix = setup_git_directory(); + trace_repo_setup(prefix); + setup_work_tree(); + + die("TODO"); +} diff --git a/git-rebase.sh b/git-legacy-rebase.sh similarity index 100% rename from git-rebase.sh rename to git-legacy-rebase.sh diff --git a/git.c b/git.c index c27c38738b2a9d..adb587f75e6833 100644 --- a/git.c +++ b/git.c @@ -522,6 +522,12 @@ static struct cmd_struct commands[] = { { "push", cmd_push, RUN_SETUP }, { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX}, + /* + * NEEDSWORK: Until the rebase is independent and needs no redirection + * to rebase shell script this is kept as is, then should be changed to + * RUN_SETUP | NEED_WORK_TREE + */ + { "rebase", cmd_rebase }, { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE }, { "receive-pack", cmd_receive_pack }, { "reflog", cmd_reflog, RUN_SETUP }, From 12dd4b9799606fcf438bec40f0bd33461486be22 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:13 -0700 Subject: [PATCH 107/406] builtin rebase: support the `verbose` and `diffstat` options This commit introduces support for the `-v` and `--stat` options of rebase. The --stat option can also be configured via the Git config setting rebase.stat. To support this, we also add a custom rebase_config() function in this commit that will be used instead of (and falls back to calling) git_default_config(). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 19fa4d3fc488c9..2d3f1d65fb5e1c 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -18,6 +18,7 @@ #include "lockfile.h" #include "parse-options.h" #include "commit.h" +#include "diff.h" static char const * const builtin_rebase_usage[] = { N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " @@ -81,6 +82,8 @@ struct rebase_options { int dont_finish_rebase; enum { REBASE_NO_QUIET = 1<<0, + REBASE_VERBOSE = 1<<1, + REBASE_DIFFSTAT = 1<<2, } flags; struct strbuf git_am_opt; }; @@ -166,6 +169,10 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "GIT_QUIET", opts->flags & REBASE_NO_QUIET ? "" : "t"); add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf); + add_var(&script_snippet, "verbose", + opts->flags & REBASE_VERBOSE ? "t" : ""); + add_var(&script_snippet, "diffstat", + opts->flags & REBASE_DIFFSTAT ? "t" : ""); switch (opts->type) { case REBASE_AM: @@ -311,6 +318,21 @@ static int reset_head(struct object_id *oid, const char *action, return ret; } +static int rebase_config(const char *var, const char *value, void *data) +{ + struct rebase_options *opts = data; + + if (!strcmp(var, "rebase.stat")) { + if (git_config_bool(var, value)) + opts->flags |= REBASE_DIFFSTAT; + else + opts->flags &= !REBASE_DIFFSTAT; + return 0; + } + + return git_default_config(var, value, data); +} + int cmd_rebase(int argc, const char **argv, const char *prefix) { struct rebase_options options = { @@ -332,7 +354,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) N_("allow pre-rebase hook to run")), OPT_NEGBIT('q', "quiet", &options.flags, N_("be quiet. implies --no-stat"), - REBASE_NO_QUIET), + REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT), + OPT_BIT('v', "verbose", &options.flags, + N_("display a diffstat of what changed upstream"), + REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), + {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, + N_("do not show diffstat of what changed upstream"), + PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, OPT_END(), }; @@ -360,7 +388,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) trace_repo_setup(prefix); setup_work_tree(); - git_config(git_default_config, NULL); + git_config(rebase_config, &options); + argc = parse_options(argc, argv, prefix, builtin_rebase_options, builtin_rebase_usage, 0); @@ -456,6 +485,33 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) argc ? argv[0] : NULL, NULL)) die(_("The pre-rebase hook refused to rebase.")); + if (options.flags & REBASE_DIFFSTAT) { + struct diff_options opts; + + if (options.flags & REBASE_VERBOSE) + printf(_("Changes from %s to %s:\n"), + oid_to_hex(&merge_base), + oid_to_hex(&options.onto->object.oid)); + + /* We want color (if set), but no pager */ + diff_setup(&opts); + opts.stat_width = -1; /* use full terminal width */ + opts.stat_graph_width = -1; /* respect statGraphWidth config */ + opts.output_format |= + DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; + opts.detect_rename = DIFF_DETECT_RENAME; + diff_setup_done(&opts); + diff_tree_oid(&merge_base, &options.onto->object.oid, + "", &opts); + diffcore_std(&opts); + diff_flush(&opts); + } + + /* Detach HEAD and reset the tree */ + if (options.flags & REBASE_NO_QUIET) + printf(_("First, rewinding head to replay your work on top of " + "it...\n")); + strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) die(_("Could not detach HEAD")); From 43594e9f2bc666b746eda415c22ed115aad92033 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 7 Aug 2018 01:16:10 +0545 Subject: [PATCH 108/406] rebase: refactor common shell functions into their own file The functions present in `git-legacy-rebase.sh` are used by the rebase backends as they are implemented as shell script functions in the `git-rebase--<backend>` files. To make the `builtin/rebase.c` work, we have to provide support via a Unix shell script snippet that uses these functions and so, we want to use the rebase backends *directly* from the builtin rebase without going through `git-legacy-rebase.sh`. This commit extracts the functions to a separate file, `git-rebase--common`, that will be read by `git-legacy-rebase.sh` and by the shell script snippets which will be used extensively in the following commits. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- .gitignore | 1 + Makefile | 1 + git-legacy-rebase.sh | 69 ++----------------------------------------- git-rebase--common.sh | 68 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 67 deletions(-) create mode 100644 git-rebase--common.sh diff --git a/.gitignore b/.gitignore index 9afa700ba128fc..3a1eae853567f7 100644 --- a/.gitignore +++ b/.gitignore @@ -118,6 +118,7 @@ /git-read-tree /git-rebase /git-rebase--am +/git-rebase--common /git-rebase--helper /git-rebase--interactive /git-rebase--merge diff --git a/Makefile b/Makefile index 7e503758a5b223..318979ab509ef1 100644 --- a/Makefile +++ b/Makefile @@ -624,6 +624,7 @@ SCRIPT_SH += git-web--browse.sh SCRIPT_LIB += git-mergetool--lib SCRIPT_LIB += git-parse-remote SCRIPT_LIB += git-rebase--am +SCRIPT_LIB += git-rebase--common SCRIPT_LIB += git-rebase--interactive SCRIPT_LIB += git-rebase--preserve-merges SCRIPT_LIB += git-rebase--merge diff --git a/git-legacy-rebase.sh b/git-legacy-rebase.sh index 797344764510e0..af2cdfef03d1a7 100755 --- a/git-legacy-rebase.sh +++ b/git-legacy-rebase.sh @@ -57,12 +57,7 @@ cd_to_toplevel LF=' ' ok_to_skip_pre_rebase= -resolvemsg=" -$(gettext 'Resolve all conflicts manually, mark them as resolved with -"git add/rm <conflicted_files>", then run "git rebase --continue". -You can instead skip this commit: run "git rebase --skip". -To abort and get back to the state before "git rebase", run "git rebase --abort".') -" + squash_onto= unset onto unset restrict_revision @@ -102,6 +97,7 @@ case "$(git config --bool commit.gpgsign)" in true) gpg_sign_opt=-S ;; *) gpg_sign_opt= ;; esac +. git-rebase--common read_basic_state () { test -f "$state_dir/head-name" && @@ -132,67 +128,6 @@ read_basic_state () { } } -write_basic_state () { - echo "$head_name" > "$state_dir"/head-name && - echo "$onto" > "$state_dir"/onto && - echo "$orig_head" > "$state_dir"/orig-head && - echo "$GIT_QUIET" > "$state_dir"/quiet && - test t = "$verbose" && : > "$state_dir"/verbose - test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy - test -n "$strategy_opts" && echo "$strategy_opts" > \ - "$state_dir"/strategy_opts - test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \ - "$state_dir"/allow_rerere_autoupdate - test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt - test -n "$signoff" && echo "$signoff" >"$state_dir"/signoff -} - -output () { - case "$verbose" in - '') - output=$("$@" 2>&1 ) - status=$? - test $status != 0 && printf "%s\n" "$output" - return $status - ;; - *) - "$@" - ;; - esac -} - -move_to_original_branch () { - case "$head_name" in - refs/*) - message="rebase finished: $head_name onto $onto" - git update-ref -m "$message" \ - $head_name $(git rev-parse HEAD) $orig_head && - git symbolic-ref \ - -m "rebase finished: returning to $head_name" \ - HEAD $head_name || - die "$(eval_gettext "Could not move back to \$head_name")" - ;; - esac -} - -apply_autostash () { - if test -f "$state_dir/autostash" - then - stash_sha1=$(cat "$state_dir/autostash") - if git stash apply $stash_sha1 >/dev/null 2>&1 - then - echo "$(gettext 'Applied autostash.')" >&2 - else - git stash store -m "autostash" -q $stash_sha1 || - die "$(eval_gettext "Cannot store \$stash_sha1")" - gettext 'Applying autostash resulted in conflicts. -Your changes are safe in the stash. -You can run "git stash pop" or "git stash drop" at any time. -' >&2 - fi - fi -} - finish_rebase () { rm -f "$(git rev-parse --git-path REBASE_HEAD)" apply_autostash && diff --git a/git-rebase--common.sh b/git-rebase--common.sh new file mode 100644 index 00000000000000..7e39d228717899 --- /dev/null +++ b/git-rebase--common.sh @@ -0,0 +1,68 @@ + +resolvemsg=" +$(gettext 'Resolve all conflicts manually, mark them as resolved with +"git add/rm <conflicted_files>", then run "git rebase --continue". +You can instead skip this commit: run "git rebase --skip". +To abort and get back to the state before "git rebase", run "git rebase --abort".') +" + +write_basic_state () { + echo "$head_name" > "$state_dir"/head-name && + echo "$onto" > "$state_dir"/onto && + echo "$orig_head" > "$state_dir"/orig-head && + echo "$GIT_QUIET" > "$state_dir"/quiet && + test t = "$verbose" && : > "$state_dir"/verbose + test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy + test -n "$strategy_opts" && echo "$strategy_opts" > \ + "$state_dir"/strategy_opts + test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \ + "$state_dir"/allow_rerere_autoupdate + test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt + test -n "$signoff" && echo "$signoff" >"$state_dir"/signoff +} + +apply_autostash () { + if test -f "$state_dir/autostash" + then + stash_sha1=$(cat "$state_dir/autostash") + if git stash apply $stash_sha1 >/dev/null 2>&1 + then + echo "$(gettext 'Applied autostash.')" >&2 + else + git stash store -m "autostash" -q $stash_sha1 || + die "$(eval_gettext "Cannot store \$stash_sha1")" + gettext 'Applying autostash resulted in conflicts. +Your changes are safe in the stash. +You can run "git stash pop" or "git stash drop" at any time. +' >&2 + fi + fi +} + +move_to_original_branch () { + case "$head_name" in + refs/*) + message="rebase finished: $head_name onto $onto" + git update-ref -m "$message" \ + $head_name $(git rev-parse HEAD) $orig_head && + git symbolic-ref \ + -m "rebase finished: returning to $head_name" \ + HEAD $head_name || + die "$(eval_gettext "Could not move back to \$head_name")" + ;; + esac +} + +output () { + case "$verbose" in + '') + output=$("$@" 2>&1 ) + status=$? + test $status != 0 && printf "%s\n" "$output" + return $status + ;; + *) + "$@" + ;; + esac +} From 4cd4c180a833cad54ab94b4bec2a31bdb867c591 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:14 -0700 Subject: [PATCH 109/406] builtin rebase: require a clean worktree This commit reads the index of the repository for rebase and checks whether the repository is ready for rebase. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 2d3f1d65fb5e1c..afef0b00462239 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -19,6 +19,7 @@ #include "parse-options.h" #include "commit.h" #include "diff.h" +#include "wt-status.h" static char const * const builtin_rebase_usage[] = { N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " @@ -479,6 +480,15 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("Could not resolve HEAD to a revision")); } + if (read_index(the_repository->index) < 0) + die(_("could not read index")); + + if (require_clean_work_tree("rebase", + _("Please commit or stash them."), 1, 1)) { + ret = 1; + goto cleanup; + } + /* If a hook exists, give it a chance to interrupt*/ if (!ok_to_skip_pre_rebase && run_hook_le(NULL, "pre-rebase", options.upstream_arg, @@ -528,6 +538,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ret = !!run_specific_rebase(&options); +cleanup: strbuf_release(&revisions); free(options.head_name); return ret; From 544236d078aeaf73cddc5af98dbc2c8cb51b1f80 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 7 Aug 2018 01:16:11 +0545 Subject: [PATCH 110/406] builtin/rebase: support running "git rebase <upstream>" This patch gives life to the skeleton added in the previous patches: With this change, we can perform a elementary rebase (without any options). It can be tested thusly by: git -c rebase.usebuiltin=true rebase HEAD~2 The rebase backends (i.e. the shell script functions defined in `git-rebase--<backend>`) are still at work here and the "builtin rebase"'s purpose is simply to parse the options and set everything up so that those rebase backends can do their work. Note: We take an alternative approach here which is *not* to set the environment variables via `run_command_v_opt_cd_env()` because those variables would then be visible by processes spawned from the rebase backends. Instead, we work hard to set them in the shell script snippet. On Windows, some of the tests fail merely due to core.fileMode not being heeded that's why the core.*config variables is parsed here. The `reset_head()` function is currently only used to detach the HEAD to onto by way of starting the rebase, but it offers additional functionality that subsequent patches will need like moving to the original branch (possibly updating it) and also to do the equivalent of `git reset --hard`. The next commits will handle and support all the wonderful rebase options. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 365 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 364 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index c44addb2a4c609..e695d8a430f155 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -9,6 +9,24 @@ #include "exec-cmd.h" #include "argv-array.h" #include "dir.h" +#include "packfile.h" +#include "refs.h" +#include "quote.h" +#include "config.h" +#include "cache-tree.h" +#include "unpack-trees.h" +#include "lockfile.h" + +static GIT_PATH_FUNC(apply_dir, "rebase-apply") +static GIT_PATH_FUNC(merge_dir, "rebase-merge") + +enum rebase_type { + REBASE_UNSPECIFIED = -1, + REBASE_AM, + REBASE_MERGE, + REBASE_INTERACTIVE, + REBASE_PRESERVE_MERGES +}; static int use_builtin_rebase(void) { @@ -30,8 +48,260 @@ static int use_builtin_rebase(void) return ret; } +static int apply_autostash(void) +{ + warning("TODO"); + return 0; +} + +struct rebase_options { + enum rebase_type type; + const char *state_dir; + struct commit *upstream; + const char *upstream_name; + char *head_name; + struct object_id orig_head; + struct commit *onto; + const char *onto_name; + const char *revisions; + int root; + struct commit *restrict_revision; + int dont_finish_rebase; +}; + +/* Returns the filename prefixed by the state_dir */ +static const char *state_dir_path(const char *filename, struct rebase_options *opts) +{ + static struct strbuf path = STRBUF_INIT; + static size_t prefix_len; + + if (!prefix_len) { + strbuf_addf(&path, "%s/", opts->state_dir); + prefix_len = path.len; + } + + strbuf_setlen(&path, prefix_len); + strbuf_addstr(&path, filename); + return path.buf; +} + +static int finish_rebase(struct rebase_options *opts) +{ + struct strbuf dir = STRBUF_INIT; + const char *argv_gc_auto[] = { "gc", "--auto", NULL }; + + delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); + apply_autostash(); + close_all_packs(the_repository->objects); + /* + * We ignore errors in 'gc --auto', since the + * user should see them. + */ + run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); + strbuf_addstr(&dir, opts->state_dir); + remove_dir_recursively(&dir, 0); + strbuf_release(&dir); + + return 0; +} + +static struct commit *peel_committish(const char *name) +{ + struct object *obj; + struct object_id oid; + + if (get_oid(name, &oid)) + return NULL; + obj = parse_object(the_repository, &oid); + return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); +} + +static void add_var(struct strbuf *buf, const char *name, const char *value) +{ + if (!value) + strbuf_addf(buf, "unset %s; ", name); + else { + strbuf_addf(buf, "%s=", name); + sq_quote_buf(buf, value); + strbuf_addstr(buf, "; "); + } +} + +static int run_specific_rebase(struct rebase_options *opts) +{ + const char *argv[] = { NULL, NULL }; + struct strbuf script_snippet = STRBUF_INIT; + int status; + const char *backend, *backend_func; + + add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); + add_var(&script_snippet, "state_dir", opts->state_dir); + + add_var(&script_snippet, "upstream_name", opts->upstream_name); + add_var(&script_snippet, "upstream", + oid_to_hex(&opts->upstream->object.oid)); + add_var(&script_snippet, "head_name", opts->head_name); + add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); + add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid)); + add_var(&script_snippet, "onto_name", opts->onto_name); + add_var(&script_snippet, "revisions", opts->revisions); + add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? + oid_to_hex(&opts->restrict_revision->object.oid) : NULL); + + switch (opts->type) { + case REBASE_AM: + backend = "git-rebase--am"; + backend_func = "git_rebase__am"; + break; + case REBASE_INTERACTIVE: + backend = "git-rebase--interactive"; + backend_func = "git_rebase__interactive"; + break; + case REBASE_MERGE: + backend = "git-rebase--merge"; + backend_func = "git_rebase__merge"; + break; + case REBASE_PRESERVE_MERGES: + backend = "git-rebase--preserve-merges"; + backend_func = "git_rebase__preserve_merges"; + break; + default: + BUG("Unhandled rebase type %d", opts->type); + break; + } + + strbuf_addf(&script_snippet, + ". git-sh-setup && . git-rebase--common &&" + " . %s && %s", backend, backend_func); + argv[0] = script_snippet.buf; + + status = run_command_v_opt(argv, RUN_USING_SHELL); + if (opts->dont_finish_rebase) + ; /* do nothing */ + else if (status == 0) { + if (!file_exists(state_dir_path("stopped-sha", opts))) + finish_rebase(opts); + } else if (status == 2) { + struct strbuf dir = STRBUF_INIT; + + apply_autostash(); + strbuf_addstr(&dir, opts->state_dir); + remove_dir_recursively(&dir, 0); + strbuf_release(&dir); + die("Nothing to do"); + } + + strbuf_release(&script_snippet); + + return status ? -1 : 0; +} + +#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" + +static int reset_head(struct object_id *oid, const char *action, + const char *switch_to_branch, int detach_head) +{ + struct object_id head_oid; + struct tree_desc desc; + struct lock_file lock = LOCK_INIT; + struct unpack_trees_options unpack_tree_opts; + struct tree *tree; + const char *reflog_action; + struct strbuf msg = STRBUF_INIT; + size_t prefix_len; + struct object_id *orig = NULL, oid_orig, + *old_orig = NULL, oid_old_orig; + int ret = 0; + + if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) + return -1; + + if (!oid) { + if (get_oid("HEAD", &head_oid)) { + rollback_lock_file(&lock); + return error(_("could not determine HEAD revision")); + } + oid = &head_oid; + } + + memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); + setup_unpack_trees_porcelain(&unpack_tree_opts, action); + unpack_tree_opts.head_idx = 1; + unpack_tree_opts.src_index = the_repository->index; + unpack_tree_opts.dst_index = the_repository->index; + unpack_tree_opts.fn = oneway_merge; + unpack_tree_opts.update = 1; + unpack_tree_opts.merge = 1; + if (!detach_head) + unpack_tree_opts.reset = 1; + + if (read_index_unmerged(the_repository->index) < 0) { + rollback_lock_file(&lock); + return error(_("could not read index")); + } + + if (!fill_tree_descriptor(&desc, oid)) { + error(_("failed to find tree of %s"), oid_to_hex(oid)); + rollback_lock_file(&lock); + free((void *)desc.buffer); + return -1; + } + + if (unpack_trees(1, &desc, &unpack_tree_opts)) { + rollback_lock_file(&lock); + free((void *)desc.buffer); + return -1; + } + + tree = parse_tree_indirect(oid); + prime_cache_tree(the_repository->index, tree); + + if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) + ret = error(_("could not write index")); + free((void *)desc.buffer); + + if (ret) + return ret; + + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); + prefix_len = msg.len; + + if (!get_oid("ORIG_HEAD", &oid_old_orig)) + old_orig = &oid_old_orig; + if (!get_oid("HEAD", &oid_orig)) { + orig = &oid_orig; + strbuf_addstr(&msg, "updating ORIG_HEAD"); + update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, + UPDATE_REFS_MSG_ON_ERR); + } else if (old_orig) + delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + strbuf_setlen(&msg, prefix_len); + strbuf_addstr(&msg, "updating HEAD"); + if (!switch_to_branch) + ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF, + UPDATE_REFS_MSG_ON_ERR); + else { + ret = create_symref("HEAD", switch_to_branch, msg.buf); + if (!ret) + ret = update_ref(msg.buf, "HEAD", oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR); + } + + strbuf_release(&msg); + return ret; +} + int cmd_rebase(int argc, const char **argv, const char *prefix) { + struct rebase_options options = { + .type = REBASE_UNSPECIFIED, + }; + const char *branch_name; + int ret, flags; + struct strbuf msg = STRBUF_INIT; + struct strbuf revisions = STRBUF_INIT; + /* * NEEDSWORK: Once the builtin rebase has been tested enough * and git-legacy-rebase.sh is retired to contrib/, this preamble @@ -54,5 +324,98 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) trace_repo_setup(prefix); setup_work_tree(); - die("TODO"); + git_config(git_default_config, NULL); + + switch (options.type) { + case REBASE_MERGE: + case REBASE_INTERACTIVE: + case REBASE_PRESERVE_MERGES: + options.state_dir = merge_dir(); + break; + case REBASE_AM: + options.state_dir = apply_dir(); + break; + default: + /* the default rebase backend is `--am` */ + options.type = REBASE_AM; + options.state_dir = apply_dir(); + break; + } + + if (!options.root) { + if (argc < 2) + die("TODO: handle @{upstream}"); + else { + options.upstream_name = argv[1]; + argc--; + argv++; + if (!strcmp(options.upstream_name, "-")) + options.upstream_name = "@{-1}"; + } + options.upstream = peel_committish(options.upstream_name); + if (!options.upstream) + die(_("invalid upstream '%s'"), options.upstream_name); + } else + die("TODO: upstream for --root"); + + /* Make sure the branch to rebase onto is valid. */ + if (!options.onto_name) + options.onto_name = options.upstream_name; + if (strstr(options.onto_name, "...")) { + die("TODO"); + } else { + options.onto = peel_committish(options.onto_name); + if (!options.onto) + die(_("Does not point to a valid commit '%s'"), + options.onto_name); + } + + /* + * If the branch to rebase is given, that is the branch we will rebase + * branch_name -- branch/commit being rebased, or + * HEAD (already detached) + * orig_head -- commit object name of tip of the branch before rebasing + * head_name -- refs/heads/<that-branch> or "detached HEAD" + */ + if (argc > 1) + die("TODO: handle switch_to"); + else { + /* Do not need to switch branches, we are already on it. */ + options.head_name = + xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, + &flags)); + if (!options.head_name) + die(_("No such ref: %s"), "HEAD"); + if (flags & REF_ISSYMREF) { + if (!skip_prefix(options.head_name, + "refs/heads/", &branch_name)) + branch_name = options.head_name; + + } else { + options.head_name = xstrdup("detached HEAD"); + branch_name = "HEAD"; + } + if (get_oid("HEAD", &options.orig_head)) + die(_("Could not resolve HEAD to a revision")); + } + + strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); + if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) + die(_("Could not detach HEAD")); + strbuf_release(&msg); + + strbuf_addf(&revisions, "%s..%s", + options.root ? oid_to_hex(&options.onto->object.oid) : + (options.restrict_revision ? + oid_to_hex(&options.restrict_revision->object.oid) : + oid_to_hex(&options.upstream->object.oid)), + oid_to_hex(&options.orig_head)); + + options.revisions = revisions.buf; + + ret = !!run_specific_rebase(&options); + + strbuf_release(&revisions); + free(options.head_name); + return ret; } From 935b6e46ca5a36e509e89a60c726c4bccfb2908a Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:16 +0545 Subject: [PATCH 111/406] builtin rebase: support --continue This commit adds the option `--continue` which is used to resume rebase after merge conflicts. The code tries to stay as close to the equivalent shell scripts found in `git-legacy-rebase.sh` as possible. When continuing a rebase, the state variables are read from state_dir. Some of the state variables are not actually stored there, such as `upstream`. The shell script version simply does not set them, but for consistency, we unset them in the builtin version. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 115 +++++++++++++++++++++++++++++++++++++++++++++-- strbuf.c | 9 ++++ strbuf.h | 3 ++ 3 files changed, 123 insertions(+), 4 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index e817956d96fe50..f112d91d678b05 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -91,6 +91,7 @@ struct rebase_options { REBASE_INTERACTIVE_EXPLICIT = 1<<4, } flags; struct strbuf git_am_opt; + const char *action; }; static int is_interactive(struct rebase_options *opts) @@ -115,6 +116,62 @@ static const char *state_dir_path(const char *filename, struct rebase_options *o return path.buf; } +/* Read one file, then strip line endings */ +static int read_one(const char *path, struct strbuf *buf) +{ + if (strbuf_read_file(buf, path, 0) < 0) + return error_errno(_("could not read '%s'"), path); + strbuf_trim_trailing_newline(buf); + return 0; +} + +/* Initialize the rebase options from the state directory. */ +static int read_basic_state(struct rebase_options *opts) +{ + struct strbuf head_name = STRBUF_INIT; + struct strbuf buf = STRBUF_INIT; + struct object_id oid; + + if (read_one(state_dir_path("head-name", opts), &head_name) || + read_one(state_dir_path("onto", opts), &buf)) + return -1; + opts->head_name = starts_with(head_name.buf, "refs/") ? + xstrdup(head_name.buf) : NULL; + strbuf_release(&head_name); + if (get_oid(buf.buf, &oid)) + return error(_("could not get 'onto': '%s'"), buf.buf); + opts->onto = lookup_commit_or_die(&oid, buf.buf); + + /* + * We always write to orig-head, but interactive rebase used to write to + * head. Fall back to reading from head to cover for the case that the + * user upgraded git with an ongoing interactive rebase. + */ + strbuf_reset(&buf); + if (file_exists(state_dir_path("orig-head", opts))) { + if (read_one(state_dir_path("orig-head", opts), &buf)) + return -1; + } else if (read_one(state_dir_path("head", opts), &buf)) + return -1; + if (get_oid(buf.buf, &opts->orig_head)) + return error(_("invalid orig-head: '%s'"), buf.buf); + + strbuf_reset(&buf); + if (read_one(state_dir_path("quiet", opts), &buf)) + return -1; + if (buf.len) + opts->flags &= ~REBASE_NO_QUIET; + else + opts->flags |= REBASE_NO_QUIET; + + if (file_exists(state_dir_path("verbose", opts))) + opts->flags |= REBASE_VERBOSE; + + strbuf_release(&buf); + + return 0; +} + static int finish_rebase(struct rebase_options *opts) { struct strbuf dir = STRBUF_INIT; @@ -168,12 +225,13 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "state_dir", opts->state_dir); add_var(&script_snippet, "upstream_name", opts->upstream_name); - add_var(&script_snippet, "upstream", - oid_to_hex(&opts->upstream->object.oid)); + add_var(&script_snippet, "upstream", opts->upstream ? + oid_to_hex(&opts->upstream->object.oid) : NULL); add_var(&script_snippet, "head_name", opts->head_name ? opts->head_name : "detached HEAD"); add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); - add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid)); + add_var(&script_snippet, "onto", opts->onto ? + oid_to_hex(&opts->onto->object.oid) : NULL); add_var(&script_snippet, "onto_name", opts->onto_name); add_var(&script_snippet, "revisions", opts->revisions); add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? @@ -189,6 +247,7 @@ static int run_specific_rebase(struct rebase_options *opts) opts->flags & REBASE_FORCE ? "t" : ""); if (opts->switch_to) add_var(&script_snippet, "switch_to", opts->switch_to); + add_var(&script_snippet, "action", opts->action ? opts->action : ""); switch (opts->type) { case REBASE_AM: @@ -400,12 +459,16 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) .git_am_opt = STRBUF_INIT, }; const char *branch_name; - int ret, flags, in_progress = 0; + int ret, flags, total_argc, in_progress = 0; int ok_to_skip_pre_rebase = 0; struct strbuf msg = STRBUF_INIT; struct strbuf revisions = STRBUF_INIT; struct strbuf buf = STRBUF_INIT; struct object_id merge_base; + enum { + NO_ACTION, + ACTION_CONTINUE, + } action = NO_ACTION; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -427,6 +490,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_BIT(0, "no-ff", &options.flags, N_("cherry-pick all commits, even if unchanged"), REBASE_FORCE), + OPT_CMDMODE(0, "continue", &action, N_("continue"), + ACTION_CONTINUE), OPT_END(), }; @@ -480,14 +545,55 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (options.type != REBASE_UNSPECIFIED) in_progress = 1; + total_argc = argc; argc = parse_options(argc, argv, prefix, builtin_rebase_options, builtin_rebase_usage, 0); + if (action != NO_ACTION && total_argc != 2) { + usage_with_options(builtin_rebase_usage, + builtin_rebase_options); + } + if (argc > 2) usage_with_options(builtin_rebase_usage, builtin_rebase_options); + switch (action) { + case ACTION_CONTINUE: { + struct object_id head; + struct lock_file lock_file = LOCK_INIT; + int fd; + + options.action = "continue"; + + /* Sanity check */ + if (get_oid("HEAD", &head)) + die(_("Cannot read HEAD")); + + fd = hold_locked_index(&lock_file, 0); + if (read_index(the_repository->index) < 0) + die(_("could not read index")); + refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, + NULL); + if (0 <= fd) + update_index_if_able(the_repository->index, + &lock_file); + rollback_lock_file(&lock_file); + + if (has_unstaged_changes(1)) { + puts(_("You must edit all merge conflicts and then\n" + "mark them as resolved using git add")); + exit(1); + } + if (read_basic_state(&options)) + exit(1); + goto run_rebase; + } + default: + die("TODO"); + } + /* Make sure no rebase is in progress */ if (in_progress) { const char *last_slash = strrchr(options.state_dir, '/'); @@ -719,6 +825,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.revisions = revisions.buf; +run_rebase: ret = !!run_specific_rebase(&options); cleanup: diff --git a/strbuf.c b/strbuf.c index 64041c3c249b15..f6a6cf78b9426a 100644 --- a/strbuf.c +++ b/strbuf.c @@ -120,6 +120,15 @@ void strbuf_trim_trailing_dir_sep(struct strbuf *sb) sb->buf[sb->len] = '\0'; } +void strbuf_trim_trailing_newline(struct strbuf *sb) +{ + if (sb->len > 0 && sb->buf[sb->len - 1] == '\n') { + if (--sb->len > 0 && sb->buf[sb->len - 1] == '\r') + --sb->len; + sb->buf[sb->len] = '\0'; + } +} + void strbuf_ltrim(struct strbuf *sb) { char *b = sb->buf; diff --git a/strbuf.h b/strbuf.h index 60a35aef165ca1..b7aea8a9660160 100644 --- a/strbuf.h +++ b/strbuf.h @@ -190,6 +190,9 @@ extern void strbuf_ltrim(struct strbuf *); /* Strip trailing directory separators */ extern void strbuf_trim_trailing_dir_sep(struct strbuf *); +/* Strip trailing LF or CR/LF */ +extern void strbuf_trim_trailing_newline(struct strbuf *sb); + /** * Replace the contents of the strbuf with a reencoded form. Returns -1 * on error, 0 on success. From 4cb28541b33f6c49b84d9fcbb45807623d06ba77 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:17 +0545 Subject: [PATCH 112/406] builtin rebase: support --skip This commit adds the option `--skip` which is used to restart rebase after skipping the current patch. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index f112d91d678b05..c477108f796fbb 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -21,6 +21,7 @@ #include "diff.h" #include "wt-status.h" #include "revision.h" +#include "rerere.h" static char const * const builtin_rebase_usage[] = { N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " @@ -468,6 +469,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) enum { NO_ACTION, ACTION_CONTINUE, + ACTION_SKIP, } action = NO_ACTION; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, @@ -492,6 +494,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) REBASE_FORCE), OPT_CMDMODE(0, "continue", &action, N_("continue"), ACTION_CONTINUE), + OPT_CMDMODE(0, "skip", &action, + N_("skip current patch and continue"), ACTION_SKIP), OPT_END(), }; @@ -590,6 +594,20 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) exit(1); goto run_rebase; } + case ACTION_SKIP: { + struct string_list merge_rr = STRING_LIST_INIT_DUP; + + options.action = "skip"; + + rerere_clear(&merge_rr); + string_list_clear(&merge_rr, 1); + + if (reset_head(NULL, "reset", NULL, 0) < 0) + die(_("could not discard worktree changes")); + if (read_basic_state(&options)) + exit(1); + goto run_rebase; + } default: die("TODO"); } From d3105ef416a209c938ea2a016f98fa6822321e6d Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:16 -0700 Subject: [PATCH 113/406] builtin rebase: try to fast forward when possible In this commit, we add support to fast forward. Note: we will need the merge base later, therefore the call to can_fast_forward() really needs to be the first one when testing whether we can skip the rebase entirely (otherwise, it would make more sense to skip the possibly expensive operation if, say, running an interactive rebase). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index afef0b00462239..d67df28efc9a94 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -20,6 +20,7 @@ #include "commit.h" #include "diff.h" #include "wt-status.h" +#include "revision.h" static char const * const builtin_rebase_usage[] = { N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " @@ -89,6 +90,12 @@ struct rebase_options { struct strbuf git_am_opt; }; +static int is_interactive(struct rebase_options *opts) +{ + return opts->type == REBASE_INTERACTIVE || + opts->type == REBASE_PRESERVE_MERGES; +} + /* Returns the filename prefixed by the state_dir */ static const char *state_dir_path(const char *filename, struct rebase_options *opts) { @@ -334,6 +341,46 @@ static int rebase_config(const char *var, const char *value, void *data) return git_default_config(var, value, data); } +/* + * Determines whether the commits in from..to are linear, i.e. contain + * no merge commits. This function *expects* `from` to be an ancestor of + * `to`. + */ +static int is_linear_history(struct commit *from, struct commit *to) +{ + while (to && to != from) { + parse_commit(to); + if (!to->parents) + return 1; + if (to->parents->next) + return 0; + to = to->parents->item; + } + return 1; +} + +static int can_fast_forward(struct commit *onto, struct object_id *head_oid, + struct object_id *merge_base) +{ + struct commit *head = lookup_commit(the_repository, head_oid); + struct commit_list *merge_bases; + int res; + + if (!head) + return 0; + + merge_bases = get_merge_bases(onto, head); + if (merge_bases && !merge_bases->next) { + oidcpy(merge_base, &merge_bases->item->object.oid); + res = !oidcmp(merge_base, &onto->object.oid); + } else { + oidcpy(merge_base, &null_oid); + res = 0; + } + free_commit_list(merge_bases); + return res && is_linear_history(onto, head); +} + int cmd_rebase(int argc, const char **argv, const char *prefix) { struct rebase_options options = { @@ -489,6 +536,31 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) goto cleanup; } + /* + * Now we are rebasing commits upstream..orig_head (or with --root, + * everything leading up to orig_head) on top of onto. + */ + + /* + * Check if we are already based on onto with linear history, + * but this should be done only when upstream and onto are the same + * and if this is not an interactive rebase. + */ + if (can_fast_forward(options.onto, &options.orig_head, &merge_base) && + !is_interactive(&options) && !options.restrict_revision && + !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) { + int flag; + + if (!(options.flags & REBASE_NO_QUIET)) + ; /* be quiet */ + else if (!strcmp(branch_name, "HEAD") && + resolve_ref_unsafe("HEAD", 0, NULL, &flag)) + puts(_("HEAD is up to date, rebase forced.")); + else + printf(_("Current branch %s is up to date, rebase " + "forced.\n"), branch_name); + } + /* If a hook exists, give it a chance to interrupt*/ if (!ok_to_skip_pre_rebase && run_hook_le(NULL, "pre-rebase", options.upstream_arg, From cb757c53d0037eff2583b7e986e6fb4ef6c5a84c Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:18 +0545 Subject: [PATCH 114/406] builtin rebase: support --abort This commit teaches the builtin rebase the "abort" action, which a user can call to roll back a rebase that is in progress. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index c477108f796fbb..ceb786d3a052a5 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -470,6 +470,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) NO_ACTION, ACTION_CONTINUE, ACTION_SKIP, + ACTION_ABORT, } action = NO_ACTION; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, @@ -496,6 +497,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_CONTINUE), OPT_CMDMODE(0, "skip", &action, N_("skip current patch and continue"), ACTION_SKIP), + OPT_CMDMODE(0, "abort", &action, + N_("abort and check out the original branch"), + ACTION_ABORT), OPT_END(), }; @@ -608,6 +612,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) exit(1); goto run_rebase; } + case ACTION_ABORT: { + struct string_list merge_rr = STRING_LIST_INIT_DUP; + options.action = "abort"; + + rerere_clear(&merge_rr); + string_list_clear(&merge_rr, 1); + + if (read_basic_state(&options)) + exit(1); + if (reset_head(&options.orig_head, "reset", + options.head_name, 0) < 0) + die(_("could not move back to %s"), + oid_to_hex(&options.orig_head)); + ret = finish_rebase(&options); + goto cleanup; + } default: die("TODO"); } From 75405ec77b0c22a6f40f04ce597b646536cfc571 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:17 -0700 Subject: [PATCH 115/406] builtin rebase: support --force-rebase In this commit, we add support to `--force-rebase` option. The equivalent part of the shell script found in `git-legacy-rebase.sh` is converted as faithfully as possible to C. The --force-rebase option ensures that the rebase does not simply fast-forward even if it could. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index d67df28efc9a94..8a7bf3d468bab6 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -86,6 +86,7 @@ struct rebase_options { REBASE_NO_QUIET = 1<<0, REBASE_VERBOSE = 1<<1, REBASE_DIFFSTAT = 1<<2, + REBASE_FORCE = 1<<3, } flags; struct strbuf git_am_opt; }; @@ -181,6 +182,8 @@ static int run_specific_rebase(struct rebase_options *opts) opts->flags & REBASE_VERBOSE ? "t" : ""); add_var(&script_snippet, "diffstat", opts->flags & REBASE_DIFFSTAT ? "t" : ""); + add_var(&script_snippet, "force_rebase", + opts->flags & REBASE_FORCE ? "t" : ""); switch (opts->type) { case REBASE_AM: @@ -409,6 +412,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, N_("do not show diffstat of what changed upstream"), PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, + OPT_BIT('f', "force-rebase", &options.flags, + N_("cherry-pick all commits, even if unchanged"), + REBASE_FORCE), + OPT_BIT(0, "no-ff", &options.flags, + N_("cherry-pick all commits, even if unchanged"), + REBASE_FORCE), OPT_END(), }; @@ -551,7 +560,18 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) { int flag; - if (!(options.flags & REBASE_NO_QUIET)) + if (!(options.flags & REBASE_FORCE)) { + if (!(options.flags & REBASE_NO_QUIET)) + ; /* be quiet */ + else if (!strcmp(branch_name, "HEAD") && + resolve_ref_unsafe("HEAD", 0, NULL, &flag)) + puts(_("HEAD is up to date.")); + else + printf(_("Current branch %s is up to date.\n"), + branch_name); + ret = !!finish_rebase(&options); + goto cleanup; + } else if (!(options.flags & REBASE_NO_QUIET)) ; /* be quiet */ else if (!strcmp(branch_name, "HEAD") && resolve_ref_unsafe("HEAD", 0, NULL, &flag)) From 7b33e5cf3aa913e73f98099e227d8185309ff15d Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:19 +0545 Subject: [PATCH 116/406] builtin rebase: support --quit With this patch, the builtin rebase handles the `--quit` action which can be used to abort a rebase without rolling back any changes performed during the rebase (this is useful when a user forgot that they were in the middle of a rebase and continued working normally). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index ceb786d3a052a5..f64fa42422feb4 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -471,6 +471,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_CONTINUE, ACTION_SKIP, ACTION_ABORT, + ACTION_QUIT, } action = NO_ACTION; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, @@ -500,6 +501,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_CMDMODE(0, "abort", &action, N_("abort and check out the original branch"), ACTION_ABORT), + OPT_CMDMODE(0, "quit", &action, + N_("abort but keep HEAD where it is"), ACTION_QUIT), OPT_END(), }; @@ -628,6 +631,14 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ret = finish_rebase(&options); goto cleanup; } + case ACTION_QUIT: { + strbuf_reset(&buf); + strbuf_addstr(&buf, options.state_dir); + ret = !!remove_dir_recursively(&buf, 0); + if (ret) + die(_("could not remove '%s'"), options.state_dir); + goto cleanup; + } default: die("TODO"); } From 73e9171341d9d5c27b648e2e8d63a25773639db4 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:18 -0700 Subject: [PATCH 117/406] builtin rebase: start a new rebase only if none is in progress To run a new rebase, there needs to be a check to assure that no other rebase is in progress. New rebase operation cannot start until an ongoing rebase operation completes or is terminated. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 8a7bf3d468bab6..d45f8f9008182f 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -87,6 +87,7 @@ struct rebase_options { REBASE_VERBOSE = 1<<1, REBASE_DIFFSTAT = 1<<2, REBASE_FORCE = 1<<3, + REBASE_INTERACTIVE_EXPLICIT = 1<<4, } flags; struct strbuf git_am_opt; }; @@ -392,10 +393,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) .git_am_opt = STRBUF_INIT, }; const char *branch_name; - int ret, flags; + int ret, flags, in_progress = 0; int ok_to_skip_pre_rebase = 0; struct strbuf msg = STRBUF_INIT; struct strbuf revisions = STRBUF_INIT; + struct strbuf buf = STRBUF_INIT; struct object_id merge_base; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, @@ -447,6 +449,30 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) git_config(rebase_config, &options); + if (is_directory(apply_dir())) { + options.type = REBASE_AM; + options.state_dir = apply_dir(); + } else if (is_directory(merge_dir())) { + strbuf_reset(&buf); + strbuf_addf(&buf, "%s/rewritten", merge_dir()); + if (is_directory(buf.buf)) { + options.type = REBASE_PRESERVE_MERGES; + options.flags |= REBASE_INTERACTIVE_EXPLICIT; + } else { + strbuf_reset(&buf); + strbuf_addf(&buf, "%s/interactive", merge_dir()); + if(file_exists(buf.buf)) { + options.type = REBASE_INTERACTIVE; + options.flags |= REBASE_INTERACTIVE_EXPLICIT; + } else + options.type = REBASE_MERGE; + } + options.state_dir = merge_dir(); + } + + if (options.type != REBASE_UNSPECIFIED) + in_progress = 1; + argc = parse_options(argc, argv, prefix, builtin_rebase_options, builtin_rebase_usage, 0); @@ -455,6 +481,26 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) usage_with_options(builtin_rebase_usage, builtin_rebase_options); + /* Make sure no rebase is in progress */ + if (in_progress) { + const char *last_slash = strrchr(options.state_dir, '/'); + const char *state_dir_base = + last_slash ? last_slash + 1 : options.state_dir; + const char *cmd_live_rebase = + "git rebase (--continue | --abort | --skip)"; + strbuf_reset(&buf); + strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); + die(_("It seems that there is already a %s directory, and\n" + "I wonder if you are in the middle of another rebase. " + "If that is the\n" + "case, please try\n\t%s\n" + "If that is not the case, please\n\t%s\n" + "and run me again. I am stopping in case you still " + "have something\n" + "valuable there.\n"), + state_dir_base, cmd_live_rebase, buf.buf); + } + if (!(options.flags & REBASE_NO_QUIET)) strbuf_addstr(&options.git_am_opt, " -q"); From e78af9373341c55d762c8967e46191d40c48c9d6 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:20 +0545 Subject: [PATCH 118/406] builtin rebase: support --edit-todo and --show-current-patch While these sub-commands are very different in spirit, their implementation is almost identical, so we convert them in one go. And since those are the last sub-commands that needed to be converted, now we can also turn that `default:` case into a bug (because we should now handle all the actions). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index f64fa42422feb4..12d34848bd979c 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -472,6 +472,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_SKIP, ACTION_ABORT, ACTION_QUIT, + ACTION_EDIT_TODO, + ACTION_SHOW_CURRENT_PATCH, } action = NO_ACTION; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, @@ -503,6 +505,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_ABORT), OPT_CMDMODE(0, "quit", &action, N_("abort but keep HEAD where it is"), ACTION_QUIT), + OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list " + "during an interactive rebase"), ACTION_EDIT_TODO), + OPT_CMDMODE(0, "show-current-patch", &action, + N_("show the patch file being applied or merged"), + ACTION_SHOW_CURRENT_PATCH), OPT_END(), }; @@ -570,6 +577,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) usage_with_options(builtin_rebase_usage, builtin_rebase_options); + if (action == ACTION_EDIT_TODO && !is_interactive(&options)) + die(_("The --edit-todo action can only be used during " + "interactive rebase.")); + switch (action) { case ACTION_CONTINUE: { struct object_id head; @@ -639,8 +650,18 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("could not remove '%s'"), options.state_dir); goto cleanup; } + case ACTION_EDIT_TODO: + options.action = "edit-todo"; + options.dont_finish_rebase = 1; + goto run_rebase; + case ACTION_SHOW_CURRENT_PATCH: + options.action = "show-current-patch"; + options.dont_finish_rebase = 1; + goto run_rebase; + case NO_ACTION: + break; default: - die("TODO"); + BUG("action: %d", action); } /* Make sure no rebase is in progress */ From f17dd0d4d7990f8a0e20a07c63f6a518fa278339 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:20 -0700 Subject: [PATCH 119/406] builtin rebase: only store fully-qualified refs in `options.head_name` When running a rebase on a detached HEAD, we currently store the string "detached HEAD" in options.head_name. That is a faithful translation of the shell script version, and we still kind of need it for the purposes of the scripted backends. It is poor style for C, though, where we would really only want a valid, fully-qualified ref name as value, and NULL for detached HEADs, using "detached HEAD" for display only. Make it so. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index d45f8f9008182f..afc75fe7314e6d 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -169,7 +169,8 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "upstream_name", opts->upstream_name); add_var(&script_snippet, "upstream", oid_to_hex(&opts->upstream->object.oid)); - add_var(&script_snippet, "head_name", opts->head_name); + add_var(&script_snippet, "head_name", + opts->head_name ? opts->head_name : "detached HEAD"); add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid)); add_var(&script_snippet, "onto_name", opts->onto_name); @@ -251,6 +252,9 @@ static int reset_head(struct object_id *oid, const char *action, *old_orig = NULL, oid_old_orig; int ret = 0; + if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) + BUG("Not a fully qualified branch: '%s'", switch_to_branch); + if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) return -1; @@ -558,7 +562,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) * branch_name -- branch/commit being rebased, or * HEAD (already detached) * orig_head -- commit object name of tip of the branch before rebasing - * head_name -- refs/heads/<that-branch> or "detached HEAD" + * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) */ if (argc > 0) die("TODO: handle switch_to"); @@ -575,7 +579,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) branch_name = options.head_name; } else { - options.head_name = xstrdup("detached HEAD"); + free(options.head_name); + options.head_name = NULL; branch_name = "HEAD"; } if (get_oid("HEAD", &options.orig_head)) From 173c40bb4b5d35e182adfed7a3351bff52f5998f Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:21 +0545 Subject: [PATCH 120/406] builtin rebase: actions require a rebase in progress This commit prevents actions (such as --continue, --skip) from running when there is no rebase in progress. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 12d34848bd979c..9238757443832d 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -577,6 +577,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) usage_with_options(builtin_rebase_usage, builtin_rebase_options); + if (action != NO_ACTION && !in_progress) + die(_("No rebase in progress?")); + if (action == ACTION_EDIT_TODO && !is_interactive(&options)) die(_("The --edit-todo action can only be used during " "interactive rebase.")); From 08a704f2479e5383b43646b7eb050194c238c043 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:27:21 -0700 Subject: [PATCH 121/406] builtin rebase: support `git rebase <upstream> <switch-to>` This commit adds support for `switch-to` which is used to switch to the target branch if needed. The equivalent codes found in shell script `git-legacy-rebase.sh` is converted to builtin `rebase.c`. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index afc75fe7314e6d..e817956d96fe50 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -79,6 +79,7 @@ struct rebase_options { struct commit *onto; const char *onto_name; const char *revisions; + const char *switch_to; int root; struct commit *restrict_revision; int dont_finish_rebase; @@ -186,6 +187,8 @@ static int run_specific_rebase(struct rebase_options *opts) opts->flags & REBASE_DIFFSTAT ? "t" : ""); add_var(&script_snippet, "force_rebase", opts->flags & REBASE_FORCE ? "t" : ""); + if (opts->switch_to) + add_var(&script_snippet, "switch_to", opts->switch_to); switch (opts->type) { case REBASE_AM: @@ -564,9 +567,23 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) * orig_head -- commit object name of tip of the branch before rebasing * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) */ - if (argc > 0) - die("TODO: handle switch_to"); - else { + if (argc == 1) { + /* Is it "rebase other branchname" or "rebase other commit"? */ + branch_name = argv[0]; + options.switch_to = argv[0]; + + /* Is it a local branch? */ + strbuf_reset(&buf); + strbuf_addf(&buf, "refs/heads/%s", branch_name); + if (!read_ref(buf.buf, &options.orig_head)) + options.head_name = xstrdup(buf.buf); + /* If not is it a valid ref (branch or commit)? */ + else if (!get_oid(branch_name, &options.orig_head)) + options.head_name = NULL; + else + die(_("fatal: no such branch/commit '%s'"), + branch_name); + } else if (argc == 0) { /* Do not need to switch branches, we are already on it. */ options.head_name = xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, @@ -585,7 +602,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } if (get_oid("HEAD", &options.orig_head)) die(_("Could not resolve HEAD to a revision")); - } + } else + BUG("unexpected number of arguments left to parse"); if (read_index(the_repository->index) < 0) die(_("could not read index")); @@ -612,6 +630,28 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) int flag; if (!(options.flags & REBASE_FORCE)) { + /* Lazily switch to the target branch if needed... */ + if (options.switch_to) { + struct object_id oid; + + if (get_oid(options.switch_to, &oid) < 0) { + ret = !!error(_("could not parse '%s'"), + options.switch_to); + goto cleanup; + } + + strbuf_reset(&buf); + strbuf_addf(&buf, "rebase: checkout %s", + options.switch_to); + if (reset_head(&oid, "checkout", + options.head_name, 0) < 0) { + ret = !!error(_("could not switch to " + "%s"), + options.switch_to); + goto cleanup; + } + } + if (!(options.flags & REBASE_NO_QUIET)) ; /* be quiet */ else if (!strcmp(branch_name, "HEAD") && From d919bdb3aebbd6fd9061ee8c00ef4508953cd334 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:49 -0700 Subject: [PATCH 122/406] builtin rebase: allow selecting the rebase "backend" With this commit the builtin rebase supports selecting the "rebase backends" (or "type") `interactive`, `preserve-merges`, and `merge`. The `state_dir` was already handled according to the rebase type in a previous commit. Note that there is one quirk in the shell script: `--interactive` followed by `--merge` won't reset the type to "merge" but keeps the type as "interactive". And as t3418 tests this explicitly, we have to support it in the builtin rebase, too. Likewise, `--interactive` followed by `--preserve-merges` makes it an "explicitly interactive" rebase, i.e. a rebase that should show the todo list, while `--preserve-merges` alone is not interactive (and t5520 tests for this via `git pull --rebase=preserve`). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 2165656ae24a87..61695ac7b954de 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -452,6 +452,29 @@ static int can_fast_forward(struct commit *onto, struct object_id *head_oid, return res && is_linear_history(onto, head); } +/* -i followed by -m is still -i */ +static int parse_opt_merge(const struct option *opt, const char *arg, int unset) +{ + struct rebase_options *opts = opt->value; + + if (!is_interactive(opts)) + opts->type = REBASE_MERGE; + + return 0; +} + +/* -i followed by -p is still explicitly interactive, but -p alone is not */ +static int parse_opt_interactive(const struct option *opt, const char *arg, + int unset) +{ + struct rebase_options *opts = opt->value; + + opts->type = REBASE_INTERACTIVE; + opts->flags |= REBASE_INTERACTIVE_EXPLICIT; + + return 0; +} + int cmd_rebase(int argc, const char **argv, const char *prefix) { struct rebase_options options = { @@ -510,6 +533,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_CMDMODE(0, "show-current-patch", &action, N_("show the patch file being applied or merged"), ACTION_SHOW_CURRENT_PATCH), + { OPTION_CALLBACK, 'm', "merge", &options, NULL, + N_("use merging strategies to rebase"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, + parse_opt_merge }, + { OPTION_CALLBACK, 'i', "interactive", &options, NULL, + N_("let the user edit the list of commits to rebase"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, + parse_opt_interactive }, + OPT_SET_INT('p', "preserve-merges", &options.type, + N_("try to recreate merges instead of ignoring " + "them"), REBASE_PRESERVE_MERGES), OPT_END(), }; @@ -884,6 +918,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) diff_flush(&opts); } + if (is_interactive(&options)) + goto run_rebase; + /* Detach HEAD and reset the tree */ if (options.flags & REBASE_NO_QUIET) printf(_("First, rewinding head to replay your work on top of " From f61a31bb0f2a451f31cad1c1eff8fa8171cde55b Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:50 -0700 Subject: [PATCH 123/406] builtin rebase: support --signoff This commit adds support for `--signoff` which is used to add a `Signed-off-by` trailer to all the rebased commits. The actual handling is left to the rebase backends. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 61695ac7b954de..3c8b29d82cd932 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -93,6 +93,7 @@ struct rebase_options { } flags; struct strbuf git_am_opt; const char *action; + int signoff; }; static int is_interactive(struct rebase_options *opts) @@ -168,6 +169,11 @@ static int read_basic_state(struct rebase_options *opts) if (file_exists(state_dir_path("verbose", opts))) opts->flags |= REBASE_VERBOSE; + if (file_exists(state_dir_path("signoff", opts))) { + opts->signoff = 1; + opts->flags |= REBASE_FORCE; + } + strbuf_release(&buf); return 0; @@ -249,6 +255,7 @@ static int run_specific_rebase(struct rebase_options *opts) if (opts->switch_to) add_var(&script_snippet, "switch_to", opts->switch_to); add_var(&script_snippet, "action", opts->action ? opts->action : ""); + add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : ""); switch (opts->type) { case REBASE_AM: @@ -513,6 +520,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, N_("do not show diffstat of what changed upstream"), PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, + OPT_BOOL(0, "signoff", &options.signoff, + N_("add a Signed-off-by: line to each commit")), OPT_BIT('f', "force-rebase", &options.flags, N_("cherry-pick all commits, even if unchanged"), REBASE_FORCE), @@ -745,6 +754,14 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) break; } + if (options.signoff) { + if (options.type == REBASE_PRESERVE_MERGES) + die("cannot combine '--signoff' with " + "'--preserve-merges'"); + strbuf_addstr(&options.git_am_opt, " --signoff"); + options.flags |= REBASE_FORCE; + } + if (!options.root) { if (argc < 1) die("TODO: handle @{upstream}"); From 7612873f7a0a28475c8c501672ec7309ac96dd74 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:52 -0700 Subject: [PATCH 124/406] builtin rebase: support --rerere-autoupdate The `--rerere-autoupdate` option allows rerere to update the index with resolved conflicts. This commit follows closely the equivalent part of `git-legacy-rebase.sh`. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 3c8b29d82cd932..24dc646ea5b6b3 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -94,6 +94,7 @@ struct rebase_options { struct strbuf git_am_opt; const char *action; int signoff; + int allow_rerere_autoupdate; }; static int is_interactive(struct rebase_options *opts) @@ -174,6 +175,21 @@ static int read_basic_state(struct rebase_options *opts) opts->flags |= REBASE_FORCE; } + if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) { + strbuf_reset(&buf); + if (read_one(state_dir_path("allow_rerere_autoupdate", opts), + &buf)) + return -1; + if (!strcmp(buf.buf, "--rerere-autoupdate")) + opts->allow_rerere_autoupdate = 1; + else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) + opts->allow_rerere_autoupdate = 0; + else + warning(_("ignoring invalid allow_rerere_autoupdate: " + "'%s'"), buf.buf); + } else + opts->allow_rerere_autoupdate = -1; + strbuf_release(&buf); return 0; @@ -256,6 +272,10 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "switch_to", opts->switch_to); add_var(&script_snippet, "action", opts->action ? opts->action : ""); add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : ""); + add_var(&script_snippet, "allow_rerere_autoupdate", + opts->allow_rerere_autoupdate < 0 ? "" : + opts->allow_rerere_autoupdate ? + "--rerere-autoupdate" : "--no-rerere-autoupdate"); switch (opts->type) { case REBASE_AM: @@ -488,6 +508,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) .type = REBASE_UNSPECIFIED, .flags = REBASE_NO_QUIET, .git_am_opt = STRBUF_INIT, + .allow_rerere_autoupdate = -1, }; const char *branch_name; int ret, flags, total_argc, in_progress = 0; @@ -553,6 +574,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_SET_INT('p', "preserve-merges", &options.type, N_("try to recreate merges instead of ignoring " "them"), REBASE_PRESERVE_MERGES), + OPT_BOOL(0, "rerere-autoupdate", + &options.allow_rerere_autoupdate, + N_("allow rerere to update index with resolved " + "conflict")), OPT_END(), }; From e4ff3ba0ca255dc74fa4e3fb75f27deb5f991892 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:53 -0700 Subject: [PATCH 125/406] builtin rebase: support --committer-date-is-author-date This option is simply handed down to `git am` by way of setting the `git_am_opt` variable that is handled by the `git-rebase--am` backend. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 24dc646ea5b6b3..6ce50eae0f2ea7 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -526,6 +526,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_EDIT_TODO, ACTION_SHOW_CURRENT_PATCH, } action = NO_ACTION; + int committer_date_is_author_date = 0; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -543,6 +544,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, OPT_BOOL(0, "signoff", &options.signoff, N_("add a Signed-off-by: line to each commit")), + OPT_BOOL(0, "committer-date-is-author-date", + &committer_date_is_author_date, + N_("passed to 'git am'")), OPT_BIT('f', "force-rebase", &options.flags, N_("cherry-pick all commits, even if unchanged"), REBASE_FORCE), @@ -763,6 +767,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (!(options.flags & REBASE_NO_QUIET)) strbuf_addstr(&options.git_am_opt, " -q"); + if (committer_date_is_author_date) { + strbuf_addstr(&options.git_am_opt, + " --committer-date-is-author-date"); + options.flags |= REBASE_FORCE; + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From 7d2b978f01a5af91469142b20a6c0f2da69f638c Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:54 -0700 Subject: [PATCH 126/406] builtin rebase: support `ignore-whitespace` option This commit adds support for the `--ignore-whitespace` option of the rebase command. This option is simply passed to the `--am` backend. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 6ce50eae0f2ea7..f023fb4339c3ad 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -527,6 +527,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_SHOW_CURRENT_PATCH, } action = NO_ACTION; int committer_date_is_author_date = 0; + int ignore_whitespace = 0; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -542,6 +543,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, N_("do not show diffstat of what changed upstream"), PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, + OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace, + N_("passed to 'git apply'")), OPT_BOOL(0, "signoff", &options.signoff, N_("add a Signed-off-by: line to each commit")), OPT_BOOL(0, "committer-date-is-author-date", @@ -773,6 +776,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.flags |= REBASE_FORCE; } + if (ignore_whitespace) + strbuf_addstr(&options.git_am_opt, " --ignore-whitespace"); + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From fc36c421907d2b3bc99961da7b921c01ed254eae Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:56 -0700 Subject: [PATCH 127/406] builtin rebase: support `ignore-date` option This commit adds support for `--ignore-date` which is passed to `git am` to easily change the dates of the rebased commits. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index f023fb4339c3ad..0f3e156daca6a5 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -527,6 +527,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ACTION_SHOW_CURRENT_PATCH, } action = NO_ACTION; int committer_date_is_author_date = 0; + int ignore_date = 0; int ignore_whitespace = 0; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, @@ -550,6 +551,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "committer-date-is-author-date", &committer_date_is_author_date, N_("passed to 'git am'")), + OPT_BOOL(0, "ignore-date", &ignore_date, + N_("passed to 'git am'")), OPT_BIT('f', "force-rebase", &options.flags, N_("cherry-pick all commits, even if unchanged"), REBASE_FORCE), @@ -779,6 +782,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (ignore_whitespace) strbuf_addstr(&options.git_am_opt, " --ignore-whitespace"); + if (ignore_date) { + strbuf_addstr(&options.git_am_opt, " --ignore-date"); + options.flags |= REBASE_FORCE; + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From bcfaff7b1cd29bc7ddb70e913be494c68f2a09fa Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:57 -0700 Subject: [PATCH 128/406] builtin rebase: support `keep-empty` option The `--keep-empty` option can be used to keep the commits that do not change anything from its parents in the result. While the scripted version uses `interactive_rebase=implied` to indicate that the rebase needs to use the `git-rebase--interactive` backend in non-interactive mode as fallback when figuring out which backend to use, the C version needs to use a different route because the backend will already be chosen during the `parse_options()` call. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 0f3e156daca6a5..2336b6b9793d39 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -95,6 +95,7 @@ struct rebase_options { const char *action; int signoff; int allow_rerere_autoupdate; + int keep_empty; }; static int is_interactive(struct rebase_options *opts) @@ -103,6 +104,23 @@ static int is_interactive(struct rebase_options *opts) opts->type == REBASE_PRESERVE_MERGES; } +static void imply_interactive(struct rebase_options *opts, const char *option) +{ + switch (opts->type) { + case REBASE_AM: + die(_("%s requires an interactive rebase"), option); + break; + case REBASE_INTERACTIVE: + case REBASE_PRESERVE_MERGES: + break; + case REBASE_MERGE: + /* we silently *upgrade* --merge to --interactive if needed */ + default: + opts->type = REBASE_INTERACTIVE; /* implied */ + break; + } +} + /* Returns the filename prefixed by the state_dir */ static const char *state_dir_path(const char *filename, struct rebase_options *opts) { @@ -276,6 +294,7 @@ static int run_specific_rebase(struct rebase_options *opts) opts->allow_rerere_autoupdate < 0 ? "" : opts->allow_rerere_autoupdate ? "--rerere-autoupdate" : "--no-rerere-autoupdate"); + add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); switch (opts->type) { case REBASE_AM: @@ -588,6 +607,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) &options.allow_rerere_autoupdate, N_("allow rerere to update index with resolved " "conflict")), + OPT_BOOL('k', "keep-empty", &options.keep_empty, + N_("preserve empty commits during rebase")), OPT_END(), }; @@ -787,6 +808,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.flags |= REBASE_FORCE; } + if (options.keep_empty) + imply_interactive(&options, "--keep-empty"); + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From a2a50562467a03a488a39fc0285e8f78a90f820d Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 14:59:58 -0700 Subject: [PATCH 129/406] builtin rebase: support `--autosquash` This commit adds support for the `--autosquash` option which is used to automatically squash the commits marked as `squash` or `fixup` in their messages. This is converted following `git-legacy-rebase.sh` closely. This option can also be configured via the Git config setting rebase.autosquash. To support this, we also add a custom rebase_config() function in this commit that will be used instead (and falls back to) git_default_config(). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 2336b6b9793d39..e2b3fc2154cc00 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -96,6 +96,7 @@ struct rebase_options { int signoff; int allow_rerere_autoupdate; int keep_empty; + int autosquash; }; static int is_interactive(struct rebase_options *opts) @@ -295,6 +296,7 @@ static int run_specific_rebase(struct rebase_options *opts) opts->allow_rerere_autoupdate ? "--rerere-autoupdate" : "--no-rerere-autoupdate"); add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); + add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); switch (opts->type) { case REBASE_AM: @@ -455,6 +457,11 @@ static int rebase_config(const char *var, const char *value, void *data) return 0; } + if (!strcmp(var, "rebase.autosquash")) { + opts->autosquash = git_config_bool(var, value); + return 0; + } + return git_default_config(var, value, data); } @@ -609,6 +616,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) "conflict")), OPT_BOOL('k', "keep-empty", &options.keep_empty, N_("preserve empty commits during rebase")), + OPT_BOOL(0, "autosquash", &options.autosquash, + N_("move commits that begin with " + "squash!/fixup! under -i")), OPT_END(), }; From 940e964ef073340b3000f687f6afbf1fb6908bcf Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:00 -0700 Subject: [PATCH 130/406] builtin rebase: support `--gpg-sign` option This commit introduces support for `--gpg-sign` option which is used to GPG-sign commits. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index e2b3fc2154cc00..23a5bb30030b52 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -97,6 +97,7 @@ struct rebase_options { int allow_rerere_autoupdate; int keep_empty; int autosquash; + char *gpg_sign_opt; }; static int is_interactive(struct rebase_options *opts) @@ -209,6 +210,15 @@ static int read_basic_state(struct rebase_options *opts) } else opts->allow_rerere_autoupdate = -1; + if (file_exists(state_dir_path("gpg_sign_opt", opts))) { + strbuf_reset(&buf); + if (read_one(state_dir_path("gpg_sign_opt", opts), + &buf)) + return -1; + free(opts->gpg_sign_opt); + opts->gpg_sign_opt = xstrdup(buf.buf); + } + strbuf_release(&buf); return 0; @@ -297,6 +307,7 @@ static int run_specific_rebase(struct rebase_options *opts) "--rerere-autoupdate" : "--no-rerere-autoupdate"); add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); + add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt); switch (opts->type) { case REBASE_AM: @@ -462,6 +473,13 @@ static int rebase_config(const char *var, const char *value, void *data) return 0; } + if (!strcmp(var, "commit.gpgsign")) { + free(opts->gpg_sign_opt); + opts->gpg_sign_opt = git_config_bool(var, value) ? + xstrdup("-S") : NULL; + return 0; + } + return git_default_config(var, value, data); } @@ -555,6 +573,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) int committer_date_is_author_date = 0; int ignore_date = 0; int ignore_whitespace = 0; + const char *gpg_sign = NULL; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -619,6 +638,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "autosquash", &options.autosquash, N_("move commits that begin with " "squash!/fixup! under -i")), + { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"), + N_("GPG-sign commits"), + PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, OPT_END(), }; @@ -821,6 +843,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (options.keep_empty) imply_interactive(&options, "--keep-empty"); + if (gpg_sign) { + free(options.gpg_sign_opt); + options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: @@ -1046,5 +1073,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) cleanup: strbuf_release(&revisions); free(options.head_name); + free(options.gpg_sign_opt); return ret; } From 33e2f56dbb329ed8572d37520fdc1f486cddf116 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 20:51:22 +0545 Subject: [PATCH 131/406] builtin rebase: stop if `git am` is in progress This commit checks for the file `applying` used by `git am` in `rebase-apply/` and if the file is present it means `git am` is in progress so it errors out. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 9238757443832d..2165656ae24a87 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -539,6 +539,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) git_config(rebase_config, &options); + strbuf_reset(&buf); + strbuf_addf(&buf, "%s/applying", apply_dir()); + if(file_exists(buf.buf)) + die(_("It looks like 'git am' is in progress. Cannot rebase.")); + if (is_directory(apply_dir())) { options.type = REBASE_AM; options.state_dir = apply_dir(); From 86582a3c3c28c4e21862b469a316c6420ef1b486 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:01 -0700 Subject: [PATCH 132/406] builtin rebase: support `-C` and `--whitespace=<type>` This commit converts more code from the shell script version to the builtin rebase. In this instance, we just have to be careful to keep support for passing multiple `--whitespace` options, as the shell script version does so, too. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 23a5bb30030b52..c2f28cdd9e32ca 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -574,6 +574,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) int ignore_date = 0; int ignore_whitespace = 0; const char *gpg_sign = NULL; + int opt_c = -1; + struct string_list whitespace = STRING_LIST_INIT_NODUP; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -641,6 +643,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"), N_("GPG-sign commits"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, + OPT_STRING_LIST(0, "whitespace", &whitespace, + N_("whitespace"), N_("passed to 'git apply'")), + OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"), + REBASE_AM), OPT_END(), }; @@ -848,6 +854,23 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); } + if (opt_c >= 0) + strbuf_addf(&options.git_am_opt, " -C%d", opt_c); + + if (whitespace.nr) { + int i; + + for (i = 0; i < whitespace.nr; i++) { + const char *item = whitespace.items[i].string; + + strbuf_addf(&options.git_am_opt, " --whitespace=%s", + item); + + if ((!strcmp(item, "fix")) || (!strcmp(item, "strip"))) + options.flags |= REBASE_FORCE; + } + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From 1c890fac8b6e1ea2424cfa1b93e2e56083e1760d Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:02 -0700 Subject: [PATCH 133/406] builtin rebase: support `--autostash` option To support `--autostash` we introduce a function `apply_autostash()` just like in `git-legacy-rebase.sh`. Rather than refactoring and using the same function that exists in `sequencer.c`, we go a different route here, to avoid clashes with the sister GSoC project that turns the interactive rebase into a builtin. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 117 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 109 insertions(+), 8 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index c2f28cdd9e32ca..07d8dc6d08b75b 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -63,12 +63,6 @@ static int use_builtin_rebase(void) return ret; } -static int apply_autostash(void) -{ - warning("TODO"); - return 0; -} - struct rebase_options { enum rebase_type type; const char *state_dir; @@ -98,6 +92,7 @@ struct rebase_options { int keep_empty; int autosquash; char *gpg_sign_opt; + int autostash; }; static int is_interactive(struct rebase_options *opts) @@ -224,13 +219,56 @@ static int read_basic_state(struct rebase_options *opts) return 0; } +static int apply_autostash(struct rebase_options *opts) +{ + const char *path = state_dir_path("autostash", opts); + struct strbuf autostash = STRBUF_INIT; + struct child_process stash_apply = CHILD_PROCESS_INIT; + + if (!file_exists(path)) + return 0; + + if (read_one(state_dir_path("autostash", opts), &autostash)) + return error(_("Could not read '%s'"), path); + argv_array_pushl(&stash_apply.args, + "stash", "apply", autostash.buf, NULL); + stash_apply.git_cmd = 1; + stash_apply.no_stderr = stash_apply.no_stdout = + stash_apply.no_stdin = 1; + if (!run_command(&stash_apply)) + printf(_("Applied autostash.\n")); + else { + struct argv_array args = ARGV_ARRAY_INIT; + int res = 0; + + argv_array_pushl(&args, + "stash", "store", "-m", "autostash", "-q", + autostash.buf, NULL); + if (run_command_v_opt(args.argv, RUN_GIT_CMD)) + res = error(_("Cannot store %s"), autostash.buf); + argv_array_clear(&args); + strbuf_release(&autostash); + if (res) + return res; + + fprintf(stderr, + _("Applying autostash resulted in conflicts.\n" + "Your changes are safe in the stash.\n" + "You can run \"git stash pop\" or \"git stash drop\" " + "at any time.\n")); + } + + strbuf_release(&autostash); + return 0; +} + static int finish_rebase(struct rebase_options *opts) { struct strbuf dir = STRBUF_INIT; const char *argv_gc_auto[] = { "gc", "--auto", NULL }; delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); - apply_autostash(); + apply_autostash(opts); close_all_packs(the_repository->objects); /* * We ignore errors in 'gc --auto', since the @@ -345,7 +383,7 @@ static int run_specific_rebase(struct rebase_options *opts) } else if (status == 2) { struct strbuf dir = STRBUF_INIT; - apply_autostash(); + apply_autostash(opts); strbuf_addstr(&dir, opts->state_dir); remove_dir_recursively(&dir, 0); strbuf_release(&dir); @@ -480,6 +518,11 @@ static int rebase_config(const char *var, const char *value, void *data) return 0; } + if (!strcmp(var, "rebase.autostash")) { + opts->autostash = git_config_bool(var, value); + return 0; + } + return git_default_config(var, value, data); } @@ -647,6 +690,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) N_("whitespace"), N_("passed to 'git apply'")), OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"), REBASE_AM), + OPT_BOOL(0, "autostash", &options.autostash, + N_("automatically stash/stash pop before and after")), OPT_END(), }; @@ -976,6 +1021,62 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (read_index(the_repository->index) < 0) die(_("could not read index")); + if (options.autostash) { + struct lock_file lock_file = LOCK_INIT; + int fd; + + fd = hold_locked_index(&lock_file, 0); + refresh_cache(REFRESH_QUIET); + if (0 <= fd) + update_index_if_able(&the_index, &lock_file); + rollback_lock_file(&lock_file); + + if (has_unstaged_changes(0) || has_uncommitted_changes(0)) { + const char *autostash = + state_dir_path("autostash", &options); + struct child_process stash = CHILD_PROCESS_INIT; + struct object_id oid; + struct commit *head = + lookup_commit_reference(the_repository, + &options.orig_head); + + argv_array_pushl(&stash.args, + "stash", "create", "autostash", NULL); + stash.git_cmd = 1; + stash.no_stdin = 1; + strbuf_reset(&buf); + if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) + die(_("Cannot autostash")); + strbuf_trim_trailing_newline(&buf); + if (get_oid(buf.buf, &oid)) + die(_("Unexpected stash response: '%s'"), + buf.buf); + strbuf_reset(&buf); + strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); + + if (safe_create_leading_directories_const(autostash)) + die(_("Could not create directory for '%s'"), + options.state_dir); + write_file(autostash, "%s", buf.buf); + printf(_("Created autostash: %s\n"), buf.buf); + if (reset_head(&head->object.oid, "reset --hard", + NULL, 0) < 0) + die(_("could not reset --hard")); + printf(_("HEAD is now at %s"), + find_unique_abbrev(&head->object.oid, + DEFAULT_ABBREV)); + strbuf_reset(&buf); + pp_commit_easy(CMIT_FMT_ONELINE, head, &buf); + if (buf.len > 0) + printf(" %s", buf.buf); + putchar('\n'); + + if (discard_index(the_repository->index) < 0 || + read_index(the_repository->index) < 0) + die(_("could not read index")); + } + } + if (require_clean_work_tree("rebase", _("Please commit or stash them."), 1, 1)) { ret = 1; From 7d56aee2652beb204c020787dabec1ae2c66a209 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:04 -0700 Subject: [PATCH 134/406] builtin rebase: support `--exec` This commit adds support for the `--exec` option which takes a shell command-line as argument. This argument will be appended as an `exec <cmd>` command after each line in the todo list that creates a commit in the final history. commands. Note: while the shell script version of `git rebase` assigned the empty string to `cmd` by default, we *unset* it here because the code looks nicer and it does not change the behavior. The `--exec` option requires `--interactive` machinery. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 07d8dc6d08b75b..2c73cc8a61f0c3 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -93,6 +93,7 @@ struct rebase_options { int autosquash; char *gpg_sign_opt; int autostash; + char *cmd; }; static int is_interactive(struct rebase_options *opts) @@ -346,6 +347,7 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt); + add_var(&script_snippet, "cmd", opts->cmd); switch (opts->type) { case REBASE_AM: @@ -619,6 +621,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) const char *gpg_sign = NULL; int opt_c = -1; struct string_list whitespace = STRING_LIST_INIT_NODUP; + struct string_list exec = STRING_LIST_INIT_NODUP; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -692,6 +695,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) REBASE_AM), OPT_BOOL(0, "autostash", &options.autostash, N_("automatically stash/stash pop before and after")), + OPT_STRING_LIST('x', "exec", &exec, N_("exec"), + N_("add exec lines after each commit of the " + "editable list")), OPT_END(), }; @@ -916,6 +922,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } } + if (exec.nr) { + int i; + + imply_interactive(&options, "--exec"); + + strbuf_reset(&buf); + for (i = 0; i < exec.nr; i++) + strbuf_addf(&buf, "exec %s\n", exec.items[i].string); + options.cmd = xstrdup(buf.buf); + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: @@ -1198,5 +1215,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_release(&revisions); free(options.head_name); free(options.gpg_sign_opt); + free(options.cmd); return ret; } From 316bc1476898880ad75b614ba0c33c32f45c18f9 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:05 -0700 Subject: [PATCH 135/406] builtin rebase: support `--allow-empty-message` option This commit introduces the `--allow-empty-message` option to `builtin/rebase.c`. The motivation behind this option is: if there are empty messages (which is not allowed in Git by default, but can be imported from different version control systems), the rebase will fail. Using `--allow-empty-message` overrides that behaviour which will allow the commits having empty messages to continue in rebase operation. Note: a very recent change made this the default in the shell scripted `git rebase`, therefore the builtin rebase does the same. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 2c73cc8a61f0c3..730aefb7a9ea3c 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -94,6 +94,7 @@ struct rebase_options { char *gpg_sign_opt; int autostash; char *cmd; + int allow_empty_message; }; static int is_interactive(struct rebase_options *opts) @@ -348,6 +349,8 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt); add_var(&script_snippet, "cmd", opts->cmd); + add_var(&script_snippet, "allow_empty_message", + opts->allow_empty_message ? "--allow-empty-message" : ""); switch (opts->type) { case REBASE_AM: @@ -598,6 +601,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) .flags = REBASE_NO_QUIET, .git_am_opt = STRBUF_INIT, .allow_rerere_autoupdate = -1, + .allow_empty_message = 1, }; const char *branch_name; int ret, flags, total_argc, in_progress = 0; @@ -698,6 +702,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_STRING_LIST('x', "exec", &exec, N_("exec"), N_("add exec lines after each commit of the " "editable list")), + OPT_BOOL(0, "allow-empty-message", + &options.allow_empty_message, + N_("allow rebasing commits with empty messages")), OPT_END(), }; From 44388852376abfc4b827eb96bd766e33b46703dd Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:07 -0700 Subject: [PATCH 136/406] builtin rebase: support --rebase-merges[=[no-]rebase-cousins] The mode to rebase non-linear branches is now supported by the builtin rebase, too. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 730aefb7a9ea3c..43b46998da0bc9 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -95,6 +95,7 @@ struct rebase_options { int autostash; char *cmd; int allow_empty_message; + int rebase_merges, rebase_cousins; }; static int is_interactive(struct rebase_options *opts) @@ -351,6 +352,10 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "cmd", opts->cmd); add_var(&script_snippet, "allow_empty_message", opts->allow_empty_message ? "--allow-empty-message" : ""); + add_var(&script_snippet, "rebase_merges", + opts->rebase_merges ? "t" : ""); + add_var(&script_snippet, "rebase_cousins", + opts->rebase_cousins ? "t" : ""); switch (opts->type) { case REBASE_AM: @@ -626,6 +631,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) int opt_c = -1; struct string_list whitespace = STRING_LIST_INIT_NODUP; struct string_list exec = STRING_LIST_INIT_NODUP; + const char *rebase_merges = NULL; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -705,6 +711,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "allow-empty-message", &options.allow_empty_message, N_("allow rebasing commits with empty messages")), + {OPTION_STRING, 'r', "rebase-merges", &rebase_merges, + N_("mode"), + N_("try to rebase merges instead of skipping them"), + PARSE_OPT_OPTARG, NULL, (intptr_t)""}, OPT_END(), }; @@ -940,6 +950,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.cmd = xstrdup(buf.buf); } + if (rebase_merges) { + if (!*rebase_merges) + ; /* default mode; do nothing */ + else if (!strcmp("rebase-cousins", rebase_merges)) + options.rebase_cousins = 1; + else if (strcmp("no-rebase-cousins", rebase_merges)) + die(_("Unknown mode: %s"), rebase_merges); + options.rebase_merges = 1; + imply_interactive(&options, "--rebase-merges"); + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From 685f995174f35cdc44a96cb50f8f0aa9c5983b85 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:08 -0700 Subject: [PATCH 137/406] merge-base --fork-point: extract libified function We need this functionality in the builtin rebase. Note: to make this function truly reusable, we have to switch the call get_merges_many_dirty() to get_merges_many() because we want the commit flags to be reset (otherwise, subsequent get_merge_bases() calls would obtain incorrect results). This did not matter when the function was called in `git rev-parse --fork-point` because in that command, the process definitely did not traverse any commits before exiting. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/merge-base.c | 81 ++++---------------------------------------- commit.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ commit.h | 2 ++ 3 files changed, 89 insertions(+), 75 deletions(-) diff --git a/builtin/merge-base.c b/builtin/merge-base.c index 08d91b1f0c0172..790ceaeed68fae 100644 --- a/builtin/merge-base.c +++ b/builtin/merge-base.c @@ -110,54 +110,12 @@ static int handle_is_ancestor(int argc, const char **argv) return 1; } -struct rev_collect { - struct commit **commit; - int nr; - int alloc; - unsigned int initial : 1; -}; - -static void add_one_commit(struct object_id *oid, struct rev_collect *revs) -{ - struct commit *commit; - - if (is_null_oid(oid)) - return; - - commit = lookup_commit(the_repository, oid); - if (!commit || - (commit->object.flags & TMP_MARK) || - parse_commit(commit)) - return; - - ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc); - revs->commit[revs->nr++] = commit; - commit->object.flags |= TMP_MARK; -} - -static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid, - const char *ident, timestamp_t timestamp, - int tz, const char *message, void *cbdata) -{ - struct rev_collect *revs = cbdata; - - if (revs->initial) { - revs->initial = 0; - add_one_commit(ooid, revs); - } - add_one_commit(noid, revs); - return 0; -} - static int handle_fork_point(int argc, const char **argv) { struct object_id oid; char *refname; + struct commit *derived, *fork_point; const char *commitname; - struct rev_collect revs; - struct commit *derived; - struct commit_list *bases; - int i, ret = 0; switch (dwim_ref(argv[0], strlen(argv[0]), &oid, &refname)) { case 0: @@ -173,41 +131,14 @@ static int handle_fork_point(int argc, const char **argv) die("Not a valid object name: '%s'", commitname); derived = lookup_commit_reference(the_repository, &oid); - memset(&revs, 0, sizeof(revs)); - revs.initial = 1; - for_each_reflog_ent(refname, collect_one_reflog_ent, &revs); - if (!revs.nr && !get_oid(refname, &oid)) - add_one_commit(&oid, &revs); + fork_point = get_fork_point(refname, derived); - for (i = 0; i < revs.nr; i++) - revs.commit[i]->object.flags &= ~TMP_MARK; - - bases = get_merge_bases_many_dirty(derived, revs.nr, revs.commit); - - /* - * There should be one and only one merge base, when we found - * a common ancestor among reflog entries. - */ - if (!bases || bases->next) { - ret = 1; - goto cleanup_return; - } - - /* And the found one must be one of the reflog entries */ - for (i = 0; i < revs.nr; i++) - if (&bases->item->object == &revs.commit[i]->object) - break; /* found */ - if (revs.nr <= i) { - ret = 1; /* not found */ - goto cleanup_return; - } - - printf("%s\n", oid_to_hex(&bases->item->object.oid)); + if (!fork_point) + return 1; -cleanup_return: - free_commit_list(bases); - return ret; + printf("%s\n", oid_to_hex(&fork_point->object.oid)); + return 0; } int cmd_merge_base(int argc, const char **argv, const char *prefix) diff --git a/commit.c b/commit.c index 449c1f4920cff6..2d94e0b19984e2 100644 --- a/commit.c +++ b/commit.c @@ -17,6 +17,7 @@ #include "sha1-lookup.h" #include "wt-status.h" #include "advice.h" +#include "refs.h" static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **); @@ -961,6 +962,86 @@ static struct commit_list *merge_bases_many(struct commit *one, int n, struct co return result; } +struct rev_collect { + struct commit **commit; + int nr; + int alloc; + unsigned int initial : 1; +}; + +static void add_one_commit(struct object_id *oid, struct rev_collect *revs) +{ + struct commit *commit; + + if (is_null_oid(oid)) + return; + + commit = lookup_commit(the_repository, oid); + if (!commit || + (commit->object.flags & TMP_MARK) || + parse_commit(commit)) + return; + + ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc); + revs->commit[revs->nr++] = commit; + commit->object.flags |= TMP_MARK; +} + +static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid, + const char *ident, timestamp_t timestamp, + int tz, const char *message, void *cbdata) +{ + struct rev_collect *revs = cbdata; + + if (revs->initial) { + revs->initial = 0; + add_one_commit(ooid, revs); + } + add_one_commit(noid, revs); + return 0; +} + +struct commit *get_fork_point(const char *refname, struct commit *commit) +{ + struct object_id oid; + struct rev_collect revs; + struct commit_list *bases; + int i; + struct commit *ret = NULL; + + memset(&revs, 0, sizeof(revs)); + revs.initial = 1; + for_each_reflog_ent(refname, collect_one_reflog_ent, &revs); + + if (!revs.nr && !get_oid(refname, &oid)) + add_one_commit(&oid, &revs); + + for (i = 0; i < revs.nr; i++) + revs.commit[i]->object.flags &= ~TMP_MARK; + + bases = get_merge_bases_many(commit, revs.nr, revs.commit); + + /* + * There should be one and only one merge base, when we found + * a common ancestor among reflog entries. + */ + if (!bases || bases->next) + goto cleanup_return; + + /* And the found one must be one of the reflog entries */ + for (i = 0; i < revs.nr; i++) + if (&bases->item->object == &revs.commit[i]->object) + break; /* found */ + if (revs.nr <= i) + goto cleanup_return; + + ret = bases->item; + +cleanup_return: + free_commit_list(bases); + return ret; +} + struct commit_list *get_octopus_merge_bases(struct commit_list *in) { struct commit_list *i, *j, *k, *ret = NULL; diff --git a/commit.h b/commit.h index da0db36eba2bf1..b34240017f616d 100644 --- a/commit.h +++ b/commit.h @@ -211,6 +211,8 @@ extern struct commit_list *get_octopus_merge_bases(struct commit_list *in); /* To be used only when object flags after this call no longer matter */ extern struct commit_list *get_merge_bases_many_dirty(struct commit *one, int n, struct commit **twos); +struct commit *get_fork_point(const char *refname, struct commit *commit); + /* largest positive number a signed 32-bit integer can contain */ #define INFINITE_DEPTH 0x7fffffff From 39864ab3a32aded2d1c3fdf16dfef23f736c5709 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:09 -0700 Subject: [PATCH 138/406] builtin rebase: support `fork-point` option This commit adds support for `--fork-point` and `--no-fork-point`. This is converted as-is from `git-legacy-rebase.sh`. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 43b46998da0bc9..04d830408bc0f7 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -632,6 +632,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) struct string_list whitespace = STRING_LIST_INIT_NODUP; struct string_list exec = STRING_LIST_INIT_NODUP; const char *rebase_merges = NULL; + int fork_point = -1; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -715,6 +716,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) N_("mode"), N_("try to rebase merges instead of skipping them"), PARSE_OPT_OPTARG, NULL, (intptr_t)""}, + OPT_BOOL(0, "fork-point", &fork_point, + N_("use 'merge-base --fork-point' to refine upstream")), OPT_END(), }; @@ -1063,6 +1066,14 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } else BUG("unexpected number of arguments left to parse"); + if (fork_point > 0) { + struct commit *head = + lookup_commit_reference(the_repository, + &options.orig_head); + options.restrict_revision = + get_fork_point(options.upstream_name, head); + } + if (read_index(the_repository->index) < 0) die(_("could not read index")); From 63b30731e3e23d3d9b8d926f0b891ae21f9e4c1d Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:11 -0700 Subject: [PATCH 139/406] builtin rebase: add support for custom merge strategies When running a rebase in non-am mode, it uses the recursive merge to cherry-pick the commits, and the rebase command allows to configure the merge strategy to be used in this operation. This commit adds that support to the builtin rebase. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 04d830408bc0f7..c64d46afd53f3e 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -96,6 +96,7 @@ struct rebase_options { char *cmd; int allow_empty_message; int rebase_merges, rebase_cousins; + char *strategy, *strategy_opts; }; static int is_interactive(struct rebase_options *opts) @@ -217,6 +218,22 @@ static int read_basic_state(struct rebase_options *opts) opts->gpg_sign_opt = xstrdup(buf.buf); } + if (file_exists(state_dir_path("strategy", opts))) { + strbuf_reset(&buf); + if (read_one(state_dir_path("strategy", opts), &buf)) + return -1; + free(opts->strategy); + opts->strategy = xstrdup(buf.buf); + } + + if (file_exists(state_dir_path("strategy_opts", opts))) { + strbuf_reset(&buf); + if (read_one(state_dir_path("strategy_opts", opts), &buf)) + return -1; + free(opts->strategy_opts); + opts->strategy_opts = xstrdup(buf.buf); + } + strbuf_release(&buf); return 0; @@ -356,6 +373,8 @@ static int run_specific_rebase(struct rebase_options *opts) opts->rebase_merges ? "t" : ""); add_var(&script_snippet, "rebase_cousins", opts->rebase_cousins ? "t" : ""); + add_var(&script_snippet, "strategy", opts->strategy); + add_var(&script_snippet, "strategy_opts", opts->strategy_opts); switch (opts->type) { case REBASE_AM: @@ -633,6 +652,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) struct string_list exec = STRING_LIST_INIT_NODUP; const char *rebase_merges = NULL; int fork_point = -1; + struct string_list strategy_options = STRING_LIST_INIT_NODUP; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -718,6 +738,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) PARSE_OPT_OPTARG, NULL, (intptr_t)""}, OPT_BOOL(0, "fork-point", &fork_point, N_("use 'merge-base --fork-point' to refine upstream")), + OPT_STRING('s', "strategy", &options.strategy, + N_("strategy"), N_("use the given merge strategy")), + OPT_STRING_LIST('X', "strategy-option", &strategy_options, + N_("option"), + N_("pass the argument through to the merge " + "strategy")), OPT_END(), }; @@ -964,6 +990,37 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) imply_interactive(&options, "--rebase-merges"); } + if (strategy_options.nr) { + int i; + + if (!options.strategy) + options.strategy = "recursive"; + + strbuf_reset(&buf); + for (i = 0; i < strategy_options.nr; i++) + strbuf_addf(&buf, " --%s", + strategy_options.items[i].string); + options.strategy_opts = xstrdup(buf.buf); + } + + if (options.strategy) { + options.strategy = xstrdup(options.strategy); + switch (options.type) { + case REBASE_AM: + die(_("--strategy requires --merge or --interactive")); + case REBASE_MERGE: + case REBASE_INTERACTIVE: + case REBASE_PRESERVE_MERGES: + /* compatible */ + break; + case REBASE_UNSPECIFIED: + options.type = REBASE_MERGE; + break; + default: + BUG("unhandled rebase type (%d)", options.type); + } + } + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From 8c486c07eaa3105dd76cecc88036ad9c707285db Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:21:30 +0545 Subject: [PATCH 140/406] builtin rebase: optionally auto-detect the upstream The `git rebase` command, when called without the `<upstream>` command-line argument, automatically looks for the upstream branch configured for the current branch. With this commit, the builtin rebase learned that trick, too. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index b2cf779f1b42a5..7e2f68eb282254 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -622,6 +622,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg, return 0; } +static void NORETURN error_on_missing_default_upstream(void) +{ + struct branch *current_branch = branch_get(NULL); + + printf(_("%s\n" + "Please specify which branch you want to rebase against.\n" + "See git-rebase(1) for details.\n" + "\n" + " git rebase '<branch>'\n" + "\n"), + current_branch ? _("There is no tracking information for " + "the current branch.") : + _("You are not currently on a branch.")); + + if (current_branch) { + const char *remote = current_branch->remote_name; + + if (!remote) + remote = _("<remote>"); + + printf(_("If you wish to set tracking information for this " + "branch you can do so with:\n" + "\n" + " git branch --set-upstream-to=%s/<branch> %s\n" + "\n"), + remote, current_branch->name); + } + exit(1); +} + int cmd_rebase(int argc, const char **argv, const char *prefix) { struct rebase_options options = { @@ -1057,9 +1087,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } if (!options.root) { - if (argc < 1) - die("TODO: handle @{upstream}"); - else { + if (argc < 1) { + struct branch *branch; + + branch = branch_get(NULL); + options.upstream_name = branch_get_upstream(branch, + NULL); + if (!options.upstream_name) + error_on_missing_default_upstream(); + if (fork_point < 0) + fork_point = 1; + } else { options.upstream_name = argv[0]; argc--; argv++; From 085fa8e2ebc0a31ed7a3fb4c5ea26722872fa779 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Tue, 4 Sep 2018 15:00:12 -0700 Subject: [PATCH 141/406] builtin rebase: support --root This option allows to rebase entire histories up to, and including, the root commit. The conversion from the shell script is straight-forward, apart from the fact that we do not have to write an empty tree in C. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index c64d46afd53f3e..b2cf779f1b42a5 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -76,6 +76,7 @@ struct rebase_options { const char *revisions; const char *switch_to; int root; + struct object_id *squash_onto; struct commit *restrict_revision; int dont_finish_rebase; enum { @@ -375,6 +376,9 @@ static int run_specific_rebase(struct rebase_options *opts) opts->rebase_cousins ? "t" : ""); add_var(&script_snippet, "strategy", opts->strategy); add_var(&script_snippet, "strategy_opts", opts->strategy_opts); + add_var(&script_snippet, "rebase_root", opts->root ? "t" : ""); + add_var(&script_snippet, "squash_onto", + opts->squash_onto ? oid_to_hex(opts->squash_onto) : ""); switch (opts->type) { case REBASE_AM: @@ -653,6 +657,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) const char *rebase_merges = NULL; int fork_point = -1; struct string_list strategy_options = STRING_LIST_INIT_NODUP; + struct object_id squash_onto; + char *squash_onto_name = NULL; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -744,6 +750,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) N_("option"), N_("pass the argument through to the merge " "strategy")), + OPT_BOOL(0, "root", &options.root, + N_("rebase all reachable commits up to the root(s)")), OPT_END(), }; @@ -1021,6 +1029,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } } + if (options.root && !options.onto_name) + imply_interactive(&options, "--root without --onto"); + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: @@ -1059,8 +1070,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (!options.upstream) die(_("invalid upstream '%s'"), options.upstream_name); options.upstream_arg = options.upstream_name; - } else - die("TODO: upstream for --root"); + } else { + if (!options.onto_name) { + if (commit_tree("", 0, the_hash_algo->empty_tree, NULL, + &squash_onto, NULL, NULL) < 0) + die(_("Could not create new root commit")); + options.squash_onto = &squash_onto; + options.onto_name = squash_onto_name = + xstrdup(oid_to_hex(&squash_onto)); + } + options.upstream_name = NULL; + options.upstream = NULL; + if (argc > 1) + usage_with_options(builtin_rebase_usage, + builtin_rebase_options); + options.upstream_arg = "--root"; + } /* Make sure the branch to rebase onto is valid. */ if (!options.onto_name) @@ -1208,6 +1233,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) */ if (can_fast_forward(options.onto, &options.orig_head, &merge_base) && !is_interactive(&options) && !options.restrict_revision && + options.upstream && !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) { int flag; @@ -1312,5 +1338,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) free(options.head_name); free(options.gpg_sign_opt); free(options.cmd); + free(squash_onto_name); return ret; } From f22a807d8d50ab22fa08d8ec0a3d9824592fb682 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:21:31 +0545 Subject: [PATCH 142/406] builtin rebase: optionally pass custom reflogs to reset_head() In the next patch, we will make use of that in the code that fast-forwards to `onto` whenever possible. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 7e2f68eb282254..49856d9bf76387 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -431,7 +431,8 @@ static int run_specific_rebase(struct rebase_options *opts) #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" static int reset_head(struct object_id *oid, const char *action, - const char *switch_to_branch, int detach_head) + const char *switch_to_branch, int detach_head, + const char *reflog_orig_head, const char *reflog_head) { struct object_id head_oid; struct tree_desc desc; @@ -506,20 +507,26 @@ static int reset_head(struct object_id *oid, const char *action, old_orig = &oid_old_orig; if (!get_oid("HEAD", &oid_orig)) { orig = &oid_orig; - strbuf_addstr(&msg, "updating ORIG_HEAD"); - update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, + if (!reflog_orig_head) { + strbuf_addstr(&msg, "updating ORIG_HEAD"); + reflog_orig_head = msg.buf; + } + update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0, UPDATE_REFS_MSG_ON_ERR); } else if (old_orig) delete_ref(NULL, "ORIG_HEAD", old_orig, 0); - strbuf_setlen(&msg, prefix_len); - strbuf_addstr(&msg, "updating HEAD"); + if (!reflog_head) { + strbuf_setlen(&msg, prefix_len); + strbuf_addstr(&msg, "updating HEAD"); + reflog_head = msg.buf; + } if (!switch_to_branch) - ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF, + ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR); else { ret = create_symref("HEAD", switch_to_branch, msg.buf); if (!ret) - ret = update_ref(msg.buf, "HEAD", oid, NULL, 0, + ret = update_ref(reflog_head, "HEAD", oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR); } @@ -900,7 +907,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) rerere_clear(&merge_rr); string_list_clear(&merge_rr, 1); - if (reset_head(NULL, "reset", NULL, 0) < 0) + if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0) die(_("could not discard worktree changes")); if (read_basic_state(&options)) exit(1); @@ -916,7 +923,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (read_basic_state(&options)) exit(1); if (reset_head(&options.orig_head, "reset", - options.head_name, 0) < 0) + options.head_name, 0, NULL, NULL) < 0) die(_("could not move back to %s"), oid_to_hex(&options.orig_head)); ret = finish_rebase(&options); @@ -1236,7 +1243,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) write_file(autostash, "%s", buf.buf); printf(_("Created autostash: %s\n"), buf.buf); if (reset_head(&head->object.oid, "reset --hard", - NULL, 0) < 0) + NULL, 0, NULL, NULL) < 0) die(_("could not reset --hard")); printf(_("HEAD is now at %s"), find_unique_abbrev(&head->object.oid, @@ -1290,7 +1297,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_addf(&buf, "rebase: checkout %s", options.switch_to); if (reset_head(&oid, "checkout", - options.head_name, 0) < 0) { + options.head_name, 0, + NULL, NULL) < 0) { ret = !!error(_("could not switch to " "%s"), options.switch_to); @@ -1355,7 +1363,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) "it...\n")); strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); - if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) + if (reset_head(&options.onto->object.oid, "checkout", NULL, 1, + NULL, msg.buf)) die(_("Could not detach HEAD")); strbuf_release(&msg); From 1703262e4188dfe2580b22ec47279efebe0ad75f Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:28 +0200 Subject: [PATCH 143/406] sequencer: make three functions and an enum from sequencer.c public This makes rebase_path_todo(), get_missing_commit_check_level(), write_message() and the enum check_level accessible outside sequencer.c, renames check_level to missing_commit_check_level, and prefixes its value names by MISSING_COMMIT_ to avoid namespace pollution. This function and this enum will eventually be moved to rebase-interactive.c and become static again, so no special attention was given to the naming. This will be needed for the rewrite of append_todo_help() from shell to C, as it will be in a new library source file, rebase-interactive.c. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- sequencer.c | 26 +++++++++++--------------- sequencer.h | 11 +++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/sequencer.c b/sequencer.c index dc2c58d464c14b..8f65432a92dd51 100644 --- a/sequencer.c +++ b/sequencer.c @@ -52,7 +52,7 @@ static GIT_PATH_FUNC(rebase_path, "rebase-merge") * the lines are processed, they are removed from the front of this * file and written to the tail of 'done'. */ -static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") +GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") /* * The rebase command lines that have already been processed. A line * is moved here when it is first handled, before any associated user @@ -373,8 +373,8 @@ static void print_advice(int show_hint, struct replay_opts *opts) } } -static int write_message(const void *buf, size_t len, const char *filename, - int append_eol) +int write_message(const void *buf, size_t len, const char *filename, + int append_eol) { struct lock_file msg_file = LOCK_INIT; @@ -4407,24 +4407,20 @@ int transform_todos(unsigned flags) return i; } -enum check_level { - CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR -}; - -static enum check_level get_missing_commit_check_level(void) +enum missing_commit_check_level get_missing_commit_check_level(void) { const char *value; if (git_config_get_value("rebase.missingcommitscheck", &value) || !strcasecmp("ignore", value)) - return CHECK_IGNORE; + return MISSING_COMMIT_CHECK_IGNORE; if (!strcasecmp("warn", value)) - return CHECK_WARN; + return MISSING_COMMIT_CHECK_WARN; if (!strcasecmp("error", value)) - return CHECK_ERROR; + return MISSING_COMMIT_CHECK_ERROR; warning(_("unrecognized setting %s for option " "rebase.missingCommitsCheck. Ignoring."), value); - return CHECK_IGNORE; + return MISSING_COMMIT_CHECK_IGNORE; } define_commit_slab(commit_seen, unsigned char); @@ -4436,7 +4432,7 @@ define_commit_slab(commit_seen, unsigned char); */ int check_todo_list(void) { - enum check_level check_level = get_missing_commit_check_level(); + enum missing_commit_check_level check_level = get_missing_commit_check_level(); struct strbuf todo_file = STRBUF_INIT; struct todo_list todo_list = TODO_LIST_INIT; struct strbuf missing = STRBUF_INIT; @@ -4453,7 +4449,7 @@ int check_todo_list(void) advise_to_edit_todo = res = parse_insn_buffer(todo_list.buf.buf, &todo_list); - if (res || check_level == CHECK_IGNORE) + if (res || check_level == MISSING_COMMIT_CHECK_IGNORE) goto leave_check; /* Mark the commits in git-rebase-todo as seen */ @@ -4488,7 +4484,7 @@ int check_todo_list(void) if (!missing.len) goto leave_check; - if (check_level == CHECK_ERROR) + if (check_level == MISSING_COMMIT_CHECK_ERROR) advise_to_edit_todo = res = 1; fprintf(stderr, diff --git a/sequencer.h b/sequencer.h index c751c9d6e4f78e..63e18a10351928 100644 --- a/sequencer.h +++ b/sequencer.h @@ -8,6 +8,7 @@ struct commit; const char *git_path_commit_editmsg(void); const char *git_path_seq_dir(void); +const char *rebase_path_todo(void); #define APPEND_SIGNOFF_DEDUP (1u << 0) @@ -62,6 +63,15 @@ struct replay_opts { }; #define REPLAY_OPTS_INIT { .action = -1, .current_fixups = STRBUF_INIT } +enum missing_commit_check_level { + MISSING_COMMIT_CHECK_IGNORE = 0, + MISSING_COMMIT_CHECK_WARN, + MISSING_COMMIT_CHECK_ERROR +}; + +int write_message(const void *buf, size_t len, const char *filename, + int append_eol); + /* Call this to setup defaults before parsing command line options */ void sequencer_init_config(struct replay_opts *opts); int sequencer_pick_revisions(struct replay_opts *opts); @@ -84,6 +94,7 @@ int sequencer_make_script(FILE *out, int argc, const char **argv, int sequencer_add_exec_commands(const char *command); int transform_todos(unsigned flags); +enum missing_commit_check_level get_missing_commit_check_level(void); int check_todo_list(void); int skip_unnecessary_picks(void); int rearrange_squash(void); From 2e62cdb83cfb6c1ea1e2fc099fff03248c8f8107 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:21:32 +0545 Subject: [PATCH 144/406] builtin rebase: fast-forward to onto if it is a proper descendant When trying to rebase onto a direct descendant of HEAD, we can take a shortcut and fast-forward instead. This commit makes it so. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 49856d9bf76387..7522be837c8951 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1368,6 +1368,24 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("Could not detach HEAD")); strbuf_release(&msg); + /* + * If the onto is a proper descendant of the tip of the branch, then + * we just fast-forwarded. + */ + strbuf_reset(&msg); + if (!oidcmp(&merge_base, &options.orig_head)) { + printf(_("Fast-forwarded %s to %s. \n"), + branch_name, options.onto_name); + strbuf_addf(&msg, "rebase finished: %s onto %s", + options.head_name ? options.head_name : "detached HEAD", + oid_to_hex(&options.onto->object.oid)); + reset_head(NULL, "Fast-forwarded", options.head_name, 0, + "HEAD", msg.buf); + strbuf_release(&msg); + ret = !!finish_rebase(&options); + goto cleanup; + } + strbuf_addf(&revisions, "%s..%s", options.root ? oid_to_hex(&options.onto->object.oid) : (options.restrict_revision ? From a91ad9fbf5a67515cee8f51bf988e5eff2e815b3 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:29 +0200 Subject: [PATCH 145/406] rebase -i: rewrite append_todo_help() in C This rewrites append_todo_help() from shell to C. It also incorporates some parts of initiate_action() and complete_action() that also write help texts to the todo file. This also introduces the source file rebase-interactive.c. This file will contain functions necessary for interactive rebase that are too specific for the sequencer, and is part of libgit.a. Two flags are added to rebase--helper.c: one to call append_todo_help() (`--append-todo-help`), and another one to tell append_todo_help() to write the help text suited for the edit-todo mode (`--write-edit-todo`). Finally, append_todo_help() is removed from git-rebase--interactive.sh to use `rebase--helper --append-todo-help` instead. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- Makefile | 1 + builtin/rebase--helper.c | 11 ++++-- git-rebase--interactive.sh | 52 ++--------------------------- rebase-interactive.c | 68 ++++++++++++++++++++++++++++++++++++++ rebase-interactive.h | 6 ++++ 5 files changed, 86 insertions(+), 52 deletions(-) create mode 100644 rebase-interactive.c create mode 100644 rebase-interactive.h diff --git a/Makefile b/Makefile index 5a969f5830a410..e819e4dcb62c71 100644 --- a/Makefile +++ b/Makefile @@ -935,6 +935,7 @@ LIB_OBJS += quote.o LIB_OBJS += range-diff.o LIB_OBJS += reachable.o LIB_OBJS += read-cache.o +LIB_OBJS += rebase-interactive.o LIB_OBJS += reflog-walk.o LIB_OBJS += refs.o LIB_OBJS += refs/files-backend.o diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index f7c2a5fdc815a8..05e73e71d42b09 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -3,6 +3,7 @@ #include "config.h" #include "parse-options.h" #include "sequencer.h" +#include "rebase-interactive.h" static const char * const builtin_rebase_helper_usage[] = { N_("git rebase--helper [<options>]"), @@ -12,12 +13,12 @@ static const char * const builtin_rebase_helper_usage[] = { int cmd_rebase__helper(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; - unsigned flags = 0, keep_empty = 0, rebase_merges = 0; + unsigned flags = 0, keep_empty = 0, rebase_merges = 0, write_edit_todo = 0; int abbreviate_commands = 0, rebase_cousins = -1; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, - ADD_EXEC + ADD_EXEC, APPEND_TODO_HELP } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -27,6 +28,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), OPT_BOOL(0, "rebase-cousins", &rebase_cousins, N_("keep original branch points of cousins")), + OPT_BOOL(0, "write-edit-todo", &write_edit_todo, + N_("append the edit-todo message to the todo-list")), OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), CONTINUE), OPT_CMDMODE(0, "abort", &command, N_("abort rebase"), @@ -45,6 +48,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("rearrange fixup/squash lines"), REARRANGE_SQUASH), OPT_CMDMODE(0, "add-exec-commands", &command, N_("insert exec commands in todo list"), ADD_EXEC), + OPT_CMDMODE(0, "append-todo-help", &command, + N_("insert the help in the todo list"), APPEND_TODO_HELP), OPT_END() }; @@ -84,5 +89,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!rearrange_squash(); if (command == ADD_EXEC && argc == 2) return !!sequencer_add_exec_commands(argv[1]); + if (command == APPEND_TODO_HELP && argc == 1) + return !!append_todo_help(write_edit_todo, keep_empty); usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 299ded21375ed3..94c23a7af2291f 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -39,38 +39,6 @@ comment_for_reflog () { esac } -append_todo_help () { - gettext " -Commands: -p, pick <commit> = use commit -r, reword <commit> = use commit, but edit the commit message -e, edit <commit> = use commit, but stop for amending -s, squash <commit> = use commit, but meld into previous commit -f, fixup <commit> = like \"squash\", but discard this commit's log message -x, exec <command> = run command (the rest of the line) using shell -d, drop <commit> = remove commit -l, label <label> = label current HEAD with a name -t, reset <label> = reset HEAD to a label -m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] -. create a merge commit using the original merge commit's -. message (or the oneline, if no original merge commit was -. specified). Use -c <commit> to reword the commit message. - -These lines can be re-ordered; they are executed from top to bottom. -" | git stripspace --comment-lines >>"$todo" - - if test $(get_missing_commit_check_level) = error - then - gettext " -Do not remove any line. Use 'drop' explicitly to remove a commit. -" | git stripspace --comment-lines >>"$todo" - else - gettext " -If you remove a line here THAT COMMIT WILL BE LOST. -" | git stripspace --comment-lines >>"$todo" - fi -} - die_abort () { apply_autostash rm -rf "$state_dir" @@ -143,13 +111,7 @@ initiate_action () { git stripspace --strip-comments <"$todo" >"$todo".new mv -f "$todo".new "$todo" collapse_todo_ids - append_todo_help - gettext " -You are editing the todo file of an ongoing interactive rebase. -To continue rebase after editing, run: - git rebase --continue - -" | git stripspace --comment-lines >>"$todo" + git rebase--helper --append-todo-help --write-edit-todo git_sequence_editor "$todo" || die "$(gettext "Could not execute editor")" @@ -220,17 +182,7 @@ $comment_char $(eval_ngettext \ "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \ "$todocount") EOF - append_todo_help - gettext " - However, if you remove everything, the rebase will be aborted. - - " | git stripspace --comment-lines >>"$todo" - - if test -z "$keep_empty" - then - printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo" - fi - + git rebase--helper --append-todo-help ${keep_empty:+--keep-empty} has_action "$todo" || return 2 diff --git a/rebase-interactive.c b/rebase-interactive.c new file mode 100644 index 00000000000000..d7996bc8d9ff2e --- /dev/null +++ b/rebase-interactive.c @@ -0,0 +1,68 @@ +#include "cache.h" +#include "commit.h" +#include "rebase-interactive.h" +#include "sequencer.h" +#include "strbuf.h" + +int append_todo_help(unsigned edit_todo, unsigned keep_empty) +{ + struct strbuf buf = STRBUF_INIT; + FILE *todo; + int ret; + const char *msg = _("\nCommands:\n" +"p, pick <commit> = use commit\n" +"r, reword <commit> = use commit, but edit the commit message\n" +"e, edit <commit> = use commit, but stop for amending\n" +"s, squash <commit> = use commit, but meld into previous commit\n" +"f, fixup <commit> = like \"squash\", but discard this commit's log message\n" +"x, exec <command> = run command (the rest of the line) using shell\n" +"d, drop <commit> = remove commit\n" +"l, label <label> = label current HEAD with a name\n" +"t, reset <label> = reset HEAD to a label\n" +"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n" +". create a merge commit using the original merge commit's\n" +". message (or the oneline, if no original merge commit was\n" +". specified). Use -c <commit> to reword the commit message.\n" +"\n" +"These lines can be re-ordered; they are executed from top to bottom.\n"); + + todo = fopen_or_warn(rebase_path_todo(), "a"); + if (!todo) + return 1; + + strbuf_add_commented_lines(&buf, msg, strlen(msg)); + + if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR) + msg = _("\nDo not remove any line. Use 'drop' " + "explicitly to remove a commit.\n"); + else + msg = _("\nIf you remove a line here " + "THAT COMMIT WILL BE LOST.\n"); + + strbuf_add_commented_lines(&buf, msg, strlen(msg)); + + if (edit_todo) + msg = _("\nYou are editing the todo file " + "of an ongoing interactive rebase.\n" + "To continue rebase after editing, run:\n" + " git rebase --continue\n\n"); + else + msg = _("\nHowever, if you remove everything, " + "the rebase will be aborted.\n\n"); + + strbuf_add_commented_lines(&buf, msg, strlen(msg)); + + if (!keep_empty) { + msg = _("Note that empty commits are commented out"); + strbuf_add_commented_lines(&buf, msg, strlen(msg)); + } + + ret = fputs(buf.buf, todo); + if (ret < 0) + error_errno(_("could not append help text to '%s'"), rebase_path_todo()); + + fclose(todo); + strbuf_release(&buf); + + return ret; +} diff --git a/rebase-interactive.h b/rebase-interactive.h new file mode 100644 index 00000000000000..47372624e03595 --- /dev/null +++ b/rebase-interactive.h @@ -0,0 +1,6 @@ +#ifndef REBASE_INTERACTIVE_H +#define REBASE_INTERACTIVE_H + +int append_todo_help(unsigned edit_todo, unsigned keep_empty); + +#endif From fc3d2c1d2ae21cc9ed720040434c61a0aae358d4 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:21:33 +0545 Subject: [PATCH 146/406] builtin rebase: show progress when connected to a terminal In this commit, we pass `--progress` to the `format-patch` command if stderr is connected to an interactive terminal, unless we're in quiet mode. This `--progress` option will be used in `format-patch` to show progress reports on stderr as patches are generated. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 7522be837c8951..600788bf725ff5 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -98,6 +98,7 @@ struct rebase_options { int allow_empty_message; int rebase_merges, rebase_cousins; char *strategy, *strategy_opts; + struct strbuf git_format_patch_opt; }; static int is_interactive(struct rebase_options *opts) @@ -379,6 +380,8 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "rebase_root", opts->root ? "t" : ""); add_var(&script_snippet, "squash_onto", opts->squash_onto ? oid_to_hex(opts->squash_onto) : ""); + add_var(&script_snippet, "git_format_patch_opt", + opts->git_format_patch_opt.buf); switch (opts->type) { case REBASE_AM: @@ -667,6 +670,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) .git_am_opt = STRBUF_INIT, .allow_rerere_autoupdate = -1, .allow_empty_message = 1, + .git_format_patch_opt = STRBUF_INIT, }; const char *branch_name; int ret, flags, total_argc, in_progress = 0; @@ -1069,6 +1073,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (options.root && !options.onto_name) imply_interactive(&options, "--root without --onto"); + if (isatty(2) && options.flags & REBASE_NO_QUIET) + strbuf_addstr(&options.git_format_patch_opt, " --progress"); + switch (options.type) { case REBASE_MERGE: case REBASE_INTERACTIVE: From 645abc2fe29003d64f156b9f70c3bda8a40338a2 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:30 +0200 Subject: [PATCH 147/406] editor: add a function to launch the sequence editor As part of the rewrite of interactive rebase, the sequencer will need to open the sequence editor to allow the user to edit the todo list. Instead of duplicating the existing launch_editor() function, this refactors it to a new function, launch_specified_editor(), which takes the editor as a parameter, in addition to the path, the buffer and the environment variables. launch_sequence_editor() is then added to launch the sequence editor. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- cache.h | 1 + editor.c | 27 +++++++++++++++++++++++++-- strbuf.h | 2 ++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cache.h b/cache.h index 4d014541ab7bc7..d1533775929786 100644 --- a/cache.h +++ b/cache.h @@ -1472,6 +1472,7 @@ extern const char *fmt_name(const char *name, const char *email); extern const char *ident_default_name(void); extern const char *ident_default_email(void); extern const char *git_editor(void); +extern const char *git_sequence_editor(void); extern const char *git_pager(int stdout_is_tty); extern int is_terminal_dumb(void); extern int git_ident_config(const char *, const char *, void *); diff --git a/editor.c b/editor.c index 9a9b4e12d1db7f..c985eee1f9d1c7 100644 --- a/editor.c +++ b/editor.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "config.h" #include "strbuf.h" #include "run-command.h" #include "sigchain.h" @@ -34,10 +35,21 @@ const char *git_editor(void) return editor; } -int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) +const char *git_sequence_editor(void) { - const char *editor = git_editor(); + const char *editor = getenv("GIT_SEQUENCE_EDITOR"); + + if (!editor) + git_config_get_string_const("sequence.editor", &editor); + if (!editor) + editor = git_editor(); + return editor; +} + +static int launch_specified_editor(const char *editor, const char *path, + struct strbuf *buffer, const char *const *env) +{ if (!editor) return error("Terminal is dumb, but EDITOR unset"); @@ -95,3 +107,14 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en return error_errno("could not read file '%s'", path); return 0; } + +int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) +{ + return launch_specified_editor(git_editor(), path, buffer, env); +} + +int launch_sequence_editor(const char *path, struct strbuf *buffer, + const char *const *env) +{ + return launch_specified_editor(git_sequence_editor(), path, buffer, env); +} diff --git a/strbuf.h b/strbuf.h index 60a35aef165ca1..66da9822fd860c 100644 --- a/strbuf.h +++ b/strbuf.h @@ -575,6 +575,8 @@ extern void strbuf_add_unique_abbrev(struct strbuf *sb, * file's contents are not read into the buffer upon completion. */ extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env); +extern int launch_sequence_editor(const char *path, struct strbuf *buffer, + const char *const *env); extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size); From 083b772f14cc0b611d4bd88340409e7f62712d99 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:21:34 +0545 Subject: [PATCH 148/406] builtin rebase: use no-op editor when interactive is "implied" Some options are only handled by the git-rebase--interactive backend, even if run non-interactively. For this awkward situation (run non-interactively, but use the interactive backend), the shell scripted version of `git rebase` introduced the concept of an "implied interactive rebase". All it does is to replace the editor by a dummy one (`:` is the Unix command that takes arbitrary command-line parameters, ignores them and simply exits with success). Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 600788bf725ff5..36d311b42fb9eb 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -383,6 +383,13 @@ static int run_specific_rebase(struct rebase_options *opts) add_var(&script_snippet, "git_format_patch_opt", opts->git_format_patch_opt.buf); + if (is_interactive(opts) && + !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) { + strbuf_addstr(&script_snippet, + "GIT_EDITOR=:; export GIT_EDITOR; "); + opts->autosquash = 0; + } + switch (opts->type) { case REBASE_AM: backend = "git-rebase--am"; From 4167334c7843e2b32d54ea5fdeb0af626d97f3a0 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:31 +0200 Subject: [PATCH 149/406] rebase -i: rewrite the edit-todo functionality in C This rewrites the edit-todo functionality from shell to C. To achieve that, a new command mode, `edit-todo`, is added, and the `write-edit-todo` flag is removed, as the shell script does not need to write the edit todo help message to the todo list anymore. The shell version is then stripped in favour of a call to the helper. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 13 ++++++++----- git-rebase--interactive.sh | 11 +---------- rebase-interactive.c | 27 +++++++++++++++++++++++++++ rebase-interactive.h | 1 + 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 05e73e71d42b09..731a64971d6bc2 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -13,12 +13,12 @@ static const char * const builtin_rebase_helper_usage[] = { int cmd_rebase__helper(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; - unsigned flags = 0, keep_empty = 0, rebase_merges = 0, write_edit_todo = 0; + unsigned flags = 0, keep_empty = 0, rebase_merges = 0; int abbreviate_commands = 0, rebase_cousins = -1; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, - ADD_EXEC, APPEND_TODO_HELP + ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -28,8 +28,6 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), OPT_BOOL(0, "rebase-cousins", &rebase_cousins, N_("keep original branch points of cousins")), - OPT_BOOL(0, "write-edit-todo", &write_edit_todo, - N_("append the edit-todo message to the todo-list")), OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), CONTINUE), OPT_CMDMODE(0, "abort", &command, N_("abort rebase"), @@ -50,6 +48,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("insert exec commands in todo list"), ADD_EXEC), OPT_CMDMODE(0, "append-todo-help", &command, N_("insert the help in the todo list"), APPEND_TODO_HELP), + OPT_CMDMODE(0, "edit-todo", &command, + N_("edit the todo list during an interactive rebase"), + EDIT_TODO), OPT_END() }; @@ -90,6 +91,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) if (command == ADD_EXEC && argc == 2) return !!sequencer_add_exec_commands(argv[1]); if (command == APPEND_TODO_HELP && argc == 1) - return !!append_todo_help(write_edit_todo, keep_empty); + return !!append_todo_help(0, keep_empty); + if (command == EDIT_TODO && argc == 1) + return !!edit_todo_list(flags); usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 94c23a7af2291f..2defe607f44722 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -108,16 +108,7 @@ initiate_action () { --continue ;; edit-todo) - git stripspace --strip-comments <"$todo" >"$todo".new - mv -f "$todo".new "$todo" - collapse_todo_ids - git rebase--helper --append-todo-help --write-edit-todo - - git_sequence_editor "$todo" || - die "$(gettext "Could not execute editor")" - expand_todo_ids - - exit + exec git rebase--helper --edit-todo ;; show-current-patch) exec git show REBASE_HEAD -- diff --git a/rebase-interactive.c b/rebase-interactive.c index d7996bc8d9ff2e..3f9468fc6997d5 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -66,3 +66,30 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty) return ret; } + +int edit_todo_list(unsigned flags) +{ + struct strbuf buf = STRBUF_INIT; + const char *todo_file = rebase_path_todo(); + + if (strbuf_read_file(&buf, todo_file, 0) < 0) + return error_errno(_("could not read '%s'."), todo_file); + + strbuf_stripspace(&buf, 1); + if (write_message(buf.buf, buf.len, todo_file, 0)) { + strbuf_release(&buf); + return -1; + } + + strbuf_release(&buf); + + transform_todos(flags | TODO_LIST_SHORTEN_IDS); + append_todo_help(1, 0); + + if (launch_sequence_editor(todo_file, NULL, NULL)) + return -1; + + transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS)); + + return 0; +} diff --git a/rebase-interactive.h b/rebase-interactive.h index 47372624e03595..155219e742d223 100644 --- a/rebase-interactive.h +++ b/rebase-interactive.h @@ -2,5 +2,6 @@ #define REBASE_INTERACTIVE_H int append_todo_help(unsigned edit_todo, unsigned keep_empty); +int edit_todo_list(unsigned flags); #endif From b829c5560711be4aee6a7b18f7bc77e205b37289 Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:21:35 +0545 Subject: [PATCH 150/406] builtin rebase: error out on incompatible option/mode combinations While working on the GSoC project to convert the rebase command to a builtin, the rebase command learned to error out on certain command-line option combinations that cannot work, such as --whitespace=fix with --interactive. This commit converts that code. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 36d311b42fb9eb..d3d1d39bfa0fd6 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1099,6 +1099,28 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) break; } + if (options.git_am_opt.len) { + const char *p; + + /* all am options except -q are compatible only with --am */ + strbuf_reset(&buf); + strbuf_addbuf(&buf, &options.git_am_opt); + strbuf_addch(&buf, ' '); + while ((p = strstr(buf.buf, " -q "))) + strbuf_splice(&buf, p - buf.buf, 4, " ", 1); + strbuf_trim(&buf); + + if (is_interactive(&options) && buf.len) + die(_("error: cannot combine interactive options " + "(--interactive, --exec, --rebase-merges, " + "--preserve-merges, --keep-empty, --root + " + "--onto) with am options (%s)"), buf.buf); + if (options.type == REBASE_MERGE && buf.len) + die(_("error: cannot combine merge options (--merge, " + "--strategy, --strategy-option) with am options " + "(%s)"), buf.buf); + } + if (options.signoff) { if (options.type == REBASE_PRESERVE_MERGES) die("cannot combine '--signoff' with " @@ -1107,6 +1129,25 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.flags |= REBASE_FORCE; } + if (options.type == REBASE_PRESERVE_MERGES) + /* + * Note: incompatibility with --signoff handled in signoff block above + * Note: incompatibility with --interactive is just a strong warning; + * git-rebase.txt caveats with "unless you know what you are doing" + */ + if (options.rebase_merges) + die(_("error: cannot combine '--preserve_merges' with " + "'--rebase-merges'")); + + if (options.rebase_merges) { + if (strategy_options.nr) + die(_("error: cannot combine '--rebase_merges' with " + "'--strategy-option'")); + if (options.strategy) + die(_("error: cannot combine '--rebase_merges' with " + "'--strategy'")); + } + if (!options.root) { if (argc < 1) { struct branch *branch; From ec75152a3c1cdbe2f03cc0b425a955218623843c Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:32 +0200 Subject: [PATCH 151/406] sequencer: add a new function to silence a command, except if it fails This adds a new function, run_command_silent_on_success(), to redirect the stdout and stderr of a command to a strbuf, and then to run that command. This strbuf is printed only if the command fails. It is functionnaly similar to output() from git-rebase.sh. run_git_commit() is then refactored to use of run_command_silent_on_success(). Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- sequencer.c | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/sequencer.c b/sequencer.c index 8f65432a92dd51..02b69ceee2af47 100644 --- a/sequencer.c +++ b/sequencer.c @@ -800,6 +800,23 @@ N_("you have staged changes in your working tree\n" #define VERIFY_MSG (1<<4) #define CREATE_ROOT_COMMIT (1<<5) +static int run_command_silent_on_success(struct child_process *cmd) +{ + struct strbuf buf = STRBUF_INIT; + int rc; + + cmd->stdout_to_stderr = 1; + rc = pipe_command(cmd, + NULL, 0, + NULL, 0, + &buf, 0); + + if (rc) + fputs(buf.buf, stderr); + strbuf_release(&buf); + return rc; +} + /* * If we are cherry-pick, and if the merge did not result in * hand-editing, we will hit this commit and inherit the original @@ -861,18 +878,11 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts, cmd.git_cmd = 1; - if (is_rebase_i(opts)) { - if (!(flags & EDIT_MSG)) { - cmd.stdout_to_stderr = 1; - cmd.err = -1; - } + if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) { + const char *gpg_opt = gpg_sign_opt_quoted(opts); - if (read_env_script(&cmd.env_array)) { - const char *gpg_opt = gpg_sign_opt_quoted(opts); - - return error(_(staged_changes_advice), - gpg_opt, gpg_opt); - } + return error(_(staged_changes_advice), + gpg_opt, gpg_opt); } argv_array_push(&cmd.args, "commit"); @@ -902,21 +912,10 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts, if (opts->allow_empty_message) argv_array_push(&cmd.args, "--allow-empty-message"); - if (cmd.err == -1) { - /* hide stderr on success */ - struct strbuf buf = STRBUF_INIT; - int rc = pipe_command(&cmd, - NULL, 0, - /* stdout is already redirected */ - NULL, 0, - &buf, 0); - if (rc) - fputs(buf.buf, stderr); - strbuf_release(&buf); - return rc; - } - - return run_command(&cmd); + if (is_rebase_i(opts) && !(flags & EDIT_MSG)) + return run_command_silent_on_success(&cmd); + else + return run_command(&cmd); } static int rest_is_empty(const struct strbuf *sb, int start) From f1b659653ceabed2b6985cc54e4380aa9bb729af Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:33 +0200 Subject: [PATCH 152/406] rebase -i: rewrite setup_reflog_action() in C MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This rewrites (the misnamed) setup_reflog_action() from shell to C. The new version is called prepare_branch_to_be_rebased(). A new command is added to rebase--helper.c, “checkout-base”, as well as a new flag, “verbose”, to avoid silencing the output of the checkout operation called by checkout_base_commit(). The function `run_git_checkout()` will also be used in the next commit, therefore its code is not part of `checkout_base_commit()`. The shell version is then stripped in favour of a call to the helper. As $GIT_REFLOG_ACTION is no longer set at the first call of checkout_onto(), a call to comment_for_reflog() is added at the beginning of this function. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 7 ++++++- git-rebase--interactive.sh | 16 ++-------------- sequencer.c | 30 ++++++++++++++++++++++++++++++ sequencer.h | 2 ++ 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 731a64971d6bc2..0e76dadba6f461 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -18,7 +18,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, - ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO + ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -28,6 +28,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), OPT_BOOL(0, "rebase-cousins", &rebase_cousins, N_("keep original branch points of cousins")), + OPT__VERBOSE(&opts.verbose, N_("be verbose")), OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), CONTINUE), OPT_CMDMODE(0, "abort", &command, N_("abort rebase"), @@ -51,6 +52,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list during an interactive rebase"), EDIT_TODO), + OPT_CMDMODE(0, "prepare-branch", &command, + N_("prepare the branch to be rebased"), PREPARE_BRANCH), OPT_END() }; @@ -94,5 +97,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!append_todo_help(0, keep_empty); if (command == EDIT_TODO && argc == 1) return !!edit_todo_list(flags); + if (command == PREPARE_BRANCH && argc == 2) + return !!prepare_branch_to_be_rebased(&opts, argv[1]); usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 2defe607f44722..77e972bb6ce608 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -72,6 +72,7 @@ collapse_todo_ids() { # Switch to the branch in $into and notify it in the reflog checkout_onto () { + comment_for_reflog start GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" output git checkout $onto || die_abort "$(gettext "could not detach HEAD")" git update-ref ORIG_HEAD $orig_head @@ -119,19 +120,6 @@ initiate_action () { esac } -setup_reflog_action () { - comment_for_reflog start - - if test ! -z "$switch_to" - then - GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" - output git checkout "$switch_to" -- || - die "$(eval_gettext "Could not checkout \$switch_to")" - - comment_for_reflog start - fi -} - init_basic_state () { orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")" mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")" @@ -211,7 +199,7 @@ git_rebase__interactive () { return 0 fi - setup_reflog_action + git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose} init_basic_state init_revisions_and_shortrevisions diff --git a/sequencer.c b/sequencer.c index 02b69ceee2af47..998a858427060f 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3281,6 +3281,36 @@ static const char *reflog_message(struct replay_opts *opts, return buf.buf; } +static int run_git_checkout(struct replay_opts *opts, const char *commit, + const char *action) +{ + struct child_process cmd = CHILD_PROCESS_INIT; + + cmd.git_cmd = 1; + + argv_array_push(&cmd.args, "checkout"); + argv_array_push(&cmd.args, commit); + argv_array_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action); + + if (opts->verbose) + return run_command(&cmd); + else + return run_command_silent_on_success(&cmd); +} + +int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit) +{ + const char *action; + + if (commit && *commit) { + action = reflog_message(opts, "start", "checkout %s", commit); + if (run_git_checkout(opts, commit, action)) + return error(_("could not checkout %s"), commit); + } + + return 0; +} + static const char rescheduled_advice[] = N_("Could not execute the todo command\n" "\n" diff --git a/sequencer.h b/sequencer.h index 63e18a10351928..eb0323f65a36e3 100644 --- a/sequencer.h +++ b/sequencer.h @@ -114,6 +114,8 @@ int update_head_with_reflog(const struct commit *old_head, void commit_post_rewrite(const struct commit *current_head, const struct object_id *new_head); +int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit); + #define SUMMARY_INITIAL_COMMIT (1 << 0) #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1) void print_commit_summary(const char *prefix, const struct object_id *oid, From 60c397cbd82b459dd2fdeaa0d1cccab326930939 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:34 +0200 Subject: [PATCH 153/406] rebase -i: rewrite checkout_onto() in C MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This rewrites checkout_onto() from shell to C. A new command (“checkout-onto”) is added to rebase--helper.c. The shell version is then stripped. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 7 ++++++- git-rebase--interactive.sh | 25 ++++--------------------- sequencer.c | 19 +++++++++++++++++++ sequencer.h | 3 +++ 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 0e76dadba6f461..7d9426d23c78c7 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -18,7 +18,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, - ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH + ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH, + CHECKOUT_ONTO } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -54,6 +55,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) EDIT_TODO), OPT_CMDMODE(0, "prepare-branch", &command, N_("prepare the branch to be rebased"), PREPARE_BRANCH), + OPT_CMDMODE(0, "checkout-onto", &command, + N_("checkout a commit"), CHECKOUT_ONTO), OPT_END() }; @@ -99,5 +102,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!edit_todo_list(flags); if (command == PREPARE_BRANCH && argc == 2) return !!prepare_branch_to_be_rebased(&opts, argv[1]); + if (command == CHECKOUT_ONTO && argc == 4) + return !!checkout_onto(&opts, argv[1], argv[2], argv[3]); usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 77e972bb6ce608..b68f108f28dafb 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -28,17 +28,6 @@ case "$comment_char" in ;; esac -orig_reflog_action="$GIT_REFLOG_ACTION" - -comment_for_reflog () { - case "$orig_reflog_action" in - ''|rebase*) - GIT_REFLOG_ACTION="rebase -i ($1)" - export GIT_REFLOG_ACTION - ;; - esac -} - die_abort () { apply_autostash rm -rf "$state_dir" @@ -70,14 +59,6 @@ collapse_todo_ids() { git rebase--helper --shorten-ids } -# Switch to the branch in $into and notify it in the reflog -checkout_onto () { - comment_for_reflog start - GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" - output git checkout $onto || die_abort "$(gettext "could not detach HEAD")" - git update-ref ORIG_HEAD $orig_head -} - get_missing_commit_check_level () { check_level=$(git config --get rebase.missingCommitsCheck) check_level=${check_level:-ignore} @@ -176,7 +157,8 @@ EOF git rebase--helper --check-todo-list || { ret=$? - checkout_onto + git rebase--helper --checkout-onto "$onto_name" "$onto" \ + "$orig_head" ${verbose:+--verbose} exit $ret } @@ -186,7 +168,8 @@ EOF onto="$(git rebase--helper --skip-unnecessary-picks)" || die "Could not skip unnecessary pick commands" - checkout_onto + git rebase--helper --checkout-onto "$onto_name" "$onto" "$orig_head" \ + ${verbose:+--verbose} require_clean_work_tree "rebase" exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ --continue diff --git a/sequencer.c b/sequencer.c index 998a858427060f..c075d0c79e16fe 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3311,6 +3311,25 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit) return 0; } +int checkout_onto(struct replay_opts *opts, + const char *onto_name, const char *onto, + const char *orig_head) +{ + struct object_id oid; + const char *action = reflog_message(opts, "start", "checkout %s", onto_name); + + if (get_oid(orig_head, &oid)) + return error(_("%s: not a valid OID"), orig_head); + + if (run_git_checkout(opts, onto, action)) { + apply_autostash(opts); + sequencer_remove_state(opts); + return error(_("could not detach HEAD")); + } + + return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR); +} + static const char rescheduled_advice[] = N_("Could not execute the todo command\n" "\n" diff --git a/sequencer.h b/sequencer.h index eb0323f65a36e3..24fac0ce5534e2 100644 --- a/sequencer.h +++ b/sequencer.h @@ -115,6 +115,9 @@ void commit_post_rewrite(const struct commit *current_head, const struct object_id *new_head); int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit); +int checkout_onto(struct replay_opts *opts, + const char *onto_name, const char *onto, + const char *orig_head); #define SUMMARY_INITIAL_COMMIT (1 << 0) #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1) From 5adbf6be68d05ba0ed7dc8a85d88016f8ed2bada Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:35 +0200 Subject: [PATCH 154/406] sequencer: refactor append_todo_help() to write its message to a buffer This refactors append_todo_help() to write its message to a buffer instead of the todo-list. This is needed for the rewrite of complete_action(), which will come after the next commit. As rebase--helper still needs the file manipulation part of append_todo_help(), it is extracted to a temporary function, append_todo_help_to_file(). This function will go away after the rewrite of complete_action(). Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 2 +- rebase-interactive.c | 43 ++++++++++++++++++++++++++++------------ rebase-interactive.h | 4 +++- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 7d9426d23c78c7..313092c4659eba 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -97,7 +97,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) if (command == ADD_EXEC && argc == 2) return !!sequencer_add_exec_commands(argv[1]); if (command == APPEND_TODO_HELP && argc == 1) - return !!append_todo_help(0, keep_empty); + return !!append_todo_help_to_file(0, keep_empty); if (command == EDIT_TODO && argc == 1) return !!edit_todo_list(flags); if (command == PREPARE_BRANCH && argc == 2) diff --git a/rebase-interactive.c b/rebase-interactive.c index 3f9468fc6997d5..4a9a10eff4a5a2 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -4,11 +4,9 @@ #include "sequencer.h" #include "strbuf.h" -int append_todo_help(unsigned edit_todo, unsigned keep_empty) +void append_todo_help(unsigned edit_todo, unsigned keep_empty, + struct strbuf *buf) { - struct strbuf buf = STRBUF_INIT; - FILE *todo; - int ret; const char *msg = _("\nCommands:\n" "p, pick <commit> = use commit\n" "r, reword <commit> = use commit, but edit the commit message\n" @@ -26,11 +24,7 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty) "\n" "These lines can be re-ordered; they are executed from top to bottom.\n"); - todo = fopen_or_warn(rebase_path_todo(), "a"); - if (!todo) - return 1; - - strbuf_add_commented_lines(&buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg)); if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR) msg = _("\nDo not remove any line. Use 'drop' " @@ -39,7 +33,7 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty) msg = _("\nIf you remove a line here " "THAT COMMIT WILL BE LOST.\n"); - strbuf_add_commented_lines(&buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg)); if (edit_todo) msg = _("\nYou are editing the todo file " @@ -50,12 +44,25 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty) msg = _("\nHowever, if you remove everything, " "the rebase will be aborted.\n\n"); - strbuf_add_commented_lines(&buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg)); if (!keep_empty) { msg = _("Note that empty commits are commented out"); - strbuf_add_commented_lines(&buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg)); } +} + +int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty) +{ + struct strbuf buf = STRBUF_INIT; + FILE *todo; + int ret; + + todo = fopen_or_warn(rebase_path_todo(), "a"); + if (!todo) + return -1; + + append_todo_help(edit_todo, keep_empty, &buf); ret = fputs(buf.buf, todo); if (ret < 0) @@ -84,7 +91,17 @@ int edit_todo_list(unsigned flags) strbuf_release(&buf); transform_todos(flags | TODO_LIST_SHORTEN_IDS); - append_todo_help(1, 0); + + if (strbuf_read_file(&buf, todo_file, 0) < 0) + return error_errno(_("could not read '%s'."), todo_file); + + append_todo_help(1, 0, &buf); + if (write_message(buf.buf, buf.len, todo_file, 0)) { + strbuf_release(&buf); + return -1; + } + + strbuf_release(&buf); if (launch_sequence_editor(todo_file, NULL, NULL)) return -1; diff --git a/rebase-interactive.h b/rebase-interactive.h index 155219e742d223..d33f3176b72703 100644 --- a/rebase-interactive.h +++ b/rebase-interactive.h @@ -1,7 +1,9 @@ #ifndef REBASE_INTERACTIVE_H #define REBASE_INTERACTIVE_H -int append_todo_help(unsigned edit_todo, unsigned keep_empty); +void append_todo_help(unsigned edit_todo, unsigned keep_empty, + struct strbuf *buf); +int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty); int edit_todo_list(unsigned flags); #endif From a5e1c9261bdd048002810cebfac2eacbb2775d28 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:36 +0200 Subject: [PATCH 155/406] sequencer: change the way skip_unnecessary_picks() returns its result Instead of skip_unnecessary_picks() printing its result to stdout, it returns it into a struct object_id, as the rewrite of complete_action() (to come in the next commit) will need it. rebase--helper then is modified to fit this change. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 10 ++++++++-- sequencer.c | 13 ++++++------- sequencer.h | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 313092c4659eba..bed3dd2b951909 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -90,8 +90,14 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!transform_todos(flags); if (command == CHECK_TODO_LIST && argc == 1) return !!check_todo_list(); - if (command == SKIP_UNNECESSARY_PICKS && argc == 1) - return !!skip_unnecessary_picks(); + if (command == SKIP_UNNECESSARY_PICKS && argc == 1) { + struct object_id oid; + int ret = skip_unnecessary_picks(&oid); + + if (!ret) + puts(oid_to_hex(&oid)); + return !!ret; + } if (command == REARRANGE_SQUASH && argc == 1) return !!rearrange_squash(); if (command == ADD_EXEC && argc == 2) diff --git a/sequencer.c b/sequencer.c index c075d0c79e16fe..20855ad4ac0819 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4578,17 +4578,17 @@ static int rewrite_file(const char *path, const char *buf, size_t len) } /* skip picking commits whose parents are unchanged */ -int skip_unnecessary_picks(void) +int skip_unnecessary_picks(struct object_id *output_oid) { const char *todo_file = rebase_path_todo(); struct strbuf buf = STRBUF_INIT; struct todo_list todo_list = TODO_LIST_INIT; - struct object_id onto_oid, *oid = &onto_oid, *parent_oid; + struct object_id *parent_oid; int fd, i; if (!read_oneliner(&buf, rebase_path_onto(), 0)) return error(_("could not read 'onto'")); - if (get_oid(buf.buf, &onto_oid)) { + if (get_oid(buf.buf, output_oid)) { strbuf_release(&buf); return error(_("need a HEAD to fixup")); } @@ -4618,9 +4618,9 @@ int skip_unnecessary_picks(void) if (item->commit->parents->next) break; /* merge commit */ parent_oid = &item->commit->parents->item->object.oid; - if (hashcmp(parent_oid->hash, oid->hash)) + if (hashcmp(parent_oid->hash, output_oid->hash)) break; - oid = &item->commit->object.oid; + oidcpy(output_oid, &item->commit->object.oid); } if (i > 0) { int offset = get_item_line_offset(&todo_list, i); @@ -4649,11 +4649,10 @@ int skip_unnecessary_picks(void) todo_list.current = i; if (is_fixup(peek_command(&todo_list, 0))) - record_in_rewritten(oid, peek_command(&todo_list, 0)); + record_in_rewritten(output_oid, peek_command(&todo_list, 0)); } todo_list_release(&todo_list); - printf("%s\n", oid_to_hex(oid)); return 0; } diff --git a/sequencer.h b/sequencer.h index 24fac0ce5534e2..aa932db6edb804 100644 --- a/sequencer.h +++ b/sequencer.h @@ -96,7 +96,7 @@ int sequencer_add_exec_commands(const char *command); int transform_todos(unsigned flags); enum missing_commit_check_level get_missing_commit_check_level(void); int check_todo_list(void); -int skip_unnecessary_picks(void); +int skip_unnecessary_picks(struct object_id *output_oid); int rearrange_squash(void); extern const char sign_off_header[]; From 37b2af4d029177e9617518cc7a1f4342779f048f Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Fri, 10 Aug 2018 18:51:37 +0200 Subject: [PATCH 156/406] t3404: todo list with commented-out commands only aborts If the todo list generated by `--make-script` is empty, complete_action() writes a noop, but if it has only commented-out commands, it will abort with the message "Nothing to do", and does not launch the editor. This adds a new test to ensure that complete_action() behaves this way. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- t/t3404-rebase-interactive.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index 86bba5ed7c23f7..bd7f61c7f1dbcc 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -75,6 +75,16 @@ test_expect_success 'rebase --keep-empty' ' test_line_count = 6 actual ' +cat > expect <<EOF +Nothing to do +EOF + +test_expect_success 'rebase -i with empty HEAD' ' + set_fake_editor && + test_must_fail env FAKE_LINES="1 exec_true" git rebase -i HEAD^ >actual 2>&1 && + test_i18ncmp expect actual +' + test_expect_success 'rebase -i with the exec command' ' git checkout master && ( From 555d0b3be9f6dc7221b8e6f139dfa2f9f11314ab Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:36 +0200 Subject: [PATCH 157/406] rebase -i: rewrite complete_action() in C MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This rewrites complete_action() from shell to C. A new mode is added to rebase--helper (`--complete-action`), as well as a new flag (`--autosquash`). Finally, complete_action() is stripped from git-rebase--interactive.sh. The original complete_action() would return the code 2 when the todo list contained no actions. This was a special case for rebase -i and -p; git-rebase.sh would then apply the autostash, delete the state directory, and die with the message "Nothing to do". This cleanup is rewritten in C instead of returning 2. As rebase -i no longer returns 2, the comment describing this behaviour in git-rebase.sh is updated to reflect this change. The message "Nothing to do" is now printed with error(), and so becomes "error: nothing to do". Some tests in t3404 check this value, so they are updated to fit this change. The first check might seem useless as we write "noop" to the todo list if it is empty. Actually, the todo list might contain commented commands (ie. empty commits). In this case, complete_action() won’t write "noop", and will abort without starting the editor. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 12 +++- git-rebase--interactive.sh | 53 ++--------------- git-rebase.sh | 2 +- sequencer.c | 104 ++++++++++++++++++++++++++++++++++ sequencer.h | 4 ++ t/t3404-rebase-interactive.sh | 2 +- 6 files changed, 124 insertions(+), 53 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index bed3dd2b951909..01b33339589e34 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -13,13 +13,13 @@ static const char * const builtin_rebase_helper_usage[] = { int cmd_rebase__helper(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; - unsigned flags = 0, keep_empty = 0, rebase_merges = 0; + unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; int abbreviate_commands = 0, rebase_cousins = -1; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH, - CHECKOUT_ONTO + CHECKOUT_ONTO, COMPLETE_ACTION } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -29,6 +29,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), OPT_BOOL(0, "rebase-cousins", &rebase_cousins, N_("keep original branch points of cousins")), + OPT_BOOL(0, "autosquash", &autosquash, + N_("move commits that begin with squash!/fixup!")), OPT__VERBOSE(&opts.verbose, N_("be verbose")), OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), CONTINUE), @@ -57,6 +59,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("prepare the branch to be rebased"), PREPARE_BRANCH), OPT_CMDMODE(0, "checkout-onto", &command, N_("checkout a commit"), CHECKOUT_ONTO), + OPT_CMDMODE(0, "complete-action", &command, + N_("complete the action"), COMPLETE_ACTION), OPT_END() }; @@ -110,5 +114,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!prepare_branch_to_be_rebased(&opts, argv[1]); if (command == CHECKOUT_ONTO && argc == 4) return !!checkout_onto(&opts, argv[1], argv[2], argv[3]); + if (command == COMPLETE_ACTION && argc == 6) + return !!complete_action(&opts, flags, argv[1], argv[2], argv[3], + argv[4], argv[5], autosquash); + usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index b68f108f28dafb..59dc4888a6da11 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -127,54 +127,6 @@ init_revisions_and_shortrevisions () { fi } -complete_action() { - test -s "$todo" || echo noop >> "$todo" - test -z "$autosquash" || git rebase--helper --rearrange-squash || exit - test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd" - - todocount=$(git stripspace --strip-comments <"$todo" | wc -l) - todocount=${todocount##* } - -cat >>"$todo" <<EOF - -$comment_char $(eval_ngettext \ - "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \ - "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \ - "$todocount") -EOF - git rebase--helper --append-todo-help ${keep_empty:+--keep-empty} - - has_action "$todo" || - return 2 - - cp "$todo" "$todo".backup - collapse_todo_ids - git_sequence_editor "$todo" || - die_abort "$(gettext "Could not execute editor")" - - has_action "$todo" || - return 2 - - git rebase--helper --check-todo-list || { - ret=$? - git rebase--helper --checkout-onto "$onto_name" "$onto" \ - "$orig_head" ${verbose:+--verbose} - exit $ret - } - - expand_todo_ids - - test -n "$force_rebase" || - onto="$(git rebase--helper --skip-unnecessary-picks)" || - die "Could not skip unnecessary pick commands" - - git rebase--helper --checkout-onto "$onto_name" "$onto" "$orig_head" \ - ${verbose:+--verbose} - require_clean_work_tree "rebase" - exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ - --continue -} - git_rebase__interactive () { initiate_action "$action" ret=$? @@ -193,5 +145,8 @@ git_rebase__interactive () { $revisions ${restrict_revision+^$restrict_revision} >"$todo" || die "$(gettext "Could not generate todo list")" - complete_action + exec git rebase--helper --complete-action "$shortrevisions" "$onto_name" \ + "$shortonto" "$orig_head" "$cmd" $allow_empty_message \ + ${autosquash:+--autosquash} ${keep_empty:+--keep-empty} \ + ${verbose:+--verbose} ${force_rebase:+--no-ff} } diff --git a/git-rebase.sh b/git-rebase.sh index 797344764510e0..51a6db7daa0e65 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -219,7 +219,7 @@ run_specific_rebase () { if test $ret -eq 0 then finish_rebase - elif test $ret -eq 2 # special exit status for rebase -i + elif test $ret -eq 2 # special exit status for rebase -p then apply_autostash && rm -rf "$state_dir" && diff --git a/sequencer.c b/sequencer.c index 20855ad4ac0819..969c3b6044c135 100644 --- a/sequencer.c +++ b/sequencer.c @@ -30,6 +30,7 @@ #include "oidset.h" #include "commit-slab.h" #include "alias.h" +#include "rebase-interactive.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" @@ -53,6 +54,9 @@ static GIT_PATH_FUNC(rebase_path, "rebase-merge") * file and written to the tail of 'done'. */ GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") +static GIT_PATH_FUNC(rebase_path_todo_backup, + "rebase-merge/git-rebase-todo.backup") + /* * The rebase command lines that have already been processed. A line * is moved here when it is first handled, before any associated user @@ -4657,6 +4661,106 @@ int skip_unnecessary_picks(struct object_id *output_oid) return 0; } +int complete_action(struct replay_opts *opts, unsigned flags, + const char *shortrevisions, const char *onto_name, + const char *onto, const char *orig_head, const char *cmd, + unsigned autosquash) +{ + const char *shortonto, *todo_file = rebase_path_todo(); + struct todo_list todo_list = TODO_LIST_INIT; + struct strbuf *buf = &(todo_list.buf); + struct object_id oid; + struct stat st; + + get_oid(onto, &oid); + shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV); + + if (!lstat(todo_file, &st) && st.st_size == 0 && + write_message("noop\n", 5, todo_file, 0)) + return -1; + + if (autosquash && rearrange_squash()) + return -1; + + if (cmd && *cmd) + sequencer_add_exec_commands(cmd); + + if (strbuf_read_file(buf, todo_file, 0) < 0) + return error_errno(_("could not read '%s'."), todo_file); + + if (parse_insn_buffer(buf->buf, &todo_list)) { + todo_list_release(&todo_list); + return error(_("unusable todo list: '%s'"), todo_file); + } + + if (count_commands(&todo_list) == 0) { + apply_autostash(opts); + sequencer_remove_state(opts); + todo_list_release(&todo_list); + + return error(_("nothing to do")); + } + + strbuf_addch(buf, '\n'); + strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", + "Rebase %s onto %s (%d commands)", + count_commands(&todo_list)), + shortrevisions, shortonto, count_commands(&todo_list)); + append_todo_help(0, flags & TODO_LIST_KEEP_EMPTY, buf); + + if (write_message(buf->buf, buf->len, todo_file, 0)) { + todo_list_release(&todo_list); + return -1; + } + + if (copy_file(rebase_path_todo_backup(), todo_file, 0666)) + return error(_("could not copy '%s' to '%s'."), todo_file, + rebase_path_todo_backup()); + + if (transform_todos(flags | TODO_LIST_SHORTEN_IDS)) + return error(_("could not transform the todo list")); + + strbuf_reset(buf); + + if (launch_sequence_editor(todo_file, buf, NULL)) { + apply_autostash(opts); + sequencer_remove_state(opts); + todo_list_release(&todo_list); + + return -1; + } + + strbuf_stripspace(buf, 1); + if (buf->len == 0) { + apply_autostash(opts); + sequencer_remove_state(opts); + todo_list_release(&todo_list); + + return error(_("nothing to do")); + } + + todo_list_release(&todo_list); + + if (check_todo_list()) { + checkout_onto(opts, onto_name, onto, orig_head); + return -1; + } + + if (transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS))) + return error(_("could not transform the todo list")); + + if (opts->allow_ff && skip_unnecessary_picks(&oid)) + return error(_("could not skip unnecessary pick commands")); + + if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head)) + return -1; +; + if (require_clean_work_tree("rebase", "", 1, 1)) + return -1; + + return sequencer_continue(opts); +} + struct subject2item_entry { struct hashmap_entry entry; int i; diff --git a/sequencer.h b/sequencer.h index aa932db6edb804..3a9b776e026b73 100644 --- a/sequencer.h +++ b/sequencer.h @@ -97,6 +97,10 @@ int transform_todos(unsigned flags); enum missing_commit_check_level get_missing_commit_check_level(void); int check_todo_list(void); int skip_unnecessary_picks(struct object_id *output_oid); +int complete_action(struct replay_opts *opts, unsigned flags, + const char *shortrevisions, const char *onto_name, + const char *onto, const char *orig_head, const char *cmd, + unsigned autosquash); int rearrange_squash(void); extern const char sign_off_header[]; diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index bd7f61c7f1dbcc..0dd834543de506 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -76,7 +76,7 @@ test_expect_success 'rebase --keep-empty' ' ' cat > expect <<EOF -Nothing to do +error: nothing to do EOF test_expect_success 'rebase -i with empty HEAD' ' From ab0d865f93fb94207f9c3566831cf0f8695d41c6 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:37 +0200 Subject: [PATCH 158/406] rebase -i: remove unused modes and functions This removes the modes `--skip-unnecessary-picks`, `--append-todo-help`, and `--checkout-onto` from rebase--helper.c, the functions of git-rebase--interactive.sh that were rendered useless by the rewrite of complete_action(), and append_todo_help_to_file() from rebase-interactive.c. skip_unnecessary_picks() and checkout_onto() becomes static, as they are only used inside of the sequencer. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 23 ++---------------- git-rebase--interactive.sh | 50 -------------------------------------- rebase-interactive.c | 22 ----------------- rebase-interactive.h | 1 - sequencer.c | 8 +++--- sequencer.h | 4 --- 6 files changed, 6 insertions(+), 102 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 01b33339589e34..e1460136f549d4 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -17,9 +17,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) int abbreviate_commands = 0, rebase_cousins = -1; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, - CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, - ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH, - CHECKOUT_ONTO, COMPLETE_ACTION + CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH, + COMPLETE_ACTION } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -44,21 +43,15 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("expand commit ids in the todo list"), EXPAND_OIDS), OPT_CMDMODE(0, "check-todo-list", &command, N_("check the todo list"), CHECK_TODO_LIST), - OPT_CMDMODE(0, "skip-unnecessary-picks", &command, - N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS), OPT_CMDMODE(0, "rearrange-squash", &command, N_("rearrange fixup/squash lines"), REARRANGE_SQUASH), OPT_CMDMODE(0, "add-exec-commands", &command, N_("insert exec commands in todo list"), ADD_EXEC), - OPT_CMDMODE(0, "append-todo-help", &command, - N_("insert the help in the todo list"), APPEND_TODO_HELP), OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list during an interactive rebase"), EDIT_TODO), OPT_CMDMODE(0, "prepare-branch", &command, N_("prepare the branch to be rebased"), PREPARE_BRANCH), - OPT_CMDMODE(0, "checkout-onto", &command, - N_("checkout a commit"), CHECKOUT_ONTO), OPT_CMDMODE(0, "complete-action", &command, N_("complete the action"), COMPLETE_ACTION), OPT_END() @@ -94,26 +87,14 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!transform_todos(flags); if (command == CHECK_TODO_LIST && argc == 1) return !!check_todo_list(); - if (command == SKIP_UNNECESSARY_PICKS && argc == 1) { - struct object_id oid; - int ret = skip_unnecessary_picks(&oid); - - if (!ret) - puts(oid_to_hex(&oid)); - return !!ret; - } if (command == REARRANGE_SQUASH && argc == 1) return !!rearrange_squash(); if (command == ADD_EXEC && argc == 2) return !!sequencer_add_exec_commands(argv[1]); - if (command == APPEND_TODO_HELP && argc == 1) - return !!append_todo_help_to_file(0, keep_empty); if (command == EDIT_TODO && argc == 1) return !!edit_todo_list(flags); if (command == PREPARE_BRANCH && argc == 2) return !!prepare_branch_to_be_rebased(&opts, argv[1]); - if (command == CHECKOUT_ONTO && argc == 4) - return !!checkout_onto(&opts, argv[1], argv[2], argv[3]); if (command == COMPLETE_ACTION && argc == 6) return !!complete_action(&opts, flags, argv[1], argv[2], argv[3], argv[4], argv[5], autosquash); diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 59dc4888a6da11..0d66c0f8b8db82 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -16,56 +16,6 @@ todo="$state_dir"/git-rebase-todo GIT_CHERRY_PICK_HELP="$resolvemsg" export GIT_CHERRY_PICK_HELP -comment_char=$(git config --get core.commentchar 2>/dev/null) -case "$comment_char" in -'' | auto) - comment_char="#" - ;; -?) - ;; -*) - comment_char=$(echo "$comment_char" | cut -c1) - ;; -esac - -die_abort () { - apply_autostash - rm -rf "$state_dir" - die "$1" -} - -has_action () { - test -n "$(git stripspace --strip-comments <"$1")" -} - -git_sequence_editor () { - if test -z "$GIT_SEQUENCE_EDITOR" - then - GIT_SEQUENCE_EDITOR="$(git config sequence.editor)" - if [ -z "$GIT_SEQUENCE_EDITOR" ] - then - GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $? - fi - fi - - eval "$GIT_SEQUENCE_EDITOR" '"$@"' -} - -expand_todo_ids() { - git rebase--helper --expand-ids -} - -collapse_todo_ids() { - git rebase--helper --shorten-ids -} - -get_missing_commit_check_level () { - check_level=$(git config --get rebase.missingCommitsCheck) - check_level=${check_level:-ignore} - # Don't be case sensitive - printf '%s' "$check_level" | tr 'A-Z' 'a-z' -} - # Initiate an action. If the cannot be any # further action it may exec a command # or exit and not return. diff --git a/rebase-interactive.c b/rebase-interactive.c index 4a9a10eff4a5a2..0f4119cbae1bb6 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -52,28 +52,6 @@ void append_todo_help(unsigned edit_todo, unsigned keep_empty, } } -int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty) -{ - struct strbuf buf = STRBUF_INIT; - FILE *todo; - int ret; - - todo = fopen_or_warn(rebase_path_todo(), "a"); - if (!todo) - return -1; - - append_todo_help(edit_todo, keep_empty, &buf); - - ret = fputs(buf.buf, todo); - if (ret < 0) - error_errno(_("could not append help text to '%s'"), rebase_path_todo()); - - fclose(todo); - strbuf_release(&buf); - - return ret; -} - int edit_todo_list(unsigned flags) { struct strbuf buf = STRBUF_INIT; diff --git a/rebase-interactive.h b/rebase-interactive.h index d33f3176b72703..971da037769476 100644 --- a/rebase-interactive.h +++ b/rebase-interactive.h @@ -3,7 +3,6 @@ void append_todo_help(unsigned edit_todo, unsigned keep_empty, struct strbuf *buf); -int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty); int edit_todo_list(unsigned flags); #endif diff --git a/sequencer.c b/sequencer.c index 969c3b6044c135..fb936532998775 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3315,9 +3315,9 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit) return 0; } -int checkout_onto(struct replay_opts *opts, - const char *onto_name, const char *onto, - const char *orig_head) +static int checkout_onto(struct replay_opts *opts, + const char *onto_name, const char *onto, + const char *orig_head) { struct object_id oid; const char *action = reflog_message(opts, "start", "checkout %s", onto_name); @@ -4582,7 +4582,7 @@ static int rewrite_file(const char *path, const char *buf, size_t len) } /* skip picking commits whose parents are unchanged */ -int skip_unnecessary_picks(struct object_id *output_oid) +static int skip_unnecessary_picks(struct object_id *output_oid) { const char *todo_file = rebase_path_todo(); struct strbuf buf = STRBUF_INIT; diff --git a/sequencer.h b/sequencer.h index 3a9b776e026b73..0bbc4c4b730af9 100644 --- a/sequencer.h +++ b/sequencer.h @@ -96,7 +96,6 @@ int sequencer_add_exec_commands(const char *command); int transform_todos(unsigned flags); enum missing_commit_check_level get_missing_commit_check_level(void); int check_todo_list(void); -int skip_unnecessary_picks(struct object_id *output_oid); int complete_action(struct replay_opts *opts, unsigned flags, const char *shortrevisions, const char *onto_name, const char *onto, const char *orig_head, const char *cmd, @@ -119,9 +118,6 @@ void commit_post_rewrite(const struct commit *current_head, const struct object_id *new_head); int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit); -int checkout_onto(struct replay_opts *opts, - const char *onto_name, const char *onto, - const char *orig_head); #define SUMMARY_INITIAL_COMMIT (1 << 0) #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1) From 146448db38303806652076ec233c638bed79e3fe Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:38 +0200 Subject: [PATCH 159/406] rebase -i: implement the logic to initialize $revisions in C MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This rewrites the part of init_revisions_and_shortrevisions() needed by `--make-script` from shell to C. The new version is called get_revision_ranges(), and is a static function inside of rebase--helper.c. As this does not initialize $shortrevision, the original shell version is not yet stripped. Unlike init_revisions_and_shortrevisions(), get_revision_ranges() doesn’t write $squash_onto to the state directory, it’s done by the handler of `--make-script` instead. Finally, this drops the $revision argument passed to `--make-script` in git-rebase--interactive.sh, and rebase--helper is changed accordingly. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 56 ++++++++++++++++++++++++++++++++++++-- git-rebase--interactive.sh | 4 ++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index e1460136f549d4..acc71a6f99a067 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -4,6 +4,25 @@ #include "parse-options.h" #include "sequencer.h" #include "rebase-interactive.h" +#include "argv-array.h" + +static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") + +static int get_revision_ranges(const char *upstream, const char *onto, + const char **head_hash, + char **revisions) +{ + const char *base_rev = upstream ? upstream : onto; + struct object_id orig_head; + + if (get_oid("HEAD", &orig_head)) + return error(_("no HEAD?")); + + *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ); + *revisions = xstrfmt("%s...%s", base_rev, *head_hash); + + return 0; +} static const char * const builtin_rebase_helper_usage[] = { N_("git rebase--helper [<options>]"), @@ -14,7 +33,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; - int abbreviate_commands = 0, rebase_cousins = -1; + int abbreviate_commands = 0, rebase_cousins = -1, ret; + const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL, + *squash_onto = NULL, *upstream = NULL; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH, @@ -54,6 +75,13 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("prepare the branch to be rebased"), PREPARE_BRANCH), OPT_CMDMODE(0, "complete-action", &command, N_("complete the action"), COMPLETE_ACTION), + OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")), + OPT_STRING(0, "restrict-revision", &restrict_revision, + N_("restrict-revision"), N_("restrict revision")), + OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"), + N_("squash onto")), + OPT_STRING(0, "upstream", &upstream, N_("upstream"), + N_("the upstream commit")), OPT_END() }; @@ -81,8 +109,30 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!sequencer_continue(&opts); if (command == ABORT && argc == 1) return !!sequencer_remove_state(&opts); - if (command == MAKE_SCRIPT && argc > 1) - return !!sequencer_make_script(stdout, argc, argv, flags); + if (command == MAKE_SCRIPT && argc == 1) { + char *revisions = NULL; + struct argv_array make_script_args = ARGV_ARRAY_INIT; + + if (!upstream && squash_onto) + write_file(path_squash_onto(), "%s\n", squash_onto); + + ret = get_revision_ranges(upstream, onto, &head_hash, &revisions); + if (ret) + return ret; + + argv_array_pushl(&make_script_args, "", revisions, NULL); + if (restrict_revision) + argv_array_push(&make_script_args, restrict_revision); + + ret = sequencer_make_script(stdout, + make_script_args.argc, make_script_args.argv, + flags); + + free(revisions); + argv_array_clear(&make_script_args); + + return !!ret; + } if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1) return !!transform_todos(flags); if (command == CHECK_TODO_LIST && argc == 1) diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 0d66c0f8b8db82..4ca47aed1eca50 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -92,7 +92,9 @@ git_rebase__interactive () { git rebase--helper --make-script ${keep_empty:+--keep-empty} \ ${rebase_merges:+--rebase-merges} \ ${rebase_cousins:+--rebase-cousins} \ - $revisions ${restrict_revision+^$restrict_revision} >"$todo" || + ${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"} \ + ${squash_onto:+--squash-onto "$squash_onto"} \ + ${restrict_revision:+--restrict-revision ^"$restrict_revision"} >"$todo" || die "$(gettext "Could not generate todo list")" exec git rebase--helper --complete-action "$shortrevisions" "$onto_name" \ From 9b0bee567bafd9bbcd5ac412ca4f69aaa5fb00c7 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:39 +0200 Subject: [PATCH 160/406] rebase -i: rewrite the rest of init_revisions_and_shortrevisions() in C This rewrites the part of init_revisions_and_shortrevisions() needed by `--complete-action` (which initialize $shortrevisions) from shell to C. When `upstream` is empty, it means that the user launched a `rebase --root`, and `onto` contains the ID of an empty commit. As a range between an empty commit and `head` is not really meaningful, `onto` is not used to initialize `shortrevisions` in this case. The corresponding arguments passed to `--complete-action` are then dropped, and init_revisions_and_shortrevisions() is stripped from git-rebase--interactive.sh Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 40 ++++++++++++++++++++++++++++++++------ git-rebase--interactive.sh | 27 ++++--------------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index acc71a6f99a067..0716bbfd786cbf 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -10,7 +10,7 @@ static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") static int get_revision_ranges(const char *upstream, const char *onto, const char **head_hash, - char **revisions) + char **revisions, char **shortrevisions) { const char *base_rev = upstream ? upstream : onto; struct object_id orig_head; @@ -19,7 +19,25 @@ static int get_revision_ranges(const char *upstream, const char *onto, return error(_("no HEAD?")); *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ); - *revisions = xstrfmt("%s...%s", base_rev, *head_hash); + + if (revisions) + *revisions = xstrfmt("%s...%s", base_rev, *head_hash); + if (shortrevisions) { + const char *shorthead; + + shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV); + + if (upstream) { + const char *shortrev; + struct object_id rev_oid; + + get_oid(base_rev, &rev_oid); + shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV); + + *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead); + } else + *shortrevisions = xstrdup(shorthead); + } return 0; } @@ -116,7 +134,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) if (!upstream && squash_onto) write_file(path_squash_onto(), "%s\n", squash_onto); - ret = get_revision_ranges(upstream, onto, &head_hash, &revisions); + ret = get_revision_ranges(upstream, onto, &head_hash, &revisions, NULL); if (ret) return ret; @@ -145,9 +163,19 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!edit_todo_list(flags); if (command == PREPARE_BRANCH && argc == 2) return !!prepare_branch_to_be_rebased(&opts, argv[1]); - if (command == COMPLETE_ACTION && argc == 6) - return !!complete_action(&opts, flags, argv[1], argv[2], argv[3], - argv[4], argv[5], autosquash); + if (command == COMPLETE_ACTION && argc == 3) { + char *shortrevisions = NULL; + + ret = get_revision_ranges(upstream, onto, &head_hash, NULL, &shortrevisions); + if (ret) + return ret; + + ret = complete_action(&opts, flags, shortrevisions, argv[1], onto, + head_hash, argv[2], autosquash); + + free(shortrevisions); + return !!ret; + } usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 4ca47aed1eca50..08e9a21c2f15f4 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -60,23 +60,6 @@ init_basic_state () { write_basic_state } -init_revisions_and_shortrevisions () { - shorthead=$(git rev-parse --short $orig_head) - shortonto=$(git rev-parse --short $onto) - if test -z "$rebase_root" - # this is now equivalent to ! -z "$upstream" - then - shortupstream=$(git rev-parse --short $upstream) - revisions=$upstream...$orig_head - shortrevisions=$shortupstream..$shorthead - else - revisions=$onto...$orig_head - shortrevisions=$shorthead - test -z "$squash_onto" || - echo "$squash_onto" >"$state_dir"/squash-onto - fi -} - git_rebase__interactive () { initiate_action "$action" ret=$? @@ -87,8 +70,6 @@ git_rebase__interactive () { git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose} init_basic_state - init_revisions_and_shortrevisions - git rebase--helper --make-script ${keep_empty:+--keep-empty} \ ${rebase_merges:+--rebase-merges} \ ${rebase_cousins:+--rebase-cousins} \ @@ -97,8 +78,8 @@ git_rebase__interactive () { ${restrict_revision:+--restrict-revision ^"$restrict_revision"} >"$todo" || die "$(gettext "Could not generate todo list")" - exec git rebase--helper --complete-action "$shortrevisions" "$onto_name" \ - "$shortonto" "$orig_head" "$cmd" $allow_empty_message \ - ${autosquash:+--autosquash} ${keep_empty:+--keep-empty} \ - ${verbose:+--verbose} ${force_rebase:+--no-ff} + exec git rebase--helper --complete-action "$onto_name" "$cmd" \ + $allow_empty_message ${autosquash:+--autosquash} ${verbose:+--verbose} \ + ${keep_empty:+--keep-empty} ${force_rebase:+--no-ff} \ + ${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"} } From 33ae4e062bfaab445cb270adb393628d7e90fca6 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:40 +0200 Subject: [PATCH 161/406] rebase -i: rewrite write_basic_state() in C This rewrites write_basic_state() from git-rebase.sh in C. This is the first step in the conversion of init_basic_state(), hence the mode in rebase--helper.c is called INIT_BASIC_STATE. init_basic_state() will be converted in the next commit. The part of read_strategy_opts() that parses the stategy options is moved to a new function to allow its use in rebase--helper.c. Finally, the call to write_basic_state() is removed from git-rebase--interactive.sh, replaced by a call to `--init-basic-state`. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 28 +++++++++++++- git-rebase--interactive.sh | 7 +++- sequencer.c | 77 ++++++++++++++++++++++++++++++++------ sequencer.h | 4 ++ 4 files changed, 102 insertions(+), 14 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 0716bbfd786cbf..63c5086e423199 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -5,6 +5,8 @@ #include "sequencer.h" #include "rebase-interactive.h" #include "argv-array.h" +#include "rerere.h" +#include "alias.h" static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") @@ -53,11 +55,12 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; int abbreviate_commands = 0, rebase_cousins = -1, ret; const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL, - *squash_onto = NULL, *upstream = NULL; + *squash_onto = NULL, *upstream = NULL, *head_name = NULL; + char *raw_strategies = NULL; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH, - COMPLETE_ACTION + COMPLETE_ACTION, INIT_BASIC_STATE } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -69,6 +72,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("keep original branch points of cousins")), OPT_BOOL(0, "autosquash", &autosquash, N_("move commits that begin with squash!/fixup!")), + OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")), OPT__VERBOSE(&opts.verbose, N_("be verbose")), OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), CONTINUE), @@ -93,6 +97,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("prepare the branch to be rebased"), PREPARE_BRANCH), OPT_CMDMODE(0, "complete-action", &command, N_("complete the action"), COMPLETE_ACTION), + OPT_CMDMODE(0, "init-basic-state", &command, + N_("initialise the rebase state"), INIT_BASIC_STATE), OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")), OPT_STRING(0, "restrict-revision", &restrict_revision, N_("restrict-revision"), N_("restrict revision")), @@ -100,6 +106,14 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("squash onto")), OPT_STRING(0, "upstream", &upstream, N_("upstream"), N_("the upstream commit")), + OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")), + OPT_STRING('S', "gpg-sign", &opts.gpg_sign, N_("gpg-sign"), + N_("GPG-sign commits")), + OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"), + N_("rebase strategy")), + OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"), + N_("strategy options")), + OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto), OPT_END() }; @@ -176,6 +190,16 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) free(shortrevisions); return !!ret; } + if (command == INIT_BASIC_STATE) { + if (raw_strategies) + parse_strategy_opts(&opts, raw_strategies); + + ret = get_revision_ranges(upstream, onto, &head_hash, NULL, NULL); + if (ret) + return ret; + + return !!write_basic_state(&opts, head_name, onto, head_hash); + } usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 08e9a21c2f15f4..6367da66e2dd05 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -57,7 +57,6 @@ init_basic_state () { rm -f "$(git rev-parse --git-path REBASE_HEAD)" : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")" - write_basic_state } git_rebase__interactive () { @@ -70,6 +69,12 @@ git_rebase__interactive () { git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose} init_basic_state + git rebase--helper --init-basic-state ${upstream:+--upstream "$upstream"} \ + ${onto:+--onto "$onto"} ${head_name:+--head-name "$head_name"} \ + ${verbose:+--verbose} ${strategy:+--strategy "$strategy"} \ + ${strategy_opts:+--strategy-opts="$strategy_opts"} \ + "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" || exit + git rebase--helper --make-script ${keep_empty:+--keep-empty} \ ${rebase_merges:+--rebase-merges} \ ${rebase_cousins:+--rebase-cousins} \ diff --git a/sequencer.c b/sequencer.c index fb936532998775..ad2193507be131 100644 --- a/sequencer.c +++ b/sequencer.c @@ -144,7 +144,7 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete") /* * The following files are written by git-rebase just after parsing the - * command-line (and are only consumed, not modified, by the sequencer). + * command-line. */ static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") @@ -156,6 +156,7 @@ static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") +static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet") static int git_sequencer_config(const char *k, const char *v, void *cb) { @@ -2243,21 +2244,14 @@ static int populate_opts_cb(const char *key, const char *value, void *data) return 0; } -static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) +void parse_strategy_opts(struct replay_opts *opts, char *raw_opts) { int i; - char *strategy_opts_string; + char *strategy_opts_string = raw_opts; - strbuf_reset(buf); - if (!read_oneliner(buf, rebase_path_strategy(), 0)) - return; - opts->strategy = strbuf_detach(buf, NULL); - if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) - return; - - strategy_opts_string = buf->buf; if (*strategy_opts_string == ' ') strategy_opts_string++; + opts->xopts_nr = split_cmdline(strategy_opts_string, (const char ***)&opts->xopts); for (i = 0; i < opts->xopts_nr; i++) { @@ -2268,6 +2262,18 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) } } +static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) +{ + strbuf_reset(buf); + if (!read_oneliner(buf, rebase_path_strategy(), 0)) + return; + opts->strategy = strbuf_detach(buf, NULL); + if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) + return; + + parse_strategy_opts(opts, buf->buf); +} + static int read_populate_opts(struct replay_opts *opts) { if (is_rebase_i(opts)) { @@ -2335,6 +2341,55 @@ static int read_populate_opts(struct replay_opts *opts) return 0; } +static void write_strategy_opts(struct replay_opts *opts) +{ + int i; + struct strbuf buf = STRBUF_INIT; + + for (i = 0; i < opts->xopts_nr; ++i) + strbuf_addf(&buf, " --%s", opts->xopts[i]); + + write_file(rebase_path_strategy_opts(), "%s\n", buf.buf); + strbuf_release(&buf); +} + +int write_basic_state(struct replay_opts *opts, const char *head_name, + const char *onto, const char *orig_head) +{ + const char *quiet = getenv("GIT_QUIET"); + + if (head_name) + write_file(rebase_path_head_name(), "%s\n", head_name); + if (onto) + write_file(rebase_path_onto(), "%s\n", onto); + if (orig_head) + write_file(rebase_path_orig_head(), "%s\n", orig_head); + + if (quiet) + write_file(rebase_path_quiet(), "%s\n", quiet); + else + write_file(rebase_path_quiet(), "\n"); + + if (opts->verbose) + write_file(rebase_path_verbose(), ""); + if (opts->strategy) + write_file(rebase_path_strategy(), "%s\n", opts->strategy); + if (opts->xopts_nr > 0) + write_strategy_opts(opts); + + if (opts->allow_rerere_auto == RERERE_AUTOUPDATE) + write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n"); + else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE) + write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n"); + + if (opts->gpg_sign) + write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign); + if (opts->signoff) + write_file(rebase_path_signoff(), "--signoff\n"); + + return 0; +} + static int walk_revs_populate_todo(struct todo_list *todo_list, struct replay_opts *opts) { diff --git a/sequencer.h b/sequencer.h index 0bbc4c4b730af9..491974355227e5 100644 --- a/sequencer.h +++ b/sequencer.h @@ -124,3 +124,7 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit); void print_commit_summary(const char *prefix, const struct object_id *oid, unsigned int flags); #endif + +void parse_strategy_opts(struct replay_opts *opts, char *raw_opts); +int write_basic_state(struct replay_opts *opts, const char *head_name, + const char *onto, const char *orig_head); From c57acfffc8a094d67ebe8c95cde2ff74682ee330 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:41 +0200 Subject: [PATCH 162/406] rebase -i: rewrite init_basic_state() in C This rewrites init_basic_state() from shell to C. The call to write_basic_state() in cmd_rebase__helper() is replaced by a call to the new function. The shell version is then stripped from git-rebase--interactive.sh. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--helper.c | 23 ++++++++++++++++++++++- git-rebase--interactive.sh | 9 --------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 63c5086e423199..f8128037d3fb3e 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -5,10 +5,13 @@ #include "sequencer.h" #include "rebase-interactive.h" #include "argv-array.h" +#include "refs.h" #include "rerere.h" #include "alias.h" +static GIT_PATH_FUNC(path_state_dir, "rebase-merge/") static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") +static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive") static int get_revision_ranges(const char *upstream, const char *onto, const char **head_hash, @@ -44,6 +47,24 @@ static int get_revision_ranges(const char *upstream, const char *onto, return 0; } +static int init_basic_state(struct replay_opts *opts, const char *head_name, + const char *onto, const char *orig_head) +{ + FILE *interactive; + + if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir())) + return error_errno(_("could not create temporary %s"), path_state_dir()); + + delete_reflog("REBASE_HEAD"); + + interactive = fopen(path_interactive(), "w"); + if (!interactive) + return error_errno(_("could not mark as interactive")); + fclose(interactive); + + return write_basic_state(opts, head_name, onto, orig_head); +} + static const char * const builtin_rebase_helper_usage[] = { N_("git rebase--helper [<options>]"), NULL @@ -198,7 +219,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) if (ret) return ret; - return !!write_basic_state(&opts, head_name, onto, head_hash); + return !!init_basic_state(&opts, head_name, onto, head_hash); } usage_with_options(builtin_rebase_helper_usage, options); diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 6367da66e2dd05..761be95ed1a9e6 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -51,14 +51,6 @@ initiate_action () { esac } -init_basic_state () { - orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")" - mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")" - rm -f "$(git rev-parse --git-path REBASE_HEAD)" - - : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")" -} - git_rebase__interactive () { initiate_action "$action" ret=$? @@ -67,7 +59,6 @@ git_rebase__interactive () { fi git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose} - init_basic_state git rebase--helper --init-basic-state ${upstream:+--upstream "$upstream"} \ ${onto:+--onto "$onto"} ${head_name:+--head-name "$head_name"} \ From d833c4e559707d286d78d530d64612c8e7117328 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:42 +0200 Subject: [PATCH 163/406] rebase -i: implement the main part of interactive rebase as a builtin This rewrites the part of interactive rebase which initializes the basic state, make the script and complete the action, as a buitin, named git-rebase--interactive2 for now. Others modes (`--continue`, `--edit-todo`, etc.) will be rewritten in the next commit. git-rebase--interactive.sh is modified to call git-rebase--interactive2 instead of git-rebase--helper. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- .gitignore | 1 + Makefile | 1 + builtin.h | 1 + builtin/rebase--interactive2.c | 207 +++++++++++++++++++++++++++++++++ git-rebase--interactive.sh | 41 ++++--- git.c | 1 + 6 files changed, 233 insertions(+), 19 deletions(-) create mode 100644 builtin/rebase--interactive2.c diff --git a/.gitignore b/.gitignore index ffceea7d59fd21..a81219a0bbc6a7 100644 --- a/.gitignore +++ b/.gitignore @@ -119,6 +119,7 @@ /git-rebase--am /git-rebase--helper /git-rebase--interactive +/git-rebase--interactive2 /git-rebase--merge /git-rebase--preserve-merges /git-receive-pack diff --git a/Makefile b/Makefile index e819e4dcb62c71..ceed422ee04488 100644 --- a/Makefile +++ b/Makefile @@ -1074,6 +1074,7 @@ BUILTIN_OBJS += builtin/pull.o BUILTIN_OBJS += builtin/push.o BUILTIN_OBJS += builtin/range-diff.o BUILTIN_OBJS += builtin/read-tree.o +BUILTIN_OBJS += builtin/rebase--interactive2.o BUILTIN_OBJS += builtin/rebase--helper.o BUILTIN_OBJS += builtin/receive-pack.o BUILTIN_OBJS += builtin/reflog.o diff --git a/builtin.h b/builtin.h index 99206df4bd43fc..fc2f15e1df72e0 100644 --- a/builtin.h +++ b/builtin.h @@ -203,6 +203,7 @@ extern int cmd_pull(int argc, const char **argv, const char *prefix); extern int cmd_push(int argc, const char **argv, const char *prefix); extern int cmd_range_diff(int argc, const char **argv, const char *prefix); extern int cmd_read_tree(int argc, const char **argv, const char *prefix); +extern int cmd_rebase__interactive(int argc, const char **argv, const char *prefix); extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix); extern int cmd_receive_pack(int argc, const char **argv, const char *prefix); extern int cmd_reflog(int argc, const char **argv, const char *prefix); diff --git a/builtin/rebase--interactive2.c b/builtin/rebase--interactive2.c new file mode 100644 index 00000000000000..45336073a87444 --- /dev/null +++ b/builtin/rebase--interactive2.c @@ -0,0 +1,207 @@ +#include "builtin.h" +#include "cache.h" +#include "config.h" +#include "parse-options.h" +#include "sequencer.h" +#include "rebase-interactive.h" +#include "argv-array.h" +#include "refs.h" +#include "rerere.h" +#include "run-command.h" + +static GIT_PATH_FUNC(path_state_dir, "rebase-merge/") +static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") +static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive") + +static int get_revision_ranges(const char *upstream, const char *onto, + const char **head_hash, + char **revisions, char **shortrevisions) +{ + const char *base_rev = upstream ? upstream : onto, *shorthead; + struct object_id orig_head; + + if (get_oid("HEAD", &orig_head)) + return error(_("no HEAD?")); + + *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ); + *revisions = xstrfmt("%s...%s", base_rev, *head_hash); + + shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV); + + if (upstream) { + const char *shortrev; + struct object_id rev_oid; + + get_oid(base_rev, &rev_oid); + shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV); + + *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead); + } else + *shortrevisions = xstrdup(shorthead); + + return 0; +} + +static int init_basic_state(struct replay_opts *opts, const char *head_name, + const char *onto, const char *orig_head) +{ + FILE *interactive; + + if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir())) + return error_errno(_("could not create temporary %s"), path_state_dir()); + + delete_reflog("REBASE_HEAD"); + + interactive = fopen(path_interactive(), "w"); + if (!interactive) + return error_errno(_("could not mark as interactive")); + fclose(interactive); + + return write_basic_state(opts, head_name, onto, orig_head); +} + +static int do_interactive_rebase(struct replay_opts *opts, unsigned flags, + const char *switch_to, const char *upstream, + const char *onto, const char *onto_name, + const char *squash_onto, const char *head_name, + const char *restrict_revision, char *raw_strategies, + const char *cmd, unsigned autosquash) +{ + int ret; + const char *head_hash = NULL; + char *revisions = NULL, *shortrevisions = NULL; + struct argv_array make_script_args = ARGV_ARRAY_INIT; + FILE *todo_list; + + if (prepare_branch_to_be_rebased(opts, switch_to)) + return -1; + + if (get_revision_ranges(upstream, onto, &head_hash, + &revisions, &shortrevisions)) + return -1; + + if (raw_strategies) + parse_strategy_opts(opts, raw_strategies); + + if (init_basic_state(opts, head_name, onto, head_hash)) { + free(revisions); + free(shortrevisions); + + return -1; + } + + if (!upstream && squash_onto) + write_file(path_squash_onto(), "%s\n", squash_onto); + + todo_list = fopen(rebase_path_todo(), "w"); + if (!todo_list) { + free(revisions); + free(shortrevisions); + + return error_errno(_("could not open %s"), rebase_path_todo()); + } + + argv_array_pushl(&make_script_args, "", revisions, NULL); + if (restrict_revision) + argv_array_push(&make_script_args, restrict_revision); + + ret = sequencer_make_script(todo_list, + make_script_args.argc, make_script_args.argv, + flags); + fclose(todo_list); + + if (ret) + error(_("could not generate todo list")); + else { + discard_cache(); + ret = complete_action(opts, flags, shortrevisions, onto_name, onto, + head_hash, cmd, autosquash); + } + + free(revisions); + free(shortrevisions); + argv_array_clear(&make_script_args); + + return ret; +} + +static const char * const builtin_rebase_interactive_usage[] = { + N_("git rebase--interactive [<options>]"), + NULL +}; + +int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) +{ + struct replay_opts opts = REPLAY_OPTS_INIT; + unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; + int abbreviate_commands = 0, rebase_cousins = -1; + const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL, + *squash_onto = NULL, *upstream = NULL, *head_name = NULL, + *switch_to = NULL, *cmd = NULL; + char *raw_strategies = NULL; + struct option options[] = { + OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), + OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")), + OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message, + N_("allow commits with empty messages")), + OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), + OPT_BOOL(0, "rebase-cousins", &rebase_cousins, + N_("keep original branch points of cousins")), + OPT_BOOL(0, "autosquash", &autosquash, + N_("move commits that begin with squash!/fixup!")), + OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")), + OPT__VERBOSE(&opts.verbose, N_("be verbose")), + OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")), + OPT_STRING(0, "restrict-revision", &restrict_revision, + N_("restrict-revision"), N_("restrict revision")), + OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"), + N_("squash onto")), + OPT_STRING(0, "upstream", &upstream, N_("upstream"), + N_("the upstream commit")), + OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")), + { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign, N_("key-id"), + N_("GPG-sign commits"), + PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, + OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"), + N_("rebase strategy")), + OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"), + N_("strategy options")), + OPT_STRING(0, "switch-to", &switch_to, N_("switch-to"), + N_("the branch or commit to checkout")), + OPT_STRING(0, "onto-name", &onto_name, N_("onto-name"), N_("onto name")), + OPT_STRING(0, "cmd", &cmd, N_("cmd"), N_("the command to run")), + OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto), + OPT_END() + }; + + sequencer_init_config(&opts); + git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands); + + opts.action = REPLAY_INTERACTIVE_REBASE; + opts.allow_ff = 1; + opts.allow_empty = 1; + + if (argc == 1) + usage_with_options(builtin_rebase_interactive_usage, options); + + argc = parse_options(argc, argv, NULL, options, + builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0); + + opts.gpg_sign = xstrdup_or_null(opts.gpg_sign); + + flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0; + flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; + flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0; + flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0; + + if (rebase_cousins >= 0 && !rebase_merges) + warning(_("--[no-]rebase-cousins has no effect without " + "--rebase-merges")); + + if (!onto && !upstream) + die(_("a base commit must be provided with --upstream or --onto")); + + return !!do_interactive_rebase(&opts, flags, switch_to, upstream, onto, + onto_name, squash_onto, head_name, restrict_revision, + raw_strategies, cmd, autosquash); +} diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 761be95ed1a9e6..e87d708a4db8a5 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -58,24 +58,27 @@ git_rebase__interactive () { return 0 fi - git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose} + test -n "$keep_empty" && keep_empty="--keep-empty" + test -n "$rebase_merges" && rebase_merges="--rebase-merges" + test -n "$rebase_cousins" && rebase_cousins="--rebase-cousins" + test -n "$autosquash" && autosquash="--autosquash" + test -n "$verbose" && verbose="--verbose" + test -n "$force_rebase" && force_rebase="--no-ff" + test -n "$restrict_revisions" && restrict_revisions="--restrict-revisions=^$restrict_revisions" + test -n "$upstream" && upstream="--upstream=$upstream" + test -n "$onto" && onto="--onto=$onto" + test -n "$squash_onto" && squash_onto="--squash-onto=$squash_onto" + test -n "$onto_name" && onto_name="--onto-name=$onto_name" + test -n "$head_name" && head_name="--head-name=$head_name" + test -n "$strategy" && strategy="--strategy=$strategy" + test -n "$strategy_opts" && strategy_opts="--strategy-opts=$strategy_opts" + test -n "$switch_to" && switch_to="--switch-to=$switch_to" + test -n "$cmd" && cmd="--cmd=$cmd" - git rebase--helper --init-basic-state ${upstream:+--upstream "$upstream"} \ - ${onto:+--onto "$onto"} ${head_name:+--head-name "$head_name"} \ - ${verbose:+--verbose} ${strategy:+--strategy "$strategy"} \ - ${strategy_opts:+--strategy-opts="$strategy_opts"} \ - "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" || exit - - git rebase--helper --make-script ${keep_empty:+--keep-empty} \ - ${rebase_merges:+--rebase-merges} \ - ${rebase_cousins:+--rebase-cousins} \ - ${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"} \ - ${squash_onto:+--squash-onto "$squash_onto"} \ - ${restrict_revision:+--restrict-revision ^"$restrict_revision"} >"$todo" || - die "$(gettext "Could not generate todo list")" - - exec git rebase--helper --complete-action "$onto_name" "$cmd" \ - $allow_empty_message ${autosquash:+--autosquash} ${verbose:+--verbose} \ - ${keep_empty:+--keep-empty} ${force_rebase:+--no-ff} \ - ${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"} + exec git rebase--interactive2 "$keep_empty" "$rebase_merges" "$rebase_cousins" \ + "$upstream" "$onto" "$squash_onto" "$restrict_revision" \ + "$allow_empty_message" "$autosquash" "$verbose" \ + "$force_rebase" "$onto_name" "$head_name" "$strategy" \ + "$strategy_opts" "$cmd" "$switch_to" \ + "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" } diff --git a/git.c b/git.c index c27c38738b2a9d..acb6d96e2e6fe3 100644 --- a/git.c +++ b/git.c @@ -522,6 +522,7 @@ static struct cmd_struct commands[] = { { "push", cmd_push, RUN_SETUP }, { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX}, + { "rebase--interactive2", cmd_rebase__interactive, RUN_SETUP | NEED_WORK_TREE }, { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE }, { "receive-pack", cmd_receive_pack }, { "reflog", cmd_reflog, RUN_SETUP }, From 5946180af8642fa94808378a666470b10f7366d9 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:43 +0200 Subject: [PATCH 164/406] rebase--interactive2: rewrite the submodes of interactive rebase in C This rewrites the submodes of interactive rebase (`--continue`, `--skip`, `--edit-todo`, and `--show-current-patch`) in C. git-rebase.sh is then modified to call directly git-rebase--interactive2 instead of git-rebase--interactive.sh. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase--interactive2.c | 51 ++++++++++++++++++++++++++++++---- git-rebase.sh | 45 +++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/builtin/rebase--interactive2.c b/builtin/rebase--interactive2.c index 45336073a87444..aaf13c9621dcca 100644 --- a/builtin/rebase--interactive2.c +++ b/builtin/rebase--interactive2.c @@ -134,11 +134,14 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; - int abbreviate_commands = 0, rebase_cousins = -1; + int abbreviate_commands = 0, rebase_cousins = -1, ret = 0; const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL, *squash_onto = NULL, *upstream = NULL, *head_name = NULL, *switch_to = NULL, *cmd = NULL; char *raw_strategies = NULL; + enum { + NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH + } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")), @@ -151,6 +154,13 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) N_("move commits that begin with squash!/fixup!")), OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")), OPT__VERBOSE(&opts.verbose, N_("be verbose")), + OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), + CONTINUE), + OPT_CMDMODE(0, "skip", &command, N_("skip commit"), SKIP), + OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"), + EDIT_TODO), + OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"), + SHOW_CURRENT_PATCH), OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")), OPT_STRING(0, "restrict-revision", &restrict_revision, N_("restrict-revision"), N_("restrict revision")), @@ -198,10 +208,39 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) warning(_("--[no-]rebase-cousins has no effect without " "--rebase-merges")); - if (!onto && !upstream) - die(_("a base commit must be provided with --upstream or --onto")); + switch (command) { + case NONE: + if (!onto && !upstream) + die(_("a base commit must be provided with --upstream or --onto")); + + ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto, + onto_name, squash_onto, head_name, restrict_revision, + raw_strategies, cmd, autosquash); + break; + case SKIP: { + struct string_list merge_rr = STRING_LIST_INIT_DUP; + + rerere_clear(&merge_rr); + /* fallthrough */ + case CONTINUE: + ret = sequencer_continue(&opts); + break; + } + case EDIT_TODO: + ret = edit_todo_list(flags); + break; + case SHOW_CURRENT_PATCH: { + struct child_process cmd = CHILD_PROCESS_INIT; + + cmd.git_cmd = 1; + argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL); + ret = run_command(&cmd); + + break; + } + default: + BUG("invalid command '%d'", command); + } - return !!do_interactive_rebase(&opts, flags, switch_to, upstream, onto, - onto_name, squash_onto, head_name, restrict_revision, - raw_strategies, cmd, autosquash); + return !!ret; } diff --git a/git-rebase.sh b/git-rebase.sh index 51a6db7daa0e65..6e1e413cf275c2 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -200,19 +200,56 @@ finish_rebase () { rm -rf "$state_dir" } +run_interactive () { + GIT_CHERRY_PICK_HELP="$resolvemsg" + export GIT_CHERRY_PICK_HELP + + test -n "$keep_empty" && keep_empty="--keep-empty" + test -n "$rebase_merges" && rebase_merges="--rebase-merges" + test -n "$rebase_cousins" && rebase_cousins="--rebase-cousins" + test -n "$autosquash" && autosquash="--autosquash" + test -n "$verbose" && verbose="--verbose" + test -n "$force_rebase" && force_rebase="--no-ff" + test -n "$restrict_revision" && \ + restrict_revision="--restrict-revision=^$restrict_revision" + test -n "$upstream" && upstream="--upstream=$upstream" + test -n "$onto" && onto="--onto=$onto" + test -n "$squash_onto" && squash_onto="--squash-onto=$squash_onto" + test -n "$onto_name" && onto_name="--onto-name=$onto_name" + test -n "$head_name" && head_name="--head-name=$head_name" + test -n "$strategy" && strategy="--strategy=$strategy" + test -n "$strategy_opts" && strategy_opts="--strategy-opts=$strategy_opts" + test -n "$switch_to" && switch_to="--switch-to=$switch_to" + test -n "$cmd" && cmd="--cmd=$cmd" + test -n "$action" && action="--$action" + + exec git rebase--interactive2 "$action" "$keep_empty" "$rebase_merges" "$rebase_cousins" \ + "$upstream" "$onto" "$squash_onto" "$restrict_revision" \ + "$allow_empty_message" "$autosquash" "$verbose" \ + "$force_rebase" "$onto_name" "$head_name" "$strategy" \ + "$strategy_opts" "$cmd" "$switch_to" \ + "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" +} + run_specific_rebase () { if [ "$interactive_rebase" = implied ]; then GIT_EDITOR=: export GIT_EDITOR autosquash= fi - . git-rebase--$type - if test -z "$preserve_merges" + if test -n "$interactive_rebase" -a -z "$preserve_merges" then - git_rebase__$type + run_interactive else - git_rebase__preserve_merges + . git-rebase--$type + + if test -z "$preserve_merges" + then + git_rebase__$type + else + git_rebase__preserve_merges + fi fi ret=$? From 9bf613bb19e75f14aecc444941c6c7696da925ad Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:44 +0200 Subject: [PATCH 165/406] rebase -i: remove git-rebase--interactive.sh This removes git-rebase--interactive.sh, as its functionnality has been replaced by git-rebase--interactive2. git-rebase--interactive2.c is then renamed to git-rebase--interactive.c. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- .gitignore | 1 - Makefile | 4 +- ...--interactive2.c => rebase--interactive.c} | 0 git-rebase--interactive.sh | 84 ------------------- git-rebase.sh | 2 +- git.c | 2 +- 6 files changed, 3 insertions(+), 90 deletions(-) rename builtin/{rebase--interactive2.c => rebase--interactive.c} (100%) delete mode 100644 git-rebase--interactive.sh diff --git a/.gitignore b/.gitignore index a81219a0bbc6a7..ffceea7d59fd21 100644 --- a/.gitignore +++ b/.gitignore @@ -119,7 +119,6 @@ /git-rebase--am /git-rebase--helper /git-rebase--interactive -/git-rebase--interactive2 /git-rebase--merge /git-rebase--preserve-merges /git-receive-pack diff --git a/Makefile b/Makefile index ceed422ee04488..e78023ae4866d0 100644 --- a/Makefile +++ b/Makefile @@ -624,7 +624,6 @@ SCRIPT_SH += git-web--browse.sh SCRIPT_LIB += git-mergetool--lib SCRIPT_LIB += git-parse-remote SCRIPT_LIB += git-rebase--am -SCRIPT_LIB += git-rebase--interactive SCRIPT_LIB += git-rebase--preserve-merges SCRIPT_LIB += git-rebase--merge SCRIPT_LIB += git-sh-setup @@ -1074,8 +1073,8 @@ BUILTIN_OBJS += builtin/pull.o BUILTIN_OBJS += builtin/push.o BUILTIN_OBJS += builtin/range-diff.o BUILTIN_OBJS += builtin/read-tree.o -BUILTIN_OBJS += builtin/rebase--interactive2.o BUILTIN_OBJS += builtin/rebase--helper.o +BUILTIN_OBJS += builtin/rebase--interactive.o BUILTIN_OBJS += builtin/receive-pack.o BUILTIN_OBJS += builtin/reflog.o BUILTIN_OBJS += builtin/remote.o @@ -2414,7 +2413,6 @@ XGETTEXT_FLAGS_PERL = $(XGETTEXT_FLAGS) --language=Perl \ LOCALIZED_C = $(C_OBJ:o=c) $(LIB_H) $(GENERATED_H) LOCALIZED_SH = $(SCRIPT_SH) LOCALIZED_SH += git-parse-remote.sh -LOCALIZED_SH += git-rebase--interactive.sh LOCALIZED_SH += git-rebase--preserve-merges.sh LOCALIZED_SH += git-sh-setup.sh LOCALIZED_PERL = $(SCRIPT_PERL) diff --git a/builtin/rebase--interactive2.c b/builtin/rebase--interactive.c similarity index 100% rename from builtin/rebase--interactive2.c rename to builtin/rebase--interactive.c diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh deleted file mode 100644 index e87d708a4db8a5..00000000000000 --- a/git-rebase--interactive.sh +++ /dev/null @@ -1,84 +0,0 @@ -# This shell script fragment is sourced by git-rebase to implement -# its interactive mode. "git rebase --interactive" makes it easy -# to fix up commits in the middle of a series and rearrange commits. -# -# Copyright (c) 2006 Johannes E. Schindelin -# -# The original idea comes from Eric W. Biederman, in -# https://public-inbox.org/git/m1odwkyuf5.fsf_-_@ebiederm.dsl.xmission.com/ -# -# The file containing rebase commands, comments, and empty lines. -# This file is created by "git rebase -i" then edited by the user. As -# the lines are processed, they are removed from the front of this -# file and written to the tail of $done. -todo="$state_dir"/git-rebase-todo - -GIT_CHERRY_PICK_HELP="$resolvemsg" -export GIT_CHERRY_PICK_HELP - -# Initiate an action. If the cannot be any -# further action it may exec a command -# or exit and not return. -# -# TODO: Consider a cleaner return model so it -# never exits and always return 0 if process -# is complete. -# -# Parameter 1 is the action to initiate. -# -# Returns 0 if the action was able to complete -# and if 1 if further processing is required. -initiate_action () { - case "$1" in - continue) - exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ - --continue - ;; - skip) - git rerere clear - exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ - --continue - ;; - edit-todo) - exec git rebase--helper --edit-todo - ;; - show-current-patch) - exec git show REBASE_HEAD -- - ;; - *) - return 1 # continue - ;; - esac -} - -git_rebase__interactive () { - initiate_action "$action" - ret=$? - if test $ret = 0; then - return 0 - fi - - test -n "$keep_empty" && keep_empty="--keep-empty" - test -n "$rebase_merges" && rebase_merges="--rebase-merges" - test -n "$rebase_cousins" && rebase_cousins="--rebase-cousins" - test -n "$autosquash" && autosquash="--autosquash" - test -n "$verbose" && verbose="--verbose" - test -n "$force_rebase" && force_rebase="--no-ff" - test -n "$restrict_revisions" && restrict_revisions="--restrict-revisions=^$restrict_revisions" - test -n "$upstream" && upstream="--upstream=$upstream" - test -n "$onto" && onto="--onto=$onto" - test -n "$squash_onto" && squash_onto="--squash-onto=$squash_onto" - test -n "$onto_name" && onto_name="--onto-name=$onto_name" - test -n "$head_name" && head_name="--head-name=$head_name" - test -n "$strategy" && strategy="--strategy=$strategy" - test -n "$strategy_opts" && strategy_opts="--strategy-opts=$strategy_opts" - test -n "$switch_to" && switch_to="--switch-to=$switch_to" - test -n "$cmd" && cmd="--cmd=$cmd" - - exec git rebase--interactive2 "$keep_empty" "$rebase_merges" "$rebase_cousins" \ - "$upstream" "$onto" "$squash_onto" "$restrict_revision" \ - "$allow_empty_message" "$autosquash" "$verbose" \ - "$force_rebase" "$onto_name" "$head_name" "$strategy" \ - "$strategy_opts" "$cmd" "$switch_to" \ - "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" -} diff --git a/git-rebase.sh b/git-rebase.sh index 6e1e413cf275c2..3e7798e07b9b0f 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -223,7 +223,7 @@ run_interactive () { test -n "$cmd" && cmd="--cmd=$cmd" test -n "$action" && action="--$action" - exec git rebase--interactive2 "$action" "$keep_empty" "$rebase_merges" "$rebase_cousins" \ + exec git rebase--interactive "$action" "$keep_empty" "$rebase_merges" "$rebase_cousins" \ "$upstream" "$onto" "$squash_onto" "$restrict_revision" \ "$allow_empty_message" "$autosquash" "$verbose" \ "$force_rebase" "$onto_name" "$head_name" "$strategy" \ diff --git a/git.c b/git.c index acb6d96e2e6fe3..9407a091de4cde 100644 --- a/git.c +++ b/git.c @@ -522,7 +522,7 @@ static struct cmd_struct commands[] = { { "push", cmd_push, RUN_SETUP }, { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX}, - { "rebase--interactive2", cmd_rebase__interactive, RUN_SETUP | NEED_WORK_TREE }, + { "rebase--interactive", cmd_rebase__interactive, RUN_SETUP | NEED_WORK_TREE }, { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE }, { "receive-pack", cmd_receive_pack }, { "reflog", cmd_reflog, RUN_SETUP }, From 1c0fa29bb201e0bc331e7f8af56c711efe4d1970 Mon Sep 17 00:00:00 2001 From: Alban Gruin <alban.gruin@gmail.com> Date: Tue, 28 Aug 2018 14:10:45 +0200 Subject: [PATCH 166/406] rebase -i: move rebase--helper modes to rebase--interactive This moves the rebase--helper modes still used by git-rebase--preserve-merges.sh (`--shorten-ids`, `--expand-ids`, `--check-todo-list`, `--rearrange-squash` and `--add-exec-commands`) to rebase--interactive.c. git-rebase--preserve-merges.sh is modified accordingly, and rebase--helper.c is removed as it is useless now. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- .gitignore | 1 - Makefile | 1 - builtin.h | 1 - builtin/rebase--helper.c | 226 --------------------------------- builtin/rebase--interactive.c | 27 +++- git-rebase--preserve-merges.sh | 10 +- git.c | 1 - 7 files changed, 31 insertions(+), 236 deletions(-) delete mode 100644 builtin/rebase--helper.c diff --git a/.gitignore b/.gitignore index ffceea7d59fd21..b3c118bbfc51a9 100644 --- a/.gitignore +++ b/.gitignore @@ -117,7 +117,6 @@ /git-read-tree /git-rebase /git-rebase--am -/git-rebase--helper /git-rebase--interactive /git-rebase--merge /git-rebase--preserve-merges diff --git a/Makefile b/Makefile index e78023ae4866d0..137219b04cf3b2 100644 --- a/Makefile +++ b/Makefile @@ -1073,7 +1073,6 @@ BUILTIN_OBJS += builtin/pull.o BUILTIN_OBJS += builtin/push.o BUILTIN_OBJS += builtin/range-diff.o BUILTIN_OBJS += builtin/read-tree.o -BUILTIN_OBJS += builtin/rebase--helper.o BUILTIN_OBJS += builtin/rebase--interactive.o BUILTIN_OBJS += builtin/receive-pack.o BUILTIN_OBJS += builtin/reflog.o diff --git a/builtin.h b/builtin.h index fc2f15e1df72e0..a0c3176c69708d 100644 --- a/builtin.h +++ b/builtin.h @@ -204,7 +204,6 @@ extern int cmd_push(int argc, const char **argv, const char *prefix); extern int cmd_range_diff(int argc, const char **argv, const char *prefix); extern int cmd_read_tree(int argc, const char **argv, const char *prefix); extern int cmd_rebase__interactive(int argc, const char **argv, const char *prefix); -extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix); extern int cmd_receive_pack(int argc, const char **argv, const char *prefix); extern int cmd_reflog(int argc, const char **argv, const char *prefix); extern int cmd_remote(int argc, const char **argv, const char *prefix); diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c deleted file mode 100644 index f8128037d3fb3e..00000000000000 --- a/builtin/rebase--helper.c +++ /dev/null @@ -1,226 +0,0 @@ -#include "builtin.h" -#include "cache.h" -#include "config.h" -#include "parse-options.h" -#include "sequencer.h" -#include "rebase-interactive.h" -#include "argv-array.h" -#include "refs.h" -#include "rerere.h" -#include "alias.h" - -static GIT_PATH_FUNC(path_state_dir, "rebase-merge/") -static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") -static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive") - -static int get_revision_ranges(const char *upstream, const char *onto, - const char **head_hash, - char **revisions, char **shortrevisions) -{ - const char *base_rev = upstream ? upstream : onto; - struct object_id orig_head; - - if (get_oid("HEAD", &orig_head)) - return error(_("no HEAD?")); - - *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ); - - if (revisions) - *revisions = xstrfmt("%s...%s", base_rev, *head_hash); - if (shortrevisions) { - const char *shorthead; - - shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV); - - if (upstream) { - const char *shortrev; - struct object_id rev_oid; - - get_oid(base_rev, &rev_oid); - shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV); - - *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead); - } else - *shortrevisions = xstrdup(shorthead); - } - - return 0; -} - -static int init_basic_state(struct replay_opts *opts, const char *head_name, - const char *onto, const char *orig_head) -{ - FILE *interactive; - - if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir())) - return error_errno(_("could not create temporary %s"), path_state_dir()); - - delete_reflog("REBASE_HEAD"); - - interactive = fopen(path_interactive(), "w"); - if (!interactive) - return error_errno(_("could not mark as interactive")); - fclose(interactive); - - return write_basic_state(opts, head_name, onto, orig_head); -} - -static const char * const builtin_rebase_helper_usage[] = { - N_("git rebase--helper [<options>]"), - NULL -}; - -int cmd_rebase__helper(int argc, const char **argv, const char *prefix) -{ - struct replay_opts opts = REPLAY_OPTS_INIT; - unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; - int abbreviate_commands = 0, rebase_cousins = -1, ret; - const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL, - *squash_onto = NULL, *upstream = NULL, *head_name = NULL; - char *raw_strategies = NULL; - enum { - CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, - CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH, - COMPLETE_ACTION, INIT_BASIC_STATE - } command = 0; - struct option options[] = { - OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), - OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")), - OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message, - N_("allow commits with empty messages")), - OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), - OPT_BOOL(0, "rebase-cousins", &rebase_cousins, - N_("keep original branch points of cousins")), - OPT_BOOL(0, "autosquash", &autosquash, - N_("move commits that begin with squash!/fixup!")), - OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")), - OPT__VERBOSE(&opts.verbose, N_("be verbose")), - OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), - CONTINUE), - OPT_CMDMODE(0, "abort", &command, N_("abort rebase"), - ABORT), - OPT_CMDMODE(0, "make-script", &command, - N_("make rebase script"), MAKE_SCRIPT), - OPT_CMDMODE(0, "shorten-ids", &command, - N_("shorten commit ids in the todo list"), SHORTEN_OIDS), - OPT_CMDMODE(0, "expand-ids", &command, - N_("expand commit ids in the todo list"), EXPAND_OIDS), - OPT_CMDMODE(0, "check-todo-list", &command, - N_("check the todo list"), CHECK_TODO_LIST), - OPT_CMDMODE(0, "rearrange-squash", &command, - N_("rearrange fixup/squash lines"), REARRANGE_SQUASH), - OPT_CMDMODE(0, "add-exec-commands", &command, - N_("insert exec commands in todo list"), ADD_EXEC), - OPT_CMDMODE(0, "edit-todo", &command, - N_("edit the todo list during an interactive rebase"), - EDIT_TODO), - OPT_CMDMODE(0, "prepare-branch", &command, - N_("prepare the branch to be rebased"), PREPARE_BRANCH), - OPT_CMDMODE(0, "complete-action", &command, - N_("complete the action"), COMPLETE_ACTION), - OPT_CMDMODE(0, "init-basic-state", &command, - N_("initialise the rebase state"), INIT_BASIC_STATE), - OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")), - OPT_STRING(0, "restrict-revision", &restrict_revision, - N_("restrict-revision"), N_("restrict revision")), - OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"), - N_("squash onto")), - OPT_STRING(0, "upstream", &upstream, N_("upstream"), - N_("the upstream commit")), - OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")), - OPT_STRING('S', "gpg-sign", &opts.gpg_sign, N_("gpg-sign"), - N_("GPG-sign commits")), - OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"), - N_("rebase strategy")), - OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"), - N_("strategy options")), - OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto), - OPT_END() - }; - - sequencer_init_config(&opts); - git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands); - - opts.action = REPLAY_INTERACTIVE_REBASE; - opts.allow_ff = 1; - opts.allow_empty = 1; - - argc = parse_options(argc, argv, NULL, options, - builtin_rebase_helper_usage, PARSE_OPT_KEEP_ARGV0); - - flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0; - flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; - flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0; - flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0; - flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0; - - if (rebase_cousins >= 0 && !rebase_merges) - warning(_("--[no-]rebase-cousins has no effect without " - "--rebase-merges")); - - if (command == CONTINUE && argc == 1) - return !!sequencer_continue(&opts); - if (command == ABORT && argc == 1) - return !!sequencer_remove_state(&opts); - if (command == MAKE_SCRIPT && argc == 1) { - char *revisions = NULL; - struct argv_array make_script_args = ARGV_ARRAY_INIT; - - if (!upstream && squash_onto) - write_file(path_squash_onto(), "%s\n", squash_onto); - - ret = get_revision_ranges(upstream, onto, &head_hash, &revisions, NULL); - if (ret) - return ret; - - argv_array_pushl(&make_script_args, "", revisions, NULL); - if (restrict_revision) - argv_array_push(&make_script_args, restrict_revision); - - ret = sequencer_make_script(stdout, - make_script_args.argc, make_script_args.argv, - flags); - - free(revisions); - argv_array_clear(&make_script_args); - - return !!ret; - } - if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1) - return !!transform_todos(flags); - if (command == CHECK_TODO_LIST && argc == 1) - return !!check_todo_list(); - if (command == REARRANGE_SQUASH && argc == 1) - return !!rearrange_squash(); - if (command == ADD_EXEC && argc == 2) - return !!sequencer_add_exec_commands(argv[1]); - if (command == EDIT_TODO && argc == 1) - return !!edit_todo_list(flags); - if (command == PREPARE_BRANCH && argc == 2) - return !!prepare_branch_to_be_rebased(&opts, argv[1]); - if (command == COMPLETE_ACTION && argc == 3) { - char *shortrevisions = NULL; - - ret = get_revision_ranges(upstream, onto, &head_hash, NULL, &shortrevisions); - if (ret) - return ret; - - ret = complete_action(&opts, flags, shortrevisions, argv[1], onto, - head_hash, argv[2], autosquash); - - free(shortrevisions); - return !!ret; - } - if (command == INIT_BASIC_STATE) { - if (raw_strategies) - parse_strategy_opts(&opts, raw_strategies); - - ret = get_revision_ranges(upstream, onto, &head_hash, NULL, NULL); - if (ret) - return ret; - - return !!init_basic_state(&opts, head_name, onto, head_hash); - } - - usage_with_options(builtin_rebase_helper_usage, options); -} diff --git a/builtin/rebase--interactive.c b/builtin/rebase--interactive.c index aaf13c9621dcca..a2ab68ed063224 100644 --- a/builtin/rebase--interactive.c +++ b/builtin/rebase--interactive.c @@ -140,7 +140,8 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) *switch_to = NULL, *cmd = NULL; char *raw_strategies = NULL; enum { - NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH + NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH, + SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -161,6 +162,16 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) EDIT_TODO), OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"), SHOW_CURRENT_PATCH), + OPT_CMDMODE(0, "shorten-ids", &command, + N_("shorten commit ids in the todo list"), SHORTEN_OIDS), + OPT_CMDMODE(0, "expand-ids", &command, + N_("expand commit ids in the todo list"), EXPAND_OIDS), + OPT_CMDMODE(0, "check-todo-list", &command, + N_("check the todo list"), CHECK_TODO_LIST), + OPT_CMDMODE(0, "rearrange-squash", &command, + N_("rearrange fixup/squash lines"), REARRANGE_SQUASH), + OPT_CMDMODE(0, "add-exec-commands", &command, + N_("insert exec commands in todo list"), ADD_EXEC), OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")), OPT_STRING(0, "restrict-revision", &restrict_revision, N_("restrict-revision"), N_("restrict revision")), @@ -203,6 +214,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0; flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0; + flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0; if (rebase_cousins >= 0 && !rebase_merges) warning(_("--[no-]rebase-cousins has no effect without " @@ -238,6 +250,19 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) break; } + case SHORTEN_OIDS: + case EXPAND_OIDS: + ret = transform_todos(flags); + break; + case CHECK_TODO_LIST: + ret = check_todo_list(); + break; + case REARRANGE_SQUASH: + ret = rearrange_squash(); + break; + case ADD_EXEC: + ret = sequencer_add_exec_commands(cmd); + break; default: BUG("invalid command '%d'", command); } diff --git a/git-rebase--preserve-merges.sh b/git-rebase--preserve-merges.sh index c214c5e4d6ce21..afbb65765d4610 100644 --- a/git-rebase--preserve-merges.sh +++ b/git-rebase--preserve-merges.sh @@ -711,11 +711,11 @@ do_rest () { } expand_todo_ids() { - git rebase--helper --expand-ids + git rebase--interactive --expand-ids } collapse_todo_ids() { - git rebase--helper --shorten-ids + git rebase--interactive --shorten-ids } # Switch to the branch in $into and notify it in the reflog @@ -876,8 +876,8 @@ init_revisions_and_shortrevisions () { complete_action() { test -s "$todo" || echo noop >> "$todo" - test -z "$autosquash" || git rebase--helper --rearrange-squash || exit - test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd" + test -z "$autosquash" || git rebase--interactive --rearrange-squash || exit + test -n "$cmd" && git rebase--interactive --add-exec-commands --cmd "$cmd" todocount=$(git stripspace --strip-comments <"$todo" | wc -l) todocount=${todocount##* } @@ -912,7 +912,7 @@ However, if you remove everything, the rebase will be aborted. has_action "$todo" || return 2 - git rebase--helper --check-todo-list || { + git rebase--interactive --check-todo-list || { ret=$? checkout_onto exit $ret diff --git a/git.c b/git.c index 9407a091de4cde..b4c8d303d29465 100644 --- a/git.c +++ b/git.c @@ -523,7 +523,6 @@ static struct cmd_struct commands[] = { { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX}, { "rebase--interactive", cmd_rebase__interactive, RUN_SETUP | NEED_WORK_TREE }, - { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE }, { "receive-pack", cmd_receive_pack }, { "reflog", cmd_reflog, RUN_SETUP }, { "remote", cmd_remote, RUN_SETUP }, From e76af7b36155c0df9329a84132e6305f2383c725 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 29 Aug 2018 07:31:17 -0700 Subject: [PATCH 167/406] builtin rebase: prepare for builtin rebase -i The builtin rebase and the builtin interactive rebase have been developed independently, on purpose: Google Summer of Code rules specifically state that students have to work on independent projects, they cannot collaborate on the same project. One fallout is that the rebase-in-c and rebase-i-in-c patches cause no merge conflicts but a royal number of tests in the test suite to fail. It is easy to explain why: rebase-in-c was developed under the assumption that all rebase backends are implemented in Unix shell script and can be sourced via `. git-rebase--<backend>`, which is no longer true with rebase-i-in-c, where git-rebase--interactive is a hard-linked builtin. This patch fixes that. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index d3d1d39bfa0fd6..286886717bfbab 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -326,6 +326,13 @@ static void add_var(struct strbuf *buf, const char *name, const char *value) } } +static const char *resolvemsg = +N_("Resolve all conflicts manually, mark them as resolved with\n" +"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n" +"You can instead skip this commit: run \"git rebase --skip\".\n" +"To abort and get back to the state before \"git rebase\", run " +"\"git rebase --abort\"."); + static int run_specific_rebase(struct rebase_options *opts) { const char *argv[] = { NULL, NULL }; @@ -333,6 +340,79 @@ static int run_specific_rebase(struct rebase_options *opts) int status; const char *backend, *backend_func; + if (opts->type == REBASE_INTERACTIVE) { + /* Run builtin interactive rebase */ + struct child_process child = CHILD_PROCESS_INIT; + + argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s", + resolvemsg); + if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) { + argv_array_push(&child.env_array, "GIT_EDITOR=:"); + opts->autosquash = 0; + } + + child.git_cmd = 1; + argv_array_push(&child.args, "rebase--interactive"); + + if (opts->action) + argv_array_pushf(&child.args, "--%s", opts->action); + if (opts->keep_empty) + argv_array_push(&child.args, "--keep-empty"); + if (opts->rebase_merges) + argv_array_push(&child.args, "--rebase-merges"); + if (opts->rebase_cousins) + argv_array_push(&child.args, "--rebase-cousins"); + if (opts->autosquash) + argv_array_push(&child.args, "--autosquash"); + if (opts->flags & REBASE_VERBOSE) + argv_array_push(&child.args, "--verbose"); + if (opts->flags & REBASE_FORCE) + argv_array_push(&child.args, "--no-ff"); + if (opts->restrict_revision) + argv_array_pushf(&child.args, + "--restrict-revision=^%s", + oid_to_hex(&opts->restrict_revision->object.oid)); + if (opts->upstream) + argv_array_pushf(&child.args, "--upstream=%s", + oid_to_hex(&opts->upstream->object.oid)); + if (opts->onto) + argv_array_pushf(&child.args, "--onto=%s", + oid_to_hex(&opts->onto->object.oid)); + if (opts->squash_onto) + argv_array_pushf(&child.args, "--squash-onto=%s", + oid_to_hex(opts->squash_onto)); + if (opts->onto_name) + argv_array_pushf(&child.args, "--onto-name=%s", + opts->onto_name); + argv_array_pushf(&child.args, "--head-name=%s", + opts->head_name ? + opts->head_name : "detached HEAD"); + if (opts->strategy) + argv_array_pushf(&child.args, "--strategy=%s", + opts->strategy); + if (opts->strategy_opts) + argv_array_pushf(&child.args, "--strategy-opts=%s", + opts->strategy_opts); + if (opts->switch_to) + argv_array_pushf(&child.args, "--switch-to=%s", + opts->switch_to); + if (opts->cmd) + argv_array_pushf(&child.args, "--cmd=%s", opts->cmd); + if (opts->allow_empty_message) + argv_array_push(&child.args, "--allow-empty-message"); + if (opts->allow_rerere_autoupdate > 0) + argv_array_push(&child.args, "--rerere-autoupdate"); + else if (opts->allow_rerere_autoupdate == 0) + argv_array_push(&child.args, "--no-rerere-autoupdate"); + if (opts->gpg_sign_opt) + argv_array_push(&child.args, opts->gpg_sign_opt); + if (opts->signoff) + argv_array_push(&child.args, "--signoff"); + + status = run_command(&child); + goto finished_rebase; + } + add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); add_var(&script_snippet, "state_dir", opts->state_dir); @@ -418,6 +498,7 @@ static int run_specific_rebase(struct rebase_options *opts) argv[0] = script_snippet.buf; status = run_command_v_opt(argv, RUN_USING_SHELL); +finished_rebase: if (opts->dont_finish_rebase) ; /* do nothing */ else if (status == 0) { From c3f03bf0bb719c2f0475cc8c3efb6d7ed9adc0bd Mon Sep 17 00:00:00 2001 From: Pratik Karki <predatoramigo@gmail.com> Date: Wed, 8 Aug 2018 21:38:02 +0545 Subject: [PATCH 168/406] rebase: default to using the builtin rebase Now that the builtin rebase is feature-complete, we should use it by default. Let's keep the legacy scripted version around for the time being; Once the builtin rebase is well-tested enough, we can remove `git-legacy-rebase.sh`. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> --- builtin/rebase.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 286886717bfbab..b4881ebb222338 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -54,7 +54,7 @@ static int use_builtin_rebase(void) cp.git_cmd = 1; if (capture_command(&cp, &out, 6)) { strbuf_release(&out); - return 0; + return 1; } strbuf_trim(&out); From 35ecbdcc60246367ae5bffe79f621ac01bbd8aab Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 2 Oct 2018 22:05:17 +0200 Subject: [PATCH 169/406] t0001: fix on case-insensitive filesystems On a case-insensitive filesystem, such as HFS+ or NTFS, it is possible that the idea Bash has of the current directory differs in case from what Git thinks it is. That's totally okay, though, and we should not expect otherwise. Reported by Jameson Miller. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t0001-init.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 182da069f1743b..76653d4a790159 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -307,10 +307,20 @@ test_expect_success 'init prefers command line to GIT_DIR' ' test_path_is_missing otherdir/refs ' +downcase_on_case_insensitive_fs () { + test false = "$(git config --get core.filemode)" || return 0 + for f + do + tr A-Z a-z <"$f" >"$f".downcased && + mv -f "$f".downcased "$f" || return 1 + done +} + test_expect_success 'init with separate gitdir' ' rm -rf newdir && git init --separate-git-dir realgitdir newdir && echo "gitdir: $(pwd)/realgitdir" >expected && + downcase_on_case_insensitive_fs expected newdir/.git && test_cmp expected newdir/.git && test_path_is_dir realgitdir/refs ' @@ -365,6 +375,7 @@ test_expect_success 're-init to update git link' ' git init --separate-git-dir ../surrealgitdir ) && echo "gitdir: $(pwd)/surrealgitdir" >expected && + downcase_on_case_insensitive_fs expected newdir/.git && test_cmp expected newdir/.git && test_path_is_dir surrealgitdir/refs && test_path_is_missing realgitdir/refs @@ -378,6 +389,7 @@ test_expect_success 're-init to move gitdir' ' git init --separate-git-dir ../realgitdir ) && echo "gitdir: $(pwd)/realgitdir" >expected && + downcase_on_case_insensitive_fs expected newdir/.git && test_cmp expected newdir/.git && test_path_is_dir realgitdir/refs ' From 147b98363bd5b5e60fb4530dca1389ca87e51ae7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 15 Nov 2018 11:52:51 +0100 Subject: [PATCH 170/406] mingw: replace an obsolete link with the superseding one The MSDN documentation has been superseded by Microsoft Docs (which is backed by a repository on GitHub containing many, many files in Markdown format). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..54758a3a5071be 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -897,8 +897,8 @@ char *mingw_getcwd(char *pointer, int len) } /* - * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx - * (Parsing C++ Command-Line Arguments) + * See "Parsing C++ Command-Line Arguments" at Microsoft's Docs: + * https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments */ static const char *quote_arg(const char *arg) { From 9364164a592ee95a4d8fa22cb42c4e8b320a73b6 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Sun, 6 May 2018 01:43:27 +0300 Subject: [PATCH 171/406] sha1-name.c: add `get_oidf()` which acts like `get_oid()` Compared to `get_oid()`, `get_oidf()` has as parameters a pointer to `object_id`, a printf format string and additional arguments. This will help simplify the code in subsequent commits. Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- cache.h | 1 + sha1-name.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/cache.h b/cache.h index 4d014541ab7bc7..0276fae1c20079 100644 --- a/cache.h +++ b/cache.h @@ -1319,6 +1319,7 @@ struct object_context { GET_OID_BLOB) extern int get_oid(const char *str, struct object_id *oid); +extern int get_oidf(struct object_id *oid, const char *fmt, ...); extern int get_oid_commit(const char *str, struct object_id *oid); extern int get_oid_committish(const char *str, struct object_id *oid); extern int get_oid_tree(const char *str, struct object_id *oid); diff --git a/sha1-name.c b/sha1-name.c index c9cc1318b7394e..261b960bbd2b24 100644 --- a/sha1-name.c +++ b/sha1-name.c @@ -1471,6 +1471,25 @@ int get_oid(const char *name, struct object_id *oid) return get_oid_with_context(name, 0, oid, &unused); } +/* + * This returns a non-zero value if the string (built using printf + * format and the given arguments) is not a valid object. + */ +int get_oidf(struct object_id *oid, const char *fmt, ...) +{ + va_list ap; + int ret; + struct strbuf sb = STRBUF_INIT; + + va_start(ap, fmt); + strbuf_vaddf(&sb, fmt, ap); + va_end(ap); + + ret = get_oid(sb.buf, oid); + strbuf_release(&sb); + + return ret; +} /* * Many callers know that the user meant to name a commit-ish by From 7e5c60ec250ea1ee837df76d1a82640602c76cfb Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Thu, 6 Sep 2018 16:07:39 +0300 Subject: [PATCH 172/406] strbuf.c: add `strbuf_join_argv()` Implement `strbuf_join_argv()` to join arguments into a strbuf. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- strbuf.c | 15 +++++++++++++++ strbuf.h | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/strbuf.c b/strbuf.c index 64041c3c249b15..3eb431b2b032cc 100644 --- a/strbuf.c +++ b/strbuf.c @@ -259,6 +259,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) strbuf_setlen(sb, sb->len + sb2->len); } +const char *strbuf_join_argv(struct strbuf *buf, + int argc, const char **argv, char delim) +{ + if (!argc) + return buf->buf; + + strbuf_addstr(buf, *argv); + while (--argc) { + strbuf_addch(buf, delim); + strbuf_addstr(buf, *(++argv)); + } + + return buf->buf; +} + void strbuf_addchars(struct strbuf *sb, int c, size_t n) { strbuf_grow(sb, n); diff --git a/strbuf.h b/strbuf.h index 60a35aef165ca1..fdc338e5ca50be 100644 --- a/strbuf.h +++ b/strbuf.h @@ -284,6 +284,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s) */ extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2); +/** + * Join the arguments into a buffer. `delim` is put between every + * two arguments. + */ +const char *strbuf_join_argv(struct strbuf *buf, int argc, + const char **argv, char delim); + /** * This function can be used to expand a format string containing * placeholders. To that end, it parses the string and calls the specified From 77b5e3f69b60908fab42593d11ff4b0f3ef06945 Mon Sep 17 00:00:00 2001 From: Joel Teichroeb <joel@teichroeb.net> Date: Wed, 4 Apr 2018 19:28:06 -0700 Subject: [PATCH 173/406] stash: improve option parsing test coverage In preparation for converting the stash command incrementally to a builtin command, this patch improves test coverage of the option parsing. Both for having too many parameters, or too few. Signed-off-by: Joel Teichroeb <joel@teichroeb.net> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- t/t3903-stash.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 6450bc669860f0..3eaca6505387d8 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -444,6 +444,36 @@ test_expect_failure 'stash file to directory' ' test foo = "$(cat file/file)" ' +test_expect_success 'giving too many ref arguments does not modify files' ' + git stash clear && + test_when_finished "git reset --hard HEAD" && + echo foo >file2 && + git stash && + echo bar >file2 && + git stash && + test-tool chmtime =123456789 file2 && + for type in apply pop "branch stash-branch" + do + test_must_fail git stash $type stash@{0} stash@{1} 2>err && + test_i18ngrep "Too many revisions" err && + test 123456789 = $(test-tool chmtime -g file2) || return 1 + done +' + +test_expect_success 'drop: too many arguments errors out (does nothing)' ' + git stash list >expect && + test_must_fail git stash drop stash@{0} stash@{1} 2>err && + test_i18ngrep "Too many revisions" err && + git stash list >actual && + test_cmp expect actual +' + +test_expect_success 'show: too many arguments errors out (does nothing)' ' + test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out && + test_i18ngrep "Too many revisions" err && + test_must_be_empty out +' + test_expect_success 'stash create - no changes' ' git stash clear && test_when_finished "git reset --hard HEAD" && @@ -479,6 +509,11 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' ' test $(git ls-files --modified | wc -l) -eq 1 ' +test_expect_success 'stash branch complains with no arguments' ' + test_must_fail git stash branch 2>err && + test_i18ngrep "No branch name specified" err +' + test_expect_success 'stash show format defaults to --stat' ' git stash clear && test_when_finished "git reset --hard HEAD" && From 853c7d480358150f2cb7c611f4a550fcc001784a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 15 Nov 2018 20:22:44 +0100 Subject: [PATCH 174/406] t3903: modernize style Remove whitespaces after redirection operators and wrap long lines. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- t/t3903-stash.sh | 128 ++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 3eaca6505387d8..098a387a828fe6 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -8,22 +8,22 @@ test_description='Test git stash' . ./test-lib.sh test_expect_success 'stash some dirty working directory' ' - echo 1 > file && + echo 1 >file && git add file && echo unrelated >other-file && git add other-file && test_tick && git commit -m initial && - echo 2 > file && + echo 2 >file && git add file && - echo 3 > file && + echo 3 >file && test_tick && git stash && git diff-files --quiet && git diff-index --cached --quiet HEAD ' -cat > expect << EOF +cat >expect <<EOF diff --git a/file b/file index 0cfbf08..00750ed 100644 --- a/file @@ -35,8 +35,8 @@ EOF test_expect_success 'parents of stash' ' test $(git rev-parse stash^) = $(git rev-parse HEAD) && - git diff stash^2..stash > output && - test_cmp output expect + git diff stash^2..stash >output && + test_cmp expect output ' test_expect_success 'applying bogus stash does nothing' ' @@ -74,7 +74,7 @@ test_expect_success 'apply stashed changes' ' test_expect_success 'apply stashed changes (including index)' ' git reset --hard HEAD^ && - echo 6 > other-file && + echo 6 >other-file && git add other-file && test_tick && git commit -m other-file && @@ -99,12 +99,12 @@ test_expect_success 'stash drop complains of extra options' ' test_expect_success 'drop top stash' ' git reset --hard && - git stash list > stashlist1 && - echo 7 > file && + git stash list >expected && + echo 7 >file && git stash && git stash drop && - git stash list > stashlist2 && - test_cmp stashlist1 stashlist2 && + git stash list >actual && + test_cmp expected actual && git stash apply && test 3 = $(cat file) && test 1 = $(git show :file) && @@ -113,9 +113,9 @@ test_expect_success 'drop top stash' ' test_expect_success 'drop middle stash' ' git reset --hard && - echo 8 > file && + echo 8 >file && git stash && - echo 9 > file && + echo 9 >file && git stash && git stash drop stash@{1} && test 2 = $(git stash list | wc -l) && @@ -160,7 +160,7 @@ test_expect_success 'stash pop' ' test 0 = $(git stash list | wc -l) ' -cat > expect << EOF +cat >expect <<EOF diff --git a/file2 b/file2 new file mode 100644 index 0000000..1fe912c @@ -170,7 +170,7 @@ index 0000000..1fe912c +bar2 EOF -cat > expect1 << EOF +cat >expect1 <<EOF diff --git a/file b/file index 257cc56..5716ca5 100644 --- a/file @@ -180,7 +180,7 @@ index 257cc56..5716ca5 100644 +bar EOF -cat > expect2 << EOF +cat >expect2 <<EOF diff --git a/file b/file index 7601807..5716ca5 100644 --- a/file @@ -198,79 +198,79 @@ index 0000000..1fe912c EOF test_expect_success 'stash branch' ' - echo foo > file && + echo foo >file && git commit file -m first && - echo bar > file && - echo bar2 > file2 && + echo bar >file && + echo bar2 >file2 && git add file2 && git stash && - echo baz > file && + echo baz >file && git commit file -m second && git stash branch stashbranch && test refs/heads/stashbranch = $(git symbolic-ref HEAD) && test $(git rev-parse HEAD) = $(git rev-parse master^) && - git diff --cached > output && - test_cmp output expect && - git diff > output && - test_cmp output expect1 && + git diff --cached >output && + test_cmp expect output && + git diff >output && + test_cmp expect1 output && git add file && git commit -m alternate\ second && - git diff master..stashbranch > output && + git diff master..stashbranch >output && test_cmp output expect2 && test 0 = $(git stash list | wc -l) ' test_expect_success 'apply -q is quiet' ' - echo foo > file && + echo foo >file && git stash && - git stash apply -q > output.out 2>&1 && + git stash apply -q >output.out 2>&1 && test_must_be_empty output.out ' test_expect_success 'save -q is quiet' ' - git stash save --quiet > output.out 2>&1 && + git stash save --quiet >output.out 2>&1 && test_must_be_empty output.out ' test_expect_success 'pop -q is quiet' ' - git stash pop -q > output.out 2>&1 && + git stash pop -q >output.out 2>&1 && test_must_be_empty output.out ' test_expect_success 'pop -q --index works and is quiet' ' - echo foo > file && + echo foo >file && git add file && git stash save --quiet && - git stash pop -q --index > output.out 2>&1 && + git stash pop -q --index >output.out 2>&1 && test foo = "$(git show :file)" && test_must_be_empty output.out ' test_expect_success 'drop -q is quiet' ' git stash && - git stash drop -q > output.out 2>&1 && + git stash drop -q >output.out 2>&1 && test_must_be_empty output.out ' test_expect_success 'stash -k' ' - echo bar3 > file && - echo bar4 > file2 && + echo bar3 >file && + echo bar4 >file2 && git add file2 && git stash -k && test bar,bar4 = $(cat file),$(cat file2) ' test_expect_success 'stash --no-keep-index' ' - echo bar33 > file && - echo bar44 > file2 && + echo bar33 >file && + echo bar44 >file2 && git add file2 && git stash --no-keep-index && test bar,bar2 = $(cat file),$(cat file2) ' test_expect_success 'stash --invalid-option' ' - echo bar5 > file && - echo bar6 > file2 && + echo bar5 >file && + echo bar6 >file2 && git add file2 && test_must_fail git stash --invalid-option && test_must_fail git stash save --invalid-option && @@ -486,11 +486,12 @@ test_expect_success 'stash branch - no stashes on stack, stash-like argument' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && STASH_ID=$(git stash create) && git reset --hard && git stash branch stash-branch ${STASH_ID} && - test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" && + test_when_finished "git reset --hard HEAD && git checkout master && + git branch -D stash-branch" && test $(git ls-files --modified | wc -l) -eq 1 ' @@ -498,14 +499,15 @@ test_expect_success 'stash branch - stashes on stack, stash-like argument' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && git stash && test_when_finished "git stash drop" && - echo bar >> file && + echo bar >>file && STASH_ID=$(git stash create) && git reset --hard && git stash branch stash-branch ${STASH_ID} && - test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" && + test_when_finished "git reset --hard HEAD && git checkout master && + git branch -D stash-branch" && test $(git ls-files --modified | wc -l) -eq 1 ' @@ -518,10 +520,10 @@ test_expect_success 'stash show format defaults to --stat' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && git stash && test_when_finished "git stash drop" && - echo bar >> file && + echo bar >>file && STASH_ID=$(git stash create) && git reset --hard && cat >expected <<-EOF && @@ -536,10 +538,10 @@ test_expect_success 'stash show - stashes on stack, stash-like argument' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && git stash && test_when_finished "git stash drop" && - echo bar >> file && + echo bar >>file && STASH_ID=$(git stash create) && git reset --hard && echo "1 0 file" >expected && @@ -551,10 +553,10 @@ test_expect_success 'stash show -p - stashes on stack, stash-like argument' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && git stash && test_when_finished "git stash drop" && - echo bar >> file && + echo bar >>file && STASH_ID=$(git stash create) && git reset --hard && cat >expected <<-EOF && @@ -574,7 +576,7 @@ test_expect_success 'stash show - no stashes on stack, stash-like argument' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && STASH_ID=$(git stash create) && git reset --hard && echo "1 0 file" >expected && @@ -586,7 +588,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' ' git stash clear && test_when_finished "git reset --hard HEAD" && git reset --hard && - echo foo >> file && + echo foo >>file && STASH_ID=$(git stash create) && git reset --hard && cat >expected <<-EOF && @@ -606,9 +608,9 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r git stash clear && test_when_finished "git reset --hard HEAD && git stash clear" && git reset --hard && - echo foo > file && + echo foo >file && git stash && - echo bar > file && + echo bar >file && git stash && test_must_fail git stash drop $(git rev-parse stash@{0}) && git stash pop && @@ -620,9 +622,9 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re git stash clear && test_when_finished "git reset --hard HEAD && git stash clear" && git reset --hard && - echo foo > file && + echo foo >file && git stash && - echo bar > file && + echo bar >file && git stash && test_must_fail git stash pop $(git rev-parse stash@{0}) && git stash pop && @@ -632,8 +634,8 @@ test_expect_success 'stash pop - fail early if specified stash is not a stash re test_expect_success 'ref with non-existent reflog' ' git stash clear && - echo bar5 > file && - echo bar6 > file2 && + echo bar5 >file && + echo bar6 >file2 && git add file2 && git stash && test_must_fail git rev-parse --quiet --verify does-not-exist && @@ -653,8 +655,8 @@ test_expect_success 'ref with non-existent reflog' ' test_expect_success 'invalid ref of the form stash@{n}, n >= N' ' git stash clear && test_must_fail git stash drop stash@{0} && - echo bar5 > file && - echo bar6 > file2 && + echo bar5 >file && + echo bar6 >file2 && git add file2 && git stash && test_must_fail git stash drop stash@{1} && @@ -724,7 +726,7 @@ test_expect_success 'stash apply shows status same as git status (relative to cu test_i18ncmp expect actual ' -cat > expect << EOF +cat >expect <<EOF diff --git a/HEAD b/HEAD new file mode 100644 index 0000000..fe0cbee @@ -737,15 +739,15 @@ EOF test_expect_success 'stash where working directory contains "HEAD" file' ' git stash clear && git reset --hard && - echo file-not-a-ref > HEAD && + echo file-not-a-ref >HEAD && git add HEAD && test_tick && git stash && git diff-files --quiet && git diff-index --cached --quiet HEAD && test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" && - git diff stash^..stash > output && - test_cmp output expect + git diff stash^..stash >output && + test_cmp expect output ' test_expect_success 'store called with invalid commit' ' From 8d1e0e1b9d9fc9cd6383290cf57b1f1cee42f870 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Mon, 21 May 2018 22:30:36 +0300 Subject: [PATCH 175/406] stash: rename test cases to be more descriptive Rename some test cases' labels to be more descriptive and under 80 characters per line. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- t/t3903-stash.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 098a387a828fe6..8b09a3d6ccc5bb 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -604,7 +604,7 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' ' test_cmp expected actual ' -test_expect_success 'stash drop - fail early if specified stash is not a stash reference' ' +test_expect_success 'drop: fail early if specified stash is not a stash ref' ' git stash clear && test_when_finished "git reset --hard HEAD && git stash clear" && git reset --hard && @@ -618,7 +618,7 @@ test_expect_success 'stash drop - fail early if specified stash is not a stash r git reset --hard HEAD ' -test_expect_success 'stash pop - fail early if specified stash is not a stash reference' ' +test_expect_success 'pop: fail early if specified stash is not a stash ref' ' git stash clear && test_when_finished "git reset --hard HEAD && git stash clear" && git reset --hard && @@ -682,7 +682,7 @@ test_expect_success 'invalid ref of the form "n", n >= N' ' git stash drop ' -test_expect_success 'stash branch should not drop the stash if the branch exists' ' +test_expect_success 'branch: do not drop the stash if the branch exists' ' git stash clear && echo foo >file && git add file && @@ -693,7 +693,7 @@ test_expect_success 'stash branch should not drop the stash if the branch exists git rev-parse stash@{0} -- ' -test_expect_success 'stash branch should not drop the stash if the apply fails' ' +test_expect_success 'branch: should not drop the stash if the apply fails' ' git stash clear && git reset HEAD~1 --hard && echo foo >file && @@ -707,7 +707,7 @@ test_expect_success 'stash branch should not drop the stash if the apply fails' git rev-parse stash@{0} -- ' -test_expect_success 'stash apply shows status same as git status (relative to current directory)' ' +test_expect_success 'apply: show same status as git status (relative to ./)' ' git stash clear && echo 1 >subdir/subfile1 && echo 2 >subdir/subfile2 && @@ -1048,7 +1048,7 @@ test_expect_success 'stash push -p with pathspec shows no changes only once' ' test_i18ncmp expect actual ' -test_expect_success 'stash push with pathspec shows no changes when there are none' ' +test_expect_success 'push <pathspec>: show no changes when there are none' ' >foo && git add foo && git commit -m "tmp" && @@ -1058,7 +1058,7 @@ test_expect_success 'stash push with pathspec shows no changes when there are no test_i18ncmp expect actual ' -test_expect_success 'stash push with pathspec not in the repository errors out' ' +test_expect_success 'push: <pathspec> not in the repository errors out' ' >untracked && test_must_fail git stash push untracked && test_path_is_file untracked From 9cdf86b21ccb3a044fb1246c8c1d107574d71315 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Mon, 27 Aug 2018 00:36:39 +0300 Subject: [PATCH 176/406] stash: add tests for `git stash show` config This commit introduces tests for `git stash show` config. It tests all the cases where `stash.showStat` and `stash.showPatch` are unset or set to true / false. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- t/t3907-stash-show-config.sh | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 t/t3907-stash-show-config.sh diff --git a/t/t3907-stash-show-config.sh b/t/t3907-stash-show-config.sh new file mode 100755 index 00000000000000..10914bba7b3737 --- /dev/null +++ b/t/t3907-stash-show-config.sh @@ -0,0 +1,83 @@ +#!/bin/sh + +test_description='Test git stash show configuration.' + +. ./test-lib.sh + +test_expect_success 'setup' ' + test_commit file +' + +# takes three parameters: +# 1. the stash.showStat value (or "<unset>") +# 2. the stash.showPatch value (or "<unset>") +# 3. the diff options of the expected output (or nothing for no output) +test_stat_and_patch () { + if test "<unset>" = "$1" + then + test_unconfig stash.showStat + else + test_config stash.showStat "$1" + fi && + + if test "<unset>" = "$2" + then + test_unconfig stash.showPatch + else + test_config stash.showPatch "$2" + fi && + + shift 2 && + echo 2 >file.t && + if test $# != 0 + then + git diff "$@" >expect + fi && + git stash && + git stash show >actual && + + if test $# = 0 + then + test_must_be_empty actual + else + test_cmp expect actual + fi +} + +test_expect_success 'showStat unset showPatch unset' ' + test_stat_and_patch "<unset>" "<unset>" --stat +' + +test_expect_success 'showStat unset showPatch false' ' + test_stat_and_patch "<unset>" false --stat +' + +test_expect_success 'showStat unset showPatch true' ' + test_stat_and_patch "<unset>" true --stat -p +' + +test_expect_success 'showStat false showPatch unset' ' + test_stat_and_patch false "<unset>" +' + +test_expect_success 'showStat false showPatch false' ' + test_stat_and_patch false false +' + +test_expect_success 'showStat false showPatch true' ' + test_stat_and_patch false true -p +' + +test_expect_success 'showStat true showPatch unset' ' + test_stat_and_patch true "<unset>" --stat +' + +test_expect_success 'showStat true showPatch false' ' + test_stat_and_patch true false --stat +' + +test_expect_success 'showStat true showPatch true' ' + test_stat_and_patch true true --stat -p +' + +test_done From 0b77852088fe8fbad5f5b755157b39c4be77fea1 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Sun, 7 Oct 2018 18:10:17 +0300 Subject: [PATCH 177/406] stash: mention options in `show` synopsis Mention in the documentation, that `show` accepts any option known to `git diff`. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- Documentation/git-stash.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt index 7ef8c4791177b2..e31ea7d3037d55 100644 --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] 'git stash' list [<options>] -'git stash' show [<stash>] +'git stash' show [<options>] [<stash>] 'git stash' drop [-q|--quiet] [<stash>] 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>] 'git stash' branch <branchname> [<stash>] @@ -106,7 +106,7 @@ stash@{1}: On master: 9cc0589... Add git-stash The command takes options applicable to the 'git log' command to control what is shown and how. See linkgit:git-log[1]. -show [<stash>]:: +show [<options>] [<stash>]:: Show the changes recorded in the stash entry as a diff between the stashed contents and the commit back when the stash entry was first From bf0db564830c2ce6b855abb695736adb9b6b1664 Mon Sep 17 00:00:00 2001 From: Joel Teichroeb <joel@teichroeb.net> Date: Wed, 4 Apr 2018 19:28:07 -0700 Subject: [PATCH 178/406] stash: convert apply to builtin Add a builtin helper for performing stash commands. Converting all at once proved hard to review, so starting with just apply lets conversion get started without the other commands being finished. The helper is being implemented as a drop in replacement for stash so that when it is complete it can simply be renamed and the shell script deleted. Delete the contents of the apply_stash shell function and replace it with a call to stash--helper apply until pop is also converted. Signed-off-by: Joel Teichroeb <joel@teichroeb.net> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- .gitignore | 1 + Makefile | 1 + builtin.h | 1 + builtin/stash--helper.c | 452 ++++++++++++++++++++++++++++++++++++++++ git-stash.sh | 78 +------ git.c | 1 + 6 files changed, 463 insertions(+), 71 deletions(-) create mode 100644 builtin/stash--helper.c diff --git a/.gitignore b/.gitignore index ffceea7d59fd21..b59661cb884688 100644 --- a/.gitignore +++ b/.gitignore @@ -157,6 +157,7 @@ /git-show-ref /git-stage /git-stash +/git-stash--helper /git-status /git-stripspace /git-submodule diff --git a/Makefile b/Makefile index 5a969f5830a410..482196f4718f45 100644 --- a/Makefile +++ b/Makefile @@ -1093,6 +1093,7 @@ BUILTIN_OBJS += builtin/shortlog.o BUILTIN_OBJS += builtin/show-branch.o BUILTIN_OBJS += builtin/show-index.o BUILTIN_OBJS += builtin/show-ref.o +BUILTIN_OBJS += builtin/stash--helper.o BUILTIN_OBJS += builtin/stripspace.o BUILTIN_OBJS += builtin/submodule--helper.o BUILTIN_OBJS += builtin/symbolic-ref.o diff --git a/builtin.h b/builtin.h index 99206df4bd43fc..317bc338f729f2 100644 --- a/builtin.h +++ b/builtin.h @@ -223,6 +223,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix); extern int cmd_show_branch(int argc, const char **argv, const char *prefix); extern int cmd_show_index(int argc, const char **argv, const char *prefix); extern int cmd_status(int argc, const char **argv, const char *prefix); +extern int cmd_stash__helper(int argc, const char **argv, const char *prefix); extern int cmd_stripspace(int argc, const char **argv, const char *prefix); extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix); extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix); diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c new file mode 100644 index 00000000000000..198bcb65f0b83f --- /dev/null +++ b/builtin/stash--helper.c @@ -0,0 +1,452 @@ +#include "builtin.h" +#include "config.h" +#include "parse-options.h" +#include "refs.h" +#include "lockfile.h" +#include "cache-tree.h" +#include "unpack-trees.h" +#include "merge-recursive.h" +#include "argv-array.h" +#include "run-command.h" +#include "dir.h" +#include "rerere.h" + +static const char * const git_stash_helper_usage[] = { + N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), + NULL +}; + +static const char * const git_stash_helper_apply_usage[] = { + N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), + NULL +}; + +static const char *ref_stash = "refs/stash"; +static struct strbuf stash_index_path = STRBUF_INIT; + +/* + * w_commit is set to the commit containing the working tree + * b_commit is set to the base commit + * i_commit is set to the commit containing the index tree + * u_commit is set to the commit containing the untracked files tree + * w_tree is set to the working tree + * b_tree is set to the base tree + * i_tree is set to the index tree + * u_tree is set to the untracked files tree + */ + +struct stash_info { + struct object_id w_commit; + struct object_id b_commit; + struct object_id i_commit; + struct object_id u_commit; + struct object_id w_tree; + struct object_id b_tree; + struct object_id i_tree; + struct object_id u_tree; + struct strbuf revision; + int is_stash_ref; + int has_u; +}; + +static void free_stash_info(struct stash_info *info) +{ + strbuf_release(&info->revision); +} + +static void assert_stash_like(struct stash_info *info, const char *revision) +{ + if (get_oidf(&info->b_commit, "%s^1", revision) || + get_oidf(&info->w_tree, "%s:", revision) || + get_oidf(&info->b_tree, "%s^1:", revision) || + get_oidf(&info->i_tree, "%s^2:", revision)) + die(_("'%s' is not a stash-like commit"), revision); +} + +static int get_stash_info(struct stash_info *info, int argc, const char **argv) +{ + int ret; + char *end_of_rev; + char *expanded_ref; + const char *revision; + const char *commit = NULL; + struct object_id dummy; + struct strbuf symbolic = STRBUF_INIT; + + if (argc > 1) { + int i; + struct strbuf refs_msg = STRBUF_INIT; + + for (i = 0; i < argc; i++) + strbuf_addf(&refs_msg, " '%s'", argv[i]); + + fprintf_ln(stderr, _("Too many revisions specified:%s"), + refs_msg.buf); + strbuf_release(&refs_msg); + + return -1; + } + + if (argc == 1) + commit = argv[0]; + + strbuf_init(&info->revision, 0); + if (!commit) { + if (!ref_exists(ref_stash)) { + free_stash_info(info); + fprintf_ln(stderr, _("No stash entries found.")); + return -1; + } + + strbuf_addf(&info->revision, "%s@{0}", ref_stash); + } else if (strspn(commit, "0123456789") == strlen(commit)) { + strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit); + } else { + strbuf_addstr(&info->revision, commit); + } + + revision = info->revision.buf; + + if (get_oid(revision, &info->w_commit)) { + error(_("%s is not a valid reference"), revision); + free_stash_info(info); + return -1; + } + + assert_stash_like(info, revision); + + info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision); + + end_of_rev = strchrnul(revision, '@'); + strbuf_add(&symbolic, revision, end_of_rev - revision); + + ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref); + strbuf_release(&symbolic); + switch (ret) { + case 0: /* Not found, but valid ref */ + info->is_stash_ref = 0; + break; + case 1: + info->is_stash_ref = !strcmp(expanded_ref, ref_stash); + break; + default: /* Invalid or ambiguous */ + free_stash_info(info); + } + + free(expanded_ref); + return !(ret == 0 || ret == 1); +} + +static int reset_tree(struct object_id *i_tree, int update, int reset) +{ + int nr_trees = 1; + struct unpack_trees_options opts; + struct tree_desc t[MAX_UNPACK_TREES]; + struct tree *tree; + struct lock_file lock_file = LOCK_INIT; + + read_cache_preload(NULL); + if (refresh_cache(REFRESH_QUIET)) + return -1; + + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); + + memset(&opts, 0, sizeof(opts)); + + tree = parse_tree_indirect(i_tree); + if (parse_tree(tree)) + return -1; + + init_tree_desc(t, tree->buffer, tree->size); + + opts.head_idx = 1; + opts.src_index = &the_index; + opts.dst_index = &the_index; + opts.merge = 1; + opts.reset = reset; + opts.update = update; + opts.fn = oneway_merge; + + if (unpack_trees(nr_trees, t, &opts)) + return -1; + + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) + return error(_("unable to write new index file")); + + return 0; +} + +static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) +{ + struct child_process cp = CHILD_PROCESS_INIT; + const char *w_commit_hex = oid_to_hex(w_commit); + + /* + * Diff-tree would not be very hard to replace with a native function, + * however it should be done together with apply_cached. + */ + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL); + argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex); + + return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); +} + +static int apply_cached(struct strbuf *out) +{ + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * Apply currently only reads either from stdin or a file, thus + * apply_all_patches would have to be updated to optionally take a + * buffer. + */ + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "apply", "--cached", NULL); + return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); +} + +static int reset_head(void) +{ + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * Reset is overall quite simple, however there is no current public + * API for resetting. + */ + cp.git_cmd = 1; + argv_array_push(&cp.args, "reset"); + + return run_command(&cp); +} + +static int get_newly_staged(struct strbuf *out, struct object_id *c_tree) +{ + struct child_process cp = CHILD_PROCESS_INIT; + const char *c_tree_hex = oid_to_hex(c_tree); + + /* + * diff-index is very similar to diff-tree above, and should be + * converted together with update_index. + */ + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only", + "--diff-filter=A", NULL); + argv_array_push(&cp.args, c_tree_hex); + return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); +} + +static int update_index(struct strbuf *out) +{ + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * Update-index is very complicated and may need to have a public + * function exposed in order to remove this forking. + */ + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL); + return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); +} + +static int restore_untracked(struct object_id *u_tree) +{ + int res; + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * We need to run restore files from a given index, but without + * affecting the current index, so we use GIT_INDEX_FILE with + * run_command to fork processes that will not interfere. + */ + cp.git_cmd = 1; + argv_array_push(&cp.args, "read-tree"); + argv_array_push(&cp.args, oid_to_hex(u_tree)); + argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + if (run_command(&cp)) { + remove_path(stash_index_path.buf); + return -1; + } + + child_process_init(&cp); + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "checkout-index", "--all", NULL); + argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + + res = run_command(&cp); + remove_path(stash_index_path.buf); + return res; +} + +static int do_apply_stash(const char *prefix, struct stash_info *info, + int index, int quiet) +{ + int ret; + int has_index = index; + struct merge_options o; + struct object_id c_tree; + struct object_id index_tree; + struct commit *result; + const struct object_id *bases[1]; + + read_cache_preload(NULL); + if (refresh_cache(REFRESH_QUIET)) + return -1; + + if (write_cache_as_tree(&c_tree, 0, NULL) || reset_tree(&c_tree, 0, 0)) + return error(_("cannot apply a stash in the middle of a merge")); + + if (index) { + if (!oidcmp(&info->b_tree, &info->i_tree) || + !oidcmp(&c_tree, &info->i_tree)) { + has_index = 0; + } else { + struct strbuf out = STRBUF_INIT; + + if (diff_tree_binary(&out, &info->w_commit)) { + strbuf_release(&out); + return error(_("could not generate diff %s^!."), + oid_to_hex(&info->w_commit)); + } + + ret = apply_cached(&out); + strbuf_release(&out); + if (ret) + return error(_("conflicts in index." + "Try without --index.")); + + discard_cache(); + read_cache(); + if (write_cache_as_tree(&index_tree, 0, NULL)) + return error(_("could not save index tree")); + + reset_head(); + } + } + + if (info->has_u && restore_untracked(&info->u_tree)) + return error(_("could not restore untracked files from stash")); + + init_merge_options(&o); + + o.branch1 = "Updated upstream"; + o.branch2 = "Stashed changes"; + + if (!oidcmp(&info->b_tree, &c_tree)) + o.branch1 = "Version stash was based on"; + + if (quiet) + o.verbosity = 0; + + if (o.verbosity >= 3) + printf_ln(_("Merging %s with %s"), o.branch1, o.branch2); + + bases[0] = &info->b_tree; + + ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases, + &result); + if (ret) { + rerere(0); + + if (index) + fprintf_ln(stderr, _("Index was not unstashed.")); + + return ret; + } + + if (has_index) { + if (reset_tree(&index_tree, 0, 0)) + return -1; + } else { + struct strbuf out = STRBUF_INIT; + + if (get_newly_staged(&out, &c_tree)) { + strbuf_release(&out); + return -1; + } + + if (reset_tree(&c_tree, 0, 1)) { + strbuf_release(&out); + return -1; + } + + ret = update_index(&out); + strbuf_release(&out); + if (ret) + return -1; + + discard_cache(); + } + + if (quiet) { + if (refresh_cache(REFRESH_QUIET)) + warning("could not refresh index"); + } else { + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * Status is quite simple and could be replaced with calls to + * wt_status in the future, but it adds complexities which may + * require more tests. + */ + cp.git_cmd = 1; + cp.dir = prefix; + argv_array_push(&cp.args, "status"); + run_command(&cp); + } + + return 0; +} + +static int apply_stash(int argc, const char **argv, const char *prefix) +{ + int ret; + int quiet = 0; + int index = 0; + struct stash_info info; + struct option options[] = { + OPT__QUIET(&quiet, N_("be quiet, only report errors")), + OPT_BOOL(0, "index", &index, + N_("attempt to recreate the index")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_apply_usage, 0); + + if (get_stash_info(&info, argc, argv)) + return -1; + + ret = do_apply_stash(prefix, &info, index, quiet); + free_stash_info(&info); + return ret; +} + +int cmd_stash__helper(int argc, const char **argv, const char *prefix) +{ + pid_t pid = getpid(); + const char *index_file; + + struct option options[] = { + OPT_END() + }; + + git_config(git_default_config, NULL); + + argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage, + PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); + + index_file = get_index_file(); + strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, + (uintmax_t)pid); + + if (argc < 1) + usage_with_options(git_stash_helper_usage, options); + if (!strcmp(argv[0], "apply")) + return !!apply_stash(argc, argv, prefix); + + usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), + git_stash_helper_usage, options); +} diff --git a/git-stash.sh b/git-stash.sh index 94793c1a913abf..809b1c2d1d3ac7 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -566,76 +566,11 @@ assert_stash_ref() { } apply_stash () { - - assert_stash_like "$@" - - git update-index -q --refresh || die "$(gettext "unable to refresh index")" - - # current index state - c_tree=$(git write-tree) || - die "$(gettext "Cannot apply a stash in the middle of a merge")" - - unstashed_index_tree= - if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" && - test "$c_tree" != "$i_tree" - then - git diff-tree --binary $s^2^..$s^2 | git apply --cached - test $? -ne 0 && - die "$(gettext "Conflicts in index. Try without --index.")" - unstashed_index_tree=$(git write-tree) || - die "$(gettext "Could not save index tree")" - git reset - fi - - if test -n "$u_tree" - then - GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" && - GIT_INDEX_FILE="$TMPindex" git checkout-index --all && - rm -f "$TMPindex" || - die "$(gettext "Could not restore untracked files from stash entry")" - fi - - eval " - GITHEAD_$w_tree='Stashed changes' && - GITHEAD_$c_tree='Updated upstream' && - GITHEAD_$b_tree='Version stash was based on' && - export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree - " - - if test -n "$GIT_QUIET" - then - GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY - fi - if git merge-recursive $b_tree -- $c_tree $w_tree - then - # No conflict - if test -n "$unstashed_index_tree" - then - git read-tree "$unstashed_index_tree" - else - a="$TMP-added" && - git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" && - git read-tree --reset $c_tree && - git update-index --add --stdin <"$a" || - die "$(gettext "Cannot unstage modified files")" - rm -f "$a" - fi - squelch= - if test -n "$GIT_QUIET" - then - squelch='>/dev/null 2>&1' - fi - (cd "$START_DIR" && eval "git status $squelch") || : - else - # Merge conflict; keep the exit status from merge-recursive - status=$? - git rerere - if test -n "$INDEX_OPTION" - then - gettextln "Index was not unstashed." >&2 - fi - exit $status - fi + cd "$START_DIR" + git stash--helper apply "$@" + res=$? + cd_to_toplevel + return $res } pop_stash() { @@ -713,7 +648,8 @@ push) ;; apply) shift - apply_stash "$@" + cd "$START_DIR" + git stash--helper apply "$@" ;; clear) shift diff --git a/git.c b/git.c index c27c38738b2a9d..3c0e762d7d2011 100644 --- a/git.c +++ b/git.c @@ -544,6 +544,7 @@ static struct cmd_struct commands[] = { { "show-index", cmd_show_index }, { "show-ref", cmd_show_ref, RUN_SETUP }, { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, + { "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE }, { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, { "stripspace", cmd_stripspace }, { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT }, From 8c141eb66ad6f761c00caddf60890e2b1f9fa22d Mon Sep 17 00:00:00 2001 From: Joel Teichroeb <joel@teichroeb.net> Date: Wed, 4 Apr 2018 19:28:08 -0700 Subject: [PATCH 179/406] stash: convert drop and clear to builtin Add the drop and clear commands to the builtin helper. These two are each simple, but are being added together as they are quite related. We have to unfortunately keep the drop and clear functions in the shell script as functions are called with parameters internally that are not valid when the commands are called externally. Once pop is converted they can both be removed. Signed-off-by: Joel Teichroeb <joel@teichroeb.net> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 117 ++++++++++++++++++++++++++++++++++++++++ git-stash.sh | 4 +- 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 198bcb65f0b83f..5bd9e1c0741179 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -12,7 +12,14 @@ #include "rerere.h" static const char * const git_stash_helper_usage[] = { + N_("git stash--helper drop [-q|--quiet] [<stash>]"), N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), + N_("git stash--helper clear"), + NULL +}; + +static const char * const git_stash_helper_drop_usage[] = { + N_("git stash--helper drop [-q|--quiet] [<stash>]"), NULL }; @@ -21,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = { NULL }; +static const char * const git_stash_helper_clear_usage[] = { + N_("git stash--helper clear"), + NULL +}; + static const char *ref_stash = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; @@ -137,6 +149,32 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv) return !(ret == 0 || ret == 1); } +static int do_clear_stash(void) +{ + struct object_id obj; + if (get_oid(ref_stash, &obj)) + return 0; + + return delete_ref(NULL, ref_stash, &obj, 0); +} + +static int clear_stash(int argc, const char **argv, const char *prefix) +{ + struct option options[] = { + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_clear_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + if (argc) + return error(_("git stash clear with parameters is " + "unimplemented")); + + return do_clear_stash(); +} + static int reset_tree(struct object_id *i_tree, int update, int reset) { int nr_trees = 1; @@ -424,6 +462,81 @@ static int apply_stash(int argc, const char **argv, const char *prefix) return ret; } +static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet) +{ + int ret; + struct child_process cp_reflog = CHILD_PROCESS_INIT; + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * reflog does not provide a simple function for deleting refs. One will + * need to be added to avoid implementing too much reflog code here + */ + + cp_reflog.git_cmd = 1; + argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref", + "--rewrite", NULL); + argv_array_push(&cp_reflog.args, info->revision.buf); + ret = run_command(&cp_reflog); + if (!ret) { + if (!quiet) + printf_ln(_("Dropped %s (%s)"), info->revision.buf, + oid_to_hex(&info->w_commit)); + } else { + return error(_("%s: Could not drop stash entry"), + info->revision.buf); + } + + /* + * This could easily be replaced by get_oid, but currently it will throw + * a fatal error when a reflog is empty, which we can not recover from. + */ + cp.git_cmd = 1; + /* Even though --quiet is specified, rev-parse still outputs the hash */ + cp.no_stdout = 1; + argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL); + argv_array_pushf(&cp.args, "%s@{0}", ref_stash); + ret = run_command(&cp); + + /* do_clear_stash if we just dropped the last stash entry */ + if (ret) + do_clear_stash(); + + return 0; +} + +static void assert_stash_ref(struct stash_info *info) +{ + if (!info->is_stash_ref) { + free_stash_info(info); + error(_("'%s' is not a stash reference"), info->revision.buf); + exit(128); + } +} + +static int drop_stash(int argc, const char **argv, const char *prefix) +{ + int ret; + int quiet = 0; + struct stash_info info; + struct option options[] = { + OPT__QUIET(&quiet, N_("be quiet, only report errors")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_drop_usage, 0); + + if (get_stash_info(&info, argc, argv)) + return -1; + + assert_stash_ref(&info); + + ret = do_drop_stash(prefix, &info, quiet); + free_stash_info(&info); + return ret; +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -446,6 +559,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_stash_helper_usage, options); if (!strcmp(argv[0], "apply")) return !!apply_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "clear")) + return !!clear_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "drop")) + return !!drop_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index 809b1c2d1d3ac7..a99d5dc9e5d655 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -653,7 +653,7 @@ apply) ;; clear) shift - clear_stash "$@" + git stash--helper clear "$@" ;; create) shift @@ -665,7 +665,7 @@ store) ;; drop) shift - drop_stash "$@" + git stash--helper drop "$@" ;; pop) shift From c8624e3eae6bc374f16e1b974c67626bd596d36c Mon Sep 17 00:00:00 2001 From: Joel Teichroeb <joel@teichroeb.net> Date: Wed, 4 Apr 2018 19:28:09 -0700 Subject: [PATCH 180/406] stash: convert branch to builtin Add stash branch to the helper and delete the apply_to_branch function from the shell script. Checkout does not currently provide a function for checking out a branch as cmd_checkout does a large amount of sanity checks first that we require here. Signed-off-by: Joel Teichroeb <joel@teichroeb.net> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 46 +++++++++++++++++++++++++++++++++++++++++ git-stash.sh | 17 ++------------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 5bd9e1c0741179..a8badaf557f5b4 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -14,6 +14,7 @@ static const char * const git_stash_helper_usage[] = { N_("git stash--helper drop [-q|--quiet] [<stash>]"), N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), + N_("git stash--helper branch <branchname> [<stash>]"), N_("git stash--helper clear"), NULL }; @@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = { NULL }; +static const char * const git_stash_helper_branch_usage[] = { + N_("git stash--helper branch <branchname> [<stash>]"), + NULL +}; + static const char * const git_stash_helper_clear_usage[] = { N_("git stash--helper clear"), NULL @@ -537,6 +543,44 @@ static int drop_stash(int argc, const char **argv, const char *prefix) return ret; } +static int branch_stash(int argc, const char **argv, const char *prefix) +{ + int ret; + const char *branch = NULL; + struct stash_info info; + struct child_process cp = CHILD_PROCESS_INIT; + struct option options[] = { + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_branch_usage, 0); + + if (!argc) { + fprintf_ln(stderr, _("No branch name specified")); + return -1; + } + + branch = argv[0]; + + if (get_stash_info(&info, argc - 1, argv + 1)) + return -1; + + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "checkout", "-b", NULL); + argv_array_push(&cp.args, branch); + argv_array_push(&cp.args, oid_to_hex(&info.b_commit)); + ret = run_command(&cp); + if (!ret) + ret = do_apply_stash(prefix, &info, 1, 0); + if (!ret && info.is_stash_ref) + ret = do_drop_stash(prefix, &info, 0); + + free_stash_info(&info); + + return ret; +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -563,6 +607,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!clear_stash(argc, argv, prefix); else if (!strcmp(argv[0], "drop")) return !!drop_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "branch")) + return !!branch_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index a99d5dc9e5d655..29d9f442559c5e 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -598,20 +598,6 @@ drop_stash () { clear_stash } -apply_to_branch () { - test -n "$1" || die "$(gettext "No branch name specified")" - branch=$1 - shift 1 - - set -- --index "$@" - assert_stash_like "$@" - - git checkout -b $branch $REV^ && - apply_stash "$@" && { - test -z "$IS_STASH_REF" || drop_stash "$@" - } -} - test "$1" = "-p" && set "push" "$@" PARSE_CACHE='--not-parsed' @@ -673,7 +659,8 @@ pop) ;; branch) shift - apply_to_branch "$@" + cd "$START_DIR" + git stash--helper branch "$@" ;; *) case $# in From 47e8dc4fe4c2fc2390ad6e75a3530f20befe3480 Mon Sep 17 00:00:00 2001 From: Joel Teichroeb <joel@teichroeb.net> Date: Wed, 4 Apr 2018 19:28:10 -0700 Subject: [PATCH 181/406] stash: convert pop to builtin Add stash pop to the helper and delete the pop_stash, drop_stash, assert_stash_ref functions from the shell script now that they are no longer needed. Signed-off-by: Joel Teichroeb <joel@teichroeb.net> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 39 +++++++++++++++++++++++++++++++++- git-stash.sh | 47 ++--------------------------------------- 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index a8badaf557f5b4..b269b3064aa059 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -13,7 +13,7 @@ static const char * const git_stash_helper_usage[] = { N_("git stash--helper drop [-q|--quiet] [<stash>]"), - N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), + N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), N_("git stash--helper branch <branchname> [<stash>]"), N_("git stash--helper clear"), NULL @@ -24,6 +24,11 @@ static const char * const git_stash_helper_drop_usage[] = { NULL }; +static const char * const git_stash_helper_pop_usage[] = { + N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"), + NULL +}; + static const char * const git_stash_helper_apply_usage[] = { N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), NULL @@ -543,6 +548,36 @@ static int drop_stash(int argc, const char **argv, const char *prefix) return ret; } +static int pop_stash(int argc, const char **argv, const char *prefix) +{ + int ret; + int index = 0; + int quiet = 0; + struct stash_info info; + struct option options[] = { + OPT__QUIET(&quiet, N_("be quiet, only report errors")), + OPT_BOOL(0, "index", &index, + N_("attempt to recreate the index")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_pop_usage, 0); + + if (get_stash_info(&info, argc, argv)) + return -1; + + assert_stash_ref(&info); + if ((ret = do_apply_stash(prefix, &info, index, quiet))) + printf_ln(_("The stash entry is kept in case " + "you need it again.")); + else + ret = do_drop_stash(prefix, &info, quiet); + + free_stash_info(&info); + return ret; +} + static int branch_stash(int argc, const char **argv, const char *prefix) { int ret; @@ -607,6 +642,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!clear_stash(argc, argv, prefix); else if (!strcmp(argv[0], "drop")) return !!drop_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "pop")) + return !!pop_stash(argc, argv, prefix); else if (!strcmp(argv[0], "branch")) return !!branch_stash(argc, argv, prefix); diff --git a/git-stash.sh b/git-stash.sh index 29d9f442559c5e..8f2640fe902b21 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -554,50 +554,6 @@ assert_stash_like() { } } -is_stash_ref() { - is_stash_like "$@" && test -n "$IS_STASH_REF" -} - -assert_stash_ref() { - is_stash_ref "$@" || { - args="$*" - die "$(eval_gettext "'\$args' is not a stash reference")" - } -} - -apply_stash () { - cd "$START_DIR" - git stash--helper apply "$@" - res=$? - cd_to_toplevel - return $res -} - -pop_stash() { - assert_stash_ref "$@" - - if apply_stash "$@" - then - drop_stash "$@" - else - status=$? - say "$(gettext "The stash entry is kept in case you need it again.")" - exit $status - fi -} - -drop_stash () { - assert_stash_ref "$@" - - git reflog delete --updateref --rewrite "${REV}" && - say "$(eval_gettext "Dropped \${REV} (\$s)")" || - die "$(eval_gettext "\${REV}: Could not drop stash entry")" - - # clear_stash if we just dropped the last stash entry - git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null || - clear_stash -} - test "$1" = "-p" && set "push" "$@" PARSE_CACHE='--not-parsed' @@ -655,7 +611,8 @@ drop) ;; pop) shift - pop_stash "$@" + cd "$START_DIR" + git stash--helper pop "$@" ;; branch) shift From 4ef8676efc565f48f4c61d9c5d9638045f096840 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Sat, 19 May 2018 12:56:58 +0300 Subject: [PATCH 182/406] stash: convert list to builtin Add stash list to the helper and delete the list_stash function from the shell script. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 31 +++++++++++++++++++++++++++++++ git-stash.sh | 7 +------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index b269b3064aa059..9a0b4c30e3e380 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -12,6 +12,7 @@ #include "rerere.h" static const char * const git_stash_helper_usage[] = { + N_("git stash--helper list [<options>]"), N_("git stash--helper drop [-q|--quiet] [<stash>]"), N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), N_("git stash--helper branch <branchname> [<stash>]"), @@ -19,6 +20,11 @@ static const char * const git_stash_helper_usage[] = { NULL }; +static const char * const git_stash_helper_list_usage[] = { + N_("git stash--helper list [<options>]"), + NULL +}; + static const char * const git_stash_helper_drop_usage[] = { N_("git stash--helper drop [-q|--quiet] [<stash>]"), NULL @@ -616,6 +622,29 @@ static int branch_stash(int argc, const char **argv, const char *prefix) return ret; } +static int list_stash(int argc, const char **argv, const char *prefix) +{ + struct child_process cp = CHILD_PROCESS_INIT; + struct option options[] = { + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_list_usage, + PARSE_OPT_KEEP_UNKNOWN); + + if (!ref_exists(ref_stash)) + return 0; + + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g", + "--first-parent", "-m", NULL); + argv_array_pushv(&cp.args, argv); + argv_array_push(&cp.args, ref_stash); + argv_array_push(&cp.args, "--"); + return run_command(&cp); +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -646,6 +675,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!pop_stash(argc, argv, prefix); else if (!strcmp(argv[0], "branch")) return !!branch_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "list")) + return !!list_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index 8f2640fe902b21..6052441aa2f790 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -382,11 +382,6 @@ have_stash () { git rev-parse --verify --quiet $ref_stash >/dev/null } -list_stash () { - have_stash || return 0 - git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash -- -} - show_stash () { ALLOW_UNKNOWN_FLAGS=t assert_stash_like "$@" @@ -574,7 +569,7 @@ test -n "$seen_non_option" || set "push" "$@" case "$1" in list) shift - list_stash "$@" + git stash--helper list "$@" ;; show) shift From f2f91dc2fbbe35ecb057fd463d1c5ace2df562a3 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Sun, 20 May 2018 01:44:12 +0300 Subject: [PATCH 183/406] stash: convert show to builtin Add stash show to the helper and delete the show_stash, have_stash, assert_stash_like, is_stash_like and parse_flags_and_rev functions from the shell script now that they are no longer needed. In shell version, although `git stash show` accepts `--index` and `--quiet` options, it ignores them. In C, both options are passed further to `git diff`. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 87 ++++++++++++++++++++++++++ git-stash.sh | 132 +--------------------------------------- 2 files changed, 88 insertions(+), 131 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 9a0b4c30e3e380..9aa5812ebc99f8 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -10,9 +10,12 @@ #include "run-command.h" #include "dir.h" #include "rerere.h" +#include "revision.h" +#include "log-tree.h" static const char * const git_stash_helper_usage[] = { N_("git stash--helper list [<options>]"), + N_("git stash--helper show [<options>] [<stash>]"), N_("git stash--helper drop [-q|--quiet] [<stash>]"), N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), N_("git stash--helper branch <branchname> [<stash>]"), @@ -25,6 +28,11 @@ static const char * const git_stash_helper_list_usage[] = { NULL }; +static const char * const git_stash_helper_show_usage[] = { + N_("git stash--helper show [<options>] [<stash>]"), + NULL +}; + static const char * const git_stash_helper_drop_usage[] = { N_("git stash--helper drop [-q|--quiet] [<stash>]"), NULL @@ -645,6 +653,83 @@ static int list_stash(int argc, const char **argv, const char *prefix) return run_command(&cp); } +static int show_stat = 1; +static int show_patch; + +static int git_stash_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "stash.showstat")) { + show_stat = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "stash.showpatch")) { + show_patch = git_config_bool(var, value); + return 0; + } + return git_default_config(var, value, cb); +} + +static int show_stash(int argc, const char **argv, const char *prefix) +{ + int i; + int opts = 0; + int ret = 0; + struct stash_info info; + struct rev_info rev; + struct argv_array stash_args = ARGV_ARRAY_INIT; + struct option options[] = { + OPT_END() + }; + + init_diff_ui_defaults(); + git_config(git_diff_ui_config, NULL); + init_revisions(&rev, prefix); + + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + argv_array_push(&stash_args, argv[i]); + else + opts++; + } + + ret = get_stash_info(&info, stash_args.argc, stash_args.argv); + argv_array_clear(&stash_args); + if (ret) + return -1; + + /* + * The config settings are applied only if there are not passed + * any options. + */ + if (!opts) { + git_config(git_stash_config, NULL); + if (show_stat) + rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT; + + if (show_patch) + rev.diffopt.output_format |= DIFF_FORMAT_PATCH; + + if (!show_stat && !show_patch) { + free_stash_info(&info); + return 0; + } + } + + argc = setup_revisions(argc, argv, &rev, NULL); + if (argc > 1) { + free_stash_info(&info); + usage_with_options(git_stash_helper_show_usage, options); + } + + rev.diffopt.flags.recursive = 1; + setup_diff_pager(&rev.diffopt); + diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt); + log_tree_diff_flush(&rev); + + free_stash_info(&info); + return diff_result_code(&rev.diffopt, 0); +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -677,6 +762,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!branch_stash(argc, argv, prefix); else if (!strcmp(argv[0], "list")) return !!list_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "show")) + return !!show_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index 6052441aa2f790..0d05cbc1e53f34 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -378,35 +378,6 @@ save_stash () { fi } -have_stash () { - git rev-parse --verify --quiet $ref_stash >/dev/null -} - -show_stash () { - ALLOW_UNKNOWN_FLAGS=t - assert_stash_like "$@" - - if test -z "$FLAGS" - then - if test "$(git config --bool stash.showStat || echo true)" = "true" - then - FLAGS=--stat - fi - - if test "$(git config --bool stash.showPatch || echo false)" = "true" - then - FLAGS=${FLAGS}${FLAGS:+ }-p - fi - - if test -z "$FLAGS" - then - return 0 - fi - fi - - git diff ${FLAGS} $b_commit $w_commit -} - show_help () { exec git help stash exit 1 @@ -448,107 +419,6 @@ show_help () { # * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t" # -parse_flags_and_rev() -{ - test "$PARSE_CACHE" = "$*" && return 0 # optimisation - PARSE_CACHE="$*" - - IS_STASH_LIKE= - IS_STASH_REF= - INDEX_OPTION= - s= - w_commit= - b_commit= - i_commit= - u_commit= - w_tree= - b_tree= - i_tree= - u_tree= - - FLAGS= - REV= - for opt - do - case "$opt" in - -q|--quiet) - GIT_QUIET=-t - ;; - --index) - INDEX_OPTION=--index - ;; - --help) - show_help - ;; - -*) - test "$ALLOW_UNKNOWN_FLAGS" = t || - die "$(eval_gettext "unknown option: \$opt")" - FLAGS="${FLAGS}${FLAGS:+ }$opt" - ;; - *) - REV="${REV}${REV:+ }'$opt'" - ;; - esac - done - - eval set -- $REV - - case $# in - 0) - have_stash || die "$(gettext "No stash entries found.")" - set -- ${ref_stash}@{0} - ;; - 1) - : - ;; - *) - die "$(eval_gettext "Too many revisions specified: \$REV")" - ;; - esac - - case "$1" in - *[!0-9]*) - : - ;; - *) - set -- "${ref_stash}@{$1}" - ;; - esac - - REV=$(git rev-parse --symbolic --verify --quiet "$1") || { - reference="$1" - die "$(eval_gettext "\$reference is not a valid reference")" - } - - i_commit=$(git rev-parse --verify --quiet "$REV^2") && - set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) && - s=$1 && - w_commit=$1 && - b_commit=$2 && - w_tree=$3 && - b_tree=$4 && - i_tree=$5 && - IS_STASH_LIKE=t && - test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" && - IS_STASH_REF=t - - u_commit=$(git rev-parse --verify --quiet "$REV^3") && - u_tree=$(git rev-parse "$REV^3:" 2>/dev/null) -} - -is_stash_like() -{ - parse_flags_and_rev "$@" - test -n "$IS_STASH_LIKE" -} - -assert_stash_like() { - is_stash_like "$@" || { - args="$*" - die "$(eval_gettext "'\$args' is not a stash-like commit")" - } -} - test "$1" = "-p" && set "push" "$@" PARSE_CACHE='--not-parsed' @@ -573,7 +443,7 @@ list) ;; show) shift - show_stash "$@" + git stash--helper show "$@" ;; save) shift From e0482708fecc8c4ab5168c223f070621fc3980a6 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Sun, 20 May 2018 17:26:25 +0300 Subject: [PATCH 184/406] stash: convert store to builtin Add stash store to the helper and delete the store_stash function from the shell script. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 62 +++++++++++++++++++++++++++++++++++++++++ git-stash.sh | 43 ++-------------------------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 9aa5812ebc99f8..66f8f42a3bab72 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -58,6 +58,11 @@ static const char * const git_stash_helper_clear_usage[] = { NULL }; +static const char * const git_stash_helper_store_usage[] = { + N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"), + NULL +}; + static const char *ref_stash = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; @@ -730,6 +735,61 @@ static int show_stash(int argc, const char **argv, const char *prefix) return diff_result_code(&rev.diffopt, 0); } +static int do_store_stash(const struct object_id *w_commit, const char *stash_msg, + int quiet) +{ + if (!stash_msg) + stash_msg = "Created via \"git stash store\"."; + + if (update_ref(stash_msg, ref_stash, w_commit, NULL, + REF_FORCE_CREATE_REFLOG, + quiet ? UPDATE_REFS_QUIET_ON_ERR : + UPDATE_REFS_MSG_ON_ERR)) { + if (!quiet) { + fprintf_ln(stderr, _("Cannot update %s with %s"), + ref_stash, oid_to_hex(w_commit)); + } + return -1; + } + + return 0; +} + +static int store_stash(int argc, const char **argv, const char *prefix) +{ + int quiet = 0; + const char *stash_msg = NULL; + struct object_id obj; + struct object_context dummy; + struct option options[] = { + OPT__QUIET(&quiet, N_("be quiet")), + OPT_STRING('m', "message", &stash_msg, "message", + N_("stash message")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_store_usage, + PARSE_OPT_KEEP_UNKNOWN); + + if (argc != 1) { + if (!quiet) + fprintf_ln(stderr, _("\"git stash store\" requires one " + "<commit> argument")); + return -1; + } + + if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj, + &dummy)) { + if (!quiet) + fprintf_ln(stderr, _("Cannot update %s with %s"), + ref_stash, argv[0]); + return -1; + } + + return do_store_stash(&obj, stash_msg, quiet); +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -764,6 +824,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!list_stash(argc, argv, prefix); else if (!strcmp(argv[0], "show")) return !!show_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "store")) + return !!store_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index 0d05cbc1e53f34..5739c515275cff 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -191,45 +191,6 @@ create_stash () { die "$(gettext "Cannot record working tree state")" } -store_stash () { - while test $# != 0 - do - case "$1" in - -m|--message) - shift - stash_msg="$1" - ;; - -m*) - stash_msg=${1#-m} - ;; - --message=*) - stash_msg=${1#--message=} - ;; - -q|--quiet) - quiet=t - ;; - *) - break - ;; - esac - shift - done - test $# = 1 || - die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")" - - w_commit="$1" - if test -z "$stash_msg" - then - stash_msg="Created via \"git stash store\"." - fi - - git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit - ret=$? - test $ret != 0 && test -z "$quiet" && - die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")" - return $ret -} - push_stash () { keep_index= patch_mode= @@ -308,7 +269,7 @@ push_stash () { clear_stash || die "$(gettext "Cannot initialize stash")" create_stash -m "$stash_msg" -u "$untracked" -- "$@" - store_stash -m "$stash_msg" -q $w_commit || + git stash--helper store -m "$stash_msg" -q $w_commit || die "$(gettext "Cannot save the current status")" say "$(eval_gettext "Saved working directory and index state \$stash_msg")" @@ -468,7 +429,7 @@ create) ;; store) shift - store_stash "$@" + git stash--helper store "$@" ;; drop) shift From 710ea911b92f1ca8268d8e0d1e7cd71ec13190b9 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Wed, 27 Jun 2018 18:20:43 +0300 Subject: [PATCH 185/406] stash: convert create to builtin Add stash create to the helper. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 457 +++++++++++++++++++++++++++++++++++++++- git-stash.sh | 2 +- 2 files changed, 457 insertions(+), 2 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 66f8f42a3bab72..92b90970ba3ae3 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -12,6 +12,9 @@ #include "rerere.h" #include "revision.h" #include "log-tree.h" +#include "diffcore.h" + +#define INCLUDE_ALL_FILES 2 static const char * const git_stash_helper_usage[] = { N_("git stash--helper list [<options>]"), @@ -63,6 +66,11 @@ static const char * const git_stash_helper_store_usage[] = { NULL }; +static const char * const git_stash_helper_create_usage[] = { + N_("git stash--helper create [<message>]"), + NULL +}; + static const char *ref_stash = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; @@ -288,6 +296,24 @@ static int reset_head(void) return run_command(&cp); } +static void add_diff_to_buf(struct diff_queue_struct *q, + struct diff_options *options, + void *data) +{ + int i; + + for (i = 0; i < q->nr; i++) { + strbuf_addstr(data, q->queue[i]->one->path); + + /* + * The reason we add "0" at the end of this strbuf + * is because we will pass the output further to + * "git update-index -z ...". + */ + strbuf_addch(data, '\0'); + } +} + static int get_newly_staged(struct strbuf *out, struct object_id *c_tree) { struct child_process cp = CHILD_PROCESS_INIT; @@ -790,6 +816,433 @@ static int store_stash(int argc, const char **argv, const char *prefix) return do_store_stash(&obj, stash_msg, quiet); } +static void add_pathspecs(struct argv_array *args, + struct pathspec ps) { + int i; + + for (i = 0; i < ps.nr; i++) + argv_array_push(args, ps.items[i].match); +} + +/* + * `untracked_files` will be filled with the names of untracked files. + * The return value is: + * + * = 0 if there are not any untracked files + * > 0 if there are untracked files + */ +static int get_untracked_files(struct pathspec ps, int include_untracked, + struct strbuf *untracked_files) +{ + int i; + int max_len; + int found = 0; + char *seen; + struct dir_struct dir; + + memset(&dir, 0, sizeof(dir)); + if (include_untracked != INCLUDE_ALL_FILES) + setup_standard_excludes(&dir); + + seen = xcalloc(ps.nr, 1); + + max_len = fill_directory(&dir, the_repository->index, &ps); + for (i = 0; i < dir.nr; i++) { + struct dir_entry *ent = dir.entries[i]; + if (dir_path_match(&the_index, ent, &ps, max_len, seen)) { + found++; + strbuf_addstr(untracked_files, ent->name); + /* NUL-terminate: will be fed to update-index -z */ + strbuf_addch(untracked_files, 0); + } + free(ent); + } + + free(seen); + free(dir.entries); + free(dir.ignored); + clear_directory(&dir); + return found; +} + +/* + * The return value of `check_changes()` can be: + * + * < 0 if there was an error + * = 0 if there are no changes. + * > 0 if there are changes. + */ +static int check_changes(struct pathspec ps, int include_untracked) +{ + int result; + struct rev_info rev; + struct object_id dummy; + struct strbuf out = STRBUF_INIT; + + /* No initial commit. */ + if (get_oid("HEAD", &dummy)) + return -1; + + if (read_cache() < 0) + return -1; + + init_revisions(&rev, NULL); + rev.prune_data = ps; + + rev.diffopt.flags.quick = 1; + rev.diffopt.flags.ignore_submodules = 1; + rev.abbrev = 0; + + add_head_to_pending(&rev); + diff_setup_done(&rev.diffopt); + + result = run_diff_index(&rev, 1); + if (diff_result_code(&rev.diffopt, result)) + return 1; + + object_array_clear(&rev.pending); + result = run_diff_files(&rev, 0); + if (diff_result_code(&rev.diffopt, result)) + return 1; + + if (include_untracked && get_untracked_files(ps, include_untracked, + &out)) { + strbuf_release(&out); + return 1; + } + + strbuf_release(&out); + return 0; +} + +static int save_untracked_files(struct stash_info *info, struct strbuf *msg, + struct strbuf files) +{ + int ret = 0; + struct strbuf untracked_msg = STRBUF_INIT; + struct strbuf out = STRBUF_INIT; + struct child_process cp_upd_index = CHILD_PROCESS_INIT; + struct child_process cp_write_tree = CHILD_PROCESS_INIT; + + cp_upd_index.git_cmd = 1; + argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add", + "--remove", "--stdin", NULL); + argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + + strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf); + if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0, + NULL, 0)) { + ret = -1; + goto done; + } + + cp_write_tree.git_cmd = 1; + argv_array_push(&cp_write_tree.args, "write-tree"); + argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) { + ret = -1; + goto done; + } + get_oid_hex(out.buf, &info->u_tree); + + if (commit_tree(untracked_msg.buf, untracked_msg.len, + &info->u_tree, NULL, &info->u_commit, NULL, NULL)) { + ret = -1; + goto done; + } + +done: + strbuf_release(&untracked_msg); + strbuf_release(&out); + remove_path(stash_index_path.buf); + return ret; +} + +static int stash_patch(struct stash_info *info, struct pathspec ps, + struct strbuf *out_patch) +{ + int ret = 0; + struct strbuf out = STRBUF_INIT; + struct child_process cp_read_tree = CHILD_PROCESS_INIT; + struct child_process cp_add_i = CHILD_PROCESS_INIT; + struct child_process cp_write_tree = CHILD_PROCESS_INIT; + struct child_process cp_diff_tree = CHILD_PROCESS_INIT; + + remove_path(stash_index_path.buf); + + cp_read_tree.git_cmd = 1; + argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL); + argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + if (run_command(&cp_read_tree)) { + ret = -1; + goto done; + } + + /* Find out what the user wants. */ + cp_add_i.git_cmd = 1; + argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash", + "--", NULL); + add_pathspecs(&cp_add_i.args, ps); + argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + if (run_command(&cp_add_i)) { + ret = -1; + goto done; + } + + /* State of the working tree. */ + cp_write_tree.git_cmd = 1; + argv_array_push(&cp_write_tree.args, "write-tree"); + argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) { + ret = -1; + goto done; + } + + get_oid_hex(out.buf, &info->w_tree); + + cp_diff_tree.git_cmd = 1; + argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD", + oid_to_hex(&info->w_tree), "--", NULL); + if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) { + ret = -1; + goto done; + } + + if (!out_patch->len) { + fprintf_ln(stderr, _("No changes selected")); + ret = 1; + } + +done: + strbuf_release(&out); + remove_path(stash_index_path.buf); + return ret; +} + +static int stash_working_tree(struct stash_info *info, struct pathspec ps) +{ + int ret = 0; + struct rev_info rev; + struct child_process cp_upd_index = CHILD_PROCESS_INIT; + struct child_process cp_write_tree = CHILD_PROCESS_INIT; + struct strbuf out = STRBUF_INIT; + struct strbuf diff_output = STRBUF_INIT; + + set_alternate_index_output(stash_index_path.buf); + if (reset_tree(&info->i_tree, 0, 0)) { + ret = -1; + goto done; + } + set_alternate_index_output(NULL); + + init_revisions(&rev, NULL); + rev.prune_data = ps; + rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; + rev.diffopt.format_callback = add_diff_to_buf; + rev.diffopt.format_callback_data = &diff_output; + + if (read_cache_preload(&rev.diffopt.pathspec) < 0) { + ret = -1; + goto done; + } + + add_pending_object(&rev, parse_object(the_repository, &info->b_commit), + ""); + if (run_diff_index(&rev, 0)) { + ret = -1; + goto done; + } + + cp_upd_index.git_cmd = 1; + argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add", + "--remove", "--stdin", NULL); + argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + + if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len, + NULL, 0, NULL, 0)) { + ret = -1; + goto done; + } + + cp_write_tree.git_cmd = 1; + argv_array_push(&cp_write_tree.args, "write-tree"); + argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s", + stash_index_path.buf); + if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) { + ret = -1; + goto done; + } + + get_oid_hex(out.buf, &info->w_tree); + +done: + UNLEAK(rev); + strbuf_release(&out); + object_array_clear(&rev.pending); + strbuf_release(&diff_output); + remove_path(stash_index_path.buf); + return ret; +} + +static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, + int include_untracked, int patch_mode, + struct stash_info *info) +{ + int ret = 0; + int flags = 0; + int untracked_commit_option = 0; + const char *head_short_sha1 = NULL; + const char *branch_ref = NULL; + const char *branch_name = "(no branch)"; + struct commit *head_commit = NULL; + struct commit_list *parents = NULL; + struct strbuf msg = STRBUF_INIT; + struct strbuf commit_tree_label = STRBUF_INIT; + struct strbuf untracked_files = STRBUF_INIT; + struct strbuf patch = STRBUF_INIT; + + read_cache_preload(NULL); + refresh_cache(REFRESH_QUIET); + + if (get_oid("HEAD", &info->b_commit)) { + fprintf_ln(stderr, _("You do not have the initial commit yet")); + ret = -1; + goto done; + } else { + head_commit = lookup_commit(the_repository, &info->b_commit); + } + + if (!check_changes(ps, include_untracked)) { + ret = 1; + goto done; + } + + branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags); + if (flags & REF_ISSYMREF) + branch_name = strrchr(branch_ref, '/') + 1; + head_short_sha1 = find_unique_abbrev(&head_commit->object.oid, + DEFAULT_ABBREV); + strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1); + pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg); + + strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf); + commit_list_insert(head_commit, &parents); + if (write_cache_as_tree(&info->i_tree, 0, NULL) || + commit_tree(commit_tree_label.buf, commit_tree_label.len, + &info->i_tree, parents, &info->i_commit, NULL, NULL)) { + fprintf_ln(stderr, _("Cannot save the current index state")); + ret = -1; + goto done; + } + + if (include_untracked && get_untracked_files(ps, include_untracked, + &untracked_files)) { + if (save_untracked_files(info, &msg, untracked_files)) { + fprintf_ln(stderr, _("Cannot save " + "the untracked files")); + ret = -1; + goto done; + } + untracked_commit_option = 1; + } + if (patch_mode) { + ret = stash_patch(info, ps, &patch); + if (ret < 0) { + fprintf_ln(stderr, _("Cannot save the current " + "worktree state")); + goto done; + } else if (ret > 0) { + goto done; + } + } else { + if (stash_working_tree(info, ps)) { + fprintf_ln(stderr, _("Cannot save the current " + "worktree state")); + ret = -1; + goto done; + } + } + + if (!stash_msg_buf->len) { + strbuf_release(stash_msg_buf); + strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf); + } else { + struct strbuf temp_buf = STRBUF_INIT; + strbuf_addf(&temp_buf, "On %s: %s", branch_name, + stash_msg_buf->buf); + strbuf_release(stash_msg_buf); + *stash_msg_buf = temp_buf; + } + + /* + * `parents` will be empty after calling `commit_tree()`, so there is + * no need to call `free_commit_list()` + */ + parents = NULL; + if (untracked_commit_option) + commit_list_insert(lookup_commit(the_repository, + &info->u_commit), + &parents); + commit_list_insert(lookup_commit(the_repository, &info->i_commit), + &parents); + commit_list_insert(head_commit, &parents); + + if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree, + parents, &info->w_commit, NULL, NULL)) { + fprintf_ln(stderr, _("Cannot record working tree state")); + ret = -1; + goto done; + } + +done: + strbuf_release(&commit_tree_label); + strbuf_release(&msg); + strbuf_release(&untracked_files); + return ret; +} + +static int create_stash(int argc, const char **argv, const char *prefix) +{ + int include_untracked = 0; + int ret = 0; + const char *stash_msg = NULL; + struct strbuf stash_msg_buf = STRBUF_INIT; + struct stash_info info; + struct pathspec ps; + struct option options[] = { + OPT_BOOL('u', "include-untracked", &include_untracked, + N_("include untracked files in stash")), + OPT_STRING('m', "message", &stash_msg, N_("message"), + N_("stash message")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_create_usage, + 0); + + memset(&ps, 0, sizeof(ps)); + strbuf_addstr(&stash_msg_buf, stash_msg); + ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info); + + if (!ret) + printf_ln("%s", oid_to_hex(&info.w_commit)); + + strbuf_release(&stash_msg_buf); + + /* + * ret can be 1 if there were no changes. In this case, we should + * not error out. + */ + return ret < 0; +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -799,7 +1252,7 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) OPT_END() }; - git_config(git_default_config, NULL); + git_config(git_diff_basic_config, NULL); argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage, PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); @@ -826,6 +1279,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!show_stash(argc, argv, prefix); else if (!strcmp(argv[0], "store")) return !!store_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "create")) + return !!create_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index 5739c515275cff..ab06e4ffb8a62c 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -425,7 +425,7 @@ clear) ;; create) shift - create_stash -m "$*" && echo "$w_commit" + git stash--helper create --message "$*" ;; store) shift From fc2de0c39c92f8518d12f87fe6d8dc39be697325 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Thu, 26 Jul 2018 16:30:24 +0300 Subject: [PATCH 186/406] stash: convert push to builtin Add stash push to the helper. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 245 +++++++++++++++++++++++++++++++++++++++- git-stash.sh | 6 +- 2 files changed, 245 insertions(+), 6 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 92b90970ba3ae3..036b10e05280ba 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = { N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), N_("git stash--helper branch <branchname> [<stash>]"), N_("git stash--helper clear"), + N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" + " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" + " [--] [<pathspec>...]]"), NULL }; @@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = { NULL }; +static const char * const git_stash_helper_push_usage[] = { + N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" + " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" + " [--] [<pathspec>...]]"), + NULL +}; + static const char *ref_stash = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; @@ -1092,7 +1102,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps) static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, int include_untracked, int patch_mode, - struct stash_info *info) + struct stash_info *info, struct strbuf *patch) { int ret = 0; int flags = 0; @@ -1105,7 +1115,6 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, struct strbuf msg = STRBUF_INIT; struct strbuf commit_tree_label = STRBUF_INIT; struct strbuf untracked_files = STRBUF_INIT; - struct strbuf patch = STRBUF_INIT; read_cache_preload(NULL); refresh_cache(REFRESH_QUIET); @@ -1152,7 +1161,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, untracked_commit_option = 1; } if (patch_mode) { - ret = stash_patch(info, ps, &patch); + ret = stash_patch(info, ps, patch); if (ret < 0) { fprintf_ln(stderr, _("Cannot save the current " "worktree state")); @@ -1229,7 +1238,8 @@ static int create_stash(int argc, const char **argv, const char *prefix) memset(&ps, 0, sizeof(ps)); strbuf_addstr(&stash_msg_buf, stash_msg); - ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info); + ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info, + NULL); if (!ret) printf_ln("%s", oid_to_hex(&info.w_commit)); @@ -1243,6 +1253,231 @@ static int create_stash(int argc, const char **argv, const char *prefix) return ret < 0; } +static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, + int keep_index, int patch_mode, int include_untracked) +{ + int ret = 0; + struct stash_info info; + struct strbuf patch = STRBUF_INIT; + struct strbuf stash_msg_buf = STRBUF_INIT; + + if (patch_mode && keep_index == -1) + keep_index = 1; + + if (patch_mode && include_untracked) { + fprintf_ln(stderr, _("Can't use --patch and --include-untracked" + " or --all at the same time")); + ret = -1; + goto done; + } + + read_cache_preload(NULL); + if (!include_untracked && ps.nr) { + int i; + char *ps_matched = xcalloc(ps.nr, 1); + + for (i = 0; i < active_nr; i++) + ce_path_match(&the_index, active_cache[i], &ps, + ps_matched); + + if (report_path_error(ps_matched, &ps, NULL)) { + fprintf_ln(stderr, _("Did you forget to 'git add'?")); + ret = -1; + free(ps_matched); + goto done; + } + free(ps_matched); + } + + if (refresh_cache(REFRESH_QUIET)) { + ret = -1; + goto done; + } + + if (!check_changes(ps, include_untracked)) { + if (!quiet) + printf_ln(_("No local changes to save")); + goto done; + } + + if (!reflog_exists(ref_stash) && do_clear_stash()) { + ret = -1; + fprintf_ln(stderr, _("Cannot initialize stash")); + goto done; + } + + if (stash_msg) + strbuf_addstr(&stash_msg_buf, stash_msg); + if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, + &info, &patch)) { + ret = -1; + goto done; + } + + if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) { + ret = -1; + fprintf_ln(stderr, _("Cannot save the current status")); + goto done; + } + + printf_ln(_("Saved working directory and index state %s"), + stash_msg_buf.buf); + + if (!patch_mode) { + if (include_untracked && !ps.nr) { + struct child_process cp = CHILD_PROCESS_INIT; + + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "clean", "--force", + "--quiet", "-d", NULL); + if (include_untracked == INCLUDE_ALL_FILES) + argv_array_push(&cp.args, "-x"); + if (run_command(&cp)) { + ret = -1; + goto done; + } + } + if (ps.nr) { + struct child_process cp_add = CHILD_PROCESS_INIT; + struct child_process cp_diff = CHILD_PROCESS_INIT; + struct child_process cp_apply = CHILD_PROCESS_INIT; + struct strbuf out = STRBUF_INIT; + + cp_add.git_cmd = 1; + argv_array_push(&cp_add.args, "add"); + if (!include_untracked) + argv_array_push(&cp_add.args, "-u"); + if (include_untracked == INCLUDE_ALL_FILES) + argv_array_push(&cp_add.args, "--force"); + argv_array_push(&cp_add.args, "--"); + add_pathspecs(&cp_add.args, ps); + if (run_command(&cp_add)) { + ret = -1; + goto done; + } + + cp_diff.git_cmd = 1; + argv_array_pushl(&cp_diff.args, "diff-index", "-p", + "--cached", "--binary", "HEAD", "--", + NULL); + add_pathspecs(&cp_diff.args, ps); + if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) { + ret = -1; + goto done; + } + + cp_apply.git_cmd = 1; + argv_array_pushl(&cp_apply.args, "apply", "--index", + "-R", NULL); + if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0, + NULL, 0)) { + ret = -1; + goto done; + } + } else { + struct child_process cp = CHILD_PROCESS_INIT; + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "reset", "--hard", "-q", + NULL); + if (run_command(&cp)) { + ret = -1; + goto done; + } + } + + if (keep_index == 1 && !is_null_oid(&info.i_tree)) { + struct child_process cp_ls = CHILD_PROCESS_INIT; + struct child_process cp_checkout = CHILD_PROCESS_INIT; + struct strbuf out = STRBUF_INIT; + + if (reset_tree(&info.i_tree, 0, 1)) { + ret = -1; + goto done; + } + + cp_ls.git_cmd = 1; + argv_array_pushl(&cp_ls.args, "ls-files", "-z", + "--modified", "--", NULL); + + add_pathspecs(&cp_ls.args, ps); + if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) { + ret = -1; + goto done; + } + + cp_checkout.git_cmd = 1; + argv_array_pushl(&cp_checkout.args, "checkout-index", + "-z", "--force", "--stdin", NULL); + if (pipe_command(&cp_checkout, out.buf, out.len, NULL, + 0, NULL, 0)) { + ret = -1; + goto done; + } + } + goto done; + } else { + struct child_process cp = CHILD_PROCESS_INIT; + + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "apply", "-R", NULL); + + if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) { + fprintf_ln(stderr, _("Cannot remove worktree changes")); + ret = -1; + goto done; + } + + if (keep_index < 1) { + struct child_process cp = CHILD_PROCESS_INIT; + + cp.git_cmd = 1; + argv_array_pushl(&cp.args, "reset", "-q", "--", NULL); + add_pathspecs(&cp.args, ps); + if (run_command(&cp)) { + ret = -1; + goto done; + } + } + goto done; + } + +done: + strbuf_release(&stash_msg_buf); + return ret; +} + +static int push_stash(int argc, const char **argv, const char *prefix) +{ + int keep_index = -1; + int patch_mode = 0; + int include_untracked = 0; + int quiet = 0; + const char *stash_msg = NULL; + struct pathspec ps; + struct option options[] = { + OPT_BOOL('k', "keep-index", &keep_index, + N_("keep index")), + OPT_BOOL('p', "patch", &patch_mode, + N_("stash in patch mode")), + OPT__QUIET(&quiet, N_("quiet mode")), + OPT_BOOL('u', "include-untracked", &include_untracked, + N_("include untracked files in stash")), + OPT_SET_INT('a', "all", &include_untracked, + N_("include ignore files"), 2), + OPT_STRING('m', "message", &stash_msg, N_("message"), + N_("stash message")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_push_usage, + 0); + + parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv); + return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode, + include_untracked); +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -1281,6 +1516,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!store_stash(argc, argv, prefix); else if (!strcmp(argv[0], "create")) return !!create_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "push")) + return !!push_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index ab06e4ffb8a62c..c3146f62ab57d0 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -412,7 +412,8 @@ save) ;; push) shift - push_stash "$@" + cd "$START_DIR" + git stash--helper push "$@" ;; apply) shift @@ -448,7 +449,8 @@ branch) *) case $# in 0) - push_stash && + cd "$START_DIR" + git stash--helper push && say "$(gettext "(To restore them type \"git stash apply\")")" ;; *) From a1c36f00573287eb39ea0a3dc48d3661216e7cd5 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Thu, 26 Jul 2018 18:09:35 +0300 Subject: [PATCH 187/406] stash: make push -q quiet There is a change in behaviour with this commit. When there was no initial commit, the shell version of stash would still display a message. This commit makes `push` to not display any message if `--quiet` or `-q` is specified. Add tests for `--quiet`. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 56 ++++++++++++++++++++++++++--------------- t/t3903-stash.sh | 23 +++++++++++++++++ 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 036b10e05280ba..8dc1cc2ed18f10 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -971,7 +971,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg, } static int stash_patch(struct stash_info *info, struct pathspec ps, - struct strbuf *out_patch) + struct strbuf *out_patch, int quiet) { int ret = 0; struct strbuf out = STRBUF_INIT; @@ -1024,7 +1024,8 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, } if (!out_patch->len) { - fprintf_ln(stderr, _("No changes selected")); + if (!quiet) + fprintf_ln(stderr, _("No changes selected")); ret = 1; } @@ -1102,7 +1103,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps) static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, int include_untracked, int patch_mode, - struct stash_info *info, struct strbuf *patch) + struct stash_info *info, struct strbuf *patch, + int quiet) { int ret = 0; int flags = 0; @@ -1120,7 +1122,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, refresh_cache(REFRESH_QUIET); if (get_oid("HEAD", &info->b_commit)) { - fprintf_ln(stderr, _("You do not have the initial commit yet")); + if (!quiet) + fprintf_ln(stderr, _("You do not have " + "the initial commit yet")); ret = -1; goto done; } else { @@ -1145,7 +1149,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, if (write_cache_as_tree(&info->i_tree, 0, NULL) || commit_tree(commit_tree_label.buf, commit_tree_label.len, &info->i_tree, parents, &info->i_commit, NULL, NULL)) { - fprintf_ln(stderr, _("Cannot save the current index state")); + if (!quiet) + fprintf_ln(stderr, _("Cannot save the current " + "index state")); ret = -1; goto done; } @@ -1153,26 +1159,29 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, if (include_untracked && get_untracked_files(ps, include_untracked, &untracked_files)) { if (save_untracked_files(info, &msg, untracked_files)) { - fprintf_ln(stderr, _("Cannot save " - "the untracked files")); + if (!quiet) + fprintf_ln(stderr, _("Cannot save " + "the untracked files")); ret = -1; goto done; } untracked_commit_option = 1; } if (patch_mode) { - ret = stash_patch(info, ps, patch); + ret = stash_patch(info, ps, patch, quiet); if (ret < 0) { - fprintf_ln(stderr, _("Cannot save the current " - "worktree state")); + if (!quiet) + fprintf_ln(stderr, _("Cannot save the current " + "worktree state")); goto done; } else if (ret > 0) { goto done; } } else { if (stash_working_tree(info, ps)) { - fprintf_ln(stderr, _("Cannot save the current " - "worktree state")); + if (!quiet) + fprintf_ln(stderr, _("Cannot save the current " + "worktree state")); ret = -1; goto done; } @@ -1204,7 +1213,9 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree, parents, &info->w_commit, NULL, NULL)) { - fprintf_ln(stderr, _("Cannot record working tree state")); + if (!quiet) + fprintf_ln(stderr, _("Cannot record " + "working tree state")); ret = -1; goto done; } @@ -1239,7 +1250,7 @@ static int create_stash(int argc, const char **argv, const char *prefix) memset(&ps, 0, sizeof(ps)); strbuf_addstr(&stash_msg_buf, stash_msg); ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info, - NULL); + NULL, 0); if (!ret) printf_ln("%s", oid_to_hex(&info.w_commit)); @@ -1302,26 +1313,29 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, if (!reflog_exists(ref_stash) && do_clear_stash()) { ret = -1; - fprintf_ln(stderr, _("Cannot initialize stash")); + if (!quiet) + fprintf_ln(stderr, _("Cannot initialize stash")); goto done; } if (stash_msg) strbuf_addstr(&stash_msg_buf, stash_msg); if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, - &info, &patch)) { + &info, &patch, quiet)) { ret = -1; goto done; } if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) { ret = -1; - fprintf_ln(stderr, _("Cannot save the current status")); + if (!quiet) + fprintf_ln(stderr, _("Cannot save the current status")); goto done; } - printf_ln(_("Saved working directory and index state %s"), - stash_msg_buf.buf); + if (!quiet) + printf_ln(_("Saved working directory and index state %s"), + stash_msg_buf.buf); if (!patch_mode) { if (include_untracked && !ps.nr) { @@ -1422,7 +1436,9 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, argv_array_pushl(&cp.args, "apply", "-R", NULL); if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) { - fprintf_ln(stderr, _("Cannot remove worktree changes")); + if (!quiet) + fprintf_ln(stderr, _("Cannot remove " + "worktree changes")); ret = -1; goto done; } diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 8b09a3d6ccc5bb..77e0b72035e94b 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1064,6 +1064,29 @@ test_expect_success 'push: <pathspec> not in the repository errors out' ' test_path_is_file untracked ' +test_expect_success 'push: -q is quiet with changes' ' + >foo && + git add foo && + git stash push -q >output 2>&1 && + test_must_be_empty output +' + +test_expect_success 'push: -q is quiet with no changes' ' + git stash push -q >output 2>&1 && + test_must_be_empty output +' + +test_expect_success 'push: -q is quiet even if there is no initial commit' ' + git init foo_dir && + test_when_finished rm -rf foo_dir && + ( + cd foo_dir && + >bar && + test_must_fail git stash push -q >output 2>&1 && + test_must_be_empty output + ) +' + test_expect_success 'untracked files are left in place when -u is not given' ' >file && git add file && From 7caa1d2bc6bbf3cdcf8c72f3897fb8f4fa1c5f81 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Mon, 30 Jul 2018 18:56:51 +0300 Subject: [PATCH 188/406] stash: convert save to builtin Add stash save to the helper and delete functions which are no longer needed (`show_help()`, `save_stash()`, `push_stash()`, `create_stash()`, `clear_stash()`, `untracked_files()` and `no_changes()`). Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash--helper.c | 50 +++++++ git-stash.sh | 311 +--------------------------------------- 2 files changed, 52 insertions(+), 309 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 8dc1cc2ed18f10..e6f27fc1fa0311 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -26,6 +26,8 @@ static const char * const git_stash_helper_usage[] = { N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" " [--] [<pathspec>...]]"), + N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" + " [-u|--include-untracked] [-a|--all] [<message>]"), NULL }; @@ -81,6 +83,12 @@ static const char * const git_stash_helper_push_usage[] = { NULL }; +static const char * const git_stash_helper_save_usage[] = { + N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" + " [-u|--include-untracked] [-a|--all] [<message>]"), + NULL +}; + static const char *ref_stash = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; @@ -1494,6 +1502,46 @@ static int push_stash(int argc, const char **argv, const char *prefix) include_untracked); } +static int save_stash(int argc, const char **argv, const char *prefix) +{ + int keep_index = -1; + int patch_mode = 0; + int include_untracked = 0; + int quiet = 0; + int ret = 0; + const char *stash_msg = NULL; + struct pathspec ps; + struct strbuf stash_msg_buf = STRBUF_INIT; + struct option options[] = { + OPT_BOOL('k', "keep-index", &keep_index, + N_("keep index")), + OPT_BOOL('p', "patch", &patch_mode, + N_("stash in patch mode")), + OPT__QUIET(&quiet, N_("quiet mode")), + OPT_BOOL('u', "include-untracked", &include_untracked, + N_("include untracked files in stash")), + OPT_SET_INT('a', "all", &include_untracked, + N_("include ignore files"), 2), + OPT_STRING('m', "message", &stash_msg, "message", + N_("stash message")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_save_usage, + PARSE_OPT_KEEP_DASHDASH); + + if (argc) + stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' '); + + memset(&ps, 0, sizeof(ps)); + ret = do_push_stash(ps, stash_msg, quiet, keep_index, + patch_mode, include_untracked); + + strbuf_release(&stash_msg_buf); + return ret; +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -1534,6 +1582,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!create_stash(argc, argv, prefix); else if (!strcmp(argv[0], "push")) return !!push_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "save")) + return !!save_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index c3146f62ab57d0..695f1feba3dde8 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -36,314 +36,6 @@ else reset_color= fi -no_changes () { - git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" && - git diff-files --quiet --ignore-submodules -- "$@" && - (test -z "$untracked" || test -z "$(untracked_files "$@")") -} - -untracked_files () { - if test "$1" = "-z" - then - shift - z=-z - else - z= - fi - excl_opt=--exclude-standard - test "$untracked" = "all" && excl_opt= - git ls-files -o $z $excl_opt -- "$@" -} - -clear_stash () { - if test $# != 0 - then - die "$(gettext "git stash clear with parameters is unimplemented")" - fi - if current=$(git rev-parse --verify --quiet $ref_stash) - then - git update-ref -d $ref_stash $current - fi -} - -create_stash () { - stash_msg= - untracked= - while test $# != 0 - do - case "$1" in - -m|--message) - shift - stash_msg=${1?"BUG: create_stash () -m requires an argument"} - ;; - -m*) - stash_msg=${1#-m} - ;; - --message=*) - stash_msg=${1#--message=} - ;; - -u|--include-untracked) - shift - untracked=${1?"BUG: create_stash () -u requires an argument"} - ;; - --) - shift - break - ;; - esac - shift - done - - git update-index -q --refresh - if no_changes "$@" - then - exit 0 - fi - - # state of the base commit - if b_commit=$(git rev-parse --verify HEAD) - then - head=$(git rev-list --oneline -n 1 HEAD --) - else - die "$(gettext "You do not have the initial commit yet")" - fi - - if branch=$(git symbolic-ref -q HEAD) - then - branch=${branch#refs/heads/} - else - branch='(no branch)' - fi - msg=$(printf '%s: %s' "$branch" "$head") - - # state of the index - i_tree=$(git write-tree) && - i_commit=$(printf 'index on %s\n' "$msg" | - git commit-tree $i_tree -p $b_commit) || - die "$(gettext "Cannot save the current index state")" - - if test -n "$untracked" - then - # Untracked files are stored by themselves in a parentless commit, for - # ease of unpacking later. - u_commit=$( - untracked_files -z "$@" | ( - GIT_INDEX_FILE="$TMPindex" && - export GIT_INDEX_FILE && - rm -f "$TMPindex" && - git update-index -z --add --remove --stdin && - u_tree=$(git write-tree) && - printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree && - rm -f "$TMPindex" - ) ) || die "$(gettext "Cannot save the untracked files")" - - untracked_commit_option="-p $u_commit"; - else - untracked_commit_option= - fi - - if test -z "$patch_mode" - then - - # state of the working tree - w_tree=$( ( - git read-tree --index-output="$TMPindex" -m $i_tree && - GIT_INDEX_FILE="$TMPindex" && - export GIT_INDEX_FILE && - git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" && - git update-index -z --add --remove --stdin <"$TMP-stagenames" && - git write-tree && - rm -f "$TMPindex" - ) ) || - die "$(gettext "Cannot save the current worktree state")" - - else - - rm -f "$TMP-index" && - GIT_INDEX_FILE="$TMP-index" git read-tree HEAD && - - # find out what the user wants - GIT_INDEX_FILE="$TMP-index" \ - git add--interactive --patch=stash -- "$@" && - - # state of the working tree - w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) || - die "$(gettext "Cannot save the current worktree state")" - - git diff-tree -p HEAD $w_tree -- >"$TMP-patch" && - test -s "$TMP-patch" || - die "$(gettext "No changes selected")" - - rm -f "$TMP-index" || - die "$(gettext "Cannot remove temporary index (can't happen)")" - - fi - - # create the stash - if test -z "$stash_msg" - then - stash_msg=$(printf 'WIP on %s' "$msg") - else - stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg") - fi - w_commit=$(printf '%s\n' "$stash_msg" | - git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) || - die "$(gettext "Cannot record working tree state")" -} - -push_stash () { - keep_index= - patch_mode= - untracked= - stash_msg= - while test $# != 0 - do - case "$1" in - -k|--keep-index) - keep_index=t - ;; - --no-keep-index) - keep_index=n - ;; - -p|--patch) - patch_mode=t - # only default to keep if we don't already have an override - test -z "$keep_index" && keep_index=t - ;; - -q|--quiet) - GIT_QUIET=t - ;; - -u|--include-untracked) - untracked=untracked - ;; - -a|--all) - untracked=all - ;; - -m|--message) - shift - test -z ${1+x} && usage - stash_msg=$1 - ;; - -m*) - stash_msg=${1#-m} - ;; - --message=*) - stash_msg=${1#--message=} - ;; - --help) - show_help - ;; - --) - shift - break - ;; - -*) - option="$1" - eval_gettextln "error: unknown option for 'stash push': \$option" - usage - ;; - *) - break - ;; - esac - shift - done - - eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")" - - if test -n "$patch_mode" && test -n "$untracked" - then - die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")" - fi - - test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1 - - git update-index -q --refresh - if no_changes "$@" - then - say "$(gettext "No local changes to save")" - exit 0 - fi - - git reflog exists $ref_stash || - clear_stash || die "$(gettext "Cannot initialize stash")" - - create_stash -m "$stash_msg" -u "$untracked" -- "$@" - git stash--helper store -m "$stash_msg" -q $w_commit || - die "$(gettext "Cannot save the current status")" - say "$(eval_gettext "Saved working directory and index state \$stash_msg")" - - if test -z "$patch_mode" - then - test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION= - if test -n "$untracked" && test $# = 0 - then - git clean --force --quiet -d $CLEAN_X_OPTION - fi - - if test $# != 0 - then - test -z "$untracked" && UPDATE_OPTION="-u" || UPDATE_OPTION= - test "$untracked" = "all" && FORCE_OPTION="--force" || FORCE_OPTION= - git add $UPDATE_OPTION $FORCE_OPTION -- "$@" - git diff-index -p --cached --binary HEAD -- "$@" | - git apply --index -R - else - git reset --hard -q - fi - - if test "$keep_index" = "t" && test -n "$i_tree" - then - git read-tree --reset $i_tree - git ls-files -z --modified -- "$@" | - git checkout-index -z --force --stdin - fi - else - git apply -R < "$TMP-patch" || - die "$(gettext "Cannot remove worktree changes")" - - if test "$keep_index" != "t" - then - git reset -q -- "$@" - fi - fi -} - -save_stash () { - push_options= - while test $# != 0 - do - case "$1" in - --) - shift - break - ;; - -*) - # pass all options through to push_stash - push_options="$push_options $1" - ;; - *) - break - ;; - esac - shift - done - - stash_msg="$*" - - if test -z "$stash_msg" - then - push_stash $push_options - else - push_stash $push_options -m "$stash_msg" - fi -} - -show_help () { - exec git help stash - exit 1 -} - # # Parses the remaining options looking for flags and # at most one revision defaulting to ${ref_stash}@{0} @@ -408,7 +100,8 @@ show) ;; save) shift - save_stash "$@" + cd "$START_DIR" + git stash--helper save "$@" ;; push) shift From 5fda50086cc9a939880da0d4a91332d1d64e8ba2 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Tue, 31 Jul 2018 17:26:56 +0300 Subject: [PATCH 189/406] stash: convert `stash--helper.c` into `stash.c` The old shell script `git-stash.sh` was removed and replaced entirely by `builtin/stash.c`. In order to do that, `create` and `push` were adapted to work without `stash.sh`. For example, before this commit, `git stash create` called `git stash--helper create --message "$*"`. If it called `git stash--helper create "$@"`, then some of these changes wouldn't have been necessary. This commit also removes the word `helper` since now stash is called directly and not by a shell script. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- .gitignore | 1 - Makefile | 3 +- builtin.h | 2 +- builtin/{stash--helper.c => stash.c} | 157 +++++++++++++++------------ git-stash.sh | 153 -------------------------- git.c | 2 +- 6 files changed, 92 insertions(+), 226 deletions(-) rename builtin/{stash--helper.c => stash.c} (91%) delete mode 100755 git-stash.sh diff --git a/.gitignore b/.gitignore index b59661cb884688..ffceea7d59fd21 100644 --- a/.gitignore +++ b/.gitignore @@ -157,7 +157,6 @@ /git-show-ref /git-stage /git-stash -/git-stash--helper /git-status /git-stripspace /git-submodule diff --git a/Makefile b/Makefile index 482196f4718f45..f064d5eed3f1ab 100644 --- a/Makefile +++ b/Makefile @@ -617,7 +617,6 @@ SCRIPT_SH += git-quiltimport.sh SCRIPT_SH += git-rebase.sh SCRIPT_SH += git-remote-testgit.sh SCRIPT_SH += git-request-pull.sh -SCRIPT_SH += git-stash.sh SCRIPT_SH += git-submodule.sh SCRIPT_SH += git-web--browse.sh @@ -1093,7 +1092,7 @@ BUILTIN_OBJS += builtin/shortlog.o BUILTIN_OBJS += builtin/show-branch.o BUILTIN_OBJS += builtin/show-index.o BUILTIN_OBJS += builtin/show-ref.o -BUILTIN_OBJS += builtin/stash--helper.o +BUILTIN_OBJS += builtin/stash.o BUILTIN_OBJS += builtin/stripspace.o BUILTIN_OBJS += builtin/submodule--helper.o BUILTIN_OBJS += builtin/symbolic-ref.o diff --git a/builtin.h b/builtin.h index 317bc338f729f2..e60f0fc58ffb97 100644 --- a/builtin.h +++ b/builtin.h @@ -223,7 +223,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix); extern int cmd_show_branch(int argc, const char **argv, const char *prefix); extern int cmd_show_index(int argc, const char **argv, const char *prefix); extern int cmd_status(int argc, const char **argv, const char *prefix); -extern int cmd_stash__helper(int argc, const char **argv, const char *prefix); +extern int cmd_stash(int argc, const char **argv, const char *prefix); extern int cmd_stripspace(int argc, const char **argv, const char *prefix); extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix); extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix); diff --git a/builtin/stash--helper.c b/builtin/stash.c similarity index 91% rename from builtin/stash--helper.c rename to builtin/stash.c index e6f27fc1fa0311..9bef5ddc4b98c6 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash.c @@ -16,75 +16,70 @@ #define INCLUDE_ALL_FILES 2 -static const char * const git_stash_helper_usage[] = { - N_("git stash--helper list [<options>]"), - N_("git stash--helper show [<options>] [<stash>]"), - N_("git stash--helper drop [-q|--quiet] [<stash>]"), - N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), - N_("git stash--helper branch <branchname> [<stash>]"), - N_("git stash--helper clear"), - N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" +static const char * const git_stash_usage[] = { + N_("git stash list [<options>]"), + N_("git stash show [<options>] [<stash>]"), + N_("git stash drop [-q|--quiet] [<stash>]"), + N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), + N_("git stash branch <branchname> [<stash>]"), + N_("git stash clear"), + N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" " [--] [<pathspec>...]]"), - N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" + N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] [<message>]"), NULL }; -static const char * const git_stash_helper_list_usage[] = { - N_("git stash--helper list [<options>]"), +static const char * const git_stash_list_usage[] = { + N_("git stash list [<options>]"), NULL }; -static const char * const git_stash_helper_show_usage[] = { - N_("git stash--helper show [<options>] [<stash>]"), +static const char * const git_stash_show_usage[] = { + N_("git stash show [<options>] [<stash>]"), NULL }; -static const char * const git_stash_helper_drop_usage[] = { - N_("git stash--helper drop [-q|--quiet] [<stash>]"), +static const char * const git_stash_drop_usage[] = { + N_("git stash drop [-q|--quiet] [<stash>]"), NULL }; -static const char * const git_stash_helper_pop_usage[] = { - N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"), +static const char * const git_stash_pop_usage[] = { + N_("git stash pop [--index] [-q|--quiet] [<stash>]"), NULL }; -static const char * const git_stash_helper_apply_usage[] = { - N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"), +static const char * const git_stash_apply_usage[] = { + N_("git stash apply [--index] [-q|--quiet] [<stash>]"), NULL }; -static const char * const git_stash_helper_branch_usage[] = { - N_("git stash--helper branch <branchname> [<stash>]"), +static const char * const git_stash_branch_usage[] = { + N_("git stash branch <branchname> [<stash>]"), NULL }; -static const char * const git_stash_helper_clear_usage[] = { - N_("git stash--helper clear"), +static const char * const git_stash_clear_usage[] = { + N_("git stash clear"), NULL }; -static const char * const git_stash_helper_store_usage[] = { - N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"), +static const char * const git_stash_store_usage[] = { + N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"), NULL }; -static const char * const git_stash_helper_create_usage[] = { - N_("git stash--helper create [<message>]"), - NULL -}; - -static const char * const git_stash_helper_push_usage[] = { - N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" +static const char * const git_stash_push_usage[] = { + N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" " [--] [<pathspec>...]]"), NULL }; -static const char * const git_stash_helper_save_usage[] = { - N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" +static const char * const git_stash_save_usage[] = { + N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] [<message>]"), NULL }; @@ -221,7 +216,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_clear_usage, + git_stash_clear_usage, PARSE_OPT_STOP_AT_NON_OPTION); if (argc) @@ -526,7 +521,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_apply_usage, 0); + git_stash_apply_usage, 0); if (get_stash_info(&info, argc, argv)) return -1; @@ -599,7 +594,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_drop_usage, 0); + git_stash_drop_usage, 0); if (get_stash_info(&info, argc, argv)) return -1; @@ -625,7 +620,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_pop_usage, 0); + git_stash_pop_usage, 0); if (get_stash_info(&info, argc, argv)) return -1; @@ -652,7 +647,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_branch_usage, 0); + git_stash_branch_usage, 0); if (!argc) { fprintf_ln(stderr, _("No branch name specified")); @@ -687,7 +682,7 @@ static int list_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_list_usage, + git_stash_list_usage, PARSE_OPT_KEEP_UNKNOWN); if (!ref_exists(ref_stash)) @@ -767,7 +762,7 @@ static int show_stash(int argc, const char **argv, const char *prefix) argc = setup_revisions(argc, argv, &rev, NULL); if (argc > 1) { free_stash_info(&info); - usage_with_options(git_stash_helper_show_usage, options); + usage_with_options(git_stash_show_usage, options); } rev.diffopt.flags.recursive = 1; @@ -813,7 +808,7 @@ static int store_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_store_usage, + git_stash_store_usage, PARSE_OPT_KEEP_UNKNOWN); if (argc != 1) { @@ -1237,28 +1232,16 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, static int create_stash(int argc, const char **argv, const char *prefix) { - int include_untracked = 0; int ret = 0; - const char *stash_msg = NULL; struct strbuf stash_msg_buf = STRBUF_INIT; struct stash_info info; struct pathspec ps; - struct option options[] = { - OPT_BOOL('u', "include-untracked", &include_untracked, - N_("include untracked files in stash")), - OPT_STRING('m', "message", &stash_msg, N_("message"), - N_("stash message")), - OPT_END() - }; - argc = parse_options(argc, argv, prefix, options, - git_stash_helper_create_usage, - 0); + /* Starting with argv[1], since argv[0] is "create" */ + strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' '); memset(&ps, 0, sizeof(ps)); - strbuf_addstr(&stash_msg_buf, stash_msg); - ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info, - NULL, 0); + ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0); if (!ret) printf_ln("%s", oid_to_hex(&info.w_commit)); @@ -1493,9 +1476,10 @@ static int push_stash(int argc, const char **argv, const char *prefix) OPT_END() }; - argc = parse_options(argc, argv, prefix, options, - git_stash_helper_push_usage, - 0); + if (argc) + argc = parse_options(argc, argv, prefix, options, + git_stash_push_usage, + 0); parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv); return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode, @@ -1528,7 +1512,7 @@ static int save_stash(int argc, const char **argv, const char *prefix) }; argc = parse_options(argc, argv, prefix, options, - git_stash_helper_save_usage, + git_stash_save_usage, PARSE_OPT_KEEP_DASHDASH); if (argc) @@ -1542,10 +1526,12 @@ static int save_stash(int argc, const char **argv, const char *prefix) return ret; } -int cmd_stash__helper(int argc, const char **argv, const char *prefix) +int cmd_stash(int argc, const char **argv, const char *prefix) { + int i = -1; pid_t pid = getpid(); const char *index_file; + struct argv_array args = ARGV_ARRAY_INIT; struct option options[] = { OPT_END() @@ -1553,16 +1539,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) git_config(git_diff_basic_config, NULL); - argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage, + argc = parse_options(argc, argv, prefix, options, git_stash_usage, PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); index_file = get_index_file(); strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid); - if (argc < 1) - usage_with_options(git_stash_helper_usage, options); - if (!strcmp(argv[0], "apply")) + if (!argc) + return !!push_stash(0, NULL, prefix); + else if (!strcmp(argv[0], "apply")) return !!apply_stash(argc, argv, prefix); else if (!strcmp(argv[0], "clear")) return !!clear_stash(argc, argv, prefix); @@ -1584,7 +1570,42 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) return !!push_stash(argc, argv, prefix); else if (!strcmp(argv[0], "save")) return !!save_stash(argc, argv, prefix); + else if (*argv[0] != '-') + usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), + git_stash_usage, options); + + if (strcmp(argv[0], "-p")) { + while (++i < argc && strcmp(argv[i], "--")) { + /* + * `akpqu` is a string which contains all short options, + * except `-m` which is verified separately. + */ + if ((strlen(argv[i]) == 2) && *argv[i] == '-' && + strchr("akpqu", argv[i][1])) + continue; + + if (!strcmp(argv[i], "--all") || + !strcmp(argv[i], "--keep-index") || + !strcmp(argv[i], "--no-keep-index") || + !strcmp(argv[i], "--patch") || + !strcmp(argv[i], "--quiet") || + !strcmp(argv[i], "--include-untracked")) + continue; + + /* + * `-m` and `--message=` are verified separately because + * they need to be immediately followed by a string + * (i.e.`-m"foobar"` or `--message="foobar"`). + */ + if (starts_with(argv[i], "-m") || + starts_with(argv[i], "--message=")) + continue; + + usage_with_options(git_stash_usage, options); + } + } - usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), - git_stash_helper_usage, options); + argv_array_push(&args, "push"); + argv_array_pushv(&args, argv); + return !!push_stash(args.argc, args.argv, prefix); } diff --git a/git-stash.sh b/git-stash.sh deleted file mode 100755 index 695f1feba3dde8..00000000000000 --- a/git-stash.sh +++ /dev/null @@ -1,153 +0,0 @@ -#!/bin/sh -# Copyright (c) 2007, Nanako Shiraishi - -dashless=$(basename "$0" | sed -e 's/-/ /') -USAGE="list [<options>] - or: $dashless show [<stash>] - or: $dashless drop [-q|--quiet] [<stash>] - or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>] - or: $dashless branch <branchname> [<stash>] - or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet] - [-u|--include-untracked] [-a|--all] [<message>] - or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet] - [-u|--include-untracked] [-a|--all] [-m <message>] - [-- <pathspec>...]] - or: $dashless clear" - -SUBDIRECTORY_OK=Yes -OPTIONS_SPEC= -START_DIR=$(pwd) -. git-sh-setup -require_work_tree -prefix=$(git rev-parse --show-prefix) || exit 1 -cd_to_toplevel - -TMP="$GIT_DIR/.git-stash.$$" -TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$ -trap 'rm -f "$TMP-"* "$TMPindex"' 0 - -ref_stash=refs/stash - -if git config --get-colorbool color.interactive; then - help_color="$(git config --get-color color.interactive.help 'red bold')" - reset_color="$(git config --get-color '' reset)" -else - help_color= - reset_color= -fi - -# -# Parses the remaining options looking for flags and -# at most one revision defaulting to ${ref_stash}@{0} -# if none found. -# -# Derives related tree and commit objects from the -# revision, if one is found. -# -# stash records the work tree, and is a merge between the -# base commit (first parent) and the index tree (second parent). -# -# REV is set to the symbolic version of the specified stash-like commit -# IS_STASH_LIKE is non-blank if ${REV} looks like a stash -# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref -# s is set to the SHA1 of the stash commit -# w_commit is set to the commit containing the working tree -# b_commit is set to the base commit -# i_commit is set to the commit containing the index tree -# u_commit is set to the commit containing the untracked files tree -# w_tree is set to the working tree -# b_tree is set to the base tree -# i_tree is set to the index tree -# u_tree is set to the untracked files tree -# -# GIT_QUIET is set to t if -q is specified -# INDEX_OPTION is set to --index if --index is specified. -# FLAGS is set to the remaining flags (if allowed) -# -# dies if: -# * too many revisions specified -# * no revision is specified and there is no stash stack -# * a revision is specified which cannot be resolve to a SHA1 -# * a non-existent stash reference is specified -# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t" -# - -test "$1" = "-p" && set "push" "$@" - -PARSE_CACHE='--not-parsed' -# The default command is "push" if nothing but options are given -seen_non_option= -for opt -do - case "$opt" in - --) break ;; - -*) ;; - *) seen_non_option=t; break ;; - esac -done - -test -n "$seen_non_option" || set "push" "$@" - -# Main command set -case "$1" in -list) - shift - git stash--helper list "$@" - ;; -show) - shift - git stash--helper show "$@" - ;; -save) - shift - cd "$START_DIR" - git stash--helper save "$@" - ;; -push) - shift - cd "$START_DIR" - git stash--helper push "$@" - ;; -apply) - shift - cd "$START_DIR" - git stash--helper apply "$@" - ;; -clear) - shift - git stash--helper clear "$@" - ;; -create) - shift - git stash--helper create --message "$*" - ;; -store) - shift - git stash--helper store "$@" - ;; -drop) - shift - git stash--helper drop "$@" - ;; -pop) - shift - cd "$START_DIR" - git stash--helper pop "$@" - ;; -branch) - shift - cd "$START_DIR" - git stash--helper branch "$@" - ;; -*) - case $# in - 0) - cd "$START_DIR" - git stash--helper push && - say "$(gettext "(To restore them type \"git stash apply\")")" - ;; - *) - usage - esac - ;; -esac diff --git a/git.c b/git.c index 3c0e762d7d2011..78548397cf1771 100644 --- a/git.c +++ b/git.c @@ -544,7 +544,7 @@ static struct cmd_struct commands[] = { { "show-index", cmd_show_index }, { "show-ref", cmd_show_ref, RUN_SETUP }, { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, - { "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE }, + { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE }, { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, { "stripspace", cmd_stripspace }, { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT }, From 7e88abd08c6a154c7d2903d5197ce2e0a4c36f69 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Wed, 1 Aug 2018 13:06:44 +0300 Subject: [PATCH 190/406] stash: optimize `get_untracked_files()` and `check_changes()` This commits introduces a optimization by avoiding calling the same functions again. For example, `git stash push -u` would call at some points the following functions: * `check_changes()` (inside `do_push_stash()`) * `do_create_stash()`, which calls: `check_changes()` and `get_untracked_files()` Note that `check_changes()` also calls `get_untracked_files()`. So, `check_changes()` is called 2 times and `get_untracked_files()` 3 times. The old function `check_changes()` now consists of two functions: `get_untracked_files()` and `check_changes_tracked_files()`. These are the call chains for `push` and `create`: * `push_stash()` -> `do_push_stash()` -> `do_create_stash()` * `create_stash()` -> `do_create_stash()` To prevent calling the same functions over and over again, `check_changes()` inside `do_create_stash()` is now placed in the caller functions (`create_stash()` and `do_push_stash()`). This way `check_changes()` and `get_untracked files()` are called only one time. https://public-inbox.org/git/20180818223329.GJ11326@hank.intra.tgummerer.com/ Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash.c | 50 ++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/builtin/stash.c b/builtin/stash.c index 9bef5ddc4b98c6..79275b995dc5c7 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -879,18 +879,18 @@ static int get_untracked_files(struct pathspec ps, int include_untracked, } /* - * The return value of `check_changes()` can be: + * The return value of `check_changes_tracked_files()` can be: * * < 0 if there was an error * = 0 if there are no changes. * > 0 if there are changes. */ -static int check_changes(struct pathspec ps, int include_untracked) + +static int check_changes_tracked_files(struct pathspec ps) { int result; struct rev_info rev; struct object_id dummy; - struct strbuf out = STRBUF_INIT; /* No initial commit. */ if (get_oid("HEAD", &dummy)) @@ -918,14 +918,26 @@ static int check_changes(struct pathspec ps, int include_untracked) if (diff_result_code(&rev.diffopt, result)) return 1; + return 0; +} + +/* + * The function will fill `untracked_files` with the names of untracked files + * It will return 1 if there were any changes and 0 if there were not. + */ + +static int check_changes(struct pathspec ps, int include_untracked, + struct strbuf *untracked_files) +{ + int ret = 0; + if (check_changes_tracked_files(ps)) + ret = 1; + if (include_untracked && get_untracked_files(ps, include_untracked, - &out)) { - strbuf_release(&out); - return 1; - } + untracked_files)) + ret = 1; - strbuf_release(&out); - return 0; + return ret; } static int save_untracked_files(struct stash_info *info, struct strbuf *msg, @@ -1134,7 +1146,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, head_commit = lookup_commit(the_repository, &info->b_commit); } - if (!check_changes(ps, include_untracked)) { + if (!check_changes(ps, include_untracked, &untracked_files)) { ret = 1; goto done; } @@ -1159,8 +1171,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, goto done; } - if (include_untracked && get_untracked_files(ps, include_untracked, - &untracked_files)) { + if (include_untracked) { if (save_untracked_files(info, &msg, untracked_files)) { if (!quiet) fprintf_ln(stderr, _("Cannot save " @@ -1241,18 +1252,14 @@ static int create_stash(int argc, const char **argv, const char *prefix) strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' '); memset(&ps, 0, sizeof(ps)); - ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0); + if (!check_changes_tracked_files(ps)) + return 0; - if (!ret) + if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0))) printf_ln("%s", oid_to_hex(&info.w_commit)); strbuf_release(&stash_msg_buf); - - /* - * ret can be 1 if there were no changes. In this case, we should - * not error out. - */ - return ret < 0; + return ret; } static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, @@ -1262,6 +1269,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, struct stash_info info; struct strbuf patch = STRBUF_INIT; struct strbuf stash_msg_buf = STRBUF_INIT; + struct strbuf untracked_files = STRBUF_INIT; if (patch_mode && keep_index == -1) keep_index = 1; @@ -1296,7 +1304,7 @@ static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, goto done; } - if (!check_changes(ps, include_untracked)) { + if (!check_changes(ps, include_untracked, &untracked_files)) { if (!quiet) printf_ln(_("No local changes to save")); goto done; From 22309ca0c86d4a5e1d2ed77f0158855516291823 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 22 Oct 2018 15:15:02 -0700 Subject: [PATCH 191/406] rebase (autostash): avoid duplicate call to state_dir_path() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We already called that function at this point, and stored the result in the `path` variable. We might just as well use it ;-) Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Reviewed-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 4bfba202dc3dc9..84884d48cc88e7 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -250,7 +250,7 @@ static int apply_autostash(struct rebase_options *opts) if (!file_exists(path)) return 0; - if (read_one(state_dir_path("autostash", opts), &autostash)) + if (read_one(path, &autostash)) return error(_("Could not read '%s'"), path); argv_array_pushl(&stash_apply.args, "stash", "apply", autostash.buf, NULL); From e9be5ccccf282decbaa032d62d9efee8910d7372 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 22 Oct 2018 15:15:03 -0700 Subject: [PATCH 192/406] rebase (autostash): store the full OID in <state-dir>/autostash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was reported by Gábor Szeder and analyzed by Alban Gruin that the built-in rebase stores only abbreviated stash hashes in the `autostash` file. This is problematic e.g. in t5520-pull.sh, where the abbreviated hash is so short that it sometimes consists only of digits, which are subsequently mistaken ("DWIMmed") for numbers by `git stash apply`. Let's align the behavior of the built-in rebase with the scripted rebase and store the full stash hash instead. That makes it a lot less likely that it consists only of digits. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Reviewed-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 84884d48cc88e7..0041ea65d6aa27 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1376,7 +1376,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (safe_create_leading_directories_const(autostash)) die(_("Could not create directory for '%s'"), options.state_dir); - write_file(autostash, "%s", buf.buf); + write_file(autostash, "%s", oid_to_hex(&oid)); printf(_("Created autostash: %s\n"), buf.buf); if (reset_head(&head->object.oid, "reset --hard", NULL, 0, NULL, NULL) < 0) From 4a693248b6d358b20916cad099c4e9970a765aeb Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Date: Thu, 2 Aug 2018 20:50:24 +0300 Subject: [PATCH 193/406] stash: replace all `write-tree` child processes with API calls This commit replaces spawning `git write-tree` with API calls. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> --- builtin/stash.c | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/builtin/stash.c b/builtin/stash.c index 79275b995dc5c7..c15db3fe533856 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -945,9 +945,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg, { int ret = 0; struct strbuf untracked_msg = STRBUF_INIT; - struct strbuf out = STRBUF_INIT; struct child_process cp_upd_index = CHILD_PROCESS_INIT; - struct child_process cp_write_tree = CHILD_PROCESS_INIT; + struct index_state istate = { NULL }; cp_upd_index.git_cmd = 1; argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add", @@ -962,15 +961,11 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg, goto done; } - cp_write_tree.git_cmd = 1; - argv_array_push(&cp_write_tree.args, "write-tree"); - argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s", - stash_index_path.buf); - if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) { + if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0, + NULL)) { ret = -1; goto done; } - get_oid_hex(out.buf, &info->u_tree); if (commit_tree(untracked_msg.buf, untracked_msg.len, &info->u_tree, NULL, &info->u_commit, NULL, NULL)) { @@ -979,8 +974,8 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg, } done: + discard_index(&istate); strbuf_release(&untracked_msg); - strbuf_release(&out); remove_path(stash_index_path.buf); return ret; } @@ -989,11 +984,10 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, struct strbuf *out_patch, int quiet) { int ret = 0; - struct strbuf out = STRBUF_INIT; struct child_process cp_read_tree = CHILD_PROCESS_INIT; struct child_process cp_add_i = CHILD_PROCESS_INIT; - struct child_process cp_write_tree = CHILD_PROCESS_INIT; struct child_process cp_diff_tree = CHILD_PROCESS_INIT; + struct index_state istate = { NULL }; remove_path(stash_index_path.buf); @@ -1019,17 +1013,12 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, } /* State of the working tree. */ - cp_write_tree.git_cmd = 1; - argv_array_push(&cp_write_tree.args, "write-tree"); - argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s", - stash_index_path.buf); - if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) { + if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, + NULL)) { ret = -1; goto done; } - get_oid_hex(out.buf, &info->w_tree); - cp_diff_tree.git_cmd = 1; argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD", oid_to_hex(&info->w_tree), "--", NULL); @@ -1045,7 +1034,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps, } done: - strbuf_release(&out); + discard_index(&istate); remove_path(stash_index_path.buf); return ret; } @@ -1055,9 +1044,8 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps) int ret = 0; struct rev_info rev; struct child_process cp_upd_index = CHILD_PROCESS_INIT; - struct child_process cp_write_tree = CHILD_PROCESS_INIT; - struct strbuf out = STRBUF_INIT; struct strbuf diff_output = STRBUF_INIT; + struct index_state istate = { NULL }; set_alternate_index_output(stash_index_path.buf); if (reset_tree(&info->i_tree, 0, 0)) { @@ -1096,20 +1084,15 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps) goto done; } - cp_write_tree.git_cmd = 1; - argv_array_push(&cp_write_tree.args, "write-tree"); - argv_array_pushf(&cp_write_tree.env_array, "GIT_INDEX_FILE=%s", - stash_index_path.buf); - if (pipe_command(&cp_write_tree, NULL, 0, &out, 0,NULL, 0)) { + if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, + NULL)) { ret = -1; goto done; } - get_oid_hex(out.buf, &info->w_tree); - done: + discard_index(&istate); UNLEAK(rev); - strbuf_release(&out); object_array_clear(&rev.pending); strbuf_release(&diff_output); remove_path(stash_index_path.buf); From c750c69c9ea34064960eec20ca9b4dcb5d7989da Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 22 Oct 2018 15:15:05 -0700 Subject: [PATCH 194/406] rebase (autostash): use an explicit OID to apply the stash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `git stash apply <argument>` sees an argument that consists only of digits, it tries to be smart and interpret it as `stash@{<number>}`. Unfortunately, an all-digit hash (which is unlikely but still possible) is therefore misinterpreted as `stash@{<n>}` reflog. To prevent that from happening, let's append `^0` after the stash hash, to make sure that it is interpreted as an OID rather than as a number. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Reviewed-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index 0041ea65d6aa27..4c664f958ce49d 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -252,6 +252,8 @@ static int apply_autostash(struct rebase_options *opts) if (read_one(path, &autostash)) return error(_("Could not read '%s'"), path); + /* Ensure that the hash is not mistaken for a number */ + strbuf_addstr(&autostash, "^0"); argv_array_pushl(&stash_apply.args, "stash", "apply", autostash.buf, NULL); stash_apply.git_cmd = 1; From 0ebe47d32c23cb117f5c65f907a273dc246991cb Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 24 Aug 2018 15:14:04 +0200 Subject: [PATCH 195/406] Add back the original, scripted `git stash` This simply copies the version as of v2.19.0-rc0 verbatim. As of now, it is not hooked up. The next commit will change the builtin `stash` to hand off to the scripted `git stash` when `stash.useBuiltin=false`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-stash.sh | 752 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 752 insertions(+) create mode 100755 git-stash.sh diff --git a/git-stash.sh b/git-stash.sh new file mode 100755 index 00000000000000..94793c1a913abf --- /dev/null +++ b/git-stash.sh @@ -0,0 +1,752 @@ +#!/bin/sh +# Copyright (c) 2007, Nanako Shiraishi + +dashless=$(basename "$0" | sed -e 's/-/ /') +USAGE="list [<options>] + or: $dashless show [<stash>] + or: $dashless drop [-q|--quiet] [<stash>] + or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>] + or: $dashless branch <branchname> [<stash>] + or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet] + [-u|--include-untracked] [-a|--all] [<message>] + or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet] + [-u|--include-untracked] [-a|--all] [-m <message>] + [-- <pathspec>...]] + or: $dashless clear" + +SUBDIRECTORY_OK=Yes +OPTIONS_SPEC= +START_DIR=$(pwd) +. git-sh-setup +require_work_tree +prefix=$(git rev-parse --show-prefix) || exit 1 +cd_to_toplevel + +TMP="$GIT_DIR/.git-stash.$$" +TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$ +trap 'rm -f "$TMP-"* "$TMPindex"' 0 + +ref_stash=refs/stash + +if git config --get-colorbool color.interactive; then + help_color="$(git config --get-color color.interactive.help 'red bold')" + reset_color="$(git config --get-color '' reset)" +else + help_color= + reset_color= +fi + +no_changes () { + git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" && + git diff-files --quiet --ignore-submodules -- "$@" && + (test -z "$untracked" || test -z "$(untracked_files "$@")") +} + +untracked_files () { + if test "$1" = "-z" + then + shift + z=-z + else + z= + fi + excl_opt=--exclude-standard + test "$untracked" = "all" && excl_opt= + git ls-files -o $z $excl_opt -- "$@" +} + +clear_stash () { + if test $# != 0 + then + die "$(gettext "git stash clear with parameters is unimplemented")" + fi + if current=$(git rev-parse --verify --quiet $ref_stash) + then + git update-ref -d $ref_stash $current + fi +} + +create_stash () { + stash_msg= + untracked= + while test $# != 0 + do + case "$1" in + -m|--message) + shift + stash_msg=${1?"BUG: create_stash () -m requires an argument"} + ;; + -m*) + stash_msg=${1#-m} + ;; + --message=*) + stash_msg=${1#--message=} + ;; + -u|--include-untracked) + shift + untracked=${1?"BUG: create_stash () -u requires an argument"} + ;; + --) + shift + break + ;; + esac + shift + done + + git update-index -q --refresh + if no_changes "$@" + then + exit 0 + fi + + # state of the base commit + if b_commit=$(git rev-parse --verify HEAD) + then + head=$(git rev-list --oneline -n 1 HEAD --) + else + die "$(gettext "You do not have the initial commit yet")" + fi + + if branch=$(git symbolic-ref -q HEAD) + then + branch=${branch#refs/heads/} + else + branch='(no branch)' + fi + msg=$(printf '%s: %s' "$branch" "$head") + + # state of the index + i_tree=$(git write-tree) && + i_commit=$(printf 'index on %s\n' "$msg" | + git commit-tree $i_tree -p $b_commit) || + die "$(gettext "Cannot save the current index state")" + + if test -n "$untracked" + then + # Untracked files are stored by themselves in a parentless commit, for + # ease of unpacking later. + u_commit=$( + untracked_files -z "$@" | ( + GIT_INDEX_FILE="$TMPindex" && + export GIT_INDEX_FILE && + rm -f "$TMPindex" && + git update-index -z --add --remove --stdin && + u_tree=$(git write-tree) && + printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree && + rm -f "$TMPindex" + ) ) || die "$(gettext "Cannot save the untracked files")" + + untracked_commit_option="-p $u_commit"; + else + untracked_commit_option= + fi + + if test -z "$patch_mode" + then + + # state of the working tree + w_tree=$( ( + git read-tree --index-output="$TMPindex" -m $i_tree && + GIT_INDEX_FILE="$TMPindex" && + export GIT_INDEX_FILE && + git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" && + git update-index -z --add --remove --stdin <"$TMP-stagenames" && + git write-tree && + rm -f "$TMPindex" + ) ) || + die "$(gettext "Cannot save the current worktree state")" + + else + + rm -f "$TMP-index" && + GIT_INDEX_FILE="$TMP-index" git read-tree HEAD && + + # find out what the user wants + GIT_INDEX_FILE="$TMP-index" \ + git add--interactive --patch=stash -- "$@" && + + # state of the working tree + w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) || + die "$(gettext "Cannot save the current worktree state")" + + git diff-tree -p HEAD $w_tree -- >"$TMP-patch" && + test -s "$TMP-patch" || + die "$(gettext "No changes selected")" + + rm -f "$TMP-index" || + die "$(gettext "Cannot remove temporary index (can't happen)")" + + fi + + # create the stash + if test -z "$stash_msg" + then + stash_msg=$(printf 'WIP on %s' "$msg") + else + stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg") + fi + w_commit=$(printf '%s\n' "$stash_msg" | + git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) || + die "$(gettext "Cannot record working tree state")" +} + +store_stash () { + while test $# != 0 + do + case "$1" in + -m|--message) + shift + stash_msg="$1" + ;; + -m*) + stash_msg=${1#-m} + ;; + --message=*) + stash_msg=${1#--message=} + ;; + -q|--quiet) + quiet=t + ;; + *) + break + ;; + esac + shift + done + test $# = 1 || + die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")" + + w_commit="$1" + if test -z "$stash_msg" + then + stash_msg="Created via \"git stash store\"." + fi + + git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit + ret=$? + test $ret != 0 && test -z "$quiet" && + die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")" + return $ret +} + +push_stash () { + keep_index= + patch_mode= + untracked= + stash_msg= + while test $# != 0 + do + case "$1" in + -k|--keep-index) + keep_index=t + ;; + --no-keep-index) + keep_index=n + ;; + -p|--patch) + patch_mode=t + # only default to keep if we don't already have an override + test -z "$keep_index" && keep_index=t + ;; + -q|--quiet) + GIT_QUIET=t + ;; + -u|--include-untracked) + untracked=untracked + ;; + -a|--all) + untracked=all + ;; + -m|--message) + shift + test -z ${1+x} && usage + stash_msg=$1 + ;; + -m*) + stash_msg=${1#-m} + ;; + --message=*) + stash_msg=${1#--message=} + ;; + --help) + show_help + ;; + --) + shift + break + ;; + -*) + option="$1" + eval_gettextln "error: unknown option for 'stash push': \$option" + usage + ;; + *) + break + ;; + esac + shift + done + + eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")" + + if test -n "$patch_mode" && test -n "$untracked" + then + die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")" + fi + + test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1 + + git update-index -q --refresh + if no_changes "$@" + then + say "$(gettext "No local changes to save")" + exit 0 + fi + + git reflog exists $ref_stash || + clear_stash || die "$(gettext "Cannot initialize stash")" + + create_stash -m "$stash_msg" -u "$untracked" -- "$@" + store_stash -m "$stash_msg" -q $w_commit || + die "$(gettext "Cannot save the current status")" + say "$(eval_gettext "Saved working directory and index state \$stash_msg")" + + if test -z "$patch_mode" + then + test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION= + if test -n "$untracked" && test $# = 0 + then + git clean --force --quiet -d $CLEAN_X_OPTION + fi + + if test $# != 0 + then + test -z "$untracked" && UPDATE_OPTION="-u" || UPDATE_OPTION= + test "$untracked" = "all" && FORCE_OPTION="--force" || FORCE_OPTION= + git add $UPDATE_OPTION $FORCE_OPTION -- "$@" + git diff-index -p --cached --binary HEAD -- "$@" | + git apply --index -R + else + git reset --hard -q + fi + + if test "$keep_index" = "t" && test -n "$i_tree" + then + git read-tree --reset $i_tree + git ls-files -z --modified -- "$@" | + git checkout-index -z --force --stdin + fi + else + git apply -R < "$TMP-patch" || + die "$(gettext "Cannot remove worktree changes")" + + if test "$keep_index" != "t" + then + git reset -q -- "$@" + fi + fi +} + +save_stash () { + push_options= + while test $# != 0 + do + case "$1" in + --) + shift + break + ;; + -*) + # pass all options through to push_stash + push_options="$push_options $1" + ;; + *) + break + ;; + esac + shift + done + + stash_msg="$*" + + if test -z "$stash_msg" + then + push_stash $push_options + else + push_stash $push_options -m "$stash_msg" + fi +} + +have_stash () { + git rev-parse --verify --quiet $ref_stash >/dev/null +} + +list_stash () { + have_stash || return 0 + git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash -- +} + +show_stash () { + ALLOW_UNKNOWN_FLAGS=t + assert_stash_like "$@" + + if test -z "$FLAGS" + then + if test "$(git config --bool stash.showStat || echo true)" = "true" + then + FLAGS=--stat + fi + + if test "$(git config --bool stash.showPatch || echo false)" = "true" + then + FLAGS=${FLAGS}${FLAGS:+ }-p + fi + + if test -z "$FLAGS" + then + return 0 + fi + fi + + git diff ${FLAGS} $b_commit $w_commit +} + +show_help () { + exec git help stash + exit 1 +} + +# +# Parses the remaining options looking for flags and +# at most one revision defaulting to ${ref_stash}@{0} +# if none found. +# +# Derives related tree and commit objects from the +# revision, if one is found. +# +# stash records the work tree, and is a merge between the +# base commit (first parent) and the index tree (second parent). +# +# REV is set to the symbolic version of the specified stash-like commit +# IS_STASH_LIKE is non-blank if ${REV} looks like a stash +# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref +# s is set to the SHA1 of the stash commit +# w_commit is set to the commit containing the working tree +# b_commit is set to the base commit +# i_commit is set to the commit containing the index tree +# u_commit is set to the commit containing the untracked files tree +# w_tree is set to the working tree +# b_tree is set to the base tree +# i_tree is set to the index tree +# u_tree is set to the untracked files tree +# +# GIT_QUIET is set to t if -q is specified +# INDEX_OPTION is set to --index if --index is specified. +# FLAGS is set to the remaining flags (if allowed) +# +# dies if: +# * too many revisions specified +# * no revision is specified and there is no stash stack +# * a revision is specified which cannot be resolve to a SHA1 +# * a non-existent stash reference is specified +# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t" +# + +parse_flags_and_rev() +{ + test "$PARSE_CACHE" = "$*" && return 0 # optimisation + PARSE_CACHE="$*" + + IS_STASH_LIKE= + IS_STASH_REF= + INDEX_OPTION= + s= + w_commit= + b_commit= + i_commit= + u_commit= + w_tree= + b_tree= + i_tree= + u_tree= + + FLAGS= + REV= + for opt + do + case "$opt" in + -q|--quiet) + GIT_QUIET=-t + ;; + --index) + INDEX_OPTION=--index + ;; + --help) + show_help + ;; + -*) + test "$ALLOW_UNKNOWN_FLAGS" = t || + die "$(eval_gettext "unknown option: \$opt")" + FLAGS="${FLAGS}${FLAGS:+ }$opt" + ;; + *) + REV="${REV}${REV:+ }'$opt'" + ;; + esac + done + + eval set -- $REV + + case $# in + 0) + have_stash || die "$(gettext "No stash entries found.")" + set -- ${ref_stash}@{0} + ;; + 1) + : + ;; + *) + die "$(eval_gettext "Too many revisions specified: \$REV")" + ;; + esac + + case "$1" in + *[!0-9]*) + : + ;; + *) + set -- "${ref_stash}@{$1}" + ;; + esac + + REV=$(git rev-parse --symbolic --verify --quiet "$1") || { + reference="$1" + die "$(eval_gettext "\$reference is not a valid reference")" + } + + i_commit=$(git rev-parse --verify --quiet "$REV^2") && + set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) && + s=$1 && + w_commit=$1 && + b_commit=$2 && + w_tree=$3 && + b_tree=$4 && + i_tree=$5 && + IS_STASH_LIKE=t && + test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" && + IS_STASH_REF=t + + u_commit=$(git rev-parse --verify --quiet "$REV^3") && + u_tree=$(git rev-parse "$REV^3:" 2>/dev/null) +} + +is_stash_like() +{ + parse_flags_and_rev "$@" + test -n "$IS_STASH_LIKE" +} + +assert_stash_like() { + is_stash_like "$@" || { + args="$*" + die "$(eval_gettext "'\$args' is not a stash-like commit")" + } +} + +is_stash_ref() { + is_stash_like "$@" && test -n "$IS_STASH_REF" +} + +assert_stash_ref() { + is_stash_ref "$@" || { + args="$*" + die "$(eval_gettext "'\$args' is not a stash reference")" + } +} + +apply_stash () { + + assert_stash_like "$@" + + git update-index -q --refresh || die "$(gettext "unable to refresh index")" + + # current index state + c_tree=$(git write-tree) || + die "$(gettext "Cannot apply a stash in the middle of a merge")" + + unstashed_index_tree= + if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" && + test "$c_tree" != "$i_tree" + then + git diff-tree --binary $s^2^..$s^2 | git apply --cached + test $? -ne 0 && + die "$(gettext "Conflicts in index. Try without --index.")" + unstashed_index_tree=$(git write-tree) || + die "$(gettext "Could not save index tree")" + git reset + fi + + if test -n "$u_tree" + then + GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" && + GIT_INDEX_FILE="$TMPindex" git checkout-index --all && + rm -f "$TMPindex" || + die "$(gettext "Could not restore untracked files from stash entry")" + fi + + eval " + GITHEAD_$w_tree='Stashed changes' && + GITHEAD_$c_tree='Updated upstream' && + GITHEAD_$b_tree='Version stash was based on' && + export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree + " + + if test -n "$GIT_QUIET" + then + GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY + fi + if git merge-recursive $b_tree -- $c_tree $w_tree + then + # No conflict + if test -n "$unstashed_index_tree" + then + git read-tree "$unstashed_index_tree" + else + a="$TMP-added" && + git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" && + git read-tree --reset $c_tree && + git update-index --add --stdin <"$a" || + die "$(gettext "Cannot unstage modified files")" + rm -f "$a" + fi + squelch= + if test -n "$GIT_QUIET" + then + squelch='>/dev/null 2>&1' + fi + (cd "$START_DIR" && eval "git status $squelch") || : + else + # Merge conflict; keep the exit status from merge-recursive + status=$? + git rerere + if test -n "$INDEX_OPTION" + then + gettextln "Index was not unstashed." >&2 + fi + exit $status + fi +} + +pop_stash() { + assert_stash_ref "$@" + + if apply_stash "$@" + then + drop_stash "$@" + else + status=$? + say "$(gettext "The stash entry is kept in case you need it again.")" + exit $status + fi +} + +drop_stash () { + assert_stash_ref "$@" + + git reflog delete --updateref --rewrite "${REV}" && + say "$(eval_gettext "Dropped \${REV} (\$s)")" || + die "$(eval_gettext "\${REV}: Could not drop stash entry")" + + # clear_stash if we just dropped the last stash entry + git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null || + clear_stash +} + +apply_to_branch () { + test -n "$1" || die "$(gettext "No branch name specified")" + branch=$1 + shift 1 + + set -- --index "$@" + assert_stash_like "$@" + + git checkout -b $branch $REV^ && + apply_stash "$@" && { + test -z "$IS_STASH_REF" || drop_stash "$@" + } +} + +test "$1" = "-p" && set "push" "$@" + +PARSE_CACHE='--not-parsed' +# The default command is "push" if nothing but options are given +seen_non_option= +for opt +do + case "$opt" in + --) break ;; + -*) ;; + *) seen_non_option=t; break ;; + esac +done + +test -n "$seen_non_option" || set "push" "$@" + +# Main command set +case "$1" in +list) + shift + list_stash "$@" + ;; +show) + shift + show_stash "$@" + ;; +save) + shift + save_stash "$@" + ;; +push) + shift + push_stash "$@" + ;; +apply) + shift + apply_stash "$@" + ;; +clear) + shift + clear_stash "$@" + ;; +create) + shift + create_stash -m "$*" && echo "$w_commit" + ;; +store) + shift + store_stash "$@" + ;; +drop) + shift + drop_stash "$@" + ;; +pop) + shift + pop_stash "$@" + ;; +branch) + shift + apply_to_branch "$@" + ;; +*) + case $# in + 0) + push_stash && + say "$(gettext "(To restore them type \"git stash apply\")")" + ;; + *) + usage + esac + ;; +esac From 348cb912d2fa8bc330d434ea070e67d11ef6c1ab Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 23 Oct 2018 12:57:16 -0700 Subject: [PATCH 196/406] rebase --autostash: demonstrate a problem with dirty submodules It has been reported that dirty submodules cause problems with the built-in rebase when it is asked to autostash. The symptom is: fatal: Unexpected stash response: '' This patch adds a regression test that demonstrates that bug. Original report: https://github.com/git-for-windows/git/issues/1820 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t3420-rebase-autostash.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh index 0c4eefec760efa..7eb9f904191da5 100755 --- a/t/t3420-rebase-autostash.sh +++ b/t/t3420-rebase-autostash.sh @@ -351,4 +351,14 @@ test_expect_success 'autostash is saved on editor failure with conflict' ' test_cmp expected file0 ' +test_expect_failure 'autostash with dirty submodules' ' + test_when_finished "git reset --hard && git checkout master" && + git checkout -b with-submodule && + git submodule add ./ sub && + test_tick && + git commit -m add-submodule && + echo changed >sub/file0 && + git rebase -i --autostash HEAD +' + test_done From 15a638770ba9698146a1dc010682d0a28abe5d26 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 24 Aug 2018 15:07:41 +0200 Subject: [PATCH 197/406] stash: optionally use the scripted version again We recently converted the `git stash` command from Unix shell scripts to builtins. Just like we have `rebase.useBuiltin` to fall back to the scripted rebase, to give end users a way out when they discover a bug in the builtin command, this commit adds support for `stash.useBuiltin`. This is necessary because Git for Windows wants to ship the builtin stash earlier than core Git: Git for Windows v2.19.0 will come with the option of a drastically faster (if a lot less battle-tested) `git stash`. As the file name `git-stash` is already in use, let's rename the scripted backend to `git-legacy-stash`. To make the test suite pass with `stash.useBuiltin=false`, this commit also backports rudimentary support for `-q` (but only *just* enough to appease the test suite), and adds a super-ugly hack to force exit code 129 for `git stash -h`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitignore | 1 + Makefile | 1 + builtin/stash.c | 35 +++++++++++++++++++++++++++++ git-stash.sh => git-legacy-stash.sh | 34 +++++++++++++++++++++++++--- git-sh-setup.sh | 1 + git.c | 7 +++++- 6 files changed, 75 insertions(+), 4 deletions(-) rename git-stash.sh => git-legacy-stash.sh (97%) diff --git a/.gitignore b/.gitignore index ffceea7d59fd21..575b395ced9637 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ /git-init-db /git-interpret-trailers /git-instaweb +/git-legacy-stash /git-log /git-ls-files /git-ls-remote diff --git a/Makefile b/Makefile index f064d5eed3f1ab..eb1349c88364cc 100644 --- a/Makefile +++ b/Makefile @@ -615,6 +615,7 @@ SCRIPT_SH += git-merge-resolve.sh SCRIPT_SH += git-mergetool.sh SCRIPT_SH += git-quiltimport.sh SCRIPT_SH += git-rebase.sh +SCRIPT_SH += git-legacy-stash.sh SCRIPT_SH += git-remote-testgit.sh SCRIPT_SH += git-request-pull.sh SCRIPT_SH += git-submodule.sh diff --git a/builtin/stash.c b/builtin/stash.c index c15db3fe533856..29d04c9f2fe181 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -13,6 +13,7 @@ #include "revision.h" #include "log-tree.h" #include "diffcore.h" +#include "exec-cmd.h" #define INCLUDE_ALL_FILES 2 @@ -1517,6 +1518,26 @@ static int save_stash(int argc, const char **argv, const char *prefix) return ret; } +static int use_builtin_stash(void) +{ + struct child_process cp = CHILD_PROCESS_INIT; + struct strbuf out = STRBUF_INIT; + int ret; + + argv_array_pushl(&cp.args, + "config", "--bool", "stash.usebuiltin", NULL); + cp.git_cmd = 1; + if (capture_command(&cp, &out, 6)) { + strbuf_release(&out); + return 1; + } + + strbuf_trim(&out); + ret = !strcmp("true", out.buf); + strbuf_release(&out); + return ret; +} + int cmd_stash(int argc, const char **argv, const char *prefix) { int i = -1; @@ -1528,6 +1549,20 @@ int cmd_stash(int argc, const char **argv, const char *prefix) OPT_END() }; + if (!use_builtin_stash()) { + const char *path = mkpath("%s/git-legacy-stash", + git_exec_path()); + + if (sane_execvp(path, (char **)argv) < 0) + die_errno(_("could not exec %s"), path); + else + BUG("sane_execvp() returned???"); + } + + prefix = setup_git_directory(); + trace_repo_setup(prefix); + setup_work_tree(); + git_config(git_diff_basic_config, NULL); argc = parse_options(argc, argv, prefix, options, git_stash_usage, diff --git a/git-stash.sh b/git-legacy-stash.sh similarity index 97% rename from git-stash.sh rename to git-legacy-stash.sh index 94793c1a913abf..eed7da488456a5 100755 --- a/git-stash.sh +++ b/git-legacy-stash.sh @@ -66,6 +66,28 @@ clear_stash () { fi } +maybe_quiet () { + case "$1" in + --keep-stdout) + shift + if test -n "$GIT_QUIET" + then + eval "$@" 2>/dev/null + else + eval "$@" + fi + ;; + *) + if test -n "$GIT_QUIET" + then + eval "$@" >/dev/null 2>&1 + else + eval "$@" + fi + ;; + esac +} + create_stash () { stash_msg= untracked= @@ -95,15 +117,18 @@ create_stash () { done git update-index -q --refresh - if no_changes "$@" + if maybe_quiet no_changes "$@" then exit 0 fi # state of the base commit - if b_commit=$(git rev-parse --verify HEAD) + if b_commit=$(maybe_quiet --keep-stdout git rev-parse --verify HEAD) then head=$(git rev-list --oneline -n 1 HEAD --) + elif test -n "$GIT_QUIET" + then + exit 1 else die "$(gettext "You do not have the initial commit yet")" fi @@ -298,7 +323,7 @@ push_stash () { test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1 git update-index -q --refresh - if no_changes "$@" + if maybe_quiet no_changes "$@" then say "$(gettext "No local changes to save")" exit 0 @@ -353,6 +378,9 @@ save_stash () { while test $# != 0 do case "$1" in + -q|--quiet) + GIT_QUIET=t + ;; --) shift break diff --git a/git-sh-setup.sh b/git-sh-setup.sh index 378928518b2c42..10d97641856855 100644 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -101,6 +101,7 @@ $LONG_USAGE")" case "$1" in -h) echo "$LONG_USAGE" + case "$0" in *git-legacy-stash) exit 129;; esac exit esac fi diff --git a/git.c b/git.c index 78548397cf1771..dba1c68182acbb 100644 --- a/git.c +++ b/git.c @@ -544,7 +544,12 @@ static struct cmd_struct commands[] = { { "show-index", cmd_show_index }, { "show-ref", cmd_show_ref, RUN_SETUP }, { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, - { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE }, + /* + * NEEDSWORK: Until the builtin stash is thoroughly robust and no + * longer needs redirection to the stash shell script this is kept as + * is, then should be changed to RUN_SETUP | NEED_WORK_TREE + */ + { "stash", cmd_stash }, { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, { "stripspace", cmd_stripspace }, { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT }, From 34058dcb703c1eebf844ba2d584eba38c1afebe9 Mon Sep 17 00:00:00 2001 From: Junio C Hamano <gitster@pobox.com> Date: Fri, 28 Sep 2018 15:03:02 -0700 Subject: [PATCH 198/406] rebase: fix typoes in error messages The separator between words in a multi-word option name is a dash, not an underscore. Inspired by a matching change by Ralf Thielow for the scripted version of "git rebase". Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index b4881ebb222338..4bfba202dc3dc9 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1217,15 +1217,15 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) * git-rebase.txt caveats with "unless you know what you are doing" */ if (options.rebase_merges) - die(_("error: cannot combine '--preserve_merges' with " + die(_("error: cannot combine '--preserve-merges' with " "'--rebase-merges'")); if (options.rebase_merges) { if (strategy_options.nr) - die(_("error: cannot combine '--rebase_merges' with " + die(_("error: cannot combine '--rebase-merges' with " "'--strategy-option'")); if (options.strategy) - die(_("error: cannot combine '--rebase_merges' with " + die(_("error: cannot combine '--rebase-merges' with " "'--strategy'")); } From ebf928b6b8d457492de7462cd2b7d19feaf01e61 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 03:44:30 -0800 Subject: [PATCH 199/406] rebase: consolidate clean-up code before leaving reset_head() The same clean-up code is repeated quite a few times; Let's DRY up the code some. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index a40e312d54617e..3c302da0f177bb 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -542,13 +542,15 @@ static int reset_head(struct object_id *oid, const char *action, if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) BUG("Not a fully qualified branch: '%s'", switch_to_branch); - if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) - return -1; + if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { + ret = -1; + goto leave_reset_head; + } if (!oid) { if (get_oid("HEAD", &head_oid)) { - rollback_lock_file(&lock); - return error(_("could not determine HEAD revision")); + ret = error(_("could not determine HEAD revision")); + goto leave_reset_head; } oid = &head_oid; } @@ -565,32 +567,27 @@ static int reset_head(struct object_id *oid, const char *action, unpack_tree_opts.reset = 1; if (read_index_unmerged(the_repository->index) < 0) { - rollback_lock_file(&lock); - return error(_("could not read index")); + ret = error(_("could not read index")); + goto leave_reset_head; } if (!fill_tree_descriptor(&desc, oid)) { - error(_("failed to find tree of %s"), oid_to_hex(oid)); - rollback_lock_file(&lock); - free((void *)desc.buffer); - return -1; + ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); + goto leave_reset_head; } if (unpack_trees(1, &desc, &unpack_tree_opts)) { - rollback_lock_file(&lock); - free((void *)desc.buffer); - return -1; + ret = -1; + goto leave_reset_head; } tree = parse_tree_indirect(oid); prime_cache_tree(the_repository->index, tree); - if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) + if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) { ret = error(_("could not write index")); - free((void *)desc.buffer); - - if (ret) - return ret; + goto leave_reset_head; + } reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); @@ -624,7 +621,10 @@ static int reset_head(struct object_id *oid, const char *action, UPDATE_REFS_MSG_ON_ERR); } +leave_reset_head: strbuf_release(&msg); + rollback_lock_file(&lock); + free((void *)desc.buffer); return ret; } From d800c2c39923f59a03b7cb6faa04d6f77b69c0ff Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sun, 23 Oct 2016 01:22:20 -0700 Subject: [PATCH 200/406] t7800: fix quoting When passing a command-line to call an external diff command to the difftool, we must be prepared for paths containing special characters, e.g. backslashes in the temporary directory's path on Windows. This patch is needed in preparation for the next commit, which will make the MinGW version of Git *not* rewrite TMP to use forward slashes instead of backslashes. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t7800-difftool.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh index 668bbee73c8dc5..f29aa8f344f1a6 100755 --- a/t/t7800-difftool.sh +++ b/t/t7800-difftool.sh @@ -332,7 +332,7 @@ test_expect_success 'difftool --extcmd cat arg1' ' test_expect_success 'difftool --extcmd cat arg2' ' echo branch >expect && git difftool --no-prompt \ - --extcmd sh\ -c\ \"cat\ \$2\" branch >actual && + --extcmd sh\ -c\ \"cat\ \\\"\$2\\\"\" branch >actual && test_cmp expect actual ' From 0afcdab82872fb2802c28b8c36d845f8661ffe8c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 7 Nov 2018 06:00:48 -0800 Subject: [PATCH 201/406] built-in rebase: demonstrate regression with --autostash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An unnamed colleague of Ævar Arnfjörð Bjarmason reported a breakage where a `pull --rebase` (which did not really need to do anything but stash, see that nothing was changed, and apply the stash again) also detached the HEAD. This patch adds a minimal reproducer for this regression. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t3420-rebase-autostash.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh index f355c6825a4a5f..d4e2520bcb5575 100755 --- a/t/t3420-rebase-autostash.sh +++ b/t/t3420-rebase-autostash.sh @@ -361,4 +361,12 @@ test_expect_success 'autostash with dirty submodules' ' git rebase -i --autostash HEAD ' +test_expect_failure 'branch is left alone when possible' ' + git checkout -b unchanged-branch && + echo changed >file0 && + git rebase --autostash unchanged-branch && + test changed = "$(cat file0)" && + test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)" +' + test_done From 8c579562c0ffb72e96f80bff4ef34cf554e52604 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 03:44:31 -0800 Subject: [PATCH 202/406] rebase: prepare reset_head() for more flags Currently, we only accept the flag indicating whether the HEAD should be detached not. In the next commit, we want to introduce another flag: to toggle between emulating `reset --hard` vs `checkout -q`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 3c302da0f177bb..2a63555006ce9c 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -523,10 +523,13 @@ static int run_specific_rebase(struct rebase_options *opts) #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" +#define RESET_HEAD_DETACH (1<<0) + static int reset_head(struct object_id *oid, const char *action, - const char *switch_to_branch, int detach_head, + const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head) { + unsigned detach_head = flags & RESET_HEAD_DETACH; struct object_id head_oid; struct tree_desc desc; struct lock_file lock = LOCK_INIT; @@ -1502,8 +1505,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) "it...\n")); strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); - if (reset_head(&options.onto->object.oid, "checkout", NULL, 1, - NULL, msg.buf)) + if (reset_head(&options.onto->object.oid, "checkout", NULL, + RESET_HEAD_DETACH, NULL, msg.buf)) die(_("Could not detach HEAD")); strbuf_release(&msg); From dc16f149b95e8ea4c797b3d1bd7fd413c4f78e7e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 13 Mar 2018 19:11:27 +0100 Subject: [PATCH 203/406] mingw: reencode environment variables on the fly (UTF-16 <-> UTF-8) On Windows, the authoritative environment is encoded in UTF-16. In Git for Windows, we convert that to UTF-8 (because UTF-16 is *such* a foreign idea to Git that its source code is unprepared for it). Previously, out of performance concerns, we converted the entire environment to UTF-8 in one fell swoop at the beginning, and upon putenv() and run_command() converted it back. Having a private copy of the environment comes with its own perils: when a library used by Git's source code tries to modify the environment, it does not really work (in Git for Windows' case, libcurl, see https://github.com/git-for-windows/git/compare/bcad1e6d58^...bcad1e6d58^2 for a glimpse of the issues). Hence, it makes our environment handling substantially more robust if we switch to on-the-fly-conversion in `getenv()`/`putenv()` calls. Based on an initial version in the MSVC context by Jeff Hostetler, this patch makes it so. Surprisingly, this has a *positive* effect on speed: at the time when the current code was written, we tested the performance, and there were *so many* `getenv()` calls that it seemed better to convert everything in one go. In the meantime, though, Git has obviously been cleaned up a bit with regards to `getenv()` calls so that the Git processes spawned by the test suite use an average of only 40 `getenv()`/`putenv()` calls over the process lifetime. Speaking of the entire test suite: the total time spent in the re-encoding in the current code takes about 32.4 seconds (out of 113 minutes runtime), whereas the code introduced in this patch takes only about 8.2 seconds in total. Not much, but it proves that we need not be concerned about the performance impact introduced by this patch. Helped-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 280 +++++++++++++++++++++++++++++-------------------- compat/mingw.h | 32 +++++- 2 files changed, 196 insertions(+), 116 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..c1de85f11893bf 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1040,44 +1040,121 @@ static char *path_lookup(const char *cmd, int exe_only) return prog; } -static int do_putenv(char **env, const char *name, int size, int free_old); +static const wchar_t *wcschrnul(const wchar_t *s, wchar_t c) +{ + while (*s && *s != c) + s++; + return s; +} + +/* Compare only keys */ +static int wenvcmp(const void *a, const void *b) +{ + wchar_t *p = *(wchar_t **)a, *q = *(wchar_t **)b; + size_t p_len, q_len; + + /* Find the keys */ + p_len = wcschrnul(p, L'=') - p; + q_len = wcschrnul(q, L'=') - q; + + /* If the length differs, include the shorter key's NUL */ + if (p_len < q_len) + p_len++; + else if (p_len > q_len) + p_len = q_len + 1; + + return _wcsnicmp(p, q, p_len); +} -/* used number of elements of environ array, including terminating NULL */ -static int environ_size = 0; -/* allocated size of environ array, in bytes */ -static int environ_alloc = 0; +/* We need a stable sort to convert the environment between UTF-16 <-> UTF-8 */ +#ifndef INTERNAL_QSORT +#include "qsort.c" +#endif /* - * Create environment block suitable for CreateProcess. Merges current - * process environment and the supplied environment changes. + * Build an environment block combining the inherited environment + * merged with the given list of settings. + * + * Values of the form "KEY=VALUE" in deltaenv override inherited values. + * Values of the form "KEY" in deltaenv delete inherited values. + * + * Multiple entries in deltaenv for the same key are explicitly allowed. + * + * We return a contiguous block of UNICODE strings with a final trailing + * zero word. */ static wchar_t *make_environment_block(char **deltaenv) { - wchar_t *wenvblk = NULL; - char **tmpenv; - int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0; + wchar_t *wenv = GetEnvironmentStringsW(), *wdeltaenv, *result, *p; + size_t wlen, s, delta_size, size; + + wchar_t **array = NULL; + size_t alloc = 0, nr = 0, i; + + size = 1; /* for extra NUL at the end */ + + /* If there is no deltaenv to apply, simply return a copy. */ + if (!deltaenv || !*deltaenv) { + for (p = wenv; p && *p; ) { + size_t s = wcslen(p) + 1; + size += s; + p += s; + } + + ALLOC_ARRAY(result, size); + memcpy(result, wenv, size * sizeof(*wenv)); + FreeEnvironmentStringsW(wenv); + return result; + } + + /* + * If there is a deltaenv, let's accumulate all keys into `array`, + * sort them using the stable git_qsort() and then copy, skipping + * duplicate keys + */ + for (p = wenv; p && *p; ) { + ALLOC_GROW(array, nr + 1, alloc); + s = wcslen(p) + 1; + array[nr++] = p; + p += s; + size += s; + } + + /* (over-)assess size needed for wchar version of deltaenv */ + for (delta_size = 0, i = 0; deltaenv[i]; i++) + delta_size += strlen(deltaenv[i]) * 2 + 1; + ALLOC_ARRAY(wdeltaenv, delta_size); + + /* convert the deltaenv, appending to array */ + for (i = 0, p = wdeltaenv; deltaenv[i]; i++) { + ALLOC_GROW(array, nr + 1, alloc); + wlen = xutftowcs(p, deltaenv[i], wdeltaenv + delta_size - p); + array[nr++] = p; + p += wlen + 1; + } - while (deltaenv && deltaenv[i]) - i++; + git_qsort(array, nr, sizeof(*array), wenvcmp); + ALLOC_ARRAY(result, size + delta_size); - /* copy the environment, leaving space for changes */ - ALLOC_ARRAY(tmpenv, size + i); - memcpy(tmpenv, environ, size * sizeof(char*)); + for (p = result, i = 0; i < nr; i++) { + /* Skip any duplicate keys; last one wins */ + while (i + 1 < nr && !wenvcmp(array + i, array + i + 1)) + i++; - /* merge supplied environment changes into the temporary environment */ - for (i = 0; deltaenv && deltaenv[i]; i++) - size = do_putenv(tmpenv, deltaenv[i], size, 0); + /* Skip "to delete" entry */ + if (!wcschr(array[i], L'=')) + continue; - /* create environment block from temporary environment */ - for (i = 0; tmpenv[i]; i++) { - size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */ - ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz); - wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1; + size = wcslen(array[i]) + 1; + memcpy(p, array[i], size * sizeof(*p)); + p += size; } - /* add final \0 terminator */ - wenvblk[wenvpos] = 0; - free(tmpenv); - return wenvblk; + *p = L'\0'; + + free(array); + free(wdeltaenv); + FreeEnvironmentStringsW(wenv); + return result; } struct pinfo_t { @@ -1319,87 +1396,83 @@ int mingw_kill(pid_t pid, int sig) } /* - * Compare environment entries by key (i.e. stopping at '=' or '\0'). + * UTF-8 versions of getenv(), putenv() and unsetenv(). + * Internally, they use the CRT's stock UNICODE routines + * to avoid data loss. */ -static int compareenv(const void *v1, const void *v2) +char *mingw_getenv(const char *name) { - const char *e1 = *(const char**)v1; - const char *e2 = *(const char**)v2; +#define GETENV_MAX_RETAIN 30 + static char *values[GETENV_MAX_RETAIN]; + static int value_counter; + int len_key, len_value; + wchar_t *w_key; + char *value; + wchar_t w_value[32768]; - for (;;) { - int c1 = *e1++; - int c2 = *e2++; - c1 = (c1 == '=') ? 0 : tolower(c1); - c2 = (c2 == '=') ? 0 : tolower(c2); - if (c1 > c2) - return 1; - if (c1 < c2) - return -1; - if (c1 == 0) - return 0; - } -} + if (!name || !*name) + return NULL; -static int bsearchenv(char **env, const char *name, size_t size) -{ - unsigned low = 0, high = size; - while (low < high) { - unsigned mid = low + ((high - low) >> 1); - int cmp = compareenv(&env[mid], &name); - if (cmp < 0) - low = mid + 1; - else if (cmp > 0) - high = mid; - else - return mid; + len_key = strlen(name) + 1; + /* We cannot use xcalloc() here because that uses getenv() itself */ + w_key = calloc(len_key, sizeof(wchar_t)); + if (!w_key) + die("Out of memory, (tried to allocate %u wchar_t's)", len_key); + xutftowcs(w_key, name, len_key); + len_value = GetEnvironmentVariableW(w_key, w_value, ARRAY_SIZE(w_value)); + if (!len_value && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { + free(w_key); + return NULL; } - return ~low; /* not found, return 1's complement of insert position */ + free(w_key); + + len_value = len_value * 3 + 1; + /* We cannot use xcalloc() here because that uses getenv() itself */ + value = calloc(len_value, sizeof(char)); + if (!value) + die("Out of memory, (tried to allocate %u bytes)", len_value); + xwcstoutf(value, w_value, len_value); + + /* + * We return `value` which is an allocated value and the caller is NOT + * expecting to have to free it, so we keep a round-robin array, + * invalidating the buffer after GETENV_MAX_RETAIN getenv() calls. + */ + free(values[value_counter]); + values[value_counter++] = value; + if (value_counter >= ARRAY_SIZE(values)) + value_counter = 0; + + return value; } -/* - * If name contains '=', then sets the variable, otherwise it unsets it - * Size includes the terminating NULL. Env must have room for size + 1 entries - * (in case of insert). Returns the new size. Optionally frees removed entries. - */ -static int do_putenv(char **env, const char *name, int size, int free_old) +int mingw_putenv(const char *namevalue) { - int i = bsearchenv(env, name, size - 1); + int size; + wchar_t *wide, *equal; + BOOL result; - /* optionally free removed / replaced entry */ - if (i >= 0 && free_old) - free(env[i]); + if (!namevalue || !*namevalue) + return 0; - if (strchr(name, '=')) { - /* if new value ('key=value') is specified, insert or replace entry */ - if (i < 0) { - i = ~i; - memmove(&env[i + 1], &env[i], (size - i) * sizeof(char*)); - size++; - } - env[i] = (char*) name; - } else if (i >= 0) { - /* otherwise ('key') remove existing entry */ - size--; - memmove(&env[i], &env[i + 1], (size - i) * sizeof(char*)); + size = strlen(namevalue) * 2 + 1; + wide = calloc(size, sizeof(wchar_t)); + if (!wide) + die("Out of memory, (tried to allocate %u wchar_t's)", size); + xutftowcs(wide, namevalue, size); + equal = wcschr(wide, L'='); + if (!equal) + result = SetEnvironmentVariableW(wide, NULL); + else { + *equal = L'\0'; + result = SetEnvironmentVariableW(wide, equal + 1); } - return size; -} + free(wide); -char *mingw_getenv(const char *name) -{ - char *value; - int pos = bsearchenv(environ, name, environ_size - 1); - if (pos < 0) - return NULL; - value = strchr(environ[pos], '='); - return value ? &value[1] : NULL; -} + if (!result) + errno = err_win_to_posix(GetLastError()); -int mingw_putenv(const char *namevalue) -{ - ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc); - environ_size = do_putenv(environ, namevalue, environ_size, 1); - return 0; + return result ? 0 : -1; } /* @@ -2261,17 +2334,6 @@ void mingw_startup(void) maxlen = wcslen(wargv[0]); for (i = 1; i < argc; i++) maxlen = max(maxlen, wcslen(wargv[i])); - for (i = 0; wenv[i]; i++) - maxlen = max(maxlen, wcslen(wenv[i])); - - /* - * nedmalloc can't free CRT memory, allocate resizable environment - * list. Note that xmalloc / xmemdupz etc. call getenv, so we cannot - * use it while initializing the environment itself. - */ - environ_size = i + 1; - environ_alloc = alloc_nr(environ_size * sizeof(char*)); - environ = malloc_startup(environ_alloc); /* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */ maxlen = 3 * maxlen + 1; @@ -2280,14 +2342,8 @@ void mingw_startup(void) /* convert command line arguments and environment to UTF-8 */ for (i = 0; i < argc; i++) __argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen); - for (i = 0; wenv[i]; i++) - environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen); - environ[i] = NULL; free(buffer); - /* sort environment for O(log n) getenv / putenv */ - qsort(environ, i, sizeof(char*), compareenv); - /* fix Windows specific environment settings */ setup_windows_environment(); diff --git a/compat/mingw.h b/compat/mingw.h index 571019d0bddcea..be6f3a0e26f495 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -257,11 +257,35 @@ char *mingw_mktemp(char *template); char *mingw_getcwd(char *pointer, int len); #define getcwd mingw_getcwd +#ifdef NO_UNSETENV +#error "NO_UNSETENV is incompatible with the Windows-specific startup code!" +#endif + +/* + * We bind *env() routines (even the mingw_ ones) to private mingw_ versions. + * These talk to the CRT using UNICODE/wchar_t, but maintain the original + * narrow-char API. + * + * Note that the MSCRT maintains both ANSI (getenv()) and UNICODE (_wgetenv()) + * routines and stores both versions of each environment variable in parallel + * (and secretly updates both when you set one or the other), but it uses CP_ACP + * to do the conversion rather than CP_UTF8. + * + * Since everything in the git code base is UTF8, we define the mingw_ routines + * to access the CRT using the UNICODE routines and manually convert them to + * UTF8. This also avoids round-trip problems. + * + * This also helps with our linkage, since "_wenviron" is publicly exported + * from the CRT. But to access "_environ" we would have to statically link + * to the CRT (/MT). + * + * We require NO_SETENV (and let gitsetenv() call our mingw_putenv). + */ +#define getenv mingw_getenv +#define putenv mingw_putenv +#define unsetenv mingw_putenv char *mingw_getenv(const char *name); -#define getenv mingw_getenv -int mingw_putenv(const char *namevalue); -#define putenv mingw_putenv -#define unsetenv mingw_putenv +int mingw_putenv(const char *name); int mingw_gethostname(char *host, int namelen); #define gethostname mingw_gethostname From c7b199eeac521893f4edb1fc8383aa1cbb32bdc5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 23 Oct 2018 12:57:17 -0700 Subject: [PATCH 204/406] rebase --autostash: fix issue with dirty submodules Since we cannot stash dirty submodules, there is no use in requiring them to be clean (or stash them when they are not). This brings the built-in rebase in line with the previous, scripted version, which also did not care about dirty submodules (but it was admittedly not very easy to figure that out). This fixes https://github.com/git-for-windows/git/issues/1820 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 2 +- t/t3420-rebase-autostash.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 4c664f958ce49d..d184038d0c10da 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1352,7 +1352,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) update_index_if_able(&the_index, &lock_file); rollback_lock_file(&lock_file); - if (has_unstaged_changes(0) || has_uncommitted_changes(0)) { + if (has_unstaged_changes(1) || has_uncommitted_changes(1)) { const char *autostash = state_dir_path("autostash", &options); struct child_process stash = CHILD_PROCESS_INIT; diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh index 7eb9f904191da5..f355c6825a4a5f 100755 --- a/t/t3420-rebase-autostash.sh +++ b/t/t3420-rebase-autostash.sh @@ -351,7 +351,7 @@ test_expect_success 'autostash is saved on editor failure with conflict' ' test_cmp expected file0 ' -test_expect_failure 'autostash with dirty submodules' ' +test_expect_success 'autostash with dirty submodules' ' test_when_finished "git reset --hard && git checkout master" && git checkout -b with-submodule && git submodule add ./ sub && From 63c9f82ff2d059ead14eb482b7c23ba2586690fd Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 7 Nov 2018 06:00:50 -0800 Subject: [PATCH 205/406] built-in rebase --autostash: leave the current branch alone if possible When we converted a `git reset --hard` call in the original Unix shell script to built-in code, we asked to reset the worktree and the index and explicitly *not* to detach the HEAD. By mistake, though, we still did. Let's fix this. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 3 ++- t/t3420-rebase-autostash.sh | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index d184038d0c10da..a40e312d54617e 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -614,7 +614,8 @@ static int reset_head(struct object_id *oid, const char *action, reflog_head = msg.buf; } if (!switch_to_branch) - ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF, + ret = update_ref(reflog_head, "HEAD", oid, orig, + detach_head ? REF_NO_DEREF : 0, UPDATE_REFS_MSG_ON_ERR); else { ret = create_symref("HEAD", switch_to_branch, msg.buf); diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh index d4e2520bcb5575..4c7494cc8f77a3 100755 --- a/t/t3420-rebase-autostash.sh +++ b/t/t3420-rebase-autostash.sh @@ -361,7 +361,7 @@ test_expect_success 'autostash with dirty submodules' ' git rebase -i --autostash HEAD ' -test_expect_failure 'branch is left alone when possible' ' +test_expect_success 'branch is left alone when possible' ' git checkout -b unchanged-branch && echo changed >file0 && git rebase --autostash unchanged-branch && From aba8705ea3284c5a00117bec96a788d63f342c26 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 03:44:32 -0800 Subject: [PATCH 206/406] built-in rebase: reinstate `checkout -q` behavior where appropriate When we converted a `git checkout -q $onto^0` call to use `reset_head()`, we inadvertently incurred a change from a twoway_merge to a oneway_merge, as if we wanted a `git reset --hard` instead. This has performance ramifications under certain, though, as the oneway_merge needs to lstat() every single index entry whereas twoway_merge does not. So let's go back to the old behavior. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 2a63555006ce9c..fdba33c0e9da68 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -524,14 +524,16 @@ static int run_specific_rebase(struct rebase_options *opts) #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" #define RESET_HEAD_DETACH (1<<0) +#define RESET_HEAD_HARD (1<<1) static int reset_head(struct object_id *oid, const char *action, const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head) { unsigned detach_head = flags & RESET_HEAD_DETACH; + unsigned reset_hard = flags & RESET_HEAD_HARD; struct object_id head_oid; - struct tree_desc desc; + struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; struct unpack_trees_options unpack_tree_opts; struct tree *tree; @@ -540,7 +542,7 @@ static int reset_head(struct object_id *oid, const char *action, size_t prefix_len; struct object_id *orig = NULL, oid_orig, *old_orig = NULL, oid_old_orig; - int ret = 0; + int ret = 0, nr = 0; if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) BUG("Not a fully qualified branch: '%s'", switch_to_branch); @@ -550,20 +552,20 @@ static int reset_head(struct object_id *oid, const char *action, goto leave_reset_head; } - if (!oid) { - if (get_oid("HEAD", &head_oid)) { - ret = error(_("could not determine HEAD revision")); - goto leave_reset_head; - } - oid = &head_oid; + if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { + ret = error(_("could not determine HEAD revision")); + goto leave_reset_head; } + if (!oid) + oid = &head_oid; + memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); setup_unpack_trees_porcelain(&unpack_tree_opts, action); unpack_tree_opts.head_idx = 1; unpack_tree_opts.src_index = the_repository->index; unpack_tree_opts.dst_index = the_repository->index; - unpack_tree_opts.fn = oneway_merge; + unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; unpack_tree_opts.update = 1; unpack_tree_opts.merge = 1; if (!detach_head) @@ -574,12 +576,17 @@ static int reset_head(struct object_id *oid, const char *action, goto leave_reset_head; } - if (!fill_tree_descriptor(&desc, oid)) { + if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) { + ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); + goto leave_reset_head; + } + + if (!fill_tree_descriptor(&desc[nr++], oid)) { ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); goto leave_reset_head; } - if (unpack_trees(1, &desc, &unpack_tree_opts)) { + if (unpack_trees(nr, desc, &unpack_tree_opts)) { ret = -1; goto leave_reset_head; } @@ -627,7 +634,8 @@ static int reset_head(struct object_id *oid, const char *action, leave_reset_head: strbuf_release(&msg); rollback_lock_file(&lock); - free((void *)desc.buffer); + while (nr) + free((void *)desc[--nr].buffer); return ret; } @@ -1005,7 +1013,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) rerere_clear(&merge_rr); string_list_clear(&merge_rr, 1); - if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0) + if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD, + NULL, NULL) < 0) die(_("could not discard worktree changes")); if (read_basic_state(&options)) exit(1); @@ -1021,7 +1030,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (read_basic_state(&options)) exit(1); if (reset_head(&options.orig_head, "reset", - options.head_name, 0, NULL, NULL) < 0) + options.head_name, RESET_HEAD_HARD, + NULL, NULL) < 0) die(_("could not move back to %s"), oid_to_hex(&options.orig_head)); ret = finish_rebase(&options); @@ -1385,7 +1395,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) write_file(autostash, "%s", oid_to_hex(&oid)); printf(_("Created autostash: %s\n"), buf.buf); if (reset_head(&head->object.oid, "reset --hard", - NULL, 0, NULL, NULL) < 0) + NULL, RESET_HEAD_HARD, NULL, NULL) < 0) die(_("could not reset --hard")); printf(_("HEAD is now at %s"), find_unique_abbrev(&head->object.oid, From ed009d988b43e9d9821448fd0fc0ef9416f208c5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 19 Oct 2018 11:24:07 +0200 Subject: [PATCH 207/406] mingw: factor out code to set stat() data In our fstat() emulation, we convert the file metadata from Win32 data structures to an emulated POSIX structure. To structure the code better, let's factor that part out into its own function. Note: it would be tempting to try to unify this code with the part of do_lstat() that does the same thing, but they operate on different data structures: BY_HANDLE_FILE_INFORMATION vs WIN32_FILE_ATTRIBUTE_DATA. So unfortunately, they cannot be unified. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a573510..1c3e07c4b9a493 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -706,6 +706,29 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf) return do_lstat(follow, alt_name, buf); } +static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) +{ + BY_HANDLE_FILE_INFORMATION fdata; + + if (!GetFileInformationByHandle(hnd, &fdata)) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + + buf->st_ino = 0; + buf->st_gid = 0; + buf->st_uid = 0; + buf->st_nlink = 1; + buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); + buf->st_size = fdata.nFileSizeLow | + (((off_t)fdata.nFileSizeHigh)<<32); + buf->st_dev = buf->st_rdev = 0; /* not used by Git */ + buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); + buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); + buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + return 0; +} + int mingw_lstat(const char *file_name, struct stat *buf) { return do_stat_internal(0, file_name, buf); @@ -718,7 +741,6 @@ int mingw_stat(const char *file_name, struct stat *buf) int mingw_fstat(int fd, struct stat *buf) { HANDLE fh = (HANDLE)_get_osfhandle(fd); - BY_HANDLE_FILE_INFORMATION fdata; if (fh == INVALID_HANDLE_VALUE) { errno = EBADF; @@ -728,20 +750,9 @@ int mingw_fstat(int fd, struct stat *buf) if (GetFileType(fh) != FILE_TYPE_DISK) return _fstati64(fd, buf); - if (GetFileInformationByHandle(fh, &fdata)) { - buf->st_ino = 0; - buf->st_gid = 0; - buf->st_uid = 0; - buf->st_nlink = 1; - buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); - buf->st_size = fdata.nFileSizeLow | - (((off_t)fdata.nFileSizeHigh)<<32); - buf->st_dev = buf->st_rdev = 0; /* not used by Git */ - buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); - buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); - buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + if (!get_file_info_by_handle(fh, buf)) return 0; - } + errno = EBADF; return -1; } From be712c5ee08663d51bcd08cd5224a884d08557a0 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Fri, 25 Sep 2015 18:02:55 +0200 Subject: [PATCH 208/406] mingw: replace MSVCRT's fstat() with a Win32-based implementation fstat() is the only stat-related CRT function for which we don't have a full replacement yet (and thus the only reason to stick with MSVCRT's 'struct stat' definition). Fully implement fstat(), in preparation of implementing a POSIX 2013 compatible 'struct stat' with nanosecond-precision file times. This allows us also to implement some clever code to handle pipes and character devices in our own way. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 1c3e07c4b9a493..10939bb48d217f 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -741,20 +741,31 @@ int mingw_stat(const char *file_name, struct stat *buf) int mingw_fstat(int fd, struct stat *buf) { HANDLE fh = (HANDLE)_get_osfhandle(fd); + DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE; - if (fh == INVALID_HANDLE_VALUE) { - errno = EBADF; - return -1; - } - /* direct non-file handles to MS's fstat() */ - if (GetFileType(fh) != FILE_TYPE_DISK) - return _fstati64(fd, buf); + switch (type) { + case FILE_TYPE_DISK: + return get_file_info_by_handle(fh, buf); - if (!get_file_info_by_handle(fh, buf)) + case FILE_TYPE_CHAR: + case FILE_TYPE_PIPE: + /* initialize stat fields */ + memset(buf, 0, sizeof(*buf)); + buf->st_nlink = 1; + + if (type == FILE_TYPE_CHAR) { + buf->st_mode = _S_IFCHR; + } else { + buf->st_mode = _S_IFIFO; + if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL)) + buf->st_size = avail; + } return 0; - errno = EBADF; - return -1; + default: + errno = EBADF; + return -1; + } } static inline void time_t_to_filetime(time_t t, FILETIME *ft) From 3d28f8fc444aa98378e54fdf4a129bfc51a5a5e1 Mon Sep 17 00:00:00 2001 From: Anton Serbulov <aserbulov@plesk.com> Date: Fri, 15 Jan 2016 12:25:02 +0600 Subject: [PATCH 209/406] mingw: fix getcwd when the parent directory cannot be queried `GetLongPathName()` function may fail when it is unable to query the parent directory of a path component to determine the long name for that component. It happens, because of it uses `FindFirstFile()` function for each next short part of path. The `FindFirstFile()` requires `List Directory` and `Synchronize` desired access for a calling process. In case of lacking such permission for some part of path, the `GetLongPathName()` returns 0 as result and `GetLastError()` returns ERROR_ACCESS_DENIED. `GetFinalPathNameByHandle()` function can help in such cases, because it requires `Read Attributes` and `Synchronize` desired access to the target path only. The `GetFinalPathNameByHandle()` function was introduced on `Windows Server 2008/Windows Vista`. So we need to load it dynamically. `CreateFile()` parameters: `lpFileName` = path to the current directory `dwDesiredAccess` = 0 (it means `Read Attributes` and `Synchronize`) `dwShareMode` = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE (it prevents `Sharing Violation`) `lpSecurityAttributes` = NULL (default security attributes) `dwCreationDisposition` = OPEN_EXISTING (required to obtain a directory handle) `dwFlagsAndAttributes` = FILE_FLAG_BACKUP_SEMANTICS (required to obtain a directory handle) `hTemplateFile` = NULL (when opening an existing file or directory, `CreateFile` ignores this parameter) The string that is returned by `GetFinalPathNameByHandle()` function uses the \\?\ syntax. To skip the prefix and convert backslashes to slashes, the `normalize_ntpath()` mingw function will be used. Note: `GetFinalPathNameByHandle()` function returns a final path. It is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that points to "D:\yourdir", the final path would be "D:\yourdir". Signed-off-by: Anton Serbulov <aserbulov@plesk.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 8e6bf79a1a93a5..23734982bac171 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -202,6 +202,31 @@ static int ask_yes_no_if_possible(const char *format, ...) } } +/* Normalizes NT paths as returned by some low-level APIs. */ +static wchar_t *normalize_ntpath(wchar_t *wbuf) +{ + int i; + /* fix absolute path prefixes */ + if (wbuf[0] == '\\') { + /* strip NT namespace prefixes */ + if (!wcsncmp(wbuf, L"\\??\\", 4) || + !wcsncmp(wbuf, L"\\\\?\\", 4)) + wbuf += 4; + else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12)) + wbuf += 12; + /* replace remaining '...UNC\' with '\\' */ + if (!wcsnicmp(wbuf, L"UNC\\", 4)) { + wbuf += 2; + *wbuf = '\\'; + } + } + /* convert backslashes to slashes */ + for (i = 0; wbuf[i]; i++) + if (wbuf[i] == '\\') + wbuf[i] = '/'; + return wbuf; +} + int mingw_unlink(const char *pathname) { int ret, tries = 0; @@ -895,6 +920,20 @@ char *mingw_getcwd(char *pointer, int len) return NULL; } ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer)); + if (!ret && GetLastError() == ERROR_ACCESS_DENIED) { + HANDLE hnd = CreateFileW(cwd, 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (hnd == INVALID_HANDLE_VALUE) + return NULL; + ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0); + CloseHandle(hnd); + if (!ret || ret >= ARRAY_SIZE(wpointer)) + return NULL; + if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0) + return NULL; + return pointer; + } if (!ret || ret >= ARRAY_SIZE(wpointer)) return NULL; if (xwcstoutf(pointer, wpointer, len) < 0) From cdbc898f5a65472533bb8571df154dccd2fe3fc4 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 26 Sep 2015 02:03:28 +0200 Subject: [PATCH 210/406] mingw: implement nanosecond-precision file times We no longer use any of MSVCRT's stat-functions, so there's no need to stick to a CRT-compatible 'struct stat' either. Define and use our own POSIX-2013-compatible 'struct stat' with nanosecond- precision file times. Note: This can cause performance issues when using Git variants with different file time resolutions, as the timestamps are stored in the Git index: after updating the index with a Git variant that uses second-precision file times, a nanosecond-aware Git will think that pretty much every single file listed in the index is out of date. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 18 ++++++++++-------- compat/mingw.h | 36 ++++++++++++++++++++++++++---------- config.mak.uname | 2 -- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 10939bb48d217f..3783afdb0221ac 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -562,9 +562,11 @@ static inline long long filetime_to_hnsec(const FILETIME *ft) return winTime - 116444736000000000LL; } -static inline time_t filetime_to_time_t(const FILETIME *ft) +static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts) { - return (time_t)(filetime_to_hnsec(ft) / 10000000); + long long hnsec = filetime_to_hnsec(ft); + ts->tv_sec = (time_t)(hnsec / 10000000); + ts->tv_nsec = (hnsec % 10000000) * 100; } /** @@ -623,9 +625,9 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) buf->st_size = fdata.nFileSizeLow | (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ - buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); - buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); - buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); + filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); + filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim)); if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { WIN32_FIND_DATAW findbuf; HANDLE handle = FindFirstFileW(wfilename, &findbuf); @@ -723,9 +725,9 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) buf->st_size = fdata.nFileSizeLow | (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ - buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); - buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); - buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); + filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); + filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim)); return 0; } diff --git a/compat/mingw.h b/compat/mingw.h index 571019d0bddcea..9419b27e1275e0 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -327,18 +327,41 @@ static inline int getrlimit(int resource, struct rlimit *rlp) } /* - * Use mingw specific stat()/lstat()/fstat() implementations on Windows. + * Use mingw specific stat()/lstat()/fstat() implementations on Windows, + * including our own struct stat with 64 bit st_size and nanosecond-precision + * file times. */ #ifndef __MINGW64_VERSION_MAJOR #define off_t off64_t #define lseek _lseeki64 +struct timespec { + time_t tv_sec; + long tv_nsec; +}; #endif -/* use struct stat with 64 bit st_size */ +struct mingw_stat { + _dev_t st_dev; + _ino_t st_ino; + _mode_t st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + off64_t st_size; + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; +}; + +#define st_atime st_atim.tv_sec +#define st_mtime st_mtim.tv_sec +#define st_ctime st_ctim.tv_sec + #ifdef stat #undef stat #endif -#define stat _stati64 +#define stat mingw_stat int mingw_lstat(const char *file_name, struct stat *buf); int mingw_stat(const char *file_name, struct stat *buf); int mingw_fstat(int fd, struct stat *buf); @@ -351,13 +374,6 @@ int mingw_fstat(int fd, struct stat *buf); #endif #define lstat mingw_lstat -#ifndef _stati64 -# define _stati64(x,y) mingw_stat(x,y) -#elif defined (_USE_32BIT_TIME_T) -# define _stat32i64(x,y) mingw_stat(x,y) -#else -# define _stat64(x,y) mingw_stat(x,y) -#endif int mingw_utime(const char *file_name, const struct utimbuf *times); #define utime mingw_utime diff --git a/config.mak.uname b/config.mak.uname index e47af72e018fc3..912576f7ae1378 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -370,7 +370,6 @@ ifeq ($(uname_S),Windows) RUNTIME_PREFIX = YesPlease HAVE_WPGMPTR = YesWeDo NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease - NO_NSEC = YesPlease USE_WIN32_MMAP = YesPlease MMAP_PREVENTS_DELETE = UnfortunatelyYes # USE_NED_ALLOCATOR = YesPlease @@ -520,7 +519,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) RUNTIME_PREFIX = YesPlease HAVE_WPGMPTR = YesWeDo NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease - NO_NSEC = YesPlease USE_WIN32_MMAP = YesPlease MMAP_PREVENTS_DELETE = UnfortunatelyYes USE_NED_ALLOCATOR = YesPlease From 60ead4e2ad0072648a28ef81268c2b2e49f78b4a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 17 Nov 2016 17:24:10 +0100 Subject: [PATCH 211/406] Allow for platform-specific core.* config settings In the Git for Windows project, we have ample precendent for config settings that apply to Windows, and to Windows only. Let's formalize this concept by introducing a platform_core_config() function that can be #define'd in a platform-specific manner. This will allow us to contain platform-specific code better, as the corresponding variables no longer need to be exported so that they can be defined in environment.c and be set in config.c Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 5 +++++ compat/mingw.h | 3 +++ config.c | 6 +++--- git-compat-util.h | 8 ++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 23734982bac171..570313e3a23cda 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -202,6 +202,11 @@ static int ask_yes_no_if_possible(const char *format, ...) } } +int mingw_core_config(const char *var, const char *value, void *cb) +{ + return 0; +} + /* Normalizes NT paths as returned by some low-level APIs. */ static wchar_t *normalize_ntpath(wchar_t *wbuf) { diff --git a/compat/mingw.h b/compat/mingw.h index 571019d0bddcea..908279decc0c83 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -11,6 +11,9 @@ typedef _sigset_t sigset_t; #undef _POSIX_THREAD_SAFE_FUNCTIONS #endif +extern int mingw_core_config(const char *var, const char *value, void *cb); +#define platform_core_config mingw_core_config + /* * things that are not available in header files */ diff --git a/config.c b/config.c index f2d89ae2e7c37a..8b3e0f0c66de47 100644 --- a/config.c +++ b/config.c @@ -1093,7 +1093,7 @@ int git_config_color(char *dest, const char *var, const char *value) return 0; } -static int git_default_core_config(const char *var, const char *value) +static int git_default_core_config(const char *var, const char *value, void *cb) { /* This needs a better name */ if (!strcmp(var, "core.filemode")) { @@ -1363,7 +1363,7 @@ static int git_default_core_config(const char *var, const char *value) } /* Add other config variables here and to Documentation/config.txt. */ - return 0; + return platform_core_config(var, value, cb); } static int git_default_i18n_config(const char *var, const char *value) @@ -1451,7 +1451,7 @@ static int git_default_mailmap_config(const char *var, const char *value) int git_default_config(const char *var, const char *value, void *cb) { if (starts_with(var, "core.")) - return git_default_core_config(var, value); + return git_default_core_config(var, value, cb); if (starts_with(var, "user.")) return git_ident_config(var, value, cb); diff --git a/git-compat-util.h b/git-compat-util.h index 48c955541e2000..77deb0bc66c7ac 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -342,6 +342,14 @@ typedef uintmax_t timestamp_t; #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin" #endif +#ifndef platform_core_config +static inline int noop_core_config(const char *var, const char *value, void *cb) +{ + return 0; +} +#define platform_core_config noop_core_config +#endif + #ifndef has_dos_drive_prefix static inline int git_has_dos_drive_prefix(const char *path) { From e73f1f85efde831204be5784cd730602a26f7e19 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 17 Nov 2016 17:33:06 +0100 Subject: [PATCH 212/406] Move Windows-specific config settings into compat/mingw.c Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- cache.h | 8 -------- compat/mingw.c | 18 ++++++++++++++++++ config.c | 8 -------- environment.c | 1 - 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cache.h b/cache.h index 4d014541ab7bc7..f49d9fbac8c8ac 100644 --- a/cache.h +++ b/cache.h @@ -901,14 +901,6 @@ int use_optional_locks(void); extern char comment_line_char; extern int auto_comment_line_char; -/* Windows only */ -enum hide_dotfiles_type { - HIDE_DOTFILES_FALSE = 0, - HIDE_DOTFILES_TRUE, - HIDE_DOTFILES_DOTGITONLY -}; -extern enum hide_dotfiles_type hide_dotfiles; - enum log_refs_config { LOG_REFS_UNSET = -1, LOG_REFS_NONE = 0, diff --git a/compat/mingw.c b/compat/mingw.c index 570313e3a23cda..553f59dd41f4c7 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -5,6 +5,7 @@ #include "../strbuf.h" #include "../run-command.h" #include "../cache.h" +#include "../config.h" #define HCAST(type, handle) ((type)(intptr_t)handle) @@ -202,8 +203,25 @@ static int ask_yes_no_if_possible(const char *format, ...) } } +/* Windows only */ +enum hide_dotfiles_type { + HIDE_DOTFILES_FALSE = 0, + HIDE_DOTFILES_TRUE, + HIDE_DOTFILES_DOTGITONLY +}; + +static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; + int mingw_core_config(const char *var, const char *value, void *cb) { + if (!strcmp(var, "core.hidedotfiles")) { + if (value && !strcasecmp(value, "dotgitonly")) + hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; + else + hide_dotfiles = git_config_bool(var, value); + return 0; + } + return 0; } diff --git a/config.c b/config.c index 8b3e0f0c66de47..221de4bc0fb58f 100644 --- a/config.c +++ b/config.c @@ -1344,14 +1344,6 @@ static int git_default_core_config(const char *var, const char *value, void *cb) return 0; } - if (!strcmp(var, "core.hidedotfiles")) { - if (value && !strcasecmp(value, "dotgitonly")) - hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; - else - hide_dotfiles = git_config_bool(var, value); - return 0; - } - if (!strcmp(var, "core.partialclonefilter")) { return git_config_string(&core_partial_clone_filter_default, var, value); diff --git a/environment.c b/environment.c index 3f3c8746c2b6e3..30ecd4e784019b 100644 --- a/environment.c +++ b/environment.c @@ -71,7 +71,6 @@ int core_apply_sparse_checkout; int merge_log_config = -1; int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */ unsigned long pack_size_limit_cfg; -enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET; #ifndef PROTECT_HFS_DEFAULT From 35c1ad0cb81d8dc1157a8a453bc9276bc61f2bf6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 17 Nov 2016 18:05:04 +0100 Subject: [PATCH 213/406] mingw: unset PERL5LIB by default Git for Windows ships with its own Perl interpreter, and insists on using it, so it will most likely wreak havoc if PERL5LIB is set before launching Git. Let's just unset that environment variables when spawning processes. To make this feature extensible (and overrideable), there is a new config setting `core.unsetenvvars` that allows specifying a comma-separated list of names to unset before spawning processes. Reported by Gabriel Fuhrmann. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Documentation/config.txt | 6 ++++++ compat/mingw.c | 35 ++++++++++++++++++++++++++++++++++- t/t0029-core-unsetenvvars.sh | 30 ++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100755 t/t0029-core-unsetenvvars.sh diff --git a/Documentation/config.txt b/Documentation/config.txt index eb66a119753726..b25f1c0dc427dd 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -909,6 +909,12 @@ relatively high IO latencies. When enabled, Git will do the index comparison to the filesystem data in parallel, allowing overlapping IO's. Defaults to true. +core.unsetenvvars:: + EXPERIMENTAL, Windows-only: comma-separated list of environment + variables' names that need to be unset before spawning any other + process. Defaults to `PERL5LIB` to account for the fact that Git + for Windows insists on using its own Perl interpreter. + core.createObject:: You can set this to 'link', in which case a hardlink followed by a delete of the source are used to make sure that object creation diff --git a/compat/mingw.c b/compat/mingw.c index 553f59dd41f4c7..34731a47156e69 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -211,6 +211,7 @@ enum hide_dotfiles_type { }; static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; +static char *unset_environment_variables; int mingw_core_config(const char *var, const char *value, void *cb) { @@ -222,6 +223,12 @@ int mingw_core_config(const char *var, const char *value, void *cb) return 0; } + if (!strcmp(var, "core.unsetenvvars")) { + free(unset_environment_variables); + unset_environment_variables = xstrdup(value); + return 0; + } + return 0; } @@ -1149,6 +1156,27 @@ static wchar_t *make_environment_block(char **deltaenv) return wenvblk; } +static void do_unset_environment_variables(void) +{ + static int done; + char *p = unset_environment_variables; + + if (done || !p) + return; + done = 1; + + for (;;) { + char *comma = strchr(p, ','); + + if (comma) + *comma = '\0'; + unsetenv(p); + if (!comma) + break; + p = comma + 1; + } +} + struct pinfo_t { struct pinfo_t *next; pid_t pid; @@ -1167,9 +1195,12 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL; unsigned flags = CREATE_UNICODE_ENVIRONMENT; BOOL ret; + HANDLE cons; + + do_unset_environment_variables(); /* Determine whether or not we are associated to a console */ - HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE, + cons = CreateFile("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (cons == INVALID_HANDLE_VALUE) { @@ -2360,6 +2391,8 @@ void mingw_startup(void) /* fix Windows specific environment settings */ setup_windows_environment(); + unset_environment_variables = xstrdup("PERL5LIB"); + /* initialize critical section for waitpid pinfo_t list */ InitializeCriticalSection(&pinfo_cs); diff --git a/t/t0029-core-unsetenvvars.sh b/t/t0029-core-unsetenvvars.sh new file mode 100755 index 00000000000000..24ce46a6ea15cf --- /dev/null +++ b/t/t0029-core-unsetenvvars.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +test_description='test the Windows-only core.unsetenvvars setting' + +. ./test-lib.sh + +if ! test_have_prereq MINGW +then + skip_all='skipping Windows-specific tests' + test_done +fi + +test_expect_success 'setup' ' + mkdir -p "$TRASH_DIRECTORY/.git/hooks" && + write_script "$TRASH_DIRECTORY/.git/hooks/pre-commit" <<-\EOF + echo $HOBBES >&2 + EOF +' + +test_expect_success 'core.unsetenvvars works' ' + HOBBES=Calvin && + export HOBBES && + git commit --allow-empty -m with 2>err && + grep Calvin err && + git -c core.unsetenvvars=FINDUS,HOBBES,CALVIN \ + commit --allow-empty -m without 2>err && + ! grep Calvin err +' + +test_done From 155f6f89a7fc643cad3387b26c7ee12f7c17dc95 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 16:47:15 +0100 Subject: [PATCH 214/406] rebase -r: demonstrate bug with conflicting merges When calling `merge` on a branch that has already been merged, that `merge` is skipped quietly, but currently a MERGE_HEAD file is being left behind and will then be grabbed by the next `pick` (that did not want to create a *merge* commit). Demonstrate this. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t3430-rebase-merges.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh index aa7bfc88ece9de..1f08a336878c21 100755 --- a/t/t3430-rebase-merges.sh +++ b/t/t3430-rebase-merges.sh @@ -396,4 +396,20 @@ test_expect_success 'with --autosquash and --exec' ' grep "G: +G" actual ' +test_expect_failure '--continue after resolving conflicts after a merge' ' + git checkout -b already-has-g E && + git cherry-pick E..G && + test_commit H2 && + + git checkout -b conflicts-in-merge H && + test_commit H2 H2.t conflicts H2-conflict && + test_must_fail git rebase -r already-has-g && + grep conflicts H2.t && + echo resolved >H2.t && + git add -u && + git rebase --continue && + test_must_fail git rev-parse --verify HEAD^2 && + test_path_is_missing .git/MERGE_HEAD +' + test_done From 694d96d3a40cef0d4bf5ced4a234a7cf8df7a137 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 21:39:48 +0100 Subject: [PATCH 215/406] rebase -r: do not write MERGE_HEAD unless needed When we detect that a `merge` can be skipped because the merged commit is already an ancestor of HEAD, we do not need to commit, therefore writing the MERGE_HEAD file is useless. It is actually worse than useless: a subsequent `git commit` will pick it up and think that we want to merge that commit, still. To avoid that, move the code that writes the MERGE_HEAD file to a location where we already know that the `merge` cannot be skipped. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- sequencer.c | 8 ++++---- t/t3430-rebase-merges.sh | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sequencer.c b/sequencer.c index ad2193507be131..bec191aa692f87 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3184,10 +3184,6 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len, } merge_commit = to_merge->item; - write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ, - git_path_merge_head(the_repository), 0); - write_message("no-ff", 5, git_path_merge_mode(the_repository), 0); - bases = get_merge_bases(head_commit, merge_commit); if (bases && !oidcmp(&merge_commit->object.oid, &bases->item->object.oid)) { @@ -3196,6 +3192,10 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len, goto leave_merge; } + write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ, + git_path_merge_head(the_repository), 0); + write_message("no-ff", 5, git_path_merge_mode(the_repository), 0); + for (j = bases; j; j = j->next) commit_list_insert(j->item, &reversed); free_commit_list(bases); diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh index 1f08a336878c21..cc5646836fce1a 100755 --- a/t/t3430-rebase-merges.sh +++ b/t/t3430-rebase-merges.sh @@ -396,7 +396,7 @@ test_expect_success 'with --autosquash and --exec' ' grep "G: +G" actual ' -test_expect_failure '--continue after resolving conflicts after a merge' ' +test_expect_success '--continue after resolving conflicts after a merge' ' git checkout -b already-has-g E && git cherry-pick E..G && test_commit H2 && From a64b5ad5e65c3e75453542dc7a8690c5f4ef475f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 21:44:11 +0100 Subject: [PATCH 216/406] rebase -i: include MERGE_HEAD into files to clean up Every once in a while, the interactive rebase makes sure that no stale files are lying around. These days, we need to include MERGE_HEAD into that set of files, as the `merge` command will generate them. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- sequencer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sequencer.c b/sequencer.c index bec191aa692f87..98b41ae8908cd4 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3434,6 +3434,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) unlink(rebase_path_author_script()); unlink(rebase_path_stopped_sha()); unlink(rebase_path_amend()); + unlink(git_path_merge_head(the_repository)); delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); } if (item->command <= TODO_SQUASH) { @@ -3790,6 +3791,7 @@ static int commit_staged_changes(struct replay_opts *opts, opts, flags)) return error(_("could not commit staged changes.")); unlink(rebase_path_amend()); + unlink(git_path_merge_head(the_repository)); if (final_fixup) { unlink(rebase_path_fixup_msg()); unlink(rebase_path_squash_msg()); From ac06fe354b80e3567689a311456c673cdf483609 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 21:47:06 +0100 Subject: [PATCH 217/406] built-in rebase --skip/--abort: clean up stale .git/<name> files The scripted version of the rebase used to execute `git reset --hard` when skipping or aborting. When we ported this to C, we did update the worktree and some reflogs, but we failed to imitate `git reset --hard`'s behavior regarding files in .git/ such as MERGE_HEAD. Let's address this oversight. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builtin/rebase.c b/builtin/rebase.c index b4881ebb222338..321ad0142f3215 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -22,6 +22,7 @@ #include "wt-status.h" #include "revision.h" #include "rerere.h" +#include "branch.h" static char const * const builtin_rebase_usage[] = { N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " @@ -1001,6 +1002,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0) die(_("could not discard worktree changes")); + remove_branch_state(); if (read_basic_state(&options)) exit(1); goto run_rebase; @@ -1018,6 +1020,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.head_name, 0, NULL, NULL) < 0) die(_("could not move back to %s"), oid_to_hex(&options.orig_head)); + remove_branch_state(); ret = finish_rebase(&options); goto cleanup; } From f2abea2e1dffab37cbdb3386b04ee8ca383f5366 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 12 Nov 2018 21:49:26 +0100 Subject: [PATCH 218/406] status: rebase and merge can be in progress at the same time Since `git rebase -r` was introduced, that is possible. But our machinery did not think that possible, and failed to say anything about the rebase in progress when in the middle of a merge. Let's work around that in the minimal fashion. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- wt-status.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wt-status.c b/wt-status.c index 5ffab61015da90..30b81576a3aa72 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1553,6 +1553,7 @@ void wt_status_get_state(struct wt_status_state *state, struct object_id oid; if (!stat(git_path_merge_head(the_repository), &st)) { + wt_status_check_rebase(NULL, state); state->merge_in_progress = 1; } else if (wt_status_check_rebase(NULL, state)) { ; /* all set */ @@ -1576,9 +1577,13 @@ static void wt_longstatus_print_state(struct wt_status *s, struct wt_status_state *state) { const char *state_color = color(WT_STATUS_HEADER, s); - if (state->merge_in_progress) + if (state->merge_in_progress) { + if (state->rebase_interactive_in_progress) { + show_rebase_information(s, state, state_color); + fputs("\n", s->fp); + } show_merge_in_progress(s, state, state_color); - else if (state->am_in_progress) + } else if (state->am_in_progress) show_am_in_progress(s, state, state_color); else if (state->rebase_in_progress || state->rebase_interactive_in_progress) show_rebase_in_progress(s, state, state_color); From b7d63ca9803a3cee5aa4d44f46df14989fa4dec0 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 23 Aug 2018 16:15:47 +0200 Subject: [PATCH 219/406] builtin rebase: call `git am` directly While the scripted `git rebase` still has to rely on the `git-rebase--am.sh` script to implement the glue between the `rebase` and the `am` commands, we can go a more direct route in the builtin rebase and avoid using a shell script altogether. This reduces the chances of Git for Windows running into trouble due to problems with the POSIX emulation layer (known as "MSYS2 runtime", itself a derivative of the Cygwin runtime): when no shell script is called, the POSIX emulation layer is avoided altogether. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/rebase.c | 410 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 292 insertions(+), 118 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index fdba33c0e9da68..3bf01ba0dd36aa 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -241,6 +241,37 @@ static int read_basic_state(struct rebase_options *opts) return 0; } +static int write_basic_state(struct rebase_options *opts) +{ + write_file(state_dir_path("head-name", opts), "%s", + opts->head_name ? opts->head_name : "detached HEAD"); + write_file(state_dir_path("onto", opts), "%s", + opts->onto ? oid_to_hex(&opts->onto->object.oid) : ""); + write_file(state_dir_path("orig-head", opts), "%s", + oid_to_hex(&opts->orig_head)); + write_file(state_dir_path("quiet", opts), "%s", + opts->flags & REBASE_NO_QUIET ? "" : "t"); + if (opts->flags & REBASE_VERBOSE) + write_file(state_dir_path("verbose", opts), ""); + if (opts->strategy) + write_file(state_dir_path("strategy", opts), "%s", + opts->strategy); + if (opts->strategy_opts) + write_file(state_dir_path("strategy_opts", opts), "%s", + opts->strategy_opts); + if (opts->allow_rerere_autoupdate >= 0) + write_file(state_dir_path("allow_rerere_autoupdate", opts), + "-%s-rerere-autoupdate", + opts->allow_rerere_autoupdate ? "" : "-no"); + if (opts->gpg_sign_opt) + write_file(state_dir_path("gpg_sign_opt", opts), "%s", + opts->gpg_sign_opt); + if (opts->signoff) + write_file(state_dir_path("strategy", opts), "--signoff"); + + return 0; +} + static int apply_autostash(struct rebase_options *opts) { const char *path = state_dir_path("autostash", opts); @@ -328,6 +359,140 @@ static void add_var(struct strbuf *buf, const char *name, const char *value) } } +#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" + +#define RESET_HEAD_DETACH (1<<0) +#define RESET_HEAD_HARD (1<<1) + +static int reset_head(struct object_id *oid, const char *action, + const char *switch_to_branch, unsigned flags, + const char *reflog_orig_head, const char *reflog_head) +{ + unsigned detach_head = flags & RESET_HEAD_DETACH; + unsigned reset_hard = flags & RESET_HEAD_HARD; + struct object_id head_oid; + struct tree_desc desc[2] = { { NULL }, { NULL } }; + struct lock_file lock = LOCK_INIT; + struct unpack_trees_options unpack_tree_opts; + struct tree *tree; + const char *reflog_action; + struct strbuf msg = STRBUF_INIT; + size_t prefix_len; + struct object_id *orig = NULL, oid_orig, + *old_orig = NULL, oid_old_orig; + int ret = 0, nr = 0; + + if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) + BUG("Not a fully qualified branch: '%s'", switch_to_branch); + + if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { + ret = -1; + goto leave_reset_head; + } + + if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { + ret = error(_("could not determine HEAD revision")); + goto leave_reset_head; + } + + if (!oid) + oid = &head_oid; + + memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); + setup_unpack_trees_porcelain(&unpack_tree_opts, action); + unpack_tree_opts.head_idx = 1; + unpack_tree_opts.src_index = the_repository->index; + unpack_tree_opts.dst_index = the_repository->index; + unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; + unpack_tree_opts.update = 1; + unpack_tree_opts.merge = 1; + if (!detach_head) + unpack_tree_opts.reset = 1; + + if (read_index_unmerged(the_repository->index) < 0) { + ret = error(_("could not read index")); + goto leave_reset_head; + } + + if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) { + ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); + goto leave_reset_head; + } + + if (!fill_tree_descriptor(&desc[nr++], oid)) { + ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); + goto leave_reset_head; + } + + if (unpack_trees(nr, desc, &unpack_tree_opts)) { + ret = -1; + goto leave_reset_head; + } + + tree = parse_tree_indirect(oid); + prime_cache_tree(the_repository->index, tree); + + if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) { + ret = error(_("could not write index")); + goto leave_reset_head; + } + + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); + prefix_len = msg.len; + + if (!get_oid("ORIG_HEAD", &oid_old_orig)) + old_orig = &oid_old_orig; + if (!get_oid("HEAD", &oid_orig)) { + orig = &oid_orig; + if (!reflog_orig_head) { + strbuf_addstr(&msg, "updating ORIG_HEAD"); + reflog_orig_head = msg.buf; + } + update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0, + UPDATE_REFS_MSG_ON_ERR); + } else if (old_orig) + delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + if (!reflog_head) { + strbuf_setlen(&msg, prefix_len); + strbuf_addstr(&msg, "updating HEAD"); + reflog_head = msg.buf; + } + if (!switch_to_branch) + ret = update_ref(reflog_head, "HEAD", oid, orig, + detach_head ? REF_NO_DEREF : 0, + UPDATE_REFS_MSG_ON_ERR); + else { + ret = create_symref("HEAD", switch_to_branch, msg.buf); + if (!ret) + ret = update_ref(reflog_head, "HEAD", oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR); + } + +leave_reset_head: + strbuf_release(&msg); + rollback_lock_file(&lock); + while (nr) + free((void *)desc[--nr].buffer); + return ret; +} + +static int move_to_original_branch(struct rebase_options *opts) +{ + struct strbuf buf = STRBUF_INIT; + int ret; + + if (opts->head_name && opts->onto) + strbuf_addf(&buf, "rebase finished: %s onto %s", + opts->head_name, + oid_to_hex(&opts->onto->object.oid)); + ret = reset_head(NULL, "checkout", opts->head_name, 0, + "HEAD", buf.buf); + + strbuf_release(&buf); + return ret; +} + static const char *resolvemsg = N_("Resolve all conflicts manually, mark them as resolved with\n" "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n" @@ -335,6 +500,128 @@ N_("Resolve all conflicts manually, mark them as resolved with\n" "To abort and get back to the state before \"git rebase\", run " "\"git rebase --abort\"."); +static int run_am(struct rebase_options *opts) +{ + struct child_process am = CHILD_PROCESS_INIT; + struct child_process format_patch = CHILD_PROCESS_INIT; + struct strbuf revisions = STRBUF_INIT; + int status; + char *rebased_patches; + + am.git_cmd = 1; + argv_array_push(&am.args, "am"); + + if (opts->action && !strcmp("continue", opts->action)) { + argv_array_push(&am.args, "--resolved"); + argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg); + if (opts->gpg_sign_opt) + argv_array_push(&am.args, opts->gpg_sign_opt); + status = run_command(&am); + if (status) + return status; + + discard_cache(); + return move_to_original_branch(opts); + } + if (opts->action && !strcmp("skip", opts->action)) { + argv_array_push(&am.args, "--skip"); + argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg); + status = run_command(&am); + if (status) + return status; + + discard_cache(); + return move_to_original_branch(opts); + } + if (opts->action && !strcmp("show-current-patch", opts->action)) { + argv_array_push(&am.args, "--show-current-patch"); + return run_command(&am); + } + + strbuf_addf(&revisions, "%s...%s", + oid_to_hex(opts->root ? + /* this is now equivalent to ! -z "$upstream" */ + &opts->onto->object.oid : + &opts->upstream->object.oid), + oid_to_hex(&opts->orig_head)); + + rebased_patches = xstrdup(git_path("rebased-patches")); + format_patch.out = open(rebased_patches, O_WRONLY | O_CREAT, 0666); + if (format_patch.out < 0) { + status = error_errno(_("could not write '%s'"), + rebased_patches); + free(rebased_patches); + argv_array_clear(&am.args); + return status; + } + + format_patch.git_cmd = 1; + argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout", + "--full-index", "--cherry-pick", "--right-only", + "--src-prefix=a/", "--dst-prefix=b/", "--no-renames", + "--no-cover-letter", "--pretty=mboxrd", NULL); + if (opts->git_format_patch_opt.len) + argv_array_split(&format_patch.args, + opts->git_format_patch_opt.buf); + argv_array_push(&format_patch.args, revisions.buf); + if (opts->restrict_revision) + argv_array_pushf(&format_patch.args, "^%s", + oid_to_hex(&opts->restrict_revision->object.oid)); + + status = run_command(&format_patch); + if (status) { + unlink(rebased_patches); + free(rebased_patches); + argv_array_clear(&am.args); + + reset_head(&opts->orig_head, "checkout", opts->head_name, 0, + "HEAD", NULL); + error(_("\ngit encountered an error while preparing the " + "patches to replay\n" + "these revisions:\n" + "\n %s\n\n" + "As a result, git cannot rebase them."), + opts->revisions); + + strbuf_release(&revisions); + return status; + } + strbuf_release(&revisions); + + am.in = open(rebased_patches, O_RDONLY); + if (am.in < 0) { + status = error_errno(_("could not read '%s'"), + rebased_patches); + free(rebased_patches); + argv_array_clear(&am.args); + return status; + } + + argv_array_split(&am.args, opts->git_am_opt.buf); + argv_array_push(&am.args, "--rebasing"); + argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg); + argv_array_push(&am.args, "--patch-format=mboxrd"); + if (opts->allow_rerere_autoupdate > 0) + argv_array_push(&am.args, "--rerere-autoupdate"); + else if (opts->allow_rerere_autoupdate == 0) + argv_array_push(&am.args, "--no-rerere-autoupdate"); + if (opts->gpg_sign_opt) + argv_array_push(&am.args, opts->gpg_sign_opt); + status = run_command(&am); + unlink(rebased_patches); + free(rebased_patches); + + if (!status) { + discard_cache(); + return move_to_original_branch(opts); + } + + if (is_directory(opts->state_dir)) + write_basic_state(opts); + + return status; +} + static int run_specific_rebase(struct rebase_options *opts) { const char *argv[] = { NULL, NULL }; @@ -415,6 +702,11 @@ static int run_specific_rebase(struct rebase_options *opts) goto finished_rebase; } + if (opts->type == REBASE_AM) { + status = run_am(opts); + goto finished_rebase; + } + add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); add_var(&script_snippet, "state_dir", opts->state_dir); @@ -521,124 +813,6 @@ static int run_specific_rebase(struct rebase_options *opts) return status ? -1 : 0; } -#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" - -#define RESET_HEAD_DETACH (1<<0) -#define RESET_HEAD_HARD (1<<1) - -static int reset_head(struct object_id *oid, const char *action, - const char *switch_to_branch, unsigned flags, - const char *reflog_orig_head, const char *reflog_head) -{ - unsigned detach_head = flags & RESET_HEAD_DETACH; - unsigned reset_hard = flags & RESET_HEAD_HARD; - struct object_id head_oid; - struct tree_desc desc[2] = { { NULL }, { NULL } }; - struct lock_file lock = LOCK_INIT; - struct unpack_trees_options unpack_tree_opts; - struct tree *tree; - const char *reflog_action; - struct strbuf msg = STRBUF_INIT; - size_t prefix_len; - struct object_id *orig = NULL, oid_orig, - *old_orig = NULL, oid_old_orig; - int ret = 0, nr = 0; - - if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) - BUG("Not a fully qualified branch: '%s'", switch_to_branch); - - if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { - ret = -1; - goto leave_reset_head; - } - - if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { - ret = error(_("could not determine HEAD revision")); - goto leave_reset_head; - } - - if (!oid) - oid = &head_oid; - - memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); - setup_unpack_trees_porcelain(&unpack_tree_opts, action); - unpack_tree_opts.head_idx = 1; - unpack_tree_opts.src_index = the_repository->index; - unpack_tree_opts.dst_index = the_repository->index; - unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; - unpack_tree_opts.update = 1; - unpack_tree_opts.merge = 1; - if (!detach_head) - unpack_tree_opts.reset = 1; - - if (read_index_unmerged(the_repository->index) < 0) { - ret = error(_("could not read index")); - goto leave_reset_head; - } - - if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) { - ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); - goto leave_reset_head; - } - - if (!fill_tree_descriptor(&desc[nr++], oid)) { - ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); - goto leave_reset_head; - } - - if (unpack_trees(nr, desc, &unpack_tree_opts)) { - ret = -1; - goto leave_reset_head; - } - - tree = parse_tree_indirect(oid); - prime_cache_tree(the_repository->index, tree); - - if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) { - ret = error(_("could not write index")); - goto leave_reset_head; - } - - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); - prefix_len = msg.len; - - if (!get_oid("ORIG_HEAD", &oid_old_orig)) - old_orig = &oid_old_orig; - if (!get_oid("HEAD", &oid_orig)) { - orig = &oid_orig; - if (!reflog_orig_head) { - strbuf_addstr(&msg, "updating ORIG_HEAD"); - reflog_orig_head = msg.buf; - } - update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0, - UPDATE_REFS_MSG_ON_ERR); - } else if (old_orig) - delete_ref(NULL, "ORIG_HEAD", old_orig, 0); - if (!reflog_head) { - strbuf_setlen(&msg, prefix_len); - strbuf_addstr(&msg, "updating HEAD"); - reflog_head = msg.buf; - } - if (!switch_to_branch) - ret = update_ref(reflog_head, "HEAD", oid, orig, - detach_head ? REF_NO_DEREF : 0, - UPDATE_REFS_MSG_ON_ERR); - else { - ret = create_symref("HEAD", switch_to_branch, msg.buf); - if (!ret) - ret = update_ref(reflog_head, "HEAD", oid, NULL, 0, - UPDATE_REFS_MSG_ON_ERR); - } - -leave_reset_head: - strbuf_release(&msg); - rollback_lock_file(&lock); - while (nr) - free((void *)desc[--nr].buffer); - return ret; -} - static int rebase_config(const char *var, const char *value, void *data) { struct rebase_options *opts = data; From 04ca03a9c26ba733b134347056789c0d6d3aa6b6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 24 Aug 2018 15:14:04 +0200 Subject: [PATCH 220/406] Add back the original, scripted interactive rebase backend This simply copies the version as of v2.19.0-rc0 verbatim. As of now, it is not hooked up (because it needs a couple more changes to work); The next commit will use the scripted interactive rebase backend from `git rebase` again when `rebase.useBuiltin=false`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-rebase--interactive.sh | 283 +++++++++++++++++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 git-rebase--interactive.sh diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh new file mode 100644 index 00000000000000..299ded21375ed3 --- /dev/null +++ b/git-rebase--interactive.sh @@ -0,0 +1,283 @@ +# This shell script fragment is sourced by git-rebase to implement +# its interactive mode. "git rebase --interactive" makes it easy +# to fix up commits in the middle of a series and rearrange commits. +# +# Copyright (c) 2006 Johannes E. Schindelin +# +# The original idea comes from Eric W. Biederman, in +# https://public-inbox.org/git/m1odwkyuf5.fsf_-_@ebiederm.dsl.xmission.com/ +# +# The file containing rebase commands, comments, and empty lines. +# This file is created by "git rebase -i" then edited by the user. As +# the lines are processed, they are removed from the front of this +# file and written to the tail of $done. +todo="$state_dir"/git-rebase-todo + +GIT_CHERRY_PICK_HELP="$resolvemsg" +export GIT_CHERRY_PICK_HELP + +comment_char=$(git config --get core.commentchar 2>/dev/null) +case "$comment_char" in +'' | auto) + comment_char="#" + ;; +?) + ;; +*) + comment_char=$(echo "$comment_char" | cut -c1) + ;; +esac + +orig_reflog_action="$GIT_REFLOG_ACTION" + +comment_for_reflog () { + case "$orig_reflog_action" in + ''|rebase*) + GIT_REFLOG_ACTION="rebase -i ($1)" + export GIT_REFLOG_ACTION + ;; + esac +} + +append_todo_help () { + gettext " +Commands: +p, pick <commit> = use commit +r, reword <commit> = use commit, but edit the commit message +e, edit <commit> = use commit, but stop for amending +s, squash <commit> = use commit, but meld into previous commit +f, fixup <commit> = like \"squash\", but discard this commit's log message +x, exec <command> = run command (the rest of the line) using shell +d, drop <commit> = remove commit +l, label <label> = label current HEAD with a name +t, reset <label> = reset HEAD to a label +m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] +. create a merge commit using the original merge commit's +. message (or the oneline, if no original merge commit was +. specified). Use -c <commit> to reword the commit message. + +These lines can be re-ordered; they are executed from top to bottom. +" | git stripspace --comment-lines >>"$todo" + + if test $(get_missing_commit_check_level) = error + then + gettext " +Do not remove any line. Use 'drop' explicitly to remove a commit. +" | git stripspace --comment-lines >>"$todo" + else + gettext " +If you remove a line here THAT COMMIT WILL BE LOST. +" | git stripspace --comment-lines >>"$todo" + fi +} + +die_abort () { + apply_autostash + rm -rf "$state_dir" + die "$1" +} + +has_action () { + test -n "$(git stripspace --strip-comments <"$1")" +} + +git_sequence_editor () { + if test -z "$GIT_SEQUENCE_EDITOR" + then + GIT_SEQUENCE_EDITOR="$(git config sequence.editor)" + if [ -z "$GIT_SEQUENCE_EDITOR" ] + then + GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $? + fi + fi + + eval "$GIT_SEQUENCE_EDITOR" '"$@"' +} + +expand_todo_ids() { + git rebase--helper --expand-ids +} + +collapse_todo_ids() { + git rebase--helper --shorten-ids +} + +# Switch to the branch in $into and notify it in the reflog +checkout_onto () { + GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" + output git checkout $onto || die_abort "$(gettext "could not detach HEAD")" + git update-ref ORIG_HEAD $orig_head +} + +get_missing_commit_check_level () { + check_level=$(git config --get rebase.missingCommitsCheck) + check_level=${check_level:-ignore} + # Don't be case sensitive + printf '%s' "$check_level" | tr 'A-Z' 'a-z' +} + +# Initiate an action. If the cannot be any +# further action it may exec a command +# or exit and not return. +# +# TODO: Consider a cleaner return model so it +# never exits and always return 0 if process +# is complete. +# +# Parameter 1 is the action to initiate. +# +# Returns 0 if the action was able to complete +# and if 1 if further processing is required. +initiate_action () { + case "$1" in + continue) + exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ + --continue + ;; + skip) + git rerere clear + exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ + --continue + ;; + edit-todo) + git stripspace --strip-comments <"$todo" >"$todo".new + mv -f "$todo".new "$todo" + collapse_todo_ids + append_todo_help + gettext " +You are editing the todo file of an ongoing interactive rebase. +To continue rebase after editing, run: + git rebase --continue + +" | git stripspace --comment-lines >>"$todo" + + git_sequence_editor "$todo" || + die "$(gettext "Could not execute editor")" + expand_todo_ids + + exit + ;; + show-current-patch) + exec git show REBASE_HEAD -- + ;; + *) + return 1 # continue + ;; + esac +} + +setup_reflog_action () { + comment_for_reflog start + + if test ! -z "$switch_to" + then + GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" + output git checkout "$switch_to" -- || + die "$(eval_gettext "Could not checkout \$switch_to")" + + comment_for_reflog start + fi +} + +init_basic_state () { + orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")" + mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")" + rm -f "$(git rev-parse --git-path REBASE_HEAD)" + + : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")" + write_basic_state +} + +init_revisions_and_shortrevisions () { + shorthead=$(git rev-parse --short $orig_head) + shortonto=$(git rev-parse --short $onto) + if test -z "$rebase_root" + # this is now equivalent to ! -z "$upstream" + then + shortupstream=$(git rev-parse --short $upstream) + revisions=$upstream...$orig_head + shortrevisions=$shortupstream..$shorthead + else + revisions=$onto...$orig_head + shortrevisions=$shorthead + test -z "$squash_onto" || + echo "$squash_onto" >"$state_dir"/squash-onto + fi +} + +complete_action() { + test -s "$todo" || echo noop >> "$todo" + test -z "$autosquash" || git rebase--helper --rearrange-squash || exit + test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd" + + todocount=$(git stripspace --strip-comments <"$todo" | wc -l) + todocount=${todocount##* } + +cat >>"$todo" <<EOF + +$comment_char $(eval_ngettext \ + "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \ + "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \ + "$todocount") +EOF + append_todo_help + gettext " + However, if you remove everything, the rebase will be aborted. + + " | git stripspace --comment-lines >>"$todo" + + if test -z "$keep_empty" + then + printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo" + fi + + + has_action "$todo" || + return 2 + + cp "$todo" "$todo".backup + collapse_todo_ids + git_sequence_editor "$todo" || + die_abort "$(gettext "Could not execute editor")" + + has_action "$todo" || + return 2 + + git rebase--helper --check-todo-list || { + ret=$? + checkout_onto + exit $ret + } + + expand_todo_ids + + test -n "$force_rebase" || + onto="$(git rebase--helper --skip-unnecessary-picks)" || + die "Could not skip unnecessary pick commands" + + checkout_onto + require_clean_work_tree "rebase" + exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ + --continue +} + +git_rebase__interactive () { + initiate_action "$action" + ret=$? + if test $ret = 0; then + return 0 + fi + + setup_reflog_action + init_basic_state + + init_revisions_and_shortrevisions + + git rebase--helper --make-script ${keep_empty:+--keep-empty} \ + ${rebase_merges:+--rebase-merges} \ + ${rebase_cousins:+--rebase-cousins} \ + $revisions ${restrict_revision+^$restrict_revision} >"$todo" || + die "$(gettext "Could not generate todo list")" + + complete_action +} From 92609f890288d4f9dee88bc3c6e9c215e129d4c7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 24 Aug 2018 15:07:41 +0200 Subject: [PATCH 221/406] non-builtin rebase: use non-builtin interactive backend We recently converted both the `git rebase` and the `git rebase -i` command from Unix shell scripts to builtins. The former has a safety valve allowing to fall back to the scripted `rebase`, just in case that there is a bug in the builtin `rebase`: setting the config variable `rebase.useBuiltin` to `false` will fall back to using the scripted version. The latter did not have such a safety hatch. Let's reinstate the scripted interactive rebase backend so that `rebase.useBuiltin=false` will not use the builtin interactive rebase, just in case that an end user runs into a bug with the builtin version and needs to get out of the fix really quickly. This is necessary because Git for Windows wants to ship the builtin rebase/interactive rebase earlier than core Git: Git for Windows v2.19.0 will come with the option of a drastically faster (if a lot less battle-tested) `git rebase`/`git rebase -i`. As the file name `git-rebase--interactive` is already in use, let's rename the scripted backend to `git-legacy-rebase--interactive`. A couple of additional touch-ups are needed (such as teaching the builtin `rebase--interactive`, which assumed the role of the `rebase--helper`, to perform the two tricks to skip the unnecessary picks and to generate a new todo list) to make things work again. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitignore | 1 + Makefile | 1 + builtin/rebase--interactive.c | 18 +++++++- ...ve.sh => git-legacy-rebase--interactive.sh | 20 ++++----- git-legacy-rebase.sh | 42 ++++--------------- sequencer.c | 2 +- sequencer.h | 2 + 7 files changed, 41 insertions(+), 45 deletions(-) rename git-rebase--interactive.sh => git-legacy-rebase--interactive.sh (91%) diff --git a/.gitignore b/.gitignore index 1e457eeab6961c..a371024425c3ef 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ /git-interpret-trailers /git-instaweb /git-legacy-rebase +/git-legacy-rebase--interactive /git-legacy-stash /git-log /git-ls-files diff --git a/Makefile b/Makefile index d65da1b411ce37..221410e7baa6a4 100644 --- a/Makefile +++ b/Makefile @@ -621,6 +621,7 @@ SCRIPT_SH += git-request-pull.sh SCRIPT_SH += git-submodule.sh SCRIPT_SH += git-web--browse.sh +SCRIPT_LIB += git-legacy-rebase--interactive SCRIPT_LIB += git-mergetool--lib SCRIPT_LIB += git-parse-remote SCRIPT_LIB += git-rebase--am diff --git a/builtin/rebase--interactive.c b/builtin/rebase--interactive.c index a2ab68ed063224..4b9d2a07cb235b 100644 --- a/builtin/rebase--interactive.c +++ b/builtin/rebase--interactive.c @@ -141,7 +141,8 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) char *raw_strategies = NULL; enum { NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH, - SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC + SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, + MAKE_SCRIPT, SKIP_UNNECESSARY_PICKS, } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -192,6 +193,10 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) OPT_STRING(0, "onto-name", &onto_name, N_("onto-name"), N_("onto name")), OPT_STRING(0, "cmd", &cmd, N_("cmd"), N_("the command to run")), OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto), + OPT_CMDMODE(0, "make-script", &command, + N_("make rebase script"), MAKE_SCRIPT), + OPT_CMDMODE(0, "skip-unnecessary-picks", &command, + N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS), OPT_END() }; @@ -263,6 +268,17 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) case ADD_EXEC: ret = sequencer_add_exec_commands(cmd); break; + case MAKE_SCRIPT: + ret = sequencer_make_script(stdout, argc, argv, flags); + break; + case SKIP_UNNECESSARY_PICKS: { + struct object_id oid; + + ret = skip_unnecessary_picks(&oid); + if (!ret) + printf("%s\n", oid_to_hex(&oid)); + break; + } default: BUG("invalid command '%d'", command); } diff --git a/git-rebase--interactive.sh b/git-legacy-rebase--interactive.sh similarity index 91% rename from git-rebase--interactive.sh rename to git-legacy-rebase--interactive.sh index 299ded21375ed3..9740875ad50bf7 100644 --- a/git-rebase--interactive.sh +++ b/git-legacy-rebase--interactive.sh @@ -95,11 +95,11 @@ git_sequence_editor () { } expand_todo_ids() { - git rebase--helper --expand-ids + git rebase--interactive --expand-ids } collapse_todo_ids() { - git rebase--helper --shorten-ids + git rebase--interactive --shorten-ids } # Switch to the branch in $into and notify it in the reflog @@ -131,12 +131,12 @@ get_missing_commit_check_level () { initiate_action () { case "$1" in continue) - exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ + exec git rebase--interactive ${force_rebase:+--no-ff} $allow_empty_message \ --continue ;; skip) git rerere clear - exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ + exec git rebase--interactive ${force_rebase:+--no-ff} $allow_empty_message \ --continue ;; edit-todo) @@ -207,8 +207,8 @@ init_revisions_and_shortrevisions () { complete_action() { test -s "$todo" || echo noop >> "$todo" - test -z "$autosquash" || git rebase--helper --rearrange-squash || exit - test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd" + test -z "$autosquash" || git rebase--interactive --rearrange-squash || exit + test -n "$cmd" && git rebase--interactive --add-exec-commands --cmd "$cmd" todocount=$(git stripspace --strip-comments <"$todo" | wc -l) todocount=${todocount##* } @@ -243,7 +243,7 @@ EOF has_action "$todo" || return 2 - git rebase--helper --check-todo-list || { + git rebase--interactive --check-todo-list || { ret=$? checkout_onto exit $ret @@ -252,12 +252,12 @@ EOF expand_todo_ids test -n "$force_rebase" || - onto="$(git rebase--helper --skip-unnecessary-picks)" || + onto="$(git rebase--interactive --skip-unnecessary-picks)" || die "Could not skip unnecessary pick commands" checkout_onto require_clean_work_tree "rebase" - exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \ + exec git rebase--interactive ${force_rebase:+--no-ff} $allow_empty_message \ --continue } @@ -273,7 +273,7 @@ git_rebase__interactive () { init_revisions_and_shortrevisions - git rebase--helper --make-script ${keep_empty:+--keep-empty} \ + git rebase--interactive --make-script ${keep_empty:+--keep-empty} \ ${rebase_merges:+--rebase-merges} \ ${rebase_cousins:+--rebase-cousins} \ $revisions ${restrict_revision+^$restrict_revision} >"$todo" || diff --git a/git-legacy-rebase.sh b/git-legacy-rebase.sh index 7600765f541880..4312e69c6b92ac 100755 --- a/git-legacy-rebase.sh +++ b/git-legacy-rebase.sh @@ -135,37 +135,6 @@ finish_rebase () { rm -rf "$state_dir" } -run_interactive () { - GIT_CHERRY_PICK_HELP="$resolvemsg" - export GIT_CHERRY_PICK_HELP - - test -n "$keep_empty" && keep_empty="--keep-empty" - test -n "$rebase_merges" && rebase_merges="--rebase-merges" - test -n "$rebase_cousins" && rebase_cousins="--rebase-cousins" - test -n "$autosquash" && autosquash="--autosquash" - test -n "$verbose" && verbose="--verbose" - test -n "$force_rebase" && force_rebase="--no-ff" - test -n "$restrict_revision" && \ - restrict_revision="--restrict-revision=^$restrict_revision" - test -n "$upstream" && upstream="--upstream=$upstream" - test -n "$onto" && onto="--onto=$onto" - test -n "$squash_onto" && squash_onto="--squash-onto=$squash_onto" - test -n "$onto_name" && onto_name="--onto-name=$onto_name" - test -n "$head_name" && head_name="--head-name=$head_name" - test -n "$strategy" && strategy="--strategy=$strategy" - test -n "$strategy_opts" && strategy_opts="--strategy-opts=$strategy_opts" - test -n "$switch_to" && switch_to="--switch-to=$switch_to" - test -n "$cmd" && cmd="--cmd=$cmd" - test -n "$action" && action="--$action" - - exec git rebase--interactive "$action" "$keep_empty" "$rebase_merges" "$rebase_cousins" \ - "$upstream" "$onto" "$squash_onto" "$restrict_revision" \ - "$allow_empty_message" "$autosquash" "$verbose" \ - "$force_rebase" "$onto_name" "$head_name" "$strategy" \ - "$strategy_opts" "$cmd" "$switch_to" \ - "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" -} - run_specific_rebase () { if [ "$interactive_rebase" = implied ]; then GIT_EDITOR=: @@ -175,7 +144,9 @@ run_specific_rebase () { if test -n "$interactive_rebase" -a -z "$preserve_merges" then - run_interactive + . git-legacy-rebase--$type + + git_rebase__$type else . git-rebase--$type @@ -195,7 +166,12 @@ run_specific_rebase () { then apply_autostash && rm -rf "$state_dir" && - die "Nothing to do" + if test -n "$interactive_rebase" -a -z "$preserve_merges" + then + die "error: nothing to do" + else + die "Nothing to do" + fi fi exit $ret } diff --git a/sequencer.c b/sequencer.c index 98b41ae8908cd4..a54470bb9ba548 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4639,7 +4639,7 @@ static int rewrite_file(const char *path, const char *buf, size_t len) } /* skip picking commits whose parents are unchanged */ -static int skip_unnecessary_picks(struct object_id *output_oid) +int skip_unnecessary_picks(struct object_id *output_oid) { const char *todo_file = rebase_path_todo(); struct strbuf buf = STRBUF_INIT; diff --git a/sequencer.h b/sequencer.h index 491974355227e5..0a3f21240312a7 100644 --- a/sequencer.h +++ b/sequencer.h @@ -128,3 +128,5 @@ void print_commit_summary(const char *prefix, const struct object_id *oid, void parse_strategy_opts(struct replay_opts *opts, char *raw_opts); int write_basic_state(struct replay_opts *opts, const char *head_name, const char *onto, const char *orig_head); + +int skip_unnecessary_picks(struct object_id *output_oid); \ No newline at end of file From a2cbc843327c2a0fbf72cf64417a80aeda8eab51 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 10 Jan 2017 23:30:13 +0100 Subject: [PATCH 222/406] mingw: respect core.hidedotfiles = false in git-init again This is a brown paper bag. When adding the tests, we actually failed to verify that the config variable is heeded in git-init at all. And when changing the original patch that marked the .git/ directory as hidden after reading the config, it was lost on this developer that the new code would use the hide_dotfiles variable before the config was read. The fix is obvious: read the (limited, pre-init) config *before* creating the .git/ directory. This fixes https://github.com/git-for-windows/git/issues/789 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/init-db.c | 6 ++++++ t/t0001-init.sh | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/builtin/init-db.c b/builtin/init-db.c index 12ddda7e7bac51..f7f7bab789135d 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -155,6 +155,9 @@ static int git_init_db_config(const char *k, const char *v, void *cb) if (!strcmp(k, "init.templatedir")) return git_config_pathname(&init_db_template_dir, k, v); + if (starts_with(k, "core.")) + return platform_core_config(k, v, cb); + return 0; } @@ -361,6 +364,9 @@ int init_db(const char *git_dir, const char *real_git_dir, } startup_info->have_repository = 1; + /* Just look for `init.templatedir` and `core.hidedotfiles` */ + git_config(git_init_db_config, NULL); + safe_create_dir(git_dir, 0); init_is_bare_repository = is_bare_repository(); diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 182da069f1743b..97a338e5cdd9c7 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -453,6 +453,18 @@ test_expect_success 're-init from a linked worktree' ' ) ' +test_expect_success MINGW 'core.hidedotfiles = false' ' + git config --global core.hidedotfiles false && + rm -rf newdir && + ( + sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG && + mkdir newdir && + cd newdir && + git init + ) && + ! is_hidden newdir/.git +' + test_expect_success MINGW 'redirect std handles' ' GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir && test .git = "$(cat output.txt)" && From afa65bfa013a583b8c42aac43a811f2f9f7ccda1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 21 Jul 2017 22:17:20 +0200 Subject: [PATCH 223/406] tests: add t/helper/ to the PATH with --with-dashes We really need to be able to find the test helpers... Really. This change was forgotten when we moved the test helpers into t/helper/ Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 44288cbb598435..1d9a2d86a18ea6 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -942,7 +942,7 @@ else # normal case, use ../bin-wrappers only unless $with_dashes: GIT_EXEC_PATH=$GIT_BUILD_DIR if test -n "$with_dashes" then - PATH="$GIT_BUILD_DIR:$PATH" + PATH="$GIT_BUILD_DIR:$GIT_BUILD_DIR/t/helper:$PATH" fi fi GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt From a0e524d98753e77e457365740f061fe8eb3d300a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 18 Oct 2018 14:19:06 +0200 Subject: [PATCH 224/406] t0061: fix with --with-dashes and RUNTIME_PREFIX When building Git with RUNTIME_PREFIX and starting a test helper from t/helper/, it fails to detect the system prefix correctly. This is the reason that the warning RUNTIME_PREFIX requested, but prefix computation failed. [...] to be printed. In t0061, we did not expect that to happen, and it actually did not happen in the normal case, because bin-wrappers/test-tool specifically sets GIT_TEXTDOMAINDIR (and as a consequence, nothing in test-tool wants to know about the runtime prefix). However, with --with-dashes, bin-wrappers/test-tool is no longer called, but t/helper/test-tool is called directly. So let's just ignore the RUNTIME_PREFIX warning. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t0061-run-command.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 3e131c5325e363..d779e07ef2372b 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -144,7 +144,8 @@ test_trace () { expect="$1" shift GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \ - sed -e 's/.* run_command: //' -e '/trace: .*/d' >actual && + sed -e 's/.* run_command: //' -e '/trace: .*/d' \ + -e '/RUNTIME_PREFIX requested/d' >actual && echo "$expect true" >expect && test_cmp expect actual } From 8c1b7d6abdd321a65cfce53f57fe2944e9e558d8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 24 Oct 2018 18:34:09 +0200 Subject: [PATCH 225/406] tests: optionally skip bin-wrappers/ This speeds up the tests by a bit on Windows, where running Unix shell scripts (and spawning processes) is not exactly a cheap operation. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/README | 9 +++++++++ t/test-lib.sh | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/t/README b/t/README index 9028b47d923ca0..31b0daed95525c 100644 --- a/t/README +++ b/t/README @@ -169,6 +169,15 @@ appropriately before running "make". implied by other options like --valgrind and GIT_TEST_INSTALLED. +--no-bin-wrappers:: + By default, the test suite uses the wrappers in + `../bin-wrappers/` to execute `git` and friends. With this option, + `../git` and friends are run directly. This is not recommended + in general, as the wrappers contain safeguards to ensure that no + files from an installed Git are used, but can speed up test runs + especially on platforms where running shell scripts is expensive + (most notably, Windows). + --root=<directory>:: Create "trash" directories used to store all temporary data during testing under <directory>, instead of the t/ directory. diff --git a/t/test-lib.sh b/t/test-lib.sh index 1d9a2d86a18ea6..3b858d4317b40c 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -243,6 +243,8 @@ do test -z "$HARNESS_ACTIVE" && quiet=t; shift ;; --with-dashes) with_dashes=t; shift ;; + --no-bin-wrappers) + no_bin_wrappers=t; shift ;; --no-color) color=; shift ;; --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind) @@ -929,16 +931,21 @@ then PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} else # normal case, use ../bin-wrappers only unless $with_dashes: - git_bin_dir="$GIT_BUILD_DIR/bin-wrappers" - if ! test -x "$git_bin_dir/git" + if test -n "$no_bin_wrappers" then - if test -z "$with_dashes" + with_dashes=t + else + git_bin_dir="$GIT_BUILD_DIR/bin-wrappers" + if ! test -x "$git_bin_dir/git" then - say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH" + if test -z "$with_dashes" + then + say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH" + fi + with_dashes=t fi - with_dashes=t + PATH="$git_bin_dir:$PATH" fi - PATH="$git_bin_dir:$PATH" GIT_EXEC_PATH=$GIT_BUILD_DIR if test -n "$with_dashes" then From 9ef2ea46acf457039446659ccfc96b797af8bd45 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 30 Aug 2018 21:16:47 +0200 Subject: [PATCH 226/406] ci: rename the library of common functions The name is hard-coded to reflect that we use Travis CI for continuous testing. In the next commits, we will extend this to be able use Azure DevOps, too. So let's adjust the name to make it more generic. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- ci/install-dependencies.sh | 2 +- ci/{lib-travisci.sh => lib.sh} | 0 ci/print-test-failures.sh | 2 +- ci/run-build-and-tests.sh | 2 +- ci/run-linux32-docker.sh | 2 +- ci/run-static-analysis.sh | 2 +- ci/run-windows-build.sh | 2 +- ci/test-documentation.sh | 2 +- 8 files changed, 7 insertions(+), 7 deletions(-) rename ci/{lib-travisci.sh => lib.sh} (100%) diff --git a/ci/install-dependencies.sh b/ci/install-dependencies.sh index 75a9fd2475168f..961064658e083a 100755 --- a/ci/install-dependencies.sh +++ b/ci/install-dependencies.sh @@ -3,7 +3,7 @@ # Install dependencies required to build and test Git on Linux and macOS # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh P4WHENCE=http://filehost.perforce.com/perforce/r$LINUX_P4_VERSION LFSWHENCE=https://github.com/github/git-lfs/releases/download/v$LINUX_GIT_LFS_VERSION diff --git a/ci/lib-travisci.sh b/ci/lib.sh similarity index 100% rename from ci/lib-travisci.sh rename to ci/lib.sh diff --git a/ci/print-test-failures.sh b/ci/print-test-failures.sh index d55460a2128701..7aef39a2fd249f 100755 --- a/ci/print-test-failures.sh +++ b/ci/print-test-failures.sh @@ -3,7 +3,7 @@ # Print output of failing tests # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh # Tracing executed commands would produce too much noise in the loop below. set +x diff --git a/ci/run-build-and-tests.sh b/ci/run-build-and-tests.sh index 2a5bff4a1ca3de..e28ac2fb9a09f3 100755 --- a/ci/run-build-and-tests.sh +++ b/ci/run-build-and-tests.sh @@ -3,7 +3,7 @@ # Build and test Git # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh ln -s "$cache_dir/.prove" t/.prove diff --git a/ci/run-linux32-docker.sh b/ci/run-linux32-docker.sh index 21637903ce0790..751acfcf8a8c5c 100755 --- a/ci/run-linux32-docker.sh +++ b/ci/run-linux32-docker.sh @@ -3,7 +3,7 @@ # Download and run Docker image to build and test 32-bit Git # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh docker pull daald/ubuntu32:xenial diff --git a/ci/run-static-analysis.sh b/ci/run-static-analysis.sh index 5688f261d0813d..dc189c7456b3b0 100755 --- a/ci/run-static-analysis.sh +++ b/ci/run-static-analysis.sh @@ -3,7 +3,7 @@ # Perform various static code analysis checks # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh make --jobs=2 coccicheck diff --git a/ci/run-windows-build.sh b/ci/run-windows-build.sh index d99a180e528084..a73a4eca0aa605 100755 --- a/ci/run-windows-build.sh +++ b/ci/run-windows-build.sh @@ -6,7 +6,7 @@ # supported) and a commit hash. # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh test $# -ne 2 && echo "Unexpected number of parameters" && exit 1 test -z "$GFW_CI_TOKEN" && echo "GFW_CI_TOKEN not defined" && exit diff --git a/ci/test-documentation.sh b/ci/test-documentation.sh index a20de9ca127f82..d3cdbac73f8b3d 100755 --- a/ci/test-documentation.sh +++ b/ci/test-documentation.sh @@ -3,7 +3,7 @@ # Perform sanity checks on documentation and build it. # -. ${0%/*}/lib-travisci.sh +. ${0%/*}/lib.sh gem install asciidoctor From 56065dbf84232d257081b309a960be1c002d9a0e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 30 Aug 2018 21:57:28 +0200 Subject: [PATCH 227/406] ci/lib.sh: encapsulate Travis-specific things The upcoming patches will allow building git.git via Azure Pipelines (i.e. Azure DevOps' Continuous Integration), where variable names and URLs look a bit different than in Travis CI. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- ci/install-dependencies.sh | 3 ++- ci/lib.sh | 44 +++++++++++++++++++++++++++----------- ci/print-test-failures.sh | 2 +- ci/test-documentation.sh | 1 + 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/ci/install-dependencies.sh b/ci/install-dependencies.sh index 961064658e083a..63fa37f68f75b6 100755 --- a/ci/install-dependencies.sh +++ b/ci/install-dependencies.sh @@ -28,7 +28,8 @@ osx-clang|osx-gcc) brew update --quiet # Uncomment this if you want to run perf tests: # brew install gnu-time - brew install git-lfs gettext + test -z "$BREW_INSTALL_PACKAGES" || + brew install $BREW_INSTALL_PACKAGES brew link --force gettext brew install caskroom/cask/perforce ;; diff --git a/ci/lib.sh b/ci/lib.sh index 06970f72134fae..8532555b4ea25c 100755 --- a/ci/lib.sh +++ b/ci/lib.sh @@ -1,5 +1,26 @@ # Library of functions shared by all CI scripts +if test true = "$TRAVIS" +then + # We are running within Travis CI + CI_BRANCH="$TRAVIS_BRANCH" + CI_COMMIT="$TRAVIS_COMMIT" + CI_JOB_ID="$TRAVIS_JOB_ID" + CI_JOB_NUMBER="$TRAVIS_JOB_NUMBER" + CI_OS_NAME="$TRAVIS_OS_NAME" + CI_REPO_SLUG="$TRAVIS_REPO_SLUG" + + cache_dir="$HOME/travis-cache" + + url_for_job_id () { + echo "https://travis-ci.org/$CI_REPO_SLUG/jobs/$1" + } + + BREW_INSTALL_PACKAGES="git-lfs gettext" + export GIT_PROVE_OPTS="--timer --jobs 3 --state=failed,slow,save" + export GIT_TEST_OPTS="--verbose-log -x --immediate" +fi + skip_branch_tip_with_tag () { # Sometimes, a branch is pushed at the same time the tag that points # at the same commit as the tip of the branch is pushed, and building @@ -13,10 +34,10 @@ skip_branch_tip_with_tag () { # we can skip the build because we won't be skipping a build # of a tag. - if TAG=$(git describe --exact-match "$TRAVIS_BRANCH" 2>/dev/null) && - test "$TAG" != "$TRAVIS_BRANCH" + if TAG=$(git describe --exact-match "$CI_BRANCH" 2>/dev/null) && + test "$TAG" != "$CI_BRANCH" then - echo "$(tput setaf 2)Tip of $TRAVIS_BRANCH is exactly at $TAG$(tput sgr0)" + echo "$(tput setaf 2)Tip of $CI_BRANCH is exactly at $TAG$(tput sgr0)" exit 0 fi } @@ -25,7 +46,7 @@ skip_branch_tip_with_tag () { # job if we encounter the same tree again and can provide a useful info # message. save_good_tree () { - echo "$(git rev-parse $TRAVIS_COMMIT^{tree}) $TRAVIS_COMMIT $TRAVIS_JOB_NUMBER $TRAVIS_JOB_ID" >>"$good_trees_file" + echo "$(git rev-parse $CI_COMMIT^{tree}) $CI_COMMIT $CI_JOB_NUMBER $CI_JOB_ID" >>"$good_trees_file" # limit the file size tail -1000 "$good_trees_file" >"$good_trees_file".tmp mv "$good_trees_file".tmp "$good_trees_file" @@ -35,7 +56,7 @@ save_good_tree () { # successfully before (e.g. because the branch got rebased, changing only # the commit messages). skip_good_tree () { - if ! good_tree_info="$(grep "^$(git rev-parse $TRAVIS_COMMIT^{tree}) " "$good_trees_file")" + if ! good_tree_info="$(grep "^$(git rev-parse $CI_COMMIT^{tree}) " "$good_trees_file")" then # Haven't seen this tree yet, or no cached good trees file yet. # Continue the build job. @@ -45,18 +66,18 @@ skip_good_tree () { echo "$good_tree_info" | { read tree prev_good_commit prev_good_job_number prev_good_job_id - if test "$TRAVIS_JOB_ID" = "$prev_good_job_id" + if test "$CI_JOB_ID" = "$prev_good_job_id" then cat <<-EOF - $(tput setaf 2)Skipping build job for commit $TRAVIS_COMMIT.$(tput sgr0) + $(tput setaf 2)Skipping build job for commit $CI_COMMIT.$(tput sgr0) This commit has already been built and tested successfully by this build job. To force a re-build delete the branch's cache and then hit 'Restart job'. EOF else cat <<-EOF - $(tput setaf 2)Skipping build job for commit $TRAVIS_COMMIT.$(tput sgr0) + $(tput setaf 2)Skipping build job for commit $CI_COMMIT.$(tput sgr0) This commit's tree has already been built and tested successfully in build job $prev_good_job_number for commit $prev_good_commit. - The log of that build job is available at https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$prev_good_job_id + The log of that build job is available at $(url_for_job_id $prev_good_job_id) To force a re-build delete the branch's cache and then hit 'Restart job'. EOF fi @@ -81,7 +102,6 @@ check_unignored_build_artifacts () # and installing dependencies. set -ex -cache_dir="$HOME/travis-cache" good_trees_file="$cache_dir/good-trees" mkdir -p "$cache_dir" @@ -91,13 +111,11 @@ skip_good_tree if test -z "$jobname" then - jobname="$TRAVIS_OS_NAME-$CC" + jobname="$CI_OS_NAME-$CC" fi export DEVELOPER=1 export DEFAULT_TEST_TARGET=prove -export GIT_PROVE_OPTS="--timer --jobs 3 --state=failed,slow,save" -export GIT_TEST_OPTS="--verbose-log -x --immediate" export GIT_TEST_CLONE_2GB=YesPlease if [ "$jobname" = linux-gcc ]; then export CC=gcc-8 diff --git a/ci/print-test-failures.sh b/ci/print-test-failures.sh index 7aef39a2fd249f..d2045b63a6f8fb 100755 --- a/ci/print-test-failures.sh +++ b/ci/print-test-failures.sh @@ -69,7 +69,7 @@ do fi done -if [ $combined_trash_size -gt 0 ] +if [ -n "$TRAVIS_JOB_ID" -a $combined_trash_size -gt 0 ] then echo "------------------------------------------------------------------------" echo "Trash directories embedded in this log can be extracted by running:" diff --git a/ci/test-documentation.sh b/ci/test-documentation.sh index d3cdbac73f8b3d..7d0beb28327633 100755 --- a/ci/test-documentation.sh +++ b/ci/test-documentation.sh @@ -5,6 +5,7 @@ . ${0%/*}/lib.sh +test -n "$ALREADY_HAVE_ASCIIDOCTOR" || gem install asciidoctor make check-builtins From ee0383cf1edf0bde427c55a381c608029c8d4c3f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 31 Aug 2018 11:29:26 +0200 Subject: [PATCH 228/406] test-date: add a subcommand to measure times in shell scripts In the next commit, we want to teach Git's test suite to optionally output test results in JUnit-style .xml files. These files contain information about the time spent. So we need a way to measure time. While we could use `date +%s` for that, this will give us only seconds, i.e. very coarse-grained timings. GNU `date` supports `date +%s.%N` (i.e. nanosecond-precision output), but there is no equivalent in BSD `date` (read: on macOS, we would not be able to obtain precise timings). So let's introduce `test-tool date getnanos`, with an optional start time, that outputs preciser values. Granted, it is a bit pointless to try measuring times accurately in shell scripts, certainly to nanosecond precision. But it is better than second-granularity. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/helper/test-date.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/helper/test-date.c b/t/helper/test-date.c index a0837371aba179..792a80537467bd 100644 --- a/t/helper/test-date.c +++ b/t/helper/test-date.c @@ -7,6 +7,7 @@ static const char *usage_msg = "\n" " test-tool date parse [date]...\n" " test-tool date approxidate [date]...\n" " test-tool date timestamp [date]...\n" +" test-tool date getnanos [start-nanos]\n" " test-tool date is64bit\n" " test-tool date time_t-is64bit\n"; @@ -82,6 +83,15 @@ static void parse_approx_timestamp(const char **argv, struct timeval *now) } } +static void getnanos(const char **argv, struct timeval *now) +{ + double seconds = getnanotime() / 1.0e9; + + if (*argv) + seconds -= strtod(*argv, NULL); + printf("%lf\n", seconds); +} + int cmd__date(int argc, const char **argv) { struct timeval now; @@ -108,6 +118,8 @@ int cmd__date(int argc, const char **argv) parse_approxidate(argv+1, &now); else if (!strcmp(*argv, "timestamp")) parse_approx_timestamp(argv+1, &now); + else if (!strcmp(*argv, "getnanos")) + getnanos(argv+1, &now); else if (!strcmp(*argv, "is64bit")) return sizeof(timestamp_t) == 8 ? 0 : 1; else if (!strcmp(*argv, "time_t-is64bit")) From 53289cf285cdad43d4cf9b572ba7a5eb34013a30 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 29 Aug 2018 23:19:27 +0200 Subject: [PATCH 229/406] tests: optionally write results as JUnit-style .xml This will come in handy when publishing the results of Git's test suite during an automated Azure DevOps run. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/.gitignore | 1 + t/test-lib.sh | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/t/.gitignore b/t/.gitignore index 348715f0e4bcfe..91cf5772fe5643 100644 --- a/t/.gitignore +++ b/t/.gitignore @@ -2,3 +2,4 @@ /test-results /.prove /chainlinttmp +/out/ diff --git a/t/test-lib.sh b/t/test-lib.sh index 3b858d4317b40c..eaa752da4dd8fa 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -290,6 +290,9 @@ do --verbose-log) verbose_log=t shift ;; + --write-junit-xml) + write_junit_xml=t + shift ;; *) echo "error: unknown test option '$1'" >&2; exit 1 ;; esac @@ -433,11 +436,24 @@ trap 'exit $?' INT # the test_expect_* functions instead. test_ok_ () { + if test -n "$write_junit_xml" + then + write_junit_xml_testcase "$*" + fi test_success=$(($test_success + 1)) say_color "" "ok $test_count - $@" } test_failure_ () { + if test -n "$write_junit_xml" + then + junit_insert="<failure message=\"not ok $test_count -" + junit_insert="$junit_insert $(xml_attr_encode "$1")\">" + junit_insert="$junit_insert $(xml_attr_encode \ + "$(printf '%s\n' "$@" | sed 1d)")" + junit_insert="$junit_insert</failure>" + write_junit_xml_testcase "$1" " $junit_insert" + fi test_failure=$(($test_failure + 1)) say_color error "not ok $test_count - $1" shift @@ -446,11 +462,19 @@ test_failure_ () { } test_known_broken_ok_ () { + if test -n "$write_junit_xml" + then + write_junit_xml_testcase "$* (breakage fixed)" + fi test_fixed=$(($test_fixed+1)) say_color error "ok $test_count - $@ # TODO known breakage vanished" } test_known_broken_failure_ () { + if test -n "$write_junit_xml" + then + write_junit_xml_testcase "$* (known breakage)" + fi test_broken=$(($test_broken+1)) say_color warn "not ok $test_count - $@ # TODO known breakage" } @@ -708,6 +732,10 @@ test_start_ () { test_count=$(($test_count+1)) maybe_setup_verbose maybe_setup_valgrind + if test -n "$write_junit_xml" + then + junit_start=$(test-tool date getnanos) + fi } test_finish_ () { @@ -745,6 +773,13 @@ test_skip () { case "$to_skip" in t) + if test -n "$write_junit_xml" + then + message="$(xml_attr_encode "$skipped_reason")" + write_junit_xml_testcase "$1" \ + " <skipped message=\"$message\" />" + fi + say_color skip >&3 "skipping test: $@" say_color skip "ok $test_count # skip $1 ($skipped_reason)" : true @@ -760,9 +795,58 @@ test_at_end_hook_ () { : } +write_junit_xml () { + case "$1" in + --truncate) + >"$junit_xml_path" + junit_have_testcase= + shift + ;; + esac + printf '%s\n' "$@" >>"$junit_xml_path" +} + +xml_attr_encode () { + # We do not translate CR to because BSD sed does not handle + # \r in the regex. In practice, the output should not even have any + # carriage returns. + printf '%s\n' "$@" | + sed -e 's/&/\&/g' -e "s/'/\'/g" -e 's/"/\"/g' \ + -e 's/</\</g' -e 's/>/\>/g' \ + -e 's/ /\ /g' -e 's/$/\ /' -e '$s/ $//' | + tr -d '\012\015' +} + +write_junit_xml_testcase () { + junit_attrs="name=\"$(xml_attr_encode "$this_test.$test_count $1")\"" + shift + junit_attrs="$junit_attrs classname=\"$this_test\"" + junit_attrs="$junit_attrs time=\"$(test-tool \ + date getnanos $junit_start)\"" + write_junit_xml "$(printf '%s\n' \ + " <testcase $junit_attrs>" "$@" " </testcase>")" + junit_have_testcase=t +} + test_done () { GIT_EXIT_OK=t + if test -n "$write_junit_xml" && test -n "$junit_xml_path" + then + test -n "$junit_have_testcase" || { + junit_start=$(test-tool date getnanos) + write_junit_xml_testcase "all tests skipped" + } + + # adjust the overall time + junit_time=$(test-tool date getnanos $junit_suite_start) + sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \ + <"$junit_xml_path" >"$junit_xml_path.new" + mv "$junit_xml_path.new" "$junit_xml_path" + + write_junit_xml " </testsuite>" "</testsuites>" + fi + if test -z "$HARNESS_ACTIVE" then test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results" @@ -1003,6 +1087,7 @@ then else mkdir -p "$TRASH_DIRECTORY" fi + # Use -P to resolve symlinks in our working directory so that the cwd # in subprocesses like git equals our $PWD (for pathname comparisons). cd -P "$TRASH_DIRECTORY" || exit 1 @@ -1016,6 +1101,19 @@ then test_done fi +if test -n "$write_junit_xml" +then + junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out" + mkdir -p "$junit_xml_dir" + junit_xml_base=${0##*/} + junit_xml_path="$junit_xml_dir/TEST-${junit_xml_base%.sh}.xml" + junit_attrs="name=\"${junit_xml_base%.sh}\"" + junit_attrs="$junit_attrs timestamp=\"$(TZ=UTC \ + date +%Y-%m-%dT%H:%M:%S)\"" + write_junit_xml --truncate "<testsuites>" " <testsuite $junit_attrs>" + junit_suite_start=$(test-tool date getnanos) +fi + # Provide an implementation of the 'yes' utility yes () { if test $# = 0 From 42eb1fc0c8c753b6fa95c625093c763664e5abc5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 30 Aug 2018 21:59:46 +0200 Subject: [PATCH 230/406] ci/lib.sh: add support for Azure Pipelines This patch introduces a conditional arm that defines some environment variables and a function that displays the URL given the job id (to identify previous runs for known-good trees). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- ci/lib.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ci/lib.sh b/ci/lib.sh index 8532555b4ea25c..584abcd529bfa5 100755 --- a/ci/lib.sh +++ b/ci/lib.sh @@ -19,6 +19,29 @@ then BREW_INSTALL_PACKAGES="git-lfs gettext" export GIT_PROVE_OPTS="--timer --jobs 3 --state=failed,slow,save" export GIT_TEST_OPTS="--verbose-log -x --immediate" +elif test -n "$SYSTEM_TASKDEFINITIONSURI" +then + # We are running in Azure Pipelines + CI_BRANCH="$BUILD_SOURCEBRANCH" + CI_COMMIT="$BUILD_SOURCEVERSION" + CI_JOB_ID="$BUILD_BUILDID" + CI_JOB_NUMBER="$BUILD_BUILDNUMBER" + CI_OS_NAME="$(echo "$AGENT_OS" | tr A-Z a-z)" + test darwin != "$CI_OS_NAME" || CI_OS_NAME=osx + CI_REPO_SLUG="$(expr "$BUILD_REPOSITORY_URI" : '.*/\([^/]*/[^/]*\)$')" + CC="${CC:-gcc}" + + # use a subdirectory of the cache dir (because the file share is shared + # among *all* phases) + cache_dir="$HOME/test-cache/$SYSTEM_PHASENAME" + + url_for_job_id () { + echo "$SYSTEM_TASKDEFINITIONSURI$SYSTEM_TEAMPROJECT/_build/results?buildId=$1" + } + + BREW_INSTALL_PACKAGES= + export GIT_PROVE_OPTS="--timer --jobs 10 --state=failed,slow,save" + export GIT_TEST_OPTS="--quiet --write-junit-xml" fi skip_branch_tip_with_tag () { From 18e0d3a22216bdd94556f1d4358e083fef31df24 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 30 Aug 2018 18:15:42 +0200 Subject: [PATCH 231/406] Add a build definition for Azure DevOps This commit adds an azure-pipelines.yml file which is Azure DevOps' equivalent to Travis CI's .travis.yml. To make things a bit easier to understand, we refrain from using the `matrix` feature here because (while it is powerful) it can be a bit confusing to users who are not familiar with CI setups. Therefore, we use a separate phase even for similar configurations (such as GCC vs Clang on Linux, GCC vs Clang on macOS). Also, we make use of the shiny new feature we just introduced where the test suite can output JUnit-style .xml files. This information is made available in a nice UI that allows the viewer to filter by phase and/or test number, and to see trends such as: number of (failing) tests, time spent running the test suite, etc. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- azure-pipelines.yml | 325 ++++++++++++++++++++++++++++++++++++++++++ ci/mount-fileshare.sh | 26 ++++ 2 files changed, 351 insertions(+) create mode 100644 azure-pipelines.yml create mode 100755 ci/mount-fileshare.sh diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 00000000000000..4dc6fea129a413 --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,325 @@ +resources: +- repo: self + fetchDepth: 1 + +phases: +- phase: linux_clang + displayName: linux-clang + condition: succeeded() + queue: + name: Hosted Ubuntu 1604 + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + sudo apt-get update && + sudo rm /var/lib/apt/lists/lock && + sudo apt-get -y install git gcc make libssl-dev libcurl4-openssl-dev libexpat-dev tcl tk gettext git-email zlib1g-dev apache2-bin && + + export CC=clang || exit 1 + + ci/install-dependencies.sh + ci/run-build-and-tests.sh || { + ci/print-test-failures.sh + exit 1 + } + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || sudo umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-build-and-tests.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'linux-clang' + platform: Linux + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: linux_gcc + displayName: linux-gcc + condition: succeeded() + queue: + name: Hosted Ubuntu 1604 + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + sudo apt-get update && + sudo rm /var/lib/apt/lists/lock && + sudo apt-get -y install git gcc make libssl-dev libcurl4-openssl-dev libexpat-dev tcl tk gettext git-email zlib1g-dev apache2-bin || exit 1 + + ci/install-dependencies.sh + ci/run-build-and-tests.sh || { + ci/print-test-failures.sh + exit 1 + } + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || sudo umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-build-and-tests.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'linux-gcc' + platform: Linux + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: osx_clang + displayName: osx-clang + condition: succeeded() + queue: + name: Hosted macOS + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + export CC=clang + + ci/install-dependencies.sh + ci/run-build-and-tests.sh || { + ci/print-test-failures.sh + exit 1 + } + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-build-and-tests.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'osx-clang' + platform: macOS + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: osx_gcc + displayName: osx-gcc + condition: succeeded() + queue: + name: Hosted macOS + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + ci/install-dependencies.sh + ci/run-build-and-tests.sh || { + ci/print-test-failures.sh + exit 1 + } + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-build-and-tests.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'osx-gcc' + platform: macOS + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: gettext_poison + displayName: GETTEXT_POISON + condition: succeeded() + queue: + name: Hosted Ubuntu 1604 + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + sudo apt-get update && + sudo rm /var/lib/apt/lists/lock && + sudo apt-get -y install git gcc make libssl-dev libcurl4-openssl-dev libexpat-dev tcl tk gettext git-email zlib1g-dev && + + export jobname=GETTEXT_POISON || exit 1 + + ci/run-build-and-tests.sh || { + ci/print-test-failures.sh + exit 1 + } + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || sudo umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-build-and-tests.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'gettext-poison' + platform: Linux + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: windows + displayName: Windows + condition: succeeded() + queue: + name: Hosted + timeoutInMinutes: 240 + steps: + - powershell: | + # Helper to check the error level of the latest command (exit with error when appropriate) + function c() { if (!$?) { exit(1) } } + + if ("$GITFILESHAREPWD" -ne "" -and "$GITFILESHAREPWD" -ne "`$`(gitfileshare.pwd)") { + net use s: \\gitfileshare.file.core.windows.net\test-cache "$GITFILESHAREPWD" /user:AZURE\gitfileshare /persistent:no; c + cmd /c mklink /d "$(Build.SourcesDirectory)\test-cache" S:\; c + } + + # Add build agent's MinGit to PATH + $env:PATH = $env:AGENT_HOMEDIRECTORY +"\externals\\git\cmd;" +$env:PATH + + # Helper to initialize (or update) a Git worktree + function init ($path, $url, $set_origin) { + if (Test-Path $path) { + cd $path; c + if (Test-Path .git) { + git init; c + } else { + git status + } + } else { + git init $path; c + cd $path; c + } + git config core.autocrlf false; c + git config core.untrackedCache true; c + if (($set_origin -ne 0) -and !(git config remote.origin.url)) { + git remote add origin $url; c + } + git fetch --depth=1 $url master; c + git reset --hard FETCH_HEAD; c + git clean -df; c + } + + # Initialize Git for Windows' SDK + $sdk_path = "$(Build.SourcesDirectory)\git-sdk-64" + init "$sdk_path" "https://dev.azure.com/git-for-windows/git-sdk-64/_git/git-sdk-64" 0 + init usr\src\build-extra https://github.com/git-for-windows/build-extra 1 + + cd "$(Build.SourcesDirectory)"; c + + $env:HOME = "$(Build.SourcesDirectory)" + $env:MSYSTEM = "MINGW64" + git-sdk-64\git-cmd --command=usr\\bin\\bash.exe -lc @" + . ci/lib.sh + + make -j10 DEVELOPER=1 NO_PERL=1 || exit 1 + NO_PERL=1 NO_SVN_TESTS=1 GIT_TEST_OPTS=\"--no-chain-lint --no-bin-wrappers --quiet --write-junit-xml\" time make -j15 -k DEVELOPER=1 test || { + NO_PERL=1 NO_SVN_TESTS=1 GIT_TEST_OPTS=\"-i -v -x\" make -k -C t failed; exit 1 + } + + save_good_tree + "@ + c + + if ("$GITFILESHAREPWD" -ne "" -and "$GITFILESHAREPWD" -ne "`$`(gitfileshare.pwd)") { + cmd /c rmdir "$(Build.SourcesDirectory)\test-cache" + } + displayName: 'build & test' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'windows' + platform: Windows + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: linux32 + displayName: Linux32 + condition: succeeded() + queue: + name: Hosted Ubuntu 1604 + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + sudo apt-get update && + sudo rm /var/lib/apt/lists/lock && + sudo apt-get -y install \ + apt-transport-https \ + ca-certificates \ + curl \ + software-properties-common && + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && + sudo add-apt-repository \ + "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ + $(lsb_release -cs) \ + stable" && + sudo apt-get update && + sudo apt-get -y install docker-ce && + + sudo AGENT_OS="$AGENT_OS" BUILD_BUILDNUMBER="$BUILD_BUILDNUMBER" BUILD_REPOSITORY_URI="$BUILD_REPOSITORY_URI" BUILD_SOURCEBRANCH="$BUILD_SOURCEBRANCH" BUILD_SOURCEVERSION="$BUILD_SOURCEVERSION" SYSTEM_PHASENAME="$SYSTEM_PHASENAME" SYSTEM_TASKDEFINITIONSURI="$SYSTEM_TASKDEFINITIONSURI" SYSTEM_TEAMPROJECT="$SYSTEM_TEAMPROJECT" CC=$CC MAKEFLAGS=-j3 bash -lxc ci/run-linux32-docker.sh || exit 1 + + sudo chmod a+r t/out/TEST-*.xml + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || sudo umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-linux32-docker.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + - task: PublishTestResults@2 + displayName: 'Publish Test Results **/TEST-*.xml' + inputs: + mergeTestResults: true + testRunTitle: 'linux32' + platform: Linux + publishRunAttachments: false + condition: succeededOrFailed() + +- phase: static_analysis + displayName: StaticAnalysis + condition: succeeded() + queue: + name: Hosted Ubuntu 1604 + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + sudo apt-get update && + sudo rm /var/lib/apt/lists/lock && + sudo apt-get install -y coccinelle && + + export jobname=StaticAnalysis && + + ci/run-static-analysis.sh || exit 1 + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || sudo umount "$HOME/test-cache" || exit 1 + displayName: 'ci/run-static-analysis.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) + +- phase: documentation + displayName: Documentation + condition: succeeded() + queue: + name: Hosted Ubuntu 1604 + steps: + - bash: | + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || ci/mount-fileshare.sh //gitfileshare.file.core.windows.net/test-cache gitfileshare "$GITFILESHAREPWD" "$HOME/test-cache" || exit 1 + + sudo apt-get update && + sudo rm /var/lib/apt/lists/lock && + sudo apt-get install -y asciidoc xmlto asciidoctor && + + export ALREADY_HAVE_ASCIIDOCTOR=yes. && + export jobname=Documentation && + + ci/test-documentation.sh || exit 1 + + test "$GITFILESHAREPWD" = '$(gitfileshare.pwd)' || sudo umount "$HOME/test-cache" || exit 1 + displayName: 'ci/test-documentation.sh' + env: + GITFILESHAREPWD: $(gitfileshare.pwd) diff --git a/ci/mount-fileshare.sh b/ci/mount-fileshare.sh new file mode 100755 index 00000000000000..5fb5f74b705cb4 --- /dev/null +++ b/ci/mount-fileshare.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +die () { + echo "$*" >&2 + exit 1 +} + +test $# = 4 || +die "Usage: $0 <share> <username> <password> <mountpoint" + +mkdir -p "$4" || die "Could not create $4" + +case "$(uname -s)" in +Linux) + sudo mount -t cifs -o vers=3.0,username="$2",password="$3",dir_mode=0777,file_mode=0777,serverino "$1" "$4" + ;; +Darwin) + pass="$(echo "$3" | sed -e 's/\//%2F/g' -e 's/+/%2B/g')" && + mount -t smbfs,soft "smb://$2:$pass@${1#//}" "$4" + ;; +*) + die "No support for $(uname -s)" + ;; +esac || +die "Could not mount $4" + From 1abc832afa5bd575131b41a3b9ce5fa8f2db9a20 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 14 Sep 2018 14:42:13 -0500 Subject: [PATCH 232/406] tests: introduce `test_atexit` When running the p4 daemon or `git daemon`, we want to kill it at the end of the test script. So far, we do this "manually". However, in the next few commits we want to teach the test suite to optionally re-run scripts with different options, therefore we will have to have a consistent way to stop daemons. Let's introduce `test_atexit`, which is loosely modeled after `test_when_finished` (but has a broader scope: rather than running the commands after the current test case, run them when the test script finishes, and also run them when the `--immediate` option is in effect). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t0000-basic.sh | 18 ++++++++++++++++++ t/test-lib-functions.sh | 29 +++++++++++++++++++++++++++++ t/test-lib.sh | 4 ++++ 3 files changed, 51 insertions(+) diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index 850f651e4e4340..3df22e3989568e 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -821,6 +821,24 @@ test_expect_success 'tests clean up even on failures' " EOF " +test_expect_success 'test_atexit is run' " + test_must_fail run_sub_test_lib_test \ + atexit-cleanup 'Run atexit commands' -i <<-\\EOF && + test_expect_success 'tests clean up even after a failure' ' + > ../../clean-atexit && + test_atexit rm ../../clean-atexit && + > ../../also-clean-atexit && + test_atexit rm ../../also-clean-atexit && + > ../../dont-clean-atexit && + (exit 1) + ' + test_done + EOF + test_path_exists dont-clean-atexit && + test_path_is_missing clean-atexit && + test_path_is_missing also-clean-atexit +" + ################################################################ # Basics of the basics diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 4207af40777c69..98c981f4dc2a69 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -889,6 +889,35 @@ test_when_finished () { } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup" } +# This function can be used to schedule some commands to be run +# unconditionally at the end of the test script, e.g. to stop a daemon: +# +# test_expect_success 'test git daemon' ' +# git daemon & +# daemon_pid=$! && +# test_atexit "kill $daemon_pid" && +# hello world +# ' + +test_atexit () { + # We cannot detect when we are in a subshell in general, but by + # doing so on Bash is better than nothing (the test will + # silently pass on other shells). + test "${BASH_SUBSHELL-0}" = 0 || + error "bug in test script: test_atexit does nothing in a subshell" + test_atexit_cleanup="{ $* + } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup" +} + +test_atexit_handler () { + test : != "$test_atexit_cleanup" || return 0 + + setup_malloc_check + test_eval_ "$test_atexit_cleanup" + test_atexit_cleanup=: + teardown_malloc_check +} + # Most tests can use the created repository, but some may need to create more. # Usage: test_create_repo <directory> test_create_repo () { diff --git a/t/test-lib.sh b/t/test-lib.sh index eaa752da4dd8fa..6b13fa45f9f947 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -415,6 +415,7 @@ test_external_has_tap=0 die () { code=$? + test_atexit_handler || code=$? if test -n "$GIT_EXIT_OK" then exit $code @@ -828,9 +829,12 @@ write_junit_xml_testcase () { junit_have_testcase=t } +test_atexit_cleanup=: test_done () { GIT_EXIT_OK=t + test -n "$immediate" || test_atexit_handler + if test -n "$write_junit_xml" && test -n "$junit_xml_path" then test -n "$junit_have_testcase" || { From 35f510f4eaafa31438c333c460b08fde94b491d9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 14 Sep 2018 14:46:23 -0500 Subject: [PATCH 233/406] git-daemon: use `test_atexit` in the tests This makes use of the just-introduced consistent way to specify that a long-running process needs to be terminated at the end of a test script run. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/interop/i5500-git-daemon.sh | 1 - t/lib-git-daemon.sh | 3 +-- t/t5570-git-daemon.sh | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/t/interop/i5500-git-daemon.sh b/t/interop/i5500-git-daemon.sh index 1daf69420be194..4d22e42f8422ad 100755 --- a/t/interop/i5500-git-daemon.sh +++ b/t/interop/i5500-git-daemon.sh @@ -37,5 +37,4 @@ test_expect_success "fetch with $VERSION_B" ' test_cmp expect actual ' -stop_git_daemon test_done diff --git a/t/lib-git-daemon.sh b/t/lib-git-daemon.sh index edbea2d986134f..a896af2284bc5a 100644 --- a/t/lib-git-daemon.sh +++ b/t/lib-git-daemon.sh @@ -13,7 +13,6 @@ # # test_expect_success ... # -# stop_git_daemon # test_done test_tristate GIT_TEST_GIT_DAEMON @@ -43,7 +42,7 @@ start_git_daemon() { mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH" - trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT + test_atexit 'stop_git_daemon' say >&3 "Starting git daemon ..." mkfifo git_daemon_output diff --git a/t/t5570-git-daemon.sh b/t/t5570-git-daemon.sh index 7466aad111fe4e..08f95c80a28d9e 100755 --- a/t/t5570-git-daemon.sh +++ b/t/t5570-git-daemon.sh @@ -211,5 +211,4 @@ test_expect_success FAKENC 'hostname interpolation works after LF-stripping' ' test_cmp expect actual ' -stop_git_daemon test_done From 8c589eb5dd6efef9f230af38770d15bef34cdb55 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 14 Sep 2018 14:55:29 -0500 Subject: [PATCH 234/406] git-p4: use `test_atexit` to kill the daemon This should be more reliable than the current method, and prepares the test suite for a consistent way to clean up before re-running the tests with different options. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/lib-git-p4.sh | 10 +--------- t/t0000-basic.sh | 2 ++ t/t9800-git-p4-basic.sh | 4 ---- t/t9801-git-p4-branch.sh | 4 ---- t/t9802-git-p4-filetype.sh | 4 ---- t/t9803-git-p4-shell-metachars.sh | 4 ---- t/t9804-git-p4-label.sh | 4 ---- t/t9805-git-p4-skip-submit-edit.sh | 4 ---- t/t9806-git-p4-options.sh | 5 ----- t/t9807-git-p4-submit.sh | 4 ---- t/t9808-git-p4-chdir.sh | 4 ---- t/t9809-git-p4-client-view.sh | 4 ---- t/t9810-git-p4-rcs.sh | 4 ---- t/t9811-git-p4-label-import.sh | 5 ----- t/t9812-git-p4-wildcards.sh | 4 ---- t/t9813-git-p4-preserve-users.sh | 4 ---- t/t9814-git-p4-rename.sh | 4 ---- t/t9815-git-p4-submit-fail.sh | 4 ---- t/t9816-git-p4-locked.sh | 4 ---- t/t9817-git-p4-exclude.sh | 4 ---- t/t9818-git-p4-block.sh | 4 ---- t/t9819-git-p4-case-folding.sh | 4 ---- t/t9820-git-p4-editor-handling.sh | 4 ---- t/t9821-git-p4-path-variations.sh | 4 ---- t/t9822-git-p4-path-encoding.sh | 4 ---- t/t9823-git-p4-mock-lfs.sh | 4 ---- t/t9824-git-p4-git-lfs.sh | 4 ---- t/t9825-git-p4-handle-utf16-without-bom.sh | 4 ---- t/t9826-git-p4-keep-empty-commits.sh | 4 ---- t/t9827-git-p4-change-filetype.sh | 4 ---- t/t9828-git-p4-map-user.sh | 4 ---- t/t9829-git-p4-jobs.sh | 4 ---- t/t9830-git-p4-symlink-dir.sh | 4 ---- t/t9831-git-p4-triggers.sh | 4 ---- t/t9832-unshelve.sh | 4 ---- t/t9833-errors.sh | 5 ----- 36 files changed, 3 insertions(+), 148 deletions(-) diff --git a/t/lib-git-p4.sh b/t/lib-git-p4.sh index c27599474cf2f2..f4f5d7d29675b4 100644 --- a/t/lib-git-p4.sh +++ b/t/lib-git-p4.sh @@ -74,15 +74,6 @@ cli="$TRASH_DIRECTORY/cli" git="$TRASH_DIRECTORY/git" pidfile="$TRASH_DIRECTORY/p4d.pid" -# Sometimes "prove" seems to hang on exit because p4d is still running -cleanup () { - if test -f "$pidfile" - then - kill -9 $(cat "$pidfile") 2>/dev/null && exit 255 - fi -} -trap cleanup EXIT - # git p4 submit generates a temp file, which will # not get cleaned up if the submission fails. Don't # clutter up /tmp on the test machine. @@ -141,6 +132,7 @@ start_p4d () { # p4d failed to start return 1 fi + test_atexit kill_p4d # build a p4 user so author@example.com has an entry p4_add_user author diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index 3df22e3989568e..149f4ca8de574e 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -134,6 +134,7 @@ check_sub_test_lib_test_err () { ) } +cat >/dev/null <<\DDD test_expect_success 'pretend we have a fully passing test suite' " run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF && for i in 1 2 3 @@ -820,6 +821,7 @@ test_expect_success 'tests clean up even on failures' " > 1..2 EOF " +DDD test_expect_success 'test_atexit is run' " test_must_fail run_sub_test_lib_test \ diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh index 729cd25770177a..5856563068c712 100755 --- a/t/t9800-git-p4-basic.sh +++ b/t/t9800-git-p4-basic.sh @@ -326,8 +326,4 @@ test_expect_success 'submit from worktree' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9801-git-p4-branch.sh b/t/t9801-git-p4-branch.sh index 6a86d6996b97de..50013132c855f8 100755 --- a/t/t9801-git-p4-branch.sh +++ b/t/t9801-git-p4-branch.sh @@ -610,8 +610,4 @@ test_expect_success 'Update a file in git side and submit to P4 using client vie ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9802-git-p4-filetype.sh b/t/t9802-git-p4-filetype.sh index 9978352d7828db..94edebe272691a 100755 --- a/t/t9802-git-p4-filetype.sh +++ b/t/t9802-git-p4-filetype.sh @@ -333,8 +333,4 @@ test_expect_success SYMLINKS 'empty symlink target' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9803-git-p4-shell-metachars.sh b/t/t9803-git-p4-shell-metachars.sh index d5c36751004960..2913277013da56 100755 --- a/t/t9803-git-p4-shell-metachars.sh +++ b/t/t9803-git-p4-shell-metachars.sh @@ -105,8 +105,4 @@ test_expect_success 'branch with shell char' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9804-git-p4-label.sh b/t/t9804-git-p4-label.sh index e30f80e6176749..32364571063d4c 100755 --- a/t/t9804-git-p4-label.sh +++ b/t/t9804-git-p4-label.sh @@ -108,8 +108,4 @@ test_expect_failure 'two labels on the same changelist' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9805-git-p4-skip-submit-edit.sh b/t/t9805-git-p4-skip-submit-edit.sh index 5fbf904dc8f4df..90ef647db7e610 100755 --- a/t/t9805-git-p4-skip-submit-edit.sh +++ b/t/t9805-git-p4-skip-submit-edit.sh @@ -98,8 +98,4 @@ test_expect_success 'no config, edited' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9806-git-p4-options.sh b/t/t9806-git-p4-options.sh index 3f5291b85752e7..4e794a01bf556f 100755 --- a/t/t9806-git-p4-options.sh +++ b/t/t9806-git-p4-options.sh @@ -300,9 +300,4 @@ test_expect_success 'use --git-dir option and GIT_DIR' ' test_path_is_file "$git"/cli_file2.t ' - -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9807-git-p4-submit.sh b/t/t9807-git-p4-submit.sh index 2325599ee60f5b..488d916c102056 100755 --- a/t/t9807-git-p4-submit.sh +++ b/t/t9807-git-p4-submit.sh @@ -542,8 +542,4 @@ test_expect_success 'submit --update-shelve' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9808-git-p4-chdir.sh b/t/t9808-git-p4-chdir.sh index 11d2b5102cde38..58a9b3b71e6d88 100755 --- a/t/t9808-git-p4-chdir.sh +++ b/t/t9808-git-p4-chdir.sh @@ -83,8 +83,4 @@ test_expect_success SYMLINKS 'p4 client root symlink should stay symbolic' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9809-git-p4-client-view.sh b/t/t9809-git-p4-client-view.sh index 897b3c3034efca..3cff1fce1b7464 100755 --- a/t/t9809-git-p4-client-view.sh +++ b/t/t9809-git-p4-client-view.sh @@ -836,8 +836,4 @@ test_expect_success 'quotes on both sides' ' git_verify "cdir 1/file11" "cdir 1/file12" ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9810-git-p4-rcs.sh b/t/t9810-git-p4-rcs.sh index cc53debe1955ec..57b533dc6fbaa9 100755 --- a/t/t9810-git-p4-rcs.sh +++ b/t/t9810-git-p4-rcs.sh @@ -360,8 +360,4 @@ test_expect_failure 'Add keywords in git which do not match the default p4 value ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9811-git-p4-label-import.sh b/t/t9811-git-p4-label-import.sh index 602b0a5d5ceafc..b70e81c3cd28d8 100755 --- a/t/t9811-git-p4-label-import.sh +++ b/t/t9811-git-p4-label-import.sh @@ -259,9 +259,4 @@ test_expect_success 'importing labels with missing revisions' ' ) ' - -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9812-git-p4-wildcards.sh b/t/t9812-git-p4-wildcards.sh index 0206771fbb91b7..254a7c244698a0 100755 --- a/t/t9812-git-p4-wildcards.sh +++ b/t/t9812-git-p4-wildcards.sh @@ -211,8 +211,4 @@ test_expect_success 'wildcard files requiring keyword scrub' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9813-git-p4-preserve-users.sh b/t/t9813-git-p4-preserve-users.sh index 783c6ad1653142..fd018c87a80636 100755 --- a/t/t9813-git-p4-preserve-users.sh +++ b/t/t9813-git-p4-preserve-users.sh @@ -138,8 +138,4 @@ test_expect_success 'not preserving user with mixed authorship' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9814-git-p4-rename.sh b/t/t9814-git-p4-rename.sh index 60baa06e27a0a6..468767cbf4b93e 100755 --- a/t/t9814-git-p4-rename.sh +++ b/t/t9814-git-p4-rename.sh @@ -242,8 +242,4 @@ test_expect_success P4D_HAVE_CONFIGURABLE_RUN_MOVE_ALLOW \ ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9815-git-p4-submit-fail.sh b/t/t9815-git-p4-submit-fail.sh index eaf03a656329c7..9779dc0d11f33b 100755 --- a/t/t9815-git-p4-submit-fail.sh +++ b/t/t9815-git-p4-submit-fail.sh @@ -422,8 +422,4 @@ test_expect_success 'cleanup chmod after submit cancel' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9816-git-p4-locked.sh b/t/t9816-git-p4-locked.sh index d048bd33fa3c94..932841003cfc4e 100755 --- a/t/t9816-git-p4-locked.sh +++ b/t/t9816-git-p4-locked.sh @@ -138,8 +138,4 @@ test_expect_failure 'move with lock taken' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9817-git-p4-exclude.sh b/t/t9817-git-p4-exclude.sh index aac568eadfcab6..96d25f0c02ccfb 100755 --- a/t/t9817-git-p4-exclude.sh +++ b/t/t9817-git-p4-exclude.sh @@ -64,8 +64,4 @@ test_expect_success 'clone, then sync with exclude' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9818-git-p4-block.sh b/t/t9818-git-p4-block.sh index ce7cb22ad37165..0db7ab99184add 100755 --- a/t/t9818-git-p4-block.sh +++ b/t/t9818-git-p4-block.sh @@ -146,8 +146,4 @@ test_expect_success 'Clone repo with self-sizing block size' ' test_line_count \> 10 log ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9819-git-p4-case-folding.sh b/t/t9819-git-p4-case-folding.sh index d808c008c11700..600ce1e0b0d7ed 100755 --- a/t/t9819-git-p4-case-folding.sh +++ b/t/t9819-git-p4-case-folding.sh @@ -53,8 +53,4 @@ test_expect_failure 'Clone UC repo with lc name' ' test_must_fail git p4 clone //depot/uc/... ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9820-git-p4-editor-handling.sh b/t/t9820-git-p4-editor-handling.sh index 3c22f74bd436b7..fa1bba1dd93614 100755 --- a/t/t9820-git-p4-editor-handling.sh +++ b/t/t9820-git-p4-editor-handling.sh @@ -31,8 +31,4 @@ test_expect_success 'EDITOR with options' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9821-git-p4-path-variations.sh b/t/t9821-git-p4-path-variations.sh index 81e46acfa8ba10..ef80f1690bcb9a 100755 --- a/t/t9821-git-p4-path-variations.sh +++ b/t/t9821-git-p4-path-variations.sh @@ -193,8 +193,4 @@ test_expect_success 'Add a new file and clone path with new file (ignorecase)' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9822-git-p4-path-encoding.sh b/t/t9822-git-p4-path-encoding.sh index c78477c19b4330..1bf7635016f543 100755 --- a/t/t9822-git-p4-path-encoding.sh +++ b/t/t9822-git-p4-path-encoding.sh @@ -67,8 +67,4 @@ test_expect_success 'Delete iso8859-1 encoded paths and clone' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9823-git-p4-mock-lfs.sh b/t/t9823-git-p4-mock-lfs.sh index 1f2dc369bfa96e..88b76dc4d6c26f 100755 --- a/t/t9823-git-p4-mock-lfs.sh +++ b/t/t9823-git-p4-mock-lfs.sh @@ -185,8 +185,4 @@ test_expect_success 'Run git p4 submit in repo configured with large file system ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9824-git-p4-git-lfs.sh b/t/t9824-git-p4-git-lfs.sh index ed80ca858c8f83..a28dbbdd566ca6 100755 --- a/t/t9824-git-p4-git-lfs.sh +++ b/t/t9824-git-p4-git-lfs.sh @@ -287,8 +287,4 @@ test_expect_success 'Add big files to repo and store files in LFS based on compr ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9825-git-p4-handle-utf16-without-bom.sh b/t/t9825-git-p4-handle-utf16-without-bom.sh index 1551845dc178e3..f049ff8229c6d3 100755 --- a/t/t9825-git-p4-handle-utf16-without-bom.sh +++ b/t/t9825-git-p4-handle-utf16-without-bom.sh @@ -43,8 +43,4 @@ test_expect_failure 'clone depot with invalid UTF-16 file in non-verbose mode' ' git p4 clone --dest="$git" //depot ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9826-git-p4-keep-empty-commits.sh b/t/t9826-git-p4-keep-empty-commits.sh index fa8b9daf1fafba..fd64afe064e5a9 100755 --- a/t/t9826-git-p4-keep-empty-commits.sh +++ b/t/t9826-git-p4-keep-empty-commits.sh @@ -127,8 +127,4 @@ test_expect_success 'Clone repo subdir with all history' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9827-git-p4-change-filetype.sh b/t/t9827-git-p4-change-filetype.sh index 7433998f4779c7..d3670bd7a24dbf 100755 --- a/t/t9827-git-p4-change-filetype.sh +++ b/t/t9827-git-p4-change-filetype.sh @@ -59,8 +59,4 @@ test_expect_success SYMLINKS 'change symbolic link to file' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9828-git-p4-map-user.sh b/t/t9828-git-p4-map-user.sh index e20395c89f3dbc..ca6c2942bdf200 100755 --- a/t/t9828-git-p4-map-user.sh +++ b/t/t9828-git-p4-map-user.sh @@ -54,8 +54,4 @@ test_expect_success 'Clone repo root path with all history' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9829-git-p4-jobs.sh b/t/t9829-git-p4-jobs.sh index 971aeeea1fb69f..88cfb1fcd3f0a1 100755 --- a/t/t9829-git-p4-jobs.sh +++ b/t/t9829-git-p4-jobs.sh @@ -92,8 +92,4 @@ test_expect_success 'check log message of changelist with more jobs' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9830-git-p4-symlink-dir.sh b/t/t9830-git-p4-symlink-dir.sh index 2ad1b0810df81e..3fb6960c18fc0c 100755 --- a/t/t9830-git-p4-symlink-dir.sh +++ b/t/t9830-git-p4-symlink-dir.sh @@ -36,8 +36,4 @@ test_expect_success 'symlinked directory' ' ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9831-git-p4-triggers.sh b/t/t9831-git-p4-triggers.sh index be44c9751aefa5..d743ca33ee6ab2 100755 --- a/t/t9831-git-p4-triggers.sh +++ b/t/t9831-git-p4-triggers.sh @@ -96,8 +96,4 @@ test_expect_success 'submit description with extra info lines from verbose p4 ch ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9832-unshelve.sh b/t/t9832-unshelve.sh index 48ec7679b8d963..b649d1b7c38120 100755 --- a/t/t9832-unshelve.sh +++ b/t/t9832-unshelve.sh @@ -131,8 +131,4 @@ test_expect_success 'try to unshelve the change' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - test_done diff --git a/t/t9833-errors.sh b/t/t9833-errors.sh index 277d34701201af..1f3d8791220b31 100755 --- a/t/t9833-errors.sh +++ b/t/t9833-errors.sh @@ -72,9 +72,4 @@ test_expect_success 'git operation with expired ticket' ' ) ' -test_expect_success 'kill p4d' ' - kill_p4d -' - - test_done From 1c1df597323866adbb722cdb2b6517e40ce2645d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 31 Aug 2018 15:40:14 +0200 Subject: [PATCH 235/406] tests: include detailed trace logs with --write-junit-xml upon failure The JUnit XML format lends itself to be presented in a powerful UI, where you can drill down to the information you are interested in very quickly. For test failures, this usually means that you want to see the detailed trace of the failing tests. With Travis CI, we passed the `--verbose-log` option to get those traces. However, that seems excessive, as we do not need/use the logs in almost all of those cases: only when a test fails do we have a way to include the trace. So let's do something different when using Azure DevOps: let's run all the tests with `--quiet` first, and only if a failure is encountered, try to trace the commands as they are executed. Of course, we cannot turn on `--verbose-log` after the fact. So let's just re-run the test with all the same options, adding `--verbose-log`. And then munging the output file into the JUnit XML on the fly. Note: there is an off chance that re-running the test in verbose mode "fixes" the failures (and this does happen from time to time!). That is a possibility we should be able to live with. Ideally, we would label this as "Passed upon rerun", and Azure Pipelines even know about that outcome, but it is not available when using the JUnit XML format for now: https://github.com/Microsoft/azure-pipelines-agent/blob/master/src/Agent.Worker/TestResults/JunitResultReader.cs Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 6b13fa45f9f947..9e9862b0f1efca 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -85,6 +85,13 @@ done,*) test "$(cat "$BASE.exit")" = 0 exit ;; +*' --write-junit-xml '*) + # record how to call this script *with* --verbose-log, in case + # we encounter a breakage + junit_rerun_options_sq="$(printf '%s\n' "$0" --verbose-log -x "$@" | + sed -e "s/'/'\\\\''/g" -e "s/^/'/" -e "s/\$/'/" | + tr '\012' ' ')" + ;; esac # For repeatability, reset the environment to known value. @@ -448,10 +455,31 @@ test_ok_ () { test_failure_ () { if test -n "$write_junit_xml" then + if test -z "$GIT_TEST_TEE_OUTPUT_FILE" + then + # clean up + test_atexit_handler + + # re-run with --verbose-log + echo "# Re-running: $junit_rerun_options_sq" >&2 + + cd "$TEST_DIRECTORY" && + eval "${TEST_SHELL_PATH}" "$junit_rerun_options_sq" \ + >/dev/null 2>&1 + status=$? + + say_color "" "$(test 0 = $status || + echo "not ")ok $test_count - (re-ran with trace)" + say "1..$test_count" + GIT_EXIT_OK=t + exit $status + fi + junit_insert="<failure message=\"not ok $test_count -" junit_insert="$junit_insert $(xml_attr_encode "$1")\">" junit_insert="$junit_insert $(xml_attr_encode \ - "$(printf '%s\n' "$@" | sed 1d)")" + "$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")" + >"$GIT_TEST_TEE_OUTPUT_FILE" junit_insert="$junit_insert</failure>" write_junit_xml_testcase "$1" " $junit_insert" fi @@ -736,6 +764,10 @@ test_start_ () { if test -n "$write_junit_xml" then junit_start=$(test-tool date getnanos) + + # truncate output + test -z "$GIT_TEST_TEE_OUTPUT_FILE" || + >"$GIT_TEST_TEE_OUTPUT_FILE" fi } From a0970ebd422852e1db4f03166a0d1bc8a411f79a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 31 Aug 2018 16:26:32 +0200 Subject: [PATCH 236/406] tests: record more stderr with --write-junit-xml in case of failure Sometimes, failures in a test case are actually caused by issues in earlier test cases. To make it easier to see those issues, let's attach the output from before the failing test case (i.e. stdout/stderr since the previous failing test case, or the start of the test script). This will be visible in the "Attachments" of the details of the failed test. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 9e9862b0f1efca..5db495fda94bd4 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -481,6 +481,9 @@ test_failure_ () { "$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")" >"$GIT_TEST_TEE_OUTPUT_FILE" junit_insert="$junit_insert</failure>" + junit_insert="$junit_insert<system-err>$(xml_attr_encode \ + "$(cat "$GIT_TEST_TEE_OUTPUT_FILE.err")")</system-err>" + >"$GIT_TEST_TEE_OUTPUT_FILE.err" write_junit_xml_testcase "$1" " $junit_insert" fi test_failure=$(($test_failure + 1)) @@ -765,9 +768,12 @@ test_start_ () { then junit_start=$(test-tool date getnanos) - # truncate output - test -z "$GIT_TEST_TEE_OUTPUT_FILE" || - >"$GIT_TEST_TEE_OUTPUT_FILE" + # append to future <system-err>; truncate output + test -z "$GIT_TEST_TEE_OUTPUT_FILE" || { + cat "$GIT_TEST_TEE_OUTPUT_FILE" \ + >>"$GIT_TEST_TEE_OUTPUT_FILE.err" + >"$GIT_TEST_TEE_OUTPUT_FILE" + } fi } From 081208bb5861ba7c68acd4050e0a99fcfb1d19aa Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 31 Aug 2018 23:53:22 +0200 Subject: [PATCH 237/406] README: add a build badge (status of the Azure Pipelines build) Just like so many other OSS projects, we now also have a build badge. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f920a42fad3423..bf4780c22d4d95 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Build Status](https:/dev.azure.com/git/git/_apis/build/status/test-git.git)](https://dev.azure.com/git/git/_build/latest?definitionId=2) + Git - fast, scalable, distributed revision control system ========================================================= From de896436df7be6a9068ddde945bbbab528a856ad Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 4 Sep 2018 13:29:26 +0200 Subject: [PATCH 238/406] travis: fix skipping tagged releases When building a PR, TRAVIS_BRANCH refers to the *target branch*. Therefore, if a PR targets `master`, and `master` happened to be tagged, we skipped the build by mistake. Fix this by using TRAVIS_PULL_REQUEST_BRANCH (i.e. the *source branch*) when available, falling back to TRAVIS_BRANCH (i.e. for CI builds, also known as "push builds"). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- ci/lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/lib.sh b/ci/lib.sh index 584abcd529bfa5..e1858ae6099d7b 100755 --- a/ci/lib.sh +++ b/ci/lib.sh @@ -3,7 +3,7 @@ if test true = "$TRAVIS" then # We are running within Travis CI - CI_BRANCH="$TRAVIS_BRANCH" + CI_BRANCH="${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}" CI_COMMIT="$TRAVIS_COMMIT" CI_JOB_ID="$TRAVIS_JOB_ID" CI_JOB_NUMBER="$TRAVIS_JOB_NUMBER" From 5f25533a30659f1078dfe28f9836ca2ffeb47887 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 24 Jul 2015 16:01:09 +0200 Subject: [PATCH 239/406] Add a Code of Conduct It is better to state clearly expectations and intentions than to assume quietly that everybody agrees. This Code of Conduct is the Open Code of Conduct as per http://todogroup.org/opencodeofconduct/ (the only modifications are the adjustments to reflect that there is no "response team" in addition to the Git for Windows maintainer, and the addition of the link to the Open Code of Conduct itself). [Completely revamped, based on the Covenant 1.4 by Brendan Forster] Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- CODE_OF_CONDUCT.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000000..590c642cfb5e15 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,76 @@ +# Git for Windows Code of Conduct + +This code of conduct outlines our expectations for participants within the **Git for Windows** community, as well as steps to reporting unacceptable behavior. We are committed to providing a welcoming and inspiring community for all and expect our code of conduct to be honored. Anyone who violates this code of conduct may be banned from the community. + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at **johannes.schindelin@gmx.de**. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ From e4a823cbb46145d0434bb68e3e5e765c5617a356 Mon Sep 17 00:00:00 2001 From: Derrick Stolee <dstolee@microsoft.com> Date: Thu, 1 Mar 2018 12:10:14 -0500 Subject: [PATCH 240/406] CONTRIBUTING.md: add guide for first-time contributors Getting started contributing to Git can be difficult on a Windows machine. CONTRIBUTING.md contains a guide to getting started, including detailed steps for setting up build tools, running tests, and submitting patches to upstream. [includes an example by Pratik Karki how to submit v2, v3, v4, etc.] Signed-off-by: Derrick Stolee <dstolee@microsoft.com> --- CONTRIBUTING.md | 427 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 427 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000000000..6bf532d705d1d5 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,427 @@ +How to Contribute to Git for Windows +==================================== + +Git was originally designed for Unix systems and still today, all the build tools for the Git +codebase assume you have standard Unix tools available in your path. If you have an open-source +mindset and want to start contributing to Git, but primarily use a Windows machine, then you may +have trouble getting started. This guide is for you. + +Get the Source +-------------- + +Clone the [GitForWindows repository on GitHub](https://github.com/git-for-windows/git). +It is helpful to create your own fork for storing your development branches. + +Windows uses different line endings than Unix systems. See +[this GitHub article on working with line endings](https://help.github.com/articles/dealing-with-line-endings/#refreshing-a-repository-after-changing-line-endings) +if you have trouble with line endings. + +Build the Source +---------------- + +First, download and install the latest [Git for Windows SDK (64-bit)](https://github.com/git-for-windows/build-extra/releases/latest). +When complete, you can run the Git SDK, which creates a new Git Bash terminal window with +the additional development commands, such as `make`. + + As of time of writing, the SDK uses a different credential manager, so you may still want to use normal Git + Bash for interacting with your remotes. Alternatively, use SSH rather than HTTPS and + avoid credential manager problems. + +You should now be ready to type `make` from the root of your `git` source directory. +Here are some helpful variations: + +* `make -j[N] DEVELOPER=1`: Compile new sources using up to N concurrent processes. + The `DEVELOPER` flag turns on all warnings; code failing these warnings will not be + accepted upstream ("upstream" = "the core Git project"). +* `make clean`: Delete all compiled files. + +When running `make`, you can use `-j$(nproc)` to automatically use the number of processors +on your machine as the number of concurrent build processes. + +You can go deeper on the Windows-specific build process by reading the +[technical overview](https://github.com/git-for-windows/git/wiki/Technical-overview) or the +[guide to compiling Git with Visual Studio](https://github.com/git-for-windows/git/wiki/Compiling-Git-with-Visual-Studio). + +## Building `git` on Windows with Visual Studio + +The typical approach to building `git` is to use the standard `Makefile` with GCC, as +above. Developers working in a Windows environment may want to instead build with the +[Microsoft Visual C++ compiler and libraries toolset (MSVC)](https://blogs.msdn.microsoft.com/vcblog/2017/03/07/msvc-the-best-choice-for-windows/). +There are a few benefits to using MSVC over GCC during your development, including creating +symbols for debugging and [performance tracing](https://github.com/Microsoft/perfview#perfview-overview). + +There are two ways to build Git for Windows using MSVC. Each have their own merits. + +### Using SDK Command Line + +Use one of the following commands from the SDK Bash window to build Git for Windows: + +``` + make MSVC=1 -j12 + make MSVC=1 DEBUG=1 -j12 +``` + +The first form produces release-mode binaries; the second produces debug-mode binaries. +Both forms produce PDB files and can be debugged. However, the first is best for perf +tracing and the second is best for single-stepping. + +You can then open Visual Studio and select File -> Open -> Project/Solution and select +the compiled `git.exe` file. This creates a basic solution and you can use the debugging +and performance tracing tools in Visual Studio to monitor a Git process. Use the Debug +Properties page to set the working directory and command line arguments. + +Be sure to clean up before switching back to GCC (or to switch between debug and +release MSVC builds): + +``` + make MSVC=1 -j12 clean + make MSVC=1 DEBUG=1 -j12 clean +``` + +### Using `vs/master` Solution + +If you prefer working in Visual Studio with a solution full of projects, then there is a +branch in Git for Windows called [`vs/master`](https://github.com/git-for-windows/git/branches). +This branch is kept up-to-date with the `master` branch, except it has one more commit that +contains the solution and project files. Read [the wiki page on this approach](https://github.com/git-for-windows/git/wiki/Compiling-Git-with-Visual-Studio) for more information. + +I want to make a small warning before you start working on the `vs/master` branch. If you +create a new topic branch based on `vs/master`, you will need to rebase onto `master` before +you can submit a pull request. The commit at the tip of `vs/master` is not intended to ever +become part of the `master` branch. If you created a branch, `myTopic` based on `vs/master`, +then use the following rebase command to move it onto the `master` branch: + +``` +git rebase --onto master vs/master myTopic +``` + +What to Change? +--------------- + +Many new contributors ask: What should I start working on? + +One way to win big with the open-source community is to look at the +[issues page](https://github.com/git-for-windows/git/issues) and see if there are any issues that +you can fix quickly, or if anything catches your eye. + +You can also look at [the unofficial Chromium issues page](https://crbug.com/git) for +multi-platform issues. You can look at recent user questions on +[the Git mailing list](https://public-inbox.org/git). + +Or you can "scratch your own itch", i.e. address an issue you have with Git. The team at Microsoft where the Git for Windows maintainer works, for example, is focused almost entirely on [improving performance](https://blogs.msdn.microsoft.com/devops/2018/01/11/microsofts-performance-contributions-to-git-in-2017/). +We approach our work by finding something that is slow and try to speed it up. We start our +investigation by reliably reproducing the slow behavior, then running that example using +the MSVC build and tracing the results in PerfView. + +You could also think of something you wish Git could do, and make it do that thing! The +only concern I would have with this approach is whether or not that feature is something +the community also wants. If this excites you though, go for it! Don't be afraid to +[get involved in the mailing list](http://vger.kernel.org/vger-lists.html#git) early for +feedback on the idea. + +Test Your Changes +----------------- + +After you make your changes, it is important that you test your changes. Manual testing is +important, but checking and extending the existing test suite is even more important. You +want to run the functional tests to see if you broke something else during your change, and +you want to extend the functional tests to be sure no one breaks your feature in the future. + +### Functional Tests + +Navigate to the `t/` directory and type `make` to run all tests or use `prove` as +[described in the Git for Windows wiki](https://github.com/git-for-windows/git/wiki/Building-Git): + +``` +prove -j12 --state=failed,save ./t[0-9]*.sh +``` + +You can also run each test directly by running the corresponding shell script with a name +like `tNNNN-descriptor.sh`. + +If you are adding new functionality, you may need to create unit tests by creating +helper commands that test a very limited action. These commands are stored in `t/helpers`. +When adding a helper, be sure to add a line to `t/Makefile` and to the `.gitignore` for the +binary file you add. The Git community prefers functional tests using the full `git` +executable, so try to exercise your new code using `git` commands before creating a test +helper. + +To find out why a test failed, repeat the test with the `-x -v -d -i` options and then +navigate to the appropriate "trash" directory to see the data shape that was used for the +test failed step. + +Read [`t/README`](t/README) for more details. + +### Performance Tests + +If you are working on improving performance, you will need to be acquainted with the +performance tests in `t/perf`. There are not too many performance tests yet, but adding one +as your first commit in a patch series helps to communicate the boost your change provides. + +To check the change in performance across multiple versions of `git`, you can use the +`t/perf/run` script. For example, to compare the performance of `git rev-list` across the +`core/master` and `core/next` branches compared to a `topic` branch, you can run + +``` +cd t/perf +./run core/master core/next topic -- p0001-rev-list.sh +``` + +You can also set certain environment variables to help test the performance on different +repositories or with more repetitions. The full list is available in +[the `t/perf/README` file](t/perf/README), +but here are a few important ones: + +``` +GIT_PERF_REPO=/path/to/repo +GIT_PERF_LARGE_REPO=/path/to/large/repo +GIT_PERF_REPEAT_COUNT=10 +``` + +When running the performance tests on Linux, you may see a message "Can't locate JSON.pm in +@INC" and that means you need to run `sudo cpanm install JSON` to get the JSON perl package. + +For running performance tests, it can be helpful to set up a few repositories with strange +data shapes, such as: + +**Many objects:** Clone repos such as [Kotlin](https://github.com/jetbrains/kotlin), [Linux](https://github.com/torvalds/linux), or [Android](https://source.android.com/setup/downloading). + +**Many pack-files:** You can split a fresh clone into multiple pack-files of size at most +16MB by running `git repack -adfF --max-pack-size=16m`. See the +[`git repack` documentation](https://git-scm.com/docs/git-repack) for more information. +You can count the number of pack-files using `ls .git/objects/pack/*.pack | wc -l`. + +**Many loose objects:** If you already split your repository into multiple pack-files, then +you can pick one to split into loose objects using `cat .git/objects/pack/[id].pack | git unpack-objects`; +delete the `[id].pack` and `[id].idx` files after this. You can count the number of loose +bjects using `ls .git/objects/??/* | wc -l`. + +**Deep history:** Usually large repositories also have deep histories, but you can use the +[test-many-commits-1m repo](https://github.com/cirosantilli/test-many-commits-1m/) to +target deep histories without the overhead of many objects. One issue with this repository: +there are no merge commits, so you will need to use a different repository to test a "wide" +commit history. + +**Large Index:** You can generate a large index and repo by using the scripts in +`t/perf/repos`. There are two scripts. `many-files.sh` which will generate a repo with +same tree and blobs but different paths. Using `many-files.sh -d 5 -w 10 -f 9` will create +a repo with ~1 million entries in the index. `inflate-repo.sh` will use an existing repo +and copy the current work tree until it is a specified size. + +Test Your Changes on Linux +-------------------------- + +It can be important to work directly on the [core Git codebase](https://github.com/git/git), +such as a recent commit into the `master` or `next` branch that has not been incorporated +into Git for Windows. Also, it can help to run functional and performance tests on your +code in Linux before submitting patches to the mailing list, which focuses on many platforms. +The differences between Windows and Linux are usually enough to catch most cross-platform +issues. + +### Using the Windows Subsystem for Linux + +The [Windows Subsystem for Linux (WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10) +allows you to [install Ubuntu Linux as an app](https://www.microsoft.com/en-us/store/p/ubuntu/9nblggh4msv6) +that can run Linux executables on top of the Windows kernel. Internally, +Linux syscalls are interpreted by the WSL, everything else is plain Ubuntu. + +First, open WSL (either type "Bash" in Cortana, or execute "bash.exe" in a CMD window). +Then install the prerequisites, and `git` for the initial clone: + +``` +sudo apt-get update +sudo apt-get install git gcc make libssl-dev libcurl4-openssl-dev \ + libexpat-dev tcl tk gettext git-email zlib1g-dev +``` + +Then, clone and build: + +``` +git clone https://github.com/git-for-windows/git +cd git +git remote add -f upstream https://github.com/git/git +make +``` + +Be sure to clone into `/home/[user]/` and not into any folder under `/mnt/?/` or your build +will fail due to colons in file names. + +### Using a Linux Virtual Machine with Hyper-V + +If you prefer, you can use a virtual machine (VM) to run Linux and test your changes in the +full environment. The test suite runs a lot faster on Linux than on Windows or with the WSL. +You can connect to the VM using an SSH terminal like +[PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/). + +The following instructions are for using Hyper-V, which is available in some versions of Windows. +There are many virtual machine alternatives available, if you do not have such a version installed. + +* [Download an Ubuntu Server ISO](https://www.ubuntu.com/download/server). +* Open [Hyper-V Manager](https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v). +* [Set up a virtual switch](https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/connect-to-network) + so your VM can reach the network. +* Select "Quick Create", name your machine, select the ISO as installation source, and un-check + "This virtual machine will run Windows." +* Go through the Ubuntu install process, being sure to select to install OpenSSH Server. +* When install is complete, log in and check the SSH server status with `sudo service ssh status`. + * If the service is not found, install with `sudo apt-get install openssh-server`. + * If the service is not running, then use `sudo service ssh start`. +* Use `shutdown -h now` to shutdown the VM, go to the Hyper-V settings for the VM, expand Network Adapter + to select "Advanced Features", and set the MAC address to be static (this can save your VM from losing + network if shut down incorrectly). +* Provide as many cores to your VM as you can (for parallel builds). +* Restart your VM, but do not connect. +* Use `ssh` in Git Bash, download [PuTTY](http://www.putty.org/), or use your favorite SSH client to connect to the VM through SSH. + +In order to build and use `git`, you will need the following libraries via `apt-get`: + +``` +sudo apt-get update +sudo apt-get install git gcc make libssl-dev libcurl4-openssl-dev \ + libexpat-dev tcl tk gettext git-email zlib1g-dev +``` + +To get your code from your Windows machine to the Linux VM, it is easiest to push the branch to your fork of Git and clone your fork in the Linux VM. + +Don't forget to set your `git` config with your preferred name, email, and editor. + +Polish Your Commits +------------------- + +Before submitting your patch, be sure to read the [coding guidelines](https://github.com/git/git/blob/master/Documentation/CodingGuidelines) +and check your code to match as best you can. This can be a lot of effort, but it saves +time during review to avoid style issues. + +The other possibly major difference between the mailing list submissions and GitHub PR workflows +is that each commit will be reviewed independently. Even if you are submitting a +patch series with multiple commits, each commit must stand on it's own and be reviewable +by itself. Make sure the commit message clearly explain the why of the commit not the how. +Describe what is wrong with the current code and how your changes have made the code better. + +When preparing your patch, it is important to put yourself in the shoes of the Git community. +Accepting a patch requires more justification than approving a pull request from someone on +your team. The community has a stable product and is responsible for keeping it stable. If +you introduce a bug, then they cannot count on you being around to fix it. When you decided +to start work on a new feature, they were not part of the design discussion and may not +even believe the feature is worth introducing. + +Questions to answer in your patch message (and commit messages) may include: +* Why is this patch necessary? +* How does the current behavior cause pain for users? +* What kinds of repositories are necessary for noticing a difference? +* What design options did you consider before writing this version? Do you have links to + code for those alternate designs? +* Is this a performance fix? Provide clear performance numbers for various well-known repos. + +Here are some other tips that we use when cleaning up our commits: + +* Commit messages should be wrapped at 76 columns per line (or less; 72 is also a + common choice). +* Make sure the commits are signed off using `git commit (-s|--signoff)`. See + [SubmittingPatches](https://github.com/git/git/blob/v2.8.1/Documentation/SubmittingPatches#L234-L286) + for more details about what this sign-off means. +* Check for whitespace errors using `git diff --check [base]...HEAD` or `git log --check`. +* Run `git rebase --whitespace=fix` to correct upstream issues with whitespace. +* Become familiar with interactive rebase (`git rebase -i`) because you will be reordering, + squashing, and editing commits as your patch or series of patches is reviewed. +* Make sure any shell scripts that you add have the executable bit set on them. This is + usually for test files that you add in the `/t` directory. You can use + `git add --chmod=+x [file]` to update it. You can test whether a file is marked as executable + using `git ls-files --stage \*.sh`; the first number is 100755 for executable files. +* Your commit titles should match the "area: change description" format. Rules of thumb: + * Choose "<area>: " prefix appropriately. + * Keep the description short and to the point. + * The word that follows the "<area>: " prefix is not capitalized. + * Do not include a full-stop at the end of the title. + * Read a few commit messages -- using `git log origin/master`, for instance -- to + become acquainted with the preferred commit message style. +* Build source using `make DEVELOPER=1` for extra-strict compiler warnings. + +Submit Your Patch +----------------- + +Git for Windows [accepts pull requests on GitHub](https://github.com/git-for-windows/git/pulls), but +these are reserved for Windows-specific improvements. For core Git, submissions are accepted on +[the Git mailing list](https://public-inbox.org/git). + +### Configure Git to Send Emails + +There are a bunch of options for configuring the `git send-email` command. These options can +be found in the documentation for +[`git config`](https://git-scm.com/docs/git-config) and +[`git send-email`](https://git-scm.com/docs/git-send-email). + +``` +git config --global sendemail.smtpserver <smtp server> +git config --global sendemail.smtpserverport 587 +git config --global sendemail.smtpencryption tls +git config --global sendemail.smtpuser <email address> +``` + +To avoid storing your password in the config file, store it in the Git credential manager: + +``` +$ git credential fill +protocol=smtp +host=<stmp server> +username=<email address> +password=password +``` + +Before submitting a patch, read the [Git documentation on submitting patches](https://github.com/git/git/blob/master/Documentation/SubmittingPatches). + +To construct a patch set, use the `git format-patch` command. There are three important options: + +* `--cover-letter`: If specified, create a `[v#-]0000-cover-letter.patch` file that can be + edited to describe the patch as a whole. If you previously added a branch description using + `git branch --edit-description`, you will end up with a 0/N mail with that description and + a nice overall diffstat. +* `--in-reply-to=[Message-ID]`: This will mark your cover letter as replying to the given + message (which should correspond to your previous iteration). To determine the correct Message-ID, + find the message you are replying to on [public-inbox.org/git](https://public-inbox.org/git) and take + the ID from between the angle brackets. + +* `--subject-prefix=[prefix]`: This defaults to [PATCH]. For subsequent iterations, you will want to + override it like `--subject-prefix="[PATCH v2]"`. You can also use the `-v` option to have it + automatically generate the version number in the patches. + +If you have multiple commits and use the `--cover-letter` option be sure to open the +`0000-cover-letter.patch` file to update the subject and add some details about the overall purpose +of the patch series. + +### Examples + +To generate a single commit patch file: +``` +git format-patch -s -o [dir] -1 +``` +To generate four patch files from the last three commits with a cover letter: +``` +git format-patch --cover-letter -s -o [dir] HEAD~4 +``` +To generate version 3 with four patch files from the last four commits with a cover letter: +``` +git format-patch --cover-letter -s -o [dir] -v 3 HEAD~4 +``` + +### Submit the Patch + +Run [`git send-email`](https://git-scm.com/docs/git-send-email), starting with a test email: + +``` +git send-email --to=yourself@address.com [dir with patches]/*.patch +``` + +After checking the receipt of your test email, you can send to the list and to any +potentially interested reviewers. + +``` +git send-email --to=git@vger.kernel.org --cc=<email1> --cc=<email2> [dir with patches]/*.patch +``` + +To submit a nth version patch (say version 3): + +``` +git send-email --to=git@vger.kernel.org --cc=<email1> --cc=<email2> \ + --in-reply-to=<the message id of cover letter of patch v2> [dir with patches]/*.patch +``` From 9aba595512ed4e1159ea403193bf9f71932283b1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 10 Jan 2014 16:16:03 -0600 Subject: [PATCH 241/406] README.md: Add a Windows-specific preamble Includes touch-ups by Philip Oakley. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf4780c22d4d95..f0eb4961c56acb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,20 @@ -[![Build Status](https:/dev.azure.com/git/git/_apis/build/status/test-git.git)](https://dev.azure.com/git/git/_build/latest?definitionId=2) +Git for Windows +=============== + +[![Build Status (Windows/macOS/Linux)](https://dev.azure.com/git-for-windows/git/_apis/build/status/git-for-windows.git)](https://dev.azure.com/git-for-windows/git/_build/latest?definitionId=17) +[![Build Status (core.autocrlf=true)](https://dev.azure.com/Git-for-Windows/git/_apis/build/status/TestWithAutoCRLF)](https://dev.azure.com/Git-for-Windows/git/_build/latest?definitionId=3) +[![Join the chat at https://gitter.im/git-for-windows/git](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/git-for-windows/git?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +This is [Git for Windows](http://git-for-windows.github.io/), the Windows port +of [Git](http://git-scm.com/). + +The Git for Windows project is run using a [governance +model](http://git-for-windows.github.io/governance-model.html). If you +encounter problems, you can report them as [GitHub +issues](https://github.com/git-for-windows/git/issues), discuss them on Git +for Windows' [Google Group](http://groups.google.com/group/git-for-windows), +and [contribute bug +fixes](https://github.com/git-for-windows/git/wiki/How-to-participate). Git - fast, scalable, distributed revision control system ========================================================= @@ -29,7 +45,7 @@ CVS users may also want to read [Documentation/gitcvs-migration.txt][] (`man gitcvs-migration` or `git help cvs-migration` if git is installed). -The user discussion and development of Git take place on the Git +The user discussion and development of core Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read [Documentation/SubmittingPatches][] for instructions on patch submission). @@ -37,6 +53,7 @@ To subscribe to the list, send an email with just "subscribe git" in the body to majordomo@vger.kernel.org. The mailing list archives are available at <https://public-inbox.org/git/>, <http://marc.info/?l=git> and other archival sites. +The core git mailing list is plain text (no HTML!). Issues which are security relevant should be disclosed privately to the Git Security mailing list <git-security@googlegroups.com>. From 450322b2bfa7cebf010f96bf10fbb171c21970e4 Mon Sep 17 00:00:00 2001 From: Jeff King <peff@peff.net> Date: Fri, 16 Nov 2018 04:43:59 -0500 Subject: [PATCH 242/406] bundle: dup() output descriptor closer to point-of-use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When writing a bundle to a file, the bundle code actually creates "your.bundle.lock" using our lockfile interface. We feed that output descriptor to a child git-pack-objects via run-command, which has the quirk that it closes the output descriptor in the parent. To avoid confusing the lockfile code (which still thinks the descriptor is valid), we dup() it, and operate on the duplicate. However, this has a confusing side effect: after the dup() but before we call pack-objects, we have _two_ descriptors open to the lockfile. If we call die() during that time, the lockfile code will try to clean up the partially-written file. It knows to close() the file before unlinking, since on some platforms (i.e., Windows) the open file would block the deletion. But it doesn't know about the duplicate descriptor. On Windows, triggering an error at the right part of the code will result in the cleanup failing and the lockfile being left in the filesystem. We can solve this by moving the dup() much closer to start_command(), shrinking the window in which we have the second descriptor open. It's easy to place this in such a way that no die() is possible. We could still die due to a signal in the exact wrong moment, but we already tolerate races there (e.g., a signal could come before we manage to put the file on the cleanup list in the first place). As a bonus, this shields create_bundle() itself from the duplicate-fd trick, and we can simplify its error handling (note that the lock rollback now happens unconditionally, but that's OK; it's a noop if we didn't open the lock in the first place). The included test uses an empty bundle to cause a failure at the right spot in the code, because that's easy to trigger (the other likely errors are write() problems like ENOSPC). Note that it would already pass on non-Windows systems (because they are happy to unlink an already-open file). Based-on-a-patch-by: Gaël Lhez <gael.lhez@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- bundle.c | 39 ++++++++++++++++++--------------------- t/t5607-clone-bundle.sh | 6 ++++++ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/bundle.c b/bundle.c index 24cbe409863a83..0ae8c2d796b284 100644 --- a/bundle.c +++ b/bundle.c @@ -243,7 +243,7 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) } -/* Write the pack data to bundle_fd, then close it if it is > 1. */ +/* Write the pack data to bundle_fd */ static int write_pack_data(int bundle_fd, struct rev_info *revs) { struct child_process pack_objects = CHILD_PROCESS_INIT; @@ -256,6 +256,20 @@ static int write_pack_data(int bundle_fd, struct rev_info *revs) pack_objects.in = -1; pack_objects.out = bundle_fd; pack_objects.git_cmd = 1; + + /* + * start_command() will close our descriptor if it's >1. Duplicate it + * to avoid surprising the caller. + */ + if (pack_objects.out > 1) { + pack_objects.out = dup(pack_objects.out); + if (pack_objects.out < 0) { + error_errno(_("unable to dup bundle descriptor")); + child_process_clear(&pack_objects); + return -1; + } + } + if (start_command(&pack_objects)) return error(_("Could not spawn pack-objects")); @@ -421,21 +435,10 @@ int create_bundle(struct bundle_header *header, const char *path, bundle_to_stdout = !strcmp(path, "-"); if (bundle_to_stdout) bundle_fd = 1; - else { + else bundle_fd = hold_lock_file_for_update(&lock, path, LOCK_DIE_ON_ERROR); - /* - * write_pack_data() will close the fd passed to it, - * but commit_lock_file() will also try to close the - * lockfile's fd. So make a copy of the file - * descriptor to avoid trying to close it twice. - */ - bundle_fd = dup(bundle_fd); - if (bundle_fd < 0) - die_errno("unable to dup file descriptor"); - } - /* write signature */ write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature)); @@ -463,10 +466,8 @@ int create_bundle(struct bundle_header *header, const char *path, goto err; /* write pack */ - if (write_pack_data(bundle_fd, &revs)) { - bundle_fd = -1; /* already closed by the above call */ + if (write_pack_data(bundle_fd, &revs)) goto err; - } if (!bundle_to_stdout) { if (commit_lock_file(&lock)) @@ -474,11 +475,7 @@ int create_bundle(struct bundle_header *header, const char *path, } return 0; err: - if (!bundle_to_stdout) { - if (0 <= bundle_fd) - close(bundle_fd); - rollback_lock_file(&lock); - } + rollback_lock_file(&lock); return -1; } diff --git a/t/t5607-clone-bundle.sh b/t/t5607-clone-bundle.sh index 348d9b3bc7ad3e..cf39e9e2437f06 100755 --- a/t/t5607-clone-bundle.sh +++ b/t/t5607-clone-bundle.sh @@ -71,4 +71,10 @@ test_expect_success 'prerequisites with an empty commit message' ' git bundle verify bundle ' +test_expect_success 'failed bundle creation does not leave cruft' ' + # This fails because the bundle would be empty. + test_must_fail git bundle create fail.bundle master..master && + test_path_is_missing fail.bundle.lock +' + test_done From 1a4a41a6825b9b36af9301d95a4cbdc404b1939e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 19 Jun 2017 17:11:16 +0200 Subject: [PATCH 243/406] mingw (t5580): document bug when cloning from backslashed UNC paths Due to a quirk in Git's method to spawn git-upload-pack, there is a problem when passing paths with backslashes in them: Git will force the command-line through the shell, which has different quoting semantics in Git for Windows (being an MSYS2 program) than regular Win32 executables such as git.exe itself. The symptom is that the first of the two backslashes in UNC paths of the form \\myserver\folder\repository.git is *stripped off*. Document this bug by introducing a test case. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5580-clone-push-unc.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/t/t5580-clone-push-unc.sh b/t/t5580-clone-push-unc.sh index ba548df4a91824..c3703765f47d19 100755 --- a/t/t5580-clone-push-unc.sh +++ b/t/t5580-clone-push-unc.sh @@ -40,6 +40,11 @@ test_expect_success clone ' git clone "file://$UNCPATH" clone ' +test_expect_failure 'clone with backslashed path' ' + BACKSLASHED="$(echo "$UNCPATH" | tr / \\\\)" && + git clone "$BACKSLASHED" backslashed +' + test_expect_success push ' ( cd clone && From aa6943d72d4136f7b2302ca58796b085611ead23 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 19 Jun 2017 16:35:17 +0200 Subject: [PATCH 244/406] mingw: special-case arguments to `sh` The MSYS2 runtime does its best to emulate the command-line wildcard expansion and de-quoting which would be performed by the calling Unix shell on Unix systems. Those Unix shell quoting rules differ from the quoting rules applying to Windows' cmd and Powershell, making it a little awkward to quote command-line parameters properly when spawning other processes. In particular, git.exe passes arguments to subprocesses that are *not* intended to be interpreted as wildcards, and if they contain backslashes, those are not to be interpreted as escape characters, e.g. when passing Windows paths. Note: this is only a problem when calling MSYS2 executables, not when calling MINGW executables such as git.exe. However, we do call MSYS2 executables frequently, most notably when setting the use_shell flag in the child_process structure. There is no elegant way to determine whether the .exe file to be executed is an MSYS2 program or a MINGW one. But since the use case of passing a command line through the shell is so prevalent, we need to work around this issue at least when executing sh.exe. Let's introduce an ugly, hard-coded test whether argv[0] is "sh", and whether it refers to the MSYS2 Bash, to determine whether we need to quote the arguments differently than usual. That still does not fix the issue completely, but at least it is something. Incidentally, this also fixes the problem where `git clone \\server\repo` failed due to incorrect handling of the backslashes when handing the path to the git-upload-pack process. We need to take care to quote not only whitespace, but also curly brackets. As aliases frequently go through the MSYS2 Bash, and as aliases frequently get parameters such as HEAD@{yesterday}, let's make sure that this does not regress by adding a test case for that. Helped-by: Kim Gybels <kgybels@infogroep.be> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 63 ++++++++++++++++++++++++++++++++++++++- t/t0061-run-command.sh | 10 +++++++ t/t5580-clone-push-unc.sh | 2 +- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 34731a47156e69..f01849f4e9fe16 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -976,7 +976,7 @@ char *mingw_getcwd(char *pointer, int len) * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx * (Parsing C++ Command-Line Arguments) */ -static const char *quote_arg(const char *arg) +static const char *quote_arg_msvc(const char *arg) { /* count chars to quote */ int len = 0, n = 0; @@ -1031,6 +1031,37 @@ static const char *quote_arg(const char *arg) return q; } +#include "quote.h" + +static const char *quote_arg_msys2(const char *arg) +{ + struct strbuf buf = STRBUF_INIT; + const char *p2 = arg, *p; + + for (p = arg; *p; p++) { + int ws = isspace(*p); + if (!ws && *p != '\\' && *p != '"' && *p != '{') + continue; + if (!buf.len) + strbuf_addch(&buf, '"'); + if (p != p2) + strbuf_add(&buf, p2, p - p2); + if (!ws && *p != '{') + strbuf_addch(&buf, '\\'); + p2 = p; + } + + if (p == arg) + strbuf_addch(&buf, '"'); + else if (!buf.len) + return arg; + else + strbuf_add(&buf, p2, p - p2), + + strbuf_addch(&buf, '"'); + return strbuf_detach(&buf, 0); +} + static const char *parse_interpreter(const char *cmd) { static char buf[100]; @@ -1185,6 +1216,34 @@ struct pinfo_t { static struct pinfo_t *pinfo = NULL; CRITICAL_SECTION pinfo_cs; +static int is_msys2_sh(const char *cmd) +{ + if (cmd && !strcmp(cmd, "sh")) { + static int ret = -1; + char *p; + + if (ret >= 0) + return ret; + + p = path_lookup(cmd, 0); + if (!p) + ret = 0; + else { + size_t len = strlen(p); + ret = len > 15 && + is_dir_sep(p[len - 15]) && + !strncasecmp(p + len - 14, "usr", 3) && + is_dir_sep(p[len - 11]) && + !strncasecmp(p + len - 10, "bin", 3) && + is_dir_sep(p[len - 7]) && + !strcasecmp(p + len - 6, "sh.exe"); + free(p); + } + return ret; + } + return 0; +} + static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv, const char *dir, int prepend_cmd, int fhin, int fhout, int fherr) @@ -1196,6 +1255,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen unsigned flags = CREATE_UNICODE_ENVIRONMENT; BOOL ret; HANDLE cons; + const char *(*quote_arg)(const char *arg) = + is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc; do_unset_environment_variables(); diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 3e131c5325e363..bc740737467f48 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -177,4 +177,14 @@ test_expect_success 'GIT_TRACE with environment variables' ' ) ' +test_expect_success MINGW 'verify curlies are quoted properly' ' + : force the rev-parse through the MSYS2 Bash && + git -c alias.r="!git rev-parse" r -- a{b}c >actual && + cat >expect <<-\EOF && + -- + a{b}c + EOF + test_cmp expect actual +' + test_done diff --git a/t/t5580-clone-push-unc.sh b/t/t5580-clone-push-unc.sh index c3703765f47d19..217adf3a632b49 100755 --- a/t/t5580-clone-push-unc.sh +++ b/t/t5580-clone-push-unc.sh @@ -40,7 +40,7 @@ test_expect_success clone ' git clone "file://$UNCPATH" clone ' -test_expect_failure 'clone with backslashed path' ' +test_expect_success 'clone with backslashed path' ' BACKSLASHED="$(echo "$UNCPATH" | tr / \\\\)" && git clone "$BACKSLASHED" backslashed ' From 9abda7ed8405477098c0da4a540daaf3cb917b56 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 13 Jul 2017 14:28:42 +0200 Subject: [PATCH 245/406] t5580: test cloning without file://, test fetching via UNC paths It gets a bit silly to add the commands to the name of the test script, so let's just rename it while we're testing more UNC stuff. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/{t5580-clone-push-unc.sh => t5580-unc-paths.sh} | 12 ++++++++++++ 1 file changed, 12 insertions(+) rename t/{t5580-clone-push-unc.sh => t5580-unc-paths.sh} (88%) diff --git a/t/t5580-clone-push-unc.sh b/t/t5580-unc-paths.sh similarity index 88% rename from t/t5580-clone-push-unc.sh rename to t/t5580-unc-paths.sh index 217adf3a632b49..254fefccdeafea 100755 --- a/t/t5580-clone-push-unc.sh +++ b/t/t5580-unc-paths.sh @@ -40,11 +40,23 @@ test_expect_success clone ' git clone "file://$UNCPATH" clone ' +test_expect_success 'clone without file://' ' + git clone "$UNCPATH" clone-without-file +' + test_expect_success 'clone with backslashed path' ' BACKSLASHED="$(echo "$UNCPATH" | tr / \\\\)" && git clone "$BACKSLASHED" backslashed ' +test_expect_success fetch ' + git init to-fetch && + ( + cd to-fetch && + git fetch "$UNCPATH" master + ) +' + test_expect_success push ' ( cd clone && From 8bfd0b75139011b97781a41891526af3e5c1fde7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 28 Nov 2017 18:02:51 +0100 Subject: [PATCH 246/406] Mark .bat files as requiring CR/LF endings Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 1bdc91e282c539..9fc30e5c76831e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,6 +5,7 @@ *.pl eof=lf diff=perl *.pm eol=lf diff=perl *.py eol=lf diff=python +*.bat eol=crlf /Documentation/git-*.txt eol=lf /command-list.txt eol=lf /GIT-VERSION-GEN eol=lf From 461e4ca6970a5a33da8ca07db88b77e7c8ddd559 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 25 Oct 2018 11:11:48 +0200 Subject: [PATCH 247/406] t0001 (mingw): do not expect specific order of stdout/stderr When redirecting stdout/stderr to the same file, we cannot guarantee that stdout will come first. In fact, in this test case, it seems that an MSVC build always prints stderr first. In any case, this test case does not want to verify the *order* but the *presence* of both outputs, so let's relax the test a little. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t0001-init.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 97a338e5cdd9c7..be69a10d73a80a 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -474,7 +474,8 @@ test_expect_success MINGW 'redirect std handles' ' GIT_REDIRECT_STDERR="2>&1" \ git rev-parse --git-dir --verify refs/invalid && printf ".git\nfatal: Needed a single revision\n" >expect && - test_cmp expect output.txt + sort <output.txt >output.sorted && + test_cmp expect output.sorted ' test_done From a140a976e99da79828cb1c37da42a7995738d9d1 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Fri, 3 Jun 2016 16:01:35 -0400 Subject: [PATCH 248/406] cache-tree.c: avoid reusing the DEBUG constant In MSVC, the DEBUG constant is set automatically whenever compiling with debug information. This is clearly not what was intended in cache-tree.c, so let's use a less ambiguous constant there. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- cache-tree.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cache-tree.c b/cache-tree.c index 16ea022c46d3b2..3bd3a9f03790fd 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -5,8 +5,8 @@ #include "cache-tree.h" #include "object-store.h" -#ifndef DEBUG -#define DEBUG 0 +#ifndef DEBUG_CACHE_TREE +#define DEBUG_CACHE_TREE 0 #endif struct cache_tree *cache_tree(void) @@ -110,7 +110,7 @@ static int do_invalidate_path(struct cache_tree *it, const char *path) int namelen; struct cache_tree_sub *down; -#if DEBUG +#if DEBUG_CACHE_TREE fprintf(stderr, "cache-tree invalidate <%s>\n", path); #endif @@ -393,7 +393,7 @@ static int update_one(struct cache_tree *it, strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz); -#if DEBUG +#if DEBUG_CACHE_TREE fprintf(stderr, "cache-tree update-one %o %.*s\n", mode, entlen, path + baselen); #endif @@ -416,7 +416,7 @@ static int update_one(struct cache_tree *it, strbuf_release(&buffer); it->entry_count = to_invalidate ? -1 : i - *skip_count; -#if DEBUG +#if DEBUG_CACHE_TREE fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", it->entry_count, it->subtree_nr, oid_to_hex(&it->oid)); @@ -455,7 +455,7 @@ static void write_one(struct strbuf *buffer, struct cache_tree *it, strbuf_add(buffer, path, pathlen); strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr); -#if DEBUG +#if DEBUG_CACHE_TREE if (0 <= it->entry_count) fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n", pathlen, path, it->entry_count, it->subtree_nr, @@ -529,7 +529,7 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p) size -= rawsz; } -#if DEBUG +#if DEBUG_CACHE_TREE if (0 <= it->entry_count) fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n", *buffer, it->entry_count, subtree_nr, From f603eaf41b4670ebee726f47fee6e69708954090 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 27 Oct 2016 06:25:56 -0700 Subject: [PATCH 249/406] obstack: fix compiler warning MS Visual C suggests that the construct condition ? (int) i : (ptrdiff_t) d is incorrect. Let's fix this by casting to ptrdiff_t also for the positive arm of the conditional. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/obstack.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/obstack.h b/compat/obstack.h index 6bc24b76445686..f0807eaa3b899f 100644 --- a/compat/obstack.h +++ b/compat/obstack.h @@ -492,7 +492,7 @@ __extension__ \ ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \ ((((h)->temp.tempint > 0 \ && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \ - ? (int) ((h)->next_free = (h)->object_base \ + ? (ptrdiff_t) ((h)->next_free = (h)->object_base \ = (h)->temp.tempint + (char *) (h)->chunk) \ : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0))) From d97da32e77060af73ad2c2973383177604dcf49d Mon Sep 17 00:00:00 2001 From: Brendan Forster <brendan@github.com> Date: Thu, 18 Feb 2016 21:29:50 +1100 Subject: [PATCH 250/406] Add an issue template With improvements by Clive Chan, Adric Norris, Ben Bodenmiller and Philip Oakley. Helped-by: Clive Chan <cc@clive.io> Helped-by: Adric Norris <landstander668@gmail.com> Helped-by: Ben Bodenmiller <bbodenmiller@hotmail.com> Helped-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Brendan Forster <brendan@github.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .github/ISSUE_TEMPLATE.md | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000000000..75edc4d5b47c29 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,63 @@ + - [ ] I was not able to find an [open](https://github.com/git-for-windows/git/issues?q=is%3Aopen) or [closed](https://github.com/git-for-windows/git/issues?q=is%3Aclosed) issue matching what I'm seeing + +### Setup + + - Which version of Git for Windows are you using? Is it 32-bit or 64-bit? + +``` +$ git --version --build-options + +** insert your machine's response here ** +``` + + - Which version of Windows are you running? Vista, 7, 8, 10? Is it 32-bit or 64-bit? + +``` +$ cmd.exe /c ver + +** insert your machine's response here ** +``` + + - What options did you set as part of the installation? Or did you choose the + defaults? + +``` +# One of the following: +> type "C:\Program Files\Git\etc\install-options.txt" +> type "C:\Program Files (x86)\Git\etc\install-options.txt" +> type "%USERPROFILE%\AppData\Local\Programs\Git\etc\install-options.txt" +$ cat /etc/install-options.txt + +** insert your machine's response here ** +``` + + - Any other interesting things about your environment that might be related + to the issue you're seeing? + +** insert your response here ** + +### Details + + - Which terminal/shell are you running Git from? e.g Bash/CMD/PowerShell/other + +** insert your response here ** + + - What commands did you run to trigger this issue? If you can provide a + [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) + this will help us understand the issue. + +``` +** insert your commands here ** +``` + - What did you expect to occur after running these commands? + +** insert here ** + + - What actually happened instead? + +** insert here ** + + - If the problem was occurring with a specific repository, can you provide the + URL to that repository to help us with testing? + +** insert URL here ** From baf8dcc6717f56b989a7b5827c241f6de15a13bc Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Fri, 22 Dec 2017 17:15:50 +0000 Subject: [PATCH 251/406] Modify the GitHub Pull Request template (to reflect Git for Windows) Git for Windows accepts pull requests; Core Git does not. Therefore we need to adjust the template (because it only matches core Git's project management style, not ours). Also: direct Git for Windows enhancements to their contributions page, space out the text for easy reading, and clarify that the mailing list is plain text, not HTML. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .github/PULL_REQUEST_TEMPLATE.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index adba13e5baf460..07b255f286e889 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,7 +1,18 @@ -Thanks for taking the time to contribute to Git! Please be advised that the -Git community does not use github.com for their contributions. Instead, we use -a mailing list (git@vger.kernel.org) for code submissions, code reviews, and -bug reports. Nevertheless, you can use submitGit to conveniently send your Pull +Thanks for taking the time to contribute to Git! + +Those seeking to contribute to the Git for Windows fork should see +http://gitforwindows.org/#contribute on how to contribute Windows specific enhancements. + +If your contribution is for the core Git functions and documentation +please be aware that the Git community does not use the github.com issues +or pull request mechanism for their contributions. + +Instead, we use the Git mailing list (git@vger.kernel.org) for code and +documenatation submissions, code reviews, and bug reports. The +mailing list is plain text only (anything with HTML is sent directly +to the spam folder). + +Nevertheless, you can use submitGit to conveniently send your Pull Requests commits to our mailing list. Please read the "guidelines for contributing" linked above! From 50011fcafc80e211c6a179131af90b4875d6b60d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 20 Feb 2018 15:44:57 +0100 Subject: [PATCH 252/406] .github: Add configuration for the Sentiment Bot The sentiment bot will help detect when things get too heated. Hopefully. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .github/config.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/config.yml diff --git a/.github/config.yml b/.github/config.yml new file mode 100644 index 00000000000000..45edb7ba37ce02 --- /dev/null +++ b/.github/config.yml @@ -0,0 +1,10 @@ +# Configuration for sentiment-bot - https://github.com/behaviorbot/sentiment-bot + +# *Required* toxicity threshold between 0 and .99 with the higher numbers being +# the most toxic. Anything higher than this threshold will be marked as toxic +# and commented on +sentimentBotToxicityThreshold: .7 + +# *Required* Comment to reply with +sentimentBotReplyComment: > + Please be sure to review the code of conduct and be respectful of other users. cc/ @git-for-windows/trusted-git-for-windows-developers From e70a5667b5093dbb71b2142abf1e44fd8ea79992 Mon Sep 17 00:00:00 2001 From: Alejandro Barreto <alejandro.barreto@ni.com> Date: Fri, 9 Mar 2018 14:17:54 -0600 Subject: [PATCH 253/406] Document how $HOME is set on Windows Git documentation refers to $HOME and $XDG_CONFIG_HOME often, but does not specify how or where these values come from on Windows where neither is set by default. The new documentation reflects the behavior of setup_windows_environment() in compat/mingw.c. Signed-off-by: Alejandro Barreto <alejandro.barreto@ni.com> --- Documentation/git.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Documentation/git.txt b/Documentation/git.txt index dba7f0c18e33e7..f6f347284bdb97 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -372,6 +372,14 @@ Environment Variables --------------------- Various Git commands use the following environment variables: +System +~~~~~~ +`HOME`:: + Specifies the path to the user's home directory. On Windows, if + unset, Git will set a process environment variable equal to: + `$HOMEDRIVE$HOMEPATH` if both `$HOMEDRIVE` and `$HOMEPATH` exist; + otherwise `$USERPROFILE` if `$USERPROFILE` exists. + The Git Repository ~~~~~~~~~~~~~~~~~~ These environment variables apply to 'all' core Git commands. Nb: it From e4437a127dd8884b6a398a2c9c7dae121bc3e7bc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 30 Oct 2018 21:39:05 +0100 Subject: [PATCH 254/406] mingw: replace mingw_startup() hack Git for Windows has special code to retrieve the command-line parameters (and even the environment) in UTF-16 encoding, so that they can be converted to UTF-8. This is necessary because Git for Windows wants to use UTF-8 encoded strings throughout its code, and the main() function does not get the parameters in that encoding. To do that, we used the __wgetmainargs() function, which is not even a Win32 API function, but provided by the MINGW "runtime" instead. Obviously, this method would not work with any other compiler than GCC, and in preparation for compiling with Visual C++, we would like to avoid that. Lucky us, there is a much more elegant way: we simply implement wmain() and link with -municode. The command-line parameters are passed to wmain() encoded in UTF-16, as desired, and this method also works with Visual C++. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 53 +++++++++++++++++++++++++++++++----------------- compat/mingw.h | 22 ++++++++++---------- config.mak.uname | 1 + 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index e81527181485b0..1d9076f481e87c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2462,18 +2462,13 @@ static void setup_windows_environment(void) setenv("TERM", "cygwin", 1); } +#if !defined(_MSC_VER) /* * Disable MSVCRT command line wildcard expansion (__getmainargs called from * mingw startup code, see init.c in mingw runtime). */ int _CRT_glob = 0; - -typedef struct { - int newmode; -} _startupinfo; - -extern int __wgetmainargs(int *argc, wchar_t ***argv, wchar_t ***env, int glob, - _startupinfo *si); +#endif static NORETURN void die_startup(void) { @@ -2551,20 +2546,23 @@ static void maybe_redirect_std_handles(void) GENERIC_WRITE, FILE_FLAG_NO_BUFFERING); } -void mingw_startup(void) +/* + * We implement wmain() and compile with -municode, which would + * normally ignore main(), but we call the latter from the former + * so that we can handle non-ASCII command-line parameters + * appropriately. + * + * To be more compatible with the core git code, we convert + * argv into UTF8 and pass them directly to main(). + */ +int wmain(int argc, const wchar_t **wargv) { - int i, maxlen, argc; - char *buffer; - wchar_t **wenv, **wargv; - _startupinfo si; + int i, maxlen, exit_status; + char *buffer, **save; + const char **argv; maybe_redirect_std_handles(); - /* get wide char arguments and environment */ - si.newmode = 0; - if (__wgetmainargs(&argc, &wargv, &wenv, _CRT_glob, &si) < 0) - die_startup(); - /* determine size of argv and environ conversion buffer */ maxlen = wcslen(wargv[0]); for (i = 1; i < argc; i++) @@ -2574,9 +2572,16 @@ void mingw_startup(void) maxlen = 3 * maxlen + 1; buffer = malloc_startup(maxlen); - /* convert command line arguments and environment to UTF-8 */ + /* + * Create a UTF-8 version of w_argv. Also create a "save" copy + * to remember all the string pointers because parse_options() + * will remove claimed items from the argv that we pass down. + */ + ALLOC_ARRAY(argv, argc + 1); + ALLOC_ARRAY(save, argc + 1); for (i = 0; i < argc; i++) - __argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen); + argv[i] = save[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen); + argv[i] = save[i] = NULL; free(buffer); /* fix Windows specific environment settings */ @@ -2595,6 +2600,16 @@ void mingw_startup(void) /* initialize Unicode console */ winansi_init(); + + /* invoke the real main() using our utf8 version of argv. */ + exit_status = main(argc, argv); + + for (i = 0; i < argc; i++) + free(save[i]); + free(save); + free(argv); + + return exit_status; } int uname(struct utsname *buf) diff --git a/compat/mingw.h b/compat/mingw.h index 8c24ddaa3efc20..c30ae2d651f64e 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -591,18 +591,18 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen); extern CRITICAL_SECTION pinfo_cs; /* - * A replacement of main() that adds win32 specific initialization. + * Git, like most portable C applications, implements a main() function. On + * Windows, this main() function would receive parameters encoded in the + * current locale, but Git for Windows would prefer UTF-8 encoded parameters. + * + * To make that happen, we still declare main() here, and then declare and + * implement wmain() (which is the Unicode variant of main()) and compile with + * -municode. This wmain() function reencodes the parameters from UTF-16 to + * UTF-8 format, sets up a couple of other things as required on Windows, and + * then hands off to the main() function. */ - -void mingw_startup(void); -#define main(c,v) dummy_decl_mingw_main(void); \ -static int mingw_main(c,v); \ -int main(int argc, const char **argv) \ -{ \ - mingw_startup(); \ - return mingw_main(__argc, (void *)__argv); \ -} \ -static int mingw_main(c,v) +int wmain(int argc, const wchar_t **w_argv); +int main(int argc, const char **argv); /* * Used by Pthread API implementation for Windows diff --git a/config.mak.uname b/config.mak.uname index 55832a3606db36..742215802f4bf4 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -527,6 +527,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) ETAGS_TARGET = ETAGS NO_POSIX_GOODIES = UnfortunatelyYes DEFAULT_HELP_FORMAT = html + BASIC_LDFLAGS += -municode -mconsole COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/winansi.o \ From 7e26024bdca90b43d8c70c90c8640ed77b92af12 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 20 Oct 2016 06:31:47 -0700 Subject: [PATCH 255/406] msvc: fix dependencies of compat/msvc.c The file compat/msvc.c includes compat/mingw.c, which means that we have to recompile compat/msvc.o if compat/mingw.c changes. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- config.mak.uname | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.mak.uname b/config.mak.uname index 742215802f4bf4..d1873437af6141 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -405,6 +405,8 @@ else BASIC_CFLAGS += -Zi -MDd endif X = .exe + +compat/msvc.o: compat/msvc.c compat/mingw.c GIT-CFLAGS endif ifeq ($(uname_S),Interix) NO_INITGROUPS = YesPlease From 3376a4aa87b8e14043b387d989fa15d5b7df6398 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Thu, 21 Apr 2016 14:07:20 +0100 Subject: [PATCH 256/406] msvc: include sigset_t definition On MSVC (VS2008) sigset_t is not defined. Signed-off-by: Philip Oakley <philipoakley@iee.org> --- compat/msvc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compat/msvc.h b/compat/msvc.h index 580bb55bf4a71d..ec4518eebfa85d 100644 --- a/compat/msvc.h +++ b/compat/msvc.h @@ -24,6 +24,8 @@ static __inline int strcasecmp (const char *s1, const char *s2) #undef ERROR +typedef int sigset_t; + #include "compat/mingw.h" #endif From 49b058c9cad5a9156b8a228706625877f4a5f45b Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Thu, 21 Apr 2016 14:52:05 +0100 Subject: [PATCH 257/406] msvc: define O_ACCMODE This constant is not defined in MSVC's headers. In UCRT's fcntl.h, _O_RDONLY, _O_WRONLY and _O_RDWR are defined as 0, 1 and 2, respectively. Yes, that means that UCRT breaks with the tradition that O_RDWR == O_RDONLY | O_WRONLY. It is a perfectly legal way to define those constants, though, therefore we need to take care of defining O_ACCMODE accordingly. This is particularly important in order to keep our "open() can set errno to EISDIR" emulation working: it tests that (flags & O_ACCMODE) is not identical to O_RDONLY before going on to test specifically whether the file for which open() reported EACCES is, in fact, a directory. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/msvc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compat/msvc.h b/compat/msvc.h index ec4518eebfa85d..ae1c58ec13ae8e 100644 --- a/compat/msvc.h +++ b/compat/msvc.h @@ -25,6 +25,8 @@ static __inline int strcasecmp (const char *s1, const char *s2) #undef ERROR typedef int sigset_t; +/* open for reading, writing, or both (not in fcntl.h) */ +#define O_ACCMODE (_O_RDONLY | _O_WRONLY | _O_RDWR) #include "compat/mingw.h" From a1c1af34d60088e09d832bf1cdf04b7f7557b1ee Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Fri, 3 Jun 2016 15:38:25 -0400 Subject: [PATCH 258/406] msvc: mark a variable as non-const VS2015 complains when using a const pointer in memcpy()/free(). Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- compat/mingw.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 1d9076f481e87c..3fcc8c76ef77a8 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1523,7 +1523,10 @@ static int try_shell_exec(const char *cmd, char *const *argv) prog = path_lookup(interpr, 1); if (prog) { int argc = 0; - const char **argv2; +#ifndef _MSC_VER + const +#endif + char **argv2; while (argv[argc]) argc++; ALLOC_ARRAY(argv2, argc + 1); argv2[0] = (char *)cmd; /* full path to the script file */ From 9f4a124f9bb76c88ec41fb76b58eb5bd779d43f5 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Fri, 3 Jun 2016 15:38:25 -0400 Subject: [PATCH 259/406] msvc: do not re-declare the timespec struct VS2015's headers already declare that struct. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- compat/mingw.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compat/mingw.h b/compat/mingw.h index c30ae2d651f64e..90a9e84a604161 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -361,11 +361,13 @@ static inline int getrlimit(int resource, struct rlimit *rlp) #ifndef __MINGW64_VERSION_MAJOR #define off_t off64_t #define lseek _lseeki64 +#ifndef _MSC_VER struct timespec { time_t tv_sec; long tv_nsec; }; #endif +#endif struct mingw_stat { _dev_t st_dev; From 84cfa39189b324f99b1a0a6276cc376e7184ab76 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Fri, 3 Jun 2016 15:38:25 -0400 Subject: [PATCH 260/406] msvc: define ftello() It is just called differently in MSVC's headers. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/msvc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compat/msvc.h b/compat/msvc.h index ae1c58ec13ae8e..b3ac4cc3affa47 100644 --- a/compat/msvc.h +++ b/compat/msvc.h @@ -24,6 +24,8 @@ static __inline int strcasecmp (const char *s1, const char *s2) #undef ERROR +#define ftello _ftelli64 + typedef int sigset_t; /* open for reading, writing, or both (not in fcntl.h) */ #define O_ACCMODE (_O_RDONLY | _O_WRONLY | _O_RDWR) From 3e0696639ce980968fd0fb00b7c67e1e1a6748e2 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Tue, 10 Jan 2017 22:53:36 +0100 Subject: [PATCH 261/406] msvc: fix detect_msys_tty() The ntstatus.h header is only available in MINGW. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- compat/winansi.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/compat/winansi.c b/compat/winansi.c index f4f08237f9ed51..11cd9b82cc8909 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -544,7 +544,20 @@ static HANDLE swap_osfhnd(int fd, HANDLE new_handle) #ifdef DETECT_MSYS_TTY #include <winternl.h> + +#if defined(_MSC_VER) + +typedef struct _OBJECT_NAME_INFORMATION +{ + UNICODE_STRING Name; + WCHAR NameBuffer[0]; +} OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION; + +#define ObjectNameInformation 1 + +#else #include <ntstatus.h> +#endif static void detect_msys_tty(int fd) { From 6ab54bca1ff6680ed9f3eee194343f7a3e751675 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Wed, 4 May 2016 16:18:07 +0100 Subject: [PATCH 262/406] msvc: add pragmas for common warnings MSVC can be overzealous about some warnings. Disable them. Signed-off-by: Philip Oakley <philipoakley@iee.org> --- compat/msvc.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compat/msvc.h b/compat/msvc.h index b3ac4cc3affa47..d081043f539d27 100644 --- a/compat/msvc.h +++ b/compat/msvc.h @@ -6,6 +6,10 @@ #include <malloc.h> #include <io.h> +#pragma warning(disable: 4018) /* signed/unsigned comparison */ +#pragma warning(disable: 4244) /* type conversion, possible loss of data */ +#pragma warning(disable: 4090) /* 'function' : different 'const' qualifiers (ALLOC_GROW etc.)*/ + /* porting function */ #define inline __inline #define __inline__ __inline From 4eba2aee683b22a2085c6e3875d1fefcd540237d Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Fri, 3 Jun 2016 15:38:25 -0400 Subject: [PATCH 263/406] msvc: do not pretend to support all signals This special-cases various signals that are not supported on Windows, such as SIGPIPE. These cause the UCRT to throw asserts (at least in debug mode). Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- compat/mingw.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 3fcc8c76ef77a8..91fd2d0a2af940 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2253,8 +2253,34 @@ int mingw_raise(int sig) sigint_fn(SIGINT); return 0; +#if defined(_MSC_VER) + /* + * <signal.h> in the CRT defines 8 signals as being + * supported on the platform. Anything else causes + * an "Invalid signal or error" (which in DEBUG builds + * causes the Abort/Retry/Ignore dialog). We by-pass + * the CRT for things we already know will fail. + */ + /*case SIGINT:*/ + case SIGILL: + case SIGFPE: + case SIGSEGV: + case SIGTERM: + case SIGBREAK: + case SIGABRT: + case SIGABRT_COMPAT: + return raise(sig); + default: + errno = EINVAL; + return -1; + +#else + default: return raise(sig); + +#endif + } } From 8f5575dbaccf9630700560d6465e3764c51f9f21 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Fri, 3 Jun 2016 11:32:01 -0400 Subject: [PATCH 264/406] msvc: support building Git using MS Visual C++ With this patch, Git can be built using the Microsoft toolchain, via: make MSVC=1 [DEBUG=1] Third party libraries are built from source using the open source "vcpkg" tool set. See https://github.com/Microsoft/vcpkg On a first build, the vcpkg tools and the third party libraries are automatically downloaded and built. DLLs for the third party libraries are copied to the top-level (and t/helper) directory to facilitate debugging. See compat/vcbuild/README. A series of .bat files are invoked by the Makefile to find the location of the installed version of Visual Studio and the associated compiler tools (essentially replicating the environment setup performed by a "Developer Command Prompt"). This should find the most recent VS2015 or VS2017 installation. Output from these scripts are used by the Makefile to define compiler and linker pathnames and -I and -L arguments. The build produces .pdb files for both debug and release builds. Note: This commit was squashed from an organic series of commits developed between 2016 and 2018 in Git for Windows' `master` branch. This combined commit eliminates the obsolete commits related to fetching NuGet packages for third party libraries. It is difficult to use NuGet packages for C/C++ sources because they may be built by earlier versions of the MSVC compiler and have CRT version and linking issues. Additionally, the C/C++ NuGet packages that were using tended to not be updated concurrently with the sources. And in the case of cURL and OpenSSL, this could expose us to security issues. Helped-by: Yue Lin Ho <b8732003@student.nsysu.edu.tw> Helped-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Makefile | 42 ++++++- compat/mingw.c | 12 ++ compat/vcbuild/.gitignore | 3 + compat/vcbuild/README | 51 +++++++++ compat/vcbuild/find_vs_env.bat | 169 +++++++++++++++++++++++++++++ compat/vcbuild/scripts/clink.pl | 26 ++++- compat/vcbuild/vcpkg_copy_dlls.bat | 39 +++++++ compat/vcbuild/vcpkg_install.bat | 81 ++++++++++++++ config.mak.uname | 74 +++++++++++-- git-compat-util.h | 9 ++ 10 files changed, 491 insertions(+), 15 deletions(-) create mode 100644 compat/vcbuild/.gitignore create mode 100644 compat/vcbuild/find_vs_env.bat create mode 100644 compat/vcbuild/vcpkg_copy_dlls.bat create mode 100644 compat/vcbuild/vcpkg_install.bat diff --git a/Makefile b/Makefile index 59735fc8f8e8a6..bf37ae70a81c39 100644 --- a/Makefile +++ b/Makefile @@ -1179,7 +1179,7 @@ endif ifdef SANE_TOOL_PATH SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH)) -BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix $(SANE_TOOL_PATH_SQ)|' +BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|' PATH := $(SANE_TOOL_PATH):${PATH} else BROKEN_PATH_FIX = '/^\# @@BROKEN_PATH_FIX@@$$/d' @@ -2750,6 +2750,33 @@ install: all $(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' $(INSTALL) -m 644 $(SCRIPT_LIB) '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' $(INSTALL) $(install_bindir_programs) '$(DESTDIR_SQ)$(bindir_SQ)' +ifdef MSVC + # We DO NOT install the individual foo.o.pdb files because they + # have already been rolled up into the exe's pdb file. + # We DO NOT have pdb files for the builtin commands (like git-status.exe) + # because it is just a copy/hardlink of git.exe, rather than a unique binary. + $(INSTALL) git.pdb '$(DESTDIR_SQ)$(bindir_SQ)' + $(INSTALL) git-shell.pdb '$(DESTDIR_SQ)$(bindir_SQ)' + $(INSTALL) git-upload-pack.pdb '$(DESTDIR_SQ)$(bindir_SQ)' + $(INSTALL) git-credential-store.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-daemon.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-fast-import.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-http-backend.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-http-fetch.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-http-push.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-imap-send.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-remote-http.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-remote-testsvn.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-sh-i18n--envsubst.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' + $(INSTALL) git-show-index.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' +ifndef DEBUG + $(INSTALL) $(vcpkg_rel_bin)/*.dll '$(DESTDIR_SQ)$(bindir_SQ)' + $(INSTALL) $(vcpkg_rel_bin)/*.pdb '$(DESTDIR_SQ)$(bindir_SQ)' +else + $(INSTALL) $(vcpkg_dbg_bin)/*.dll '$(DESTDIR_SQ)$(bindir_SQ)' + $(INSTALL) $(vcpkg_dbg_bin)/*.pdb '$(DESTDIR_SQ)$(bindir_SQ)' +endif +endif $(MAKE) -C templates DESTDIR='$(DESTDIR_SQ)' install $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(mergetools_instdir_SQ)' $(INSTALL) -m 644 mergetools/* '$(DESTDIR_SQ)$(mergetools_instdir_SQ)' @@ -2950,6 +2977,19 @@ endif $(RM) GIT-VERSION-FILE GIT-CFLAGS GIT-LDFLAGS GIT-BUILD-OPTIONS $(RM) GIT-USER-AGENT GIT-PREFIX $(RM) GIT-SCRIPT-DEFINES GIT-PERL-DEFINES GIT-PERL-HEADER GIT-PYTHON-VARS +ifdef MSVC + $(RM) $(patsubst %.o,%.o.pdb,$(OBJECTS)) + $(RM) $(patsubst %.exe,%.pdb,$(OTHER_PROGRAMS)) + $(RM) $(patsubst %.exe,%.iobj,$(OTHER_PROGRAMS)) + $(RM) $(patsubst %.exe,%.ipdb,$(OTHER_PROGRAMS)) + $(RM) $(patsubst %.exe,%.pdb,$(PROGRAMS)) + $(RM) $(patsubst %.exe,%.iobj,$(PROGRAMS)) + $(RM) $(patsubst %.exe,%.ipdb,$(PROGRAMS)) + $(RM) $(patsubst %.exe,%.pdb,$(TEST_PROGRAMS)) + $(RM) $(patsubst %.exe,%.iobj,$(TEST_PROGRAMS)) + $(RM) $(patsubst %.exe,%.ipdb,$(TEST_PROGRAMS)) + $(RM) compat/vcbuild/MSVC-DEFS-GEN +endif .PHONY: all install profile-clean cocciclean clean strip .PHONY: shell_compatibility_test please_set_SHELL_PATH_to_a_more_modern_shell diff --git a/compat/mingw.c b/compat/mingw.c index 91fd2d0a2af940..7b9b488b348b34 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2575,6 +2575,12 @@ static void maybe_redirect_std_handles(void) GENERIC_WRITE, FILE_FLAG_NO_BUFFERING); } +#ifdef _MSC_VER +#ifdef _DEBUG +#include <crtdbg.h> +#endif +#endif + /* * We implement wmain() and compile with -municode, which would * normally ignore main(), but we call the latter from the former @@ -2590,6 +2596,12 @@ int wmain(int argc, const wchar_t **wargv) char *buffer, **save; const char **argv; +#ifdef _MSC_VER +#ifdef USE_MSVC_CRTDBG + _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); +#endif +#endif + maybe_redirect_std_handles(); /* determine size of argv and environ conversion buffer */ diff --git a/compat/vcbuild/.gitignore b/compat/vcbuild/.gitignore new file mode 100644 index 00000000000000..8f8b794ef39f75 --- /dev/null +++ b/compat/vcbuild/.gitignore @@ -0,0 +1,3 @@ +/vcpkg/ +/MSVC-DEFS-GEN +/VCPKG-DEFS diff --git a/compat/vcbuild/README b/compat/vcbuild/README index 60fd873fe801da..81da36a93bae44 100644 --- a/compat/vcbuild/README +++ b/compat/vcbuild/README @@ -1,3 +1,54 @@ +The Steps to Build Git with VS2015 or VS2017 from the command line. + +1. Install the "vcpkg" open source package manager and build essential + third-party libraries. The steps for this have been captured in a + set of convenience scripts. These can be run from a stock Command + Prompt or from an SDK bash window: + + $ cd <repo_root> + $ ./compat/vcbuild/vcpkg_install.bat + + The vcpkg tools and all of the third-party sources will be installed + in this folder: + <repo_root>/compat/vcbuild/vcpkg/ + + A file will be created with a set of Makefile macros pointing to a + unified "include", "lib", and "bin" directory (release and debug) for + all of the required packages. This file will be included by the main + Makefile: + <repo_root>/compat/vcbuild/MSVC-DEFS-GEN + +2. OPTIONALLY copy the third-party *.dll and *.pdb files into the repo + root to make it easier to run and debug git.exe without having to + manipulate your PATH. This is especially true for debug sessions in + Visual Studio. + + Use ONE of the following forms which should match how you want to + compile git.exe. + + $ ./compat/vcbuild/vcpkg_copy_packages.bat debug + $ ./compat/vcbuild/vcpkg_copy_packages.bat release + +3. Build git using MSVC from an SDK bash window using one of the + following commands: + + $ make MSVC=1 + $ make MSVC=1 DEBUG=1 + +================================================================ + +Alternatively, run `make MSVC=1 vcxproj` and then load the generated +git.sln in Visual Studio. The initial build will install the vcpkg +system and build the dependencies automatically. This will take a while. + +Note that this will automatically add and commit the generated +.sln and .vcxproj files to the repo. You may want to drop this +commit before submitting a Pull Request.... + +Or maybe we should put the .sln/.vcxproj files in the .gitignore file +and not do this. I'm not sure. + +================================================================ The Steps of Build Git with VS2008 1. You need the build environment, which contains the Git dependencies diff --git a/compat/vcbuild/find_vs_env.bat b/compat/vcbuild/find_vs_env.bat new file mode 100644 index 00000000000000..1232f200f7dcd7 --- /dev/null +++ b/compat/vcbuild/find_vs_env.bat @@ -0,0 +1,169 @@ +@ECHO OFF +REM ================================================================ +REM You can use either GCC (the default) or MSVC to build git +REM using the GIT-SDK command line tools. +REM $ make +REM $ make MSVC=1 +REM +REM GIT-SDK BASH windows inherit environment variables with all of +REM the bin/lib/include paths for GCC. It DOES NOT inherit values +REM for the corresponding MSVC tools. +REM +REM During normal (non-git) Windows development, you launch one +REM of the provided "developer command prompts" to set environment +REM variables for the MSVC tools. +REM +REM Therefore, to allow MSVC command line builds of git from BASH +REM and MAKE, we must blend these two different worlds. This script +REM attempts to do that. +REM ================================================================ +REM This BAT file starts in a plain (non-developer) command prompt, +REM searches for the "best" commmand prompt setup script, installs +REM it into the current CMD process, and exports the various MSVC +REM environment variables for use by MAKE. +REM +REM The output of this script should be written to a make "include +REM file" and referenced by the top-level Makefile. +REM +REM See "config.mak.uname" (look for compat/vcbuild/MSVC-DEFS-GEN). +REM ================================================================ +REM The provided command prompts are custom to each VS release and +REM filled with lots of internal knowledge (such as Registry settings); +REM even their names vary by release, so it is not appropriate for us +REM to look inside them. Rather, just run them in a subordinate +REM process and extract the settings we need. +REM ================================================================ +REM +REM Current (VS2017 and beyond) +REM ------------------- +REM Visual Studio 2017 introduced a new installation layout and +REM support for side-by-side installation of multiple versions of +REM VS2017. Furthermore, these can all coexist with installations +REM of previous versions of VS (which have a completely different +REM layout on disk). +REM +REM VS2017 Update 2 introduced a "vswhere.exe" command: +REM https://github.com/Microsoft/vswhere +REM https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/ +REM https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/ +REM +REM VS2015 +REM ------ +REM Visual Studio 2015 uses the traditional VcVarsAll. +REM +REM Earlier Versions +REM ---------------- +REM TODO +REM +REM ================================================================ +REM Note: Throughout this script we use "dir <path> && <cmd>" rather +REM than "if exist <path>" because of script problems with pathnames +REM containing spaces. +REM ================================================================ + +REM Sanitize PATH to prevent git-sdk paths from confusing "wmic.exe" +REM (called internally in some of the system BAT files). +SET PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem; + +REM ================================================================ + +:current + SET vs_where=C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe + dir "%vs_where%" >nul 2>nul && GOTO have_vs_where + GOTO not_2017 + +:have_vs_where + REM Try to use VsWhere to get the location of VsDevCmd. + + REM Keep VsDevCmd from cd'ing away. + SET VSCMD_START_DIR=. + + REM Get the root of the VS product installation. + FOR /F "usebackq tokens=*" %%i IN (`"%vs_where%" -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop -property installationPath`) DO @SET vs_ip=%%i + + SET vs_devcmd=%vs_ip%\Common7\Tools\VsDevCmd.bat + dir "%vs_devcmd%" >nul 2>nul && GOTO have_vs_devcmd + GOTO not_2017 + +:have_vs_devcmd + REM Use VsDevCmd to setup the environment of this process. + REM Setup CL for building 64-bit apps using 64-bit tools. + @call "%vs_devcmd%" -no_logo -arch=x64 -host_arch=x64 + + SET tgt=%VSCMD_ARG_TGT_ARCH% + + SET mn=%VCToolsInstallDir% + SET msvc_includes=-I"%mn%INCLUDE" + SET msvc_libs=-L"%mn%lib\%tgt%" + SET msvc_bin_dir=%mn%bin\Host%VSCMD_ARG_HOST_ARCH%\%tgt% + + SET sdk_dir=%WindowsSdkDir% + SET sdk_ver=%WindowsSDKVersion% + SET si=%sdk_dir%Include\%sdk_ver% + SET sdk_includes=-I"%si%ucrt" -I"%si%um" -I"%si%shared" + SET sl=%sdk_dir%lib\%sdk_ver% + SET sdk_libs=-L"%sl%ucrt\%tgt%" -L"%sl%um\%tgt%" + + SET vs_ver=%VisualStudioVersion% + + GOTO print_vars + +REM ================================================================ + +:not_2017 + REM See if VS2015 is installed. + + SET vs_2015_bat=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat + dir "%vs_2015_bat%" >nul 2>nul && GOTO have_vs_2015 + GOTO not_2015 + +:have_vs_2015 + REM Use VcVarsAll like the "x64 Native" command prompt. + REM Setup CL for building 64-bit apps using 64-bit tools. + @call "%vs_2015_bat%" amd64 + + REM Note that in VS2015 they use "x64" in some contexts and "amd64" in others. + SET mn=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ + SET msvc_includes=-I"%mn%INCLUDE" + SET msvc_libs=-L"%mn%lib\amd64" + SET msvc_bin_dir=%mn%bin\amd64 + + SET sdk_dir=%WindowsSdkDir% + SET sdk_ver=%WindowsSDKVersion% + SET si=%sdk_dir%Include\%sdk_ver% + SET sdk_includes=-I"%si%ucrt" -I"%si%um" -I"%si%shared" -I"%si%winrt" + SET sl=%sdk_dir%lib\%sdk_ver% + SET sdk_libs=-L"%sl%ucrt\x64" -L"%sl%um\x64" + + SET vs_ver=%VisualStudioVersion% + + GOTO print_vars + +REM ================================================================ + +:not_2015 + REM TODO.... + echo TODO support older versions of VS. >&2 + EXIT /B 1 + +REM ================================================================ + +:print_vars + REM Dump the essential vars to stdout to allow the main + REM Makefile to include it. See config.mak.uname. + REM Include DOS-style and BASH-style path for bin dir. + + echo msvc_bin_dir=%msvc_bin_dir% + SET X1=%msvc_bin_dir:C:=/C% + SET X2=%X1:\=/% + echo msvc_bin_dir_msys=%X2% + + echo msvc_includes=%msvc_includes% + echo msvc_libs=%msvc_libs% + + echo sdk_includes=%sdk_includes% + echo sdk_libs=%sdk_libs% + + echo vs_ver=%vs_ver% + + EXIT /B 0 diff --git a/compat/vcbuild/scripts/clink.pl b/compat/vcbuild/scripts/clink.pl index a87d0da512e095..3d6fa21c1e5690 100755 --- a/compat/vcbuild/scripts/clink.pl +++ b/compat/vcbuild/scripts/clink.pl @@ -12,32 +12,49 @@ use strict; my @args = (); my @cflags = (); +my @lflags = (); my $is_linking = 0; +my $is_debug = 0; while (@ARGV) { my $arg = shift @ARGV; - if ("$arg" =~ /^-[DIMGO]/) { + if ("$arg" eq "-DDEBUG") { + # Some vcpkg-based libraries have different names for release + # and debug versions. This hack assumes that -DDEBUG comes + # before any "-l*" flags. + $is_debug = 1; + } + if ("$arg" =~ /^-[DIMGOZ]/) { push(@cflags, $arg); } elsif ("$arg" eq "-o") { my $file_out = shift @ARGV; if ("$file_out" =~ /exe$/) { $is_linking = 1; + # Create foo.exe and foo.pdb push(@args, "-OUT:$file_out"); } else { + # Create foo.o and foo.o.pdb push(@args, "-Fo$file_out"); + push(@args, "-Fd$file_out.pdb"); } } elsif ("$arg" eq "-lz") { + if ($is_debug) { + push(@args, "zlibd.lib"); + } else{ push(@args, "zlib.lib"); + } } elsif ("$arg" eq "-liconv") { - push(@args, "iconv.lib"); + push(@args, "libiconv.lib"); } elsif ("$arg" eq "-lcrypto") { push(@args, "libeay32.lib"); } elsif ("$arg" eq "-lssl") { push(@args, "ssleay32.lib"); } elsif ("$arg" eq "-lcurl") { push(@args, "libcurl.lib"); + } elsif ("$arg" eq "-lexpat") { + push(@args, "expat.lib"); } elsif ("$arg" =~ /^-L/ && "$arg" ne "-LTCG") { $arg =~ s/^-L/-LIBPATH:/; - push(@args, $arg); + push(@lflags, $arg); } elsif ("$arg" =~ /^-R/) { # eat } else { @@ -45,10 +62,11 @@ } } if ($is_linking) { + push(@args, @lflags); unshift(@args, "link.exe"); } else { unshift(@args, "cl.exe"); push(@args, @cflags); } -#printf("**** @args\n"); +printf(STDERR "**** @args\n\n\n") if (!defined($ENV{'QUIET_GEN'})); exit (system(@args) != 0); diff --git a/compat/vcbuild/vcpkg_copy_dlls.bat b/compat/vcbuild/vcpkg_copy_dlls.bat new file mode 100644 index 00000000000000..13661c14f8705c --- /dev/null +++ b/compat/vcbuild/vcpkg_copy_dlls.bat @@ -0,0 +1,39 @@ +@ECHO OFF +REM ================================================================ +REM This script is an optional step. It copies the *.dll and *.pdb +REM files (created by vcpkg_install.bat) into the top-level directory +REM of the repo so that you can type "./git.exe" and find them without +REM having to fixup your PATH. +REM +REM NOTE: Because the names of some DLL files change between DEBUG and +REM NOTE: RELEASE builds when built using "vcpkg.exe", you will need +REM NOTE: to copy up the corresponding version. +REM ================================================================ + + SETLOCAL EnableDelayedExpansion + + @FOR /F "delims=" %%D IN ("%~dp0") DO @SET cwd=%%~fD + cd %cwd% + + SET arch=x64-windows + SET inst=%cwd%vcpkg\installed\%arch% + + IF [%1]==[release] ( + echo Copying RELEASE mode DLLs to repo root... + ) ELSE IF [%1]==[debug] ( + SET inst=%inst%\debug + echo Copying DEBUG mode DLLs to repo root... + ) ELSE ( + echo ERROR: Invalid argument. + echo Usage: %~0 release + echo Usage: %~0 debug + EXIT /B 1 + ) + + xcopy /e/s/v/y %inst%\bin\*.dll ..\..\ + xcopy /e/s/v/y %inst%\bin\*.pdb ..\..\ + + xcopy /e/s/v/y %inst%\bin\*.dll ..\..\t\helper\ + xcopy /e/s/v/y %inst%\bin\*.pdb ..\..\t\helper\ + + EXIT /B 0 diff --git a/compat/vcbuild/vcpkg_install.bat b/compat/vcbuild/vcpkg_install.bat new file mode 100644 index 00000000000000..3d086c39c31a53 --- /dev/null +++ b/compat/vcbuild/vcpkg_install.bat @@ -0,0 +1,81 @@ +@ECHO OFF +REM ================================================================ +REM This script installs the "vcpkg" source package manager and uses +REM it to build the third-party libraries that git requires when it +REM is built using MSVC. +REM +REM [1] Install VCPKG. +REM [a] Create <root>/compat/vcbuild/vcpkg/ +REM [b] Download "vcpkg". +REM [c] Compile using the currently installed version of VS. +REM [d] Create <root>/compat/vcbuild/vcpkg/vcpkg.exe +REM +REM [2] Install third-party libraries. +REM [a] Download each (which may also install CMAKE). +REM [b] Compile in RELEASE mode and install in: +REM vcpkg/installed/<arch>/{bin,lib} +REM [c] Compile in DEBUG mode and install in: +REM vcpkg/installed/<arch>/debug/{bin,lib} +REM [d] Install headers in: +REM vcpkg/installed/<arch>/include +REM +REM [3] Create a set of MAKE definitions for the top-level +REM Makefile to allow "make MSVC=1" to find the above +REM third-party libraries. +REM [a] Write vcpkg/VCPGK-DEFS +REM +REM https://blogs.msdn.microsoft.com/vcblog/2016/09/19/vcpkg-a-tool-to-acquire-and-build-c-open-source-libraries-on-windows/ +REM https://github.com/Microsoft/vcpkg +REM https://vcpkg.readthedocs.io/en/latest/ +REM ================================================================ + + SETLOCAL EnableDelayedExpansion + + @FOR /F "delims=" %%D IN ("%~dp0") DO @SET cwd=%%~fD + cd %cwd% + + dir vcpkg\vcpkg.exe >nul 2>nul && GOTO :install_libraries + + echo Fetching vcpkg in %cwd%vcpkg + git.exe clone https://github.com/Microsoft/vcpkg vcpkg + IF ERRORLEVEL 1 ( EXIT /B 1 ) + + cd vcpkg + echo Building vcpkg + powershell -exec bypass scripts\bootstrap.ps1 + IF ERRORLEVEL 1 ( EXIT /B 1 ) + + echo Successfully installed %cwd%vcpkg\vcpkg.exe + +:install_libraries + SET arch=x64-windows + + echo Installing third-party libraries... + FOR %%i IN (zlib expat libiconv openssl libssh2 curl) DO ( + cd %cwd%vcpkg + SET p="packages\%%i_%arch%" + IF NOT EXIST "%p%" CALL :sub__install_one %%i + IF ERRORLEVEL 1 ( EXIT /B 1 ) + ) + +:install_defines + cd %cwd% + SET inst=%cwd%vcpkg\installed\%arch% + + echo vcpkg_inc=-I"%inst%\include">VCPKG-DEFS + echo vcpkg_rel_lib=-L"%inst%\lib">>VCPKG-DEFS + echo vcpkg_rel_bin="%inst%\bin">>VCPKG-DEFS + echo vcpkg_dbg_lib=-L"%inst%\debug\lib">>VCPKG-DEFS + echo vcpkg_dbg_bin="%inst%\debug\bin">>VCPKG-DEFS + + EXIT /B 0 + + +:sub__install_one + echo Installing package %1... + + .\vcpkg.exe install %1:%arch% + IF ERRORLEVEL 1 ( EXIT /B 1 ) + + echo Finished %1 + goto :EOF diff --git a/config.mak.uname b/config.mak.uname index d1873437af6141..320b3ec77ec1e6 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -1,5 +1,9 @@ # Platform specific Makefile tweaks based on uname detection +# Define NO_SAFESEH if you need MSVC/Visual Studio to ignore the lack of +# Microsoft's Safe Exception Handling in libraries (such as zlib). +# Typically required for VS2013+/32-bit compilation on Vista+ versions. + uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') uname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not') uname_O := $(shell sh -c 'uname -o 2>/dev/null || echo not') @@ -11,6 +15,19 @@ ifdef MSVC # avoid the MingW and Cygwin configuration sections uname_S := Windows uname_O := Windows + + # Generate and include makefile variables that point to the + # currently installed set of MSVC command line tools. +compat/vcbuild/MSVC-DEFS-GEN: compat/vcbuild/find_vs_env.bat + @"$<" | tr '\\' / >"$@" +include compat/vcbuild/MSVC-DEFS-GEN + + # See if vcpkg and the vcpkg-build versions of the third-party + # libraries that we use are installed. We include the result + # to get $(vcpkg_*) variables defined for the Makefile. +compat/vcbuild/VCPKG-DEFS: compat/vcbuild/vcpkg_install.bat + @"$<" +include compat/vcbuild/VCPKG-DEFS endif # We choose to avoid "if .. else if .. else .. endif endif" @@ -349,6 +366,19 @@ endif ifeq ($(uname_S),Windows) GIT_VERSION := $(GIT_VERSION).MSVC pathsep = ; + # Assume that this is built in Git for Windows' SDK + ifeq (MINGW32,$(MSYSTEM)) + prefix = /mingw32 + else + prefix = /mingw64 + endif + # Prepend MSVC 64-bit tool-chain to PATH. + # + # A regular Git Bash *does not* have cl.exe in its $PATH. As there is a + # link.exe next to, and required by, cl.exe, we have to prepend this + # onto the existing $PATH. + # + SANE_TOOL_PATH ?= $(msvc_bin_dir_msys) HAVE_ALLOCA_H = YesPlease NO_PREAD = YesPlease NEEDS_CRYPTO_WITH_SSL = YesPlease @@ -361,11 +391,14 @@ ifeq ($(uname_S),Windows) NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease NO_MEMMEM = YesPlease - # NEEDS_LIBICONV = YesPlease - NO_ICONV = YesPlease + NEEDS_LIBICONV = YesPlease NO_STRTOUMAX = YesPlease NO_MKDTEMP = YesPlease - SNPRINTF_RETURNS_BOGUS = YesPlease + NO_INTTYPES_H = YesPlease + # VS2015 with UCRT claims that snprintf and friends are C99 compliant, + # so we don't need this: + # + # SNPRINTF_RETURNS_BOGUS = YesPlease NO_SVN_TESTS = YesPlease RUNTIME_PREFIX = YesPlease HAVE_WPGMPTR = YesWeDo @@ -378,7 +411,6 @@ ifeq ($(uname_S),Windows) NO_REGEX = YesPlease NO_GETTEXT = YesPlease NO_PYTHON = YesPlease - BLK_SHA1 = YesPlease ETAGS_TARGET = ETAGS NO_POSIX_GOODIES = UnfortunatelyYes NATIVE_CRLF = YesPlease @@ -387,22 +419,44 @@ ifeq ($(uname_S),Windows) CC = compat/vcbuild/scripts/clink.pl AR = compat/vcbuild/scripts/lib.pl CFLAGS = - BASIC_CFLAGS = -nologo -I. -I../zlib -Icompat/vcbuild -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE + BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE COMPAT_OBJS = compat/msvc.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ compat/win32/dirent.o - COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\" + COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\" BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE - EXTLIBS = user32.lib advapi32.lib shell32.lib wininet.lib ws2_32.lib invalidcontinue.obj + # invalidcontinue.obj allows Git's source code to close the same file + # handle twice, or to access the osfhandle of an already-closed stdout + # See https://msdn.microsoft.com/en-us/library/ms235330.aspx + EXTLIBS = user32.lib advapi32.lib shell32.lib wininet.lib ws2_32.lib invalidcontinue.obj kernel32.lib ntdll.lib PTHREAD_LIBS = lib = + BASIC_CFLAGS += $(vcpkg_inc) $(sdk_includes) $(msvc_includes) +ifndef DEBUG + BASIC_CFLAGS += $(vcpkg_rel_lib) +else + BASIC_CFLAGS += $(vcpkg_dbg_lib) +endif + BASIC_CFLAGS += $(sdk_libs) $(msvc_libs) + + # Optionally enable memory leak reporting. + # BASIC_CLFAGS += -DUSE_MSVC_CRTDBG BASIC_CFLAGS += -DPROTECT_NTFS_DEFAULT=1 + # Always give "-Zi" to the compiler and "-debug" to linker (even in + # release mode) to force a PDB to be generated (like RelWithDebInfo). + BASIC_CFLAGS += -Zi + BASIC_LDFLAGS += -debug + +ifdef NO_SAFESEH + LDFLAGS += -SAFESEH:NO +endif + ifndef DEBUG - BASIC_CFLAGS += -GL -Os -MD - BASIC_LDFLAGS += -LTCG + BASIC_CFLAGS += -GL -Gy -O2 -Oy- -MD -DNDEBUG + BASIC_LDFLAGS += -release -LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:CV,FIXUP AR += -LTCG else - BASIC_CFLAGS += -Zi -MDd + BASIC_CFLAGS += -MDd -DDEBUG -D_DEBUG endif X = .exe diff --git a/git-compat-util.h b/git-compat-util.h index 2a47fc3b3079dc..a96d7b8e9f9aa6 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1,6 +1,15 @@ #ifndef GIT_COMPAT_UTIL_H #define GIT_COMPAT_UTIL_H +#ifdef USE_MSVC_CRTDBG +/* + * For these to work they must appear very early in each + * file -- before most of the standard header files. + */ +#include <stdlib.h> +#include <crtdbg.h> +#endif + #define _FILE_OFFSET_BITS 64 From 6008737a6a05c75e5551e047b3ff4f82c267a6a2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 30 Nov 2016 22:26:20 +0100 Subject: [PATCH 265/406] msvc: avoid debug assertion windows in Debug Mode For regular debugging, it is pretty helpful when a debug assertion in a running application triggers a window that offers to start the debugger. However, when running the test suite, it is not so helpful, in particular when the debug assertions are then suppressed anyway because we disable the invalid parameter checking (via invalidcontinue.obj, see the comment in config.mak.uname about that object for more information). So let's simply disable that window in Debug Mode (it is already disabled in Release Mode). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 7b9b488b348b34..52b1a5d9ceaa09 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2597,6 +2597,10 @@ int wmain(int argc, const wchar_t **wargv) const char **argv; #ifdef _MSC_VER +#ifdef _DEBUG + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); +#endif + #ifdef USE_MSVC_CRTDBG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif From 8d708ee5741cb23b9ff31ca3f589afc569a209e6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 16 Feb 2018 23:50:03 +0100 Subject: [PATCH 266/406] Vcproj.pm: auto-generate GUIDs We ran out GUIDs. Again. But there is no need to: we can generate them semi-randomly from the target file name of the project. Note: the Vcproj generator is probably only interesting for historical reasons; nevertheless, the upcoming Vcxproj generator (to support modern Visual Studio versions) is based on the Vcproj generator and it is better to fix this here first. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/Generators/Vcproj.pm | 66 ++++------------------- 1 file changed, 9 insertions(+), 57 deletions(-) diff --git a/contrib/buildsystems/Generators/Vcproj.pm b/contrib/buildsystems/Generators/Vcproj.pm index cfa74adcc23881..c79b706bc8de4f 100644 --- a/contrib/buildsystems/Generators/Vcproj.pm +++ b/contrib/buildsystems/Generators/Vcproj.pm @@ -3,6 +3,7 @@ require Exporter; use strict; use vars qw($VERSION); +use Digest::SHA qw(sha256_hex); our $VERSION = '1.00'; our(@ISA, @EXPORT, @EXPORT_OK, @AVAILABLE); @@ -12,59 +13,12 @@ BEGIN { push @EXPORT_OK, qw(generate); } -my $guid_index = 0; -my @GUIDS = ( - "{E07B9989-2BF7-4F21-8918-BE22BA467AC3}", - "{278FFB51-0296-4A44-A81A-22B87B7C3592}", - "{7346A2C4-F0FD-444F-9EBE-1AF23B2B5650}", - "{67F421AC-EB34-4D49-820B-3196807B423F}", - "{385DCFE1-CC8C-4211-A451-80FCFC31CA51}", - "{97CC46C5-D2CC-4D26-B634-E75792B79916}", - "{C7CE21FE-6EF8-4012-A5C7-A22BCEDFBA11}", - "{51575134-3FDF-42D1-BABD-3FB12669C6C9}", - "{0AE195E4-9823-4B87-8E6F-20C5614AF2FF}", - "{4B918255-67CA-43BB-A46C-26704B666E6B}", - "{18CCFEEF-C8EE-4CC1-A265-26F95C9F4649}", - "{5D5D90FA-01B7-4973-AFE5-CA88C53AC197}", - "{1F054320-036D-49E1-B384-FB5DF0BC8AC0}", - "{7CED65EE-F2D9-4171-825B-C7D561FE5786}", - "{8D341679-0F07-4664-9A56-3BA0DE88B9BC}", - "{C189FEDC-2957-4BD7-9FA4-7622241EA145}", - "{66844203-1B9F-4C53-9274-164FFF95B847}", - "{E4FEA145-DECC-440D-AEEA-598CF381FD43}", - "{73300A8E-C8AC-41B0-B555-4F596B681BA7}", - "{873FDEB1-D01D-40BF-A1BF-8BBC58EC0F51}", - "{7922C8BE-76C5-4AC6-8BF7-885C0F93B782}", - "{E245D370-308B-4A49-BFC1-1E527827975F}", - "{F6FA957B-66FC-4ED7-B260-E59BBE4FE813}", - "{E6055070-0198-431A-BC49-8DB6CEE770AE}", - "{54159234-C3EB-43DA-906B-CE5DA5C74654}", - "{594CFC35-0B60-46F6-B8EF-9983ACC1187D}", - "{D93FCAB7-1F01-48D2-B832-F761B83231A5}", - "{DBA5E6AC-E7BE-42D3-8703-4E787141526E}", - "{6171953F-DD26-44C7-A3BE-CC45F86FC11F}", - "{9E19DDBE-F5E4-4A26-A2FE-0616E04879B8}", - "{AE81A615-99E3-4885-9CE0-D9CAA193E867}", - "{FBF4067E-1855-4F6C-8BCD-4D62E801A04D}", - "{17007948-6593-4AEB-8106-F7884B4F2C19}", - "{199D4C8D-8639-4DA6-82EF-08668C35DEE0}", - "{E085E50E-C140-4CF3-BE4B-094B14F0DDD6}", - "{00785268-A9CC-4E40-AC29-BAC0019159CE}", - "{4C06F56A-DCDB-46A6-B67C-02339935CF12}", - "{3A62D3FD-519E-4EC9-8171-D2C1BFEA022F}", - "{3A62D3FD-519E-4EC9-8171-D2C1BFEA022F}", - "{9392EB58-D7BA-410B-B1F0-B2FAA6BC89A7}", - "{2ACAB2D5-E0CE-4027-BCA0-D78B2D7A6C66}", - "{86E216C3-43CE-481A-BCB2-BE5E62850635}", - "{FB631291-7923-4B91-9A57-7B18FDBB7A42}", - "{0A176EC9-E934-45B8-B87F-16C7F4C80039}", - "{DF55CA80-46E8-4C53-B65B-4990A23DD444}", - "{3A0F9895-55D2-4710-BE5E-AD7498B5BF44}", - "{294BDC5A-F448-48B6-8110-DD0A81820F8C}", - "{4B9F66E9-FAC9-47AB-B1EF-C16756FBFD06}", - "{72EA49C6-2806-48BD-B81B-D4905102E19C}", - "{5728EB7E-8929-486C-8CD5-3238D060E768}" -); +sub generate_guid ($) { + my $hex = sha256_hex($_[0]); + $hex =~ s/^(.{8})(.{4})(.{4})(.{4})(.{12}).*/{$1-$2-$3-$4-$5}/; + $hex =~ tr/a-z/A-Z/; + return $hex; +} sub generate { my ($git_dir, $out_dir, $rel_dir, %build_structure) = @_; @@ -92,9 +46,8 @@ sub createLibProject { $target =~ s/\//_/g; $target =~ s/\.a//; - my $uuid = $GUIDS[$guid_index]; + my $uuid = generate_guid($libname); $$build_structure{"LIBS_${target}_GUID"} = $uuid; - $guid_index += 1; my @srcs = sort(map("$rel_dir\\$_", @{$$build_structure{"LIBS_${libname}_SOURCES"}})); my @sources; @@ -311,9 +264,8 @@ sub createAppProject { $target =~ s/\//_/g; $target =~ s/\.exe//; - my $uuid = $GUIDS[$guid_index]; + my $uuid = generate_guid($appname); $$build_structure{"APPS_${target}_GUID"} = $uuid; - $guid_index += 1; my @srcs = sort(map("$rel_dir\\$_", @{$$build_structure{"APPS_${appname}_SOURCES"}})); my @sources; From 48143e165c71c94b49bc5a552bba739d912a84d1 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Tue, 8 Nov 2016 11:00:01 +0100 Subject: [PATCH 267/406] msvc: ignore .dll and incremental compile output Ignore .dll files copied into the top-level directory. Ignore MSVC incremental compiler output files. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index ffceea7d59fd21..56a1832f3bdd9e 100644 --- a/.gitignore +++ b/.gitignore @@ -222,5 +222,10 @@ *.user *.idb *.pdb +*.ilk +*.iobj +*.ipdb +*.dll +.vs/ /Debug/ /Release/ From 925a41c4511925929af0aced094c100b69f7445b Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Sun, 19 Jul 2015 17:43:29 +0100 Subject: [PATCH 268/406] Vcproj.pm: list git.exe first to be startup project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Visual Studio takes the first listed application/library as the default startup project [1]. Detect the 'git' project and place it the head of the apps list, rather than the tail. Export the apps list before libs list for both the projects and global structures of the .sln file. [1] http://stackoverflow.com/questions/1238553/ vs2008-where-is-the-startup-project-setting-stored-for-a-solution "In the solution file, there are a list of pseudo-XML "Project" entries. It turns out that whatever is the first one ends up as the Startup Project, unless it’s overridden in the suo file. Argh. I just rearranged the order in the file and it’s good." "just moving the pseudo-xml isn't enough. You also have to move the group of entries in the "GlobalSection(ProjectConfigurationPlatforms) = postSolution" group that has the GUID of the project you moved to the top. So there are two places to move lines." Signed-off-by: Philip Oakley <philipoakley@iee.org> --- contrib/buildsystems/Generators/Vcproj.pm | 33 +++++++++++++---------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/contrib/buildsystems/Generators/Vcproj.pm b/contrib/buildsystems/Generators/Vcproj.pm index c79b706bc8de4f..d862cae5034347 100644 --- a/contrib/buildsystems/Generators/Vcproj.pm +++ b/contrib/buildsystems/Generators/Vcproj.pm @@ -513,20 +513,18 @@ sub createGlueProject { foreach (@apps) { $_ =~ s/\//_/g; $_ =~ s/\.exe//; - push(@tmp, $_); + if ($_ eq "git" ) { + unshift(@tmp, $_); + } else { + push(@tmp, $_); + } } @apps = @tmp; open F, ">git.sln" || die "Could not open git.sln for writing!\n"; binmode F, ":crlf"; print F "$SLN_HEAD"; - foreach (@libs) { - my $libname = $_; - my $uuid = $build_structure{"LIBS_${libname}_GUID"}; - print F "$SLN_PRE"; - print F "\"${libname}\", \"${libname}\\${libname}.vcproj\", \"${uuid}\""; - print F "$SLN_POST"; - } + my $uuid_libgit = $build_structure{"LIBS_libgit_GUID"}; my $uuid_xdiff_lib = $build_structure{"LIBS_xdiff_lib_GUID"}; foreach (@apps) { @@ -540,6 +538,13 @@ sub createGlueProject { print F " EndProjectSection"; print F "$SLN_POST"; } + foreach (@libs) { + my $libname = $_; + my $uuid = $build_structure{"LIBS_${libname}_GUID"}; + print F "$SLN_PRE"; + print F "\"${libname}\", \"${libname}\\${libname}.vcproj\", \"${uuid}\""; + print F "$SLN_POST"; + } print F << "EOM"; Global @@ -551,17 +556,17 @@ EOM print F << "EOM"; GlobalSection(ProjectConfigurationPlatforms) = postSolution EOM - foreach (@libs) { - my $libname = $_; - my $uuid = $build_structure{"LIBS_${libname}_GUID"}; + foreach (@apps) { + my $appname = $_; + my $uuid = $build_structure{"APPS_${appname}_GUID"}; print F "\t\t${uuid}.Debug|Win32.ActiveCfg = Debug|Win32\n"; print F "\t\t${uuid}.Debug|Win32.Build.0 = Debug|Win32\n"; print F "\t\t${uuid}.Release|Win32.ActiveCfg = Release|Win32\n"; print F "\t\t${uuid}.Release|Win32.Build.0 = Release|Win32\n"; } - foreach (@apps) { - my $appname = $_; - my $uuid = $build_structure{"APPS_${appname}_GUID"}; + foreach (@libs) { + my $libname = $_; + my $uuid = $build_structure{"LIBS_${libname}_GUID"}; print F "\t\t${uuid}.Debug|Win32.ActiveCfg = Debug|Win32\n"; print F "\t\t${uuid}.Debug|Win32.Build.0 = Debug|Win32\n"; print F "\t\t${uuid}.Release|Win32.ActiveCfg = Release|Win32\n"; From 3b44c8acf49673795bfc37e569e39ba6865776ab Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 25 Oct 2016 03:02:36 -0700 Subject: [PATCH 269/406] Vcproj.pm: do not configure VCWebServiceProxyGeneratorTool It is not necessary, and Visual Studio 2015 no longer supports it, anyway. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/Generators/Vcproj.pm | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/contrib/buildsystems/Generators/Vcproj.pm b/contrib/buildsystems/Generators/Vcproj.pm index d862cae5034347..b17800184c63f2 100644 --- a/contrib/buildsystems/Generators/Vcproj.pm +++ b/contrib/buildsystems/Generators/Vcproj.pm @@ -115,9 +115,6 @@ sub createLibProject { <Tool Name="VCXMLDataGeneratorTool" /> - <Tool - Name="VCWebServiceProxyGeneratorTool" - /> <Tool Name="VCMIDLTool" /> @@ -181,9 +178,6 @@ sub createLibProject { <Tool Name="VCXMLDataGeneratorTool" /> - <Tool - Name="VCWebServiceProxyGeneratorTool" - /> <Tool Name="VCMIDLTool" /> @@ -339,9 +333,6 @@ sub createAppProject { <Tool Name="VCXMLDataGeneratorTool" /> - <Tool - Name="VCWebServiceProxyGeneratorTool" - /> <Tool Name="VCMIDLTool" /> @@ -410,9 +401,6 @@ sub createAppProject { <Tool Name="VCXMLDataGeneratorTool" /> - <Tool - Name="VCWebServiceProxyGeneratorTool" - /> <Tool Name="VCMIDLTool" /> From 54748edf3d013d21ea8852415b9e7674ae3b4738 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 25 Oct 2016 03:11:22 -0700 Subject: [PATCH 270/406] Vcproj.pm: urlencode '<' and '>' when generating VC projects Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/Generators/Vcproj.pm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/contrib/buildsystems/Generators/Vcproj.pm b/contrib/buildsystems/Generators/Vcproj.pm index b17800184c63f2..737647e76afd42 100644 --- a/contrib/buildsystems/Generators/Vcproj.pm +++ b/contrib/buildsystems/Generators/Vcproj.pm @@ -59,6 +59,8 @@ sub createLibProject { my $includes= join(";", sort(map(""$rel_dir\\$_"", @{$$build_structure{"LIBS_${libname}_INCLUDES"}}))); my $cflags = join(" ", sort(@{$$build_structure{"LIBS_${libname}_CFLAGS"}})); $cflags =~ s/\"/"/g; + $cflags =~ s/</</g; + $cflags =~ s/>/>/g; my $cflags_debug = $cflags; $cflags_debug =~ s/-MT/-MTd/; @@ -80,6 +82,8 @@ sub createLibProject { $defines =~ s/-D//g; $defines =~ s/\"/\\"/g; + $defines =~ s/</</g; + $defines =~ s/>/>/g; $defines =~ s/\'//g; $includes =~ s/-I//g; mkdir "$target" || die "Could not create the directory $target for lib project!\n"; @@ -271,6 +275,8 @@ sub createAppProject { my $includes= join(";", sort(map(""$rel_dir\\$_"", @{$$build_structure{"APPS_${appname}_INCLUDES"}}))); my $cflags = join(" ", sort(@{$$build_structure{"APPS_${appname}_CFLAGS"}})); $cflags =~ s/\"/"/g; + $cflags =~ s/</</g; + $cflags =~ s/>/>/g; my $cflags_debug = $cflags; $cflags_debug =~ s/-MT/-MTd/; @@ -297,6 +303,8 @@ sub createAppProject { $defines =~ s/-D//g; $defines =~ s/\"/\\"/g; + $defines =~ s/</</g; + $defines =~ s/>/>/g; $defines =~ s/\'//g; $defines =~ s/\\\\/\\/g; $includes =~ s/-I//g; From c626c28cd79f0d74911917c6d46484662157b17a Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Mon, 20 Jul 2015 16:44:59 +0100 Subject: [PATCH 271/406] contrib/buildsystems: ignore invalidcontinue.obj Since 4b623d8 (MSVC: link in invalidcontinue.obj for better POSIX compatibility, 2014-03-29), invalidcontinue.obj is linked in the MSVC build, but it was not parsed correctly by the buildsystem. Ignore it, as it is known to Visual Studio and will be handled elsewhere. Also only substitute filenames ending with .o when generating the source .c filename, otherwise we would start to expect .cbj files to generate .obj files (which are not generated by our build)... In the future there may be source files that produce .obj files so keep the two issues (.obj files with & without source files) separate. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Duncan Smart <duncan.smart@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 23da787dc55651..53e65d4db713c3 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -282,7 +282,7 @@ sub handleLibLine # exit(1); foreach (@objfiles) { my $sourcefile = $_; - $sourcefile =~ s/\.o/.c/; + $sourcefile =~ s/\.o$/.c/; push(@sources, $sourcefile); push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}}); push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}}); @@ -326,8 +326,12 @@ sub handleLinkLine } elsif ($part =~ /\.(a|lib)$/) { $part =~ s/\.a$/.lib/; push(@libs, $part); - } elsif ($part =~ /\.(o|obj)$/) { + } elsif ($part eq 'invalidcontinue.obj') { + # ignore - known to MSVC + } elsif ($part =~ /\.o$/) { push(@objfiles, $part); + } elsif ($part =~ /\.obj$/) { + # do nothing, 'make' should not be producing .obj, only .o files } else { die "Unhandled lib option @ line $lineno: $part"; } @@ -336,7 +340,7 @@ sub handleLinkLine # exit(1); foreach (@objfiles) { my $sourcefile = $_; - $sourcefile =~ s/\.o/.c/; + $sourcefile =~ s/\.o$/.c/; push(@sources, $sourcefile); push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}}); push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}}); From b64b0043258af149a67d5704d561767205eea062 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 26 Oct 2016 04:59:06 -0700 Subject: [PATCH 272/406] contrib/buildsystems: ignore irrelevant files in Generators/ The Generators/ directory can contain spurious files such as editors' backup files. Even worse, there could be .swp files which are not even valid Perl scripts. Let's just ignore anything but .pm files in said directory. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/Generators.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/buildsystems/Generators.pm b/contrib/buildsystems/Generators.pm index 408ef714b8f0dc..aa4cbaa2adacb1 100644 --- a/contrib/buildsystems/Generators.pm +++ b/contrib/buildsystems/Generators.pm @@ -17,7 +17,7 @@ BEGIN { $me = dirname($me); if (opendir(D,"$me/Generators")) { foreach my $gen (readdir(D)) { - next if ($gen =~ /^\.\.?$/); + next unless ($gen =~ /\.pm$/); require "${me}/Generators/$gen"; $gen =~ s,\.pm,,; push(@AVAILABLE, $gen); From 5fef376e6d3234d5418dfd9cb410d39c572d4fd7 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Sun, 19 Jul 2015 17:41:13 +0100 Subject: [PATCH 273/406] contrib/buildsystems: fix misleading error message The error message talked about a "lib option", but it clearly referred to a link option. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 53e65d4db713c3..11f0e16ddacb89 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -333,7 +333,7 @@ sub handleLinkLine } elsif ($part =~ /\.obj$/) { # do nothing, 'make' should not be producing .obj, only .o files } else { - die "Unhandled lib option @ line $lineno: $part"; + die "Unhandled link option @ line $lineno: $part"; } } # print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n"; From 7f49a2ae834301db0e196385e3f547348b3f34f3 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Sun, 19 Jul 2015 16:08:21 +0100 Subject: [PATCH 274/406] contrib/buildsystems: handle quoted spaces in filenames The engine.pl script expects file names not to contain spaces. However, paths with spaces are quite prevalent on Windows. Use shellwords() rather than split() to parse them correctly. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 11f0e16ddacb89..ad6a82c30ce808 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -12,6 +12,7 @@ use File::Spec; use Cwd; use Generators; +use Text::ParseWords; my (%build_structure, %compile_options, @makedry); my $out_dir = getcwd(); @@ -231,7 +232,7 @@ sub removeDuplicates sub handleCompileLine { my ($line, $lineno) = @_; - my @parts = split(' ', $line); + my @parts = shellwords($line); my $sourcefile; shift(@parts); # ignore cmd while (my $part = shift @parts) { @@ -265,7 +266,7 @@ sub handleLibLine my (@objfiles, @lflags, $libout, $part); # kill cmd and rm 'prefix' $line =~ s/^rm -f .* && .* rcs //; - my @parts = split(' ', $line); + my @parts = shellwords($line); while ($part = shift @parts) { if ($part =~ /^-/) { push(@lflags, $part); @@ -306,7 +307,7 @@ sub handleLinkLine { my ($line, $lineno) = @_; my (@objfiles, @lflags, @libs, $appout, $part); - my @parts = split(' ', $line); + my @parts = shellwords($line); shift(@parts); # ignore cmd while ($part = shift @parts) { if ($part =~ /^-IGNORE/) { From 960078f24bebc7f24cfd8f642954a6cf2113bbc9 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Mon, 9 Feb 2015 14:34:29 +0000 Subject: [PATCH 275/406] contrib/buildsystems: ignore gettext stuff Git's build contains steps to handle internationalization. This caused hiccups in the parser used to generate QMake/Visual Studio project files. As those steps are irrelevant in this context, let's just ignore them. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index ad6a82c30ce808..9db3d43a1eb2f7 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -141,6 +141,12 @@ sub parseMakeOutput next; } + if ($text =~ /^(mkdir|msgfmt) /) { + # options to the Portable Object translations + # the line "mkdir ... && msgfmt ..." contains no linker options + next; + } + if($text =~ / -c /) { # compilation handleCompileLine($text, $line); From 9226fe771e88de15fb06cf15526940d7230040d5 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Sun, 19 Jul 2015 16:45:32 +0100 Subject: [PATCH 276/406] contrib/buildsystems: redirect errors of the dry run into a log file Rather than swallowing the errors, it is better to have them in a file. To make it obvious what this is about, use the file name 'msvc-build-makedryerrors.txt'. Further, if the output is empty, simply delete that file. As we target Git for Windows' SDK (which, unlike its predecessor msysGit, offers Perl versions newer than 5.8), we can use the quite readable syntax `if -f -z $ErrsFile` (available in Perl >=5.10). Note that the file will contain the new values of the GIT_VERSION and GITGUI_VERSION if they were generated by the make file. They are omitted if the release is tagged and indentically defined in their respective GIT_VERSION_GEN file DEF_VER variables. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 9db3d43a1eb2f7..de5c0b6b25a489 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -73,7 +73,12 @@ sub showUsage EOM # Pipe a make --dry-run into a variable, if not already loaded from file -@makedry = `cd $git_dir && make -n MSVC=1 V=1 2>/dev/null` if !@makedry; +# Capture the make dry stderr to file for review (will be empty for a release build). + +my $ErrsFile = "msvc-build-makedryerrors.txt"; +@makedry = `make -C $git_dir -n MSVC=1 V=1 2>$ErrsFile` if !@makedry; +# test for an empty Errors file and remove it +unlink $ErrsFile if -f -z $ErrsFile; # Parse the make output into usable info parseMakeOutput(); From cd382604f79a982d344719d88fbc234f9bf3c2d2 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Thu, 16 Jul 2015 23:40:13 +0100 Subject: [PATCH 277/406] contrib/buildsystems: optionally capture the dry-run in a file Add an option for capturing the output of the make dry-run used in determining the msvc-build structure for easy debugging. You can use the output of `--make-out <path>` in subsequent runs via the `--in <path>` option. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index de5c0b6b25a489..732239d817a769 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -32,6 +32,7 @@ sub showUsage -g <GENERATOR> --gen <GENERATOR> Specify the buildsystem generator (default: $gen) Available: $genlist -o <PATH> --out <PATH> Specify output directory generation (default: .) + --make-out <PATH> Write the output of GNU Make into a file -i <FILE> --in <FILE> Specify input file, instead of running GNU Make -h,-? --help This help EOM @@ -39,6 +40,7 @@ sub showUsage } # Parse command-line options +my $make_out; while (@ARGV) { my $arg = shift @ARGV; if ("$arg" eq "-h" || "$arg" eq "--help" || "$arg" eq "-?") { @@ -46,6 +48,8 @@ sub showUsage exit(0); } elsif("$arg" eq "--out" || "$arg" eq "-o") { $out_dir = shift @ARGV; + } elsif("$arg" eq "--make-out") { + $make_out = shift @ARGV; } elsif("$arg" eq "--gen" || "$arg" eq "-g") { $gen = shift @ARGV; } elsif("$arg" eq "--in" || "$arg" eq "-i") { @@ -80,6 +84,12 @@ sub showUsage # test for an empty Errors file and remove it unlink $ErrsFile if -f -z $ErrsFile; +if (defined $make_out) { + open OUT, ">" . $make_out; + print OUT @makedry; + close OUT; +} + # Parse the make output into usable info parseMakeOutput(); From 816a5eda09b65693a20970284911e948c104bcc2 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Wed, 4 May 2016 15:53:48 +0100 Subject: [PATCH 278/406] contrib/buildsystems: handle the curl library option Upon seeing the '-lcurl' option, point to the libcurl.lib. While there, fix the elsif indentation. Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 732239d817a769..fddf2dc1513ccd 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -339,10 +339,12 @@ sub handleLinkLine $appout = shift @parts; } elsif ("$part" eq "-lz") { push(@libs, "zlib.lib"); - } elsif ("$part" eq "-lcrypto") { + } elsif ("$part" eq "-lcrypto") { push(@libs, "libeay32.lib"); } elsif ("$part" eq "-lssl") { push(@libs, "ssleay32.lib"); + } elsif ("$part" eq "-lcurl") { + push(@libs, "libcurl.lib"); } elsif ($part =~ /^-/) { push(@lflags, $part); } elsif ($part =~ /\.(a|lib)$/) { From 0b6897f559dab43f5b6d75245dc3717da4fde5d7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 27 Oct 2016 01:45:49 -0700 Subject: [PATCH 279/406] contrib/buildsystems: handle libiconv, too Git's test suite shows tons of breakages unless Git is compiled *without* NO_ICONV. That means, in turn, that we need to generate build definitions *with* libiconv, which in turn implies that we have to handle the -liconv option properly. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index fddf2dc1513ccd..44adadb2e6436f 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -345,6 +345,8 @@ sub handleLinkLine push(@libs, "ssleay32.lib"); } elsif ("$part" eq "-lcurl") { push(@libs, "libcurl.lib"); + } elsif ("$part" eq "-liconv") { + push(@libs, "libiconv.lib"); } elsif ($part =~ /^-/) { push(@lflags, $part); } elsif ($part =~ /\.(a|lib)$/) { From 51c49fad8462dbabafc9fc5789f44d9b64d93837 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 27 Oct 2016 07:25:00 -0700 Subject: [PATCH 280/406] contrib/buildsystems: handle options starting with a slash With the recent changes to allow building with MSVC=1, we now pass the /OPT:REF option to the compiler. This confuses the parser that wants to turn the output of a dry run into project definitions for QMake and Visual Studio: Unhandled link option @ line 213: /OPT:REF at [...] Let's just extend the code that passes through options that start with a dash, so that it passes through options that start with a slash, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 44adadb2e6436f..134a82d31f7b12 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -347,7 +347,7 @@ sub handleLinkLine push(@libs, "libcurl.lib"); } elsif ("$part" eq "-liconv") { push(@libs, "libiconv.lib"); - } elsif ($part =~ /^-/) { + } elsif ($part =~ /^[-\/]/) { push(@lflags, $part); } elsif ($part =~ /\.(a|lib)$/) { $part =~ s/\.a$/.lib/; From 79cde5ce2c298f1df8d45ca6b0bfb214f7e18359 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 4 Dec 2017 20:43:23 +0100 Subject: [PATCH 281/406] contrib/buildsystems: error out on unknown option One time too many did this developer call the `generate` script passing a `--make-out=<PATH>` option that was happily ignored (because there should be a space, not an equal sign, between `--make-out` and the path). And one time too many, this script not only ignored it but did not even complain. Let's fix that. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/engine.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 134a82d31f7b12..9f4e7a2ccb9352 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -57,6 +57,8 @@ sub showUsage open(F, "<$infile") || die "Couldn't open file $infile"; @makedry = <F>; close(F); + } else { + die "Unknown option: " . $arg; } } From d28881b1ca6883f30ceb3e923d888d0f693b2d4f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 26 Oct 2016 02:28:10 -0700 Subject: [PATCH 282/406] contrib/buildsystems: add a backend for modern Visual Studio versions Based on the previous patch series to be able to compile Git using Visual C++ from the command-line, this patch offers to generate project definitions for Visual Studio, so that Git can be developed in a modern IDE. Based on the generator for Visual Studio versions <= 2008 (which used .sln/.vcproj files), this patch copy-edits the generator of the .vcproj files to a new generator that produces .vcxproj files ready for Visual Studio 2010 and later (or MSBuild). As the vcpkg system (which is used to build Git's dependencies) cannot run in parallel (it does not lock, wreaking havoc with files being accessed and written at the same time, letting the vcpkg processes stumble over each others' toes), we make libgit the root of the project dependency tree and initialize the vcpkg system in this project's PreBuildEvent. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- contrib/buildsystems/Generators/Vcxproj.pm | 380 +++++++++++++++++++++ contrib/buildsystems/engine.pl | 2 + 2 files changed, 382 insertions(+) create mode 100644 contrib/buildsystems/Generators/Vcxproj.pm diff --git a/contrib/buildsystems/Generators/Vcxproj.pm b/contrib/buildsystems/Generators/Vcxproj.pm new file mode 100644 index 00000000000000..440fdceb094620 --- /dev/null +++ b/contrib/buildsystems/Generators/Vcxproj.pm @@ -0,0 +1,380 @@ +package Generators::Vcxproj; +require Exporter; + +use strict; +use vars qw($VERSION); +use Digest::SHA qw(sha256_hex); + +our $VERSION = '1.00'; +our(@ISA, @EXPORT, @EXPORT_OK, @AVAILABLE); +@ISA = qw(Exporter); + +BEGIN { + push @EXPORT_OK, qw(generate); +} + +sub generate_guid ($) { + my $hex = sha256_hex($_[0]); + $hex =~ s/^(.{8})(.{4})(.{4})(.{4})(.{12}).*/{$1-$2-$3-$4-$5}/; + $hex =~ tr/a-z/A-Z/; + return $hex; +} + +sub generate { + my ($git_dir, $out_dir, $rel_dir, %build_structure) = @_; + my @libs = @{$build_structure{"LIBS"}}; + foreach (@libs) { + createProject($_, $git_dir, $out_dir, $rel_dir, \%build_structure, 1); + } + + my @apps = @{$build_structure{"APPS"}}; + foreach (@apps) { + createProject($_, $git_dir, $out_dir, $rel_dir, \%build_structure, 0); + } + + createGlueProject($git_dir, $out_dir, $rel_dir, %build_structure); + return 0; +} + +sub createProject { + my ($name, $git_dir, $out_dir, $rel_dir, $build_structure, $static_library) = @_; + my $label = $static_library ? "lib" : "app"; + my $prefix = $static_library ? "LIBS_" : "APPS_"; + my $config_type = $static_library ? "StaticLibrary" : "Application"; + print "Generate $name vcxproj $label project\n"; + my $cdup = $name; + $cdup =~ s/[^\/]+/../g; + $cdup =~ s/\//\\/g; + $rel_dir = $rel_dir eq "." ? $cdup : "$cdup\\$rel_dir"; + $rel_dir =~ s/\//\\/g; + + my $target = $name; + if ($static_library) { + $target =~ s/\.a//; + } else { + $target =~ s/\.exe//; + } + + my $uuid = generate_guid($name); + $$build_structure{"$prefix${target}_GUID"} = $uuid; + my $vcxproj = $target; + $vcxproj =~ s/(.*\/)?(.*)/$&\/$2.vcxproj/; + $vcxproj =~ s/([^\/]*)(\/lib)\/(lib.vcxproj)/$1$2\/$1_$3/; + $$build_structure{"$prefix${target}_VCXPROJ"} = $vcxproj; + + my @srcs = sort(map("$rel_dir\\$_", @{$$build_structure{"$prefix${name}_SOURCES"}})); + my @sources; + foreach (@srcs) { + $_ =~ s/\//\\/g; + push(@sources, $_); + } + my $defines = join(";", sort(@{$$build_structure{"$prefix${name}_DEFINES"}})); + my $includes= join(";", sort(map { s/^-I//; s/\//\\/g; File::Spec->file_name_is_absolute($_) ? $_ : "$rel_dir\\$_" } @{$$build_structure{"$prefix${name}_INCLUDES"}})); + my $cflags = join(" ", sort(map { s/^-[GLMOWZ].*//; s/.* .*/"$&"/; $_; } @{$$build_structure{"$prefix${name}_CFLAGS"}})); + $cflags =~ s/</</g; + $cflags =~ s/>/>/g; + + my $libs_release = "\n "; + my $libs_debug = "\n "; + if (!$static_library) { + $libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}})); + $libs_debug = $libs_release; + $libs_debug =~ s/zlib\.lib/zlibd\.lib/; + } + + $defines =~ s/-D//g; + $defines =~ s/</</g; + $defines =~ s/>/>/g; + $defines =~ s/\'//g; + + die "Could not create the directory $target for $label project!\n" unless (-d "$target" || mkdir "$target"); + + open F, ">$vcxproj" or die "Could not open $vcxproj for writing!\n"; + binmode F, ":crlf :utf8"; + print F chr(0xFEFF); + print F << "EOM"; +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>$uuid</ProjectGuid> + <Keyword>Win32Proj</Keyword> + <VCPKGArch Condition="'\$(Platform)'=='Win32'">x86-windows</VCPKGArch> + <VCPKGArch Condition="'\$(Platform)'!='Win32'">x64-windows</VCPKGArch> + <VCPKGArchDirectory>$cdup\\compat\\vcbuild\\vcpkg\\installed\\\$(VCPKGArch)</VCPKGArchDirectory> + <VCPKGBinDirectory Condition="'\(Configuration)'=='Debug'">\$(VCPKGArchDirectory)\\debug\\bin</VCPKGBinDirectory> + <VCPKGLibDirectory Condition="'\(Configuration)'=='Debug'">\$(VCPKGArchDirectory)\\debug\\lib</VCPKGLibDirectory> + <VCPKGBinDirectory Condition="'\(Configuration)'!='Debug'">\$(VCPKGArchDirectory)\\bin</VCPKGBinDirectory> + <VCPKGLibDirectory Condition="'\(Configuration)'!='Debug'">\$(VCPKGArchDirectory)\\lib</VCPKGLibDirectory> + <VCPKGIncludeDirectory>\$(VCPKGArchDirectory)\\include</VCPKGIncludeDirectory> + <VCPKGLibs Condition="'\(Configuration)'=='Debug'">$libs_debug</VCPKGLibs> + <VCPKGLibs Condition="'\(Configuration)'!='Debug'">$libs_release</VCPKGLibs> + </PropertyGroup> + <Import Project="\$(VCTargetsPath)\\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'\$(Configuration)'=='Debug'" Label="Configuration"> + <UseDebugLibraries>true</UseDebugLibraries> + <LinkIncremental>true</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'\$(Configuration)'=='Release'" Label="Configuration"> + <UseDebugLibraries>false</UseDebugLibraries> + <WholeProgramOptimization>true</WholeProgramOptimization> + </PropertyGroup> + <PropertyGroup> + <ConfigurationType>$config_type</ConfigurationType> + <PlatformToolset>v140</PlatformToolset> + <!-- <CharacterSet>UTF-8</CharacterSet> --> + <OutDir>..\\</OutDir> + <!-- <IntDir>\$(ProjectDir)\$(Configuration)\\</IntDir> --> + </PropertyGroup> + <Import Project="\$(VCTargetsPath)\\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets"> + <Import Project="\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props" Condition="exists('\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup> + <GenerateManifest>false</GenerateManifest> + <EnableManagedIncrementalBuild>true</EnableManagedIncrementalBuild> + </PropertyGroup> + <ItemDefinitionGroup> + <ClCompile> + <AdditionalOptions>$cflags %(AdditionalOptions)</AdditionalOptions> + <AdditionalIncludeDirectories>$cdup;$cdup\\compat;$cdup\\compat\\regex;$cdup\\compat\\win32;$cdup\\compat\\poll;$cdup\\compat\\vcbuild\\include;\$(VCPKGIncludeDirectory);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <EnableParallelCodeGeneration /> + <MinimalRebuild>true</MinimalRebuild> + <InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion> + <PrecompiledHeader /> + <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> + </ClCompile> + <Lib> + <SuppressStartupBanner>true</SuppressStartupBanner> + </Lib> + <Link> + <AdditionalLibraryDirectories>\$(VCPKGLibDirectory);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + <AdditionalDependencies>\$(VCPKGLibs);\$(AdditionalDependencies)</AdditionalDependencies> + <AdditionalOptions>invalidcontinue.obj %(AdditionalOptions)</AdditionalOptions> + <ManifestFile>$cdup\\compat\\win32\\git.manifest</ManifestFile> + <SubSystem>Console</SubSystem> + </Link> +EOM + if ($target eq 'libgit') { + print F << "EOM"; + <PreBuildEvent Condition="!Exists('$cdup\\compat\\vcbuild\\vcpkg\\installed\\\$(VCPKGArch)\\include\\openssl\\ssl.h')"> + <Message>Initialize VCPKG</Message> + <Command>del "$cdup\\compat\\vcbuild\\vcpkg"</Command> + <Command>call "$cdup\\compat\\vcbuild\\vcpkg_install.bat"</Command> + </PreBuildEvent> +EOM + } + print F << "EOM"; + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'\$(Platform)'=='Win32'"> + <Link> + <TargetMachine>MachineX86</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'\$(Configuration)'=='Debug'"> + <ClCompile> + <Optimization>Disabled</Optimization> + <PreprocessorDefinitions>WIN32;_DEBUG;$defines;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> + </ClCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'\$(Configuration)'=='Release'"> + <ClCompile> + <Optimization>MaxSpeed</Optimization> + <IntrinsicFunctions>true</IntrinsicFunctions> + <PreprocessorDefinitions>WIN32;NDEBUG;$defines;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> + <FunctionLevelLinking>true</FunctionLevelLinking> + <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed> + </ClCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + </Link> + </ItemDefinitionGroup> + <ItemGroup> +EOM + foreach(@sources) { + print F << "EOM"; + <ClCompile Include="$_" /> +EOM + } + print F << "EOM"; + </ItemGroup> +EOM + if (!$static_library || $target =~ 'vcs-svn') { + my $uuid_libgit = $$build_structure{"LIBS_libgit_GUID"}; + my $uuid_xdiff_lib = $$build_structure{"LIBS_xdiff/lib_GUID"}; + + print F << "EOM"; + <ItemGroup> + <ProjectReference Include="$cdup\\libgit\\libgit.vcxproj"> + <Project>$uuid_libgit</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + <ProjectReference Include="$cdup\\xdiff\\lib\\xdiff_lib.vcxproj"> + <Project>$uuid_xdiff_lib</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> +EOM + if ($name =~ /(test-(line-buffer|svn-fe)|^git-remote-testsvn)\.exe$/) { + my $uuid_vcs_svn_lib = $$build_structure{"LIBS_vcs-svn/lib_GUID"}; + print F << "EOM"; + <ProjectReference Include="$cdup\\vcs-svn\\lib\\vcs-svn_lib.vcxproj"> + <Project>$uuid_vcs_svn_lib</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> +EOM + } + print F << "EOM"; + </ItemGroup> +EOM + } + print F << "EOM"; + <Import Project="\$(VCTargetsPath)\\Microsoft.Cpp.targets" /> +EOM + if (!$static_library) { + print F << "EOM"; + <Target Name="${target}_AfterBuild" AfterTargets="AfterBuild"> + <ItemGroup> + <DLLsAndPDBs Include="\$(VCPKGBinDirectory)\\*.dll;\$(VCPKGBinDirectory)\\*.pdb" /> + </ItemGroup> + <Copy SourceFiles="@(DLLsAndPDBs)" DestinationFolder="\$(OutDir)" SkipUnchangedFiles="true" UseHardlinksIfPossible="true" /> + </Target> +EOM + } + print F << "EOM"; +</Project> +EOM + close F; +} + +sub createGlueProject { + my ($git_dir, $out_dir, $rel_dir, %build_structure) = @_; + print "Generate solutions file\n"; + $rel_dir = "..\\$rel_dir"; + $rel_dir =~ s/\//\\/g; + my $SLN_HEAD = "Microsoft Visual Studio Solution File, Format Version 11.00\n# Visual Studio 2010\n"; + my $SLN_PRE = "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = "; + my $SLN_POST = "\nEndProject\n"; + + my @libs = @{$build_structure{"LIBS"}}; + my @tmp; + foreach (@libs) { + $_ =~ s/\.a//; + push(@tmp, $_); + } + @libs = @tmp; + + my @apps = @{$build_structure{"APPS"}}; + @tmp = (); + foreach (@apps) { + $_ =~ s/\.exe//; + if ($_ eq "git" ) { + unshift(@tmp, $_); + } else { + push(@tmp, $_); + } + } + @apps = @tmp; + + open F, ">git.sln" || die "Could not open git.sln for writing!\n"; + binmode F, ":crlf :utf8"; + print F chr(0xFEFF); + print F "$SLN_HEAD"; + + foreach (@apps) { + my $appname = $_; + my $uuid = $build_structure{"APPS_${appname}_GUID"}; + print F "$SLN_PRE"; + my $vcxproj = $build_structure{"APPS_${appname}_VCXPROJ"}; + $vcxproj =~ s/\//\\/g; + $appname =~ s/.*\///; + print F "\"${appname}\", \"${vcxproj}\", \"${uuid}\""; + print F "$SLN_POST"; + } + foreach (@libs) { + my $libname = $_; + my $uuid = $build_structure{"LIBS_${libname}_GUID"}; + print F "$SLN_PRE"; + my $vcxproj = $build_structure{"LIBS_${libname}_VCXPROJ"}; + $vcxproj =~ s/\//\\/g; + $libname =~ s/\//_/g; + print F "\"${libname}\", \"${vcxproj}\", \"${uuid}\""; + print F "$SLN_POST"; + } + + print F << "EOM"; +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection +EOM + print F << "EOM"; + GlobalSection(ProjectConfigurationPlatforms) = postSolution +EOM + foreach (@apps) { + my $appname = $_; + my $uuid = $build_structure{"APPS_${appname}_GUID"}; + print F "\t\t${uuid}.Debug|x64.ActiveCfg = Debug|x64\n"; + print F "\t\t${uuid}.Debug|x64.Build.0 = Debug|x64\n"; + print F "\t\t${uuid}.Debug|x86.ActiveCfg = Debug|Win32\n"; + print F "\t\t${uuid}.Debug|x86.Build.0 = Debug|Win32\n"; + print F "\t\t${uuid}.Release|x64.ActiveCfg = Release|x64\n"; + print F "\t\t${uuid}.Release|x64.Build.0 = Release|x64\n"; + print F "\t\t${uuid}.Release|x86.ActiveCfg = Release|Win32\n"; + print F "\t\t${uuid}.Release|x86.Build.0 = Release|Win32\n"; + } + foreach (@libs) { + my $libname = $_; + my $uuid = $build_structure{"LIBS_${libname}_GUID"}; + print F "\t\t${uuid}.Debug|x64.ActiveCfg = Debug|x64\n"; + print F "\t\t${uuid}.Debug|x64.Build.0 = Debug|x64\n"; + print F "\t\t${uuid}.Debug|x86.ActiveCfg = Debug|Win32\n"; + print F "\t\t${uuid}.Debug|x86.Build.0 = Debug|Win32\n"; + print F "\t\t${uuid}.Release|x64.ActiveCfg = Release|x64\n"; + print F "\t\t${uuid}.Release|x64.Build.0 = Release|x64\n"; + print F "\t\t${uuid}.Release|x86.ActiveCfg = Release|Win32\n"; + print F "\t\t${uuid}.Release|x86.Build.0 = Release|Win32\n"; + } + + print F << "EOM"; + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal +EOM + close F; +} + +1; diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 9f4e7a2ccb9352..8bb07e8e25cc50 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -347,6 +347,8 @@ sub handleLinkLine push(@libs, "ssleay32.lib"); } elsif ("$part" eq "-lcurl") { push(@libs, "libcurl.lib"); + } elsif ("$part" eq "-lexpat") { + push(@libs, "expat.lib"); } elsif ("$part" eq "-liconv") { push(@libs, "libiconv.lib"); } elsif ($part =~ /^[-\/]/) { From 2b43801de301014d2b0cc84af5603c00c189f62e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 28 Nov 2016 15:54:59 +0100 Subject: [PATCH 283/406] msvc: add a Makefile target to pre-generate the VS solution The entire idea of generating the VS solution makes only sense if we generate it via Continuous Integration; otherwise potential users would still have to download the entire Git for Windows SDK. So let's just add a target in the Makefile that can be used to generate said solution; The generated files will then be committed so that they can be pushed to a branch ready to check out by Visual Studio users. To make things even more useful, we also generate and commit other files that are required to run the test suite, such as templates and bin-wrappers: with this, developers can run the test suite in a regular Git Bash (that is part of a regular Git for Windows installation) after building the solution in Visual Studio. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- config.mak.uname | 61 ++++++++++++++++++++++++++++++++++ contrib/buildsystems/engine.pl | 3 +- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/config.mak.uname b/config.mak.uname index 320b3ec77ec1e6..c5a288cc59bc25 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -25,10 +25,12 @@ include compat/vcbuild/MSVC-DEFS-GEN # See if vcpkg and the vcpkg-build versions of the third-party # libraries that we use are installed. We include the result # to get $(vcpkg_*) variables defined for the Makefile. +ifeq (,$(SKIP_VCPKG)) compat/vcbuild/VCPKG-DEFS: compat/vcbuild/vcpkg_install.bat @"$<" include compat/vcbuild/VCPKG-DEFS endif +endif # We choose to avoid "if .. else if .. else .. endif endif" # because maintaining the nesting to match is a pain. If @@ -662,3 +664,62 @@ ifeq ($(uname_S),QNX) NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease endif + +vcxproj: + # Require clean work tree + git update-index -q --refresh && \ + git diff-files --quiet && \ + git diff-index --cached --quiet HEAD -- + + # Make .vcxproj files and add them + unset QUIET_GEN QUIET_BUILT_IN; \ + perl contrib/buildsystems/generate -g Vcxproj + git add -f git.sln {*,*/lib,t/helper/*}/*.vcxproj + + # Add command-list.h + $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 command-list.h + git add -f command-list.h + + # Add scripts + rm -f perl/perl.mak + $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 \ + $(SCRIPT_LIB) $(SCRIPT_SH_GEN) $(SCRIPT_PERL_GEN) + # Strip out the sane tool path, needed only for building + sed -i '/^git_broken_path_fix ".*/d' git-sh-setup + git add -f $(SCRIPT_LIB) $(SCRIPT_SH_GEN) $(SCRIPT_PERL_GEN) + + # Add Perl module + $(MAKE) $(LIB_PERL_GEN) + git add -f perl/build + + # Add bin-wrappers, for testing + rm -rf bin-wrappers/ + $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 $(test_bindir_programs) + # Ensure that the GIT_EXEC_PATH is a Unix-y one, and that the absolute + # path of the repository is not hard-coded (GIT_EXEC_PATH will be set + # by test-lib.sh according to the current setup) + sed -i -e 's/^\(GIT_EXEC_PATH\)=.*/test -n "$${\1##*:*}" ||\ + \1="$$(cygpath -u "$$\1")"/' \ + -e "s|'$$(pwd)|\"\$$GIT_EXEC_PATH\"'|g" bin-wrappers/* + # Ensure that test-* helpers find the .dll files copied to top-level + sed -i 's|^PATH=.*|&:"$$GIT_EXEC_PATH"|' bin-wrappers/test-* + # We do not want to force hard-linking builtins + sed -i 's|\(git\)-\([-a-z]*\)\.exe"|\1.exe" \2|g' \ + bin-wrappers/git-{receive-pack,upload-archive} + git add -f $(test_bindir_programs) + # remote-ext is a builtin, but invoked as if it were external + sed 's|receive-pack|remote-ext|g' \ + <bin-wrappers/git-receive-pack >bin-wrappers/git-remote-ext + git add -f bin-wrappers/git-remote-ext + + # Add templates + $(MAKE) -C templates + git add -f templates/boilerplates.made templates/blt/ + + # Add build options + $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 GIT-BUILD-OPTIONS + git add -f GIT-BUILD-OPTIONS + + # Commit the whole shebang + git commit -m "Generate Visual Studio solution" \ + -m "Auto-generated by \`$(MAKE)$(MAKEFLAGS) $@\`" diff --git a/contrib/buildsystems/engine.pl b/contrib/buildsystems/engine.pl index 8bb07e8e25cc50..fba8a3f056a0e1 100755 --- a/contrib/buildsystems/engine.pl +++ b/contrib/buildsystems/engine.pl @@ -82,7 +82,8 @@ sub showUsage # Capture the make dry stderr to file for review (will be empty for a release build). my $ErrsFile = "msvc-build-makedryerrors.txt"; -@makedry = `make -C $git_dir -n MSVC=1 V=1 2>$ErrsFile` if !@makedry; +@makedry = `make -C $git_dir -n MSVC=1 SKIP_VCPKG=1 V=1 2>$ErrsFile` +if !@makedry; # test for an empty Errors file and remove it unlink $ErrsFile if -f -z $ErrsFile; From 4b9e0ccf9396eabd731b591f92b222fc68cde0df Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 19 Dec 2017 13:54:14 +0100 Subject: [PATCH 284/406] vcxproj: also link-or-copy builtins The problem with not having, say, git-receive-pack.exe after a full build is that the test suite will then happily use the *installed* git-receive-pack.exe because it finds nothing else. Absolutely not what we want. We want to have confidence that our test covers the MSVC-built Git executables, and not some random stuff. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- config.mak.uname | 15 +++++++++++++++ contrib/buildsystems/Generators/Vcxproj.pm | 3 +++ 2 files changed, 18 insertions(+) diff --git a/config.mak.uname b/config.mak.uname index c5a288cc59bc25..db1f0957a59115 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -676,6 +676,21 @@ vcxproj: perl contrib/buildsystems/generate -g Vcxproj git add -f git.sln {*,*/lib,t/helper/*}/*.vcxproj + # Generate the LinkOrCopyBuiltins.targets file + (echo '<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">' && \ + echo ' <Target Name="CopyBuiltins_AfterBuild" AfterTargets="AfterBuild">' && \ + for name in $(BUILT_INS);\ + do \ + echo ' <Copy SourceFiles="$$(OutDir)\git.exe" DestinationFiles="$$(OutDir)\'"$$name"'" SkipUnchangedFiles="true" UseHardlinksIfPossible="true" />'; \ + done && \ + for name in $(REMOTE_CURL_ALIASES); \ + do \ + echo ' <Copy SourceFiles="$$(OutDir)\'"$(REMOTE_CURL_PRIMARY)"'" DestinationFiles="$$(OutDir)\'"$$name"'" SkipUnchangedFiles="true" UseHardlinksIfPossible="true" />'; \ + done && \ + echo ' </Target>' && \ + echo '</Project>') >git/LinkOrCopyBuiltins.targets + git add -f git/LinkOrCopyBuiltins.targets + # Add command-list.h $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 command-list.h git add -f command-list.h diff --git a/contrib/buildsystems/Generators/Vcxproj.pm b/contrib/buildsystems/Generators/Vcxproj.pm index 440fdceb094620..4bdb8008d1a76c 100644 --- a/contrib/buildsystems/Generators/Vcxproj.pm +++ b/contrib/buildsystems/Generators/Vcxproj.pm @@ -269,6 +269,9 @@ EOM </Target> EOM } + if ($target eq 'git') { + print F " <Import Project=\"LinkOrCopyBuiltins.targets\" />\n"; + } print F << "EOM"; </Project> EOM From ee4bc7c8170df0ee3a3d131898cec8040d2510d1 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Sun, 19 Jul 2015 16:02:40 +0100 Subject: [PATCH 285/406] .gitignore: touch up the entries regarding Visual Studio Add the Microsoft .manifest pattern, and do not anchor the 'Debug' and 'Release' entries at the top-level directory, to allow for multiple projects (one per target). Signed-off-by: Philip Oakley <philipoakley@iee.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 56a1832f3bdd9e..ca976f75a66cd3 100644 --- a/.gitignore +++ b/.gitignore @@ -227,5 +227,6 @@ *.ipdb *.dll .vs/ -/Debug/ -/Release/ +*.manifest +Debug/ +Release/ From 0aa244ea9e931ef9ef18959d93b1dace52e9e547 Mon Sep 17 00:00:00 2001 From: Philip Oakley <philipoakley@iee.org> Date: Mon, 23 Feb 2015 12:50:35 +0000 Subject: [PATCH 286/406] WIP .gitignore: ignore library directories created by MSVC VS2008 buildsystem TODO: test whether we can drop this. Signed-off-by: Philip Oakley <philipoakley@iee.org> --- .gitignore | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/.gitignore b/.gitignore index ca976f75a66cd3..e0ebfad54f1ad8 100644 --- a/.gitignore +++ b/.gitignore @@ -185,6 +185,42 @@ /gitweb/static/gitweb.js /gitweb/static/gitweb.min.* /command-list.h +/libgit +/test-chmtime +/test-ctype +/test-config +/test-date +/test-delta +/test-dump-cache-tree +/test-dump-split-index +/test-dump-untracked-cache +/test-fake-ssh +/test-scrap-cache-tree +/test-genrandom +/test-hashmap +/test-index-version +/test-line-buffer +/test-match-trees +/test-mergesort +/test-mktemp +/test-parse-options +/test-path-utils +/test-prio-queue +/test-read-cache +/test-regex +/test-revision-walking +/test-run-command +/test-sha1 +/test-sha1-array +/test-sigchain +/test-string-list +/test-submodule-config +/test-subprocess +/test-svn-fe +/test-urlmatch-normalization +/test-wildmatch +/vcs-svn_lib +/xdiff_lib *.tar.gz *.dsc *.deb From 45cd1282a6a0a9d4fe3c00af8c342d3daa2c9e0c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 25 Oct 2016 06:06:10 -0700 Subject: [PATCH 287/406] .gitignore: ignore Visual Studio's temporary/generated files Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index e0ebfad54f1ad8..33c645e3feb81f 100644 --- a/.gitignore +++ b/.gitignore @@ -266,3 +266,6 @@ *.manifest Debug/ Release/ +/UpgradeLog*.htm +/git.VC.VC.opendb +/git.VC.db From 35f3e73f36169da9df1f8072dd301ecce86c7ea1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 25 Nov 2016 18:29:51 +0100 Subject: [PATCH 288/406] bin-wrappers: append `.exe` to target paths if necessary When compiling with Visual Studio, the projects' names are identical to the executables modulo the extensions. Read: there will exist both a directory called `git` as well as an executable called `git.exe` in the end. Which means that the bin-wrappers *need* to target the `.exe` files lest they try to execute directories. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bf37ae70a81c39..53dd7f08b2ef6c 100644 --- a/Makefile +++ b/Makefile @@ -2626,7 +2626,7 @@ bin-wrappers/%: wrap-for-bin.sh @mkdir -p bin-wrappers $(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \ -e 's|@@BUILD_DIR@@|$(shell pwd)|' \ - -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%,$(@F))|' < $< > $@ && \ + -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%$(X),$(@F))$(patsubst git%,$(X),$(filter $(@F),$(BINDIR_PROGRAMS_NEED_X)))|' < $< > $@ && \ chmod +x $@ # GNU make supports exporting all variables by "export" without parameters. From aebb751543484e3456265fd557cea3a6e8df3a7d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 30 Nov 2016 12:00:59 +0100 Subject: [PATCH 289/406] t5505,t5516: create .git/branches/ when needed It is a real old anachronism from the Cogito days to have a .git/branches/ directory. And to have tests that ensure that Cogito users can migrate away from using that directory. But so be it, let's continue testing it. Let's make sure, however, that git init does not need to create that directory. This bug was noticed when testing with templates that had been pre-committed, skipping the empty branches/ directory of course because Git does not track empty directories. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5505-remote.sh | 2 ++ t/t5516-fetch-push.sh | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 241e6a319df4ce..e50a69bbdfaed3 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -824,6 +824,7 @@ test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' ' ( cd six && git remote rm origin && + mkdir -p .git/branches && echo "$origin_url" >.git/branches/origin && git remote rename origin origin && test_path_is_missing .git/branches/origin && @@ -838,6 +839,7 @@ test_expect_success 'migrate a remote from named file in $GIT_DIR/branches (2)' ( cd seven && git remote rm origin && + mkdir -p .git/branches && echo "quux#foom" > .git/branches/origin && git remote rename origin origin && test_path_is_missing .git/branches/origin && diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index 539c25aadafdcf..f123f0e3d9d4f1 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -866,6 +866,7 @@ test_expect_success 'fetch with branches' ' mk_empty testrepo && git branch second $the_first_commit && git checkout second && + mkdir -p testrepo/.git/branches && echo ".." > testrepo/.git/branches/branch1 && ( cd testrepo && @@ -879,6 +880,7 @@ test_expect_success 'fetch with branches' ' test_expect_success 'fetch with branches containing #' ' mk_empty testrepo && + mkdir -p testrepo/.git/branches && echo "..#second" > testrepo/.git/branches/branch2 && ( cd testrepo && @@ -893,6 +895,7 @@ test_expect_success 'fetch with branches containing #' ' test_expect_success 'push with branches' ' mk_empty testrepo && git checkout second && + mkdir -p .git/branches && echo "testrepo" > .git/branches/branch1 && git push branch1 && ( @@ -905,6 +908,7 @@ test_expect_success 'push with branches' ' test_expect_success 'push with branches containing #' ' mk_empty testrepo && + mkdir -p .git/branches && echo "testrepo#branch3" > .git/branches/branch2 && git push branch2 && ( From 20f0ce12726d1adcdbeae2e5f379e392755ee873 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 28 Nov 2016 18:17:49 +0100 Subject: [PATCH 290/406] git: avoid calling aliased builtins via their dashed form This is one of the few places where Git violates its own deprecation of the dashed form. It is not necessary, either. As of 595d59e2b53 (git.c: ignore pager.* when launching builtin as dashed external, 2017-08-02), Git wants to ignore the pager.* config setting when expanding aliases. So let's strip out the check_pager_config(<command-name>) call from the copy-edited code. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/git.c b/git.c index c27c38738b2a9d..c1863576735b53 100644 --- a/git.c +++ b/git.c @@ -687,6 +687,31 @@ static int run_argv(int *argcp, const char ***argv) */ if (!done_alias) handle_builtin(*argcp, *argv); + else if (get_builtin(**argv)) { + struct argv_array args = ARGV_ARRAY_INIT; + int i; + + if (get_super_prefix()) + die("%s doesn't support --super-prefix", **argv); + + commit_pager_choice(); + + argv_array_push(&args, "git"); + for (i = 0; i < *argcp; i++) + argv_array_push(&args, (*argv)[i]); + + trace_argv_printf(args.argv, "trace: exec:"); + + /* + * if we fail because the command is not found, it is + * OK to return. Otherwise, we just pass along the status code. + */ + i = run_command_v_opt(args.argv, RUN_SILENT_EXEC_FAILURE | + RUN_CLEAN_ON_EXIT); + if (i >= 0 || errno != ENOENT) + exit(i); + die("could not execute builtin %s", **argv); + } /* .. then try the external ones */ execv_dashed_external(*argv); From b05f76346d041fae199150fcc562c6deba2e2de8 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 6 Jul 2013 02:09:35 +0200 Subject: [PATCH 291/406] Win32: make FILETIME conversion functions public We will use them in the upcoming "FSCache" patches (to accelerate sequential lstat() calls). Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 18 ------------------ compat/mingw.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 52b1a5d9ceaa09..d0fba0f99da08a 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -607,24 +607,6 @@ int mingw_chmod(const char *filename, int mode) return _wchmod(wfilename, mode); } -/* - * The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC. - * Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch. - */ -static inline long long filetime_to_hnsec(const FILETIME *ft) -{ - long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; - /* Windows to Unix Epoch conversion */ - return winTime - 116444736000000000LL; -} - -static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts) -{ - long long hnsec = filetime_to_hnsec(ft); - ts->tv_sec = (time_t)(hnsec / 10000000); - ts->tv_nsec = (hnsec % 10000000) * 100; -} - /** * Verifies that safe_create_leading_directories() would succeed. */ diff --git a/compat/mingw.h b/compat/mingw.h index 90a9e84a604161..57b7633c799d66 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -353,6 +353,17 @@ static inline int getrlimit(int resource, struct rlimit *rlp) return 0; } +/* + * The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC. + * Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch. + */ +static inline long long filetime_to_hnsec(const FILETIME *ft) +{ + long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; + /* Windows to Unix Epoch conversion */ + return winTime - 116444736000000000LL; +} + /* * Use mingw specific stat()/lstat()/fstat() implementations on Windows, * including our own struct stat with 64 bit st_size and nanosecond-precision @@ -369,6 +380,13 @@ struct timespec { #endif #endif +static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts) +{ + long long hnsec = filetime_to_hnsec(ft); + ts->tv_sec = (time_t)(hnsec / 10000000); + ts->tv_nsec = (hnsec % 10000000) * 100; +} + struct mingw_stat { _dev_t st_dev; _ino_t st_ino; From 769bed2819b9e1317c067e77c190f388c75a2426 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 8 Sep 2013 14:17:31 +0200 Subject: [PATCH 292/406] Win32: dirent.c: Move opendir down Move opendir down in preparation for the next patch. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/win32/dirent.c | 68 +++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/compat/win32/dirent.c b/compat/win32/dirent.c index 52420ec7d4dad7..2603a0fa39f45a 100644 --- a/compat/win32/dirent.c +++ b/compat/win32/dirent.c @@ -18,40 +18,6 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata) ent->d_type = DT_REG; } -DIR *opendir(const char *name) -{ - wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ - WIN32_FIND_DATAW fdata; - HANDLE h; - int len; - DIR *dir; - - /* convert name to UTF-16 and check length < MAX_PATH */ - if ((len = xutftowcs_path(pattern, name)) < 0) - return NULL; - - /* append optional '/' and wildcard '*' */ - if (len && !is_dir_sep(pattern[len - 1])) - pattern[len++] = '/'; - pattern[len++] = '*'; - pattern[len] = 0; - - /* open find handle */ - h = FindFirstFileW(pattern, &fdata); - if (h == INVALID_HANDLE_VALUE) { - DWORD err = GetLastError(); - errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); - return NULL; - } - - /* initialize DIR structure and copy first dir entry */ - dir = xmalloc(sizeof(DIR)); - dir->dd_handle = h; - dir->dd_stat = 0; - finddata2dirent(&dir->dd_dir, &fdata); - return dir; -} - struct dirent *readdir(DIR *dir) { if (!dir) { @@ -90,3 +56,37 @@ int closedir(DIR *dir) free(dir); return 0; } + +DIR *opendir(const char *name) +{ + wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ + WIN32_FIND_DATAW fdata; + HANDLE h; + int len; + DIR *dir; + + /* convert name to UTF-16 and check length < MAX_PATH */ + if ((len = xutftowcs_path(pattern, name)) < 0) + return NULL; + + /* append optional '/' and wildcard '*' */ + if (len && !is_dir_sep(pattern[len - 1])) + pattern[len++] = '/'; + pattern[len++] = '*'; + pattern[len] = 0; + + /* open find handle */ + h = FindFirstFileW(pattern, &fdata); + if (h == INVALID_HANDLE_VALUE) { + DWORD err = GetLastError(); + errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); + return NULL; + } + + /* initialize DIR structure and copy first dir entry */ + dir = xmalloc(sizeof(DIR)); + dir->dd_handle = h; + dir->dd_stat = 0; + finddata2dirent(&dir->dd_dir, &fdata); + return dir; +} From dbb89d0f4b888ca8630015319123d9497860e034 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 8 Sep 2013 14:18:40 +0200 Subject: [PATCH 293/406] Win32: Make the dirent implementation pluggable Emulating the POSIX dirent API on Windows via FindFirstFile/FindNextFile is pretty staightforward, however, most of the information provided in the WIN32_FIND_DATA structure is thrown away in the process. A more sophisticated implementation may cache this data, e.g. for later reuse in calls to lstat. Make the dirent implementation pluggable so that it can be switched at runtime, e.g. based on a config option. Define a base DIR structure with pointers to readdir/closedir that match the opendir implementation (i.e. similar to vtable pointers in OOP). Define readdir/closedir so that they call the function pointers in the DIR structure. This allows to choose the opendir implementation on a call-by-call basis. Move the fixed sized dirent.d_name buffer to the dirent-specific DIR structure, as d_name may be implementation specific (e.g. a caching implementation may just set d_name to point into the cache instead of copying the entire file name string). Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/win32/dirent.c | 27 +++++++++++++++++---------- compat/win32/dirent.h | 26 +++++++++++++++++++------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/compat/win32/dirent.c b/compat/win32/dirent.c index 2603a0fa39f45a..6b87042182d950 100644 --- a/compat/win32/dirent.c +++ b/compat/win32/dirent.c @@ -1,15 +1,19 @@ #include "../../git-compat-util.h" -struct DIR { +typedef struct dirent_DIR { + struct DIR base_dir; /* extend base struct DIR */ struct dirent dd_dir; /* includes d_type */ HANDLE dd_handle; /* FindFirstFile handle */ int dd_stat; /* 0-based index */ -}; + char dd_name[MAX_PATH * 3]; /* file name (* 3 for UTF-8 conversion) */ +} dirent_DIR; + +DIR *(*opendir)(const char *dirname) = dirent_opendir; static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata) { - /* convert UTF-16 name to UTF-8 */ - xwcstoutf(ent->d_name, fdata->cFileName, sizeof(ent->d_name)); + /* convert UTF-16 name to UTF-8 (d_name points to dirent_DIR.dd_name) */ + xwcstoutf(ent->d_name, fdata->cFileName, MAX_PATH * 3); /* Set file type, based on WIN32_FIND_DATA */ if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) @@ -18,7 +22,7 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata) ent->d_type = DT_REG; } -struct dirent *readdir(DIR *dir) +static struct dirent *dirent_readdir(dirent_DIR *dir) { if (!dir) { errno = EBADF; /* No set_errno for mingw */ @@ -45,7 +49,7 @@ struct dirent *readdir(DIR *dir) return &dir->dd_dir; } -int closedir(DIR *dir) +static int dirent_closedir(dirent_DIR *dir) { if (!dir) { errno = EBADF; @@ -57,13 +61,13 @@ int closedir(DIR *dir) return 0; } -DIR *opendir(const char *name) +DIR *dirent_opendir(const char *name) { wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ WIN32_FIND_DATAW fdata; HANDLE h; int len; - DIR *dir; + dirent_DIR *dir; /* convert name to UTF-16 and check length < MAX_PATH */ if ((len = xutftowcs_path(pattern, name)) < 0) @@ -84,9 +88,12 @@ DIR *opendir(const char *name) } /* initialize DIR structure and copy first dir entry */ - dir = xmalloc(sizeof(DIR)); + dir = xmalloc(sizeof(dirent_DIR)); + dir->base_dir.preaddir = (struct dirent *(*)(DIR *dir)) dirent_readdir; + dir->base_dir.pclosedir = (int (*)(DIR *dir)) dirent_closedir; + dir->dd_dir.d_name = dir->dd_name; dir->dd_handle = h; dir->dd_stat = 0; finddata2dirent(&dir->dd_dir, &fdata); - return dir; + return (DIR*) dir; } diff --git a/compat/win32/dirent.h b/compat/win32/dirent.h index 058207e4bfed62..6b3ddee51ba926 100644 --- a/compat/win32/dirent.h +++ b/compat/win32/dirent.h @@ -1,20 +1,32 @@ #ifndef DIRENT_H #define DIRENT_H -typedef struct DIR DIR; - #define DT_UNKNOWN 0 #define DT_DIR 1 #define DT_REG 2 #define DT_LNK 3 struct dirent { - unsigned char d_type; /* file type to prevent lstat after readdir */ - char d_name[MAX_PATH * 3]; /* file name (* 3 for UTF-8 conversion) */ + unsigned char d_type; /* file type to prevent lstat after readdir */ + char *d_name; /* file name */ }; -DIR *opendir(const char *dirname); -struct dirent *readdir(DIR *dir); -int closedir(DIR *dir); +/* + * Base DIR structure, contains pointers to readdir/closedir implementations so + * that opendir may choose a concrete implementation on a call-by-call basis. + */ +typedef struct DIR { + struct dirent *(*preaddir)(struct DIR *dir); + int (*pclosedir)(struct DIR *dir); +} DIR; + +/* default dirent implementation */ +extern DIR *dirent_opendir(const char *dirname); + +/* current dirent implementation */ +extern DIR *(*opendir)(const char *dirname); + +#define readdir(dir) (dir->preaddir(dir)) +#define closedir(dir) (dir->pclosedir(dir)) #endif /* DIRENT_H */ From bdf57862d6ef29255680a065e931c0f9639c5f97 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 8 Sep 2013 14:21:30 +0200 Subject: [PATCH 294/406] Win32: make the lstat implementation pluggable Emulating the POSIX lstat API on Windows via GetFileAttributes[Ex] is quite slow. Windows operating system APIs seem to be much better at scanning the status of entire directories than checking single files. A caching implementation may improve performance by bulk-reading entire directories or reusing data obtained via opendir / readdir. Make the lstat implementation pluggable so that it can be switched at runtime, e.g. based on a config option. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 2 ++ compat/mingw.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index d0fba0f99da08a..1c213af3efa3e2 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -746,6 +746,8 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf) return do_lstat(follow, alt_name, buf); } +int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat; + static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) { BY_HANDLE_FILE_INFORMATION fdata; diff --git a/compat/mingw.h b/compat/mingw.h index 57b7633c799d66..416092dd8f8e74 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -419,7 +419,7 @@ int mingw_fstat(int fd, struct stat *buf); #ifdef lstat #undef lstat #endif -#define lstat mingw_lstat +extern int (*lstat)(const char *file_name, struct stat *buf); int mingw_utime(const char *file_name, const struct utimbuf *times); From 8b6a623ccfdb44e6fba3aceec1583f7e3f7bea80 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 8 Sep 2013 14:23:27 +0200 Subject: [PATCH 295/406] add infrastructure for read-only file system level caches Add a macro to mark code sections that only read from the file system, along with a config option and documentation. This facilitates implementation of relatively simple file system level caches without the need to synchronize with the file system. Enable read-only sections for 'git status' and preload_index. Signed-off-by: Karsten Blees <blees@dcon.de> --- Documentation/config.txt | 6 ++++++ builtin/commit.c | 1 + compat/mingw.c | 6 ++++++ compat/mingw.h | 2 ++ git-compat-util.h | 15 +++++++++++++++ preload-index.c | 2 ++ 6 files changed, 32 insertions(+) diff --git a/Documentation/config.txt b/Documentation/config.txt index 189b568b03cc0f..658698c548aba7 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -909,6 +909,12 @@ relatively high IO latencies. When enabled, Git will do the index comparison to the filesystem data in parallel, allowing overlapping IO's. Defaults to true. +core.fscache:: + Enable additional caching of file system data for some operations. ++ +Git for Windows uses this to bulk-read and cache lstat data of entire +directories (instead of doing lstat file by file). + core.unsetenvvars:: EXPERIMENTAL, Windows-only: comma-separated list of environment variables' names that need to be unset before spawning any other diff --git a/builtin/commit.c b/builtin/commit.c index 0d9828e29ebe89..f6be863a0affc0 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1355,6 +1355,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) PATHSPEC_PREFER_FULL, prefix, argv); + enable_fscache(1); read_cache_preload(&s.pathspec); refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL); diff --git a/compat/mingw.c b/compat/mingw.c index 1c213af3efa3e2..a9cff3d7262960 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -213,6 +213,7 @@ enum hide_dotfiles_type { static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; static char *unset_environment_variables; +int core_fscache; int mingw_core_config(const char *var, const char *value, void *cb) { @@ -224,6 +225,11 @@ int mingw_core_config(const char *var, const char *value, void *cb) return 0; } + if (!strcmp(var, "core.fscache")) { + core_fscache = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "core.unsetenvvars")) { free(unset_environment_variables); unset_environment_variables = xstrdup(value); diff --git a/compat/mingw.h b/compat/mingw.h index 416092dd8f8e74..68a2998da98770 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -11,6 +11,8 @@ typedef _sigset_t sigset_t; #undef _POSIX_THREAD_SAFE_FUNCTIONS #endif +extern int core_fscache; + extern int mingw_core_config(const char *var, const char *value, void *cb); #define platform_core_config mingw_core_config diff --git a/git-compat-util.h b/git-compat-util.h index a96d7b8e9f9aa6..1e1db2d465133a 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1238,6 +1238,21 @@ static inline int is_missing_file_error(int errno_) return (errno_ == ENOENT || errno_ == ENOTDIR); } +/* + * Enable/disable a read-only cache for file system data on platforms that + * support it. + * + * Implementing a live-cache is complicated and requires special platform + * support (inotify, ReadDirectoryChangesW...). enable_fscache shall be used + * to mark sections of git code that extensively read from the file system + * without modifying anything. Implementations can use this to cache e.g. stat + * data or even file content without the need to synchronize with the file + * system. + */ +#ifndef enable_fscache +#define enable_fscache(x) /* noop */ +#endif + extern int cmd_main(int, const char **); /* diff --git a/preload-index.c b/preload-index.c index 71cd2437a3b33b..9c22a0705f5715 100644 --- a/preload-index.c +++ b/preload-index.c @@ -93,6 +93,7 @@ static void preload_index(struct index_state *index, offset = 0; work = DIV_ROUND_UP(index->cache_nr, threads); memset(&data, 0, sizeof(data)); + enable_fscache(1); for (i = 0; i < threads; i++) { struct thread_data *p = data+i; p->index = index; @@ -110,6 +111,7 @@ static void preload_index(struct index_state *index, die("unable to join threaded lstat"); } trace_performance_since(start, "preload index"); + enable_fscache(0); } #endif From 999ff3077d0f5013c6af741548ed5553bb10b609 Mon Sep 17 00:00:00 2001 From: Doug Kelly <dougk.ff7@gmail.com> Date: Wed, 8 Jan 2014 20:28:15 -0600 Subject: [PATCH 296/406] Add a test demonstrating a problem with long submodule paths [jes: adjusted test number to avoid conflicts, fixed non-portable use of the 'export' statement, fixed broken && chain] Signed-off-by: Doug Kelly <dougk.ff7@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t7418-submodule-long-path.sh | 101 +++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 t/t7418-submodule-long-path.sh diff --git a/t/t7418-submodule-long-path.sh b/t/t7418-submodule-long-path.sh new file mode 100755 index 00000000000000..9f9d2ea446d480 --- /dev/null +++ b/t/t7418-submodule-long-path.sh @@ -0,0 +1,101 @@ +#!/bin/sh +# +# Copyright (c) 2013 Doug Kelly +# + +test_description='Test submodules with a path near PATH_MAX + +This test verifies that "git submodule" initialization, update and clones work, including with recursive submodules and paths approaching PATH_MAX (260 characters on Windows) +' + +TEST_NO_CREATE_REPO=1 +. ./test-lib.sh + +longpath="" +for (( i=0; i<4; i++ )); do + longpath="0123456789abcdefghijklmnopqrstuvwxyz$longpath" +done +# Pick a substring maximum of 90 characters +# This should be good, since we'll add on a lot for temp directories +longpath=${longpath:0:90}; export longpath + +test_expect_failure 'submodule with a long path' ' + git init --bare remote && + test_create_repo bundle1 && + ( + cd bundle1 && + test_commit "shoot" && + git rev-parse --verify HEAD >../expect + ) && + mkdir home && + ( + cd home && + git clone ../remote test && + cd test && + git submodule add ../bundle1 $longpath && + test_commit "sogood" && + ( + cd $longpath && + git rev-parse --verify HEAD >actual && + test_cmp ../../../expect actual + ) && + git push origin master + ) && + mkdir home2 && + ( + cd home2 && + git clone ../remote test && + cd test && + git checkout master && + git submodule update --init && + ( + cd $longpath && + git rev-parse --verify HEAD >actual && + test_cmp ../../../expect actual + ) + ) +' + +test_expect_failure 'recursive submodule with a long path' ' + git init --bare super && + test_create_repo child && + ( + cd child && + test_commit "shoot" && + git rev-parse --verify HEAD >../expect + ) && + test_create_repo parent && + ( + cd parent && + git submodule add ../child $longpath && + test_commit "aim" + ) && + mkdir home3 && + ( + cd home3 && + git clone ../super test && + cd test && + git submodule add ../parent foo && + git submodule update --init --recursive && + test_commit "sogood" && + ( + cd foo/$longpath && + git rev-parse --verify HEAD >actual && + test_cmp ../../../../expect actual + ) && + git push origin master + ) && + mkdir home4 && + ( + cd home4 && + git clone ../super test --recursive && + ( + cd test/foo/$longpath && + git rev-parse --verify HEAD >actual && + test_cmp ../../../../expect actual + ) + ) +' +unset longpath + +test_done From edccd344c6888fc77f8c6c38c7d29c0b6c7f7da5 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 1 Oct 2013 12:51:54 +0200 Subject: [PATCH 297/406] Win32: add a cache below mingw's lstat and dirent implementations Checking the work tree status is quite slow on Windows, due to slow lstat emulation (git calls lstat once for each file in the index). Windows operating system APIs seem to be much better at scanning the status of entire directories than checking single files. Add an lstat implementation that uses a cache for lstat data. Cache misses read the entire parent directory and add it to the cache. Subsequent lstat calls for the same directory are served directly from the cache. Also implement opendir / readdir / closedir so that they create and use directory listings in the cache. The cache doesn't track file system changes and doesn't plug into any modifying file APIs, so it has to be explicitly enabled for git functions that don't modify the working copy. Note: in an earlier version of this patch, the cache was always active and tracked file system changes via ReadDirectoryChangesW. However, this was much more complex and had negative impact on the performance of modifying git commands such as 'git checkout'. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/win32/fscache.c | 444 +++++++++++++++++++++++++++++++++++++++++ compat/win32/fscache.h | 10 + config.mak.uname | 4 +- git-compat-util.h | 2 + 4 files changed, 458 insertions(+), 2 deletions(-) create mode 100644 compat/win32/fscache.c create mode 100644 compat/win32/fscache.h diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c new file mode 100644 index 00000000000000..0c5490c8f2966b --- /dev/null +++ b/compat/win32/fscache.c @@ -0,0 +1,444 @@ +#include "../../cache.h" +#include "../../hashmap.h" +#include "../win32.h" +#include "fscache.h" + +static int initialized; +static volatile long enabled; +static struct hashmap map; +static CRITICAL_SECTION mutex; + +/* + * An entry in the file system cache. Used for both entire directory listings + * and file entries. + */ +struct fsentry { + struct hashmap_entry ent; + mode_t st_mode; + /* Length of name. */ + unsigned short len; + /* + * Name of the entry. For directory listings: relative path of the + * directory, without trailing '/' (empty for cwd()). For file entries: + * name of the file. Typically points to the end of the structure if + * the fsentry is allocated on the heap (see fsentry_alloc), or to a + * local variable if on the stack (see fsentry_init). + */ + const char *name; + /* Pointer to the directory listing, or NULL for the listing itself. */ + struct fsentry *list; + /* Pointer to the next file entry of the list. */ + struct fsentry *next; + + union { + /* Reference count of the directory listing. */ + volatile long refcnt; + struct { + /* More stat members (only used for file entries). */ + off64_t st_size; + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; + }; + }; +}; + +/* + * Compares the paths of two fsentry structures for equality. + */ +static int fsentry_cmp(void *unused_cmp_data, + const struct fsentry *fse1, const struct fsentry *fse2, + void *unused_keydata) +{ + int res; + if (fse1 == fse2) + return 0; + + /* compare the list parts first */ + if (fse1->list != fse2->list && + (res = fsentry_cmp(NULL, fse1->list ? fse1->list : fse1, + fse2->list ? fse2->list : fse2, NULL))) + return res; + + /* if list parts are equal, compare len and name */ + if (fse1->len != fse2->len) + return fse1->len - fse2->len; + return strnicmp(fse1->name, fse2->name, fse1->len); +} + +/* + * Calculates the hash code of an fsentry structure's path. + */ +static unsigned int fsentry_hash(const struct fsentry *fse) +{ + unsigned int hash = fse->list ? fse->list->ent.hash : 0; + return hash ^ memihash(fse->name, fse->len); +} + +/* + * Initialize an fsentry structure for use by fsentry_hash and fsentry_cmp. + */ +static void fsentry_init(struct fsentry *fse, struct fsentry *list, + const char *name, size_t len) +{ + fse->list = list; + fse->name = name; + fse->len = len; + hashmap_entry_init(fse, fsentry_hash(fse)); +} + +/* + * Allocate an fsentry structure on the heap. + */ +static struct fsentry *fsentry_alloc(struct fsentry *list, const char *name, + size_t len) +{ + /* overallocate fsentry and copy the name to the end */ + struct fsentry *fse = xmalloc(sizeof(struct fsentry) + len + 1); + char *nm = ((char*) fse) + sizeof(struct fsentry); + memcpy(nm, name, len); + nm[len] = 0; + /* init the rest of the structure */ + fsentry_init(fse, list, nm, len); + fse->next = NULL; + fse->refcnt = 1; + return fse; +} + +/* + * Add a reference to an fsentry. + */ +inline static void fsentry_addref(struct fsentry *fse) +{ + if (fse->list) + fse = fse->list; + + InterlockedIncrement(&(fse->refcnt)); +} + +/* + * Release the reference to an fsentry, frees the memory if its the last ref. + */ +static void fsentry_release(struct fsentry *fse) +{ + if (fse->list) + fse = fse->list; + + if (InterlockedDecrement(&(fse->refcnt))) + return; + + while (fse) { + struct fsentry *next = fse->next; + free(fse); + fse = next; + } +} + +/* + * Allocate and initialize an fsentry from a WIN32_FIND_DATA structure. + */ +static struct fsentry *fseentry_create_entry(struct fsentry *list, + const WIN32_FIND_DATAW *fdata) +{ + char buf[MAX_PATH * 3]; + int len; + struct fsentry *fse; + len = xwcstoutf(buf, fdata->cFileName, ARRAY_SIZE(buf)); + + fse = fsentry_alloc(list, buf, len); + + fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes); + fse->st_size = (((off64_t) (fdata->nFileSizeHigh)) << 32) + | fdata->nFileSizeLow; + filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim)); + filetime_to_timespec(&(fdata->ftLastWriteTime), &(fse->st_mtim)); + filetime_to_timespec(&(fdata->ftCreationTime), &(fse->st_ctim)); + + return fse; +} + +/* + * Create an fsentry-based directory listing (similar to opendir / readdir). + * Dir should not contain trailing '/'. Use an empty string for the current + * directory (not "."!). + */ +static struct fsentry *fsentry_create_list(const struct fsentry *dir) +{ + wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ + WIN32_FIND_DATAW fdata; + HANDLE h; + int wlen; + struct fsentry *list, **phead; + DWORD err; + + /* convert name to UTF-16 and check length < MAX_PATH */ + if ((wlen = xutftowcsn(pattern, dir->name, MAX_PATH, dir->len)) < 0) { + if (errno == ERANGE) + errno = ENAMETOOLONG; + return NULL; + } + + /* append optional '/' and wildcard '*' */ + if (wlen) + pattern[wlen++] = '/'; + pattern[wlen++] = '*'; + pattern[wlen] = 0; + + /* open find handle */ + h = FindFirstFileW(pattern, &fdata); + if (h == INVALID_HANDLE_VALUE) { + err = GetLastError(); + errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); + return NULL; + } + + /* allocate object to hold directory listing */ + list = fsentry_alloc(NULL, dir->name, dir->len); + + /* walk directory and build linked list of fsentry structures */ + phead = &list->next; + do { + *phead = fseentry_create_entry(list, &fdata); + phead = &(*phead)->next; + } while (FindNextFileW(h, &fdata)); + + /* remember result of last FindNextFile, then close find handle */ + err = GetLastError(); + FindClose(h); + + /* return the list if we've got all the files */ + if (err == ERROR_NO_MORE_FILES) + return list; + + /* otherwise free the list and return error */ + fsentry_release(list); + errno = err_win_to_posix(err); + return NULL; +} + +/* + * Adds a directory listing to the cache. + */ +static void fscache_add(struct fsentry *fse) +{ + if (fse->list) + fse = fse->list; + + for (; fse; fse = fse->next) + hashmap_add(&map, fse); +} + +/* + * Clears the cache. + */ +static void fscache_clear(void) +{ + hashmap_free(&map, 1); + hashmap_init(&map, (hashmap_cmp_fn)fsentry_cmp, NULL, 0); +} + +/* + * Checks if the cache is enabled for the given path. + */ +static inline int fscache_enabled(const char *path) +{ + return enabled > 0 && !is_absolute_path(path); +} + +/* + * Looks up or creates a cache entry for the specified key. + */ +static struct fsentry *fscache_get(struct fsentry *key) +{ + struct fsentry *fse; + + EnterCriticalSection(&mutex); + /* check if entry is in cache */ + fse = hashmap_get(&map, key, NULL); + if (fse) { + fsentry_addref(fse); + LeaveCriticalSection(&mutex); + return fse; + } + /* if looking for a file, check if directory listing is in cache */ + if (!fse && key->list) { + fse = hashmap_get(&map, key->list, NULL); + if (fse) { + LeaveCriticalSection(&mutex); + /* dir entry without file entry -> file doesn't exist */ + errno = ENOENT; + return NULL; + } + } + + /* create the directory listing (outside mutex!) */ + LeaveCriticalSection(&mutex); + fse = fsentry_create_list(key->list ? key->list : key); + if (!fse) + return NULL; + + EnterCriticalSection(&mutex); + /* add directory listing if it hasn't been added by some other thread */ + if (!hashmap_get(&map, key, NULL)) + fscache_add(fse); + + /* lookup file entry if requested (fse already points to directory) */ + if (key->list) + fse = hashmap_get(&map, key, NULL); + + /* return entry or ENOENT */ + if (fse) + fsentry_addref(fse); + else + errno = ENOENT; + + LeaveCriticalSection(&mutex); + return fse; +} + +/* + * Enables or disables the cache. Note that the cache is read-only, changes to + * the working directory are NOT reflected in the cache while enabled. + */ +int fscache_enable(int enable) +{ + int result; + + if (!initialized) { + /* allow the cache to be disabled entirely */ + if (!core_fscache) + return 0; + + InitializeCriticalSection(&mutex); + hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0); + initialized = 1; + } + + result = enable ? InterlockedIncrement(&enabled) + : InterlockedDecrement(&enabled); + + if (enable && result == 1) { + /* redirect opendir and lstat to the fscache implementations */ + opendir = fscache_opendir; + lstat = fscache_lstat; + } else if (!enable && !result) { + /* reset opendir and lstat to the original implementations */ + opendir = dirent_opendir; + lstat = mingw_lstat; + EnterCriticalSection(&mutex); + fscache_clear(); + LeaveCriticalSection(&mutex); + } + return result; +} + +/* + * Lstat replacement, uses the cache if enabled, otherwise redirects to + * mingw_lstat. + */ +int fscache_lstat(const char *filename, struct stat *st) +{ + int dirlen, base, len; + struct fsentry key[2], *fse; + + if (!fscache_enabled(filename)) + return mingw_lstat(filename, st); + + /* split filename into path + name */ + len = strlen(filename); + if (len && is_dir_sep(filename[len - 1])) + len--; + base = len; + while (base && !is_dir_sep(filename[base - 1])) + base--; + dirlen = base ? base - 1 : 0; + + /* lookup entry for path + name in cache */ + fsentry_init(key, NULL, filename, dirlen); + fsentry_init(key + 1, key, filename + base, len - base); + fse = fscache_get(key + 1); + if (!fse) + return -1; + + /* copy stat data */ + st->st_ino = 0; + st->st_gid = 0; + st->st_uid = 0; + st->st_dev = 0; + st->st_rdev = 0; + st->st_nlink = 1; + st->st_mode = fse->st_mode; + st->st_size = fse->st_size; + st->st_atim = fse->st_atim; + st->st_mtim = fse->st_mtim; + st->st_ctim = fse->st_ctim; + + /* don't forget to release fsentry */ + fsentry_release(fse); + return 0; +} + +typedef struct fscache_DIR { + struct DIR base_dir; /* extend base struct DIR */ + struct fsentry *pfsentry; + struct dirent dirent; +} fscache_DIR; + +/* + * Readdir replacement. + */ +static struct dirent *fscache_readdir(DIR *base_dir) +{ + fscache_DIR *dir = (fscache_DIR*) base_dir; + struct fsentry *next = dir->pfsentry->next; + if (!next) + return NULL; + dir->pfsentry = next; + dir->dirent.d_type = S_ISDIR(next->st_mode) ? DT_DIR : DT_REG; + dir->dirent.d_name = (char*) next->name; + return &(dir->dirent); +} + +/* + * Closedir replacement. + */ +static int fscache_closedir(DIR *base_dir) +{ + fscache_DIR *dir = (fscache_DIR*) base_dir; + fsentry_release(dir->pfsentry); + free(dir); + return 0; +} + +/* + * Opendir replacement, uses a directory listing from the cache if enabled, + * otherwise calls original dirent implementation. + */ +DIR *fscache_opendir(const char *dirname) +{ + struct fsentry key, *list; + fscache_DIR *dir; + int len; + + if (!fscache_enabled(dirname)) + return dirent_opendir(dirname); + + /* prepare name (strip trailing '/', replace '.') */ + len = strlen(dirname); + if ((len == 1 && dirname[0] == '.') || + (len && is_dir_sep(dirname[len - 1]))) + len--; + + /* get directory listing from cache */ + fsentry_init(&key, NULL, dirname, len); + list = fscache_get(&key); + if (!list) + return NULL; + + /* alloc and return DIR structure */ + dir = (fscache_DIR*) xmalloc(sizeof(fscache_DIR)); + dir->base_dir.preaddir = fscache_readdir; + dir->base_dir.pclosedir = fscache_closedir; + dir->pfsentry = list; + return (DIR*) dir; +} diff --git a/compat/win32/fscache.h b/compat/win32/fscache.h new file mode 100644 index 00000000000000..ed518b422d705e --- /dev/null +++ b/compat/win32/fscache.h @@ -0,0 +1,10 @@ +#ifndef FSCACHE_H +#define FSCACHE_H + +int fscache_enable(int enable); +#define enable_fscache(x) fscache_enable(x) + +DIR *fscache_opendir(const char *dir); +int fscache_lstat(const char *file_name, struct stat *buf); + +#endif diff --git a/config.mak.uname b/config.mak.uname index db1f0957a59115..88cac39ef81971 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -424,7 +424,7 @@ ifeq ($(uname_S),Windows) BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE COMPAT_OBJS = compat/msvc.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ - compat/win32/dirent.o + compat/win32/dirent.o compat/win32/fscache.o COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\" BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE # invalidcontinue.obj allows Git's source code to close the same file @@ -590,7 +590,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ - compat/win32/dirent.o + compat/win32/dirent.o compat/win32/fscache.o BASIC_CFLAGS += -DWIN32 -DPROTECT_NTFS_DEFAULT=1 EXTLIBS += -lws2_32 GITLIBS += git.res diff --git a/git-compat-util.h b/git-compat-util.h index 1e1db2d465133a..8f18651a78ffab 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -204,8 +204,10 @@ #if defined(__MINGW32__) /* pull in Windows compatibility stuff */ #include "compat/mingw.h" +#include "compat/win32/fscache.h" #elif defined(_MSC_VER) #include "compat/msvc.h" +#include "compat/win32/fscache.h" #else #include <sys/utsname.h> #include <sys/wait.h> From b72540402831a0515bce48d4865cf9286989eebc Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 28 Jul 2015 21:07:41 +0200 Subject: [PATCH 298/406] Win32: support long paths Windows paths are typically limited to MAX_PATH = 260 characters, even though the underlying NTFS file system supports paths up to 32,767 chars. This limitation is also evident in Windows Explorer, cmd.exe and many other applications (including IDEs). Particularly annoying is that most Windows APIs return bogus error codes if a relative path only barely exceeds MAX_PATH in conjunction with the current directory, e.g. ERROR_PATH_NOT_FOUND / ENOENT instead of the infinitely more helpful ERROR_FILENAME_EXCED_RANGE / ENAMETOOLONG. Many Windows wide char APIs support longer than MAX_PATH paths through the file namespace prefix ('\\?\' or '\\?\UNC\') followed by an absolute path. Notable exceptions include functions dealing with executables and the current directory (CreateProcess, LoadLibrary, Get/SetCurrentDirectory) as well as the entire shell API (ShellExecute, SHGetSpecialFolderPath...). Introduce a handle_long_path function to check the length of a specified path properly (and fail with ENAMETOOLONG), and to optionally expand long paths using the '\\?\' file namespace prefix. Short paths will not be modified, so we don't need to worry about device names (NUL, CON, AUX). Contrary to MSDN docs, the GetFullPathNameW function doesn't seem to be limited to MAX_PATH (at least not on Win7), so we can use it to do the heavy lifting of the conversion (translate '/' to '\', eliminate '.' and '..', and make an absolute path). Add long path error checking to xutftowcs_path for APIs with hard MAX_PATH limit. Add a new MAX_LONG_PATH constant and xutftowcs_long_path function for APIs that support long paths. While improved error checking is always active, long paths support must be explicitly enabled via 'core.longpaths' option. This is to prevent end users to shoot themselves in the foot by checking out files that Windows Explorer, cmd/bash or their favorite IDE cannot handle. Test suite: Test the case is when the full pathname length of a dir is close to 260 (MAX_PATH). Bug report and an original reproducer by Andrey Rogozhnikov: https://github.com/msysgit/git/pull/122#issuecomment-43604199 [jes: adjusted test number to avoid conflicts, added support for chdir(), etc] Thanks-to: Martin W. Kirst <maki@bitkings.de> Thanks-to: Doug Kelly <dougk.ff7@gmail.com> Signed-off-by: Karsten Blees <blees@dcon.de> Original-test-by: Andrey Rogozhnikov <rogozhnikov.andrey@gmail.com> Signed-off-by: Stepan Kasal <kasal@ucw.cz> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Documentation/config.txt | 7 ++ compat/mingw.c | 141 ++++++++++++++++++++++++++------- compat/mingw.h | 75 ++++++++++++++++-- compat/win32/dirent.c | 14 ++-- compat/win32/fscache.c | 17 ++-- t/t2029-checkout-long-paths.sh | 102 ++++++++++++++++++++++++ t/t7418-submodule-long-path.sh | 24 +++--- 7 files changed, 323 insertions(+), 57 deletions(-) create mode 100755 t/t2029-checkout-long-paths.sh diff --git a/Documentation/config.txt b/Documentation/config.txt index 658698c548aba7..e4d5581c3bed89 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -915,6 +915,13 @@ core.fscache:: Git for Windows uses this to bulk-read and cache lstat data of entire directories (instead of doing lstat file by file). +core.longpaths:: + Enable long path (> 260) support for builtin commands in Git for + Windows. This is disabled by default, as long paths are not supported + by Windows Explorer, cmd.exe and the Git for Windows tool chain + (msys, bash, tcl, perl...). Only enable this if you know what you're + doing and are prepared to live with a few quirks. + core.unsetenvvars:: EXPERIMENTAL, Windows-only: comma-separated list of environment variables' names that need to be unset before spawning any other diff --git a/compat/mingw.c b/compat/mingw.c index a9cff3d7262960..393ad5b409cecb 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -214,6 +214,7 @@ enum hide_dotfiles_type { static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; static char *unset_environment_variables; int core_fscache; +int core_long_paths; int mingw_core_config(const char *var, const char *value, void *cb) { @@ -230,6 +231,11 @@ int mingw_core_config(const char *var, const char *value, void *cb) return 0; } + if (!strcmp(var, "core.longpaths")) { + core_long_paths = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "core.unsetenvvars")) { free(unset_environment_variables); unset_environment_variables = xstrdup(value); @@ -267,8 +273,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf) int mingw_unlink(const char *pathname) { int ret, tries = 0; - wchar_t wpathname[MAX_PATH]; - if (xutftowcs_path(wpathname, pathname) < 0) + wchar_t wpathname[MAX_LONG_PATH]; + if (xutftowcs_long_path(wpathname, pathname) < 0) return -1; /* read-only files cannot be removed */ @@ -297,7 +303,7 @@ static int is_dir_empty(const wchar_t *wpath) { WIN32_FIND_DATAW findbuf; HANDLE handle; - wchar_t wbuf[MAX_PATH + 2]; + wchar_t wbuf[MAX_LONG_PATH + 2]; wcscpy(wbuf, wpath); wcscat(wbuf, L"\\*"); handle = FindFirstFileW(wbuf, &findbuf); @@ -318,8 +324,8 @@ static int is_dir_empty(const wchar_t *wpath) int mingw_rmdir(const char *pathname) { int ret, tries = 0; - wchar_t wpathname[MAX_PATH]; - if (xutftowcs_path(wpathname, pathname) < 0) + wchar_t wpathname[MAX_LONG_PATH]; + if (xutftowcs_long_path(wpathname, pathname) < 0) return -1; while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) { @@ -394,9 +400,12 @@ static int set_hidden_flag(const wchar_t *path, int set) int mingw_mkdir(const char *path, int mode) { int ret; - wchar_t wpath[MAX_PATH]; - if (xutftowcs_path(wpath, path) < 0) + wchar_t wpath[MAX_LONG_PATH]; + /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */ + if (xutftowcs_path_ex(wpath, path, MAX_LONG_PATH, -1, 248, + core_long_paths) < 0) return -1; + ret = _wmkdir(wpath); if (!ret && needs_hiding(path)) return set_hidden_flag(wpath, 1); @@ -439,7 +448,7 @@ int mingw_open (const char *filename, int oflags, ...) va_list args; unsigned mode; int fd; - wchar_t wfilename[MAX_PATH]; + wchar_t wfilename[MAX_LONG_PATH]; open_fn_t open_fn; va_start(args, oflags); @@ -454,7 +463,7 @@ int mingw_open (const char *filename, int oflags, ...) else open_fn = _wopen; - if (xutftowcs_path(wfilename, filename) < 0) + if (xutftowcs_long_path(wfilename, filename) < 0) return -1; fd = open_fn(wfilename, oflags, mode); @@ -511,10 +520,10 @@ FILE *mingw_fopen (const char *filename, const char *otype) { int hide = needs_hiding(filename); FILE *file; - wchar_t wfilename[MAX_PATH], wotype[4]; + wchar_t wfilename[MAX_LONG_PATH], wotype[4]; if (filename && !strcmp(filename, "/dev/null")) filename = "nul"; - if (xutftowcs_path(wfilename, filename) < 0 || + if (xutftowcs_long_path(wfilename, filename) < 0 || xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0) return NULL; if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) { @@ -533,10 +542,10 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream) { int hide = needs_hiding(filename); FILE *file; - wchar_t wfilename[MAX_PATH], wotype[4]; + wchar_t wfilename[MAX_LONG_PATH], wotype[4]; if (filename && !strcmp(filename, "/dev/null")) filename = "nul"; - if (xutftowcs_path(wfilename, filename) < 0 || + if (xutftowcs_long_path(wfilename, filename) < 0 || xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0) return NULL; if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) { @@ -590,25 +599,31 @@ ssize_t mingw_write(int fd, const void *buf, size_t len) int mingw_access(const char *filename, int mode) { - wchar_t wfilename[MAX_PATH]; - if (xutftowcs_path(wfilename, filename) < 0) + wchar_t wfilename[MAX_LONG_PATH]; + if (xutftowcs_long_path(wfilename, filename) < 0) return -1; /* X_OK is not supported by the MSVCRT version */ return _waccess(wfilename, mode & ~X_OK); } +/* cached length of current directory for handle_long_path */ +static int current_directory_len = 0; + int mingw_chdir(const char *dirname) { - wchar_t wdirname[MAX_PATH]; - if (xutftowcs_path(wdirname, dirname) < 0) + int result; + wchar_t wdirname[MAX_LONG_PATH]; + if (xutftowcs_long_path(wdirname, dirname) < 0) return -1; - return _wchdir(wdirname); + result = _wchdir(wdirname); + current_directory_len = GetCurrentDirectoryW(0, NULL); + return result; } int mingw_chmod(const char *filename, int mode) { - wchar_t wfilename[MAX_PATH]; - if (xutftowcs_path(wfilename, filename) < 0) + wchar_t wfilename[MAX_LONG_PATH]; + if (xutftowcs_long_path(wfilename, filename) < 0) return -1; return _wchmod(wfilename, mode); } @@ -656,8 +671,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename) static int do_lstat(int follow, const char *file_name, struct stat *buf) { WIN32_FILE_ATTRIBUTE_DATA fdata; - wchar_t wfilename[MAX_PATH]; - if (xutftowcs_path(wfilename, file_name) < 0) + wchar_t wfilename[MAX_LONG_PATH]; + if (xutftowcs_long_path(wfilename, file_name) < 0) return -1; if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) { @@ -828,8 +843,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times) FILETIME mft, aft; int fh, rc; DWORD attrs; - wchar_t wfilename[MAX_PATH]; - if (xutftowcs_path(wfilename, file_name) < 0) + wchar_t wfilename[MAX_LONG_PATH]; + if (xutftowcs_long_path(wfilename, file_name) < 0) return -1; /* must have write permission */ @@ -890,6 +905,7 @@ char *mingw_mktemp(char *template) wchar_t wtemplate[MAX_PATH]; int offset = 0; + /* we need to return the path, thus no long paths here! */ if (xutftowcs_path(wtemplate, template) < 0) return NULL; @@ -1398,6 +1414,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen si.hStdOutput = winansi_get_osfhandle(fhout); si.hStdError = winansi_get_osfhandle(fherr); + /* executables and the current directory don't support long paths */ if (xutftowcs_path(wcmd, cmd) < 0) return -1; if (dir && xutftowcs_path(wdir, dir) < 0) @@ -1966,8 +1983,9 @@ int mingw_rename(const char *pold, const char *pnew) { DWORD attrs, gle; int tries = 0; - wchar_t wpold[MAX_PATH], wpnew[MAX_PATH]; - if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0) + wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH]; + if (xutftowcs_long_path(wpold, pold) < 0 || + xutftowcs_long_path(wpnew, pnew) < 0) return -1; /* @@ -2276,9 +2294,9 @@ int mingw_raise(int sig) int link(const char *oldpath, const char *newpath) { - wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH]; - if (xutftowcs_path(woldpath, oldpath) < 0 || - xutftowcs_path(wnewpath, newpath) < 0) + wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH]; + if (xutftowcs_long_path(woldpath, oldpath) < 0 || + xutftowcs_long_path(wnewpath, newpath) < 0) return -1; if (!CreateHardLinkW(wnewpath, woldpath, NULL)) { @@ -2481,6 +2499,68 @@ static void setup_windows_environment(void) setenv("TERM", "cygwin", 1); } +int handle_long_path(wchar_t *path, int len, int max_path, int expand) +{ + int result; + wchar_t buf[MAX_LONG_PATH]; + + /* + * we don't need special handling if path is relative to the current + * directory, and current directory + path don't exceed the desired + * max_path limit. This should cover > 99 % of cases with minimal + * performance impact (git almost always uses relative paths). + */ + if ((len < 2 || (!is_dir_sep(path[0]) && path[1] != ':')) && + (current_directory_len + len < max_path)) + return len; + + /* + * handle everything else: + * - absolute paths: "C:\dir\file" + * - absolute UNC paths: "\\server\share\dir\file" + * - absolute paths on current drive: "\dir\file" + * - relative paths on other drive: "X:file" + * - prefixed paths: "\\?\...", "\\.\..." + */ + + /* convert to absolute path using GetFullPathNameW */ + result = GetFullPathNameW(path, MAX_LONG_PATH, buf, NULL); + if (!result) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + + /* + * return absolute path if it fits within max_path (even if + * "cwd + path" doesn't due to '..' components) + */ + if (result < max_path) { + wcscpy(path, buf); + return result; + } + + /* error out if we shouldn't expand the path or buf is too small */ + if (!expand || result >= MAX_LONG_PATH - 6) { + errno = ENAMETOOLONG; + return -1; + } + + /* prefix full path with "\\?\" or "\\?\UNC\" */ + if (buf[0] == '\\') { + /* ...unless already prefixed */ + if (buf[1] == '\\' && (buf[2] == '?' || buf[2] == '.')) + return len; + + wcscpy(path, L"\\\\?\\UNC\\"); + wcscpy(path + 8, buf + 2); + return result + 6; + } else { + wcscpy(path, L"\\\\?\\"); + wcscpy(path + 4, buf); + return result + 4; + } +} + #if !defined(_MSC_VER) /* * Disable MSVCRT command line wildcard expansion (__getmainargs called from @@ -2636,6 +2716,9 @@ int wmain(int argc, const wchar_t **wargv) /* initialize Unicode console */ winansi_init(); + /* init length of current directory for handle_long_path */ + current_directory_len = GetCurrentDirectoryW(0, NULL); + /* invoke the real main() using our utf8 version of argv. */ exit_status = main(argc, argv); diff --git a/compat/mingw.h b/compat/mingw.h index 68a2998da98770..4aa84ab783fbdf 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -12,6 +12,7 @@ typedef _sigset_t sigset_t; #endif extern int core_fscache; +extern int core_long_paths; extern int mingw_core_config(const char *var, const char *value, void *cb); #define platform_core_config mingw_core_config @@ -501,6 +502,42 @@ extern char *mingw_query_user_email(void); #include <inttypes.h> #endif +/** + * Max length of long paths (exceeding MAX_PATH). The actual maximum supported + * by NTFS is 32,767 (* sizeof(wchar_t)), but we choose an arbitrary smaller + * value to limit required stack memory. + */ +#define MAX_LONG_PATH 4096 + +/** + * Handles paths that would exceed the MAX_PATH limit of Windows Unicode APIs. + * + * With expand == false, the function checks for over-long paths and fails + * with ENAMETOOLONG. The path parameter is not modified, except if cwd + path + * exceeds max_path, but the resulting absolute path doesn't (e.g. due to + * eliminating '..' components). The path parameter must point to a buffer + * of max_path wide characters. + * + * With expand == true, an over-long path is automatically converted in place + * to an absolute path prefixed with '\\?\', and the new length is returned. + * The path parameter must point to a buffer of MAX_LONG_PATH wide characters. + * + * Parameters: + * path: path to check and / or convert + * len: size of path on input (number of wide chars without \0) + * max_path: max short path length to check (usually MAX_PATH = 260, but just + * 248 for CreateDirectoryW) + * expand: false to only check the length, true to expand the path to a + * '\\?\'-prefixed absolute path + * + * Return: + * length of the resulting path, or -1 on failure + * + * Errors: + * ENAMETOOLONG if path is too long + */ +int handle_long_path(wchar_t *path, int len, int max_path, int expand); + /** * Converts UTF-8 encoded string to UTF-16LE. * @@ -559,18 +596,46 @@ static inline int xutftowcs(wchar_t *wcs, const char *utf, size_t wcslen) } /** - * Simplified file system specific variant of xutftowcsn, assumes output - * buffer size is MAX_PATH wide chars and input string is \0-terminated, - * fails with ENAMETOOLONG if input string is too long. + * Simplified file system specific wrapper of xutftowcsn and handle_long_path. + * Converts ERANGE to ENAMETOOLONG. If expand is true, wcs must be at least + * MAX_LONG_PATH wide chars (see handle_long_path). */ -static inline int xutftowcs_path(wchar_t *wcs, const char *utf) +static inline int xutftowcs_path_ex(wchar_t *wcs, const char *utf, + size_t wcslen, int utflen, int max_path, int expand) { - int result = xutftowcsn(wcs, utf, MAX_PATH, -1); + int result = xutftowcsn(wcs, utf, wcslen, utflen); if (result < 0 && errno == ERANGE) errno = ENAMETOOLONG; + if (result >= 0) + result = handle_long_path(wcs, result, max_path, expand); return result; } +/** + * Simplified file system specific variant of xutftowcsn, assumes output + * buffer size is MAX_PATH wide chars and input string is \0-terminated, + * fails with ENAMETOOLONG if input string is too long. Typically used for + * Windows APIs that don't support long paths, e.g. SetCurrentDirectory, + * LoadLibrary, CreateProcess... + */ +static inline int xutftowcs_path(wchar_t *wcs, const char *utf) +{ + return xutftowcs_path_ex(wcs, utf, MAX_PATH, -1, MAX_PATH, 0); +} + +/** + * Simplified file system specific variant of xutftowcsn for Windows APIs + * that support long paths via '\\?\'-prefix, assumes output buffer size is + * MAX_LONG_PATH wide chars, fails with ENAMETOOLONG if input string is too + * long. The 'core.longpaths' git-config option controls whether the path + * is only checked or expanded to a long path. + */ +static inline int xutftowcs_long_path(wchar_t *wcs, const char *utf) +{ + return xutftowcs_path_ex(wcs, utf, MAX_LONG_PATH, -1, MAX_PATH, + core_long_paths); +} + /** * Converts UTF-16LE encoded string to UTF-8. * diff --git a/compat/win32/dirent.c b/compat/win32/dirent.c index 6b87042182d950..b3bd8d7af77291 100644 --- a/compat/win32/dirent.c +++ b/compat/win32/dirent.c @@ -63,19 +63,23 @@ static int dirent_closedir(dirent_DIR *dir) DIR *dirent_opendir(const char *name) { - wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ + wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */ WIN32_FIND_DATAW fdata; HANDLE h; int len; dirent_DIR *dir; - /* convert name to UTF-16 and check length < MAX_PATH */ - if ((len = xutftowcs_path(pattern, name)) < 0) + /* convert name to UTF-16 and check length */ + if ((len = xutftowcs_path_ex(pattern, name, MAX_LONG_PATH, -1, + MAX_PATH - 2, core_long_paths)) < 0) return NULL; - /* append optional '/' and wildcard '*' */ + /* + * append optional '\' and wildcard '*'. Note: we need to use '\' as + * Windows doesn't translate '/' to '\' for "\\?\"-prefixed paths. + */ if (len && !is_dir_sep(pattern[len - 1])) - pattern[len++] = '/'; + pattern[len++] = '\\'; pattern[len++] = '*'; pattern[len] = 0; diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 70435df680ddea..4ebd15e4265b5b 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -166,23 +166,24 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, */ static struct fsentry *fsentry_create_list(const struct fsentry *dir) { - wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ + wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */ WIN32_FIND_DATAW fdata; HANDLE h; int wlen; struct fsentry *list, **phead; DWORD err; - /* convert name to UTF-16 and check length < MAX_PATH */ - if ((wlen = xutftowcsn(pattern, dir->name, MAX_PATH, dir->len)) < 0) { - if (errno == ERANGE) - errno = ENAMETOOLONG; + /* convert name to UTF-16 and check length */ + if ((wlen = xutftowcs_path_ex(pattern, dir->name, MAX_LONG_PATH, + dir->len, MAX_PATH - 2, core_long_paths)) < 0) return NULL; - } - /* append optional '/' and wildcard '*' */ + /* + * append optional '\' and wildcard '*'. Note: we need to use '\' as + * Windows doesn't translate '/' to '\' for "\\?\"-prefixed paths. + */ if (wlen) - pattern[wlen++] = '/'; + pattern[wlen++] = '\\'; pattern[wlen++] = '*'; pattern[wlen] = 0; diff --git a/t/t2029-checkout-long-paths.sh b/t/t2029-checkout-long-paths.sh new file mode 100755 index 00000000000000..f30f8920ca689c --- /dev/null +++ b/t/t2029-checkout-long-paths.sh @@ -0,0 +1,102 @@ +#!/bin/sh + +test_description='checkout long paths on Windows + +Ensures that Git for Windows can deal with long paths (>260) enabled via core.longpaths' + +. ./test-lib.sh + +if test_have_prereq !MINGW +then + skip_all='skipping MINGW specific long paths test' + test_done +fi + +test_expect_success setup ' + p=longpathxx && # -> 10 + p=$p$p$p$p$p && # -> 50 + p=$p$p$p$p$p && # -> 250 + + path=${p}/longtestfile && # -> 263 (MAX_PATH = 260) + + blob=$(echo foobar | git hash-object -w --stdin) && + + printf "100644 %s 0\t%s\n" "$blob" "$path" | + git update-index --add --index-info && + git commit -m initial -q +' + +test_expect_success 'checkout of long paths without core.longpaths fails' ' + git config core.longpaths false && + test_must_fail git checkout -f 2>error && + grep -q "Filename too long" error && + test ! -d longpa* +' + +test_expect_success 'checkout of long paths with core.longpaths works' ' + git config core.longpaths true && + git checkout -f && + test_path_is_file longpa*/longtestfile +' + +test_expect_success 'update of long paths' ' + echo frotz >>$(ls longpa*/longtestfile) && + echo $path > expect && + git ls-files -m > actual && + test_cmp expect actual && + git add $path && + git commit -m second && + git grep "frotz" HEAD -- $path +' + +test_expect_success cleanup ' + # bash cannot delete the trash dir if it contains a long path + # lets help cleaning up (unless in debug mode) + if test -z "$debug" + then + rm -rf longpa~1 + fi +' + +# check that the template used in the test won't be too long: +abspath="$(pwd)"/testdir +test ${#abspath} -gt 230 || +test_set_prereq SHORTABSPATH + +test_expect_success SHORTABSPATH 'clean up path close to MAX_PATH' ' + p=/123456789abcdef/123456789abcdef/123456789abcdef/123456789abc/ef && + p=y$p$p$p$p && + subdir="x$(echo "$p" | tail -c $((253 - ${#abspath})) - )" && + # Now, $abspath/$subdir has exactly 254 characters, and is inside CWD + p2="$abspath/$subdir" && + test 254 = ${#p2} && + + # Be careful to overcome path limitations of the MSys tools and split + # the $subdir into two parts. ($subdir2 has to contain 16 chars and a + # slash somewhere following; that is why we asked for abspath <= 230 and + # why we placed a slash near the end of the $subdir template.) + subdir2=${subdir#????????????????*/} && + subdir1=testdir/${subdir%/$subdir2} && + mkdir -p "$subdir1" && + i=0 && + # The most important case is when absolute path is 258 characters long, + # and that will be when i == 4. + while test $i -le 7 + do + mkdir -p $subdir2 && + touch $subdir2/one-file && + mv ${subdir2%%/*} "$subdir1/" && + subdir2=z${subdir2} && + i=$(($i+1)) || + exit 1 + done && + + # now check that git is able to clear the tree: + (cd testdir && + git init && + git config core.longpaths yes && + git clean -fdx) && + test ! -d "$subdir1" +' + +test_done diff --git a/t/t7418-submodule-long-path.sh b/t/t7418-submodule-long-path.sh index 9f9d2ea446d480..2ca9794ca5805c 100755 --- a/t/t7418-submodule-long-path.sh +++ b/t/t7418-submodule-long-path.sh @@ -11,15 +11,20 @@ This test verifies that "git submodule" initialization, update and clones work, TEST_NO_CREATE_REPO=1 . ./test-lib.sh -longpath="" -for (( i=0; i<4; i++ )); do - longpath="0123456789abcdefghijklmnopqrstuvwxyz$longpath" -done -# Pick a substring maximum of 90 characters -# This should be good, since we'll add on a lot for temp directories -longpath=${longpath:0:90}; export longpath +# cloning a submodule calls is_git_directory("$path/../.git/modules/$path"), +# which effectively limits the maximum length to PATH_MAX / 2 minus some +# overhead; start with 3 * 36 = 108 chars (test 2 fails if >= 110) +longpath36=0123456789abcdefghijklmnopqrstuvwxyz +longpath180=$longpath36$longpath36$longpath36$longpath36$longpath36 -test_expect_failure 'submodule with a long path' ' +# the git database must fit within PATH_MAX, which limits the submodule name +# to PATH_MAX - len(pwd) - ~90 (= len("/objects//") + 40-byte sha1 + some +# overhead from the test case) +pwd=$(pwd) +pwdlen=$(echo "$pwd" | wc -c) +longpath=$(echo $longpath180 | cut -c 1-$((170-$pwdlen))) + +test_expect_success 'submodule with a long path' ' git init --bare remote && test_create_repo bundle1 && ( @@ -56,7 +61,7 @@ test_expect_failure 'submodule with a long path' ' ) ' -test_expect_failure 'recursive submodule with a long path' ' +test_expect_success 'recursive submodule with a long path' ' git init --bare super && test_create_repo child && ( @@ -96,6 +101,5 @@ test_expect_failure 'recursive submodule with a long path' ' ) ) ' -unset longpath test_done From 5c4dc1fc61b4903eaf4ccb9f02d96280e45f9614 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 24 Jun 2014 13:22:35 +0200 Subject: [PATCH 299/406] fscache: load directories only once If multiple threads access a directory that is not yet in the cache, the directory will be loaded by each thread. Only one of the results is added to the cache, all others are leaked. This wastes performance and memory. On cache miss, add a future object to the cache to indicate that the directory is currently being loaded. Subsequent threads register themselves with the future object and wait. When the first thread has loaded the directory, it replaces the future object with the result and notifies waiting threads. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/win32/fscache.c | 65 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 0c5490c8f2966b..70435df680ddea 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -33,6 +33,8 @@ struct fsentry { union { /* Reference count of the directory listing. */ volatile long refcnt; + /* Handle to wait on the loading thread. */ + HANDLE hwait; struct { /* More stat members (only used for file entries). */ off64_t st_size; @@ -245,16 +247,43 @@ static inline int fscache_enabled(const char *path) return enabled > 0 && !is_absolute_path(path); } +/* + * Looks up a cache entry, waits if its being loaded by another thread. + * The mutex must be owned by the calling thread. + */ +static struct fsentry *fscache_get_wait(struct fsentry *key) +{ + struct fsentry *fse = hashmap_get(&map, key, NULL); + + /* return if its a 'real' entry (future entries have refcnt == 0) */ + if (!fse || fse->list || fse->refcnt) + return fse; + + /* create an event and link our key to the future entry */ + key->hwait = CreateEvent(NULL, TRUE, FALSE, NULL); + key->next = fse->next; + fse->next = key; + + /* wait for the loading thread to signal us */ + LeaveCriticalSection(&mutex); + WaitForSingleObject(key->hwait, INFINITE); + CloseHandle(key->hwait); + EnterCriticalSection(&mutex); + + /* repeat cache lookup */ + return hashmap_get(&map, key, NULL); +} + /* * Looks up or creates a cache entry for the specified key. */ static struct fsentry *fscache_get(struct fsentry *key) { - struct fsentry *fse; + struct fsentry *fse, *future, *waiter; EnterCriticalSection(&mutex); /* check if entry is in cache */ - fse = hashmap_get(&map, key, NULL); + fse = fscache_get_wait(key); if (fse) { fsentry_addref(fse); LeaveCriticalSection(&mutex); @@ -262,7 +291,7 @@ static struct fsentry *fscache_get(struct fsentry *key) } /* if looking for a file, check if directory listing is in cache */ if (!fse && key->list) { - fse = hashmap_get(&map, key->list, NULL); + fse = fscache_get_wait(key->list); if (fse) { LeaveCriticalSection(&mutex); /* dir entry without file entry -> file doesn't exist */ @@ -271,16 +300,34 @@ static struct fsentry *fscache_get(struct fsentry *key) } } + /* add future entry to indicate that we're loading it */ + future = key->list ? key->list : key; + future->next = NULL; + future->refcnt = 0; + hashmap_add(&map, future); + /* create the directory listing (outside mutex!) */ LeaveCriticalSection(&mutex); - fse = fsentry_create_list(key->list ? key->list : key); - if (!fse) + fse = fsentry_create_list(future); + EnterCriticalSection(&mutex); + + /* remove future entry and signal waiting threads */ + hashmap_remove(&map, future, NULL); + waiter = future->next; + while (waiter) { + HANDLE h = waiter->hwait; + waiter = waiter->next; + SetEvent(h); + } + + /* leave on error (errno set by fsentry_create_list) */ + if (!fse) { + LeaveCriticalSection(&mutex); return NULL; + } - EnterCriticalSection(&mutex); - /* add directory listing if it hasn't been added by some other thread */ - if (!hashmap_get(&map, key, NULL)) - fscache_add(fse); + /* add directory listing to the cache */ + fscache_add(fse); /* lookup file entry if requested (fse already points to directory) */ if (key->list) From 31d6274e1660ff04db325cfc165b21b8abce0573 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 5 Jul 2014 00:00:36 +0200 Subject: [PATCH 300/406] Win32: fix 'lstat("dir/")' with long paths Use a suffciently large buffer to strip the trailing slash. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 393ad5b409cecb..75b51f83bdaec4 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -743,7 +743,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) static int do_stat_internal(int follow, const char *file_name, struct stat *buf) { int namelen; - char alt_name[PATH_MAX]; + char alt_name[MAX_LONG_PATH]; if (!do_lstat(follow, file_name, buf)) return 0; @@ -759,7 +759,7 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf) return -1; while (namelen && file_name[namelen-1] == '/') --namelen; - if (!namelen || namelen >= PATH_MAX) + if (!namelen || namelen >= MAX_LONG_PATH) return -1; memcpy(alt_name, file_name, namelen); From f0e01cc48e30c871b38f7b47f61bf79cb0f36140 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth <sschuberth@gmail.com> Date: Wed, 4 Sep 2013 18:18:49 +0200 Subject: [PATCH 301/406] Makefile: Set htmldir to match the default HTML docs location under MSYS Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com> --- config.mak.uname | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.mak.uname b/config.mak.uname index 88cac39ef81971..a13c6e9c898fbd 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -600,7 +600,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) X = .exe SPARSE_FLAGS = -Wno-one-bit-signed-bitfield ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) - htmldir = doc/git/html/ + htmldir = share/doc/git/$(firstword $(subst -, ,$(GIT_VERSION)))/html prefix = INSTALL = /bin/install EXTLIBS += /mingw/lib/libz.a From 1852670c1a87df73b23cbde0b925c9624054be08 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 20 Feb 2015 09:52:07 +0000 Subject: [PATCH 302/406] Help debugging with MSys2 by optionally executing bash with strace MSys2's strace facility is very useful for debugging... With this patch, the bash will be executed through strace if the environment variable GIT_STRACE_COMMANDS is set, which comes in real handy when investigating issues in the test suite. Also support passing a path to a log file via GIT_STRACE_COMMANDS to force Git to call strace.exe with the `-o <path>` argument, i.e. to log into a file rather than print the log directly. That comes in handy when the output would otherwise misinterpreted by a calling process as part of Git's output. Note: the values "1", "yes" or "true" are *not* specifying paths, but tell Git to let strace.exe log directly to the console. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 75b51f83bdaec4..f31301ea305f2f 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1380,6 +1380,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen HANDLE cons; const char *(*quote_arg)(const char *arg) = is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc; + const char *strace_env; do_unset_environment_variables(); @@ -1437,6 +1438,31 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen free(quoted); } + strace_env = getenv("GIT_STRACE_COMMANDS"); + if (strace_env) { + char *p = path_lookup("strace.exe", 1); + if (!p) + return error("strace not found!"); + if (xutftowcs_path(wcmd, p) < 0) { + free(p); + return -1; + } + free(p); + if (!strcmp("1", strace_env) || + !strcasecmp("yes", strace_env) || + !strcasecmp("true", strace_env)) + strbuf_insert(&args, 0, "strace ", 7); + else { + const char *quoted = quote_arg(strace_env); + struct strbuf buf = STRBUF_INIT; + strbuf_addf(&buf, "strace -o %s ", quoted); + if (quoted != strace_env) + free((char *)quoted); + strbuf_insert(&args, 0, buf.buf, buf.len); + strbuf_release(&buf); + } + } + ALLOC_ARRAY(wargs, st_add(st_mult(2, args.len), 1)); xutftowcs(wargs, args.buf, 2 * args.len + 1); strbuf_release(&args); From 5df723312539fba5f97e85d1d81b25cdd1432699 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 16 Feb 2015 14:06:59 +0100 Subject: [PATCH 303/406] Build Python stuff with MSys2 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- config.mak.uname | 1 + 1 file changed, 1 insertion(+) diff --git a/config.mak.uname b/config.mak.uname index a13c6e9c898fbd..6abbbf4e2d0bc1 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -643,6 +643,7 @@ else NO_LIBPCRE1_JIT = UnfortunatelyYes NO_CURL = USE_NED_ALLOCATOR = YesPlease + NO_PYTHON = else COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO NO_CURL = YesPlease From 61148c6aa985f13979bf1f9673851b003187e34a Mon Sep 17 00:00:00 2001 From: Cesar Eduardo Barros <cesarb@cesarb.net> Date: Mon, 9 Mar 2015 08:51:38 +0100 Subject: [PATCH 304/406] mingw: Embed a manifest to trick UAC into Doing The Right Thing On Windows >= Vista, not having an application manifest with a requestedExecutionLevel can cause several kinds of confusing behavior. The first and more obvious behavior is "Installer Detection", where Windows sometimes decides (by looking at things like the file name and even sequences of bytes within the executable) that an executable is an installer and should run elevated (causing the well-known popup dialog to appear). In Git's context, subcommands such as "git patch-id" or "git update-index" fall prey to this behavior. The second and more confusing behavior is "File Virtualization". It means that when files are written without having write permission, it does not fail (as expected), but they are instead redirected to somewhere else. When the files are read, the original contents are returned, though, not the ones that were just written somewhere else. Even more confusing, not all write accesses are redirected; Trying to write to write-protected .exe files, for example, will fail instead of redirecting. In addition to being unwanted behavior, File Virtualization causes dramatic slowdowns in Git (see for instance http://code.google.com/p/msysgit/issues/detail?id=320). There are two ways to prevent those two behaviors: Either you embed an application manifest within all your executables, or you add an external manifest (a file with the same name followed by .manifest) to all your executables. Since Git's builtins are hardlinked (or copied), it is simpler and more robust to embed a manifest. A recent enough MSVC compiler should already embed a working internal manifest, but for MinGW you have to do so by hand. Very lightly tested on Wine, where like on Windows XP it should not make any difference. References: - New UAC Technologies for Windows Vista http://msdn.microsoft.com/en-us/library/bb756960.aspx - Create and Embed an Application Manifest (UAC) http://msdn.microsoft.com/en-us/library/bb756929.aspx [js: simplified the embedding dramatically by reusing Git for Windows' existing Windows resource file, removed the optional (and dubious) processorArchitecture attribute of the manifest's assemblyIdentity section.] Signed-off-by: Cesar Eduardo Barros <cesarb@cesarb.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/win32/git.manifest | 25 +++++++++++++++++++++++++ git.rc | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 compat/win32/git.manifest diff --git a/compat/win32/git.manifest b/compat/win32/git.manifest new file mode 100644 index 00000000000000..771e3cce431598 --- /dev/null +++ b/compat/win32/git.manifest @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> + <assemblyIdentity type="win32" name="Git" version="0.0.0.1" /> + <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> + <security> + <requestedPrivileges> + <requestedExecutionLevel level="asInvoker" uiAccess="false" /> + </requestedPrivileges> + </security> + </trustInfo> + <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> + <application> + <!-- Windows Vista --> + <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> + <!-- Windows 7 --> + <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/> + <!-- Windows 8 --> + <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/> + <!-- Windows 8.1 --> + <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/> + <!-- Windows 10 --> + <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> + </application> + </compatibility> +</assembly> diff --git a/git.rc b/git.rc index 49002e0d541f1a..cc3fdc6cc6cb83 100644 --- a/git.rc +++ b/git.rc @@ -20,3 +20,5 @@ BEGIN VALUE "Translation", 0x409, 1200 END END + +1 RT_MANIFEST "compat/win32/git.manifest" From d7481bfa1d774e88e86c197489ec0ea54b31152b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 2 Nov 2015 08:41:10 +0100 Subject: [PATCH 305/406] mingw: enable stack smashing protector As suggested privately to Brendan Forster by some unnamed person (suggestion for the future: use the public mailing list, or even the public GitHub issue tracker, that is a much better place to offer such suggestions), we should make use of gcc's stack smashing protector that helps detect stack buffer overruns early. Rather than using -fstack-protector, we use -fstack-protector-strong because it strikes a better balance between how much code is affected and the performance impact. In a local test (time git log --grep=is -p), best of 5 timings went from 23.009s to 22.997s (i.e. the performance impact was *well* lost in the noise). This fixes https://github.com/git-for-windows/git/issues/501 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- config.mak.uname | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config.mak.uname b/config.mak.uname index 6abbbf4e2d0bc1..be81bcf47b3c4e 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -631,7 +631,8 @@ else BASIC_LDFLAGS += -Wl,--large-address-aware endif CC = gcc - COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY + COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \ + -fstack-protector-strong EXTLIBS += -lntdll INSTALL = /bin/install NO_R_TO_GCC_LINKER = YesPlease From 6772b5d51c2688344d94561c2cc5aa3034a24fdf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 6 Apr 2015 10:37:04 +0100 Subject: [PATCH 306/406] Avoid illegal filenames when building Documentation on NTFS A '+' is not a valid part of a filename with Windows file systems (it is reserved because the '+' operator meant file concatenation back in the DOS days). Let's just not use it. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Documentation/Makefile | 88 +++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/Documentation/Makefile b/Documentation/Makefile index a42dcfc74599a2..33dd48634af8f0 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -286,9 +286,9 @@ docdep_prereqs = \ cmd-list.made $(cmds_txt) doc.dep : $(docdep_prereqs) $(wildcard *.txt) build-docdep.perl - $(QUIET_GEN)$(RM) $@+ $@ && \ - $(PERL_PATH) ./build-docdep.perl >$@+ $(QUIET_STDERR) && \ - mv $@+ $@ + $(QUIET_GEN)$(RM) $@.new $@ && \ + $(PERL_PATH) ./build-docdep.perl >$@.new $(QUIET_STDERR) && \ + mv $@.new $@ -include doc.dep @@ -324,8 +324,8 @@ mergetools-list.made: ../git-mergetool--lib.sh $(wildcard ../mergetools/*) date >$@ clean: - $(RM) *.xml *.xml+ *.html *.html+ *.1 *.5 *.7 - $(RM) *.texi *.texi+ *.texi++ git.info gitman.info + $(RM) *.xml *.xml.new *.html *.html.new *.1 *.5 *.7 + $(RM) *.texi *.texi.new *.texi.new.new git.info gitman.info $(RM) *.pdf $(RM) howto-index.txt howto/*.html doc.dep $(RM) technical/*.html technical/api-index.txt @@ -334,14 +334,14 @@ clean: $(RM) manpage-base-url.xsl $(MAN_HTML): %.html : %.txt asciidoc.conf - $(QUIET_ASCIIDOC)$(RM) $@+ $@ && \ - $(TXT_TO_HTML) -d manpage -o $@+ $< && \ - mv $@+ $@ + $(QUIET_ASCIIDOC)$(RM) $@.new $@ && \ + $(TXT_TO_HTML) -d manpage -o $@.new $< && \ + mv $@.new $@ $(OBSOLETE_HTML): %.html : %.txto asciidoc.conf - $(QUIET_ASCIIDOC)$(RM) $@+ $@ && \ - $(TXT_TO_HTML) -o $@+ $< && \ - mv $@+ $@ + $(QUIET_ASCIIDOC)$(RM) $@.new $@ && \ + $(TXT_TO_HTML) -o $@.new $< && \ + mv $@.new $@ manpage-base-url.xsl: manpage-base-url.xsl.in sed "s|@@MAN_BASE_URL@@|$(MAN_BASE_URL)|" $< > $@ @@ -351,14 +351,14 @@ manpage-base-url.xsl: manpage-base-url.xsl.in $(XMLTO) -m $(MANPAGE_XSL) $(XMLTO_EXTRA) man $< %.xml : %.txt asciidoc.conf - $(QUIET_ASCIIDOC)$(RM) $@+ $@ && \ - $(TXT_TO_XML) -d manpage -o $@+ $< && \ - mv $@+ $@ + $(QUIET_ASCIIDOC)$(RM) $@.new $@ && \ + $(TXT_TO_XML) -d manpage -o $@.new $< && \ + mv $@.new $@ user-manual.xml: user-manual.txt user-manual.conf - $(QUIET_ASCIIDOC)$(RM) $@+ $@ && \ - $(TXT_TO_XML) -d book -o $@+ $< && \ - mv $@+ $@ + $(QUIET_ASCIIDOC)$(RM) $@.new $@ && \ + $(TXT_TO_XML) -d book -o $@.new $< && \ + mv $@.new $@ technical/api-index.txt: technical/api-index-skel.txt \ technical/api-index.sh $(patsubst %,%.txt,$(API_DOCS)) @@ -375,46 +375,46 @@ XSLT = docbook.xsl XSLTOPTS = --xinclude --stringparam html.stylesheet docbook-xsl.css user-manual.html: user-manual.xml $(XSLT) - $(QUIET_XSLTPROC)$(RM) $@+ $@ && \ - xsltproc $(XSLTOPTS) -o $@+ $(XSLT) $< && \ - mv $@+ $@ + $(QUIET_XSLTPROC)$(RM) $@.new $@ && \ + xsltproc $(XSLTOPTS) -o $@.new $(XSLT) $< && \ + mv $@.new $@ git.info: user-manual.texi $(QUIET_MAKEINFO)$(MAKEINFO) --no-split -o $@ user-manual.texi user-manual.texi: user-manual.xml - $(QUIET_DB2TEXI)$(RM) $@+ $@ && \ - $(DOCBOOK2X_TEXI) user-manual.xml --encoding=UTF-8 --to-stdout >$@++ && \ - $(PERL_PATH) fix-texi.perl <$@++ >$@+ && \ - rm $@++ && \ - mv $@+ $@ + $(QUIET_DB2TEXI)$(RM) $@.new $@ && \ + $(DOCBOOK2X_TEXI) user-manual.xml --encoding=UTF-8 --to-stdout >$@.new.new && \ + $(PERL_PATH) fix-texi.perl <$@.new.new >$@.new && \ + rm $@.new.new && \ + mv $@.new $@ user-manual.pdf: user-manual.xml - $(QUIET_DBLATEX)$(RM) $@+ $@ && \ - $(DBLATEX) -o $@+ $(DBLATEX_COMMON) $< && \ - mv $@+ $@ + $(QUIET_DBLATEX)$(RM) $@.new $@ && \ + $(DBLATEX) -o $@.new $(DBLATEX_COMMON) $< && \ + mv $@.new $@ gitman.texi: $(MAN_XML) cat-texi.perl texi.xsl - $(QUIET_DB2TEXI)$(RM) $@+ $@ && \ - ($(foreach xml,$(sort $(MAN_XML)),xsltproc -o $(xml)+ texi.xsl $(xml) && \ - $(DOCBOOK2X_TEXI) --encoding=UTF-8 --to-stdout $(xml)+ && \ - rm $(xml)+ &&) true) > $@++ && \ - $(PERL_PATH) cat-texi.perl $@ <$@++ >$@+ && \ - rm $@++ && \ - mv $@+ $@ + $(QUIET_DB2TEXI)$(RM) $@.new $@ && \ + ($(foreach xml,$(sort $(MAN_XML)),xsltproc -o $(xml).new texi.xsl $(xml) && \ + $(DOCBOOK2X_TEXI) --encoding=UTF-8 --to-stdout $(xml).new && \ + rm $(xml).new &&) true) > $@.new.new && \ + $(PERL_PATH) cat-texi.perl $@ <$@.new.new >$@.new && \ + rm $@.new.new && \ + mv $@.new $@ gitman.info: gitman.texi $(QUIET_MAKEINFO)$(MAKEINFO) --no-split --no-validate $*.texi $(patsubst %.txt,%.texi,$(MAN_TXT)): %.texi : %.xml - $(QUIET_DB2TEXI)$(RM) $@+ $@ && \ - $(DOCBOOK2X_TEXI) --to-stdout $*.xml >$@+ && \ - mv $@+ $@ + $(QUIET_DB2TEXI)$(RM) $@.new $@ && \ + $(DOCBOOK2X_TEXI) --to-stdout $*.xml >$@.new && \ + mv $@.new $@ howto-index.txt: howto-index.sh $(wildcard howto/*.txt) - $(QUIET_GEN)$(RM) $@+ $@ && \ - '$(SHELL_PATH_SQ)' ./howto-index.sh $(sort $(wildcard howto/*.txt)) >$@+ && \ - mv $@+ $@ + $(QUIET_GEN)$(RM) $@.new $@ && \ + '$(SHELL_PATH_SQ)' ./howto-index.sh $(sort $(wildcard howto/*.txt)) >$@.new && \ + mv $@.new $@ $(patsubst %,%.html,$(ARTICLES)) : %.html : %.txt $(QUIET_ASCIIDOC)$(TXT_TO_HTML) $*.txt @@ -423,10 +423,10 @@ WEBDOC_DEST = /pub/software/scm/git/docs howto/%.html: ASCIIDOC_EXTRA += -a git-relative-html-prefix=../ $(patsubst %.txt,%.html,$(wildcard howto/*.txt)): %.html : %.txt - $(QUIET_ASCIIDOC)$(RM) $@+ $@ && \ + $(QUIET_ASCIIDOC)$(RM) $@.new $@ && \ sed -e '1,/^$$/d' $< | \ - $(TXT_TO_HTML) - >$@+ && \ - mv $@+ $@ + $(TXT_TO_HTML) - >$@.new && \ + mv $@.new $@ install-webdoc : html '$(SHELL_PATH_SQ)' ./install-webdoc.sh $(WEBDOC_DEST) From 87f5e00238ebe4a28afcc0304cec8a04153b1f13 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Thu, 9 Apr 2015 16:19:56 +0100 Subject: [PATCH 307/406] gettext: always use UTF-8 on native Windows Git on native Windows exclusively uses UTF-8 for console output (both with mintty and native console windows). Gettext uses setlocale() to determine the output encoding for translated text, however, MSVCRT's setlocale() doesn't support UTF-8. As a result, translated text is encoded in system encoding (GetAPC()), and non-ASCII chars are mangled in console output. Use gettext's bind_textdomain_codeset() to force the encoding to UTF-8 on native Windows. In this developers' setup, HAVE_LIBCHARSET_H is apparently defined, but we *really* want to override the locale_charset() here. Signed-off-by: Karsten Blees <blees@dcon.de> --- gettext.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gettext.c b/gettext.c index 7272771c8e445d..9c16a8f7ff6639 100644 --- a/gettext.c +++ b/gettext.c @@ -11,7 +11,9 @@ #ifndef NO_GETTEXT # include <locale.h> # include <libintl.h> -# ifdef HAVE_LIBCHARSET_H +# ifdef GIT_WINDOWS_NATIVE +# define locale_charset() "UTF-8" +# elif defined HAVE_LIBCHARSET_H # include <libcharset.h> # else # include <langinfo.h> From 3446bfdfb0ce6a37be56c14d0972266393a1585c Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Mon, 6 Apr 2015 21:37:18 +0200 Subject: [PATCH 308/406] mingw: initialize HOME on startup HOME initialization was historically duplicated in many different places, including /etc/profile, launch scripts such as git-bash.vbs and gitk.cmd, and (although slightly broken) in the git-wrapper. Even unrelated projects such as GitExtensions and TortoiseGit need to implement the same logic to be able to call git directly. Initialize HOME in git's own startup code so that we can eventually retire all the duplicate initialization code. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index f31301ea305f2f..45c9a9f09fc587 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2523,6 +2523,30 @@ static void setup_windows_environment(void) /* simulate TERM to enable auto-color (see color.c) */ if (!getenv("TERM")) setenv("TERM", "cygwin", 1); + + /* calculate HOME if not set */ + if (!getenv("HOME")) { + /* + * try $HOMEDRIVE$HOMEPATH - the home share may be a network + * location, thus also check if the path exists (i.e. is not + * disconnected) + */ + if ((tmp = getenv("HOMEDRIVE"))) { + struct strbuf buf = STRBUF_INIT; + strbuf_addstr(&buf, tmp); + if ((tmp = getenv("HOMEPATH"))) { + strbuf_addstr(&buf, tmp); + if (is_directory(buf.buf)) + setenv("HOME", buf.buf, 1); + else + tmp = NULL; /* use $USERPROFILE */ + } + strbuf_release(&buf); + } + /* use $USERPROFILE if the home share is not available */ + if (!tmp && (tmp = getenv("USERPROFILE"))) + setenv("HOME", tmp, 1); + } } int handle_long_path(wchar_t *path, int len, int max_path, int expand) From 4621a08c443eac0b3cfb592f77ad4346a64f231e Mon Sep 17 00:00:00 2001 From: nalla <nalla@hamal.uberspace.de> Date: Thu, 16 Apr 2015 11:45:05 +0100 Subject: [PATCH 309/406] mingw: explicitly `fflush` stdout For performance reasons `stdout` is not unbuffered by default. That leads to problems if after printing to `stdout` a read on `stdin` is performed. For that reason interactive commands like `git clean -i` do not function properly anymore if the `stdout` is not flushed by `fflush(stdout)` before trying to read from `stdin`. In the case of `git clean -i` all reads on `stdin` were preceded by a `fflush(stdout)` call. Signed-off-by: nalla <nalla@hamal.uberspace.de> --- builtin/clean.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builtin/clean.c b/builtin/clean.c index 8d9a7dc2064707..7c18e39b1cd91a 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -576,6 +576,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff) clean_get_color(CLEAN_COLOR_RESET)); } + fflush(stdout); if (strbuf_getline_lf(&choice, stdin) != EOF) { strbuf_trim(&choice); } else { @@ -658,6 +659,7 @@ static int filter_by_patterns_cmd(void) clean_print_color(CLEAN_COLOR_PROMPT); printf(_("Input ignore patterns>> ")); clean_print_color(CLEAN_COLOR_RESET); + fflush(stdout); if (strbuf_getline_lf(&confirm, stdin) != EOF) strbuf_trim(&confirm); else @@ -756,6 +758,7 @@ static int ask_each_cmd(void) qname = quote_path_relative(item->string, NULL, &buf); /* TRANSLATORS: Make sure to keep [y/N] as is */ printf(_("Remove %s [y/N]? "), qname); + fflush(stdout); if (strbuf_getline_lf(&confirm, stdin) != EOF) { strbuf_trim(&confirm); } else { From 0d61c2524ed8b42470c143e9fe10b87bf86fa4c4 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Thu, 19 Mar 2015 16:33:44 +0100 Subject: [PATCH 310/406] mingw: Support `git_terminal_prompt` with more terminals The `git_terminal_prompt()` function expects the terminal window to be attached to a Win32 Console. However, this is not the case with terminal windows other than `cmd.exe`'s, e.g. with MSys2's own `mintty`. Non-cmd terminals such as `mintty` still have to have a Win32 Console to be proper console programs, but have to hide the Win32 Console to be able to provide more flexibility (such as being resizeable not only vertically but also horizontally). By writing to that Win32 Console, `git_terminal_prompt()` manages only to send the prompt to nowhere and to wait for input from a Console to which the user has no access. This commit introduces a function specifically to support `mintty` -- or other terminals that are compatible with MSys2's `/dev/tty` emulation. We use the `TERM` environment variable as an indicator for that: if the value starts with "xterm" (such as `mintty`'s "xterm_256color"), we prefer to let `xterm_prompt()` handle the user interaction. The most prominent user of `git_terminal_prompt()` is certainly `git-remote-https.exe`. It is an interesting use case because both `stdin` and `stdout` are redirected when Git calls said executable, yet it still wants to access the terminal. When running inside a `mintty`, the terminal is not accessible to the `git-remote-https.exe` program, though, because it is a MinGW program and the `mintty` terminal is not backed by a Win32 console. To solve that problem, we simply call out to the shell -- which is an *MSys2* program and can therefore access `/dev/tty`. Helped-by: nalla <nalla@hamal.uberspace.de> Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/terminal.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/compat/terminal.c b/compat/terminal.c index fa13ee672db33e..069f4061ed5773 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -1,7 +1,12 @@ +#ifndef NO_INTTYPES_H +#include <inttypes.h> +#endif #include "git-compat-util.h" +#include "run-command.h" #include "compat/terminal.h" #include "sigchain.h" #include "strbuf.h" +#include "cache.h" #if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE) @@ -91,6 +96,54 @@ static int disable_echo(void) return 0; } +static char *shell_prompt(const char *prompt, int echo) +{ + const char *read_input[] = { + /* Note: call 'bash' explicitly, as 'read -s' is bash-specific */ + "bash", "-c", echo ? + "cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" : + "cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty", + NULL + }; + struct child_process child = CHILD_PROCESS_INIT; + static struct strbuf buffer = STRBUF_INIT; + int prompt_len = strlen(prompt), len = -1, code; + + child.argv = read_input; + child.in = -1; + child.out = -1; + + if (start_command(&child)) + return NULL; + + if (write_in_full(child.in, prompt, prompt_len) != prompt_len) { + error("could not write to prompt script"); + close(child.in); + goto ret; + } + close(child.in); + + strbuf_reset(&buffer); + len = strbuf_read(&buffer, child.out, 1024); + if (len < 0) { + error("could not read from prompt script"); + goto ret; + } + + strbuf_strip_suffix(&buffer, "\n"); + strbuf_strip_suffix(&buffer, "\r"); + +ret: + close(child.out); + code = finish_command(&child); + if (code) { + error("failed to execute prompt script (exit code %d)", code); + return NULL; + } + + return len < 0 ? NULL : buffer.buf; +} + #endif #ifndef FORCE_TEXT @@ -102,6 +155,12 @@ char *git_terminal_prompt(const char *prompt, int echo) static struct strbuf buf = STRBUF_INIT; int r; FILE *input_fh, *output_fh; +#ifdef GIT_WINDOWS_NATIVE + const char *term = getenv("TERM"); + + if (term && starts_with(term, "xterm")) + return shell_prompt(prompt, echo); +#endif input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT); if (!input_fh) From a37a3b015548c30f02f87382efa0c0a138cd5d6f Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 9 May 2015 02:11:48 +0200 Subject: [PATCH 311/406] compat/terminal.c: only use the Windows console if bash 'read -r' fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Accessing the Windows console through the special CONIN$ / CONOUT$ devices doesn't work properly for non-ASCII usernames an passwords. It also doesn't work for terminal emulators that hide the native console window (such as mintty), and 'TERM=xterm*' is not necessarily a reliable indicator for such terminals. The new shell_prompt() function, on the other hand, works fine for both MSys1 and MSys2, in native console windows as well as mintty, and properly supports Unicode. It just needs bash on the path (for 'read -s', which is bash-specific). On Windows, try to use the shell to read from the terminal. If that fails with ENOENT (i.e. bash was not found), use CONIN/OUT as fallback. Note: To test this, create a UTF-8 credential file with non-ASCII chars, e.g. in git-bash: 'echo url=http://täst.com > cred.txt'. Then in git-cmd, 'git credential fill <cred.txt' works (shell version), while calling git without the git-wrapper (i.e. 'mingw64\bin\git credential fill <cred.txt') mangles non-ASCII chars in both console output and input. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/terminal.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 069f4061ed5773..d9d3945afa391f 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -112,6 +112,7 @@ static char *shell_prompt(const char *prompt, int echo) child.argv = read_input; child.in = -1; child.out = -1; + child.silent_exec_failure = 1; if (start_command(&child)) return NULL; @@ -155,11 +156,14 @@ char *git_terminal_prompt(const char *prompt, int echo) static struct strbuf buf = STRBUF_INIT; int r; FILE *input_fh, *output_fh; + #ifdef GIT_WINDOWS_NATIVE - const char *term = getenv("TERM"); - if (term && starts_with(term, "xterm")) - return shell_prompt(prompt, echo); + /* try shell_prompt first, fall back to CONIN/OUT if bash is missing */ + char *result = shell_prompt(prompt, echo); + if (result || errno != ENOENT) + return result; + #endif input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT); From d4c92145296c33323d72bc977c4068a597836edf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 10 Jan 2017 23:14:20 +0100 Subject: [PATCH 312/406] winansi: simplify loading the GetCurrentConsoleFontEx() function We introduced helper macros to simplify loading functions dynamically. Might just as well use them. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/winansi.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/compat/winansi.c b/compat/winansi.c index 11cd9b82cc8909..efc0abcdac43e1 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -7,6 +7,7 @@ #include <wingdi.h> #include <winreg.h> #include "win32.h" +#include "win32/lazyload.h" static int fd_is_interactive[3] = { 0, 0, 0 }; #define FD_CONSOLE 0x1 @@ -41,26 +42,21 @@ typedef struct _CONSOLE_FONT_INFOEX { #endif #endif -typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL, - PCONSOLE_FONT_INFOEX); - static void warn_if_raster_font(void) { DWORD fontFamily = 0; - PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx; + DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx, + HANDLE, BOOL, PCONSOLE_FONT_INFOEX); /* don't bother if output was ascii only */ if (!non_ascii_used) return; /* GetCurrentConsoleFontEx is available since Vista */ - pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress( - GetModuleHandle("kernel32.dll"), - "GetCurrentConsoleFontEx"); - if (pGetCurrentConsoleFontEx) { + if (INIT_PROC_ADDR(GetCurrentConsoleFontEx)) { CONSOLE_FONT_INFOEX cfi; cfi.cbSize = sizeof(cfi); - if (pGetCurrentConsoleFontEx(console, 0, &cfi)) + if (GetCurrentConsoleFontEx(console, 0, &cfi)) fontFamily = cfi.FontFamily; } else { /* pre-Vista: check default console font in registry */ From 4e859153644a9753d3ead0964f088f2388967532 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 6 Sep 2016 09:50:33 +0200 Subject: [PATCH 313/406] Unbreak interactive GPG prompt upon signing With the recent update in efee955 (gpg-interface: check gpg signature creation status, 2016-06-17), we ask GPG to send all status updates to stderr, and then catch the stderr in an strbuf. But GPG might fail, and send error messages to stderr. And we simply do not show them to the user. Even worse: this swallows any interactive prompt for a passphrase. And detaches stderr from the tty so that the passphrase cannot be read. So while the first problem could be fixed (by printing the captured stderr upon error), the second problem cannot be easily fixed, and presents a major regression. So let's just revert commit efee9553a4f97b2ecd8f49be19606dd4cf7d9c28. This fixes https://github.com/git-for-windows/git/issues/871 Cc: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- gpg-interface.c | 8 ++------ t/t7004-tag.sh | 13 ------------- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/gpg-interface.c b/gpg-interface.c index db17d65f8ac977..2558e66bc18a8d 100644 --- a/gpg-interface.c +++ b/gpg-interface.c @@ -228,11 +228,9 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig struct child_process gpg = CHILD_PROCESS_INIT; int ret; size_t i, j, bottom; - struct strbuf gpg_status = STRBUF_INIT; argv_array_pushl(&gpg.args, use_format->program, - "--status-fd=2", "-bsau", signing_key, NULL); @@ -244,12 +242,10 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig */ sigchain_push(SIGPIPE, SIG_IGN); ret = pipe_command(&gpg, buffer->buf, buffer->len, - signature, 1024, &gpg_status, 0); + signature, 1024, NULL, 0); sigchain_pop(SIGPIPE); - ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED "); - strbuf_release(&gpg_status); - if (ret) + if (ret || signature->len == bottom) return error(_("gpg failed to sign the data")); /* Strip CR from the line endings, in case we are on Windows. */ diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 0b01862c23aa18..a05df0d7b63f50 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -1345,12 +1345,6 @@ test_expect_success GPG \ 'test_config user.signingkey BobTheMouse && test_must_fail git tag -s -m tail tag-gpg-failure' -# try to produce invalid signature -test_expect_success GPG \ - 'git tag -s fails if gpg is misconfigured (bad signature format)' \ - 'test_config gpg.program echo && - test_must_fail git tag -s -m tail tag-gpg-failure' - # try to sign with bad user.signingkey test_expect_success GPGSM \ 'git tag -s fails if gpgsm is misconfigured (bad key)' \ @@ -1358,13 +1352,6 @@ test_expect_success GPGSM \ test_config gpg.format x509 && test_must_fail git tag -s -m tail tag-gpg-failure' -# try to produce invalid signature -test_expect_success GPGSM \ - 'git tag -s fails if gpgsm is misconfigured (bad signature format)' \ - 'test_config gpg.x509.program echo && - test_config gpg.format x509 && - test_must_fail git tag -s -m tail tag-gpg-failure' - # try to verify without gpg: rm -rf gpghome From 6909c8ef6fe7f9afc21f7b296b7872a77bbf6f5b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 21 Feb 2017 13:28:58 +0100 Subject: [PATCH 314/406] mingw: ensure valid CTYPE A change between versions 2.4.1 and 2.6.0 of the MSYS2 runtime modified how Cygwin's runtime (and hence Git for Windows' MSYS2 runtime derivative) handles locales: d16a56306d (Consolidate wctomb/mbtowc calls for POSIX-1.2008, 2016-07-20). An unintended side-effect is that "cold-calling" into the POSIX emulation will start with a locale based on the current code page, something that Git for Windows is very ill-prepared for, as it expects to be able to pass a command-line containing non-ASCII characters to the shell without having those characters munged. One symptom of this behavior: when `git clone` or `git fetch` shell out to call `git-upload-pack` with a path that contains non-ASCII characters, the shell tried to interpret the entire command-line (including command-line parameters) as executable path, which obviously must fail. This fixes https://github.com/git-for-windows/git/issues/1036 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 45c9a9f09fc587..73d9754631b46b 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2547,6 +2547,9 @@ static void setup_windows_environment(void) if (!tmp && (tmp = getenv("USERPROFILE"))) setenv("HOME", tmp, 1); } + + if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG")) + setenv("LC_CTYPE", "C", 1); } int handle_long_path(wchar_t *path, int len, int max_path, int expand) From 5f02e5ddd8e37ed2961850079c83af2a7486bf11 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 21 Feb 2017 20:34:38 +0100 Subject: [PATCH 315/406] mingw: make is_hidden tests in t0001/t5611 more robust We should not actually expect the first `attrib.exe` in the PATH to be the one we are looking for. Or that it is in the PATH, for that matter. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t0001-init.sh | 2 +- t/t5611-clone-config.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index be69a10d73a80a..3893c2efbb3484 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -400,7 +400,7 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' ' # Tests for the hidden file attribute on windows is_hidden () { # Use the output of `attrib`, ignore the absolute path - case "$(attrib "$1")" in *H*?:*) return 0;; esac + case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac return 1 } diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh index 39329eb7a8a64b..50e2d5fa9405f3 100755 --- a/t/t5611-clone-config.sh +++ b/t/t5611-clone-config.sh @@ -48,7 +48,7 @@ test_expect_success 'clone -c config is available during clone' ' # Tests for the hidden file attribute on windows is_hidden () { # Use the output of `attrib`, ignore the absolute path - case "$(attrib "$1")" in *H*?:*) return 0;; esac + case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac return 1 } From 9846746ef0ae144d038dabf274c9e1e606272070 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth <sschuberth@gmail.com> Date: Thu, 29 Aug 2013 13:09:27 +0200 Subject: [PATCH 316/406] MinGW: Use MakeMaker to build the Perl libraries This way the libraries get properly installed into the "site_perl" directory and we just have to move them out of the "mingw" directory. Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com> --- config.mak.uname | 1 - 1 file changed, 1 deletion(-) diff --git a/config.mak.uname b/config.mak.uname index be81bcf47b3c4e..d315df0e23dbc1 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -571,7 +571,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_STRTOUMAX = YesPlease NO_MKDTEMP = YesPlease NO_SVN_TESTS = YesPlease - NO_PERL_MAKEMAKER = YesPlease RUNTIME_PREFIX = YesPlease HAVE_WPGMPTR = YesWeDo NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease From 789292c1817c3acff0732228cb39d6d34ee2ce06 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 20 Feb 2015 14:21:04 +0000 Subject: [PATCH 317/406] Tests: optionally skip redirecting stdin/stdout/stderr There is a really useful debugging technique developed by Sverre Rabbelier that inserts "bash &&" somewhere in the test scripts, letting the developer interact at given points with the current state. Another debugging technique, used a lot by this here coder, is to run certain executables via gdb by guarding a "gdb -args" call in bin-wrappers/git. Both techniques were disabled by 781f76b1(test-lib: redirect stdin of tests). Let's reinstate the ability to run an interactive shell by making the redirection optional: setting the TEST_NO_REDIRECT environment variable will skip the redirection. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 0831f29803c23a..f20737f713a1af 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -716,7 +716,12 @@ test_eval_ () { # be _inside_ the block to avoid polluting the "set -x" output # - test_eval_inner_ "$@" </dev/null >&3 2>&4 + if test -n "$TEST_NO_REDIRECT" + then + test_eval_inner_ "$@" + else + test_eval_inner_ "$@" </dev/null >&3 2>&4 + fi { test_eval_ret_=$? if want_trace From 9beeebc5992f1afe70d163e88cf6c5b5a2d81b9e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 23 Feb 2015 15:55:47 +0000 Subject: [PATCH 318/406] Skip t9020 with MSys2 POSIX-to-Windows path mangling would make it fail. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t9020-remote-svn.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/t/t9020-remote-svn.sh b/t/t9020-remote-svn.sh index 6fca08e5e35bd3..76d9be2e1d2512 100755 --- a/t/t9020-remote-svn.sh +++ b/t/t9020-remote-svn.sh @@ -12,6 +12,12 @@ then test_done fi +if test_have_prereq MINGW +then + skip_all='skipping remote-svn tests for lack of POSIX' + test_done +fi + # Override svnrdump with our simulator PATH="$HOME:$PATH" export PATH PYTHON_PATH GIT_BUILD_DIR From 4970503698ffb74f717a20d248006b3561485059 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 11 Jan 2017 21:08:15 +0100 Subject: [PATCH 319/406] t9116: work around hard-to-debug hangs As of a couple of weeks ago, t9116 hangs sometimes -- but not always! -- when being run in the Git for Windows SDK. The issue seems to be related to redirection via a pipe, but it is really hard to diagnose, what with git.exe (a non-MSYS2 program) calling a Perl script (which is executed by an MSYS2 Perl), piping into another MSYS2 program. As hunting time is scarce these days, simply work around this for now and leave the real diagnosis and resolution for later. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t9116-git-svn-log.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/t/t9116-git-svn-log.sh b/t/t9116-git-svn-log.sh index 45773ee560dab1..0a9f1ef366db07 100755 --- a/t/t9116-git-svn-log.sh +++ b/t/t9116-git-svn-log.sh @@ -43,14 +43,18 @@ test_expect_success 'setup repository and import' ' test_expect_success 'run log' " git reset --hard origin/a && - git svn log -r2 origin/trunk | grep ^r2 && - git svn log -r4 origin/trunk | grep ^r4 && - git svn log -r3 | grep ^r3 + git svn log -r2 origin/trunk >out && + grep ^r2 out && + git svn log -r4 origin/trunk >out && + grep ^r4 out && + git svn log -r3 >out && + grep ^r3 out " test_expect_success 'run log against a from trunk' " git reset --hard origin/trunk && - git svn log -r3 origin/a | grep ^r3 + git svn log -r3 origin/a >out && + grep ^r3 out " printf 'r1 \nr2 \nr4 \n' > expected-range-r1-r2-r4 From 2fd5f31aa8bf05618c840243ea6b1ae6d2c9f680 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 11 Jan 2017 21:08:15 +0100 Subject: [PATCH 320/406] t9001: work around hard-to-debug hangs Just like the workaround we added for t9116, t9001.83 hangs sometimes -- but not always! -- when being run in the Git for Windows SDK. The issue seems to be related to redirection via a pipe, but it is really hard to diagnose, what with git.exe (a non-MSYS2 program) calling a Perl script (which is executed by an MSYS2 Perl), piping into another MSYS2 program. As hunting time is scarce these days, simply work around this for now and leave the real diagnosis and resolution for later. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t9001-send-email.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh index 1ef1a19003db5c..836e58923ddb95 100755 --- a/t/t9001-send-email.sh +++ b/t/t9001-send-email.sh @@ -1165,8 +1165,8 @@ test_expect_success $PREREQ 'in-reply-to but no threading' ' --to=nobody@example.com \ --in-reply-to="<in-reply-id@example.com>" \ --no-thread \ - $patches | - grep "In-Reply-To: <in-reply-id@example.com>" + $patches >out && + grep "In-Reply-To: <in-reply-id@example.com>" out ' test_expect_success $PREREQ 'no in-reply-to and no threading' ' From 64b01dd6bea778057c13865edc5869f7f7331ff7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 5 Oct 2017 11:48:16 +0200 Subject: [PATCH 321/406] diff: munmap() file contents before running external diff When running an external diff from, say, a diff tool, it is safe to assume that we want to write the files in question. On Windows, that means that there cannot be any other process holding an open handle to said files. So let's make sure that `git diff` itself is not holding any open handle to the files in question. This fixes https://github.com/git-for-windows/git/issues/1315 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- diff.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/diff.c b/diff.c index 145cfbae5929c6..83e7a60d9bca7c 100644 --- a/diff.c +++ b/diff.c @@ -4092,6 +4092,10 @@ static void run_external_diff(const char *pgm, argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); + if (one && one->should_munmap) + diff_free_filespec_data(one); + if (two && two->should_munmap) + diff_free_filespec_data(two); if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv)) die(_("external diff died, stopping at %s"), name); From 46061dbd06d1bad26569c42f494e69ff09fa0364 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Mon, 11 May 2015 19:54:23 +0200 Subject: [PATCH 322/406] strbuf_readlink: don't call readlink twice if hint is the exact link size strbuf_readlink() calls readlink() twice if the hint argument specifies the exact size of the link target (e.g. by passing stat.st_size as returned by lstat()). This is necessary because 'readlink(..., hint) == hint' could mean that the buffer was too small. Use hint + 1 as buffer size to prevent this. Signed-off-by: Karsten Blees <blees@dcon.de> --- strbuf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strbuf.c b/strbuf.c index 64041c3c249b15..fd0b9ddf260678 100644 --- a/strbuf.c +++ b/strbuf.c @@ -471,12 +471,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) while (hint < STRBUF_MAXLINK) { ssize_t len; - strbuf_grow(sb, hint); - len = readlink(path, sb->buf, hint); + strbuf_grow(sb, hint + 1); + len = readlink(path, sb->buf, hint + 1); if (len < 0) { if (errno != ERANGE) break; - } else if (len < hint) { + } else if (len <= hint) { strbuf_setlen(sb, len); return 0; } From 860c7b9b52b76c748fd427d5ea998077199c5ca9 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Mon, 11 May 2015 22:15:40 +0200 Subject: [PATCH 323/406] strbuf_readlink: support link targets that exceed PATH_MAX strbuf_readlink() refuses to read link targets that exceed PATH_MAX (even if a sufficient size was specified by the caller). As some platforms support longer paths, remove this restriction (similar to strbuf_getcwd()). Signed-off-by: Karsten Blees <blees@dcon.de> --- strbuf.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/strbuf.c b/strbuf.c index fd0b9ddf260678..4c52c77b6ee0da 100644 --- a/strbuf.c +++ b/strbuf.c @@ -459,8 +459,6 @@ ssize_t strbuf_write(struct strbuf *sb, FILE *f) } -#define STRBUF_MAXLINK (2*PATH_MAX) - int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) { size_t oldalloc = sb->alloc; @@ -468,7 +466,7 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) if (hint < 32) hint = 32; - while (hint < STRBUF_MAXLINK) { + for (;;) { ssize_t len; strbuf_grow(sb, hint + 1); From f7e61532ec43526f15ca7a9adff8cf9798d13c07 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Mon, 11 May 2015 19:58:14 +0200 Subject: [PATCH 324/406] lockfile.c: use is_dir_sep() instead of hardcoded '/' checks Signed-off-by: Karsten Blees <blees@dcon.de> --- lockfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lockfile.c b/lockfile.c index 8e8ab4f29f3eaf..3704a603f63b44 100644 --- a/lockfile.c +++ b/lockfile.c @@ -17,14 +17,14 @@ static void trim_last_path_component(struct strbuf *path) int i = path->len; /* back up past trailing slashes, if any */ - while (i && path->buf[i - 1] == '/') + while (i && is_dir_sep(path->buf[i - 1])) i--; /* * then go backwards until a slash, or the beginning of the * string */ - while (i && path->buf[i - 1] != '/') + while (i && !is_dir_sep(path->buf[i - 1])) i--; strbuf_setlen(path, i); From 4222d9d281c0e9ca6d9705c30c9a3fa4694257fd Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 12 May 2015 11:09:01 +0200 Subject: [PATCH 325/406] Win32: don't call GetFileAttributes twice in mingw_lstat() GetFileAttributes cannot handle paths with trailing dir separator. The current [l]stat implementation calls GetFileAttributes twice if the path has trailing slashes (first with the original path passed to [l]stat, and and a second time with a path copy with trailing '/' removed). With Unicode conversion, we get the length of the path for free and also have a (wide char) buffer that can be modified. Remove trailing directory separators before calling the Win32 API. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 48 ++++++++++++------------------------------------ 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 73d9754631b46b..6cb7cd3b097dd5 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -672,9 +672,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) { WIN32_FILE_ATTRIBUTE_DATA fdata; wchar_t wfilename[MAX_LONG_PATH]; - if (xutftowcs_long_path(wfilename, file_name) < 0) + int wlen = xutftowcs_long_path(wfilename, file_name); + if (wlen < 0) return -1; + /* strip trailing '/', or GetFileAttributes will fail */ + while (wlen && is_dir_sep(wfilename[wlen - 1])) + wfilename[--wlen] = 0; + if (!wlen) { + errno = ENOENT; + return -1; + } + if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) { buf->st_ino = 0; buf->st_gid = 0; @@ -734,39 +743,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) return -1; } -/* We provide our own lstat/fstat functions, since the provided - * lstat/fstat functions are so slow. These stat functions are - * tailored for Git's usage (read: fast), and are not meant to be - * complete. Note that Git stat()s are redirected to mingw_lstat() - * too, since Windows doesn't really handle symlinks that well. - */ -static int do_stat_internal(int follow, const char *file_name, struct stat *buf) -{ - int namelen; - char alt_name[MAX_LONG_PATH]; - - if (!do_lstat(follow, file_name, buf)) - return 0; - - /* if file_name ended in a '/', Windows returned ENOENT; - * try again without trailing slashes - */ - if (errno != ENOENT) - return -1; - - namelen = strlen(file_name); - if (namelen && file_name[namelen-1] != '/') - return -1; - while (namelen && file_name[namelen-1] == '/') - --namelen; - if (!namelen || namelen >= MAX_LONG_PATH) - return -1; - - memcpy(alt_name, file_name, namelen); - alt_name[namelen] = 0; - return do_lstat(follow, alt_name, buf); -} - int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat; static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) @@ -794,11 +770,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) int mingw_lstat(const char *file_name, struct stat *buf) { - return do_stat_internal(0, file_name, buf); + return do_lstat(0, file_name, buf); } int mingw_stat(const char *file_name, struct stat *buf) { - return do_stat_internal(1, file_name, buf); + return do_lstat(1, file_name, buf); } int mingw_fstat(int fd, struct stat *buf) From 3fd4b72e7f18b6ee5c76e52b3b6e92cef373daf7 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 16 May 2015 01:18:14 +0200 Subject: [PATCH 326/406] Win32: implement stat() with symlink support With respect to symlinks, the current stat() implementation is almost the same as lstat(): except for the file type (st_mode & S_IFMT), it returns information about the link rather than the target. Implement stat by opening the file with as little permissions as possible and calling GetFileInformationByHandle on it. This way, all link resoltion is handled by the Windows file system layer. If symlinks are disabled, use lstat() as before, but fail with ELOOP if a symlink would have to be resolved. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 6cb7cd3b097dd5..ba5965b5737e94 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -772,9 +772,26 @@ int mingw_lstat(const char *file_name, struct stat *buf) { return do_lstat(0, file_name, buf); } + int mingw_stat(const char *file_name, struct stat *buf) { - return do_lstat(1, file_name, buf); + wchar_t wfile_name[MAX_LONG_PATH]; + HANDLE hnd; + int result; + + /* open the file and let Windows resolve the links */ + if (xutftowcs_long_path(wfile_name, file_name) < 0) + return -1; + hnd = CreateFileW(wfile_name, 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (hnd == INVALID_HANDLE_VALUE) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + result = get_file_info_by_handle(hnd, buf); + CloseHandle(hnd); + return result; } int mingw_fstat(int fd, struct stat *buf) From de22e32a7473d2395727a58467d86c8c24558cdf Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 12 May 2015 00:58:39 +0200 Subject: [PATCH 327/406] Win32: remove separate do_lstat() function With the new mingw_stat() implementation, do_lstat() is only called from mingw_lstat() (with follow == 0). Remove the extra function and the old mingw_stat()-specific (follow == 1) logic. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index ba5965b5737e94..884e4dead93765 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -661,14 +661,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename) return 1; } -/* We keep the do_lstat code in a separate function to avoid recursion. - * When a path ends with a slash, the stat will fail with ENOENT. In - * this case, we strip the trailing slashes and stat again. - * - * If follow is true then act like stat() and report on the link - * target. Otherwise report on the link itself. - */ -static int do_lstat(int follow, const char *file_name, struct stat *buf) +int mingw_lstat(const char *file_name, struct stat *buf) { WIN32_FILE_ATTRIBUTE_DATA fdata; wchar_t wfilename[MAX_LONG_PATH]; @@ -702,13 +695,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) if (handle != INVALID_HANDLE_VALUE) { if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && (findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { - if (follow) { - char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; - buf->st_size = readlink(file_name, buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE); - } else { - buf->st_mode = S_IFLNK; - } - buf->st_mode |= S_IREAD; + buf->st_mode = S_IFLNK | S_IREAD; if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) buf->st_mode |= S_IWRITE; } @@ -768,11 +755,6 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) return 0; } -int mingw_lstat(const char *file_name, struct stat *buf) -{ - return do_lstat(0, file_name, buf); -} - int mingw_stat(const char *file_name, struct stat *buf) { wchar_t wfile_name[MAX_LONG_PATH]; From 5cc3bb6e7a641ae90cd8aed03a7e1befcde79014 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 00:17:56 +0200 Subject: [PATCH 328/406] Win32: let mingw_lstat() error early upon problems with reparse points When obtaining lstat information for reparse points, we need to call FindFirstFile() in addition to GetFileInformationEx() to obtain the type of the reparse point (symlink, mount point etc.). However, currently there is no error handling whatsoever if FindFirstFile() fails. Call FindFirstFile() before modifying the stat *buf output parameter and error out if the call fails. Note: The FindFirstFile() return value includes all the data that we get from GetFileAttributesEx(), so we could replace GetFileAttributesEx() with FindFirstFile(). We don't do that because GetFileAttributesEx() is about twice as fast for single files. I.e. we only pay the extra cost of calling FindFirstFile() in the rare case that we encounter a reparse point. Note: The indentation of the remaining reparse point code will be fixed in the next patch. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 884e4dead93765..5952583c8f10e3 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -664,6 +664,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename) int mingw_lstat(const char *file_name, struct stat *buf) { WIN32_FILE_ATTRIBUTE_DATA fdata; + WIN32_FIND_DATAW findbuf = { 0 }; wchar_t wfilename[MAX_LONG_PATH]; int wlen = xutftowcs_long_path(wfilename, file_name); if (wlen < 0) @@ -678,6 +679,13 @@ int mingw_lstat(const char *file_name, struct stat *buf) } if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) { + /* for reparse points, use FindFirstFile to get the reparse tag */ + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { + HANDLE handle = FindFirstFileW(wfilename, &findbuf); + if (handle == INVALID_HANDLE_VALUE) + goto error; + FindClose(handle); + } buf->st_ino = 0; buf->st_gid = 0; buf->st_uid = 0; @@ -690,20 +698,16 @@ int mingw_lstat(const char *file_name, struct stat *buf) filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim)); if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { - WIN32_FIND_DATAW findbuf; - HANDLE handle = FindFirstFileW(wfilename, &findbuf); - if (handle != INVALID_HANDLE_VALUE) { if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && (findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { buf->st_mode = S_IFLNK | S_IREAD; if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) buf->st_mode |= S_IWRITE; } - FindClose(handle); - } } return 0; } +error: switch (GetLastError()) { case ERROR_ACCESS_DENIED: case ERROR_SHARING_VIOLATION: From 94b14a2ac9bb54fbe35764482347aac7a81ce1e4 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 10 Jan 2017 23:21:56 +0100 Subject: [PATCH 329/406] Win32: teach fscache and dirent about symlinks Move S_IFLNK detection to file_attr_to_st_mode() and reuse it in fscache. Implement DT_LNK detection in dirent.c and the fscache readdir version. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 13 +++---------- compat/win32.h | 6 ++++-- compat/win32/dirent.c | 5 ++++- compat/win32/fscache.c | 6 ++++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 5952583c8f10e3..3c816036e9d1e9 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -690,21 +690,14 @@ int mingw_lstat(const char *file_name, struct stat *buf) buf->st_gid = 0; buf->st_uid = 0; buf->st_nlink = 1; - buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); + buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, + findbuf.dwReserved0); buf->st_size = fdata.nFileSizeLow | (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim)); - if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { - if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && - (findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { - buf->st_mode = S_IFLNK | S_IREAD; - if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) - buf->st_mode |= S_IWRITE; - } - } return 0; } error: @@ -749,7 +742,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) buf->st_gid = 0; buf->st_uid = 0; buf->st_nlink = 1; - buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); + buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0); buf->st_size = fdata.nFileSizeLow | (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ diff --git a/compat/win32.h b/compat/win32.h index a97e880757b6f1..671bcc81f93351 100644 --- a/compat/win32.h +++ b/compat/win32.h @@ -6,10 +6,12 @@ #include <windows.h> #endif -static inline int file_attr_to_st_mode (DWORD attr) +static inline int file_attr_to_st_mode (DWORD attr, DWORD tag) { int fMode = S_IREAD; - if (attr & FILE_ATTRIBUTE_DIRECTORY) + if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK) + fMode |= S_IFLNK; + else if (attr & FILE_ATTRIBUTE_DIRECTORY) fMode |= S_IFDIR; else fMode |= S_IFREG; diff --git a/compat/win32/dirent.c b/compat/win32/dirent.c index b3bd8d7af77291..8c654d722bb0df 100644 --- a/compat/win32/dirent.c +++ b/compat/win32/dirent.c @@ -16,7 +16,10 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata) xwcstoutf(ent->d_name, fdata->cFileName, MAX_PATH * 3); /* Set file type, based on WIN32_FIND_DATA */ - if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + if ((fdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) + && fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK) + ent->d_type = DT_LNK; + else if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ent->d_type = DT_DIR; else ent->d_type = DT_REG; diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 4ebd15e4265b5b..60aeeb1293bd5f 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -149,7 +149,8 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, fse = fsentry_alloc(list, buf, len); - fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes); + fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes, + fdata->dwReserved0); fse->st_size = (((off64_t) (fdata->nFileSizeHigh)) << 32) | fdata->nFileSizeLow; filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim)); @@ -442,7 +443,8 @@ static struct dirent *fscache_readdir(DIR *base_dir) if (!next) return NULL; dir->pfsentry = next; - dir->dirent.d_type = S_ISDIR(next->st_mode) ? DT_DIR : DT_REG; + dir->dirent.d_type = S_ISREG(next->st_mode) ? DT_REG : + S_ISDIR(next->st_mode) ? DT_DIR : DT_LNK; dir->dirent.d_name = (char*) next->name; return &(dir->dirent); } From 64dbeda62276a8720d3bb3cc18f1eefde411e665 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 16 May 2015 01:11:37 +0200 Subject: [PATCH 330/406] Win32: lstat(): return adequate stat.st_size for symlinks Git typically doesn't trust the stat.st_size member of symlinks (e.g. see strbuf_readlink()). However, some functions take shortcuts if st_size is 0 (e.g. diff_populate_filespec()). In mingw_lstat() and fscache_lstat(), make sure to return an adequate size. The extra overhead of opening and reading the reparse point to calculate the exact size is not necessary, as git doesn't rely on the value anyway. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 4 ++-- compat/win32/fscache.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 3c816036e9d1e9..2b6a06d7309903 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -692,8 +692,8 @@ int mingw_lstat(const char *file_name, struct stat *buf) buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, findbuf.dwReserved0); - buf->st_size = fdata.nFileSizeLow | - (((off_t)fdata.nFileSizeHigh)<<32); + buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH : + fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 60aeeb1293bd5f..345d7b226b8ad1 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -151,8 +151,8 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes, fdata->dwReserved0); - fse->st_size = (((off64_t) (fdata->nFileSizeHigh)) << 32) - | fdata->nFileSizeLow; + fse->st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH : + fdata->nFileSizeLow | (((off_t) fdata->nFileSizeHigh) << 32); filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim)); filetime_to_timespec(&(fdata->ftLastWriteTime), &(fse->st_mtim)); filetime_to_timespec(&(fdata->ftCreationTime), &(fse->st_ctim)); From 23827082ccb019104bb1233b8db9a9c123a2a3f3 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 19 May 2015 21:48:55 +0200 Subject: [PATCH 331/406] Win32: factor out retry logic The retry pattern is duplicated in three places. It also seems to be too hard to use: mingw_unlink() and mingw_rmdir() duplicate the code to retry, and both of them do so incompletely. They also do not restore errno if the user answers 'no'. Introduce a retry_ask_yes_no() helper function that handles retry with small delay, asking the user, and restoring errno. mingw_unlink: include _wchmod in the retry loop (which may fail if the file is locked exclusively). mingw_rmdir: include special error handling in the retry loop. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 98 ++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 55 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 2b6a06d7309903..b3b82d85e9aa25 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -10,8 +10,6 @@ #define HCAST(type, handle) ((type)(intptr_t)handle) -static const int delay[] = { 0, 1, 10, 20, 40 }; - int err_win_to_posix(DWORD winerr) { int error = ENOSYS; @@ -174,15 +172,12 @@ static int read_yes_no_answer(void) return -1; } -static int ask_yes_no_if_possible(const char *format, ...) +static int ask_yes_no_if_possible(const char *format, va_list args) { char question[4096]; const char *retry_hook[] = { NULL, NULL, NULL }; - va_list args; - va_start(args, format); vsnprintf(question, sizeof(question), format, args); - va_end(args); if ((retry_hook[0] = mingw_getenv("GIT_ASK_YESNO"))) { retry_hook[1] = question; @@ -204,6 +199,31 @@ static int ask_yes_no_if_possible(const char *format, ...) } } +static int retry_ask_yes_no(int *tries, const char *format, ...) +{ + static const int delay[] = { 0, 1, 10, 20, 40 }; + va_list args; + int result, saved_errno = errno; + + if ((*tries) < ARRAY_SIZE(delay)) { + /* + * We assume that some other process had the file open at the wrong + * moment and retry. In order to give the other process a higher + * chance to complete its operation, we give up our time slice now. + * If we have to retry again, we do sleep a bit. + */ + Sleep(delay[*tries]); + (*tries)++; + return 1; + } + + va_start(args, format); + result = ask_yes_no_if_possible(format, args); + va_end(args); + errno = saved_errno; + return result; +} + /* Windows only */ enum hide_dotfiles_type { HIDE_DOTFILES_FALSE = 0, @@ -272,31 +292,21 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf) int mingw_unlink(const char *pathname) { - int ret, tries = 0; + int tries = 0; wchar_t wpathname[MAX_LONG_PATH]; if (xutftowcs_long_path(wpathname, pathname) < 0) return -1; - /* read-only files cannot be removed */ - _wchmod(wpathname, 0666); - while ((ret = _wunlink(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) { + do { + /* read-only files cannot be removed */ + _wchmod(wpathname, 0666); + if (!_wunlink(wpathname)) + return 0; if (!is_file_in_use_error(GetLastError())) break; - /* - * We assume that some other process had the source or - * destination file open at the wrong moment and retry. - * In order to give the other process a higher chance to - * complete its operation, we give up our time slice now. - * If we have to retry again, we do sleep a bit. - */ - Sleep(delay[tries]); - tries++; - } - while (ret == -1 && is_file_in_use_error(GetLastError()) && - ask_yes_no_if_possible("Unlink of file '%s' failed. " - "Should I try again?", pathname)) - ret = _wunlink(wpathname); - return ret; + } while (retry_ask_yes_no(&tries, "Unlink of file '%s' failed. " + "Should I try again?", pathname)); + return -1; } static int is_dir_empty(const wchar_t *wpath) @@ -323,12 +333,14 @@ static int is_dir_empty(const wchar_t *wpath) int mingw_rmdir(const char *pathname) { - int ret, tries = 0; + int tries = 0; wchar_t wpathname[MAX_LONG_PATH]; if (xutftowcs_long_path(wpathname, pathname) < 0) return -1; - while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) { + do { + if (!_wrmdir(wpathname)) + return 0; if (!is_file_in_use_error(GetLastError())) errno = err_win_to_posix(GetLastError()); if (errno != EACCES) @@ -337,21 +349,9 @@ int mingw_rmdir(const char *pathname) errno = ENOTEMPTY; break; } - /* - * We assume that some other process had the source or - * destination file open at the wrong moment and retry. - * In order to give the other process a higher chance to - * complete its operation, we give up our time slice now. - * If we have to retry again, we do sleep a bit. - */ - Sleep(delay[tries]); - tries++; - } - while (ret == -1 && errno == EACCES && is_file_in_use_error(GetLastError()) && - ask_yes_no_if_possible("Deletion of directory '%s' failed. " - "Should I try again?", pathname)) - ret = _wrmdir(wpathname); - return ret; + } while (retry_ask_yes_no(&tries, "Deletion of directory '%s' failed. " + "Should I try again?", pathname)); + return -1; } static inline int needs_hiding(const char *path) @@ -2019,20 +2019,8 @@ int mingw_rename(const char *pold, const char *pnew) SetFileAttributesW(wpnew, attrs); } } - if (tries < ARRAY_SIZE(delay) && gle == ERROR_ACCESS_DENIED) { - /* - * We assume that some other process had the source or - * destination file open at the wrong moment and retry. - * In order to give the other process a higher chance to - * complete its operation, we give up our time slice now. - * If we have to retry again, we do sleep a bit. - */ - Sleep(delay[tries]); - tries++; - goto repeat; - } if (gle == ERROR_ACCESS_DENIED && - ask_yes_no_if_possible("Rename from '%s' to '%s' failed. " + retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. " "Should I try again?", pold, pnew)) goto repeat; From 31507de1f7dddf51e6c6a918b115623f42f65f48 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 01:55:05 +0200 Subject: [PATCH 332/406] Win32: change default of 'core.symlinks' to false Symlinks on Windows don't work the same way as on Unix systems. E.g. there are different types of symlinks for directories and files, creating symlinks requires administrative privileges etc. By default, disable symlink support on Windows. I.e. users explicitly have to enable it with 'git config [--system|--global] core.symlinks true'. The test suite ignores system / global config files. Allow testing *with* symlink support by checking if native symlinks are enabled in MSys2 (via 'MSYS=winsymlinks:nativestrict'). Reminder: This would need to be changed if / when we find a way to run the test suite in a non-MSys-based shell (e.g. dash). Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index b3b82d85e9aa25..9a95d21bae971f 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2508,6 +2508,15 @@ static void setup_windows_environment(void) setenv("HOME", tmp, 1); } + /* + * Change 'core.symlinks' default to false, unless native symlinks are + * enabled in MSys2 (via 'MSYS=winsymlinks:nativestrict'). Thus we can + * run the test suite (which doesn't obey config files) with or without + * symlink support. + */ + if (!(tmp = getenv("MSYS")) || !strstr(tmp, "winsymlinks:nativestrict")) + has_symlinks = 0; + if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG")) setenv("LC_CTYPE", "C", 1); } From 3d023834434e5bef2e126aafcdd75113d3406120 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sat, 16 May 2015 00:32:03 +0200 Subject: [PATCH 333/406] Win32: add symlink-specific error codes Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 9a95d21bae971f..b086d1542a9062 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -72,6 +72,7 @@ int err_win_to_posix(DWORD winerr) case ERROR_INVALID_PARAMETER: error = EINVAL; break; case ERROR_INVALID_PASSWORD: error = EPERM; break; case ERROR_INVALID_PRIMARY_GROUP: error = EINVAL; break; + case ERROR_INVALID_REPARSE_DATA: error = EINVAL; break; case ERROR_INVALID_SIGNAL_NUMBER: error = EINVAL; break; case ERROR_INVALID_TARGET_HANDLE: error = EIO; break; case ERROR_INVALID_WORKSTATION: error = EACCES; break; @@ -86,6 +87,7 @@ int err_win_to_posix(DWORD winerr) case ERROR_NEGATIVE_SEEK: error = ESPIPE; break; case ERROR_NOACCESS: error = EFAULT; break; case ERROR_NONE_MAPPED: error = EINVAL; break; + case ERROR_NOT_A_REPARSE_POINT: error = EINVAL; break; case ERROR_NOT_ENOUGH_MEMORY: error = ENOMEM; break; case ERROR_NOT_READY: error = EAGAIN; break; case ERROR_NOT_SAME_DEVICE: error = EXDEV; break; @@ -106,6 +108,9 @@ int err_win_to_posix(DWORD winerr) case ERROR_PIPE_NOT_CONNECTED: error = EPIPE; break; case ERROR_PRIVILEGE_NOT_HELD: error = EACCES; break; case ERROR_READ_FAULT: error = EIO; break; + case ERROR_REPARSE_ATTRIBUTE_CONFLICT: error = EINVAL; break; + case ERROR_REPARSE_TAG_INVALID: error = EINVAL; break; + case ERROR_REPARSE_TAG_MISMATCH: error = EINVAL; break; case ERROR_SEEK: error = EIO; break; case ERROR_SEEK_ON_DEVICE: error = ESPIPE; break; case ERROR_SHARING_BUFFER_EXCEEDED: error = ENFILE; break; From c391e8f948b26a57e9589a00db909595a2bdbc30 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 01:06:10 +0200 Subject: [PATCH 334/406] Win32: mingw_unlink: support symlinks to directories _wunlink() / DeleteFileW() refuses to delete symlinks to directories. If _wunlink() fails with ERROR_ACCESS_DENIED, try _wrmdir() as well. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index b086d1542a9062..01847f7c1bcf05 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -309,6 +309,13 @@ int mingw_unlink(const char *pathname) return 0; if (!is_file_in_use_error(GetLastError())) break; + /* + * _wunlink() / DeleteFileW() for directory symlinks fails with + * ERROR_ACCESS_DENIED (EACCES), so try _wrmdir() as well. This is the + * same error we get if a file is in use (already checked above). + */ + if (!_wrmdir(wpathname)) + return 0; } while (retry_ask_yes_no(&tries, "Unlink of file '%s' failed. " "Should I try again?", pathname)); return -1; From 714ced67279e785a3d1bad2828d506ba922e323d Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Tue, 19 May 2015 22:42:48 +0200 Subject: [PATCH 335/406] Win32: mingw_rename: support renaming symlinks MSVCRT's _wrename() cannot rename symlinks over existing files: it returns success without doing anything. Newer MSVCR*.dll versions probably do not have this problem: according to CRT sources, they just call MoveFileEx() with the MOVEFILE_COPY_ALLOWED flag. Get rid of _wrename() and call MoveFileEx() with proper error handling. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 01847f7c1bcf05..17116658d5d00d 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1991,27 +1991,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz) #undef rename int mingw_rename(const char *pold, const char *pnew) { - DWORD attrs, gle; + DWORD attrs = INVALID_FILE_ATTRIBUTES, gle; int tries = 0; wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH]; if (xutftowcs_long_path(wpold, pold) < 0 || xutftowcs_long_path(wpnew, pnew) < 0) return -1; - /* - * Try native rename() first to get errno right. - * It is based on MoveFile(), which cannot overwrite existing files. - */ - if (!_wrename(wpold, wpnew)) - return 0; - if (errno != EEXIST) - return -1; repeat: - if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING)) + if (MoveFileExW(wpold, wpnew, + MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) return 0; - /* TODO: translate more errors */ gle = GetLastError(); - if (gle == ERROR_ACCESS_DENIED && + + /* revert file attributes on failure */ + if (attrs != INVALID_FILE_ATTRIBUTES) + SetFileAttributesW(wpnew, attrs); + + if (!is_file_in_use_error(gle)) { + errno = err_win_to_posix(gle); + return -1; + } + + if (attrs == INVALID_FILE_ATTRIBUTES && (attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) { if (attrs & FILE_ATTRIBUTE_DIRECTORY) { DWORD attrsold = GetFileAttributesW(wpold); @@ -2023,16 +2025,10 @@ int mingw_rename(const char *pold, const char *pnew) return -1; } if ((attrs & FILE_ATTRIBUTE_READONLY) && - SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) { - if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING)) - return 0; - gle = GetLastError(); - /* revert file attributes on failure */ - SetFileAttributesW(wpnew, attrs); - } + SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) + goto repeat; } - if (gle == ERROR_ACCESS_DENIED && - retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. " + if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. " "Should I try again?", pold, pnew)) goto repeat; From a9533bbae570a6f4776a4a74e4d5603363e9d114 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 01:17:31 +0200 Subject: [PATCH 336/406] Win32: mingw_chdir: change to symlink-resolved directory If symlinks are enabled, resolve all symlinks when changing directories, as required by POSIX. Note: Git's real_path() function bases its link resolution algorithm on this property of chdir(). Unfortunately, the current directory on Windows is limited to only MAX_PATH (260) characters. Therefore using symlinks and long paths in combination may be problematic. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 17116658d5d00d..e863e5eb6e1cc7 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -627,7 +627,24 @@ int mingw_chdir(const char *dirname) wchar_t wdirname[MAX_LONG_PATH]; if (xutftowcs_long_path(wdirname, dirname) < 0) return -1; - result = _wchdir(wdirname); + + if (has_symlinks) { + HANDLE hnd = CreateFileW(wdirname, 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (hnd == INVALID_HANDLE_VALUE) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) { + errno = err_win_to_posix(GetLastError()); + CloseHandle(hnd); + return -1; + } + CloseHandle(hnd); + } + + result = _wchdir(normalize_ntpath(wdirname)); current_directory_len = GetCurrentDirectoryW(0, NULL); return result; } From b428368ee02f9c04abcffcb30dfb1a9905146b63 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 01:24:41 +0200 Subject: [PATCH 337/406] Win32: implement readlink() Implement readlink() by reading NTFS reparse points. Works for symlinks and directory junctions. If symlinks are disabled, fail with ENOSYS. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ compat/mingw.h | 3 +- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index e863e5eb6e1cc7..f649c523c81db8 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2,6 +2,7 @@ #include "win32.h" #include <conio.h> #include <wchar.h> +#include <winioctl.h> #include "../strbuf.h" #include "../run-command.h" #include "../cache.h" @@ -2317,6 +2318,103 @@ int link(const char *oldpath, const char *newpath) return 0; } +#ifndef _WINNT_H +/* + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). + */ +typedef struct _REPARSE_DATA_BUFFER { + DWORD ReparseTag; + WORD ReparseDataLength; + WORD Reserved; +#ifndef _MSC_VER + _ANONYMOUS_UNION +#endif + union { + struct { + WORD SubstituteNameOffset; + WORD SubstituteNameLength; + WORD PrintNameOffset; + WORD PrintNameLength; + ULONG Flags; + WCHAR PathBuffer[1]; + } SymbolicLinkReparseBuffer; + struct { + WORD SubstituteNameOffset; + WORD SubstituteNameLength; + WORD PrintNameOffset; + WORD PrintNameLength; + WCHAR PathBuffer[1]; + } MountPointReparseBuffer; + struct { + BYTE DataBuffer[1]; + } GenericReparseBuffer; + } DUMMYUNIONNAME; +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; +#endif + +int readlink(const char *path, char *buf, size_t bufsiz) +{ + HANDLE handle; + WCHAR wpath[MAX_LONG_PATH], *wbuf; + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); + DWORD dummy; + char tmpbuf[MAX_LONG_PATH]; + int len; + + if (xutftowcs_long_path(wpath, path) < 0) + return -1; + + /* read reparse point data */ + handle = CreateFileW(wpath, 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, + OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); + if (handle == INVALID_HANDLE_VALUE) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { + errno = err_win_to_posix(GetLastError()); + CloseHandle(handle); + return -1; + } + CloseHandle(handle); + + /* get target path for symlinks or mount points (aka 'junctions') */ + switch (b->ReparseTag) { + case IO_REPARSE_TAG_SYMLINK: + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); + *(WCHAR*) (((char*) wbuf) + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; + break; + case IO_REPARSE_TAG_MOUNT_POINT: + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) + + b->MountPointReparseBuffer.SubstituteNameOffset); + *(WCHAR*) (((char*) wbuf) + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; + break; + default: + errno = EINVAL; + return -1; + } + + /* + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure + * condition. There is no conversion function that produces invalid UTF-8, + * so convert to a (hopefully large enough) temporary buffer, then memcpy + * the requested number of bytes (including '\0' for robustness). + */ + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) + return -1; + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); + return min(bufsiz, len); +} + pid_t waitpid(pid_t pid, int *status, int options) { HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, diff --git a/compat/mingw.h b/compat/mingw.h index 4aa84ab783fbdf..7c7ce835650175 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -123,8 +123,6 @@ struct utsname { * trivial stubs */ -static inline int readlink(const char *path, char *buf, size_t bufsiz) -{ errno = ENOSYS; return -1; } static inline int symlink(const char *oldpath, const char *newpath) { errno = ENOSYS; return -1; } static inline int fchmod(int fildes, mode_t mode) @@ -218,6 +216,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out); int sigaction(int sig, struct sigaction *in, struct sigaction *out); int link(const char *oldpath, const char *newpath); int uname(struct utsname *buf); +int readlink(const char *path, char *buf, size_t bufsiz); /* * replacements of existing functions From 04a9212856b9adb4e0cd3b2eda4d0fadd830d68d Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 01:32:03 +0200 Subject: [PATCH 338/406] Win32: implement basic symlink() functionality (file symlinks only) Implement symlink() that always creates file symlinks. Fails with ENOSYS if symlinks are disabled or unsupported. Note: CreateSymbolicLinkW() was introduced with symlink support in Windows Vista. For compatibility with Windows XP, we need to load it dynamically and fail gracefully if it isnt's available. Signed-off-by: Karsten Blees <blees@dcon.de> --- compat/mingw.c | 28 ++++++++++++++++++++++++++++ compat/mingw.h | 3 +-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index f649c523c81db8..d38811484843c1 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2318,6 +2318,34 @@ int link(const char *oldpath, const char *newpath) return 0; } +int symlink(const char *target, const char *link) +{ + wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH]; + int len; + + /* fail if symlinks are disabled or API is not supported (WinXP) */ + if (!has_symlinks) { + errno = ENOSYS; + return -1; + } + + if ((len = xutftowcs_long_path(wtarget, target)) < 0 + || xutftowcs_long_path(wlink, link) < 0) + return -1; + + /* convert target dir separators to backslashes */ + while (len--) + if (wtarget[len] == '/') + wtarget[len] = '\\'; + + /* create file symlink */ + if (!CreateSymbolicLinkW(wlink, wtarget, 0)) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + return 0; +} + #ifndef _WINNT_H /* * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in diff --git a/compat/mingw.h b/compat/mingw.h index 7c7ce835650175..938c81ea1e6713 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -123,8 +123,6 @@ struct utsname { * trivial stubs */ -static inline int symlink(const char *oldpath, const char *newpath) -{ errno = ENOSYS; return -1; } static inline int fchmod(int fildes, mode_t mode) { errno = ENOSYS; return -1; } #ifndef __MINGW64_VERSION_MAJOR @@ -216,6 +214,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out); int sigaction(int sig, struct sigaction *in, struct sigaction *out); int link(const char *oldpath, const char *newpath); int uname(struct utsname *buf); +int symlink(const char *target, const char *link); int readlink(const char *path, char *buf, size_t bufsiz); /* From 0dcfd36e150620496c190f0677e5759eec73b6c8 Mon Sep 17 00:00:00 2001 From: Bert Belder <bertbelder@gmail.com> Date: Fri, 26 Oct 2018 11:13:45 +0200 Subject: [PATCH 339/406] Win32: symlink: move phantom symlink creation to a separate function Signed-off-by: Bert Belder <bertbelder@gmail.com> --- compat/mingw.c | 91 +++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 970e5794076890..db36ab687b470d 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -399,6 +399,54 @@ static void process_phantom_symlinks(void) LeaveCriticalSection(&phantom_symlinks_cs); } +static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink) +{ + int len; + + /* create file symlink */ + if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + + /* convert to directory symlink if target exists */ + switch (process_phantom_symlink(wtarget, wlink)) { + case PHANTOM_SYMLINK_RETRY: { + /* if target doesn't exist, add to phantom symlinks list */ + wchar_t wfullpath[MAX_LONG_PATH]; + struct phantom_symlink_info *psi; + + /* convert to absolute path to be independent of cwd */ + len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL); + if (!len || len >= MAX_LONG_PATH) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + + /* over-allocate and fill phantom_symlink_info structure */ + psi = xmalloc(sizeof(struct phantom_symlink_info) + + sizeof(wchar_t) * (len + wcslen(wtarget) + 2)); + psi->wlink = (wchar_t *)(psi + 1); + wcscpy(psi->wlink, wfullpath); + psi->wtarget = psi->wlink + len + 1; + wcscpy(psi->wtarget, wtarget); + + EnterCriticalSection(&phantom_symlinks_cs); + psi->next = phantom_symlinks; + phantom_symlinks = psi; + LeaveCriticalSection(&phantom_symlinks_cs); + break; + } + case PHANTOM_SYMLINK_DIRECTORY: + /* if we created a dir symlink, process other phantom symlinks */ + process_phantom_symlinks(); + break; + default: + break; + } + return 0; +} + /* Normalizes NT paths as returned by some low-level APIs. */ static wchar_t *normalize_ntpath(wchar_t *wbuf) { @@ -2468,48 +2516,7 @@ int symlink(const char *target, const char *link) if (wtarget[len] == '/') wtarget[len] = '\\'; - /* create file symlink */ - if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) { - errno = err_win_to_posix(GetLastError()); - return -1; - } - - /* convert to directory symlink if target exists */ - switch (process_phantom_symlink(wtarget, wlink)) { - case PHANTOM_SYMLINK_RETRY: { - /* if target doesn't exist, add to phantom symlinks list */ - wchar_t wfullpath[MAX_LONG_PATH]; - struct phantom_symlink_info *psi; - - /* convert to absolute path to be independent of cwd */ - len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL); - if (!len || len >= MAX_LONG_PATH) { - errno = err_win_to_posix(GetLastError()); - return -1; - } - - /* over-allocate and fill phantom_symlink_info structure */ - psi = xmalloc(sizeof(struct phantom_symlink_info) - + sizeof(wchar_t) * (len + wcslen(wtarget) + 2)); - psi->wlink = (wchar_t *)(psi + 1); - wcscpy(psi->wlink, wfullpath); - psi->wtarget = psi->wlink + len + 1; - wcscpy(psi->wtarget, wtarget); - - EnterCriticalSection(&phantom_symlinks_cs); - psi->next = phantom_symlinks; - phantom_symlinks = psi; - LeaveCriticalSection(&phantom_symlinks_cs); - break; - } - case PHANTOM_SYMLINK_DIRECTORY: - /* if we created a dir symlink, process other phantom symlinks */ - process_phantom_symlinks(); - break; - default: - break; - } - return 0; + return create_phantom_symlink(wtarget, wlink); } #ifndef _WINNT_H From 2970e206d036969c7dd7d0c54cf5c4f6daf0e642 Mon Sep 17 00:00:00 2001 From: Karsten Blees <blees@dcon.de> Date: Sun, 24 May 2015 01:48:35 +0200 Subject: [PATCH 340/406] Win32: symlink: add support for symlinks to directories Symlinks on Windows have a flag that indicates whether the target is a file or a directory. Symlinks of wrong type simply don't work. This even affects core Win32 APIs (e.g. DeleteFile() refuses to delete directory symlinks). However, CreateFile() with FILE_FLAG_BACKUP_SEMANTICS doesn't seem to care. Check the target type by first creating a tentative file symlink, opening it, and checking the type of the resulting handle. If it is a directory, recreate the symlink with the directory flag set. It is possible to create symlinks before the target exists (or in case of symlinks to symlinks: before the target type is known). If this happens, create a tentative file symlink and postpone the directory decision: keep a list of phantom symlinks to be processed whenever a new directory is created in mingw_mkdir(). Limitations: This algorithm may fail if a link target changes from file to directory or vice versa, or if the target directory is created in another process. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index d38811484843c1..f612345eb998d5 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -271,6 +271,131 @@ int mingw_core_config(const char *var, const char *value, void *cb) return 0; } +enum phantom_symlink_result { + PHANTOM_SYMLINK_RETRY, + PHANTOM_SYMLINK_DONE, + PHANTOM_SYMLINK_DIRECTORY +}; + +static inline int is_wdir_sep(wchar_t wchar) +{ + return wchar == L'/' || wchar == L'\\'; +} + +static const wchar_t *make_relative_to(const wchar_t *path, + const wchar_t *relative_to, wchar_t *out, + size_t size) +{ + size_t i = wcslen(relative_to), len; + + /* Is `path` already absolute? */ + if (is_wdir_sep(path[0]) || + (iswalpha(path[0]) && path[1] == L':' && is_wdir_sep(path[2]))) + return path; + + while (i > 0 && !is_wdir_sep(relative_to[i - 1])) + i--; + + /* Is `relative_to` in the current directory? */ + if (!i) + return path; + + len = wcslen(path); + if (i + len + 1 > size) { + error("Could not make '%S' relative to '%S' (too large)", + path, relative_to); + return NULL; + } + + memcpy(out, relative_to, i * sizeof(wchar_t)); + wcscpy(out + i, path); + return out; +} + +/* + * Changes a file symlink to a directory symlink if the target exists and is a + * directory. + */ +static enum phantom_symlink_result +process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink) +{ + HANDLE hnd; + BY_HANDLE_FILE_INFORMATION fdata; + wchar_t relative[MAX_LONG_PATH]; + const wchar_t *rel; + + /* check that wlink is still a file symlink */ + if ((GetFileAttributesW(wlink) + & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY)) + != FILE_ATTRIBUTE_REPARSE_POINT) + return PHANTOM_SYMLINK_DONE; + + /* make it relative, if necessary */ + rel = make_relative_to(wtarget, wlink, relative, ARRAY_SIZE(relative)); + if (!rel) + return PHANTOM_SYMLINK_DONE; + + /* let Windows resolve the link by opening it */ + hnd = CreateFileW(rel, 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (hnd == INVALID_HANDLE_VALUE) { + errno = err_win_to_posix(GetLastError()); + return PHANTOM_SYMLINK_RETRY; + } + + if (!GetFileInformationByHandle(hnd, &fdata)) { + errno = err_win_to_posix(GetLastError()); + CloseHandle(hnd); + return PHANTOM_SYMLINK_RETRY; + } + CloseHandle(hnd); + + /* if target exists and is a file, we're done */ + if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + return PHANTOM_SYMLINK_DONE; + + /* otherwise recreate the symlink with directory flag */ + if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1)) + return PHANTOM_SYMLINK_DIRECTORY; + + errno = err_win_to_posix(GetLastError()); + return PHANTOM_SYMLINK_RETRY; +} + +/* keep track of newly created symlinks to non-existing targets */ +struct phantom_symlink_info { + struct phantom_symlink_info *next; + wchar_t *wlink; + wchar_t *wtarget; +}; + +static struct phantom_symlink_info *phantom_symlinks = NULL; +static CRITICAL_SECTION phantom_symlinks_cs; + +static void process_phantom_symlinks(void) +{ + struct phantom_symlink_info *current, **psi; + EnterCriticalSection(&phantom_symlinks_cs); + /* process phantom symlinks list */ + psi = &phantom_symlinks; + while ((current = *psi)) { + enum phantom_symlink_result result = process_phantom_symlink( + current->wtarget, current->wlink); + if (result == PHANTOM_SYMLINK_RETRY) { + psi = ¤t->next; + } else { + /* symlink was processed, remove from list */ + *psi = current->next; + free(current); + /* if symlink was a directory, start over */ + if (result == PHANTOM_SYMLINK_DIRECTORY) + psi = &phantom_symlinks; + } + } + LeaveCriticalSection(&phantom_symlinks_cs); +} + /* Normalizes NT paths as returned by some low-level APIs. */ static wchar_t *normalize_ntpath(wchar_t *wbuf) { @@ -420,6 +545,8 @@ int mingw_mkdir(const char *path, int mode) return -1; ret = _wmkdir(wpath); + if (!ret) + process_phantom_symlinks(); if (!ret && needs_hiding(path)) return set_hidden_flag(wpath, 1); return ret; @@ -2343,6 +2470,42 @@ int symlink(const char *target, const char *link) errno = err_win_to_posix(GetLastError()); return -1; } + + /* convert to directory symlink if target exists */ + switch (process_phantom_symlink(wtarget, wlink)) { + case PHANTOM_SYMLINK_RETRY: { + /* if target doesn't exist, add to phantom symlinks list */ + wchar_t wfullpath[MAX_LONG_PATH]; + struct phantom_symlink_info *psi; + + /* convert to absolute path to be independent of cwd */ + len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL); + if (!len || len >= MAX_LONG_PATH) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + + /* over-allocate and fill phantom_symlink_info structure */ + psi = xmalloc(sizeof(struct phantom_symlink_info) + + sizeof(wchar_t) * (len + wcslen(wtarget) + 2)); + psi->wlink = (wchar_t *)(psi + 1); + wcscpy(psi->wlink, wfullpath); + psi->wtarget = psi->wlink + len + 1; + wcscpy(psi->wtarget, wtarget); + + EnterCriticalSection(&phantom_symlinks_cs); + psi->next = phantom_symlinks; + phantom_symlinks = psi; + LeaveCriticalSection(&phantom_symlinks_cs); + break; + } + case PHANTOM_SYMLINK_DIRECTORY: + /* if we created a dir symlink, process other phantom symlinks */ + process_phantom_symlinks(); + break; + default: + break; + } return 0; } @@ -2879,6 +3042,7 @@ int wmain(int argc, const wchar_t **wargv) /* initialize critical section for waitpid pinfo_t list */ InitializeCriticalSection(&pinfo_cs); + InitializeCriticalSection(&phantom_symlinks_cs); /* set up default file mode and file modes for stdin/out/err */ _fmode = _O_BINARY; From 2eed934ad03ab74f00158fb66274b076abe85234 Mon Sep 17 00:00:00 2001 From: Bert Belder <bertbelder@gmail.com> Date: Fri, 26 Oct 2018 11:51:51 +0200 Subject: [PATCH 341/406] Win32: symlink: specify symlink type in .gitattributes On Windows, symbolic links have a type: a "file symlink" must point at a file, and a "directory symlink" must point at a directory. If the type of symlink does not match its target, it doesn't work. Git does not record the type of symlink in the index or in a tree. On checkout it'll guess the type, which only works if the target exists at the time the symlink is created. This may often not be the case, for example when the link points at a directory inside a submodule. By specifying `symlink=file` or `symlink=dir` the user can specify what type of symlink Git should create, so Git doesn't have to rely on unreliable heuristics. Signed-off-by: Bert Belder <bertbelder@gmail.com> --- Documentation/gitattributes.txt | 30 ++++++++++++++++++ compat/mingw.c | 56 ++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt index 92010b062e0867..defb6278939717 100644 --- a/Documentation/gitattributes.txt +++ b/Documentation/gitattributes.txt @@ -378,6 +378,36 @@ sign `$` upon checkout. Any byte sequence that begins with with `$Id$` upon check-in. +`symlink` +^^^^^^^^^ + +On Windows, symbolic links have a type: a "file symlink" must point at +a file, and a "directory symlink" must point at a directory. If the +type of symlink does not match its target, it doesn't work. + +Git does not record the type of symlink in the index or in a tree. On +checkout it'll guess the type, which only works if the target exists +at the time the symlink is created. This may often not be the case, +for example when the link points at a directory inside a submodule. + +The `symlink` attribute allows you to explicitly set the type of symlink +to `file` or `dir`, so Git doesn't have to guess. If you have a set of +symlinks that point at other files, you can do: + +------------------------ +*.gif symlink=file +------------------------ + +To tell Git that a symlink points at a directory, use: + +------------------------ +tools_folder symlink=dir +------------------------ + +The `symlink` attribute is ignored on platforms other than Windows, +since they don't distinguish between different types of symlinks. + + `filter` ^^^^^^^^ diff --git a/compat/mingw.c b/compat/mingw.c index db36ab687b470d..cb69f3136e64b3 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -8,6 +8,7 @@ #include "../cache.h" #include "win32/lazyload.h" #include "../config.h" +#include "../attr.h" #define HCAST(type, handle) ((type)(intptr_t)handle) @@ -2496,6 +2497,35 @@ int link(const char *oldpath, const char *newpath) return 0; } +enum symlink_type { + SYMLINK_TYPE_UNSPECIFIED = 0, + SYMLINK_TYPE_FILE, + SYMLINK_TYPE_DIRECTORY, +}; + +static enum symlink_type check_symlink_attr(const char *link) +{ + static struct attr_check *check; + const char *value; + int r; + + if (!check) + check = attr_check_initl("symlink", NULL); + + r = git_check_attr(&the_index, link, check); + assert(!r); + + value = check->items[0].value; + if (value == NULL) + ; + else if (!strcmp(value, "file")) + return SYMLINK_TYPE_FILE; + else if (!strcmp(value, "dir")) + return SYMLINK_TYPE_DIRECTORY; + + return SYMLINK_TYPE_UNSPECIFIED; +} + int symlink(const char *target, const char *link) { wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH]; @@ -2516,7 +2546,31 @@ int symlink(const char *target, const char *link) if (wtarget[len] == '/') wtarget[len] = '\\'; - return create_phantom_symlink(wtarget, wlink); + switch (check_symlink_attr(link)) { + case SYMLINK_TYPE_UNSPECIFIED: + /* Create a phantom symlink: it is initially created as a file + * symlink, but may change to a directory symlink later if/when + * the target exists. */ + return create_phantom_symlink(wtarget, wlink); + case SYMLINK_TYPE_FILE: + if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) + break; + return 0; + case SYMLINK_TYPE_DIRECTORY: + if (!CreateSymbolicLinkW(wlink, wtarget, + symlink_directory_flags)) + break; + /* There may be dangling phantom symlinks that point at this + * one, which should now morph into directory symlinks. */ + process_phantom_symlinks(); + return 0; + default: + BUG("unhandled symlink type"); + } + + /* CreateSymbolicLinkW failed. */ + errno = err_win_to_posix(GetLastError()); + return -1; } #ifndef _WINNT_H From b1c3e01f487767ee8de0683db2f2982956c92340 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 30 May 2017 21:50:57 +0200 Subject: [PATCH 342/406] mingw: try to create symlinks without elevated permissions With Windows 10 Build 14972 in Developer Mode, a new flag is supported by CreateSymbolicLink() to create symbolic links even when running outside of an elevated session (which was previously required). This new flag is called SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE and has the numeric value 0x02. Previous Windows 10 versions will not understand that flag and return an ERROR_INVALID_PARAMETER, therefore we have to be careful to try passing that flag only when the build number indicates that it is supported. For more information about the new flag, see this blog post: https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/ This patch is loosely based on the patch submitted by Samuel D. Leslie as https://github.com/git-for-windows/git/pull/1184. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index f612345eb998d5..970e5794076890 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -271,6 +271,8 @@ int mingw_core_config(const char *var, const char *value, void *cb) return 0; } +static DWORD symlink_file_flags = 0, symlink_directory_flags = 1; + enum phantom_symlink_result { PHANTOM_SYMLINK_RETRY, PHANTOM_SYMLINK_DONE, @@ -356,7 +358,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink) return PHANTOM_SYMLINK_DONE; /* otherwise recreate the symlink with directory flag */ - if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1)) + if (DeleteFileW(wlink) && + CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags)) return PHANTOM_SYMLINK_DIRECTORY; errno = err_win_to_posix(GetLastError()); @@ -2466,7 +2469,7 @@ int symlink(const char *target, const char *link) wtarget[len] = '\\'; /* create file symlink */ - if (!CreateSymbolicLinkW(wlink, wtarget, 0)) { + if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) { errno = err_win_to_posix(GetLastError()); return -1; } @@ -2981,6 +2984,24 @@ static void maybe_redirect_std_handles(void) GENERIC_WRITE, FILE_FLAG_NO_BUFFERING); } +static void adjust_symlink_flags(void) +{ + /* + * Starting with Windows 10 Build 14972, symbolic links can be created + * using CreateSymbolicLink() without elevation by passing the flag + * SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last + * parameter, provided the Developer Mode has been enabled. Some + * earlier Windows versions complain about this flag with an + * ERROR_INVALID_PARAMETER, hence we have to test the build number + * specifically. + */ + if (GetVersion() >= 14972 << 16) { + symlink_file_flags |= 2; + symlink_directory_flags |= 2; + } + +} + #ifdef _MSC_VER #ifdef _DEBUG #include <crtdbg.h> @@ -3013,6 +3034,7 @@ int wmain(int argc, const wchar_t **wargv) #endif maybe_redirect_std_handles(); + adjust_symlink_flags(); /* determine size of argv and environ conversion buffer */ maxlen = wcslen(wargv[0]); From 059a694adfbcb7250cfec306179893c072ed94f8 Mon Sep 17 00:00:00 2001 From: Bert Belder <bertbelder@gmail.com> Date: Fri, 26 Oct 2018 23:42:09 +0200 Subject: [PATCH 343/406] Win32: symlink: add test for `symlink` attribute Signed-off-by: Bert Belder <bertbelder@gmail.com> --- t/t2040-checkout-symlink-attr.sh | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 t/t2040-checkout-symlink-attr.sh diff --git a/t/t2040-checkout-symlink-attr.sh b/t/t2040-checkout-symlink-attr.sh new file mode 100755 index 00000000000000..6b8a15116e5c95 --- /dev/null +++ b/t/t2040-checkout-symlink-attr.sh @@ -0,0 +1,51 @@ +#!/bin/sh + +test_description='checkout symlinks with `symlink` attribute on Windows + +Ensures that Git for Windows creates symlinks of the right type, +as specified by the `symlink` attribute in `.gitattributes`.' + +# Tell MSYS to create native symlinks. Without this flag test-lib's +# prerequisite detection for SYMLINKS doesn't detect the right thing. +MSYS=winsymlinks:nativestrict && export MSYS + +. ./test-lib.sh + +if ! test_have_prereq MINGW,SYMLINKS +then + skip_all='skipping $0: MinGW-only test, which requires symlink support.' + test_done +fi + +# Adds a symlink to the index without clobbering the work tree. +cache_symlink () { + sha=$(printf '%s' "$1" | git hash-object --stdin -w) && + git update-index --add --cacheinfo 120000,$sha,"$2" +} + +# MSYS2 is very forgiving, it will resolve symlinks even if the +# symlink type isn't correct. To make this test meaningful, try +# them with a native, non-MSYS executable. +cat_native () { + filename=$(cygpath -w "$1") && + cmd.exe /c "type \"$filename\"" +} + +test_expect_success 'checkout symlinks with attr' ' + cache_symlink file1 file-link && + cache_symlink dir dir-link && + + printf "file-link symlink=file\ndir-link symlink=dir\n" >.gitattributes && + git add .gitattributes && + + git checkout . && + + mkdir dir && + echo "contents1" >file1 && + echo "contents2" >dir/file2 && + + test "$(cat_native file-link)" = "contents1" && + test "$(cat_native dir-link/file2)" = "contents2" +' + +test_done From 78e867e3814298b3325a5c45ee02ca302b19151d Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Tue, 24 Jan 2017 15:12:13 -0500 Subject: [PATCH 344/406] fscache: add key for GIT_TRACE_FSCACHE Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/win32/fscache.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 4ebd15e4265b5b..c30cef75e0dc90 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -7,6 +7,7 @@ static int initialized; static volatile long enabled; static struct hashmap map; static CRITICAL_SECTION mutex; +static struct trace_key trace_fscache = TRACE_KEY_INIT(FSCACHE); /* * An entry in the file system cache. Used for both entire directory listings @@ -192,6 +193,8 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir) if (h == INVALID_HANDLE_VALUE) { err = GetLastError(); errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); + trace_printf_key(&trace_fscache, "fscache: error(%d) '%.*s'\n", + errno, dir->len, dir->name); return NULL; } @@ -377,6 +380,7 @@ int fscache_enable(int enable) fscache_clear(); LeaveCriticalSection(&mutex); } + trace_printf_key(&trace_fscache, "fscache: enable(%d)\n", enable); return result; } From a80fc8a7612a0046c4829ed99ef9b92a992ce61b Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Tue, 13 Dec 2016 14:05:32 -0500 Subject: [PATCH 345/406] fscache: remember not-found directories Teach FSCACHE to remember "not found" directories. This is a performance optimization. FSCACHE is a performance optimization available for Windows. It intercepts Posix-style lstat() calls into an in-memory directory using FindFirst/FindNext. It improves performance on Windows by catching the first lstat() call in a directory, using FindFirst/ FindNext to read the list of files (and attribute data) for the entire directory into the cache, and short-cut subsequent lstat() calls in the same directory. This gives a major performance boost on Windows. However, it does not remember "not found" directories. When STATUS runs and there are missing directories, the lstat() interception fails to find the parent directory and simply return ENOENT for the file -- it does not remember that the FindFirst on the directory failed. Thus subsequent lstat() calls in the same directory, each re-attempt the FindFirst. This completely defeats any performance gains. This can be seen by doing a sparse-checkout on a large repo and then doing a read-tree to reset the skip-worktree bits and then running status. This change reduced status times for my very large repo by 60%. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/win32/fscache.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index c30cef75e0dc90..5f050122361c15 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -165,7 +165,8 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, * Dir should not contain trailing '/'. Use an empty string for the current * directory (not "."!). */ -static struct fsentry *fsentry_create_list(const struct fsentry *dir) +static struct fsentry *fsentry_create_list(const struct fsentry *dir, + int *dir_not_found) { wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */ WIN32_FIND_DATAW fdata; @@ -174,6 +175,8 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir) struct fsentry *list, **phead; DWORD err; + *dir_not_found = 0; + /* convert name to UTF-16 and check length */ if ((wlen = xutftowcs_path_ex(pattern, dir->name, MAX_LONG_PATH, dir->len, MAX_PATH - 2, core_long_paths)) < 0) @@ -192,6 +195,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir) h = FindFirstFileW(pattern, &fdata); if (h == INVALID_HANDLE_VALUE) { err = GetLastError(); + *dir_not_found = 1; /* or empty directory */ errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); trace_printf_key(&trace_fscache, "fscache: error(%d) '%.*s'\n", errno, dir->len, dir->name); @@ -200,6 +204,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir) /* allocate object to hold directory listing */ list = fsentry_alloc(NULL, dir->name, dir->len); + list->st_mode = S_IFDIR; /* walk directory and build linked list of fsentry structures */ phead = &list->next; @@ -284,12 +289,16 @@ static struct fsentry *fscache_get_wait(struct fsentry *key) static struct fsentry *fscache_get(struct fsentry *key) { struct fsentry *fse, *future, *waiter; + int dir_not_found; EnterCriticalSection(&mutex); /* check if entry is in cache */ fse = fscache_get_wait(key); if (fse) { - fsentry_addref(fse); + if (fse->st_mode) + fsentry_addref(fse); + else + fse = NULL; /* non-existing directory */ LeaveCriticalSection(&mutex); return fse; } @@ -298,7 +307,10 @@ static struct fsentry *fscache_get(struct fsentry *key) fse = fscache_get_wait(key->list); if (fse) { LeaveCriticalSection(&mutex); - /* dir entry without file entry -> file doesn't exist */ + /* + * dir entry without file entry, or dir does not + * exist -> file doesn't exist + */ errno = ENOENT; return NULL; } @@ -312,7 +324,7 @@ static struct fsentry *fscache_get(struct fsentry *key) /* create the directory listing (outside mutex!) */ LeaveCriticalSection(&mutex); - fse = fsentry_create_list(future); + fse = fsentry_create_list(future, &dir_not_found); EnterCriticalSection(&mutex); /* remove future entry and signal waiting threads */ @@ -326,6 +338,17 @@ static struct fsentry *fscache_get(struct fsentry *key) /* leave on error (errno set by fsentry_create_list) */ if (!fse) { + if (dir_not_found && key->list) { + /* + * Record that the directory does not exist (or is + * empty, which for all practical matters is the same + * thing as far as fscache is concerned). + */ + fse = fsentry_alloc(key->list->list, + key->list->name, key->list->len); + fse->st_mode = 0; + hashmap_add(&map, fse); + } LeaveCriticalSection(&mutex); return NULL; } @@ -337,6 +360,9 @@ static struct fsentry *fscache_get(struct fsentry *key) if (key->list) fse = hashmap_get(&map, key, NULL); + if (fse && !fse->st_mode) + fse = NULL; /* non-existing directory */ + /* return entry or ENOENT */ if (fse) fsentry_addref(fse); From 8860db1102b313413af89ed2027e5582673249df Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 2 Apr 2016 13:05:08 +0200 Subject: [PATCH 346/406] mingw: support spawning programs containing spaces in their names The CreateProcessW() function does not really support spaces in its first argument, lpApplicationName. But it supports passing NULL as lpApplicationName, which makes it figure out the application from the (possibly quoted) first argument of lpCommandLine. Let's use that trick (if we are certain that the first argument matches the executable's path) to support launching programs whose path contains spaces. This fixes https://github.com/git-for-windows/git/issue/692 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 75b51f83bdaec4..e5f21fefe24b8c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1415,7 +1415,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen si.hStdError = winansi_get_osfhandle(fherr); /* executables and the current directory don't support long paths */ - if (xutftowcs_path(wcmd, cmd) < 0) + if (*argv && !strcmp(cmd, *argv)) + wcmd[0] = L'\0'; + else if (xutftowcs_path(wcmd, cmd) < 0) return -1; if (dir && xutftowcs_path(wdir, dir) < 0) return -1; @@ -1444,8 +1446,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen wenvblk = make_environment_block(deltaenv); memset(&pi, 0, sizeof(pi)); - ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags, - wenvblk, dir ? wdir : NULL, &si, &pi); + ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE, + flags, wenvblk, dir ? wdir : NULL, &si, &pi); free(wenvblk); free(wargs); From 2bcb1b247d7ce3c9cfbf2bf4eed5ee09e67737bd Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 25 Jan 2017 18:39:16 +0100 Subject: [PATCH 347/406] fscache: add a test for the dir-not-found optimization Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t1090-sparse-checkout-scope.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/t/t1090-sparse-checkout-scope.sh b/t/t1090-sparse-checkout-scope.sh index 1f61eb3e88e30c..bea7e18233df0a 100755 --- a/t/t1090-sparse-checkout-scope.sh +++ b/t/t1090-sparse-checkout-scope.sh @@ -49,4 +49,24 @@ test_expect_success 'return to full checkout of master' ' test "$(cat b)" = "modified" ' +test_expect_success MINGW 'no unnecessary opendir() with fscache' ' + git clone . fscache-test && + ( + cd fscache-test && + git config core.fscache 1 && + echo "/excluded/*" >.git/info/sparse-checkout && + for f in $(test_seq 10) + do + sha1=$(echo $f | git hash-object -w --stdin) && + git update-index --add \ + --cacheinfo 100644,$sha1,excluded/$f || break + done && + test_tick && + git commit -m excluded && + GIT_TRACE_FSCACHE=1 git status >out 2>err && + grep excluded err >grep.out && + test_line_count = 1 grep.out + ) +' + test_done From e4a94d58e8743b306f7d852a10a0210ad8d7a986 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 30 Aug 2017 01:28:22 +0200 Subject: [PATCH 348/406] mingw: ensure that core.longPaths is handled *always* A ton of Git commands simply do not read (or at least parse) the core.* settings. This is not good, as Git for Windows relies on the core.longPaths setting to be read quite early on. So let's just make sure that all commands read the config and give platform_core_config() a chance. This patch teaches tons of Git commands to respect the config setting `core.longPaths = true`, including `pack-refs`, thereby fixing https://github.com/git-for-windows/git/issues/1218 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/archive.c | 2 ++ builtin/bisect--helper.c | 2 ++ builtin/bundle.c | 2 ++ builtin/check-ref-format.c | 2 ++ builtin/clone.c | 1 + builtin/column.c | 2 ++ builtin/credential.c | 3 +++ builtin/fetch-pack.c | 2 ++ builtin/get-tar-commit-id.c | 2 ++ builtin/interpret-trailers.c | 2 ++ builtin/log.c | 1 + builtin/ls-remote.c | 2 ++ builtin/mailinfo.c | 2 ++ builtin/mailsplit.c | 2 ++ builtin/merge-index.c | 3 +++ builtin/merge-tree.c | 2 ++ builtin/mktag.c | 2 ++ builtin/mktree.c | 2 ++ builtin/pack-refs.c | 2 ++ builtin/prune-packed.c | 2 ++ builtin/prune.c | 3 +++ builtin/reflog.c | 1 + builtin/remote-ext.c | 2 ++ builtin/remote.c | 1 + builtin/rev-parse.c | 1 + builtin/show-index.c | 2 ++ builtin/show-ref.c | 2 ++ builtin/stripspace.c | 5 ++--- builtin/submodule--helper.c | 1 + builtin/upload-archive.c | 3 +++ credential-store.c | 3 +++ http-backend.c | 1 + refs.c | 2 +- 33 files changed, 63 insertions(+), 4 deletions(-) diff --git a/builtin/archive.c b/builtin/archive.c index e74f675390d975..27b6984f789a3b 100644 --- a/builtin/archive.c +++ b/builtin/archive.c @@ -9,6 +9,7 @@ #include "parse-options.h" #include "pkt-line.h" #include "sideband.h" +#include "config.h" static void create_output_file(const char *output_file) { @@ -94,6 +95,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix) OPT_END() }; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, local_opts, NULL, PARSE_OPT_KEEP_ALL); diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 4b5fadcbe1ae6a..f6742e8077b38a 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -3,6 +3,7 @@ #include "parse-options.h" #include "bisect.h" #include "refs.h" +#include "config.h" static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -129,6 +130,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) OPT_END() }; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, git_bisect_helper_usage, 0); diff --git a/builtin/bundle.c b/builtin/bundle.c index d0de59b94ff23e..da8c94123fefe9 100644 --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -1,6 +1,7 @@ #include "builtin.h" #include "cache.h" #include "bundle.h" +#include "config.h" /* * Basic handler for bundle files to connect repositories via sneakernet. @@ -21,6 +22,7 @@ int cmd_bundle(int argc, const char **argv, const char *prefix) const char *cmd, *bundle_file; int bundle_fd = -1; + git_config(git_default_config, NULL); if (argc < 3) usage(builtin_bundle_usage); diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c index bc67d3f0a83d35..abee1be4721055 100644 --- a/builtin/check-ref-format.c +++ b/builtin/check-ref-format.c @@ -6,6 +6,7 @@ #include "refs.h" #include "builtin.h" #include "strbuf.h" +#include "config.h" static const char builtin_check_ref_format_usage[] = "git check-ref-format [--normalize] [<options>] <refname>\n" @@ -58,6 +59,7 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix) int flags = 0; const char *refname; + git_config(git_default_config, NULL); if (argc == 2 && !strcmp(argv[1], "-h")) usage(builtin_check_ref_format_usage); diff --git a/builtin/clone.c b/builtin/clone.c index 2c7a1d973a85bc..e62df8ec504e4b 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -900,6 +900,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) struct refspec rs = REFSPEC_INIT_FETCH; struct argv_array ref_prefixes = ARGV_ARRAY_INIT; + git_config(platform_core_config, NULL); fetch_if_missing = 0; packet_trace_identity("clone"); diff --git a/builtin/column.c b/builtin/column.c index 5228ccf37a5c8f..a046a1d5959ea3 100644 --- a/builtin/column.c +++ b/builtin/column.c @@ -34,6 +34,8 @@ int cmd_column(int argc, const char **argv, const char *prefix) OPT_END() }; + git_config(platform_core_config, NULL); + /* This one is special and must be the first one */ if (argc > 1 && starts_with(argv[1], "--command=")) { command = argv[1] + 10; diff --git a/builtin/credential.c b/builtin/credential.c index 879acfbcda7514..d75dcdc64aa3c3 100644 --- a/builtin/credential.c +++ b/builtin/credential.c @@ -1,6 +1,7 @@ #include "git-compat-util.h" #include "credential.h" #include "builtin.h" +#include "config.h" static const char usage_msg[] = "git credential [fill|approve|reject]"; @@ -10,6 +11,8 @@ int cmd_credential(int argc, const char **argv, const char *prefix) const char *op; struct credential c = CREDENTIAL_INIT; + git_config(git_default_config, NULL); + if (argc != 2 || !strcmp(argv[1], "-h")) usage(usage_msg); op = argv[1]; diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index 1a1bc63566b44b..208a97a9163e23 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -5,6 +5,7 @@ #include "connect.h" #include "sha1-array.h" #include "protocol.h" +#include "config.h" static const char fetch_pack_usage[] = "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] " @@ -55,6 +56,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) struct string_list deepen_not = STRING_LIST_INIT_DUP; struct packet_reader reader; + git_config(git_default_config, NULL); fetch_if_missing = 0; packet_trace_identity("fetch-pack"); diff --git a/builtin/get-tar-commit-id.c b/builtin/get-tar-commit-id.c index 2706fcfaf2261e..afb3dbf9176f52 100644 --- a/builtin/get-tar-commit-id.c +++ b/builtin/get-tar-commit-id.c @@ -6,6 +6,7 @@ #include "tar.h" #include "builtin.h" #include "quote.h" +#include "config.h" static const char builtin_get_tar_commit_id_usage[] = "git get-tar-commit-id"; @@ -25,6 +26,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix) if (argc != 1) usage(builtin_get_tar_commit_id_usage); + git_config(git_default_config, NULL); n = read_in_full(0, buffer, HEADERSIZE); if (n < 0) die_errno("git get-tar-commit-id: read error"); diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index b742539d4de203..4778fe23205f84 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -10,6 +10,7 @@ #include "parse-options.h" #include "string-list.h" #include "trailer.h" +#include "config.h" static const char * const git_interpret_trailers_usage[] = { N_("git interpret-trailers [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"), @@ -109,6 +110,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) OPT_END() }; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, git_interpret_trailers_usage, 0); diff --git a/builtin/log.c b/builtin/log.c index e094560d9abca6..4532c55a871c8c 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -1915,6 +1915,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix) OPT_END() }; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, cherry_usage, 0); switch (argc) { diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c index 1a25df7ee15b45..a917af265d49f2 100644 --- a/builtin/ls-remote.c +++ b/builtin/ls-remote.c @@ -4,6 +4,7 @@ #include "ref-filter.h" #include "remote.h" #include "refs.h" +#include "config.h" static const char * const ls_remote_usage[] = { N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n" @@ -84,6 +85,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) PARSE_OPT_STOP_AT_NON_OPTION); dest = argv[0]; + git_config(git_default_config, NULL); if (argc > 1) { int i; pattern = xcalloc(argc, sizeof(const char *)); diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c index cfb667a594c845..150fe3d942371d 100644 --- a/builtin/mailinfo.c +++ b/builtin/mailinfo.c @@ -7,6 +7,7 @@ #include "utf8.h" #include "strbuf.h" #include "mailinfo.h" +#include "config.h" static const char mailinfo_usage[] = "git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info"; @@ -18,6 +19,7 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix) int status; char *msgfile, *patchfile; + git_config(git_default_config, NULL); setup_mailinfo(&mi); def_charset = get_commit_output_encoding(); diff --git a/builtin/mailsplit.c b/builtin/mailsplit.c index 664400b8169b67..472d2eb8a4d6f7 100644 --- a/builtin/mailsplit.c +++ b/builtin/mailsplit.c @@ -8,6 +8,7 @@ #include "builtin.h" #include "string-list.h" #include "strbuf.h" +#include "config.h" static const char git_mailsplit_usage[] = "git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]"; @@ -276,6 +277,7 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix) const char **argp; static const char *stdin_only[] = { "-", NULL }; + git_config(git_default_config, NULL); for (argp = argv+1; *argp; argp++) { const char *arg = *argp; diff --git a/builtin/merge-index.c b/builtin/merge-index.c index c99443b095bd8a..caaef0c03d7431 100644 --- a/builtin/merge-index.c +++ b/builtin/merge-index.c @@ -1,5 +1,6 @@ #include "builtin.h" #include "run-command.h" +#include "config.h" static const char *pgm; static int one_shot, quiet; @@ -74,6 +75,8 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix) */ signal(SIGCHLD, SIG_DFL); + git_config(git_default_config, NULL); + if (argc < 3) usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])"); diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c index f8023bae1e2ece..ffbcf8b6777616 100644 --- a/builtin/merge-tree.c +++ b/builtin/merge-tree.c @@ -6,6 +6,7 @@ #include "blob.h" #include "exec-cmd.h" #include "merge-blobs.h" +#include "config.h" static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>"; @@ -369,6 +370,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix) if (argc != 4) usage(merge_tree_usage); + git_config(git_default_config, NULL); buf1 = get_tree_descriptor(t+0, argv[1]); buf2 = get_tree_descriptor(t+1, argv[2]); buf3 = get_tree_descriptor(t+2, argv[3]); diff --git a/builtin/mktag.c b/builtin/mktag.c index 6fb7dc8578d685..ab9468713b2ff0 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -2,6 +2,7 @@ #include "tag.h" #include "replace-object.h" #include "object-store.h" +#include "config.h" /* * A signature file has a very simple fixed format: four lines @@ -158,6 +159,7 @@ int cmd_mktag(int argc, const char **argv, const char *prefix) if (argc != 1) usage("git mktag"); + git_config(git_default_config, NULL); if (strbuf_read(&buf, 0, 4096) < 0) { die_errno("could not read from stdin"); } diff --git a/builtin/mktree.c b/builtin/mktree.c index 2dc4ad6ba8f227..426f22734efa61 100644 --- a/builtin/mktree.c +++ b/builtin/mktree.c @@ -8,6 +8,7 @@ #include "tree.h" #include "parse-options.h" #include "object-store.h" +#include "config.h" static struct treeent { unsigned mode; @@ -157,6 +158,7 @@ int cmd_mktree(int ac, const char **av, const char *prefix) OPT_END() }; + git_config(git_default_config, NULL); ac = parse_options(ac, av, prefix, option, mktree_usage, 0); getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; diff --git a/builtin/pack-refs.c b/builtin/pack-refs.c index f3353564f99205..ce8a5e0fc4467c 100644 --- a/builtin/pack-refs.c +++ b/builtin/pack-refs.c @@ -2,6 +2,7 @@ #include "parse-options.h" #include "refs.h" #include "repository.h" +#include "config.h" static char const * const pack_refs_usage[] = { N_("git pack-refs [<options>]"), @@ -16,6 +17,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix) OPT_BIT(0, "prune", &flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE), OPT_END(), }; + git_config(git_default_config, NULL); if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0)) usage_with_options(pack_refs_usage, opts); return refs_pack_refs(get_main_ref_store(the_repository), flags); diff --git a/builtin/prune-packed.c b/builtin/prune-packed.c index a9e7b552b9d117..99189d22005d2c 100644 --- a/builtin/prune-packed.c +++ b/builtin/prune-packed.c @@ -4,6 +4,7 @@ #include "parse-options.h" #include "packfile.h" #include "object-store.h" +#include "config.h" static const char * const prune_packed_usage[] = { N_("git prune-packed [-n | --dry-run] [-q | --quiet]"), @@ -60,6 +61,7 @@ int cmd_prune_packed(int argc, const char **argv, const char *prefix) OPT_END() }; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, prune_packed_options, prune_packed_usage, 0); diff --git a/builtin/prune.c b/builtin/prune.c index 4916a4daa264ee..eea876cc063e33 100644 --- a/builtin/prune.c +++ b/builtin/prune.c @@ -7,6 +7,7 @@ #include "parse-options.h" #include "progress.h" #include "object-store.h" +#include "config.h" static const char * const prune_usage[] = { N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"), @@ -116,6 +117,8 @@ int cmd_prune(int argc, const char **argv, const char *prefix) }; char *s; + git_config(git_default_config, NULL); + expire = TIME_MAX; save_commit_buffer = 0; read_replace_refs = 0; diff --git a/builtin/reflog.c b/builtin/reflog.c index 3acef5a0abed40..4b51e25d7b3a92 100644 --- a/builtin/reflog.c +++ b/builtin/reflog.c @@ -723,6 +723,7 @@ static const char reflog_usage[] = int cmd_reflog(int argc, const char **argv, const char *prefix) { + git_config(git_default_config, NULL); if (argc > 1 && !strcmp(argv[1], "-h")) usage(reflog_usage); diff --git a/builtin/remote-ext.c b/builtin/remote-ext.c index 6a9127a33c019c..4eb669fde498e9 100644 --- a/builtin/remote-ext.c +++ b/builtin/remote-ext.c @@ -2,6 +2,7 @@ #include "transport.h" #include "run-command.h" #include "pkt-line.h" +#include "config.h" static const char usage_msg[] = "git remote-ext <remote> <url>"; @@ -198,5 +199,6 @@ int cmd_remote_ext(int argc, const char **argv, const char *prefix) if (argc != 3) usage(usage_msg); + git_config(git_default_config, NULL); return command_loop(argv[2]); } diff --git a/builtin/remote.c b/builtin/remote.c index 7876db1c20d317..2273cce813aac3 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1611,6 +1611,7 @@ int cmd_remote(int argc, const char **argv, const char *prefix) }; int result; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, PARSE_OPT_STOP_AT_NON_OPTION); diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 0f09bbbf65a4bc..12ceb901f010f0 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -423,6 +423,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) struct option *opts = NULL; int onb = 0, osz = 0, unb = 0, usz = 0; + git_config(git_default_config, NULL); strbuf_addstr(&parsed, "set --"); argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, PARSE_OPT_KEEP_DASHDASH); diff --git a/builtin/show-index.c b/builtin/show-index.c index a6e678809efecf..a0a62f1ad69b67 100644 --- a/builtin/show-index.c +++ b/builtin/show-index.c @@ -1,6 +1,7 @@ #include "builtin.h" #include "cache.h" #include "pack.h" +#include "config.h" static const char show_index_usage[] = "git show-index"; @@ -14,6 +15,7 @@ int cmd_show_index(int argc, const char **argv, const char *prefix) if (argc != 1) usage(show_index_usage); + git_config(git_default_config, NULL); if (fread(top_index, 2 * 4, 1, stdin) != 1) die("unable to read header"); if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) { diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 2f13f1316fadc2..df4fae0c5895c0 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -6,6 +6,7 @@ #include "tag.h" #include "string-list.h" #include "parse-options.h" +#include "config.h" static const char * const show_ref_usage[] = { N_("git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference] [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [<pattern>...]"), @@ -181,6 +182,7 @@ static const struct option show_ref_options[] = { int cmd_show_ref(int argc, const char **argv, const char *prefix) { + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, show_ref_options, show_ref_usage, 0); diff --git a/builtin/stripspace.c b/builtin/stripspace.c index bdf032886912bb..f4b867efecac3e 100644 --- a/builtin/stripspace.c +++ b/builtin/stripspace.c @@ -45,10 +45,9 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix) if (argc) usage_with_options(stripspace_usage, options); - if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) { + if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) setup_git_directory_gently(NULL); - git_config(git_default_config, NULL); - } + git_config(git_default_config, NULL); if (strbuf_read(&buf, 0, 1024) < 0) die_errno("could not read the input"); diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 5e6f2db4cdbcf9..fd567dd2dfd293 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2036,6 +2036,7 @@ static struct cmd_struct commands[] = { int cmd_submodule__helper(int argc, const char **argv, const char *prefix) { int i; + git_config(git_default_config, NULL); if (argc < 2 || !strcmp(argv[1], "-h")) usage("git submodule--helper <command>"); diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c index 25d91163563614..363bb908fd36a2 100644 --- a/builtin/upload-archive.c +++ b/builtin/upload-archive.c @@ -8,6 +8,7 @@ #include "sideband.h" #include "run-command.h" #include "argv-array.h" +#include "config.h" static const char upload_archive_usage[] = "git upload-archive <repo>"; @@ -28,6 +29,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix) if (!enter_repo(argv[1], 0)) die("'%s' does not appear to be a git repository", argv[1]); + git_config(git_default_config, NULL); /* put received options in sent_argv[] */ argv_array_push(&sent_argv, "git-upload-archive"); for (;;) { @@ -77,6 +79,7 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix) { struct child_process writer = { argv }; + git_config(git_default_config, NULL); if (argc == 2 && !strcmp(argv[1], "-h")) usage(upload_archive_usage); diff --git a/credential-store.c b/credential-store.c index ac295420dd0d03..fbbdb006685dd3 100644 --- a/credential-store.c +++ b/credential-store.c @@ -3,6 +3,7 @@ #include "credential.h" #include "string-list.h" #include "parse-options.h" +#include "config.h" static struct lock_file credential_lock; @@ -160,6 +161,8 @@ int cmd_main(int argc, const char **argv) umask(077); + git_config(git_default_config, NULL); + argc = parse_options(argc, (const char **)argv, NULL, options, usage, 0); if (argc != 1) usage_with_options(usage, options); diff --git a/http-backend.c b/http-backend.c index 458642ef72b879..be87e52435535a 100644 --- a/http-backend.c +++ b/http-backend.c @@ -777,6 +777,7 @@ int cmd_main(int argc, const char **argv) setup_path(); if (!enter_repo(dir, 0)) not_found(&hdr, "Not a git repository: '%s'", dir); + git_config(git_default_config, NULL); if (!getenv("GIT_HTTP_EXPORT_ALL") && access("git-daemon-export-ok", F_OK) ) not_found(&hdr, "Repository not exported: '%s'", dir); diff --git a/refs.c b/refs.c index de81c7be7ca8d3..c52ae6984b3ede 100644 --- a/refs.c +++ b/refs.c @@ -1258,7 +1258,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti } string_list_append(hide_refs, ref); } - return 0; + return git_default_config(var, value, NULL); } int ref_is_hidden(const char *refname, const char *refname_full) From 2a912b04d48e655978a0050a9a30757782134a2e Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Wed, 1 Nov 2017 15:05:44 -0400 Subject: [PATCH 349/406] dir.c: make add_excludes aware of fscache during status Teach read_directory_recursive() and add_excludes() to be aware of optional fscache and avoid trying to open() and fstat() non-existant ".gitignore" files in every directory in the worktree. The current code in add_excludes() calls open() and then fstat() for a ".gitignore" file in each directory present in the worktree. Change that when fscache is enabled to call lstat() first and if present, call open(). This seems backwards because both lstat needs to do more work than fstat. But when fscache is enabled, fscache will already know if the .gitignore file exists and can completely avoid the IO calls. This works because of the lstat diversion to mingw_lstat when fscache is enabled. This reduced status times on a 350K file enlistment of the Windows repo on a NVMe SSD by 0.25 seconds. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- compat/win32/fscache.c | 5 +++++ compat/win32/fscache.h | 3 +++ dir.c | 27 +++++++++++++++++++++------ git-compat-util.h | 4 ++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 70435df680ddea..5f9516f53298db 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -8,6 +8,11 @@ static volatile long enabled; static struct hashmap map; static CRITICAL_SECTION mutex; +int fscache_is_enabled(void) +{ + return enabled; +} + /* * An entry in the file system cache. Used for both entire directory listings * and file entries. diff --git a/compat/win32/fscache.h b/compat/win32/fscache.h index ed518b422d705e..9a21fd5709c5bc 100644 --- a/compat/win32/fscache.h +++ b/compat/win32/fscache.h @@ -4,6 +4,9 @@ int fscache_enable(int enable); #define enable_fscache(x) fscache_enable(x) +int fscache_is_enabled(void); +#define is_fscache_enabled() (fscache_is_enabled()) + DIR *fscache_opendir(const char *dir); int fscache_lstat(const char *file_name, struct stat *buf); diff --git a/dir.c b/dir.c index aceb0d48692b7d..0d252ee2b17efc 100644 --- a/dir.c +++ b/dir.c @@ -817,12 +817,27 @@ static int add_excludes(const char *fname, const char *base, int baselen, size_t size = 0; char *buf; - fd = open(fname, O_RDONLY); - if (fd < 0 || fstat(fd, &st) < 0) { - if (fd < 0) - warn_on_fopen_errors(fname); - else - close(fd); + if (is_fscache_enabled()) { + if (lstat(fname, &st) < 0) { + fd = -1; + } else { + fd = open(fname, O_RDONLY); + if (fd < 0) + warn_on_fopen_errors(fname); + } + } else { + fd = open(fname, O_RDONLY); + if (fd < 0 || fstat(fd, &st) < 0) { + if (fd < 0) + warn_on_fopen_errors(fname); + else { + close(fd); + fd = -1; + } + } + } + + if (fd < 0) { if (!istate) return -1; r = read_skip_worktree_file_from_index(istate, fname, diff --git a/git-compat-util.h b/git-compat-util.h index 8f18651a78ffab..ae6fce306e5a7a 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1255,6 +1255,10 @@ static inline int is_missing_file_error(int errno_) #define enable_fscache(x) /* noop */ #endif +#ifndef is_fscache_enabled +#define is_fscache_enabled() (0) +#endif + extern int cmd_main(int, const char **); /* From c81f1d34b1dae5cbcf4d8386eb6cc90269a46a6b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 23 Nov 2016 20:59:44 +0100 Subject: [PATCH 350/406] Export the preload_index() function The purpose of this function is to stat() the files listed in the index in a multi-threaded fashion. It is called directly after reading the index in the read_index_preloaded() function. However, in some cases we may want to separate the index reading from the preloading step, e.g. in builtin/add.c, where we need to load the index before we parse the pathspecs (which needs to error out if one of the pathspecs refers to a path within a submodule, for which the index must have been read already), and only then will we want to preload, possibly limited by the just-parsed pathspecs. So let's just export that function to allow calling it separately. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- cache.h | 1 + preload-index.c | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cache.h b/cache.h index f49d9fbac8c8ac..c0319669edfc73 100644 --- a/cache.h +++ b/cache.h @@ -659,6 +659,7 @@ extern int daemonize(void); /* Initialize and use the cache information */ struct lock_file; extern int read_index(struct index_state *); +extern void preload_index(struct index_state *, const struct pathspec *pathspec); extern int read_index_preload(struct index_state *, const struct pathspec *pathspec); extern int do_read_index(struct index_state *istate, const char *path, int must_exist); /* for testting only! */ diff --git a/preload-index.c b/preload-index.c index 9c22a0705f5715..6857573ce6de0a 100644 --- a/preload-index.c +++ b/preload-index.c @@ -7,8 +7,7 @@ #include "fsmonitor.h" #ifdef NO_PTHREADS -static void preload_index(struct index_state *index, - const struct pathspec *pathspec) +void preload_index(struct index_state *index, const struct pathspec *pathspec) { ; /* nothing */ } @@ -73,8 +72,7 @@ static void *preload_thread(void *_data) return NULL; } -static void preload_index(struct index_state *index, - const struct pathspec *pathspec) +void preload_index(struct index_state *index, const struct pathspec *pathspec) { int threads, i, work, offset; struct thread_data data[MAX_PARALLEL]; From b119764daa18fa1d875690a910212e4137121a9d Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Wed, 20 Dec 2017 10:43:41 -0500 Subject: [PATCH 351/406] fscache: make fscache_enabled() public Make fscache_enabled() function public rather than static. Remove unneeded fscache_is_enabled() function. Change is_fscache_enabled() macro to call fscache_enabled(). is_fscache_enabled() now takes a pathname so that the answer is more precise and mean "is fscache enabled for this pathname", since fscache only stores repo-relative paths and not absolute paths, we can avoid attempting lookups for absolute paths. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- compat/win32/fscache.c | 7 +------ compat/win32/fscache.h | 4 ++-- dir.c | 2 +- git-compat-util.h | 2 +- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 5f9516f53298db..97e68a36a10f67 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -8,11 +8,6 @@ static volatile long enabled; static struct hashmap map; static CRITICAL_SECTION mutex; -int fscache_is_enabled(void) -{ - return enabled; -} - /* * An entry in the file system cache. Used for both entire directory listings * and file entries. @@ -247,7 +242,7 @@ static void fscache_clear(void) /* * Checks if the cache is enabled for the given path. */ -static inline int fscache_enabled(const char *path) +int fscache_enabled(const char *path) { return enabled > 0 && !is_absolute_path(path); } diff --git a/compat/win32/fscache.h b/compat/win32/fscache.h index 9a21fd5709c5bc..660ada053b4309 100644 --- a/compat/win32/fscache.h +++ b/compat/win32/fscache.h @@ -4,8 +4,8 @@ int fscache_enable(int enable); #define enable_fscache(x) fscache_enable(x) -int fscache_is_enabled(void); -#define is_fscache_enabled() (fscache_is_enabled()) +int fscache_enabled(const char *path); +#define is_fscache_enabled(path) fscache_enabled(path) DIR *fscache_opendir(const char *dir); int fscache_lstat(const char *file_name, struct stat *buf); diff --git a/dir.c b/dir.c index 0d252ee2b17efc..e31f1f062937df 100644 --- a/dir.c +++ b/dir.c @@ -817,7 +817,7 @@ static int add_excludes(const char *fname, const char *base, int baselen, size_t size = 0; char *buf; - if (is_fscache_enabled()) { + if (is_fscache_enabled(fname)) { if (lstat(fname, &st) < 0) { fd = -1; } else { diff --git a/git-compat-util.h b/git-compat-util.h index ae6fce306e5a7a..b81d569f626e70 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1256,7 +1256,7 @@ static inline int is_missing_file_error(int errno_) #endif #ifndef is_fscache_enabled -#define is_fscache_enabled() (0) +#define is_fscache_enabled(path) (0) #endif extern int cmd_main(int, const char **); From d308414ccaf1de755b263e252d8bab51fedb1215 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Tue, 22 Nov 2016 11:26:38 -0500 Subject: [PATCH 352/406] add: use preload-index and fscache for performance Teach "add" to use preload-index and fscache features to improve performance on very large repositories. During an "add", a call is made to run_diff_files() which calls check_remove() for each index-entry. This calls lstat(). On Windows, the fscache code intercepts the lstat() calls and builds a private cache using the FindFirst/FindNext routines, which are much faster. Somewhat independent of this, is the preload-index code which distributes some of the start-up costs across multiple threads. We need to keep the call to read_cache() before parsing the pathspecs (and hence cannot use the pathspecs to limit any preload) because parse_pathspec() is using the index to determine whether a pathspec is, in fact, in a submodule. If we would not read the index first, parse_pathspec() would not error out on a path that is inside a submodule, and t7400-submodule-basic.sh would fail with not ok 47 - do not add files from a submodule We still want the nice preload performance boost, though, so we simply call read_cache_preload(&pathspecs) after parsing the pathspecs. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- builtin/add.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/builtin/add.c b/builtin/add.c index 72baa72bc44b2c..0ff2cd9d49f668 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -461,6 +461,10 @@ int cmd_add(int argc, const char **argv, const char *prefix) die_path_inside_submodule(&the_index, &pathspec); + enable_fscache(1); + /* We do not really re-read the index but update the up-to-date flags */ + preload_index(&the_index, &pathspec); + if (add_new_files) { int baselen; From e452052fea6db3d83f0c078062009e37d74e2979 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler <jeffhost@microsoft.com> Date: Wed, 20 Dec 2017 11:19:27 -0500 Subject: [PATCH 353/406] dir.c: regression fix for add_excludes with fscache Fix regression described in: https://github.com/git-for-windows/git/issues/1392 which was introduced in: https://github.com/git-for-windows/git/commit/b2353379bba414e6c00dde913497cc9c827366f2 Problem Symptoms ================ When the user has a .gitignore file that is a symlink, the fscache optimization introduced above caused the stat-data from the symlink, rather that of the target file, to be returned. Later when the ignore file was read, the buffer length did not match the stat.st_size field and we called die("cannot use <path> as an exclude file") Optimization Rationale ====================== The above optimization calls lstat() before open() primarily to ask fscache if the file exists. It gets the current stat-data as a side effect essentially for free (since we already have it in memory). If the file does not exist, it does not need to call open(). And since very few directories have .gitignore files, we can greatly reduce time spent in the filesystem. Discussion of Fix ================= The above optimization calls lstat() rather than stat() because the fscache only intercepts lstat() calls. Calls to stat() stay directed to the mingw_stat() completly bypassing fscache. Furthermore, calls to mingw_stat() always call {open, fstat, close} so that symlinks are properly dereferenced, which adds *additional* open/close calls on top of what the original code in dir.c is doing. Since the problem only manifests for symlinks, we add code to overwrite the stat-data when the path is a symlink. This preserves the effect of the performance gains provided by the fscache in the normal case. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> --- dir.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/dir.c b/dir.c index e31f1f062937df..9fe2118956205f 100644 --- a/dir.c +++ b/dir.c @@ -817,6 +817,29 @@ static int add_excludes(const char *fname, const char *base, int baselen, size_t size = 0; char *buf; + /* + * A performance optimization for status. + * + * During a status scan, git looks in each directory for a .gitignore + * file before scanning the directory. Since .gitignore files are not + * that common, we can waste a lot of time looking for files that are + * not there. Fortunately, the fscache already knows if the directory + * contains a .gitignore file, since it has already read the directory + * and it already has the stat-data. + * + * If the fscache is enabled, use the fscache-lstat() interlude to see + * if the file exists (in the fscache hash maps) before trying to open() + * it. + * + * This causes problem when the .gitignore file is a symlink, because + * we call lstat() rather than stat() on the symlnk and the resulting + * stat-data is for the symlink itself rather than the target file. + * We CANNOT use stat() here because the fscache DOES NOT install an + * interlude for stat() and mingw_stat() always calls "open-fstat-close" + * on the file and defeats the purpose of the optimization here. Since + * symlinks are even more rare than .gitignore files, we force a fstat() + * after our open() to get stat-data for the target file. + */ if (is_fscache_enabled(fname)) { if (lstat(fname, &st) < 0) { fd = -1; @@ -824,6 +847,11 @@ static int add_excludes(const char *fname, const char *base, int baselen, fd = open(fname, O_RDONLY); if (fd < 0) warn_on_fopen_errors(fname); + else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) { + warn_on_fopen_errors(fname); + close(fd); + fd = -1; + } } } else { fd = open(fname, O_RDONLY); From 4d23bcdc9b30637e7007c20b098040e9cad0433d Mon Sep 17 00:00:00 2001 From: Takuto Ikuta <tikuta@chromium.org> Date: Wed, 22 Nov 2017 20:39:38 +0900 Subject: [PATCH 354/406] fetch-pack.c: enable fscache for stats under .git/objects When I do git fetch, git call file stats under .git/objects for each refs. This takes time when there are many refs. By enabling fscache, git takes file stats by directory traversing and that improved the speed of fetch-pack for repository having large number of refs. In my windows workstation, this improves the time of `git fetch` for chromium repository like below. I took stats 3 times. * With this patch TotalSeconds: 9.9825165 TotalSeconds: 9.1862075 TotalSeconds: 10.1956256 Avg: 9.78811653333333 * Without this patch TotalSeconds: 15.8406702 TotalSeconds: 15.6248053 TotalSeconds: 15.2085938 Avg: 15.5580231 Signed-off-by: Takuto Ikuta <tikuta@chromium.org> --- fetch-pack.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fetch-pack.c b/fetch-pack.c index 88a078e9befd28..43e0945da18021 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -678,6 +678,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, save_commit_buffer = 0; + enable_fscache(1); for (ref = *refs; ref; ref = ref->next) { struct object *o; unsigned int flags = OBJECT_INFO_QUICK; @@ -707,6 +708,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, cutoff = commit->date; } } + enable_fscache(0); oidset_clear(&loose_oid_set); From 674eaad45adf534d5554cf5c9002ec5edaa622b5 Mon Sep 17 00:00:00 2001 From: Takuto Ikuta <tikuta@chromium.org> Date: Tue, 30 Jan 2018 22:42:58 +0900 Subject: [PATCH 355/406] checkout.c: enable fscache for checkout again This is retry of #1419. I added flush_fscache macro to flush cached stats after disk writing with tests for regression reported in #1438 and #1442. git checkout checks each file path in sorted order, so cache flushing does not make performance worse unless we have large number of modified files in a directory containing many files. Using chromium repository, I tested `git checkout .` performance when I delete 10 files in different directories. With this patch: TotalSeconds: 4.307272 TotalSeconds: 4.4863595 TotalSeconds: 4.2975562 Avg: 4.36372923333333 Without this patch: TotalSeconds: 20.9705431 TotalSeconds: 22.4867685 TotalSeconds: 18.8968292 Avg: 20.7847136 I confirmed this patch passed all tests in t/ with core_fscache=1. Signed-off-by: Takuto Ikuta <tikuta@chromium.org> --- builtin/checkout.c | 2 ++ compat/win32/fscache.c | 12 ++++++++++++ compat/win32/fscache.h | 3 +++ entry.c | 3 +++ git-compat-util.h | 4 ++++ t/t7201-co.sh | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+) diff --git a/builtin/checkout.c b/builtin/checkout.c index 29ef50013dccbd..07cc016e5cc3bd 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -360,6 +360,7 @@ static int checkout_paths(const struct checkout_opts *opts, state.istate = &the_index; enable_delayed_checkout(&state); + enable_fscache(1); for (pos = 0; pos < active_nr; pos++) { struct cache_entry *ce = active_cache[pos]; if (ce->ce_flags & CE_MATCHED) { @@ -374,6 +375,7 @@ static int checkout_paths(const struct checkout_opts *opts, pos = skip_same_name(ce, pos) - 1; } } + enable_fscache(0); errs |= finish_delayed_checkout(&state); if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 97e68a36a10f67..4206713b7cddd0 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -379,6 +379,18 @@ int fscache_enable(int enable) return result; } +/* + * Flush cached stats result when fscache is enabled. + */ +void fscache_flush(void) +{ + if (enabled) { + EnterCriticalSection(&mutex); + fscache_clear(); + LeaveCriticalSection(&mutex); + } +} + /* * Lstat replacement, uses the cache if enabled, otherwise redirects to * mingw_lstat. diff --git a/compat/win32/fscache.h b/compat/win32/fscache.h index 660ada053b4309..2f06f8df97dcd0 100644 --- a/compat/win32/fscache.h +++ b/compat/win32/fscache.h @@ -7,6 +7,9 @@ int fscache_enable(int enable); int fscache_enabled(const char *path); #define is_fscache_enabled(path) fscache_enabled(path) +void fscache_flush(void); +#define flush_fscache() fscache_flush() + DIR *fscache_opendir(const char *dir); int fscache_lstat(const char *file_name, struct stat *buf); diff --git a/entry.c b/entry.c index 2a2ab6c839490a..0712743a43d0a7 100644 --- a/entry.c +++ b/entry.c @@ -367,6 +367,9 @@ static int write_entry(struct cache_entry *ce, } finish: + /* Flush cached lstat in fscache after writing to disk. */ + flush_fscache(); + if (state->refresh_cache) { assert(state->istate); if (!fstat_done) diff --git a/git-compat-util.h b/git-compat-util.h index b81d569f626e70..6f813e07685c8e 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1259,6 +1259,10 @@ static inline int is_missing_file_error(int errno_) #define is_fscache_enabled(path) (0) #endif +#ifndef flush_fscache +#define flush_fscache() /* noop */ +#endif + extern int cmd_main(int, const char **); /* diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 324933acfe9415..0b499a4ebecc15 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -32,6 +32,42 @@ fill () { } +test_expect_success MINGW 'fscache flush cache' ' + + git init fscache-test && + cd fscache-test && + git config core.fscache 1 && + echo A > test.txt && + git add test.txt && + git commit -m A && + echo B >> test.txt && + git checkout . && + test -z "$(git status -s)" && + echo A > expect.txt && + test_cmp expect.txt test.txt && + cd .. && + rm -rf fscache-test +' + +test_expect_success MINGW 'fscache flush cache dir' ' + + git init fscache-test && + cd fscache-test && + git config core.fscache 1 && + echo A > test.txt && + git add test.txt && + git commit -m A && + rm test.txt && + mkdir test.txt && + touch test.txt/test.txt && + git checkout . && + test -z "$(git status -s)" && + echo A > expect.txt && + test_cmp expect.txt test.txt && + cd .. && + rm -rf fscache-test +' + test_expect_success setup ' fill x y z > same && From e88f29b030d11be223a8986c818472c48d35aafc Mon Sep 17 00:00:00 2001 From: JiSeop Moon <zcube@zcube.kr> Date: Mon, 23 Apr 2018 22:30:18 +0900 Subject: [PATCH 356/406] mingw: introduce code to detect whether we're inside a Windows container This will come in handy in the next commit. Signed-off-by: JiSeop Moon <zcube@zcube.kr> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 32 ++++++++++++++++++++++++++++++++ compat/mingw.h | 5 +++++ 2 files changed, 37 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 7fe6b201d25daa..cf2b4085d9b365 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3115,3 +3115,35 @@ const char *program_data_config(void) } return *path.buf ? path.buf : NULL; } + +/* + * Based on https://stackoverflow.com/questions/43002803 + * + * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc] + * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100" + * "ErrorControl"=dword:00000001 + * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00, + * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00, + * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00, + * 65,00,00,00 + * "Start"=dword:00000002 + * "Type"=dword:00000010 + * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101" + * "ObjectName"="LocalSystem" + * "ServiceSidType"=dword:00000001 + */ +int is_inside_windows_container(void) +{ + static int inside_container = -1; /* -1 uninitialized */ + const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc"; + HKEY handle = NULL; + + if (inside_container != -1) + return inside_container; + + inside_container = ERROR_SUCCESS == + RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle); + RegCloseKey(handle); + + return inside_container; +} diff --git a/compat/mingw.h b/compat/mingw.h index 2fb620544ae135..848028af1058d7 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -695,3 +695,8 @@ int main(int argc, const char **argv); * Used by Pthread API implementation for Windows */ extern int err_win_to_posix(DWORD winerr); + +/* + * Check current process is inside Windows Container. + */ +extern int is_inside_windows_container(void); From 6d2bd51d48149736742e8466ab29cb3e44bd96b2 Mon Sep 17 00:00:00 2001 From: JiSeop Moon <zcube@zcube.kr> Date: Mon, 23 Apr 2018 22:31:42 +0200 Subject: [PATCH 357/406] mingw: when running in a Windows container, try to rename() harder It is a known issue that a rename() can fail with an "Access denied" error at times, when copying followed by deleting the original file works. Let's just fall back to that behavior. Signed-off-by: JiSeop Moon <zcube@zcube.kr> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index cf2b4085d9b365..1345fe5045b451 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2152,6 +2152,13 @@ int mingw_rename(const char *pold, const char *pnew) return 0; gle = GetLastError(); + if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) { + /* Fall back to copy to destination & remove source */ + if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold)) + return 0; + gle = GetLastError(); + } + /* revert file attributes on failure */ if (attrs != INVALID_FILE_ATTRIBUTES) SetFileAttributesW(wpnew, attrs); From efc27cc6336661e383159760d6bf625d67c78c27 Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Fri, 7 Sep 2018 11:39:57 -0400 Subject: [PATCH 358/406] Enable the filesystem cache (fscache) in refresh_index(). On file systems that support it, this can dramatically speed up operations like add, commit, describe, rebase, reset, rm that would otherwise have to lstat() every file to "re-match" the stat information in the index to that of the file system. On a synthetic repo with 1M files, "git reset" dropped from 52.02 seconds to 14.42 seconds for a savings of 72%. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- read-cache.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/read-cache.c b/read-cache.c index 7b1354d7590a70..18b6fb42144ecb 100644 --- a/read-cache.c +++ b/read-cache.c @@ -1483,6 +1483,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, typechange_fmt = (in_porcelain ? "T\t%s\n" : "%s needs update\n"); added_fmt = (in_porcelain ? "A\t%s\n" : "%s needs update\n"); unmerged_fmt = (in_porcelain ? "U\t%s\n" : "%s: needs merge\n"); + enable_fscache(1); for (i = 0; i < istate->cache_nr; i++) { struct cache_entry *ce, *new_entry; int cache_errno = 0; @@ -1547,6 +1548,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, replace_index_entry(istate, i, new_entry); } + enable_fscache(0); trace_performance_since(start, "refresh index"); return has_errors; } From 32b36aa0d729adb50a98808c61a3355032457bd4 Mon Sep 17 00:00:00 2001 From: JiSeop Moon <zcube@zcube.kr> Date: Mon, 23 Apr 2018 22:35:26 +0200 Subject: [PATCH 359/406] mingw: move the file_attr_to_st_mode() function definition In preparation for making this function a bit more complicated (to allow for special-casing the `ContainerMappedDirectories` in Windows containers, which look like a symbolic link, but are not), let's move it out of the header. Signed-off-by: JiSeop Moon <zcube@zcube.kr> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 14 ++++++++++++++ compat/win32.h | 14 +------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 1345fe5045b451..8f5e40339a9db1 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3154,3 +3154,17 @@ int is_inside_windows_container(void) return inside_container; } + +int file_attr_to_st_mode (DWORD attr, DWORD tag) +{ + int fMode = S_IREAD; + if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK) + fMode |= S_IFLNK; + else if (attr & FILE_ATTRIBUTE_DIRECTORY) + fMode |= S_IFDIR; + else + fMode |= S_IFREG; + if (!(attr & FILE_ATTRIBUTE_READONLY)) + fMode |= S_IWRITE; + return fMode; +} diff --git a/compat/win32.h b/compat/win32.h index 671bcc81f93351..52169ae19f4371 100644 --- a/compat/win32.h +++ b/compat/win32.h @@ -6,19 +6,7 @@ #include <windows.h> #endif -static inline int file_attr_to_st_mode (DWORD attr, DWORD tag) -{ - int fMode = S_IREAD; - if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK) - fMode |= S_IFLNK; - else if (attr & FILE_ATTRIBUTE_DIRECTORY) - fMode |= S_IFDIR; - else - fMode |= S_IFREG; - if (!(attr & FILE_ATTRIBUTE_READONLY)) - fMode |= S_IWRITE; - return fMode; -} +extern int file_attr_to_st_mode (DWORD attr, DWORD tag); static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata) { From 4d78d4dcf241b6c672fff4816964b14be1253c5a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 23 Apr 2018 23:20:00 +0200 Subject: [PATCH 360/406] mingw: Windows Docker volumes are *not* symbolic links ... even if they may look like them. As looking up the target of the "symbolic link" (just to see whether it starts with `/ContainerMappedDirectories/`) is pretty expensive, we do it when we can be *really* sure that there is a possibility that this might be the case. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: JiSeop Moon <zcube@zcube.kr> --- compat/mingw.c | 25 +++++++++++++++++++------ compat/win32.h | 2 +- compat/win32/fscache.c | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 8f5e40339a9db1..d0c5e9e511fe83 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -851,7 +851,7 @@ int mingw_lstat(const char *file_name, struct stat *buf) buf->st_uid = 0; buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, - findbuf.dwReserved0); + findbuf.dwReserved0, file_name); buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH : fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ @@ -902,7 +902,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) buf->st_gid = 0; buf->st_uid = 0; buf->st_nlink = 1; - buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0); + buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL); buf->st_size = fdata.nFileSizeLow | (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ @@ -3155,12 +3155,25 @@ int is_inside_windows_container(void) return inside_container; } -int file_attr_to_st_mode (DWORD attr, DWORD tag) +int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path) { int fMode = S_IREAD; - if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK) - fMode |= S_IFLNK; - else if (attr & FILE_ATTRIBUTE_DIRECTORY) + if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && + tag == IO_REPARSE_TAG_SYMLINK) { + int flag = S_IFLNK; + char buf[MAX_LONG_PATH]; + + /* + * Windows containers' mapped volumes are marked as reparse + * points and look like symbolic links, but they are not. + */ + if (path && is_inside_windows_container() && + readlink(path, buf, sizeof(buf)) > 27 && + starts_with(buf, "/ContainerMappedDirectories/")) + flag = S_IFDIR; + + fMode |= flag; + } else if (attr & FILE_ATTRIBUTE_DIRECTORY) fMode |= S_IFDIR; else fMode |= S_IFREG; diff --git a/compat/win32.h b/compat/win32.h index 52169ae19f4371..299f01bdf0f5a4 100644 --- a/compat/win32.h +++ b/compat/win32.h @@ -6,7 +6,7 @@ #include <windows.h> #endif -extern int file_attr_to_st_mode (DWORD attr, DWORD tag); +extern int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path); static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata) { diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 345d7b226b8ad1..ab08a961520d8e 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -149,8 +149,21 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, fse = fsentry_alloc(list, buf, len); + if (fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK && + sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 && + is_inside_windows_container()) { + size_t off = 0; + if (list) { + memcpy(buf, list->name, list->len); + buf[list->len] = '/'; + off = list->len + 1; + } + memcpy(buf + off, fse->name, fse->len); + buf[off + fse->len] = '\0'; + } + fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes, - fdata->dwReserved0); + fdata->dwReserved0, buf); fse->st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH : fdata->nFileSizeLow | (((off_t) fdata->nFileSizeHigh) << 32); filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim)); From d741aa1987979142dd3143a9a6edc078e65c3dc1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 4 Sep 2017 11:59:45 +0200 Subject: [PATCH 361/406] mingw: change core.fsyncObjectFiles = 1 by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From the documentation of said setting: This boolean will enable fsync() when writing object files. This is a total waste of time and effort on a filesystem that orders data writes properly, but can be useful for filesystems that do not use journalling (traditional UNIX filesystems) or that only journal metadata and not file contents (OS X’s HFS+, or Linux ext3 with "data=writeback"). The most common file system on Windows (NTFS) does not guarantee that order, therefore a sudden loss of power (or any other event causing an unclean shutdown) would cause corrupt files (i.e. files filled with NULs). Therefore we need to change the default. Note that the documentation makes it sound as if this causes really bad performance. In reality, writing loose objects is something that is done only rarely, and only a handful of files at a time. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 1 + 1 file changed, 1 insertion(+) diff --git a/compat/mingw.c b/compat/mingw.c index 970e5794076890..f6a5ffd7da3d39 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3035,6 +3035,7 @@ int wmain(int argc, const wchar_t **wargv) maybe_redirect_std_handles(); adjust_symlink_flags(); + fsync_object_files = 1; /* determine size of argv and environ conversion buffer */ maxlen = wcslen(wargv[0]); From 5c2b3a676b9a75fb5b0e7274fc76ae7107547c86 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 17 May 2017 17:05:09 +0200 Subject: [PATCH 362/406] mingw: kill child processes in a gentler way The TerminateProcess() function does not actually leave the child processes any chance to perform any cleanup operations. This is bad insofar as Git itself expects its signal handlers to run. A symptom is e.g. a left-behind .lock file that would not be left behind if the same operation was run, say, on Linux. To remedy this situation, we use an obscure trick: we inject a thread into the process that needs to be killed and to let that thread run the ExitProcess() function with the desired exit status. Thanks J Wyman for describing this trick. The advantage is that the ExitProcess() function lets the atexit handlers run. While this is still different from what Git expects (i.e. running a signal handler), in practice Git sets up signal handlers and atexit handlers that call the same code to clean up after itself. In case that the gentle method to terminate the process failed, we still fall back to calling TerminateProcess(), but in that case we now also make sure that processes spawned by the spawned process are terminated; TerminateProcess() does not give the spawned process a chance to do so itself. Please note that this change only affects how Git for Windows tries to terminate processes spawned by Git's own executables. Third-party software that *calls* Git and wants to terminate it *still* need to make sure to imitate this gentle method, otherwise this patch will not have any effect. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 29 +++++-- compat/win32/exit-process.h | 164 ++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 compat/win32/exit-process.h diff --git a/compat/mingw.c b/compat/mingw.c index 970e5794076890..781a50853b429c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -6,6 +6,7 @@ #include "../strbuf.h" #include "../run-command.h" #include "../cache.h" +#include "win32/exit-process.h" #include "win32/lazyload.h" #include "../config.h" @@ -1742,16 +1743,28 @@ int mingw_execvp(const char *cmd, char *const *argv) int mingw_kill(pid_t pid, int sig) { if (pid > 0 && sig == SIGTERM) { - HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); - - if (TerminateProcess(h, -1)) { + HANDLE h = OpenProcess(PROCESS_CREATE_THREAD | + PROCESS_QUERY_INFORMATION | + PROCESS_VM_OPERATION | PROCESS_VM_WRITE | + PROCESS_VM_READ | PROCESS_TERMINATE, + FALSE, pid); + int ret; + + if (h) + ret = exit_process(h, 128 + sig); + else { + h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + if (!h) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + ret = terminate_process_tree(h, 128 + sig); + } + if (ret) { + errno = err_win_to_posix(GetLastError()); CloseHandle(h); - return 0; } - - errno = err_win_to_posix(GetLastError()); - CloseHandle(h); - return -1; + return ret; } else if (pid > 0 && sig == 0) { HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); if (h) { diff --git a/compat/win32/exit-process.h b/compat/win32/exit-process.h new file mode 100644 index 00000000000000..7ff776b8bc413f --- /dev/null +++ b/compat/win32/exit-process.h @@ -0,0 +1,164 @@ +#ifndef EXIT_PROCESS_H +#define EXIT_PROCESS_H + +/* + * This file contains functions to terminate a Win32 process, as gently as + * possible. + * + * At first, we will attempt to inject a thread that calls ExitProcess(). If + * that fails, we will fall back to terminating the entire process tree. + * + * For simplicity, these functions are marked as file-local. + */ + +#include <tlhelp32.h> + +/* + * Terminates the process corresponding to the process ID and all of its + * directly and indirectly spawned subprocesses. + * + * This way of terminating the processes is not gentle: the processes get + * no chance of cleaning up after themselves (closing file handles, removing + * .lock files, terminating spawned processes (if any), etc). + */ +static int terminate_process_tree(HANDLE main_process, int exit_status) +{ + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + PROCESSENTRY32 entry; + DWORD pids[16384]; + int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0; + pid_t pid = GetProcessId(main_process); + + pids[0] = (DWORD)pid; + len = 1; + + /* + * Even if Process32First()/Process32Next() seem to traverse the + * processes in topological order (i.e. parent processes before + * child processes), there is nothing in the Win32 API documentation + * suggesting that this is guaranteed. + * + * Therefore, run through them at least twice and stop when no more + * process IDs were added to the list. + */ + for (;;) { + int orig_len = len; + + memset(&entry, 0, sizeof(entry)); + entry.dwSize = sizeof(entry); + + if (!Process32First(snapshot, &entry)) + break; + + do { + for (i = len - 1; i >= 0; i--) { + if (pids[i] == entry.th32ProcessID) + break; + if (pids[i] == entry.th32ParentProcessID) + pids[len++] = entry.th32ProcessID; + } + } while (len < max_len && Process32Next(snapshot, &entry)); + + if (orig_len == len || len >= max_len) + break; + } + + for (i = len - 1; i > 0; i--) { + HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, pids[i]); + + if (process) { + if (!TerminateProcess(process, exit_status)) + ret = -1; + CloseHandle(process); + } + } + if (!TerminateProcess(main_process, exit_status)) + ret = -1; + CloseHandle(main_process); + + return ret; +} + +/** + * Determine whether a process runs in the same architecture as the current + * one. That test is required before we assume that GetProcAddress() returns + * a valid address *for the target process*. + */ +static inline int process_architecture_matches_current(HANDLE process) +{ + static BOOL current_is_wow = -1; + BOOL is_wow; + + if (current_is_wow == -1 && + !IsWow64Process (GetCurrentProcess(), ¤t_is_wow)) + current_is_wow = -2; + if (current_is_wow == -2) + return 0; /* could not determine current process' WoW-ness */ + if (!IsWow64Process (process, &is_wow)) + return 0; /* cannot determine */ + return is_wow == current_is_wow; +} + +/** + * Inject a thread into the given process that runs ExitProcess(). + * + * Note: as kernel32.dll is loaded before any process, the other process and + * this process will have ExitProcess() at the same address. + * + * This function expects the process handle to have the access rights for + * CreateRemoteThread(): PROCESS_CREATE_THREAD, PROCESS_QUERY_INFORMATION, + * PROCESS_VM_OPERATION, PROCESS_VM_WRITE, and PROCESS_VM_READ. + * + * The idea comes from the Dr Dobb's article "A Safer Alternative to + * TerminateProcess()" by Andrew Tucker (July 1, 1999), + * http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 + * + * If this method fails, we fall back to running terminate_process_tree(). + */ +static int exit_process(HANDLE process, int exit_code) +{ + DWORD code; + + if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { + static int initialized; + static LPTHREAD_START_ROUTINE exit_process_address; + PVOID arg = (PVOID)(intptr_t)exit_code; + DWORD thread_id; + HANDLE thread = NULL; + + if (!initialized) { + HINSTANCE kernel32 = GetModuleHandle("kernel32"); + if (!kernel32) + die("BUG: cannot find kernel32"); + exit_process_address = (LPTHREAD_START_ROUTINE) + GetProcAddress(kernel32, "ExitProcess"); + initialized = 1; + } + if (!exit_process_address || + !process_architecture_matches_current(process)) + return terminate_process_tree(process, exit_code); + + thread = CreateRemoteThread(process, NULL, 0, + exit_process_address, + arg, 0, &thread_id); + if (thread) { + CloseHandle(thread); + /* + * If the process survives for 10 seconds (a completely + * arbitrary value picked from thin air), fall back to + * killing the process tree via TerminateProcess(). + */ + if (WaitForSingleObject(process, 10000) == + WAIT_OBJECT_0) { + CloseHandle(process); + return 0; + } + } + + return terminate_process_tree(process, exit_code); + } + + return 0; +} + +#endif From 129934a43aeae209d6fe57c622a211d837c9dfc6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 23 Apr 2018 00:24:29 +0200 Subject: [PATCH 363/406] mingw: really handle SIGINT Previously, we did not install any handler for Ctrl+C, but now we really want to because the MSYS2 runtime learned the trick to call the ConsoleCtrlHandler when Ctrl+C was pressed. With this, hitting Ctrl+C while `git log` is running will only terminate the Git process, but not the pager. This finally matches the behavior on Linux and on macOS. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 781a50853b429c..3d9df522560b2c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3012,7 +3012,14 @@ static void adjust_symlink_flags(void) symlink_file_flags |= 2; symlink_directory_flags |= 2; } +} +static BOOL WINAPI handle_ctrl_c(DWORD ctrl_type) +{ + if (ctrl_type != CTRL_C_EVENT) + return FALSE; /* we did not handle this */ + mingw_raise(SIGINT); + return TRUE; /* we did handle this */ } #ifdef _MSC_VER @@ -3046,6 +3053,8 @@ int wmain(int argc, const wchar_t **wargv) #endif #endif + SetConsoleCtrlHandler(handle_ctrl_c, TRUE); + maybe_redirect_std_handles(); adjust_symlink_flags(); From f28481bb7fb7284554a1deebe84117087ddb6992 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 26 Jan 2018 15:37:38 +0100 Subject: [PATCH 364/406] mingw: spawned processes need to inherit only standard handles By default, CreateProcess() does not inherit any open file handles, unless the bInheritHandles parameter is set to TRUE. Which we do need to set because we need to pass in stdin/stdout/stderr to talk to the child processes. Sadly, this means that all file handles (unless marked via O_NOINHERIT) are inherited. This lead to problems in GVFS Git, where a long-running read-object hook is used to hydrate missing objects, and depending on the circumstances, might only be called *after* Git opened a file handle. Ideally, we would not open files without O_NOINHERIT unless *really* necessary (i.e. when we want to pass the opened file handle as standard handle into a child process), but apparently it is all-too-easy to introduce incorrect open() calls: this happened, and prevented updating a file after the read-object hook was started because the hook still held a handle on said file. Happily, there is a solution: as described in the "Old New Thing" https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873 there is a way, starting with Windows Vista, that lets us define precisely which handles should be inherited by the child process. And since we bumped the minimum Windows version for use with Git for Windows to Vista with v2.10.1 (i.e. a *long* time ago), we can use this method. So let's do exactly that. We need to make sure that the list of handles to inherit does not contain duplicates; Otherwise CreateProcessW() would fail with ERROR_INVALID_ARGUMENT. While at it, stop setting errno to ENOENT unless it really is the correct value. Also, fall back to not limiting handle inheritance under certain error conditions (e.g. on Windows 7, which is a lot stricter in what handles you can specify to limit to). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 120 +++++++++++++++++++++++++++++++++++++---- t/t0061-run-command.sh | 2 +- 2 files changed, 110 insertions(+), 12 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index e5f21fefe24b8c..869765d2a84efe 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1371,8 +1371,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen const char *dir, int prepend_cmd, int fhin, int fhout, int fherr) { - STARTUPINFOW si; + static int restrict_handle_inheritance = 1; + STARTUPINFOEXW si; PROCESS_INFORMATION pi; + LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL; + HANDLE stdhandles[3]; + DWORD stdhandles_count = 0; + SIZE_T size; struct strbuf args; wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL; unsigned flags = CREATE_UNICODE_ENVIRONMENT; @@ -1408,11 +1413,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen CloseHandle(cons); } memset(&si, 0, sizeof(si)); - si.cb = sizeof(si); - si.dwFlags = STARTF_USESTDHANDLES; - si.hStdInput = winansi_get_osfhandle(fhin); - si.hStdOutput = winansi_get_osfhandle(fhout); - si.hStdError = winansi_get_osfhandle(fherr); + si.StartupInfo.cb = sizeof(si); + si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin); + si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout); + si.StartupInfo.hStdError = winansi_get_osfhandle(fherr); + + /* The list of handles cannot contain duplicates */ + if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE) + stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput; + if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE && + si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput) + stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput; + if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE && + si.StartupInfo.hStdError != si.StartupInfo.hStdInput && + si.StartupInfo.hStdError != si.StartupInfo.hStdOutput) + stdhandles[stdhandles_count++] = si.StartupInfo.hStdError; + if (stdhandles_count) + si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES; /* executables and the current directory don't support long paths */ if (*argv && !strcmp(cmd, *argv)) @@ -1446,16 +1463,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen wenvblk = make_environment_block(deltaenv); memset(&pi, 0, sizeof(pi)); - ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE, - flags, wenvblk, dir ? wdir : NULL, &si, &pi); + if (restrict_handle_inheritance && stdhandles_count && + (InitializeProcThreadAttributeList(NULL, 1, 0, &size) || + GetLastError() == ERROR_INSUFFICIENT_BUFFER) && + (attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST) + (HeapAlloc(GetProcessHeap(), 0, size))) && + InitializeProcThreadAttributeList(attr_list, 1, 0, &size) && + UpdateProcThreadAttribute(attr_list, 0, + PROC_THREAD_ATTRIBUTE_HANDLE_LIST, + stdhandles, + stdhandles_count * sizeof(HANDLE), + NULL, NULL)) { + si.lpAttributeList = attr_list; + flags |= EXTENDED_STARTUPINFO_PRESENT; + } + + ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, + stdhandles_count ? TRUE : FALSE, + flags, wenvblk, dir ? wdir : NULL, + &si.StartupInfo, &pi); + + /* + * On Windows 2008 R2, it seems that specifying certain types of handles + * (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an + * error. Rather than playing finicky and fragile games, let's just try + * to detect this situation and simply try again without restricting any + * handle inheritance. This is still better than failing to create + * processes. + */ + if (!ret && restrict_handle_inheritance && stdhandles_count) { + DWORD err = GetLastError(); + struct strbuf buf = STRBUF_INIT; + + if (err != ERROR_NO_SYSTEM_RESOURCES && + /* + * On Windows 7 and earlier, handles on pipes and character + * devices are inherited automatically, and cannot be + * specified in the thread handle list. Rather than trying + * to catch each and every corner case (and running the + * chance of *still* forgetting a few), let's just fall + * back to creating the process without trying to limit the + * handle inheritance. + */ + !(err == ERROR_INVALID_PARAMETER && + GetVersion() >> 16 < 9200) && + !getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) { + DWORD fl = 0; + int i; + + setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1); + + for (i = 0; i < stdhandles_count; i++) { + HANDLE h = stdhandles[i]; + strbuf_addf(&buf, "handle #%d: %p (type %lx, " + "handle info (%d) %lx\n", i, h, + GetFileType(h), + GetHandleInformation(h, &fl), + fl); + } + strbuf_addstr(&buf, "\nThis is a bug; please report it " + "at\nhttps://github.com/git-for-windows/" + "git/issues/new\n\n" + "To suppress this warning, please set " + "the environment variable\n\n" + "\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1" + "\n"); + } + restrict_handle_inheritance = 0; + flags &= ~EXTENDED_STARTUPINFO_PRESENT; + ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, + TRUE, flags, wenvblk, dir ? wdir : NULL, + &si.StartupInfo, &pi); + if (ret && buf.len) { + errno = err_win_to_posix(GetLastError()); + warning("failed to restrict file handles (%ld)\n\n%s", + err, buf.buf); + } + strbuf_release(&buf); + } else if (!ret) + errno = err_win_to_posix(GetLastError()); + + if (si.lpAttributeList) + DeleteProcThreadAttributeList(si.lpAttributeList); + if (attr_list) + HeapFree(GetProcessHeap(), 0, attr_list); free(wenvblk); free(wargs); - if (!ret) { - errno = ENOENT; + if (!ret) return -1; - } + CloseHandle(pi.hThread); /* diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 6f0b32d875f155..a51c2da8cc3756 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -12,7 +12,7 @@ cat >hello-script <<-EOF cat hello-script EOF -test_expect_failure MINGW 'subprocess inherits only std handles' ' +test_expect_success MINGW 'subprocess inherits only std handles' ' test-tool run-command inherited-handle ' From c7d5bbf0d3a95fa27862b7c84221f28fdaba3630 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 19 Jul 2017 22:33:00 +0200 Subject: [PATCH 365/406] transport-helper: prefer Git's builtins over dashed form This helps with minimal installations such as MinGit that refuse to waste .zip real estate by shipping identical copies of builtins (.zip files do not support hard links). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- transport-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transport-helper.c b/transport-helper.c index 7c15c41b58432d..2962c89ee5b9f5 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -119,10 +119,10 @@ static struct child_process *get_helper(struct transport *transport) helper->in = -1; helper->out = -1; helper->err = 0; - argv_array_pushf(&helper->args, "git-remote-%s", data->name); + argv_array_pushf(&helper->args, "remote-%s", data->name); argv_array_push(&helper->args, transport->remote->name); argv_array_push(&helper->args, remove_ext_force(transport->url)); - helper->git_cmd = 0; + helper->git_cmd = 1; helper->silent_exec_failure = 1; if (have_git_dir()) From c15f7435d7d0defd106be992a9aa12ab23befb0a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 20 Jul 2017 22:45:01 +0200 Subject: [PATCH 366/406] mingw: explicitly specify with which cmd to prefix the cmdline The main idea of this patch is that even if we have to look up the absolute path of the script, if only the basename was specified as argv[0], then we should use that basename on the command line, too, not the absolute path. This patch will also help with the upcoming patch where we automatically substitute "sh ..." by "busybox sh ..." if "sh" is not in the PATH but "busybox" is: we will do that by substituting the actual executable, but still keep prepending "sh" to the command line. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index b401a38021c687..68817cb3c45290 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1549,8 +1549,8 @@ static int is_msys2_sh(const char *cmd) } static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv, - const char *dir, - int prepend_cmd, int fhin, int fhout, int fherr) + const char *dir, const char *prepend_cmd, + int fhin, int fhout, int fherr) { static int restrict_handle_inheritance = 1; STARTUPINFOEXW si; @@ -1624,9 +1624,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen /* concatenate argv, quoting args as we go */ strbuf_init(&args, 0); if (prepend_cmd) { - char *quoted = (char *)quote_arg(cmd); + char *quoted = (char *)quote_arg(prepend_cmd); strbuf_addstr(&args, quoted); - if (quoted != cmd) + if (quoted != prepend_cmd) free(quoted); } for (; *argv; argv++) { @@ -1784,7 +1784,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen return (pid_t)pi.dwProcessId; } -static pid_t mingw_spawnv(const char *cmd, const char **argv, int prepend_cmd) +static pid_t mingw_spawnv(const char *cmd, const char **argv, + const char *prepend_cmd) { return mingw_spawnve_fd(cmd, argv, NULL, NULL, prepend_cmd, 0, 1, 2); } @@ -1812,14 +1813,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv, pid = -1; } else { - pid = mingw_spawnve_fd(iprog, argv, deltaenv, dir, 1, + pid = mingw_spawnve_fd(iprog, argv, deltaenv, dir, interpr, fhin, fhout, fherr); free(iprog); } argv[0] = argv0; } else - pid = mingw_spawnve_fd(prog, argv, deltaenv, dir, 0, + pid = mingw_spawnve_fd(prog, argv, deltaenv, dir, NULL, fhin, fhout, fherr); free(prog); } @@ -1845,7 +1846,7 @@ static int try_shell_exec(const char *cmd, char *const *argv) ALLOC_ARRAY(argv2, argc + 1); argv2[0] = (char *)cmd; /* full path to the script file */ memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc); - pid = mingw_spawnv(prog, argv2, 1); + pid = mingw_spawnv(prog, argv2, interpr); if (pid >= 0) { int status; if (waitpid(pid, &status, 0) < 0) @@ -1865,7 +1866,7 @@ int mingw_execv(const char *cmd, char *const *argv) if (!try_shell_exec(cmd, argv)) { int pid, status; - pid = mingw_spawnv(cmd, (const char **)argv, 0); + pid = mingw_spawnv(cmd, (const char **)argv, NULL); if (pid < 0) return -1; if (waitpid(pid, &status, 0) < 0) From c371d4b4f0729af029b28292bcd80a9d9fefe25e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 20 Jul 2017 20:41:29 +0200 Subject: [PATCH 367/406] mingw: when path_lookup() failed, try BusyBox BusyBox comes with a ton of applets ("applet" being the identical concept to Git's "builtins"). And similar to Git's builtins, the applets can be called via `busybox <command>`, or the BusyBox executable can be copied/hard-linked to the command name. The similarities do not end here. Just as with Git's builtins, it is problematic that BusyBox' hard-linked applets cannot easily be put into a .zip file: .zip archives have no concept of hard-links and therefore would store identical copies (and also extract identical copies, "inflating" the archive unnecessarily). To counteract that issue, MinGit already ships without hard-linked copies of the builtins, and the plan is to do the same with BusyBox' applets: simply ship busybox.exe as single executable, without hard-linked applets. To accommodate that, Git is being taught by this commit a very special trick, exploiting the fact that it is possible to call an executable with a command-line whose argv[0] is different from the executable's name: when `sh` is to be spawned, and no `sh` is found in the PATH, but busybox.exe is, use that executable (with unchanged argv). Likewise, if any executable to be spawned is not on the PATH, but busybox.exe is found, parse the output of `busybox.exe --help` to find out what applets are included, and if the command matches an included applet name, use busybox.exe to execute it. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 68817cb3c45290..c160557ff7c704 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -9,6 +9,7 @@ #include "win32/lazyload.h" #include "../config.h" #include "../attr.h" +#include "../string-list.h" #define HCAST(type, handle) ((type)(intptr_t)handle) @@ -1343,6 +1344,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd, return NULL; } +static char *path_lookup(const char *cmd, int exe_only); + +static char *is_busybox_applet(const char *cmd) +{ + static struct string_list applets = STRING_LIST_INIT_DUP; + static char *busybox_path; + static int busybox_path_initialized; + + /* Avoid infinite loop */ + if (!strncasecmp(cmd, "busybox", 7) && + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) + return NULL; + + if (!busybox_path_initialized) { + busybox_path = path_lookup("busybox.exe", 1); + busybox_path_initialized = 1; + } + + /* Assume that sh is compiled in... */ + if (!busybox_path || !strcasecmp(cmd, "sh")) + return xstrdup_or_null(busybox_path); + + if (!applets.nr) { + struct child_process cp = CHILD_PROCESS_INIT; + struct strbuf buf = STRBUF_INIT; + char *p; + + argv_array_pushl(&cp.args, busybox_path, "--help", NULL); + + if (capture_command(&cp, &buf, 2048)) { + string_list_append(&applets, ""); + return NULL; + } + + /* parse output */ + p = strstr(buf.buf, "Currently defined functions:\n"); + if (!p) { + warning("Could not parse output of busybox --help"); + string_list_append(&applets, ""); + return NULL; + } + p = strchrnul(p, '\n'); + for (;;) { + size_t len; + + p += strspn(p, "\n\t ,"); + len = strcspn(p, "\n\t ,"); + if (!len) + break; + p[len] = '\0'; + string_list_insert(&applets, p); + p = p + len + 1; + } + } + + return string_list_has_string(&applets, cmd) ? + xstrdup(busybox_path) : NULL; +} + /* * Determines the absolute path of cmd using the split path in path. * If cmd contains a slash or backslash, no lookup is performed. @@ -1371,6 +1431,9 @@ static char *path_lookup(const char *cmd, int exe_only) path = sep + 1; } + if (!prog && !isexe) + prog = is_busybox_applet(cmd); + return prog; } From c2abca49bf23583971ee263f3475fbe787f2bc9a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 18 Jul 2017 17:52:13 +0200 Subject: [PATCH 368/406] test-run-command: learn to run (parts of) the testsuite Instead of relying on the presence of `make`, or `prove`, we might just as well use our own facilities to run the test suite. This helps e.g. when trying to verify a Git for Windows installation without requiring to download a full Git for Windows SDK (which would use up 600+ megabytes of bandwidth, and over a gigabyte of disk space). Of course, it still requires the test helpers to be build *somewhere*, and the Git version should at least roughly match the version from which the test suite comes. At the same time, this new way to run the test suite allows to validate that a BusyBox-backed MinGit works as expected (verifying that BusyBox' functionality is enough to at least pass the test suite). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/helper/test-run-command.c | 143 ++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index e1bc58b9569aa7..84284d7e2d3dda 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -10,9 +10,14 @@ #include "test-tool.h" #include "git-compat-util.h" +#include "cache.h" #include "run-command.h" #include "argv-array.h" #include "strbuf.h" +#include "parse-options.h" +#include "string-list.h" +#include "thread-utils.h" +#include "wildmatch.h" #include <string.h> #include <errno.h> @@ -50,6 +55,141 @@ static int task_finished(int result, return 1; } +struct testsuite { + struct string_list tests, failed; + int next; + int quiet, immediate, verbose, trace; +}; + +static int next_test(struct child_process *cp, struct strbuf *err, void *cb, + void **task_cb) +{ + struct testsuite *suite = cb; + const char *test; + if (suite->next >= suite->tests.nr) + return 0; + + test = suite->tests.items[suite->next++].string; + argv_array_pushl(&cp->args, "sh", test, NULL); + if (suite->quiet) + argv_array_push(&cp->args, "--quiet"); + if (suite->immediate) + argv_array_push(&cp->args, "-i"); + if (suite->verbose) + argv_array_push(&cp->args, "-v"); + if (suite->trace) + argv_array_push(&cp->args, "-x"); + + strbuf_addf(err, "Output of '%s':\n", test); + *task_cb = (void *)test; + + return 1; +} + +static int test_finished(int result, struct strbuf *err, void *cb, + void *task_cb) +{ + struct testsuite *suite = cb; + const char *name = (const char *)task_cb; + + if (result) + string_list_append(&suite->failed, name); + + strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name); + + return 0; +} + +static int test_failed(struct strbuf *out, void *cb, void *task_cb) +{ + struct testsuite *suite = cb; + const char *name = (const char *)task_cb; + + string_list_append(&suite->failed, name); + strbuf_addf(out, "FAILED TO START: '%s'\n", name); + + return 0; +} + +static const char * const testsuite_usage[] = { + "test-run-command testsuite [<options>] [<pattern>...]", + NULL +}; + +static int testsuite(int argc, const char **argv) +{ + struct testsuite suite; + int max_jobs = 1, i, ret; + DIR *dir; + struct dirent *d; + struct option options[] = { + OPT_BOOL('i', "immediate", &suite.immediate, + "stop at first failed test case(s)"), + OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"), + OPT_BOOL('q', "quiet", &suite.quiet, "be terse"), + OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"), + OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"), + OPT_END() + }; + + memset(&suite, 0, sizeof(suite)); + suite.tests.strdup_strings = suite.failed.strdup_strings = 1; + + argc = parse_options(argc, argv, NULL, options, + testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION); + + if (max_jobs <= 0) + max_jobs = online_cpus(); + + dir = opendir("."); + if (!dir) + die("Could not open the current directory"); + while ((d = readdir(dir))) { + const char *p = d->d_name; + + if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) || + !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' || + !ends_with(p, ".sh")) + continue; + + /* No pattern: match all */ + if (!argc) { + string_list_append(&suite.tests, p); + continue; + } + + for (i = 0; i < argc; i++) + if (!wildmatch(argv[i], p, 0)) { + string_list_append(&suite.tests, p); + break; + } + } + closedir(dir); + + if (!suite.tests.nr) + die("No tests match!"); + if (max_jobs > suite.tests.nr) + max_jobs = suite.tests.nr; + + fprintf(stderr, "Running %d tests (%d at a time)\n", + suite.tests.nr, max_jobs); + + ret = run_processes_parallel(max_jobs, next_test, test_failed, + test_finished, &suite); + + if (suite.failed.nr > 0) { + ret = 1; + fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr); + for (i = 0; i < suite.failed.nr; i++) + fprintf(stderr, "\t%s\n", suite.failed.items[i].string); + } + + string_list_clear(&suite.tests, 0); + string_list_clear(&suite.failed, 0); + + return !!ret; +} + static int inherit_handle(const char *argv0) { struct child_process cp = CHILD_PROCESS_INIT; @@ -95,6 +235,9 @@ int cmd__run_command(int argc, const char **argv) struct child_process proc = CHILD_PROCESS_INIT; int jobs; + if (argc > 1 && !strcmp(argv[1], "testsuite")) + exit(testsuite(argc - 1, argv + 1)); + if (argc < 2) return 1; if (!strcmp(argv[1], "inherited-handle")) From 4b835ef3831402aca237b6d4b8865dade1c0715e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 5 Aug 2017 22:23:36 +0200 Subject: [PATCH 369/406] test-lib: avoid unnecessary Perl invocation It is a bit strange, and even undesirable, to require Perl just to run the test suite even when NO_PERL was set. This patch does not fix this problem by any stretch of imagination. However, it fixes *the* Perl invocation that *every single* test script has to run. While at it, it makes the source code also more grep'able, as the code that unsets some, but not all, GIT_* environment variables just became a *lot* more explicit. And all that while still reducing the total number of lines. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index f20737f713a1af..d8f2b649fa44b5 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -116,23 +116,18 @@ EDITOR=: # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets # deriving from the command substitution clustered with the other # ones. -unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e ' - my @env = keys %ENV; - my $ok = join("|", qw( - TRACE - DEBUG - TEST - .*_TEST - PROVE - VALGRIND - UNZIP - PERF_ - CURL_VERBOSE - TRACE_CURL - )); - my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env); - print join("\n", @vars); -') +unset VISUAL EMAIL LANGUAGE COLUMNS $(env | sed -n \ + -e '/^GIT_TRACE/d' \ + -e '/^GIT_DEBUG/d' \ + -e '/^GIT_TEST/d' \ + -e '/^GIT_.*_TEST/d' \ + -e '/^GIT_PROVE/d' \ + -e '/^GIT_VALGRIND/d' \ + -e '/^GIT_UNZIP/d' \ + -e '/^GIT_PERF_/d' \ + -e '/^GIT_CURL_VERBOSE/d' \ + -e '/^GIT_TRACE_CURL/d' \ + -e 's/^\(GIT_[^=]*\)=.*/\1/p') unset XDG_CACHE_HOME unset XDG_CONFIG_HOME unset GITPERLLIB From 0f9c661dae54afb444c549b9c4bf95e653b5d08f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 7 Jun 2018 10:47:25 +0200 Subject: [PATCH 370/406] tests: replace mingw_test_cmp with a helper in C This helper is slightly more performant than the script with MSYS2's Bash. And a lot more readable. To accommodate t1050, which wants to compare files weighing in with 3MB (falling outside of t1050's malloc limit of 1.5MB), we simply lift the allocation limit by setting the environment variable GIT_ALLOC_LIMIT to zero when calling the helper. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Makefile | 1 + t/helper/test-cmp.c | 73 +++++++++++++++++++++++++++++++++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/test-lib-functions.sh | 68 +------------------------------------- t/test-lib.sh | 2 +- 6 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 t/helper/test-cmp.c diff --git a/Makefile b/Makefile index 53dd7f08b2ef6c..fc78452da1e63c 100644 --- a/Makefile +++ b/Makefile @@ -703,6 +703,7 @@ X = PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS)) TEST_BUILTINS_OBJS += test-chmtime.o +TEST_BUILTINS_OBJS += test-cmp.o TEST_BUILTINS_OBJS += test-config.o TEST_BUILTINS_OBJS += test-ctype.o TEST_BUILTINS_OBJS += test-date.o diff --git a/t/helper/test-cmp.c b/t/helper/test-cmp.c new file mode 100644 index 00000000000000..1c646a54bf609b --- /dev/null +++ b/t/helper/test-cmp.c @@ -0,0 +1,73 @@ +#include "test-tool.h" +#include "git-compat-util.h" +#include "strbuf.h" +#include "gettext.h" +#include "parse-options.h" +#include "run-command.h" + +#ifdef WIN32 +#define NO_SUCH_DIR "\\\\.\\GLOBALROOT\\invalid" +#else +#define NO_SUCH_DIR "/dev/null" +#endif + +static int run_diff(const char *path1, const char *path2) +{ + const char *argv[] = { + "diff", "--no-index", NULL, NULL, NULL + }; + const char *env[] = { + "GIT_PAGER=cat", + "GIT_DIR=" NO_SUCH_DIR, + "HOME=" NO_SUCH_DIR, + NULL + }; + + argv[2] = path1; + argv[3] = path2; + return run_command_v_opt_cd_env(argv, + RUN_COMMAND_NO_STDIN | RUN_GIT_CMD, + NULL, env); +} + +int cmd__cmp(int argc, const char **argv) +{ + FILE *f0, *f1; + struct strbuf b0 = STRBUF_INIT, b1 = STRBUF_INIT; + + if (argc != 3) + die("Require exactly 2 arguments, got %d", argc); + + if (!(f0 = !strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r"))) + return error_errno("could not open '%s'", argv[1]); + if (!(f1 = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r"))) { + fclose(f0); + return error_errno("could not open '%s'", argv[2]); + } + + for (;;) { + int r0 = strbuf_getline(&b0, f0); + int r1 = strbuf_getline(&b1, f1); + + if (r0 == EOF) { + fclose(f0); + fclose(f1); + strbuf_release(&b0); + strbuf_release(&b1); + if (r1 == EOF) + return 0; +cmp_failed: + if (!run_diff(argv[1], argv[2])) + die("Huh? 'diff --no-index %s %s' succeeded", + argv[1], argv[2]); + return 1; + } + if (r1 == EOF || strbuf_cmp(&b0, &b1)) { + fclose(f0); + fclose(f1); + strbuf_release(&b0); + strbuf_release(&b1); + goto cmp_failed; + } + } +} diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 0edafcfd65db75..115e930ebe8796 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -8,6 +8,7 @@ struct test_cmd { static struct test_cmd cmds[] = { { "chmtime", cmd__chmtime }, + { "cmp", cmd__cmp }, { "config", cmd__config }, { "ctype", cmd__ctype }, { "date", cmd__date }, diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index e954e8c5222f77..3e9d05b4a2f823 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -4,6 +4,7 @@ #include "git-compat-util.h" int cmd__chmtime(int argc, const char **argv); +int cmd__cmp(int argc, const char **argv); int cmd__config(int argc, const char **argv); int cmd__ctype(int argc, const char **argv); int cmd__date(int argc, const char **argv); diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 40ef84a97e2a76..8b4af96960ec79 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -742,7 +742,7 @@ test_expect_code () { # - not all diff versions understand "-u" test_cmp() { - $GIT_TEST_CMP "$@" + GIT_ALLOC_LIMIT=0 $GIT_TEST_CMP "$@" } # test_cmp_bin - helper to compare binary files @@ -1020,72 +1020,6 @@ test_skip_or_die () { esac } -# The following mingw_* functions obey POSIX shell syntax, but are actually -# bash scripts, and are meant to be used only with bash on Windows. - -# A test_cmp function that treats LF and CRLF equal and avoids to fork -# diff when possible. -mingw_test_cmp () { - # Read text into shell variables and compare them. If the results - # are different, use regular diff to report the difference. - local test_cmp_a= test_cmp_b= - - # When text came from stdin (one argument is '-') we must feed it - # to diff. - local stdin_for_diff= - - # Since it is difficult to detect the difference between an - # empty input file and a failure to read the files, we go straight - # to diff if one of the inputs is empty. - if test -s "$1" && test -s "$2" - then - # regular case: both files non-empty - mingw_read_file_strip_cr_ test_cmp_a <"$1" - mingw_read_file_strip_cr_ test_cmp_b <"$2" - elif test -s "$1" && test "$2" = - - then - # read 2nd file from stdin - mingw_read_file_strip_cr_ test_cmp_a <"$1" - mingw_read_file_strip_cr_ test_cmp_b - stdin_for_diff='<<<"$test_cmp_b"' - elif test "$1" = - && test -s "$2" - then - # read 1st file from stdin - mingw_read_file_strip_cr_ test_cmp_a - mingw_read_file_strip_cr_ test_cmp_b <"$2" - stdin_for_diff='<<<"$test_cmp_a"' - fi - test -n "$test_cmp_a" && - test -n "$test_cmp_b" && - test "$test_cmp_a" = "$test_cmp_b" || - eval "diff -u \"\$@\" $stdin_for_diff" -} - -# $1 is the name of the shell variable to fill in -mingw_read_file_strip_cr_ () { - # Read line-wise using LF as the line separator - # and use IFS to strip CR. - local line - while : - do - if IFS=$'\r' read -r -d $'\n' line - then - # good - line=$line$'\n' - else - # we get here at EOF, but also if the last line - # was not terminated by LF; in the latter case, - # some text was read - if test -z "$line" - then - # EOF, really - break - fi - fi - eval "$1=\$$1\$line" - done -} - # Like "env FOO=BAR some-program", but run inside a subshell, which means # it also works for shell functions (though those functions cannot impact # the environment outside of the test_env invocation). diff --git a/t/test-lib.sh b/t/test-lib.sh index d8f2b649fa44b5..a4ae04c9fb1141 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1205,7 +1205,7 @@ case $uname_s in test_set_prereq NATIVE_CRLF test_set_prereq SED_STRIPS_CR test_set_prereq GREP_STRIPS_CR - GIT_TEST_CMP=mingw_test_cmp + GIT_TEST_CMP="test-tool cmp" ;; *CYGWIN*) test_set_prereq POSIXPERM From cb6b80a48b064513ac92bb98988faedccdd66375 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 20 Jul 2017 22:18:56 +0200 Subject: [PATCH 371/406] test-tool: learn to act as a drop-in replacement for `iconv` It is convenient to assume that everybody who wants to build & test Git has access to a working `iconv` executable (after all, we already pretty much require libiconv). However, that limits esoteric test scenarios such as Git for Windows', where an end user installation has to ship with `iconv` for the sole purpose of being testable. That payload serves no other purpose. So let's just have a test helper (to be able to test Git, the test helpers have to be available, after all) to act as `iconv` replacement. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- Makefile | 1 + t/helper/test-iconv.c | 47 +++++++++++++++++++++++++++++++++++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + 4 files changed, 50 insertions(+) create mode 100644 t/helper/test-iconv.c diff --git a/Makefile b/Makefile index fc78452da1e63c..745d2b0bb4fde3 100644 --- a/Makefile +++ b/Makefile @@ -714,6 +714,7 @@ TEST_BUILTINS_OBJS += test-dump-split-index.o TEST_BUILTINS_OBJS += test-example-decorate.o TEST_BUILTINS_OBJS += test-genrandom.o TEST_BUILTINS_OBJS += test-hashmap.o +TEST_BUILTINS_OBJS += test-iconv.o TEST_BUILTINS_OBJS += test-index-version.o TEST_BUILTINS_OBJS += test-json-writer.o TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o diff --git a/t/helper/test-iconv.c b/t/helper/test-iconv.c new file mode 100644 index 00000000000000..d3c772fddf990b --- /dev/null +++ b/t/helper/test-iconv.c @@ -0,0 +1,47 @@ +#include "test-tool.h" +#include "git-compat-util.h" +#include "strbuf.h" +#include "gettext.h" +#include "parse-options.h" +#include "utf8.h" + +int cmd__iconv(int argc, const char **argv) +{ + struct strbuf buf = STRBUF_INIT; + char *from = NULL, *to = NULL, *p; + size_t len; + int ret = 0; + const char * const iconv_usage[] = { + N_("test-helper --iconv [<options>]"), + NULL + }; + struct option options[] = { + OPT_STRING('f', "from-code", &from, "encoding", "from"), + OPT_STRING('t', "to-code", &to, "encoding", "to"), + OPT_END() + }; + + argc = parse_options(argc, argv, NULL, options, + iconv_usage, 0); + + if (argc > 1 || !from || !to) + usage_with_options(iconv_usage, options); + + if (!argc) { + if (strbuf_read(&buf, 0, 2048) < 0) + die_errno("Could not read from stdin"); + } else if (strbuf_read_file(&buf, argv[0], 2048) < 0) + die_errno("Could not read from '%s'", argv[0]); + + p = reencode_string_len(buf.buf, buf.len, to, from, &len); + if (!p) + die_errno("Could not reencode"); + if (write(1, p, len) < 0) + ret = !!error_errno("Could not write %"PRIuMAX" bytes", + (uintmax_t)len); + + strbuf_release(&buf); + free(p); + + return ret; +} diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 115e930ebe8796..9d1b1255e0fc48 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -19,6 +19,7 @@ static struct test_cmd cmds[] = { { "example-decorate", cmd__example_decorate }, { "genrandom", cmd__genrandom }, { "hashmap", cmd__hashmap }, + { "iconv", cmd__iconv }, { "index-version", cmd__index_version }, { "json-writer", cmd__json_writer }, { "lazy-init-name-hash", cmd__lazy_init_name_hash }, diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index 3e9d05b4a2f823..376fe27d7a90cf 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -15,6 +15,7 @@ int cmd__dump_split_index(int argc, const char **argv); int cmd__example_decorate(int argc, const char **argv); int cmd__genrandom(int argc, const char **argv); int cmd__hashmap(int argc, const char **argv); +int cmd__iconv(int argc, const char **argv); int cmd__index_version(int argc, const char **argv); int cmd__json_writer(int argc, const char **argv); int cmd__lazy_init_name_hash(int argc, const char **argv); From c77f5a2e29c64cb03d55a23e577520198c91aaf3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 20 Jul 2017 22:25:21 +0200 Subject: [PATCH 372/406] tests(mingw): if `iconv` is unavailable, use `test-helper --iconv` Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/t/test-lib.sh b/t/test-lib.sh index a4ae04c9fb1141..63ade659cd793b 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1206,6 +1206,12 @@ case $uname_s in test_set_prereq SED_STRIPS_CR test_set_prereq GREP_STRIPS_CR GIT_TEST_CMP="test-tool cmp" + if ! type iconv >/dev/null 2>&1 + then + iconv () { + test-tool iconv "$@" + } + fi ;; *CYGWIN*) test_set_prereq POSIXPERM From 44419b6db08c4632cccbea58bc17c3060e201c5f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 5 Aug 2017 13:44:17 +0200 Subject: [PATCH 373/406] tests: use t/diff-lib/* consistently The idea of copying README and COPYING into t/diff-lib/ was to step away from using files from outside t/ in tests. Let's really make sure that we use the files from t/diff-lib/ instead of other versions of those files. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t4022-diff-rewrite.sh | 4 ++-- t/t4023-diff-rename-typechange.sh | 14 +++++++------- t/t7001-mv.sh | 4 ++-- t/t7060-wtstatus.sh | 2 +- t/t7101-reset-empty-subdirs.sh | 10 +++++----- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/t/t4022-diff-rewrite.sh b/t/t4022-diff-rewrite.sh index 6d1c3d949c78bc..c6d44e76e2f44d 100755 --- a/t/t4022-diff-rewrite.sh +++ b/t/t4022-diff-rewrite.sh @@ -6,12 +6,12 @@ test_description='rewrite diff' test_expect_success setup ' - cat "$TEST_DIRECTORY"/../COPYING >test && + cat "$TEST_DIRECTORY"/diff-lib/COPYING >test && git add test && tr \ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \ "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM" \ - <"$TEST_DIRECTORY"/../COPYING >test && + <"$TEST_DIRECTORY"/diff-lib/COPYING >test && echo "to be deleted" >test2 && blob=$(git hash-object test2) && blob=$(git rev-parse --short $blob) && diff --git a/t/t4023-diff-rename-typechange.sh b/t/t4023-diff-rename-typechange.sh index 8c9823765e66ac..a2854004a96d0b 100755 --- a/t/t4023-diff-rename-typechange.sh +++ b/t/t4023-diff-rename-typechange.sh @@ -7,21 +7,21 @@ test_description='typechange rename detection' test_expect_success setup ' rm -f foo bar && - cat "$TEST_DIRECTORY"/../COPYING >foo && + cat "$TEST_DIRECTORY"/diff-lib/COPYING >foo && test_ln_s_add linklink bar && git add foo && git commit -a -m Initial && git tag one && git rm -f foo bar && - cat "$TEST_DIRECTORY"/../COPYING >bar && + cat "$TEST_DIRECTORY"/diff-lib/COPYING >bar && test_ln_s_add linklink foo && git add bar && git commit -a -m Second && git tag two && git rm -f foo bar && - cat "$TEST_DIRECTORY"/../COPYING >foo && + cat "$TEST_DIRECTORY"/diff-lib/COPYING >foo && git add foo && git commit -a -m Third && git tag three && @@ -35,15 +35,15 @@ test_expect_success setup ' # This is purely for sanity check git rm -f foo bar && - cat "$TEST_DIRECTORY"/../COPYING >foo && - cat "$TEST_DIRECTORY"/../Makefile >bar && + cat "$TEST_DIRECTORY"/diff-lib/COPYING >foo && + cat "$TEST_DIRECTORY"/diff-lib/README >bar && git add foo bar && git commit -a -m Fifth && git tag five && git rm -f foo bar && - cat "$TEST_DIRECTORY"/../Makefile >foo && - cat "$TEST_DIRECTORY"/../COPYING >bar && + cat "$TEST_DIRECTORY"/diff-lib/README >foo && + cat "$TEST_DIRECTORY"/diff-lib/COPYING >bar && git add foo bar && git commit -a -m Sixth && git tag six diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh index 36b50d0b4c1255..af8a8da3853eed 100755 --- a/t/t7001-mv.sh +++ b/t/t7001-mv.sh @@ -6,7 +6,7 @@ test_description='git mv in subdirs' test_expect_success \ 'prepare reference tree' \ 'mkdir path0 path1 && - cp "$TEST_DIRECTORY"/../COPYING path0/COPYING && + cp "$TEST_DIRECTORY"/diff-lib/COPYING path0/COPYING && git add path0/COPYING && git commit -m add -a' @@ -108,7 +108,7 @@ test_expect_success \ test_expect_success \ 'adding another file' \ - 'cp "$TEST_DIRECTORY"/../README.md path0/README && + 'cp "$TEST_DIRECTORY"/diff-lib/README path0/ && git add path0/README && git commit -m add2 -a' diff --git a/t/t7060-wtstatus.sh b/t/t7060-wtstatus.sh index 53cf42fac19c83..d96c668fceb8b5 100755 --- a/t/t7060-wtstatus.sh +++ b/t/t7060-wtstatus.sh @@ -62,7 +62,7 @@ EOF test_expect_success 'rename & unmerged setup' ' git rm -f -r . && - cat "$TEST_DIRECTORY/README" >ONE && + cat "$TEST_DIRECTORY/diff-lib/README" >ONE && git add ONE && test_tick && git commit -m "One commit with ONE" && diff --git a/t/t7101-reset-empty-subdirs.sh b/t/t7101-reset-empty-subdirs.sh index 96e163f084f471..cad2cd46fcf47b 100755 --- a/t/t7101-reset-empty-subdirs.sh +++ b/t/t7101-reset-empty-subdirs.sh @@ -9,7 +9,7 @@ test_description='git reset should cull empty subdirs' test_expect_success \ 'creating initial files' \ 'mkdir path0 && - cp "$TEST_DIRECTORY"/../COPYING path0/COPYING && + cp "$TEST_DIRECTORY"/diff-lib/COPYING path0/COPYING && git add path0/COPYING && git commit -m add -a' @@ -17,10 +17,10 @@ test_expect_success \ 'creating second files' \ 'mkdir path1 && mkdir path1/path2 && - cp "$TEST_DIRECTORY"/../COPYING path1/path2/COPYING && - cp "$TEST_DIRECTORY"/../COPYING path1/COPYING && - cp "$TEST_DIRECTORY"/../COPYING COPYING && - cp "$TEST_DIRECTORY"/../COPYING path0/COPYING-TOO && + cp "$TEST_DIRECTORY"/diff-lib/COPYING path1/path2/COPYING && + cp "$TEST_DIRECTORY"/diff-lib/COPYING path1/COPYING && + cp "$TEST_DIRECTORY"/diff-lib/COPYING COPYING && + cp "$TEST_DIRECTORY"/diff-lib/COPYING path0/COPYING-TOO && git add path1/path2/COPYING && git add path1/COPYING && git add COPYING && From f94f3d72fda63a14e166f1edb18d01852363e719 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 11 Oct 2018 23:55:44 +0200 Subject: [PATCH 374/406] gitattributes: mark .png files as binary Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 9fc30e5c76831e..f287471c76d750 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,6 +4,7 @@ *.perl eol=lf diff=perl *.pl eof=lf diff=perl *.pm eol=lf diff=perl +*.png binary *.py eol=lf diff=python *.bat eol=crlf /Documentation/git-*.txt eol=lf From 6fa09aef03911b3182368206d9388d7abc8e6a49 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 5 Aug 2017 20:28:37 +0200 Subject: [PATCH 375/406] tests: move test PNGs into t/diff-lib/ We already have a directory where we store files intended for use by multiple test scripts. The same directory is a better home for the test-binary-*.png files than t/. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/{ => diff-lib}/test-binary-1.png | Bin t/{ => diff-lib}/test-binary-2.png | Bin t/t3307-notes-man.sh | 2 +- t/t3903-stash.sh | 2 +- t/t4012-diff-binary.sh | 2 +- t/t4049-diff-stat-count.sh | 2 +- t/t6023-merge-file.sh | 2 +- t/t6027-merge-binary.sh | 2 +- t/t9200-git-cvsexportcommit.sh | 15 ++++++++------- 9 files changed, 14 insertions(+), 13 deletions(-) rename t/{ => diff-lib}/test-binary-1.png (100%) rename t/{ => diff-lib}/test-binary-2.png (100%) diff --git a/t/test-binary-1.png b/t/diff-lib/test-binary-1.png similarity index 100% rename from t/test-binary-1.png rename to t/diff-lib/test-binary-1.png diff --git a/t/test-binary-2.png b/t/diff-lib/test-binary-2.png similarity index 100% rename from t/test-binary-2.png rename to t/diff-lib/test-binary-2.png diff --git a/t/t3307-notes-man.sh b/t/t3307-notes-man.sh index 1aa366a410e9a3..4887ac99598be4 100755 --- a/t/t3307-notes-man.sh +++ b/t/t3307-notes-man.sh @@ -26,7 +26,7 @@ test_expect_success 'example 1: notes to add an Acked-by line' ' ' test_expect_success 'example 2: binary notes' ' - cp "$TEST_DIRECTORY"/test-binary-1.png . && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png . && git checkout B && blob=$(git hash-object -w test-binary-1.png) && git notes --ref=logo add -C "$blob" && diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 6450bc669860f0..c934e507c0ae3f 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1084,7 +1084,7 @@ test_expect_success 'stash -- <subdir> works with binary files' ' git reset && >subdir/untracked && >subdir/tracked && - cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png subdir/tracked-binary && git add subdir/tracked* && git stash -- subdir/ && test_path_is_missing subdir/tracked && diff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh index 6579c81216a9b2..10b56142047803 100755 --- a/t/t4012-diff-binary.sh +++ b/t/t4012-diff-binary.sh @@ -19,7 +19,7 @@ test_expect_success 'prepare repository' ' echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d && git update-index --add a b c d && echo git >a && - cat "$TEST_DIRECTORY"/test-binary-1.png >b && + cat "$TEST_DIRECTORY"/diff-lib/test-binary-1.png >b && echo git >c && cat b b >d ' diff --git a/t/t4049-diff-stat-count.sh b/t/t4049-diff-stat-count.sh index a34121740a4ab5..d63d18246203d2 100755 --- a/t/t4049-diff-stat-count.sh +++ b/t/t4049-diff-stat-count.sh @@ -32,7 +32,7 @@ test_expect_success 'binary changes do not count in lines' ' git reset --hard && echo a >a && echo c >c && - cat "$TEST_DIRECTORY"/test-binary-1.png >d && + cat "$TEST_DIRECTORY"/diff-lib/test-binary-1.png >d && cat >expect <<-\EOF && a | 1 + c | 1 + diff --git a/t/t6023-merge-file.sh b/t/t6023-merge-file.sh index 20aee43f95ab3c..38f2aa6120d064 100755 --- a/t/t6023-merge-file.sh +++ b/t/t6023-merge-file.sh @@ -221,7 +221,7 @@ test_expect_success "expected conflict markers" "test_cmp expect out" test_expect_success 'binary files cannot be merged' ' test_must_fail git merge-file -p \ - orig.txt "$TEST_DIRECTORY"/test-binary-1.png new1.txt 2> merge.err && + orig.txt "$TEST_DIRECTORY"/diff-lib/test-binary-1.png new1.txt 2> merge.err && grep "Cannot merge binary files" merge.err ' diff --git a/t/t6027-merge-binary.sh b/t/t6027-merge-binary.sh index 07735410b9536b..d419386bf8ff4d 100755 --- a/t/t6027-merge-binary.sh +++ b/t/t6027-merge-binary.sh @@ -6,7 +6,7 @@ test_description='ask merge-recursive to merge binary files' test_expect_success setup ' - cat "$TEST_DIRECTORY"/test-binary-1.png >m && + cat "$TEST_DIRECTORY"/diff-lib/test-binary-1.png >m && git add m && git ls-files -s | sed -e "s/ 0 / 1 /" >E1 && test_tick && diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh index c5946cb0b8a94c..52ae42c325bc79 100755 --- a/t/t9200-git-cvsexportcommit.sh +++ b/t/t9200-git-cvsexportcommit.sh @@ -55,8 +55,8 @@ test_expect_success \ 'mkdir A B C D E F && echo hello1 >A/newfile1.txt && echo hello2 >B/newfile2.txt && - cp "$TEST_DIRECTORY"/test-binary-1.png C/newfile3.png && - cp "$TEST_DIRECTORY"/test-binary-1.png D/newfile4.png && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png C/newfile3.png && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png D/newfile4.png && git add A/newfile1.txt && git add B/newfile2.txt && git add C/newfile3.png && @@ -81,8 +81,8 @@ test_expect_success \ rm -f B/newfile2.txt && rm -f C/newfile3.png && echo Hello5 >E/newfile5.txt && - cp "$TEST_DIRECTORY"/test-binary-2.png D/newfile4.png && - cp "$TEST_DIRECTORY"/test-binary-1.png F/newfile6.png && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-2.png D/newfile4.png && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png F/newfile6.png && git add E/newfile5.txt && git add F/newfile6.png && git commit -a -m "Test: Remove, add and update" && @@ -170,7 +170,7 @@ test_expect_success \ 'mkdir "G g" && echo ok then >"G g/with spaces.txt" && git add "G g/with spaces.txt" && \ - cp "$TEST_DIRECTORY"/test-binary-1.png "G g/with spaces.png" && \ + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png "G g/with spaces.png" && \ git add "G g/with spaces.png" && git commit -a -m "With spaces" && id=$(git rev-list --max-count=1 HEAD) && @@ -182,7 +182,8 @@ test_expect_success \ test_expect_success \ 'Update file with spaces in file name' \ 'echo Ok then >>"G g/with spaces.txt" && - cat "$TEST_DIRECTORY"/test-binary-1.png >>"G g/with spaces.png" && \ + cat "$TEST_DIRECTORY"/diff-lib/test-binary-1.png \ + >>"G g/with spaces.png" && \ git add "G g/with spaces.png" && git commit -a -m "Update with spaces" && id=$(git rev-list --max-count=1 HEAD) && @@ -207,7 +208,7 @@ test_expect_success !MINGW \ 'mkdir -p Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö && echo Foo >Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.txt && git add Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.txt && - cp "$TEST_DIRECTORY"/test-binary-1.png Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.png && + cp "$TEST_DIRECTORY"/diff-lib/test-binary-1.png Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.png && git add Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.png && git commit -a -m "Går det så går det" && \ id=$(git rev-list --max-count=1 HEAD) && From 0e31b88e7387e0d9effecd565749f5dbfd85204d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 18 Jul 2017 01:15:40 +0200 Subject: [PATCH 376/406] tests: only override sort & find if there are usable ones in /usr/bin/ The idea is to allow running the test suite on MinGit with BusyBox installed in /mingw64/bin/sh.exe. In that case, we will want to exclude sort & find (and other Unix utilities) from being bundled. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-sh-setup.sh | 21 ++++++++++++++------- t/test-lib.sh | 21 ++++++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/git-sh-setup.sh b/git-sh-setup.sh index 378928518b2c42..5886835fbf4863 100644 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -332,13 +332,20 @@ create_virtual_base() { # Platform specific tweaks to work around some commands case $(uname -s) in *MINGW*) - # Windows has its own (incompatible) sort and find - sort () { - /usr/bin/sort "$@" - } - find () { - /usr/bin/find "$@" - } + if test -x /usr/bin/sort + then + # Windows has its own (incompatible) sort; override + sort () { + /usr/bin/sort "$@" + } + fi + if test -x /usr/bin/find + then + # Windows has its own (incompatible) find; override + find () { + /usr/bin/find "$@" + } + fi # git sees Windows-style pwd pwd () { builtin pwd -W diff --git a/t/test-lib.sh b/t/test-lib.sh index 63ade659cd793b..9574627c15cd9a 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1187,13 +1187,20 @@ yes () { uname_s=$(uname -s) case $uname_s in *MINGW*) - # Windows has its own (incompatible) sort and find - sort () { - /usr/bin/sort "$@" - } - find () { - /usr/bin/find "$@" - } + if test -x /usr/bin/sort + then + # Windows has its own (incompatible) sort; override + sort () { + /usr/bin/sort "$@" + } + fi + if test -x /usr/bin/find + then + # Windows has its own (incompatible) find; override + find () { + /usr/bin/find "$@" + } + fi # git sees Windows-style pwd pwd () { builtin pwd -W From fd9d04e052dc41c8f1ee79b5ce8c01dc29a5de77 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 19 Nov 2018 20:34:13 +0100 Subject: [PATCH 377/406] tests: use the correct path separator with BusyBox BusyBox-w32 is a true Win32 application, i.e. it does not come with a POSIX emulation layer. That also means that it does *not* use the Unix convention of separating the entries in the PATH variable using colons, but semicolons. However, there are also BusyBox ports to Windows which use a POSIX emulation layer such as Cygwin's or MSYS2's runtime, i.e. using colons as PATH separators. As a tell-tale, let's use the presence of semicolons in the PATH variable: on Unix, it is highly unlikely that it contains semicolons, and on Windows (without POSIX emulation), it is virtually guaranteed, as everybody should have both $SYSTEMROOT and $SYSTEMROOT/system32 in their PATH. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/interop/interop-lib.sh | 8 ++++++-- t/lib-proto-disable.sh | 2 +- t/t0021-conversion.sh | 2 +- t/t0060-path-utils.sh | 24 ++++++++++++------------ t/t0061-run-command.sh | 6 +++--- t/t0300-credentials.sh | 2 +- t/t1504-ceiling-dirs.sh | 10 +++++----- t/t2300-cd-to-toplevel.sh | 2 +- t/t3402-rebase-merge.sh | 2 +- t/t3418-rebase-continue.sh | 8 ++++---- t/t5615-alternate-env.sh | 4 ++-- t/t5802-connect-helper.sh | 2 +- t/t7006-pager.sh | 4 ++-- t/t7606-merge-custom.sh | 2 +- t/t7811-grep-open.sh | 2 +- t/t9003-help-autocorrect.sh | 2 +- t/t9020-remote-svn.sh | 2 +- t/t9800-git-p4-basic.sh | 2 +- t/test-lib.sh | 17 +++++++++++++---- 19 files changed, 58 insertions(+), 45 deletions(-) diff --git a/t/interop/interop-lib.sh b/t/interop/interop-lib.sh index 3e0a2911d4f9ba..dea8883821fe3e 100644 --- a/t/interop/interop-lib.sh +++ b/t/interop/interop-lib.sh @@ -4,6 +4,10 @@ . ../../GIT-BUILD-OPTIONS INTEROP_ROOT=$(pwd) BUILD_ROOT=$INTEROP_ROOT/build +case "$PATH" in +*\;*) PATH_SEP=\; ;; +*) PATH_SEP=: ;; +esac build_version () { if test -z "$1" @@ -57,7 +61,7 @@ wrap_git () { write_script "$1" <<-EOF GIT_EXEC_PATH="$2" export GIT_EXEC_PATH - PATH="$2:\$PATH" + PATH="$2$PATH_SEP\$PATH" export GIT_EXEC_PATH exec git "\$@" EOF @@ -71,7 +75,7 @@ generate_wrappers () { echo >&2 fatal: test tried to run generic git exit 1 EOF - PATH=$(pwd)/.bin:$PATH + PATH=$(pwd)/.bin$PATH_SEP$PATH } VERSION_A=${GIT_TEST_VERSION_A:-$VERSION_A} diff --git a/t/lib-proto-disable.sh b/t/lib-proto-disable.sh index 83babe57d95900..9dc55a83a0621c 100644 --- a/t/lib-proto-disable.sh +++ b/t/lib-proto-disable.sh @@ -214,7 +214,7 @@ setup_ext_wrapper () { cd "$TRASH_DIRECTORY/remote" && eval "$*" EOF - PATH=$TRASH_DIRECTORY:$PATH && + PATH=$TRASH_DIRECTORY$PATH_SEP$PATH && export TRASH_DIRECTORY ' } diff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh index 308cd28f3bd730..47b152ac5cd8ce 100755 --- a/t/t0021-conversion.sh +++ b/t/t0021-conversion.sh @@ -5,7 +5,7 @@ test_description='blob conversion via gitattributes' . ./test-lib.sh TEST_ROOT="$PWD" -PATH=$TEST_ROOT:$PATH +PATH=$TEST_ROOT$PATH_SEP$PATH write_script <<\EOF "$TEST_ROOT/rot13.sh" tr \ diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index cd74c0a4714680..6db9319ebfeec8 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -135,25 +135,25 @@ ancestor /foo /fo -1 ancestor /foo /foo -1 ancestor /foo /bar -1 ancestor /foo /foo/bar -1 -ancestor /foo /foo:/bar -1 -ancestor /foo /:/foo:/bar 0 -ancestor /foo /foo:/:/bar 0 -ancestor /foo /:/bar:/foo 0 +ancestor /foo "/foo$PATH_SEP/bar" -1 +ancestor /foo "/$PATH_SEP/foo$PATH_SEP/bar" 0 +ancestor /foo "/foo$PATH_SEP/$PATH_SEP/bar" 0 +ancestor /foo "/$PATH_SEP/bar$PATH_SEP/foo" 0 ancestor /foo/bar / 0 ancestor /foo/bar /fo -1 ancestor /foo/bar /foo 4 ancestor /foo/bar /foo/ba -1 -ancestor /foo/bar /:/fo 0 -ancestor /foo/bar /foo:/foo/ba 4 +ancestor /foo/bar "/$PATH_SEP/fo" 0 +ancestor /foo/bar "/foo$PATH_SEP/foo/ba" 4 ancestor /foo/bar /bar -1 ancestor /foo/bar /fo -1 -ancestor /foo/bar /foo:/bar 4 -ancestor /foo/bar /:/foo:/bar 4 -ancestor /foo/bar /foo:/:/bar 4 -ancestor /foo/bar /:/bar:/fo 0 -ancestor /foo/bar /:/bar 0 +ancestor /foo/bar "/foo$PATH_SEP/bar" 4 +ancestor /foo/bar "/$PATH_SEP/foo$PATH_SEP/bar" 4 +ancestor /foo/bar "/foo$PATH_SEP/$PATH_SEP/bar" 4 +ancestor /foo/bar "/$PATH_SEP/bar$PATH_SEP/fo" 0 +ancestor /foo/bar "/$PATH_SEP/bar" 0 ancestor /foo/bar /foo 4 -ancestor /foo/bar /foo:/bar 4 +ancestor /foo/bar "/foo$PATH_SEP/bar" 4 ancestor /foo/bar /bar -1 test_expect_success 'strip_path_suffix' ' diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index a51c2da8cc3756..0215c969444e98 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -47,7 +47,7 @@ test_expect_success 'run_command does not try to execute a directory' ' cat bin2/greet EOF - PATH=$PWD/bin1:$PWD/bin2:$PATH \ + PATH=$PWD/bin1$PATH_SEP$PWD/bin2$PATH_SEP$PATH \ test-tool run-command run-command greet >actual 2>err && test_cmp bin2/greet actual && test_must_be_empty err @@ -64,7 +64,7 @@ test_expect_success POSIXPERM 'run_command passes over non-executable file' ' cat bin2/greet EOF - PATH=$PWD/bin1:$PWD/bin2:$PATH \ + PATH=$PWD/bin1$PATH_SEP$PWD/bin2$PATH_SEP$PATH \ test-tool run-command run-command greet >actual 2>err && test_cmp bin2/greet actual && test_must_be_empty err @@ -84,7 +84,7 @@ test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' ' git config alias.nitfol "!echo frotz" && chmod a-rx local-command && ( - PATH=./local-command:$PATH && + PATH=./local-command$PATH_SEP$PATH && git nitfol >actual ) && echo frotz >expect && diff --git a/t/t0300-credentials.sh b/t/t0300-credentials.sh index 82eaaea0f4954d..9391dc1fd157a2 100755 --- a/t/t0300-credentials.sh +++ b/t/t0300-credentials.sh @@ -30,7 +30,7 @@ test_expect_success 'setup helper scripts' ' test -z "$pass" || echo password=$pass EOF - PATH="$PWD:$PATH" + PATH="$PWD$PATH_SEP$PATH" ' test_expect_success 'credential_fill invokes helper' ' diff --git a/t/t1504-ceiling-dirs.sh b/t/t1504-ceiling-dirs.sh index 3d51615e42d53a..dc8473345140af 100755 --- a/t/t1504-ceiling-dirs.sh +++ b/t/t1504-ceiling-dirs.sh @@ -79,9 +79,9 @@ then GIT_CEILING_DIRECTORIES="$TRASH_ROOT/top/" test_fail subdir_ceil_at_top_slash - GIT_CEILING_DIRECTORIES=":$TRASH_ROOT/top" + GIT_CEILING_DIRECTORIES="$PATH_SEP$TRASH_ROOT/top" test_prefix subdir_ceil_at_top_no_resolve "sub/dir/" - GIT_CEILING_DIRECTORIES=":$TRASH_ROOT/top/" + GIT_CEILING_DIRECTORIES="$PATH_SEP$TRASH_ROOT/top/" test_prefix subdir_ceil_at_top_slash_no_resolve "sub/dir/" fi @@ -111,13 +111,13 @@ GIT_CEILING_DIRECTORIES="$TRASH_ROOT/subdi" test_prefix subdir_ceil_at_subdi_slash "sub/dir/" -GIT_CEILING_DIRECTORIES="/foo:$TRASH_ROOT/sub" +GIT_CEILING_DIRECTORIES="/foo$PATH_SEP$TRASH_ROOT/sub" test_fail second_of_two -GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub:/bar" +GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub$PATH_SEP/bar" test_fail first_of_two -GIT_CEILING_DIRECTORIES="/foo:$TRASH_ROOT/sub:/bar" +GIT_CEILING_DIRECTORIES="/foo$PATH_SEP$TRASH_ROOT/sub$PATH_SEP/bar" test_fail second_of_three diff --git a/t/t2300-cd-to-toplevel.sh b/t/t2300-cd-to-toplevel.sh index c8de6d8a190220..91f523d5198d8d 100755 --- a/t/t2300-cd-to-toplevel.sh +++ b/t/t2300-cd-to-toplevel.sh @@ -16,7 +16,7 @@ test_cd_to_toplevel () { test_expect_success $3 "$2" ' ( cd '"'$1'"' && - PATH="$EXEC_PATH:$PATH" && + PATH="$EXEC_PATH$PATH_SEP$PATH" && . git-sh-setup && cd_to_toplevel && [ "$(pwd -P)" = "$TOPLEVEL" ] diff --git a/t/t3402-rebase-merge.sh b/t/t3402-rebase-merge.sh index a1ec501a872b9a..d6220d9e7d7d08 100755 --- a/t/t3402-rebase-merge.sh +++ b/t/t3402-rebase-merge.sh @@ -143,7 +143,7 @@ test_expect_success 'rebase -s funny -Xopt' ' git checkout -b test-funny master^ && test_commit funny && ( - PATH=./test-bin:$PATH && + PATH=./test-bin$PATH_SEP$PATH && git rebase -s funny -Xopt master ) && test -f funny.was.run diff --git a/t/t3418-rebase-continue.sh b/t/t3418-rebase-continue.sh index 25099d715cee0d..1834e450855686 100755 --- a/t/t3418-rebase-continue.sh +++ b/t/t3418-rebase-continue.sh @@ -60,7 +60,7 @@ test_expect_success 'rebase --continue remembers merge strategy and options' ' EOF chmod +x test-bin/git-merge-funny && ( - PATH=./test-bin:$PATH && + PATH=./test-bin$PATH_SEP$PATH && test_must_fail git rebase -s funny -Xopt master topic ) && test -f funny.was.run && @@ -68,7 +68,7 @@ test_expect_success 'rebase --continue remembers merge strategy and options' ' echo "Resolved" >F2 && git add F2 && ( - PATH=./test-bin:$PATH && + PATH=./test-bin$PATH_SEP$PATH && git rebase --continue ) && test -f funny.was.run @@ -92,7 +92,7 @@ test_expect_success 'rebase -i --continue handles merge strategy and options' ' EOF chmod +x test-bin/git-merge-funny && ( - PATH=./test-bin:$PATH && + PATH=./test-bin$PATH_SEP$PATH && test_must_fail git rebase -i -s funny -Xopt -Xfoo master topic ) && test -f funny.was.run && @@ -100,7 +100,7 @@ test_expect_success 'rebase -i --continue handles merge strategy and options' ' echo "Resolved" >F2 && git add F2 && ( - PATH=./test-bin:$PATH && + PATH=./test-bin$PATH_SEP$PATH && git rebase --continue ) && test -f funny.was.run diff --git a/t/t5615-alternate-env.sh b/t/t5615-alternate-env.sh index b4905b822c0704..8ce5e99c3a9618 100755 --- a/t/t5615-alternate-env.sh +++ b/t/t5615-alternate-env.sh @@ -38,7 +38,7 @@ test_expect_success 'access alternate via absolute path' ' ' test_expect_success 'access multiple alternates' ' - check_obj "$PWD/one.git/objects:$PWD/two.git/objects" <<-EOF + check_obj "$PWD/one.git/objects$PATH_SEP$PWD/two.git/objects" <<-EOF $one blob $two blob EOF @@ -74,7 +74,7 @@ test_expect_success 'access alternate via relative path (subdir)' ' quoted='"one.git\057objects"' unquoted='two.git/objects' test_expect_success 'mix of quoted and unquoted alternates' ' - check_obj "$quoted:$unquoted" <<-EOF + check_obj "$quoted$PATH_SEP$unquoted" <<-EOF $one blob $two blob EOF diff --git a/t/t5802-connect-helper.sh b/t/t5802-connect-helper.sh index c6c2661878c0ca..a096eeeeb427cf 100755 --- a/t/t5802-connect-helper.sh +++ b/t/t5802-connect-helper.sh @@ -85,7 +85,7 @@ test_expect_success 'set up fake git-daemon' ' "$TRASH_DIRECTORY/remote" EOF export TRASH_DIRECTORY && - PATH=$TRASH_DIRECTORY:$PATH + PATH=$TRASH_DIRECTORY$PATH_SEP$PATH ' test_expect_success 'ext command can connect to git daemon (no vhost)' ' diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh index 00e09a375c2e6e..95a4d7ef5bdc87 100755 --- a/t/t7006-pager.sh +++ b/t/t7006-pager.sh @@ -54,7 +54,7 @@ test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' ' sane_unset LESS LV && PAGER="env >pager-env.out; wc" && export PAGER && - PATH="$(git --exec-path):$PATH" && + PATH="$(git --exec-path)$PATH_SEP$PATH" && export PATH && test_terminal sh -c ". git-sh-setup && git_pager" ) && @@ -388,7 +388,7 @@ test_default_pager() { EOF chmod +x \$less && ( - PATH=.:\$PATH && + PATH=.$PATH_SEP\$PATH && export PATH && $full_command ) && diff --git a/t/t7606-merge-custom.sh b/t/t7606-merge-custom.sh index 8e8c4d72464098..3c2c74ae6d0e6d 100755 --- a/t/t7606-merge-custom.sh +++ b/t/t7606-merge-custom.sh @@ -23,7 +23,7 @@ test_expect_success 'set up custom strategy' ' EOF chmod +x git-merge-theirs && - PATH=.:$PATH && + PATH=.$PATH_SEP$PATH && export PATH ' diff --git a/t/t7811-grep-open.sh b/t/t7811-grep-open.sh index d1ebfd88c7a9a9..414905be48a37d 100755 --- a/t/t7811-grep-open.sh +++ b/t/t7811-grep-open.sh @@ -52,7 +52,7 @@ test_expect_success SIMPLEPAGER 'git grep -O' ' EOF echo grep.h >expect.notless && - PATH=.:$PATH git grep -O GREP_PATTERN >out && + PATH=.$PATH_SEP$PATH git grep -O GREP_PATTERN >out && { test_cmp expect.less pager-args || test_cmp expect.notless pager-args diff --git a/t/t9003-help-autocorrect.sh b/t/t9003-help-autocorrect.sh index b1c7919c4afa41..edcf912c9ecb20 100755 --- a/t/t9003-help-autocorrect.sh +++ b/t/t9003-help-autocorrect.sh @@ -12,7 +12,7 @@ test_expect_success 'setup' ' echo distimdistim was called EOF - PATH="$PATH:." && + PATH="$PATH$PATH_SEP." && export PATH && git commit --allow-empty -m "a single log entry" && diff --git a/t/t9020-remote-svn.sh b/t/t9020-remote-svn.sh index 76d9be2e1d2512..d81878d3263aea 100755 --- a/t/t9020-remote-svn.sh +++ b/t/t9020-remote-svn.sh @@ -19,7 +19,7 @@ then fi # Override svnrdump with our simulator -PATH="$HOME:$PATH" +PATH="$HOME$PATH_SEP$PATH" export PATH PYTHON_PATH GIT_BUILD_DIR write_script "$HOME/svnrdump" <<\EOF diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh index 5856563068c712..26aa039701fe76 100755 --- a/t/t9800-git-p4-basic.sh +++ b/t/t9800-git-p4-basic.sh @@ -198,7 +198,7 @@ test_expect_success 'exit when p4 fails to produce marshaled output' ' EOF chmod 755 badp4dir/p4 && ( - PATH="$TRASH_DIRECTORY/badp4dir:$PATH" && + PATH="$TRASH_DIRECTORY/badp4dir$PATH_SEP$PATH" && export PATH && test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1 ) && diff --git a/t/test-lib.sh b/t/test-lib.sh index 9574627c15cd9a..af3169fd8330c4 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -15,6 +15,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see http://www.gnu.org/licenses/ . +# On Unix/Linux, the path separator is the colon, on other systems it +# may be different, though. On Windows, for example, it is a semicolon. +# If the PATH variable contains semicolons, it is pretty safe to assume +# that the path separator is a semicolon. +case "$PATH" in +*\;*) PATH_SEP=\; ;; +*) PATH_SEP=: ;; +esac + # Test the binaries we have just built. The tests are kept in # t/ subdirectory and are run in 'trash directory' subdirectory. if test -z "$TEST_DIRECTORY" @@ -1052,7 +1061,7 @@ then done done IFS=$OLDIFS - PATH=$GIT_VALGRIND/bin:$PATH + PATH=$GIT_VALGRIND/bin$PATH_SEP$PATH GIT_EXEC_PATH=$GIT_VALGRIND/bin export GIT_VALGRIND GIT_VALGRIND_MODE="$valgrind" @@ -1064,7 +1073,7 @@ elif test -n "$GIT_TEST_INSTALLED" then GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) || error "Cannot run git from $GIT_TEST_INSTALLED." - PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH + PATH=$GIT_TEST_INSTALLED$PATH_SEP$GIT_BUILD_DIR/t/helper$PATH_SEP$PATH GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} else # normal case, use ../bin-wrappers only unless $with_dashes: if test -n "$no_bin_wrappers" @@ -1080,12 +1089,12 @@ else # normal case, use ../bin-wrappers only unless $with_dashes: fi with_dashes=t fi - PATH="$git_bin_dir:$PATH" + PATH="$git_bin_dir$PATH_SEP$PATH" fi GIT_EXEC_PATH=$GIT_BUILD_DIR if test -n "$with_dashes" then - PATH="$GIT_BUILD_DIR:$GIT_BUILD_DIR/t/helper:$PATH" + PATH="$GIT_BUILD_DIR$PATH_SEP$GIT_BUILD_DIR/t/helper$PATH_SEP$PATH" fi fi GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt From 271742c68c54b8ef14cee971cec48a939e845185 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 30 Jun 2017 00:35:40 +0200 Subject: [PATCH 378/406] mingw: only use Bash-ism `builtin pwd -W` when available Traditionally, Git for Windows' SDK uses Bash as its default shell. However, other Unix shells are available, too. Most notably, the Win32 port of BusyBox comes with `ash` whose `pwd` command already prints Windows paths as Git for Windows wants them, while there is not even a `builtin` command. Therefore, let's be careful not to override `pwd` unless we know that the `builtin` command is available. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- git-sh-setup.sh | 14 ++++++++++---- t/test-lib.sh | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/git-sh-setup.sh b/git-sh-setup.sh index 5886835fbf4863..219c687f34c832 100644 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -346,10 +346,16 @@ case $(uname -s) in /usr/bin/find "$@" } fi - # git sees Windows-style pwd - pwd () { - builtin pwd -W - } + # On Windows, Git wants Windows paths. But /usr/bin/pwd spits out + # Unix-style paths. At least in Bash, we have a builtin pwd that + # understands the -W option to force "mixed" paths, i.e. with drive + # prefix but still with forward slashes. Let's use that, if available. + if type builtin >/dev/null 2>&1 + then + pwd () { + builtin pwd -W + } + fi is_absolute_path () { case "$1" in [/\\]* | [A-Za-z]:*) diff --git a/t/test-lib.sh b/t/test-lib.sh index af3169fd8330c4..262ab1dd89c631 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1210,10 +1210,16 @@ case $uname_s in /usr/bin/find "$@" } fi - # git sees Windows-style pwd - pwd () { - builtin pwd -W - } + # On Windows, Git wants Windows paths. But /usr/bin/pwd spits out + # Unix-style paths. At least in Bash, we have a builtin pwd that + # understands the -W option to force "mixed" paths, i.e. with drive + # prefix but still with forward slashes. Let's use that, if available. + if type builtin >/dev/null 2>&1 + then + pwd () { + builtin pwd -W + } + fi # no POSIX permissions # backslashes in pathspec are converted to '/' # exec does not inherit the PID From 320ef64ff932dff35bd303a34e32c26374c2d307 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 30 Jun 2017 22:32:33 +0200 Subject: [PATCH 379/406] tests (mingw): remove Bash-specific pwd option The -W option is only understood by MSYS2 Bash's pwd command. We already make sure to override `pwd` by `builtin pwd -W` for MINGW, so let's not double the effort here. This will also help when switching the shell to another one (such as BusyBox' ash) whose pwd does *not* understand the -W option. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t9902-completion.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 175f83d7042a0f..aebd0549498f26 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -126,12 +126,7 @@ invalid_variable_name='${foo.bar}' actual="$TRASH_DIRECTORY/actual" -if test_have_prereq MINGW -then - ROOT="$(pwd -W)" -else - ROOT="$(pwd)" -fi +ROOT="$(pwd)" test_expect_success 'setup for __git_find_repo_path/__gitdir tests' ' mkdir -p subdir/subsubdir && From 3a7260e3a93081b8027a6d4679a0ccf6f088aa05 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 19 Jul 2017 17:07:56 +0200 Subject: [PATCH 380/406] test-lib: add BUSYBOX prerequisite When running with BusyBox, we will want to avoid calling executables on the PATH that are implemented in BusyBox itself. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/test-lib.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/test-lib.sh b/t/test-lib.sh index 262ab1dd89c631..8f92b8addc63b5 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1376,6 +1376,10 @@ test_lazy_prereq UNZIP ' test $? -ne 127 ' +test_lazy_prereq BUSYBOX ' + case "$($SHELL --help 2>&1)" in *BusyBox*) true;; *) false;; esac +' + run_with_limited_cmdline () { (ulimit -s 128 && "$@") } From 386d6e441df4e78ec41eebdd3ff486cfd834bda7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 4 Aug 2017 11:51:56 +0200 Subject: [PATCH 381/406] t0021: use Windows path when appropriate Since c6b0831c9c1 (docs: warn about possible '=' in clean/smudge filter process values, 2016-12-03), t0021 writes out a file with quotes in its name, and MSYS2's path conversion heuristics mistakes that to mean that we are not talking about a path here. Therefore, we need to use Windows paths, as the test-helper is a Win32 program that would otherwise have no idea where to look for the file. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t0021-conversion.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh index 47b152ac5cd8ce..f6d066cb446dab 100755 --- a/t/t0021-conversion.sh +++ b/t/t0021-conversion.sh @@ -4,8 +4,8 @@ test_description='blob conversion via gitattributes' . ./test-lib.sh -TEST_ROOT="$PWD" -PATH=$TEST_ROOT$PATH_SEP$PATH +TEST_ROOT="$(pwd)" +PATH=$PWD$PATH_SEP$PATH write_script <<\EOF "$TEST_ROOT/rot13.sh" tr \ From c224b10d4ad26c8d25fdd25f40a1302aed37f20a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Mon, 3 Jul 2017 12:37:55 +0200 Subject: [PATCH 382/406] t1300: mark all test cases with funny filenames as !MINGW On Windows, it is impossible to create a file whose name contains a quote character. We already excluded test cases using such files from running on Windows when git.exe itself was tested. However, we still had two test cases that try to create such a file, and redirect stdin from such a file, respectively. This *seems* to work in Git for Windows' Bash due to an obscure feature inherited from Cygwin: illegal filename characters are simply mapped into/from a private UTF-8 page. Pure Win32 programs (such as git.exe) *still* cannot work with those files, of course, but at least Unix shell scripts pretend to be able to. This entire strategy breaks down when switching to any Unix shell lacking support for that private UTF-8 page trick, e.g. BusyBox-w32's ash. So let's just exclude test cases that test whether the Unix shell can redirect to/from files with "funny names" those from running on Windows, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t1300-config.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t1300-config.sh b/t/t1300-config.sh index cdf1fed5d1c33e..cec9c45bff8b18 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -1738,7 +1738,7 @@ test_expect_success '--show-origin getting a single key' ' test_cmp expect output ' -test_expect_success 'set up custom config file' ' +test_expect_success !MINGW 'set up custom config file' ' CUSTOM_CONFIG_FILE="file\" (dq) and spaces.conf" && cat >"$CUSTOM_CONFIG_FILE" <<-\EOF [user] @@ -1754,7 +1754,7 @@ test_expect_success !MINGW '--show-origin escape special file name characters' ' test_cmp expect output ' -test_expect_success '--show-origin stdin' ' +test_expect_success !MINGW '--show-origin stdin' ' cat >expect <<-\EOF && standard input: user.custom=true EOF From 9dbc8de85c85bcf5bc2546e36123104fba281b10 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 5 Jul 2017 22:21:57 +0200 Subject: [PATCH 383/406] t4124: avoid using "normal" diff mode Everybody and their dogs, cats and other pets settled on using unified diffs. It is a really quaint holdover from a long-gone era that GNU diff outputs "normal" diff by default. Yet, t4124 relied on that mode. This mode is so out of fashion in the meantime, though, that e.g. BusyBox' diff decided not even to bother to support it. It only supports unified diffs. So let's just switch away from "normal" diffs and use unified diffs, as we really are only interested in the `+` lines. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t4124-apply-ws-rule.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh index 7e32237a2ae9a2..076c9d95efe6d7 100755 --- a/t/t4124-apply-ws-rule.sh +++ b/t/t4124-apply-ws-rule.sh @@ -45,7 +45,7 @@ test_fix () { apply_patch --whitespace=fix || return 1 # find touched lines - $DIFF file target | sed -n -e "s/^> //p" >fixed + $DIFF -u file target | sed -n -e "3,\$s/^+//p" >fixed # the changed lines are all expected to change fixed_cnt=$(wc -l <fixed) From 67515a06659b2c96293ab761d3a23db936ee2e01 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 5 Aug 2017 21:36:01 +0200 Subject: [PATCH 384/406] t5003: use binary file from t/diff-lib/ At some stage, t5003-archive-zip wants to add a file that is not ASCII. To that end, it uses /bin/sh. But that file may actually not exist (it is too easy to forget that not all the world is Unix/Linux...)! Besides, we already have perfectly fine binary files intended for use solely by the tests. So let's use one of them instead. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5003-archive-zip.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh index 55c78709978ff7..8bbb896f102af2 100755 --- a/t/t5003-archive-zip.sh +++ b/t/t5003-archive-zip.sh @@ -77,7 +77,7 @@ test_expect_success \ 'mkdir a && echo simple textfile >a/a && mkdir a/bin && - cp /bin/sh a/bin && + cp "$TEST_DIRECTORY/diff-lib/test-binary-1.png" a/bin && printf "text\r" >a/text.cr && printf "text\r\n" >a/text.crlf && printf "text\n" >a/text.lf && From 4f5abc6e767a49027246e1ba9a1ae74602330900 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 5 Jul 2017 22:58:26 +0200 Subject: [PATCH 385/406] t5003: skip `unzip -a` tests with BusyBox BusyBox' unzip is working pretty well. But Git's tests want to abuse it to not only extract files, but to convert their line endings on the fly, too. BusyBox' unzip does not support that, and it would appear that it would require rather intrusive changes. So let's just work around this by skipping the test case that uses `unzip -a` and the subsequent test cases expecting `unzip -a`'s output. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5003-archive-zip.sh | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh index 8bbb896f102af2..8af96e056c888b 100755 --- a/t/t5003-archive-zip.sh +++ b/t/t5003-archive-zip.sh @@ -39,33 +39,39 @@ check_zip() { extracted=${dir_with_prefix}a original=a - test_expect_success UNZIP " extract ZIP archive with EOL conversion" ' + test_expect_success !BUSYBOX,UNZIP \ + " extract ZIP archive with EOL conversion" ' (mkdir $dir && cd $dir && "$GIT_UNZIP" -a ../$zipfile) ' - test_expect_success UNZIP " validate that text files are converted" " + test_expect_success !BUSYBOX,UNZIP \ + " validate that text files are converted" " test_cmp_bin $extracted/text.cr $extracted/text.crlf && test_cmp_bin $extracted/text.cr $extracted/text.lf " - test_expect_success UNZIP " validate that binary files are unchanged" " + test_expect_success !BUSYBOX,UNZIP \ + " validate that binary files are unchanged" " test_cmp_bin $original/binary.cr $extracted/binary.cr && test_cmp_bin $original/binary.crlf $extracted/binary.crlf && test_cmp_bin $original/binary.lf $extracted/binary.lf " - test_expect_success UNZIP " validate that diff files are converted" " + test_expect_success !BUSYBOX,UNZIP \ + " validate that diff files are converted" " test_cmp_bin $extracted/diff.cr $extracted/diff.crlf && test_cmp_bin $extracted/diff.cr $extracted/diff.lf " - test_expect_success UNZIP " validate that -diff files are unchanged" " + test_expect_success !BUSYBOX,UNZIP \ + " validate that -diff files are unchanged" " test_cmp_bin $original/nodiff.cr $extracted/nodiff.cr && test_cmp_bin $original/nodiff.crlf $extracted/nodiff.crlf && test_cmp_bin $original/nodiff.lf $extracted/nodiff.lf " - test_expect_success UNZIP " validate that custom diff is unchanged " " + test_expect_success !BUSYBOX,UNZIP \ + " validate that custom diff is unchanged " " test_cmp_bin $original/custom.cr $extracted/custom.cr && test_cmp_bin $original/custom.crlf $extracted/custom.crlf && test_cmp_bin $original/custom.lf $extracted/custom.lf From 51045133f663905fd54b7b60de62ae7b5e1b0160 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 21 Jul 2017 12:48:33 +0200 Subject: [PATCH 386/406] t5532: workaround for BusyBox on Windows While it may seem super convenient to some old Unix hands to simpy require Perl to be available when running the test suite, this is a major hassle on Windows, where we want to verify that Perl is not, actually, required in a NO_PERL build. As a super ugly workaround, we "install" a script into /usr/bin/perl reading like this: #!/bin/sh # We'd much rather avoid requiring Perl altogether when testing # an installed Git. Oh well, that's why we cannot have nice # things. exec c:/git-sdk-64/usr/bin/perl.exe "$@" The problem with that is that BusyBox assumes that the #! line in a script refers to an executable, not to a script. So when it encounters the line #!/usr/bin/perl in t5532's proxy-get-cmd, it barfs. Let's help this situation by simply executing the Perl script with the "interpreter" specified explicitly. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5532-fetch-proxy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t5532-fetch-proxy.sh b/t/t5532-fetch-proxy.sh index 9c2798603b4d7b..11fc3f2eea4baf 100755 --- a/t/t5532-fetch-proxy.sh +++ b/t/t5532-fetch-proxy.sh @@ -25,7 +25,7 @@ test_expect_success 'setup proxy script' ' write_script proxy <<-\EOF echo >&2 "proxying for $*" - cmd=$(./proxy-get-cmd) + cmd=$("$PERL_PATH" ./proxy-get-cmd) echo >&2 "Running $cmd" exec $cmd EOF From d6283e313ac4ec32f906a33e50502814a093c2d5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 21 Jul 2017 13:24:55 +0200 Subject: [PATCH 387/406] t5605: special-case hardlink test for BusyBox-w32 When t5605 tries to verify that files are hardlinked (or that they are not), it uses the `-links` option of the `find` utility. BusyBox' implementation does not support that option, and BusyBox-w32's lstat() does not even report the number of hard links correctly (for performance reasons). So let's just switch to a different method that actually works on Windows. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5605-clone-local.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/t/t5605-clone-local.sh b/t/t5605-clone-local.sh index af23419ebfc15d..693434746189da 100755 --- a/t/t5605-clone-local.sh +++ b/t/t5605-clone-local.sh @@ -8,6 +8,21 @@ repo_is_hardlinked() { test_line_count = 0 output } +if test_have_prereq MINGW,BUSYBOX +then + # BusyBox' `find` does not support `-links`. Besides, BusyBox-w32's + # lstat() does not report hard links, just like Git's mingw_lstat() + # (from where BusyBox-w32 got its initial implementation). + repo_is_hardlinked() { + for f in $(find "$1/objects" -type f) + do + "$SYSTEMROOT"/system32/fsutil.exe \ + hardlink list $f >links && + test_line_count -gt 1 links || return 1 + done + } +fi + test_expect_success 'preparing origin repository' ' : >file && git add . && git commit -m1 && git clone --bare . a.git && From 8a4dc8d27d8c28d263ad54645eac1da9b66d5182 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 5 Jul 2017 15:14:50 +0200 Subject: [PATCH 388/406] t5813: allow for $PWD to be a Windows path Git for Windows uses MSYS2's Bash to run the test suite, which comes with benefits but also at a heavy price: on the plus side, MSYS2's POSIX emulation layer allows us to continue pretending that we are on a Unix system, e.g. use Unix paths instead of Windows ones, yet this is bought at a rather noticeable performance penalty. There *are* some more native ports of Unix shells out there, though, most notably BusyBox-w32's ash. These native ports do not use any POSIX emulation layer (or at most a *very* thin one, choosing to avoid features such as fork() that are expensive to emulate on Windows), and they use native Windows paths (usually with forward slashes instead of backslashes, which is perfectly legal in almost all use cases). And here comes the problem: with a $PWD looking like, say, C:/git-sdk-64/usr/src/git/t/trash directory.t5813-proto-disable-ssh Git's test scripts get quite a bit confused, as their assumptions have been shattered. Not only does this path contain a colon (oh no!), it also does not start with a slash. This is a problem e.g. when constructing a URL as t5813 does it: ssh://remote$PWD. Not only is it impossible to separate the "host" from the path with a $PWD as above, even prefixing $PWD by a slash won't work, as /C:/git-sdk-64/... is not a valid path. As a workaround, detect when $PWD does not start with a slash on Windows, and simply strip the drive prefix, using an obscure feature of Windows paths: if an absolute Windows path starts with a slash, it is implicitly prefixed by the drive prefix of the current directory. As we are talking about the current directory here, anyway, that strategy works. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t5813-proto-disable-ssh.sh | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/t/t5813-proto-disable-ssh.sh b/t/t5813-proto-disable-ssh.sh index 3f084ee306517b..0a2c77093babad 100755 --- a/t/t5813-proto-disable-ssh.sh +++ b/t/t5813-proto-disable-ssh.sh @@ -14,8 +14,23 @@ test_expect_success 'setup repository to clone' ' ' test_proto "host:path" ssh "remote:repo.git" -test_proto "ssh://" ssh "ssh://remote$PWD/remote/repo.git" -test_proto "git+ssh://" ssh "git+ssh://remote$PWD/remote/repo.git" + +hostdir="$PWD" +if test_have_prereq MINGW && test "/${PWD#/}" != "$PWD" +then + case "$PWD" in + [A-Za-z]:/*) + hostdir="${PWD#?:}" + ;; + *) + skip_all="Unhandled PWD '$PWD'; skipping rest" + test_done + ;; + esac +fi + +test_proto "ssh://" ssh "ssh://remote$hostdir/remote/repo.git" +test_proto "git+ssh://" ssh "git+ssh://remote$hostdir/remote/repo.git" # Don't even bother setting up a "-remote" directory, as ssh would generally # complain about the bogus option rather than completing our request. Our From fc47e8e91e6b9f17ad20831dc72cde03f6816e13 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Wed, 19 Jul 2017 22:13:16 +0200 Subject: [PATCH 389/406] t7063: when running under BusyBox, avoid unsupported find option BusyBox' find implementation does not understand the -ls option, so let's not use it when we're running inside BusyBox. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t7063-status-untracked-cache.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/t/t7063-status-untracked-cache.sh b/t/t7063-status-untracked-cache.sh index 2da57fce7b12bd..33376d13a3328d 100755 --- a/t/t7063-status-untracked-cache.sh +++ b/t/t7063-status-untracked-cache.sh @@ -18,7 +18,12 @@ GIT_FORCE_UNTRACKED_CACHE=true export GIT_FORCE_UNTRACKED_CACHE sync_mtime () { - find . -type d -ls >/dev/null + if test_have_prereq BUSYBOX + then + find . -type d -print0 | xargs -0r ls -ld >/dev/null + else + find . -type d -ls >/dev/null + fi } avoid_racy() { From a94aae8a1a2192fe26122db5649590b98e5fdf30 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 7 Jul 2017 10:15:36 +0200 Subject: [PATCH 390/406] t9200: skip tests when $PWD contains a colon On Windows, the current working directory is pretty much guaranteed to contain a colon. If we feed that path to CVS, it mistakes it for a separator between host and port, though. This has not been a problem so far because Git for Windows uses MSYS2's Bash using a POSIX emulation layer that also pretends that the current directory is a Unix path (at least as long as we're in a shell script). However, that is rather limiting, as Git for Windows also explores other ports of other Unix shells. One of those is BusyBox-w32's ash, which is a native port (i.e. *not* using any POSIX emulation layer, and certainly not emulating Unix paths). So let's just detect if there is a colon in $PWD and punt in that case. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t9200-git-cvsexportcommit.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh index 52ae42c325bc79..d2735e5029571c 100755 --- a/t/t9200-git-cvsexportcommit.sh +++ b/t/t9200-git-cvsexportcommit.sh @@ -11,6 +11,13 @@ if ! test_have_prereq PERL; then test_done fi +case "$PWD" in +*:*) + skip_all='cvs would get confused by the colon in `pwd`; skipping tests' + test_done + ;; +esac + cvs >/dev/null 2>&1 if test $? -ne 1 then From 7024d4b6b1cb6d6f5ffd25f299975107074e23c9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 23 Feb 2018 02:50:03 +0100 Subject: [PATCH 391/406] mingw (git_terminal_prompt): work around BusyBox & WSL issues When trying to query the user directly via /dev/tty, both WSL's bash and BusyBox' bash emulation seem to have problems printing the value that they just read. The bash just stops in those instances, does not even execute any commands after the echo command. Let's just work around this by running the Bash snippet only in MSYS2's Bash: its `SHELL` variable has the `.exe` suffix, and neither WSL's nor BusyBox' bash set the `SHELL` variable to a path with that suffix. In the latter case, we simply exit with code 127 (indicating that the command was not found) and fall back to the CONIN$/CONOUT$ method quietly. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/terminal.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index d9d3945afa391f..703204755853cf 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -101,8 +101,10 @@ static char *shell_prompt(const char *prompt, int echo) const char *read_input[] = { /* Note: call 'bash' explicitly, as 'read -s' is bash-specific */ "bash", "-c", echo ? - "cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" : - "cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty", + "test \"a$SHELL\" != \"a${SHELL%.exe}\" || exit 127; cat >/dev/tty &&" + " read -r line </dev/tty && echo \"$line\"" : + "test \"a$SHELL\" != \"a${SHELL%.exe}\" || exit 127; cat >/dev/tty &&" + " read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty", NULL }; struct child_process child = CHILD_PROCESS_INIT; @@ -138,7 +140,10 @@ static char *shell_prompt(const char *prompt, int echo) close(child.out); code = finish_command(&child); if (code) { - error("failed to execute prompt script (exit code %d)", code); + if (code != 127) + error("failed to execute prompt script (exit code %d)", + code); + return NULL; } From 066512910ac696f3a96e55993ce8ebb010a50028 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Sat, 8 Jul 2017 21:49:12 +0200 Subject: [PATCH 392/406] t9350: skip ISO-8859-1 test when the environment is always-UTF-8 In the BusyBox-w32 version that is currently under consideration for MinGit for Windows (to reduce the .zip size, and to avoid problems with the MSYS2 runtime), the UTF-16 environment present in Windows is considered to be authoritative, and the 8-bit version is always in UTF-8 encoding. As a consequence, the ISO-8859-1 test in t9350-fast-export (which tries to set GIT_AUTHOR_NAME to a ISO-8859-1 encoded value) *must* fail in that setup. So let's detect when it would fail (due to an environment being purely kept UTF-8 encoded), and skip that test in that case. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- t/t9350-fast-export.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/t/t9350-fast-export.sh b/t/t9350-fast-export.sh index 6d6d5050a299b6..cd85f24dd7d340 100755 --- a/t/t9350-fast-export.sh +++ b/t/t9350-fast-export.sh @@ -66,7 +66,12 @@ test_expect_success 'fast-export master~2..master' ' ' -test_expect_success 'iso-8859-1' ' +test_lazy_prereq UTF8_ONLY_ENV ' + . "$TEST_DIRECTORY"/t3901/8859-1.txt && + ! git var GIT_AUTHOR_IDENT | grep "Áéí" +' + +test_expect_success !UTF8_ONLY_ENV 'iso-8859-1' ' git config i18n.commitencoding ISO8859-1 && # use author and committer name in ISO-8859-1 to match it. @@ -82,6 +87,11 @@ test_expect_success 'iso-8859-1' ' grep "Áéí óú" actual) ' + +# The subsequent tests validate timestamps, and we may just have skipped a tick +test_have_prereq !UTF8_ONLY_ENV || +test_tick + test_expect_success 'import/export-marks' ' git checkout -b marks master && @@ -196,7 +206,7 @@ GIT_COMMITTER_NAME='C O Mitter'; export GIT_COMMITTER_NAME test_expect_success 'setup copies' ' - git config --unset i18n.commitencoding && + { git config --unset i18n.commitencoding || :; } && git checkout -b copy rein && git mv file file3 && git commit -m move1 && From 2e74317302b62c023a40e6b037b89f3d53bf2e95 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 23 Feb 2018 02:50:03 +0100 Subject: [PATCH 393/406] mingw (git_terminal_prompt): do fall back to CONIN$/CONOUT$ method To support Git Bash running in a MinTTY, we use a dirty trick to access the MSYS2 pseudo terminal: we execute a Bash snippet that accesses /dev/tty. The idea was to fall back to writing to/reading from CONOUT$/CONIN$ if that Bash call failed because Bash was not found. However, we should fall back even in other error conditions, because we have not successfully read the user input. Let's make it so. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/terminal.c b/compat/terminal.c index 703204755853cf..561d339f44c0b8 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -166,7 +166,7 @@ char *git_terminal_prompt(const char *prompt, int echo) /* try shell_prompt first, fall back to CONIN/OUT if bash is missing */ char *result = shell_prompt(prompt, echo); - if (result || errno != ENOENT) + if (result) return result; #endif From d5f3d5c7c1912de573e76fa23942a466812a6ecf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Thu, 20 Jul 2017 00:23:26 +0200 Subject: [PATCH 394/406] mingw: add a Makefile target to copy test artifacts The Makefile target `install-mingit-test-artifacts` simply copies stuff and things directly into a MinGit directory, including an init.bat script to set everything up so that the tests can be run in a cmd window. Sadly, Git's test suite still relies on a Perl interpreter even if compiled with NO_PERL=YesPlease. We punt for now, installing a small script into /usr/bin/perl that hands off to an existing Perl of a Git for Windows SDK. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- config.mak.uname | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/config.mak.uname b/config.mak.uname index d315df0e23dbc1..fa44d2bfe869ae 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -649,6 +649,65 @@ else NO_CURL = YesPlease endif endif +ifeq (i686,$(uname_M)) + MINGW_PREFIX := mingw32 +endif +ifeq (x86_64,$(uname_M)) + MINGW_PREFIX := mingw64 +endif + + DESTDIR_WINDOWS = $(shell cygpath -aw '$(DESTDIR_SQ)') + DESTDIR_MIXED = $(shell cygpath -am '$(DESTDIR_SQ)') +install-mingit-test-artifacts: + install -m755 -d '$(DESTDIR_SQ)/usr/bin' + printf '%s\n%s\n' >'$(DESTDIR_SQ)/usr/bin/perl' \ + "#!/mingw64/bin/busybox sh" \ + "exec \"$(shell cygpath -am /usr/bin/perl.exe)\" \"\$$@\"" + + install -m755 -d '$(DESTDIR_SQ)' + printf '%s%s\n%s\n%s\n%s\n%s\n' >'$(DESTDIR_SQ)/init.bat' \ + "PATH=$(DESTDIR_WINDOWS)\\$(MINGW_PREFIX)\\bin;" \ + "C:\\WINDOWS;C:\\WINDOWS\\system32" \ + "@set GIT_TEST_INSTALLED=$(DESTDIR_MIXED)/$(MINGW_PREFIX)/bin" \ + "@`echo "$(DESTDIR_WINDOWS)" | sed 's/:.*/:/'`" \ + "@cd `echo "$(DESTDIR_WINDOWS)" | sed 's/^.://'`\\test-git\\t" \ + "@echo Now, run 'helper\\test-run-command testsuite'" + + install -m755 -d '$(DESTDIR_SQ)/test-git' + sed 's/^\(NO_PERL\|NO_PYTHON\)=.*/\1=YesPlease/' \ + <GIT-BUILD-OPTIONS >'$(DESTDIR_SQ)/test-git/GIT-BUILD-OPTIONS' + + install -m755 -d '$(DESTDIR_SQ)/test-git/t/helper' + install -m755 $(TEST_PROGRAMS) '$(DESTDIR_SQ)/test-git/t/helper' + (cd t && $(TAR) cf - t[0-9][0-9][0-9][0-9] diff-lib) | \ + (cd '$(DESTDIR_SQ)/test-git/t' && $(TAR) xf -) + install -m755 t/t556x_common t/*.sh '$(DESTDIR_SQ)/test-git/t' + + install -m755 -d '$(DESTDIR_SQ)/test-git/templates' + (cd templates && $(TAR) cf - blt) | \ + (cd '$(DESTDIR_SQ)/test-git/templates' && $(TAR) xf -) + + # po/build/locale for t0200 + install -m755 -d '$(DESTDIR_SQ)/test-git/po/build/locale' + (cd po/build/locale && $(TAR) cf - .) | \ + (cd '$(DESTDIR_SQ)/test-git/po/build/locale' && $(TAR) xf -) + + # git-daemon.exe for t5802, git-http-backend.exe for t5560 + install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin' + install -m755 git-daemon.exe git-http-backend.exe \ + '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin' + + # git-remote-testgit for t5801 + install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core' + install -m755 git-remote-testgit \ + '$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core' + + # git-upload-archive (dashed) for t5000 + install -m755 git-upload-archive.exe '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin' + + # git-difftool--helper for t7800 + install -m755 git-difftool--helper \ + '$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core' endif ifeq ($(uname_S),QNX) COMPAT_CFLAGS += -DSA_RESTART=0 From 6d8ad1a1882fc43b5108f26553480f25aad10984 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Fri, 23 Feb 2018 02:50:03 +0100 Subject: [PATCH 395/406] mingw (git_terminal_prompt): turn on echo explictly It turns out that when running in a Powershell window, we need to turn on ENABLE_ECHO_INPUT because the default would be *not* to echo anything. This also ensures that we use the input mode where all input is read until the user hits the Return key. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/terminal.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 561d339f44c0b8..00eb4c51470831 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -77,17 +77,26 @@ static void restore_term(void) hconin = INVALID_HANDLE_VALUE; } -static int disable_echo(void) +static int set_echo(int echo) { - hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ, NULL, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, NULL); + DWORD new_cmode; + + if (hconin == INVALID_HANDLE_VALUE) + hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); if (hconin == INVALID_HANDLE_VALUE) return -1; GetConsoleMode(hconin, &cmode); + new_cmode = cmode | ENABLE_LINE_INPUT; + if (echo) + new_cmode |= ENABLE_ECHO_INPUT; + else + new_cmode &= ~ENABLE_ECHO_INPUT; + sigchain_push_common(restore_term_on_signal); - if (!SetConsoleMode(hconin, cmode & (~ENABLE_ECHO_INPUT))) { + if (!SetConsoleMode(hconin, new_cmode)) { CloseHandle(hconin); hconin = INVALID_HANDLE_VALUE; return -1; @@ -96,6 +105,11 @@ static int disable_echo(void) return 0; } +static int disable_echo(void) +{ + return set_echo(0); +} + static char *shell_prompt(const char *prompt, int echo) { const char *read_input[] = { @@ -169,6 +183,8 @@ char *git_terminal_prompt(const char *prompt, int echo) if (result) return result; + if (echo && set_echo(1)) + return NULL; #endif input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT); From 33609ae3aaf9af907eb27d253933b6cc9bc7f220 Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Tue, 23 Oct 2018 11:42:06 -0400 Subject: [PATCH 396/406] fscache: use FindFirstFileExW to avoid retrieving the short name Use FindFirstFileExW with FindExInfoBasic to avoid forcing NTFS to look up the short name. Also switch to a larger (64K vs 4K) buffer using FIND_FIRST_EX_LARGE_FETCH to minimize round trips to the kernel. In a repo with ~200K files, this drops warm cache status times from 3.19 seconds to 2.67 seconds for a 16% savings. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- compat/win32/fscache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 2f6a4f9eb80c77..aa9df4f4f742f6 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -206,7 +206,8 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir, pattern[wlen] = 0; /* open find handle */ - h = FindFirstFileW(pattern, &fdata); + h = FindFirstFileExW(pattern, FindExInfoBasic, &fdata, FindExSearchNameMatch, + NULL, FIND_FIRST_EX_LARGE_FETCH); if (h == INVALID_HANDLE_VALUE) { err = GetLastError(); *dir_not_found = 1; /* or empty directory */ From a4427c477233b945be45e990d844546c8e4f4acd Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Thu, 1 Nov 2018 11:40:51 -0400 Subject: [PATCH 397/406] status: disable and free fscache at the end of the status command At the end of the status command, disable and free the fscache so that we don't leak the memory and so that we can dump the fscache statistics. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- builtin/commit.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/commit.c b/builtin/commit.c index 67fa9492040a80..8445d19dee4fe9 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1410,6 +1410,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) s.prefix = prefix; wt_status_print(&s); + enable_fscache(0); return 0; } From 1ca73e363c21318ac7a31a45d4b40de3d7da9abb Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Thu, 4 Oct 2018 18:10:21 -0400 Subject: [PATCH 398/406] fscache: add GIT_TEST_FSCACHE support Add support to fscache to enable running the entire test suite with the fscache enabled. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- compat/win32/fscache.c | 5 +++++ t/README | 3 +++ 2 files changed, 8 insertions(+) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index aa9df4f4f742f6..bafe42b9c9f7c6 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -2,6 +2,7 @@ #include "../../hashmap.h" #include "../win32.h" #include "fscache.h" +#include "config.h" static int initialized; static volatile long enabled; @@ -397,7 +398,11 @@ int fscache_enable(int enable) int result; if (!initialized) { + int fscache = git_env_bool("GIT_TEST_FSCACHE", -1); + /* allow the cache to be disabled entirely */ + if (fscache != -1) + core_fscache = fscache; if (!core_fscache) return 0; diff --git a/t/README b/t/README index 31b0daed95525c..24beceabdd0d1c 100644 --- a/t/README +++ b/t/README @@ -328,6 +328,9 @@ GIT_TEST_OE_DELTA_SIZE=<n> exercises the uncomon pack-objects code path where deltas larger than this limit require extra memory allocation for bookkeeping. +GIT_TEST_FSCACHE=<boolean> exercises the uncommon fscache code path +which adds a cache below mingw's lstat and dirent implementations. + Naming Tests ------------ From 20468043a784dd6d9c1771c3306c8144f564ee28 Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Mon, 5 Nov 2018 08:38:32 -0500 Subject: [PATCH 399/406] At the end of the add command, disable and free the fscache so that we don't leak the memory and so that we can dump the fscache statistics. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- builtin/add.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/add.c b/builtin/add.c index 0ff2cd9d49f668..f8ebf1c8e39753 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -542,6 +542,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) COMMIT_LOCK | SKIP_IF_UNCHANGED)) die(_("Unable to write new index file")); + enable_fscache(0); UNLEAK(pathspec); UNLEAK(dir); return exit_status; From d569fecba1aeb6571e38161a7329c389354198eb Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Thu, 4 Oct 2018 18:10:21 -0400 Subject: [PATCH 400/406] mem_pool: add GIT_TRACE_MEMPOOL support Add tracing around initializing and discarding mempools. In discard report on the amount of memory unused in the current block to help tune setting the initial_size. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- mem-pool.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mem-pool.c b/mem-pool.c index a2841a4a9ad7e4..065389aaecf918 100644 --- a/mem-pool.c +++ b/mem-pool.c @@ -5,6 +5,7 @@ #include "cache.h" #include "mem-pool.h" +static struct trace_key trace_mem_pool = TRACE_KEY_INIT(MEMPOOL); #define BLOCK_GROWTH_SIZE 1024*1024 - sizeof(struct mp_block); /* @@ -48,12 +49,16 @@ void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size) mem_pool_alloc_block(pool, initial_size, NULL); *mem_pool = pool; + trace_printf_key(&trace_mem_pool, "mem_pool (%p): init (%"PRIuMAX") initial size\n", + pool, (uintmax_t)initial_size); } void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) { struct mp_block *block, *block_to_free; + trace_printf_key(&trace_mem_pool, "mem_pool (%p): discard (%"PRIuMAX") unused\n", + mem_pool, (uintmax_t)(mem_pool->mp_block->end - mem_pool->mp_block->next_free)); block = mem_pool->mp_block; while (block) { From be35dedc8489ab7013ed9be0908614665d04f358 Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Fri, 2 Nov 2018 11:19:10 -0400 Subject: [PATCH 401/406] fscache: fscache takes an initial size Update enable_fscache() to take an optional initial size parameter which is used to initialize the hashmap so that it can avoid having to rehash as additional entries are added. Add a separate disable_fscache() macro to make the code clearer and easier to read. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- builtin/add.c | 2 +- builtin/checkout.c | 4 ++-- builtin/commit.c | 4 ++-- compat/win32/fscache.c | 8 ++++++-- compat/win32/fscache.h | 5 +++-- fetch-pack.c | 4 ++-- git-compat-util.h | 4 ++++ preload-index.c | 4 ++-- read-cache.c | 4 ++-- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/builtin/add.c b/builtin/add.c index f8ebf1c8e39753..1fab75ff6e0b2e 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -461,7 +461,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) die_path_inside_submodule(&the_index, &pathspec); - enable_fscache(1); + enable_fscache(0); /* We do not really re-read the index but update the up-to-date flags */ preload_index(&the_index, &pathspec); diff --git a/builtin/checkout.c b/builtin/checkout.c index 07cc016e5cc3bd..ec0318231f09eb 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -360,7 +360,7 @@ static int checkout_paths(const struct checkout_opts *opts, state.istate = &the_index; enable_delayed_checkout(&state); - enable_fscache(1); + enable_fscache(active_nr); for (pos = 0; pos < active_nr; pos++) { struct cache_entry *ce = active_cache[pos]; if (ce->ce_flags & CE_MATCHED) { @@ -375,7 +375,7 @@ static int checkout_paths(const struct checkout_opts *opts, pos = skip_same_name(ce, pos) - 1; } } - enable_fscache(0); + disable_fscache(); errs |= finish_delayed_checkout(&state); if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) diff --git a/builtin/commit.c b/builtin/commit.c index 8445d19dee4fe9..e98432a7525a33 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1376,7 +1376,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) PATHSPEC_PREFER_FULL, prefix, argv); - enable_fscache(1); + enable_fscache(0); read_cache_preload(&s.pathspec); refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL); @@ -1410,7 +1410,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) s.prefix = prefix; wt_status_print(&s); - enable_fscache(0); + disable_fscache(); return 0; } diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index b81eb62e4f5dc1..96fe7e335c352f 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -401,7 +401,7 @@ static struct fsentry *fscache_get(struct fsentry *key) * Enables or disables the cache. Note that the cache is read-only, changes to * the working directory are NOT reflected in the cache while enabled. */ -int fscache_enable(int enable) +int fscache_enable(int enable, size_t initial_size) { int result; @@ -417,7 +417,11 @@ int fscache_enable(int enable) InitializeCriticalSection(&mutex); lstat_requests = opendir_requests = 0; fscache_misses = fscache_requests = 0; - hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0); + /* + * avoid having to rehash by leaving room for the parent dirs. + * '4' was determined empirically by testing several repos + */ + hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4); initialized = 1; } diff --git a/compat/win32/fscache.h b/compat/win32/fscache.h index 2f06f8df97dcd0..d49c9381114da6 100644 --- a/compat/win32/fscache.h +++ b/compat/win32/fscache.h @@ -1,8 +1,9 @@ #ifndef FSCACHE_H #define FSCACHE_H -int fscache_enable(int enable); -#define enable_fscache(x) fscache_enable(x) +int fscache_enable(int enable, size_t initial_size); +#define enable_fscache(initial_size) fscache_enable(1, initial_size) +#define disable_fscache() fscache_enable(0, 0) int fscache_enabled(const char *path); #define is_fscache_enabled(path) fscache_enabled(path) diff --git a/fetch-pack.c b/fetch-pack.c index 43e0945da18021..d4f27db27cc024 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -678,7 +678,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, save_commit_buffer = 0; - enable_fscache(1); + enable_fscache(0); for (ref = *refs; ref; ref = ref->next) { struct object *o; unsigned int flags = OBJECT_INFO_QUICK; @@ -708,7 +708,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, cutoff = commit->date; } } - enable_fscache(0); + disable_fscache(); oidset_clear(&loose_oid_set); diff --git a/git-compat-util.h b/git-compat-util.h index 64a5d43a0e7860..e1b9e55683f204 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1259,6 +1259,10 @@ static inline int is_missing_file_error(int errno_) #define enable_fscache(x) /* noop */ #endif +#ifndef disable_fscache +#define disable_fscache() /* noop */ +#endif + #ifndef is_fscache_enabled #define is_fscache_enabled(path) (0) #endif diff --git a/preload-index.c b/preload-index.c index 6857573ce6de0a..af34c3dc59112a 100644 --- a/preload-index.c +++ b/preload-index.c @@ -91,7 +91,7 @@ void preload_index(struct index_state *index, const struct pathspec *pathspec) offset = 0; work = DIV_ROUND_UP(index->cache_nr, threads); memset(&data, 0, sizeof(data)); - enable_fscache(1); + enable_fscache(index->cache_nr); for (i = 0; i < threads; i++) { struct thread_data *p = data+i; p->index = index; @@ -109,7 +109,7 @@ void preload_index(struct index_state *index, const struct pathspec *pathspec) die("unable to join threaded lstat"); } trace_performance_since(start, "preload index"); - enable_fscache(0); + disable_fscache(); } #endif diff --git a/read-cache.c b/read-cache.c index 18b6fb42144ecb..6be61e5d0c887b 100644 --- a/read-cache.c +++ b/read-cache.c @@ -1483,7 +1483,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, typechange_fmt = (in_porcelain ? "T\t%s\n" : "%s needs update\n"); added_fmt = (in_porcelain ? "A\t%s\n" : "%s needs update\n"); unmerged_fmt = (in_porcelain ? "U\t%s\n" : "%s: needs merge\n"); - enable_fscache(1); + enable_fscache(0); for (i = 0; i < istate->cache_nr; i++) { struct cache_entry *ce, *new_entry; int cache_errno = 0; @@ -1548,7 +1548,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, replace_index_entry(istate, i, new_entry); } - enable_fscache(0); + disable_fscache(); trace_performance_since(start, "refresh index"); return has_errors; } From f1bf0982961bcd7b4c45e44564dbe7fde55328cd Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Thu, 4 Oct 2018 15:38:08 -0400 Subject: [PATCH 402/406] fscache: update fscache to be thread specific instead of global The threading model for fscache has been to have a single, global cache. This puts requirements on it to be thread safe so that callers like preload-index can call it from multiple threads. This was implemented with a single mutex and completion events which introduces contention between the calling threads. Simplify the threading model by making fscache thread specific. This allows us to remove the global mutex and synchronization events entirely and instead associate a fscache with every thread that requests one. This works well with the current multi-threading which divides the cache entries into blocks with a separate thread processing each block. At the end of each worker thread, if there is a fscache on the primary thread, merge the cached results from the worker into the primary thread cache. This enables us to reuse the cache later especially when scanning for untracked files. In testing, this reduced the time spent in preload_index() by about 25% and also reduced the CPU utilization significantly. On a repo with ~200K files, it reduced overall status times by ~12%. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- compat/win32/fscache.c | 292 +++++++++++++++++++++++++---------------- compat/win32/fscache.h | 22 +++- git-compat-util.h | 12 ++ preload-index.c | 7 +- 4 files changed, 213 insertions(+), 120 deletions(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index 96fe7e335c352f..c3d76bf320d5d2 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -4,14 +4,24 @@ #include "fscache.h" #include "config.h" -static int initialized; -static volatile long enabled; -static struct hashmap map; +static volatile long initialized; +static DWORD dwTlsIndex; static CRITICAL_SECTION mutex; -static unsigned int lstat_requests; -static unsigned int opendir_requests; -static unsigned int fscache_requests; -static unsigned int fscache_misses; + +/* + * Store one fscache per thread to avoid thread contention and locking. + * This is ok because multi-threaded access is 1) uncommon and 2) always + * splitting up the cache entries across multiple threads so there isn't + * any overlap between threads anyway. + */ +struct fscache { + volatile long enabled; + struct hashmap map; + unsigned int lstat_requests; + unsigned int opendir_requests; + unsigned int fscache_requests; + unsigned int fscache_misses; +}; static struct trace_key trace_fscache = TRACE_KEY_INIT(FSCACHE); /* @@ -39,8 +49,6 @@ struct fsentry { union { /* Reference count of the directory listing. */ volatile long refcnt; - /* Handle to wait on the loading thread. */ - HANDLE hwait; struct { /* More stat members (only used for file entries). */ off64_t st_size; @@ -250,86 +258,63 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir, /* * Adds a directory listing to the cache. */ -static void fscache_add(struct fsentry *fse) +static void fscache_add(struct fscache *cache, struct fsentry *fse) { if (fse->list) fse = fse->list; for (; fse; fse = fse->next) - hashmap_add(&map, fse); + hashmap_add(&cache->map, fse); } /* * Clears the cache. */ -static void fscache_clear(void) +static void fscache_clear(struct fscache *cache) { - hashmap_free(&map, 1); - hashmap_init(&map, (hashmap_cmp_fn)fsentry_cmp, NULL, 0); - lstat_requests = opendir_requests = 0; - fscache_misses = fscache_requests = 0; + hashmap_free(&cache->map, 1); + hashmap_init(&cache->map, (hashmap_cmp_fn)fsentry_cmp, NULL, 0); + cache->lstat_requests = cache->opendir_requests = 0; + cache->fscache_misses = cache->fscache_requests = 0; } /* * Checks if the cache is enabled for the given path. */ -int fscache_enabled(const char *path) +static int do_fscache_enabled(struct fscache *cache, const char *path) { - return enabled > 0 && !is_absolute_path(path); + return cache->enabled > 0 && !is_absolute_path(path); } -/* - * Looks up a cache entry, waits if its being loaded by another thread. - * The mutex must be owned by the calling thread. - */ -static struct fsentry *fscache_get_wait(struct fsentry *key) +int fscache_enabled(const char *path) { - struct fsentry *fse = hashmap_get(&map, key, NULL); - - /* return if its a 'real' entry (future entries have refcnt == 0) */ - if (!fse || fse->list || fse->refcnt) - return fse; - - /* create an event and link our key to the future entry */ - key->hwait = CreateEvent(NULL, TRUE, FALSE, NULL); - key->next = fse->next; - fse->next = key; - - /* wait for the loading thread to signal us */ - LeaveCriticalSection(&mutex); - WaitForSingleObject(key->hwait, INFINITE); - CloseHandle(key->hwait); - EnterCriticalSection(&mutex); + struct fscache *cache = fscache_getcache(); - /* repeat cache lookup */ - return hashmap_get(&map, key, NULL); + return cache ? do_fscache_enabled(cache, path) : 0; } /* * Looks up or creates a cache entry for the specified key. */ -static struct fsentry *fscache_get(struct fsentry *key) +static struct fsentry *fscache_get(struct fscache *cache, struct fsentry *key) { - struct fsentry *fse, *future, *waiter; + struct fsentry *fse; int dir_not_found; - EnterCriticalSection(&mutex); - fscache_requests++; + cache->fscache_requests++; /* check if entry is in cache */ - fse = fscache_get_wait(key); + fse = hashmap_get(&cache->map, key, NULL); if (fse) { if (fse->st_mode) fsentry_addref(fse); else fse = NULL; /* non-existing directory */ - LeaveCriticalSection(&mutex); return fse; } /* if looking for a file, check if directory listing is in cache */ if (!fse && key->list) { - fse = fscache_get_wait(key->list); + fse = hashmap_get(&cache->map, key->list, NULL); if (fse) { - LeaveCriticalSection(&mutex); /* * dir entry without file entry, or dir does not * exist -> file doesn't exist @@ -339,25 +324,8 @@ static struct fsentry *fscache_get(struct fsentry *key) } } - /* add future entry to indicate that we're loading it */ - future = key->list ? key->list : key; - future->next = NULL; - future->refcnt = 0; - hashmap_add(&map, future); - - /* create the directory listing (outside mutex!) */ - LeaveCriticalSection(&mutex); - fse = fsentry_create_list(future, &dir_not_found); - EnterCriticalSection(&mutex); - - /* remove future entry and signal waiting threads */ - hashmap_remove(&map, future, NULL); - waiter = future->next; - while (waiter) { - HANDLE h = waiter->hwait; - waiter = waiter->next; - SetEvent(h); - } + /* create the directory listing */ + fse = fsentry_create_list(key->list ? key->list : key, &dir_not_found); /* leave on error (errno set by fsentry_create_list) */ if (!fse) { @@ -370,19 +338,18 @@ static struct fsentry *fscache_get(struct fsentry *key) fse = fsentry_alloc(key->list->list, key->list->name, key->list->len); fse->st_mode = 0; - hashmap_add(&map, fse); + hashmap_add(&cache->map, fse); } - LeaveCriticalSection(&mutex); return NULL; } /* add directory listing to the cache */ - fscache_misses++; - fscache_add(fse); + cache->fscache_misses++; + fscache_add(cache, fse); /* lookup file entry if requested (fse already points to directory) */ if (key->list) - fse = hashmap_get(&map, key, NULL); + fse = hashmap_get(&cache->map, key, NULL); if (fse && !fse->st_mode) fse = NULL; /* non-existing directory */ @@ -393,59 +360,102 @@ static struct fsentry *fscache_get(struct fsentry *key) else errno = ENOENT; - LeaveCriticalSection(&mutex); return fse; } /* - * Enables or disables the cache. Note that the cache is read-only, changes to + * Enables the cache. Note that the cache is read-only, changes to * the working directory are NOT reflected in the cache while enabled. */ -int fscache_enable(int enable, size_t initial_size) +int fscache_enable(size_t initial_size) { - int result; + int fscache; + struct fscache *cache; + int result = 0; + + /* allow the cache to be disabled entirely */ + fscache = git_env_bool("GIT_TEST_FSCACHE", -1); + if (fscache != -1) + core_fscache = fscache; + if (!core_fscache) + return 0; + /* + * refcount the global fscache initialization so that the + * opendir and lstat function pointers are redirected if + * any threads are using the fscache. + */ if (!initialized) { - int fscache = git_env_bool("GIT_TEST_FSCACHE", -1); - - /* allow the cache to be disabled entirely */ - if (fscache != -1) - core_fscache = fscache; - if (!core_fscache) - return 0; - InitializeCriticalSection(&mutex); - lstat_requests = opendir_requests = 0; - fscache_misses = fscache_requests = 0; + if (!dwTlsIndex) { + dwTlsIndex = TlsAlloc(); + if (dwTlsIndex == TLS_OUT_OF_INDEXES) + return 0; + } + + /* redirect opendir and lstat to the fscache implementations */ + opendir = fscache_opendir; + lstat = fscache_lstat; + } + InterlockedIncrement(&initialized); + + /* refcount the thread specific initialization */ + cache = fscache_getcache(); + if (cache) { + InterlockedIncrement(&cache->enabled); + } else { + cache = (struct fscache *)xcalloc(1, sizeof(*cache)); + cache->enabled = 1; /* * avoid having to rehash by leaving room for the parent dirs. * '4' was determined empirically by testing several repos */ - hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4); - initialized = 1; + hashmap_init(&cache->map, (hashmap_cmp_fn)fsentry_cmp, NULL, initial_size * 4); + if (!TlsSetValue(dwTlsIndex, cache)) + BUG("TlsSetValue error"); } - result = enable ? InterlockedIncrement(&enabled) - : InterlockedDecrement(&enabled); + trace_printf_key(&trace_fscache, "fscache: enable\n"); + return result; +} - if (enable && result == 1) { - /* redirect opendir and lstat to the fscache implementations */ - opendir = fscache_opendir; - lstat = fscache_lstat; - } else if (!enable && !result) { +/* + * Disables the cache. + */ +void fscache_disable(void) +{ + struct fscache *cache; + + if (!core_fscache) + return; + + /* update the thread specific fscache initialization */ + cache = fscache_getcache(); + if (!cache) + BUG("fscache_disable() called on a thread where fscache has not been initialized"); + if (!cache->enabled) + BUG("fscache_disable() called on an fscache that is already disabled"); + InterlockedDecrement(&cache->enabled); + if (!cache->enabled) { + TlsSetValue(dwTlsIndex, NULL); + trace_printf_key(&trace_fscache, "fscache_disable: lstat %u, opendir %u, " + "total requests/misses %u/%u\n", + cache->lstat_requests, cache->opendir_requests, + cache->fscache_requests, cache->fscache_misses); + fscache_clear(cache); + free(cache); + } + + /* update the global fscache initialization */ + InterlockedDecrement(&initialized); + if (!initialized) { /* reset opendir and lstat to the original implementations */ opendir = dirent_opendir; lstat = mingw_lstat; - EnterCriticalSection(&mutex); - trace_printf_key(&trace_fscache, "fscache: lstat %u, opendir %u, " - "total requests/misses %u/%u\n", - lstat_requests, opendir_requests, - fscache_requests, fscache_misses); - fscache_clear(); - LeaveCriticalSection(&mutex); } - trace_printf_key(&trace_fscache, "fscache: enable(%d)\n", enable); - return result; + + trace_printf_key(&trace_fscache, "fscache: disable\n"); + return; } /* @@ -453,10 +463,10 @@ int fscache_enable(int enable, size_t initial_size) */ void fscache_flush(void) { - if (enabled) { - EnterCriticalSection(&mutex); - fscache_clear(); - LeaveCriticalSection(&mutex); + struct fscache *cache = fscache_getcache(); + + if (cache && cache->enabled) { + fscache_clear(cache); } } @@ -468,11 +478,12 @@ int fscache_lstat(const char *filename, struct stat *st) { int dirlen, base, len; struct fsentry key[2], *fse; + struct fscache *cache = fscache_getcache(); - if (!fscache_enabled(filename)) + if (!cache || !do_fscache_enabled(cache, filename)) return mingw_lstat(filename, st); - lstat_requests++; + cache->lstat_requests++; /* split filename into path + name */ len = strlen(filename); if (len && is_dir_sep(filename[len - 1])) @@ -485,7 +496,7 @@ int fscache_lstat(const char *filename, struct stat *st) /* lookup entry for path + name in cache */ fsentry_init(key, NULL, filename, dirlen); fsentry_init(key + 1, key, filename + base, len - base); - fse = fscache_get(key + 1); + fse = fscache_get(cache, key + 1); if (!fse) return -1; @@ -549,11 +560,12 @@ DIR *fscache_opendir(const char *dirname) struct fsentry key, *list; fscache_DIR *dir; int len; + struct fscache *cache = fscache_getcache(); - if (!fscache_enabled(dirname)) + if (!cache || !do_fscache_enabled(cache, dirname)) return dirent_opendir(dirname); - opendir_requests++; + cache->opendir_requests++; /* prepare name (strip trailing '/', replace '.') */ len = strlen(dirname); if ((len == 1 && dirname[0] == '.') || @@ -562,7 +574,7 @@ DIR *fscache_opendir(const char *dirname) /* get directory listing from cache */ fsentry_init(&key, NULL, dirname, len); - list = fscache_get(&key); + list = fscache_get(cache, &key); if (!list) return NULL; @@ -573,3 +585,53 @@ DIR *fscache_opendir(const char *dirname) dir->pfsentry = list; return (DIR*) dir; } + +struct fscache *fscache_getcache(void) +{ + return (struct fscache *)TlsGetValue(dwTlsIndex); +} + +void fscache_merge(struct fscache *dest) +{ + struct hashmap_iter iter; + struct hashmap_entry *e; + struct fscache *cache = fscache_getcache(); + + /* + * Only do the merge if fscache was enabled and we have a dest + * cache to merge into. + */ + if (!dest) { + fscache_enable(0); + return; + } + if (!cache) + BUG("fscache_merge() called on a thread where fscache has not been initialized"); + + TlsSetValue(dwTlsIndex, NULL); + trace_printf_key(&trace_fscache, "fscache_merge: lstat %u, opendir %u, " + "total requests/misses %u/%u\n", + cache->lstat_requests, cache->opendir_requests, + cache->fscache_requests, cache->fscache_misses); + + /* + * This is only safe because the primary thread we're merging into + * isn't being used so the critical section only needs to prevent + * the the child threads from stomping on each other. + */ + EnterCriticalSection(&mutex); + + hashmap_iter_init(&cache->map, &iter); + while ((e = hashmap_iter_next(&iter))) + hashmap_add(&dest->map, e); + + dest->lstat_requests += cache->lstat_requests; + dest->opendir_requests += cache->opendir_requests; + dest->fscache_requests += cache->fscache_requests; + dest->fscache_misses += cache->fscache_misses; + LeaveCriticalSection(&mutex); + + free(cache); + + InterlockedDecrement(&initialized); +} diff --git a/compat/win32/fscache.h b/compat/win32/fscache.h index d49c9381114da6..2eb8bf3f5cfee8 100644 --- a/compat/win32/fscache.h +++ b/compat/win32/fscache.h @@ -1,9 +1,16 @@ #ifndef FSCACHE_H #define FSCACHE_H -int fscache_enable(int enable, size_t initial_size); -#define enable_fscache(initial_size) fscache_enable(1, initial_size) -#define disable_fscache() fscache_enable(0, 0) +/* + * The fscache is thread specific. enable_fscache() must be called + * for each thread where caching is desired. + */ + +int fscache_enable(size_t initial_size); +#define enable_fscache(initial_size) fscache_enable(initial_size) + +void fscache_disable(void); +#define disable_fscache() fscache_disable() int fscache_enabled(const char *path); #define is_fscache_enabled(path) fscache_enabled(path) @@ -14,4 +21,13 @@ void fscache_flush(void); DIR *fscache_opendir(const char *dir); int fscache_lstat(const char *file_name, struct stat *buf); +/* opaque fscache structure */ +struct fscache; + +struct fscache *fscache_getcache(void); +#define getcache_fscache() fscache_getcache() + +void fscache_merge(struct fscache *dest); +#define merge_fscache(dest) fscache_merge(dest) + #endif diff --git a/git-compat-util.h b/git-compat-util.h index e1b9e55683f204..2d1634611ef697 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1255,6 +1255,10 @@ static inline int is_missing_file_error(int errno_) * data or even file content without the need to synchronize with the file * system. */ + + /* opaque fscache structure */ +struct fscache; + #ifndef enable_fscache #define enable_fscache(x) /* noop */ #endif @@ -1271,6 +1275,14 @@ static inline int is_missing_file_error(int errno_) #define flush_fscache() /* noop */ #endif +#ifndef getcache_fscache +#define getcache_fscache() (NULL) /* noop */ +#endif + +#ifndef merge_fscache +#define merge_fscache(dest) /* noop */ +#endif + extern int cmd_main(int, const char **); /* diff --git a/preload-index.c b/preload-index.c index af34c3dc59112a..50956aa34c70b8 100644 --- a/preload-index.c +++ b/preload-index.c @@ -15,6 +15,8 @@ void preload_index(struct index_state *index, const struct pathspec *pathspec) #include <pthread.h> +struct fscache *fscache; + /* * Mostly randomly chosen maximum thread counts: we * cap the parallelism to 20 threads, and we want @@ -43,6 +45,7 @@ static void *preload_thread(void *_data) if (nr + p->offset > index->cache_nr) nr = index->cache_nr - p->offset; + enable_fscache(nr); do { struct cache_entry *ce = *cep++; struct stat st; @@ -69,6 +72,7 @@ static void *preload_thread(void *_data) mark_fsmonitor_valid(ce); } while (--nr > 0); cache_def_clear(&cache); + merge_fscache(fscache); return NULL; } @@ -81,6 +85,7 @@ void preload_index(struct index_state *index, const struct pathspec *pathspec) if (!core_preload_index) return; + fscache = getcache_fscache(); threads = index->cache_nr / THREAD_COST; if ((index->cache_nr > 1) && (threads < 2) && getenv("GIT_FORCE_PRELOAD_TEST")) threads = 2; @@ -91,7 +96,6 @@ void preload_index(struct index_state *index, const struct pathspec *pathspec) offset = 0; work = DIV_ROUND_UP(index->cache_nr, threads); memset(&data, 0, sizeof(data)); - enable_fscache(index->cache_nr); for (i = 0; i < threads; i++) { struct thread_data *p = data+i; p->index = index; @@ -109,7 +113,6 @@ void preload_index(struct index_state *index, const struct pathspec *pathspec) die("unable to join threaded lstat"); } trace_performance_since(start, "preload index"); - disable_fscache(); } #endif From a1297bedc9fd9a1e1173a221e98460287a247c3d Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Tue, 25 Sep 2018 16:28:16 -0400 Subject: [PATCH 403/406] fscache: add fscache hit statistics Track fscache hits and misses for lstat and opendir requests. Reporting of statistics is done when the cache is disabled for the last time and freed and is only reported if GIT_TRACE_FSCACHE is set. Sample output is: 11:33:11.836428 compat/win32/fscache.c:433 fscache: lstat 3775, opendir 263, total requests/misses 4052/269 Signed-off-by: Ben Peart <benpeart@microsoft.com> --- compat/win32/fscache.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index bafe42b9c9f7c6..b81eb62e4f5dc1 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -8,6 +8,10 @@ static int initialized; static volatile long enabled; static struct hashmap map; static CRITICAL_SECTION mutex; +static unsigned int lstat_requests; +static unsigned int opendir_requests; +static unsigned int fscache_requests; +static unsigned int fscache_misses; static struct trace_key trace_fscache = TRACE_KEY_INIT(FSCACHE); /* @@ -262,6 +266,8 @@ static void fscache_clear(void) { hashmap_free(&map, 1); hashmap_init(&map, (hashmap_cmp_fn)fsentry_cmp, NULL, 0); + lstat_requests = opendir_requests = 0; + fscache_misses = fscache_requests = 0; } /* @@ -308,6 +314,7 @@ static struct fsentry *fscache_get(struct fsentry *key) int dir_not_found; EnterCriticalSection(&mutex); + fscache_requests++; /* check if entry is in cache */ fse = fscache_get_wait(key); if (fse) { @@ -370,6 +377,7 @@ static struct fsentry *fscache_get(struct fsentry *key) } /* add directory listing to the cache */ + fscache_misses++; fscache_add(fse); /* lookup file entry if requested (fse already points to directory) */ @@ -407,6 +415,8 @@ int fscache_enable(int enable) return 0; InitializeCriticalSection(&mutex); + lstat_requests = opendir_requests = 0; + fscache_misses = fscache_requests = 0; hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0); initialized = 1; } @@ -423,6 +433,10 @@ int fscache_enable(int enable) opendir = dirent_opendir; lstat = mingw_lstat; EnterCriticalSection(&mutex); + trace_printf_key(&trace_fscache, "fscache: lstat %u, opendir %u, " + "total requests/misses %u/%u\n", + lstat_requests, opendir_requests, + fscache_requests, fscache_misses); fscache_clear(); LeaveCriticalSection(&mutex); } @@ -454,6 +468,7 @@ int fscache_lstat(const char *filename, struct stat *st) if (!fscache_enabled(filename)) return mingw_lstat(filename, st); + lstat_requests++; /* split filename into path + name */ len = strlen(filename); if (len && is_dir_sep(filename[len - 1])) @@ -534,6 +549,7 @@ DIR *fscache_opendir(const char *dirname) if (!fscache_enabled(dirname)) return dirent_opendir(dirname); + opendir_requests++; /* prepare name (strip trailing '/', replace '.') */ len = strlen(dirname); if ((len == 1 && dirname[0] == '.') || From 1c2f6c80a77c2fbbd797c846e46486db2b72f471 Mon Sep 17 00:00:00 2001 From: Ben Peart <benpeart@microsoft.com> Date: Fri, 2 Nov 2018 11:19:10 -0400 Subject: [PATCH 404/406] fscache: teach fscache to use mempool Now that the fscache is single threaded, take advantage of the mem_pool as the allocator to significantly reduce the cost of allocations and frees. With the reduced cost of free, in future patches, we can start freeing the fscache at the end of commands instead of just leaking it. Signed-off-by: Ben Peart <benpeart@microsoft.com> --- compat/win32/fscache.c | 44 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/compat/win32/fscache.c b/compat/win32/fscache.c index c3d76bf320d5d2..9c62a0a1429a44 100644 --- a/compat/win32/fscache.c +++ b/compat/win32/fscache.c @@ -3,6 +3,7 @@ #include "../win32.h" #include "fscache.h" #include "config.h" +#include "../../mem-pool.h" static volatile long initialized; static DWORD dwTlsIndex; @@ -17,6 +18,7 @@ static CRITICAL_SECTION mutex; struct fscache { volatile long enabled; struct hashmap map; + struct mem_pool *mem_pool; unsigned int lstat_requests; unsigned int opendir_requests; unsigned int fscache_requests; @@ -106,11 +108,11 @@ static void fsentry_init(struct fsentry *fse, struct fsentry *list, /* * Allocate an fsentry structure on the heap. */ -static struct fsentry *fsentry_alloc(struct fsentry *list, const char *name, +static struct fsentry *fsentry_alloc(struct fscache *cache, struct fsentry *list, const char *name, size_t len) { /* overallocate fsentry and copy the name to the end */ - struct fsentry *fse = xmalloc(sizeof(struct fsentry) + len + 1); + struct fsentry *fse = mem_pool_alloc(cache->mem_pool, sizeof(struct fsentry) + len + 1); char *nm = ((char*) fse) + sizeof(struct fsentry); memcpy(nm, name, len); nm[len] = 0; @@ -133,27 +135,20 @@ inline static void fsentry_addref(struct fsentry *fse) } /* - * Release the reference to an fsentry, frees the memory if its the last ref. + * Release the reference to an fsentry. */ static void fsentry_release(struct fsentry *fse) { if (fse->list) fse = fse->list; - if (InterlockedDecrement(&(fse->refcnt))) - return; - - while (fse) { - struct fsentry *next = fse->next; - free(fse); - fse = next; - } + InterlockedDecrement(&(fse->refcnt)); } /* * Allocate and initialize an fsentry from a WIN32_FIND_DATA structure. */ -static struct fsentry *fseentry_create_entry(struct fsentry *list, +static struct fsentry *fseentry_create_entry(struct fscache *cache, struct fsentry *list, const WIN32_FIND_DATAW *fdata) { char buf[MAX_PATH * 3]; @@ -161,7 +156,7 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, struct fsentry *fse; len = xwcstoutf(buf, fdata->cFileName, ARRAY_SIZE(buf)); - fse = fsentry_alloc(list, buf, len); + fse = fsentry_alloc(cache, list, buf, len); if (fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK && sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 && @@ -192,7 +187,7 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list, * Dir should not contain trailing '/'. Use an empty string for the current * directory (not "."!). */ -static struct fsentry *fsentry_create_list(const struct fsentry *dir, +static struct fsentry *fsentry_create_list(struct fscache *cache, const struct fsentry *dir, int *dir_not_found) { wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */ @@ -231,13 +226,13 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir, } /* allocate object to hold directory listing */ - list = fsentry_alloc(NULL, dir->name, dir->len); + list = fsentry_alloc(cache, NULL, dir->name, dir->len); list->st_mode = S_IFDIR; /* walk directory and build linked list of fsentry structures */ phead = &list->next; do { - *phead = fseentry_create_entry(list, &fdata); + *phead = fseentry_create_entry(cache, list, &fdata); phead = &(*phead)->next; } while (FindNextFileW(h, &fdata)); @@ -249,7 +244,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir, if (err == ERROR_NO_MORE_FILES) return list; - /* otherwise free the list and return error */ + /* otherwise release the list and return error */ fsentry_release(list); errno = err_win_to_posix(err); return NULL; @@ -272,7 +267,10 @@ static void fscache_add(struct fscache *cache, struct fsentry *fse) */ static void fscache_clear(struct fscache *cache) { - hashmap_free(&cache->map, 1); + mem_pool_discard(cache->mem_pool, 0); + cache->mem_pool = NULL; + mem_pool_init(&cache->mem_pool, 0); + hashmap_free(&cache->map, 0); hashmap_init(&cache->map, (hashmap_cmp_fn)fsentry_cmp, NULL, 0); cache->lstat_requests = cache->opendir_requests = 0; cache->fscache_misses = cache->fscache_requests = 0; @@ -325,7 +323,7 @@ static struct fsentry *fscache_get(struct fscache *cache, struct fsentry *key) } /* create the directory listing */ - fse = fsentry_create_list(key->list ? key->list : key, &dir_not_found); + fse = fsentry_create_list(cache, key->list ? key->list : key, &dir_not_found); /* leave on error (errno set by fsentry_create_list) */ if (!fse) { @@ -335,7 +333,7 @@ static struct fsentry *fscache_get(struct fscache *cache, struct fsentry *key) * empty, which for all practical matters is the same * thing as far as fscache is concerned). */ - fse = fsentry_alloc(key->list->list, + fse = fsentry_alloc(cache, key->list->list, key->list->name, key->list->len); fse->st_mode = 0; hashmap_add(&cache->map, fse); @@ -411,6 +409,7 @@ int fscache_enable(size_t initial_size) * '4' was determined empirically by testing several repos */ hashmap_init(&cache->map, (hashmap_cmp_fn)fsentry_cmp, NULL, initial_size * 4); + mem_pool_init(&cache->mem_pool, 0); if (!TlsSetValue(dwTlsIndex, cache)) BUG("TlsSetValue error"); } @@ -442,7 +441,8 @@ void fscache_disable(void) "total requests/misses %u/%u\n", cache->lstat_requests, cache->opendir_requests, cache->fscache_requests, cache->fscache_misses); - fscache_clear(cache); + mem_pool_discard(cache->mem_pool, 0); + hashmap_free(&cache->map, 0); free(cache); } @@ -625,6 +625,8 @@ void fscache_merge(struct fscache *dest) while ((e = hashmap_iter_next(&iter))) hashmap_add(&dest->map, e); + mem_pool_combine(dest->mem_pool, cache->mem_pool); + dest->lstat_requests += cache->lstat_requests; dest->opendir_requests += cache->opendir_requests; dest->fscache_requests += cache->fscache_requests; From b41e88f4bf705da6f2bbd253b3f1935c1f8fbff0 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin <johannes.schindelin@gmx.de> Date: Tue, 6 Nov 2018 18:01:55 +0100 Subject: [PATCH 405/406] mingw: add a helper function to attach GDB to the current process When debugging Git, the criss-cross spawning of processes can make things quite a bit difficult, especially when a Unix shell script is thrown in the mix that calls a `git.exe` that then segfaults. To help debugging such things, we introduce the `open_in_gdb()` function which can be called at a code location where the segfault happens (or as close as one can get); This will open a new MinTTY window with a GDB that already attached to the current process. Inspired by Derrick Stolee. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- compat/mingw.c | 13 +++++++++++++ compat/mingw.h | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 85839cdd2b6ca6..13c34d943c0aab 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -14,6 +14,19 @@ #define HCAST(type, handle) ((type)(intptr_t)handle) +void open_in_gdb(void) +{ + static struct child_process cp = CHILD_PROCESS_INIT; + extern char *_pgmptr; + + argv_array_pushl(&cp.args, "mintty", "gdb", NULL); + argv_array_pushf(&cp.args, "--pid=%d", getpid()); + cp.clean_on_exit = 1; + if (start_command(&cp) < 0) + die_errno("Could not start gdb"); + sleep(1); +} + int err_win_to_posix(DWORD winerr) { int error = ENOSYS; diff --git a/compat/mingw.h b/compat/mingw.h index 848028af1058d7..270f19b033ba82 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -691,6 +691,16 @@ extern CRITICAL_SECTION pinfo_cs; int wmain(int argc, const wchar_t **w_argv); int main(int argc, const char **argv); +/* + * For debugging: if a problem occurs, say, in a Git process that is spawned + * from another Git process which in turn is spawned from yet another Git + * process, it can be quite daunting to figure out what is going on. + * + * Call this function to open a new MinTTY (this assumes you are in Git for + * Windows' SDK) with a GDB that attaches to the current process right away. + */ +extern void open_in_gdb(void); + /* * Used by Pthread API implementation for Windows */ From 8f9735fff6dd6685605fe9e0ed986f0720277c80 Mon Sep 17 00:00:00 2001 From: tanushree27 <tanushreetumane@gmail.com> Date: Sat, 27 Oct 2018 03:34:50 +0530 Subject: [PATCH 406/406] mingw: remove obsolete IPv6-related code To support IPv6, Git provided fall back functions for Windows versions that did not support IPv6. However, as Git dropped support for Windows XP and prior, those functions are not needed anymore. Removed those fallbacks by reverting commit[1] and using the functions directly (without 'ipv6_' prefix). [1] fe3b2b7b827c75c21d61933e073050b6840f6dbc. Signed-off-by: tanushree27 <tanushreetumane@gmail.com> --- compat/mingw.c | 182 +------------------------------------------------ compat/mingw.h | 12 ---- 2 files changed, 1 insertion(+), 193 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 85839cdd2b6ca6..81b2316ef4742f 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2070,142 +2070,10 @@ int mingw_putenv(const char *namevalue) return result ? 0 : -1; } -/* - * Note, this isn't a complete replacement for getaddrinfo. It assumes - * that service contains a numerical port, or that it is null. It - * does a simple search using gethostbyname, and returns one IPv4 host - * if one was found. - */ -static int WSAAPI getaddrinfo_stub(const char *node, const char *service, - const struct addrinfo *hints, - struct addrinfo **res) -{ - struct hostent *h = NULL; - struct addrinfo *ai; - struct sockaddr_in *sin; - - if (node) { - h = gethostbyname(node); - if (!h) - return WSAGetLastError(); - } - - ai = xmalloc(sizeof(struct addrinfo)); - *res = ai; - ai->ai_flags = 0; - ai->ai_family = AF_INET; - ai->ai_socktype = hints ? hints->ai_socktype : 0; - switch (ai->ai_socktype) { - case SOCK_STREAM: - ai->ai_protocol = IPPROTO_TCP; - break; - case SOCK_DGRAM: - ai->ai_protocol = IPPROTO_UDP; - break; - default: - ai->ai_protocol = 0; - break; - } - ai->ai_addrlen = sizeof(struct sockaddr_in); - if (hints && (hints->ai_flags & AI_CANONNAME)) - ai->ai_canonname = h ? xstrdup(h->h_name) : NULL; - else - ai->ai_canonname = NULL; - - sin = xcalloc(1, ai->ai_addrlen); - sin->sin_family = AF_INET; - /* Note: getaddrinfo is supposed to allow service to be a string, - * which should be looked up using getservbyname. This is - * currently not implemented */ - if (service) - sin->sin_port = htons(atoi(service)); - if (h) - sin->sin_addr = *(struct in_addr *)h->h_addr; - else if (hints && (hints->ai_flags & AI_PASSIVE)) - sin->sin_addr.s_addr = INADDR_ANY; - else - sin->sin_addr.s_addr = INADDR_LOOPBACK; - ai->ai_addr = (struct sockaddr *)sin; - ai->ai_next = NULL; - return 0; -} - -static void WSAAPI freeaddrinfo_stub(struct addrinfo *res) -{ - free(res->ai_canonname); - free(res->ai_addr); - free(res); -} - -static int WSAAPI getnameinfo_stub(const struct sockaddr *sa, socklen_t salen, - char *host, DWORD hostlen, - char *serv, DWORD servlen, int flags) -{ - const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; - if (sa->sa_family != AF_INET) - return EAI_FAMILY; - if (!host && !serv) - return EAI_NONAME; - - if (host && hostlen > 0) { - struct hostent *ent = NULL; - if (!(flags & NI_NUMERICHOST)) - ent = gethostbyaddr((const char *)&sin->sin_addr, - sizeof(sin->sin_addr), AF_INET); - - if (ent) - snprintf(host, hostlen, "%s", ent->h_name); - else if (flags & NI_NAMEREQD) - return EAI_NONAME; - else - snprintf(host, hostlen, "%s", inet_ntoa(sin->sin_addr)); - } - - if (serv && servlen > 0) { - struct servent *ent = NULL; - if (!(flags & NI_NUMERICSERV)) - ent = getservbyport(sin->sin_port, - flags & NI_DGRAM ? "udp" : "tcp"); - - if (ent) - snprintf(serv, servlen, "%s", ent->s_name); - else - snprintf(serv, servlen, "%d", ntohs(sin->sin_port)); - } - - return 0; -} - -static HMODULE ipv6_dll = NULL; -static void (WSAAPI *ipv6_freeaddrinfo)(struct addrinfo *res); -static int (WSAAPI *ipv6_getaddrinfo)(const char *node, const char *service, - const struct addrinfo *hints, - struct addrinfo **res); -static int (WSAAPI *ipv6_getnameinfo)(const struct sockaddr *sa, socklen_t salen, - char *host, DWORD hostlen, - char *serv, DWORD servlen, int flags); -/* - * gai_strerror is an inline function in the ws2tcpip.h header, so we - * don't need to try to load that one dynamically. - */ - -static void socket_cleanup(void) -{ - WSACleanup(); - if (ipv6_dll) - FreeLibrary(ipv6_dll); - ipv6_dll = NULL; - ipv6_freeaddrinfo = freeaddrinfo_stub; - ipv6_getaddrinfo = getaddrinfo_stub; - ipv6_getnameinfo = getnameinfo_stub; -} - static void ensure_socket_initialization(void) { WSADATA wsa; static int initialized = 0; - const char *libraries[] = { "ws2_32.dll", "wship6.dll", NULL }; - const char **name; if (initialized) return; @@ -2214,35 +2082,7 @@ static void ensure_socket_initialization(void) die("unable to initialize winsock subsystem, error %d", WSAGetLastError()); - for (name = libraries; *name; name++) { - ipv6_dll = LoadLibraryExA(*name, NULL, - LOAD_LIBRARY_SEARCH_SYSTEM32); - if (!ipv6_dll) - continue; - - ipv6_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *)) - GetProcAddress(ipv6_dll, "freeaddrinfo"); - ipv6_getaddrinfo = (int (WSAAPI *)(const char *, const char *, - const struct addrinfo *, - struct addrinfo **)) - GetProcAddress(ipv6_dll, "getaddrinfo"); - ipv6_getnameinfo = (int (WSAAPI *)(const struct sockaddr *, - socklen_t, char *, DWORD, - char *, DWORD, int)) - GetProcAddress(ipv6_dll, "getnameinfo"); - if (!ipv6_freeaddrinfo || !ipv6_getaddrinfo || !ipv6_getnameinfo) { - FreeLibrary(ipv6_dll); - ipv6_dll = NULL; - } else - break; - } - if (!ipv6_freeaddrinfo || !ipv6_getaddrinfo || !ipv6_getnameinfo) { - ipv6_freeaddrinfo = freeaddrinfo_stub; - ipv6_getaddrinfo = getaddrinfo_stub; - ipv6_getnameinfo = getnameinfo_stub; - } - - atexit(socket_cleanup); + atexit((void(*)(void)) WSACleanup); initialized = 1; } @@ -2260,26 +2100,6 @@ struct hostent *mingw_gethostbyname(const char *host) return gethostbyname(host); } -void mingw_freeaddrinfo(struct addrinfo *res) -{ - ipv6_freeaddrinfo(res); -} - -int mingw_getaddrinfo(const char *node, const char *service, - const struct addrinfo *hints, struct addrinfo **res) -{ - ensure_socket_initialization(); - return ipv6_getaddrinfo(node, service, hints, res); -} - -int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen, - char *host, DWORD hostlen, char *serv, DWORD servlen, - int flags) -{ - ensure_socket_initialization(); - return ipv6_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); -} - int mingw_socket(int domain, int type, int protocol) { int sockfd; diff --git a/compat/mingw.h b/compat/mingw.h index 848028af1058d7..ad2e6144ef2fad 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -297,18 +297,6 @@ int mingw_gethostname(char *host, int namelen); struct hostent *mingw_gethostbyname(const char *host); #define gethostbyname mingw_gethostbyname -void mingw_freeaddrinfo(struct addrinfo *res); -#define freeaddrinfo mingw_freeaddrinfo - -int mingw_getaddrinfo(const char *node, const char *service, - const struct addrinfo *hints, struct addrinfo **res); -#define getaddrinfo mingw_getaddrinfo - -int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen, - char *host, DWORD hostlen, char *serv, DWORD servlen, - int flags); -#define getnameinfo mingw_getnameinfo - int mingw_socket(int domain, int type, int protocol); #define socket mingw_socket