Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find remote reference when HEAD detached #48

Merged
merged 8 commits into from
Apr 10, 2024
Merged

find remote reference when HEAD detached #48

merged 8 commits into from
Apr 10, 2024

Conversation

gnalh
Copy link
Contributor

@gnalh gnalh commented Apr 8, 2024

Scan the references to find the matching reference name. This is equivalent to git for-each-ref --contains e0c6eaa19870c1ae30601bed5bee604c7436c9a2

After:

2024-04-08T05:37:42 [INFO] - Found git_sha: Some("e0c6eaa19870c1ae30601bed5bee604c7436c9a2")
2024-04-08T05:37:42 [INFO] - Found git_branch: Some("refs/remotes/pull/47/merge")

Before

2024-04-08T05:39:17 [INFO] - Found git_sha: Some("e0c6eaa19870c1ae30601bed5bee604c7436c9a2")
2024-04-08T05:39:17 [INFO] - Found git_branch: None

Repro:

git -c protocol.version=2 fetch --no-tags --prune --no-recurse-submodules --depth=1 origin +e0c6eaa19870c1ae30601bed5bee604c7436c9a2:refs/remotes/pull/47/merge
git checkout --progress --force refs/remotes/pull/47/merge

@gnalh gnalh marked this pull request as ready for review April 8, 2024 05:45
@gnalh gnalh changed the title find reference in detached head find remote reference when in detached head Apr 8, 2024
@gnalh gnalh changed the title find remote reference when in detached head find remote reference when HEAD detached Apr 8, 2024
src/scanner.rs Outdated
Comment on lines 174 to 177
if id.is_some() && id.unwrap().to_string() == git_head_id.to_string() {
git_head_branch =
Some(r.name().to_path().to_str().unwrap().to_string());
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sure there's a more elegant solution here. If there is, any pointers would be greatly appreciated.

Copy link

trunk-staging-io bot commented Apr 8, 2024

💊 0 quarantined ✅ 10 passed 🕐 10 new ⋅ (learn more)

docs ⋅ learn more about trunk.io

Copy link

⚠️ 0 flaky ❌ 0 failed 💊 0 quarantined ✅ 10 passed (details for db6739082df1eb5222a5208252e883f3db44a343)
docs ⋅ learn more about trunk.io

Copy link
Contributor

@riya-n riya-n left a comment

Choose a reason for hiding this comment

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

Looks good, but not sure we need to match or warn. Just a suggestion, but we could do something like this as well

if git_head_branch.is_none() {
  for rb in git_repo.references()?.remote_branches()? {
    git_head_branch = rb.map_or(None, |r| {
      let id = r.target().try_id().map(|id| id.to_string());
      let git_head_id = git_head.id().map(|id| id.to_string());
      if id.is_some() && git_head_id.is_some() && id == git_head_id {
        return r.name().to_path().to_str().map(|s| s.to_string());
      }
      None
    });
  }
}

src/scanner.rs Outdated Show resolved Hide resolved
src/scanner.rs Outdated Show resolved Hide resolved
src/scanner.rs Outdated Show resolved Hide resolved
gnalh and others added 2 commits April 9, 2024 12:44
Signed-off-by: Gabe <78751069+gnalh@users.noreply.github.com>
@gnalh gnalh merged commit 1f32450 into main Apr 10, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants