-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add support for parsing subdirectories out of git URLs #9
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be7b058
Add support for parsing subdirectories out of git URLs
camjackson fed96d2
Add method for determining if two contexts are equivalent
camjackson b6d54bf
Consolidate git parsing regexes into one place
camjackson a7a5771
Make Context/GitUrl compatibility a bit clearer
camjackson cf16eb5
Replace Context.is_compatible_with with Context.without_subdirectory
camjackson 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 |
---|---|---|
|
@@ -32,6 +32,17 @@ impl Context { | |
Context::Dir(Path::new(&s_ref).to_owned()) | ||
} | ||
} | ||
|
||
/// Returns a new Context which is the same as the | ||
/// this one, but without any subdirectory part | ||
pub fn without_subdirectory(&self) -> Context { | ||
match self { | ||
&Context::Dir(_) => self.clone(), | ||
&Context::GitUrl(ref git_url) => { | ||
Context::GitUrl(git_url.without_subdirectory()) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl_interpolatable_value!(Context); | ||
|
@@ -80,3 +91,19 @@ fn context_may_contain_dir_paths() { | |
assert_eq!(context.to_string(), path); | ||
} | ||
} | ||
|
||
#[test] | ||
fn without_subdirectory_removes_the_optional_subdir() { | ||
let dir: Context = FromStr::from_str("./foo").unwrap(); | ||
let plain_repo: Context = FromStr::from_str("[email protected]:docker/docker.git").unwrap(); | ||
let repo_with_branch: Context = FromStr::from_str("[email protected]:docker/docker.git#somebranch").unwrap(); | ||
let repo_with_subdir: Context = FromStr::from_str("[email protected]:docker/docker.git#:somedir").unwrap(); | ||
let repo_with_branch_and_subdir: Context = FromStr::from_str("[email protected]:docker/docker.git#somebranch:somedir").unwrap(); | ||
|
||
assert_eq!(dir, dir.without_subdirectory()); | ||
assert_eq!(plain_repo, plain_repo.without_subdirectory()); | ||
assert_eq!(repo_with_branch, repo_with_branch.without_subdirectory()); | ||
|
||
assert_eq!(plain_repo, repo_with_subdir.without_subdirectory()); | ||
assert_eq!(repo_with_branch, repo_with_branch_and_subdir.without_subdirectory()); | ||
} |
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 |
---|---|---|
|
@@ -71,6 +71,44 @@ impl GitUrl { | |
} | ||
} | ||
} | ||
|
||
/// Returns a new GitUrl which is the same as the | ||
/// this one, but without any subdirectory part | ||
pub fn without_subdirectory(&self) -> GitUrl { | ||
let (repository, branch, _) = self.parse_parts(); | ||
let branch_str = match branch { | ||
Some(branch) => format!("#{}", branch), | ||
None => String::new(), | ||
}; | ||
GitUrl { url: format!("{}{}", repository, branch_str) } | ||
} | ||
|
||
/// Extract the repository part of the URL | ||
pub fn repository(&self) -> String { | ||
self.parse_parts().0 | ||
} | ||
|
||
/// Extract the optional branch part of the git URL | ||
pub fn branch(&self) -> Option<String> { | ||
self.parse_parts().1 | ||
} | ||
|
||
/// Extract the optional subdirectory part of the git URL | ||
pub fn subdirectory(&self) -> Option<String> { | ||
self.parse_parts().2 | ||
} | ||
|
||
fn parse_parts(&self) -> (String, Option<String>, Option<String>) { | ||
lazy_static! { | ||
static ref URL_PARSE: Regex = Regex::new(r#"^([^#]+)(#([^:]+)?(:(.+))?)?$"#).unwrap(); | ||
} | ||
let captures = URL_PARSE.captures(&self.url).unwrap(); | ||
( | ||
captures.at(1).unwrap().to_string(), | ||
captures.at(3).map(|branch| branch.to_string()), | ||
captures.at(5).map(|branch| branch.to_string()), | ||
) | ||
} | ||
} | ||
|
||
impl fmt::Display for GitUrl { | ||
|
@@ -134,3 +172,39 @@ fn to_url_converts_git_urls_to_real_ones() { | |
assert!(GitUrl::new(url).is_err()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn it_can_extract_its_repo_branch_and_subdir_parts() { | ||
let urls = &[ | ||
"git://github.com/docker/docker", | ||
"https://github.com/docker/docker.git", | ||
"http://github.com/docker/docker.git", | ||
"[email protected]:docker/docker.git", | ||
"[email protected]:atlassianlabs/atlassian-docker.git", | ||
"github.com/docker/docker.git", | ||
]; | ||
|
||
// Refs/folders specified as per: | ||
// https://docs.docker.com/engine/reference/commandline/build/#git-repositories | ||
for &url in urls { | ||
let plain = GitUrl::new(url).unwrap(); | ||
assert_eq!(plain.repository(), url); | ||
assert_eq!(plain.branch(), None); | ||
assert_eq!(plain.subdirectory(), None); | ||
|
||
let with_ref = GitUrl::new(format!("{}{}", url, "#mybranch")).unwrap(); | ||
assert_eq!(with_ref.repository(), url); | ||
assert_eq!(with_ref.branch(), Some("mybranch".to_string())); | ||
assert_eq!(with_ref.subdirectory(), None); | ||
|
||
let with_subdir = GitUrl::new(format!("{}{}", url, "#:myfolder")).unwrap(); | ||
assert_eq!(with_subdir.repository(), url); | ||
assert_eq!(with_subdir.branch(), None); | ||
assert_eq!(with_subdir.subdirectory(), Some("myfolder".to_string())); | ||
|
||
let with_ref_and_subdir = GitUrl::new(format!("{}{}", url, "#mybranch:myfolder")).unwrap(); | ||
assert_eq!(with_ref_and_subdir.repository(), url); | ||
assert_eq!(with_ref_and_subdir.branch(), Some("mybranch".to_string())); | ||
assert_eq!(with_ref_and_subdir.subdirectory(), Some("myfolder".to_string())); | ||
} | ||
} |
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.
I'm glad to see this test case.