Handle `--bare` clone targets by hyperrealist · Pull Request #9271 · cli/cli · GitHub
Skip to content

Handle --bare clone targets#9271

Merged
williammartin merged 3 commits into
cli:trunkfrom
hyperrealist:support-bear
Jul 24, 2024
Merged

Handle --bare clone targets#9271
williammartin merged 3 commits into
cli:trunkfrom
hyperrealist:support-bear

Conversation

@hyperrealist

@hyperrealist hyperrealist commented Jun 29, 2024

Copy link
Copy Markdown

Fixes #9270

  • Append .git to target when cloned with --bare option
  • Write test case

@hyperrealist hyperrealist requested a review from a team as a code owner June 29, 2024 17:20
@hyperrealist hyperrealist requested a review from andyfeller June 29, 2024 17:20
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Jun 29, 2024

@andyfeller andyfeller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting this issue and opening the PR to fix it, @hyperrealist!

Changes look good, just want to ensure we capture this in our testing before shipping it.

Comment thread git/client.go Outdated
@hyperrealist

Copy link
Copy Markdown
Author

@williammartin

Copy link
Copy Markdown
Member

Just FYI I asked a question over on #9270 to double check this is the right direction.

@andyfeller

Copy link
Copy Markdown
Contributor

Just FYI I asked a question over on #9270 to double check this is the right direction.

Good catch on the post clone handling. 🤔 Most of the time I've worked with bare repositories were when I didn't expect to make any changes.

gitClient := opts.GitClient
ctx := context.Background()
cloneDir, err := gitClient.Clone(ctx, canonicalCloneURL, opts.GitArgs)
if err != nil {
return err
}
// If the repo is a fork, add the parent as an upstream remote and set the parent as the default repo.
if canonicalRepo.Parent != nil {
protocol := cfg.GitProtocol(canonicalRepo.Parent.RepoHost()).Value
upstreamURL := ghrepo.FormatRemoteURL(canonicalRepo.Parent, protocol)
upstreamName := opts.UpstreamName
if opts.UpstreamName == "@owner" {
upstreamName = canonicalRepo.Parent.RepoOwner()
}
gc := gitClient.Copy()
gc.RepoDir = cloneDir
if _, err := gc.AddRemote(ctx, upstreamName, upstreamURL, []string{canonicalRepo.Parent.DefaultBranchRef.Name}); err != nil {
return err
}
if err := gc.Fetch(ctx, upstreamName, ""); err != nil {
return err
}
if err := gc.SetRemoteBranches(ctx, upstreamName, `*`); err != nil {
return err
}
if err = gc.SetRemoteResolution(ctx, upstreamName, "base"); err != nil {
return err
}
connectedToTerminal := opts.IO.IsStdoutTTY()
if connectedToTerminal {
cs := opts.IO.ColorScheme()
fmt.Fprintf(opts.IO.ErrOut, "%s Repository %s set as the default repository. To learn more about the default repository, run: gh repo set-default --help\n", cs.WarningIcon(), cs.Bold(ghrepo.FullName(canonicalRepo.Parent)))
}
}
return nil

@williammartin williammartin self-requested a review July 22, 2024 14:57
Comment thread git/client.go Outdated
target = path.Base(strings.TrimSuffix(cloneURL, ".git"))
}
if slices.Contains(cloneArgs, "--bare") {
target += ".git"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is quite right. Just writing a comment here to ensure this isn't merged before I have a moment to investigate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as I suspected this is broken in the case where you provide both --bare and a target directory i.e.:

➜  cli git:(support-bear) ./bin/gh repo clone hyperrealist/tiled tiled -- --bare
Cloning into bare repository 'tiled'...
remote: Enumerating objects: 16472, done.
remote: Counting objects: 100% (376/376), done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 16472 (delta 283), reused 336 (delta 263), pack-reused 16096
Receiving objects: 100% (16472/16472), 4.30 MiB | 11.09 MiB/s, done.
Resolving deltas: 100% (12482/12482), done.
failed to run git: fatal: cannot change to 'tiled.git': No such file or directory

We should only be appending .git to the target directory if the user didn't specify one.

Follow up to PR feedback from @williammartin, `git clone --bare` will only override the local repository target if unspecified.

This commit enhances the cloning logic and associated test to ensure explicit targets are preserved when cloning.
@williammartin

williammartin commented Jul 24, 2024

Copy link
Copy Markdown
Member

@williammartin williammartin self-requested a review July 24, 2024 10:34
@williammartin williammartin dismissed andyfeller’s stale review July 24, 2024 10:35

Andy made the changes he requested and I have reviewed them

@williammartin williammartin merged commit fd51a69 into cli:trunk Jul 24, 2024
izumin5210 referenced this pull request in izumin5210/dotfiles Aug 6, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://togithub.com/cli/cli) | minor | `v2.53.0` ->
`v2.54.0` |

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.54.0`](https://togithub.com/cli/cli/releases/tag/v2.54.0):
GitHub CLI 2.54.0

[Compare Source](https://togithub.com/cli/cli/compare/v2.53.0...v2.54.0)

#### What's Changed

- Remove redundant whitespace by
[@&#8203;jessehouwing](https://togithub.com/jessehouwing) in
[https://github.com/cli/cli/pull/9334](https://togithub.com/cli/cli/pull/9334)
- Remove attestation test that requires being online by
[@&#8203;steiza](https://togithub.com/steiza) in
[https://github.com/cli/cli/pull/9340](https://togithub.com/cli/cli/pull/9340)
- Update documentation for gh api PATCH by
[@&#8203;cmbuckley](https://togithub.com/cmbuckley) in
[https://github.com/cli/cli/pull/9352](https://togithub.com/cli/cli/pull/9352)
- Clarify usage of template flags for PR and issue creation by
[@&#8203;williammartin](https://togithub.com/williammartin) in
[https://github.com/cli/cli/pull/9354](https://togithub.com/cli/cli/pull/9354)
- Expose json databaseId field for release commands by
[@&#8203;williammartin](https://togithub.com/williammartin) in
[https://github.com/cli/cli/pull/9356](https://togithub.com/cli/cli/pull/9356)
- Expose fullDatabaseId for PR json export by
[@&#8203;williammartin](https://togithub.com/williammartin) in
[https://github.com/cli/cli/pull/9355](https://togithub.com/cli/cli/pull/9355)
- Handle `--bare` clone targets by
[@&#8203;hyperrealist](https://togithub.com/hyperrealist) in
[https://github.com/cli/cli/pull/9271](https://togithub.com/cli/cli/pull/9271)
- Slightly clarify when CLI exits with code 4 by
[@&#8203;williammartin](https://togithub.com/williammartin) in
[https://github.com/cli/cli/pull/9358](https://togithub.com/cli/cli/pull/9358)
- Update sigstore-go in gh CLI to v0.5.1 by
[@&#8203;steiza](https://togithub.com/steiza) in
[https://github.com/cli/cli/pull/9366](https://togithub.com/cli/cli/pull/9366)
- Exit with 1 on authentication issues by
[@&#8203;Stausssi](https://togithub.com/Stausssi) in
[https://github.com/cli/cli/pull/9240](https://togithub.com/cli/cli/pull/9240)
- build(deps): bump github.com/gabriel-vasile/mimetype from 1.4.4 to
1.4.5 by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/cli/cli/pull/9372](https://togithub.com/cli/cli/pull/9372)
- build(deps): bump github.com/google/go-containerregistry from 0.20.0
to 0.20.1 by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/cli/cli/pull/9373](https://togithub.com/cli/cli/pull/9373)
- Add `--remove-milestone` option to `issue edit` and `pr edit` by
[@&#8203;babakks](https://togithub.com/babakks) in
[https://github.com/cli/cli/pull/9344](https://togithub.com/cli/cli/pull/9344)
- handle attest case insensitivity by
[@&#8203;ejahnGithub](https://togithub.com/ejahnGithub) in
[https://github.com/cli/cli/pull/9392](https://togithub.com/cli/cli/pull/9392)

#### New Contributors

- [@&#8203;cmbuckley](https://togithub.com/cmbuckley) made their first
contribution in
[https://github.com/cli/cli/pull/9352](https://togithub.com/cli/cli/pull/9352)
- [@&#8203;hyperrealist](https://togithub.com/hyperrealist) made their
first contribution in
[https://github.com/cli/cli/pull/9271](https://togithub.com/cli/cli/pull/9271)
- [@&#8203;Stausssi](https://togithub.com/Stausssi) made their first
contribution in
[https://github.com/cli/cli/pull/9240](https://togithub.com/cli/cli/pull/9240)
- [@&#8203;ejahnGithub](https://togithub.com/ejahnGithub) made their
first contribution in
[https://github.com/cli/cli/pull/9392](https://togithub.com/cli/cli/pull/9392)

**Full Changelog**: cli/cli@v2.53.0...v2.54.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/izumin5210/dotfiles).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: izumin5210-update-aqua-checksum[bot] <169593670+izumin5210-update-aqua-checksum[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected behavior when cloning bare repositories

4 participants