Repository: nvm
Issue: curl-install-sh: `fatal: bad object` from git clone steps
Labels: OS: windows,pull request wanted,installing nvm

#### Operating system and version:
Windows Server 2012 R2
NodeJS 20.15.1
Git for Windows 2.45.2.windows.1
Cygwin1.dll 3.4.5

#### `nvm debug` output:
<details>
<!-- do not delete the following blank line -->

```sh
# command not found?
```
</details>

#### `nvm ls` output:
<details>
<!-- do not delete the following blank line -->

```sh
# command... not found.
```
</details>

#### How did you install `nvm`?
<!-- (e.g. install script in readme, Homebrew) -->
`curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash`

#### What steps did you perform?
Ran the install command

#### What happened?

<details>
<summary>install.sh script output</summary>

```
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16555  100 16555    0     0   140k      0 --:--:-- --:--:-- --:--:--  141k
=> Downloading nvm from git to '/home/avenger/.nvm'
=> Cloning into '/home/avenger/.nvm'...
remote: Enumerating objects: 369, done.
remote: Counting objects: 100% (369/369), done.
remote: Compressing objects: 100% (315/315), done.
remote: Total 369 (delta 42), reused 166 (delta 27), pack-reused 0
Receiving objects: 100% (369/369), 368.22 KiB | 30.68 MiB/s, done.
Resolving deltas: 100% (42/42), done.
remote: Enumerating objects: 366, done.
remote: Counting objects: 100% (366/366), done.
remote: Compressing objects: 100% (315/315), done.
remote: Total 366 (delta 42), reused 162 (delta 25), pack-reused 0
Receiving objects: 100% (366/366), 365.90 KiB | 30.49 MiB/s, done.
Resolving deltas: 100% (42/42), done.
fatal: bad object cdc56687ada0c47f97632b0049b0446aadbe3a3a
error: remote did not send all necessary objects
Failed to fetch origin with v0.39.7. Please report this!
```

</details>

#### What did you expect to happen?
Install nvm.

#### Is there anything in any of your profile files that modifies the `PATH`?
<!--  (e.g. `.bashrc`, `.bash_profile`, `.zshrc`, etc) -->
`~/.bash_profile`

<!-- Please remove the following section if it does not apply to you -->
#### If you are having installation issues, or getting "N/A", what does `curl -I --compressed -v https://nodejs.org/dist/` print out?
<details>
<!-- do not delete the following blank line -->

```sh
$ curl -I --compressed -v https://nodejs.org/dist/
* STATE: INIT => CONNECT handle 0xa0001eef8; line 1909 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => RESOLVING handle 0xa0001eef8; line 1955 (connection #0)
* [CONN-0-0] setup, init filter chain
* [CONN-0-0] cf_add(filter=SOCKET)
* [CONN-0-0] cf_add(filter=SSL)
* [CONN-0-0][CF-SOCKET] setup(remotehost=nodejs.org)
* STATE: RESOLVING => CONNECTING handle 0xa0001eef8; line 2029 (connection #0)
* family0 == v4, family1 == v6
*   Trying 104.20.22.46:443...
* [CONN-0-0] connect(block=0)-> 0, done=0
* Connected to nodejs.org (104.20.22.46) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
*  CAfile: /etc/pki/tls/certs/ca-bundle.crt
*  CApath: none
* [CONN-0] Didn't find Session ID in cache for host HTTPS://nodejs.org:443
* [CONN-0-0][CF-SSL] TLSv1.3 (OUT), TLS handshake, Client hello (1):
* [CONN-0-0] connect(block=0)-> 0, done=0
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, Server hello (2):
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, Certificate (11):
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, CERT verify (15):
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, Finished (20):
* [CONN-0-0][CF-SSL] TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* [CONN-0-0][CF-SSL] TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=*.nodejs.org
*  start date: Feb 28 00:00:00 2024 GMT
*  expire date: Mar 30 23:59:59 2025 GMT
*  subjectAltName: host "nodejs.org" matched cert's "nodejs.org"
*  issuer: C=GB; ST=Greater Manchester; L=Salford; O=Sectigo Limited; CN=Sectigo RSA Domain Validation Secure Server CA
*  SSL certificate verify ok.
* [CONN-0-0] connect(block=0)-> 0, done=1
* STATE: CONNECTING => PROTOCONNECT handle 0xa0001eef8; line 2073 (connection #0)
* [CONN-0-0] connect(block=0)-> 0, done=1
* STATE: PROTOCONNECT => DO handle 0xa0001eef8; line 2103 (connection #0)
* Using HTTP2, server supports multiplexing
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* h2h3 [:method: HEAD]
* h2h3 [:path: /dist/]
* h2h3 [:scheme: https]
* h2h3 [:authority: nodejs.org]
* h2h3 [user-agent: curl/7.87.0]
* h2h3 [accept: */*]
* h2h3 [accept-encoding: deflate, gzip, br, zstd]
* Using Stream ID: 1 (easy handle 0xa0001eef8)
> HEAD /dist/ HTTP/2
> Host: nodejs.org
> user-agent: curl/7.87.0
> accept: */*
> accept-encoding: deflate, gzip, br, zstd
>
* STATE: DO => DID handle 0xa0001eef8; line 2199 (connection #0)
* multi changed, check CONNECT_PEND queue
* STATE: DID => PERFORMING handle 0xa0001eef8; line 2318 (connection #0)
* Curl_readwrite(handle=0xa0001eef8) -> 0
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* [CONN-0] Didn't find Session ID in cache for host HTTPS://nodejs.org:443
* [CONN-0] Added Session ID to cache for HTTPS://nodejs.org:443 [server]
* [CONN-0-0][CF-SSL] TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* [CONN-0] Found Session ID in cache for host HTTPS://nodejs.org:443
* old SSL session ID is stale, removing
* [CONN-0] Added Session ID to cache for HTTPS://nodejs.org:443 [server]
* readwrite_data(handle=0xa0001eef8) -> 0
* Curl_readwrite(handle=0xa0001eef8) -> 0
* readwrite_data(handle=0xa0001eef8) -> 0
* Curl_readwrite(handle=0xa0001eef8) -> 0
* HTTP/2 found, allow multiplexing
< HTTP/2 200
HTTP/2 200
< date: Tue, 23 Jul 2024 22:31:52 GMT
date: Tue, 23 Jul 2024 22:31:52 GMT
< content-type: text/html
content-type: text/html
< cache-control: public, max-age=3600, s-maxage=14400
cache-control: public, max-age=3600, s-maxage=14400
< last-modified: Tue, 23 Jul 2024 19:35:45 GMT
last-modified: Tue, 23 Jul 2024 19:35:45 GMT
< cf-cache-status: HIT
cf-cache-status: HIT
< age: 10355
age: 10355
< vary: Accept-Encoding
vary: Accept-Encoding
< strict-transport-security: max-age=31536000; includeSubDomains; preload
strict-transport-security: max-age=31536000; includeSubDomains; preload
< x-content-type-options: nosniff
x-content-type-options: nosniff
< server: cloudflare
server: cloudflare
< cf-ray: 8a7f1f8aab5c5794-IAD
cf-ray: 8a7f1f8aab5c5794-IAD
< content-encoding: br
content-encoding: br

<
* readwrite_data(handle=0xa0001eef8) -> 0
* Curl_readwrite(handle=0xa0001eef8) -> 0
* STATE: PERFORMING => DONE handle 0xa0001eef8; line 2517 (connection #0)
* multi_done: status: 0 prem: 0 done: 0
* Connection #0 to host nodejs.org left intact
* Expire cleared (transfer 0xa0001eef8)

```
</details>

## bottomline

If I download/checkout and edit `install.sh` and change all `--git-dir="${INSTALL_DIR}/.git" --work-tree="${INSTALL_DIR}"` with just `-C "${INSTALL_DIR}"` the script runs to completion.

<details><summary>`$ cat install.sh | bash`</summary>

```
=> nvm is already installed in /home/user/.nvm, trying to update using git
=> * (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/user/.bashrc
=> Appending bash_completion source string to /home/user/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

C:\Users\user\AppData\Roaming\npm
└── nvm@0.0.4
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
```

This happens because I'm running git for windows from a cygwin shell, so the linux-like path provided in `--git-dir` doesn't get translated correctly. It seems both `-C` and `--work-tree` are translated correctly. I didn't retry the script just dropping `--git-dir` as it would probably fail as my current working directory was not `~/.nvm`.

So, perhaps replacing the command to use `-C` could be a bit more portable. Maybe checking `"${OSTYPE}" == "cygwin"` could create an exception, if using both work-tree and git-dir is meant to solve issues with a specific OS/environment.

Hope this helps improve NVM!

*edit:* this issue is probably the same reported in #3329.