Skip to content

Commit f8ab261

Browse files
committed
first sketch of parsing git remotes (from git :D)
One fine day we will have our own git config parsing, too ;)
1 parent 7bbba5a commit f8ab261

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gitoxide-core/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ anyhow = "1.0.31"
2929
quick-error = "2.0.0"
3030
bytesize = "1.0.1"
3131
serde_json = { version = "1.0.56", optional = true }
32+
33+
# for 'organize' functionality
34+
git-url = { version = "^0.1.1", path = "../git-url" }
35+
bstr = { version = "0.2.14", features = ["unicode"] }

gitoxide-core/src/organize.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,54 @@ impl Default for Mode {
1616
pub fn run(_mode: Mode, _source_dir: PathBuf, _destination: PathBuf, _progress: impl Progress) -> anyhow::Result<()> {
1717
Ok(())
1818
}
19+
20+
mod parse {
21+
use anyhow::{bail, Context};
22+
use bstr::{BStr, ByteSlice};
23+
24+
fn verbose_remotes(input: &[u8]) -> anyhow::Result<Vec<(&BStr, git_url::Url)>> {
25+
fn parse_line(line: &BStr) -> anyhow::Result<(&BStr, git_url::Url)> {
26+
let mut tokens = line.splitn(2, |b| *b == b'\t');
27+
Ok(match (tokens.next(), tokens.next(), tokens.next()) {
28+
(Some(remote), Some(url_and_type), None) => {
29+
let mut tokens = url_and_type.splitn(2, |b| *b == b' ');
30+
match (tokens.next(), tokens.next(), tokens.next()) {
31+
(Some(url), Some(_type), None) => (remote.as_bstr(), git_url::parse(url)?),
32+
_ => bail!("None or more than one 'space' as separator"),
33+
}
34+
}
35+
_ => bail!("None or more than one tab as separator"),
36+
})
37+
}
38+
39+
let mut out = Vec::new();
40+
for line in input.split(|b| *b == b'\n') {
41+
let line = line.as_bstr();
42+
if line.trim().len() == 0 {
43+
continue;
44+
}
45+
out.push(
46+
parse_line(line).with_context(|| format!("Line {:?} should be <origin>TAB<URL>SPACE<TYPE>", line))?,
47+
);
48+
}
49+
50+
Ok(out)
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
static GITOXIDE_REMOTES: &[u8] = br#"commitgraph https://github.com/avoidscorn/gitoxide (fetch)
57+
commitgraph https://github.com/avoidscorn/gitoxide (push)
58+
origin https://github.com/Byron/gitoxide (fetch)
59+
origin https://github.com/Byron/gitoxide (push)
60+
rad rad://hynkuwzskprmswzeo4qdtku7grdrs4ffj3g9tjdxomgmjzhtzpqf81@hwd1yregyf1dudqwkx85x5ps3qsrqw3ihxpx3ieopq6ukuuq597p6m8161c.git (fetch)
61+
rad rad://hynkuwzskprmswzeo4qdtku7grdrs4ffj3g9tjdxomgmjzhtzpqf81@hwd1yregyf1dudqwkx85x5ps3qsrqw3ihxpx3ieopq6ukuuq597p6m8161c.git (push)
62+
"#;
63+
#[test]
64+
fn valid_verbose_remotes() -> anyhow::Result<()> {
65+
assert_eq!(verbose_remotes(GITOXIDE_REMOTES)?, vec![]);
66+
Ok(())
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)