-
Notifications
You must be signed in to change notification settings - Fork 285
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
fix(dotgit): set publicheads correctly for transparent git mode #969
Open
vegerot
wants to merge
1
commit into
facebook:main
Choose a base branch
from
vegerot:pr969
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,17 @@ impl GlobalGit { | |
&["--show-scope", "--get-regexp", "^(remote|user)\\."], | ||
) | ||
.output()?; | ||
let out = String::from_utf8(out.stdout)?; | ||
Ok(translate_git_config_output(&out)) | ||
let config = String::from_utf8(out.stdout)?; | ||
let remotes_out = self | ||
.git_cmd( | ||
"ls-remote", &["--symref", ".", "HEAD"]) | ||
.output()?; | ||
let remotes = String::from_utf8(remotes_out.stdout)?; | ||
Ok(translate_git_config_output(&config, &remotes)) | ||
} | ||
} | ||
|
||
fn translate_git_config_output(out: &str) -> (String, String) { | ||
fn translate_git_config_output(out: &str, remotes: &str) -> (String, String) { | ||
// Example output: | ||
// global user.name Foo Bar | ||
// global user.email [email protected] | ||
|
@@ -94,6 +99,16 @@ fn translate_git_config_output(out: &str) -> (String, String) { | |
)); | ||
} | ||
|
||
let default_publicheads = "origin/master,origin/main"; | ||
if let Some(default_branch) = parse_symref_head(remotes) { | ||
repo_config.push_str(&format!( | ||
"\n[remotenames]\n# from git ls-remote\npublicheads=origin/{},{}\n", | ||
default_branch, | ||
default_publicheads, | ||
)); | ||
} | ||
|
||
|
||
(user_config, repo_config) | ||
} | ||
|
||
|
@@ -135,6 +150,18 @@ fn parse_git_config_output_line(line: &str) -> Option<(&str, &str, &str)> { | |
Some((scope, name, value)) | ||
} | ||
|
||
fn parse_symref_head(lines: &str) -> Option<String> { | ||
for line in lines.lines() { | ||
// example: "ref: refs/heads/defaultbranch HEAD" | ||
let prefix = "ref: refs/heads/"; | ||
let suffix = "\tHEAD"; | ||
if line.starts_with(prefix) && line.match_indices(suffix).count() ==1 { | ||
return Some(line[prefix.len()..line.len()-suffix.len()].to_string()); | ||
} | ||
} | ||
return None | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
@@ -148,16 +175,22 @@ local remote.origin.pushurl [email protected]:foo/repo | |
local remote.upstream.url https://example.com/upstream/repo | ||
local user.email [email protected] | ||
"#; | ||
let (user, repo) = translate_git_config_output(out); | ||
let remotes = r#" | ||
ref: refs/heads/defaultbranch HEAD | ||
4661e74b5ebe8727d1b0f8c29b1697f1f42daf70 HEAD | ||
ref: refs/remotes/origin/defaultbranch refs/remotes/origin/HEAD | ||
4661e74b5ebe8727d1b0f8c29b1697f1f42daf70 refs/remotes/origin/HEAD | ||
"#; | ||
let (user, repo) = translate_git_config_output(out, remotes); | ||
assert_eq!( | ||
user, | ||
r#"[ui] | ||
# from git config: user.name and user.email | ||
username = Foo Bar <[email protected]> | ||
"# | ||
); | ||
assert_eq!( | ||
repo, | ||
let got = repo; | ||
let want = | ||
r#"[paths] | ||
# from git config: remote.origin.url | ||
default = https://example.com/foo/repo | ||
|
@@ -169,8 +202,12 @@ upstream = https://example.com/upstream/repo | |
[ui] | ||
# from git config: user.name and user.email | ||
username = Foo Bar <[email protected]> | ||
"# | ||
); | ||
|
||
[remotenames] | ||
# from git ls-remote | ||
publicheads=origin/defaultbranch,origin/master,origin/main | ||
"#; | ||
assert_eq!(got, want, "\n expanded left: {got}\n------------\nexpanded right: {want}\n"); | ||
} | ||
|
||
#[test] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This performs a network call, which might be unexpected or undesirable.
How about parsing the output of
git rev-parse --symbolic refs/remotes/origin/HEAD
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! If it's as robust as
ls-remote
then this will save a network call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
lookup_reference
to resolve the reference without the spawning process overhead.