Skip to content

Commit

Permalink
fix /blob/ to /tree/ for dirs of repos on GitHub and GitLab (fix #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Sep 2, 2020
1 parent 735c986 commit 3743eea
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum Page {
hash: String,
line: Option<Line>,
blame: bool,
is_dir: bool,
},
Issue {
number: usize,
Expand Down Expand Up @@ -181,8 +182,9 @@ impl<'a> BrowsePageParser<'a> {

let (path, line) = self.parse_path_and_line();
let path = fs::canonicalize(path)?;
let is_dir = path.is_dir();

if path.is_dir() {
if is_dir {
if self.cfg.blame {
return Error::err(ErrorKind::CannotBlameDirectory {
dir: path.to_string_lossy().into(),
Expand Down Expand Up @@ -235,6 +237,7 @@ impl<'a> BrowsePageParser<'a> {
hash,
line,
blame: self.cfg.blame,
is_dir,
})
}

Expand Down
40 changes: 26 additions & 14 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,30 @@ fn build_github_like_url(
ref hash,
line,
blame,
} => Ok(format!(
"https://{host}/{user}/{repo}/{feat}/{hash}/{path}{anchor}",
host = host,
user = user,
repo = repo,
feat = if *blame { "blame" } else { "blob" },
hash = hash,
path = to_slash(relative_path),
anchor = match line {
None => "".to_string(),
Some(Line::At(line)) => format!("#L{}", line),
Some(Line::Range(start, end)) => format!("#L{}-L{}", start, end),
},
)),
is_dir,
} => {
let feat = if *blame {
"blame"
} else if *is_dir {
"tree"
} else {
"blob"
};
Ok(format!(
"https://{host}/{user}/{repo}/{feat}/{hash}/{path}{anchor}",
host = host,
user = user,
repo = repo,
feat = feat,
hash = hash,
path = to_slash(relative_path),
anchor = match line {
None => "".to_string(),
Some(Line::At(line)) => format!("#L{}", line),
Some(Line::Range(start, end)) => format!("#L{}-L{}", start, end),
},
))
}
Page::Issue { number } => Ok(format!(
"https://{}/{}/{}/issues/{}",
host, user, repo, number
Expand Down Expand Up @@ -263,6 +273,7 @@ fn build_bitbucket_url(user: &str, repo: &str, cfg: &Config, page: &Page) -> Res
ref hash,
line,
blame,
is_dir: _,
} => Ok(format!(
"https://bitbucket.org/{user}/{repo}/{feat}/{hash}/{path}{anchor}",
user = user,
Expand Down Expand Up @@ -319,6 +330,7 @@ fn build_azure_devops_url(team: &str, repo: &str, cfg: &Config, page: &Page) ->
ref hash,
line: None,
blame,
is_dir: _,
} => Ok(format!(
"https://dev.azure.com/{}/_git/{}/commit/{}?path={}{}",
team,
Expand Down
2 changes: 2 additions & 0 deletions src/test/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ fn parse_file_or_dir() {
hash,
line: None,
blame,
is_dir,
} => {
assert_eq!(relative_path, relative.to_str().unwrap());
assert!(!hash.is_empty());
assert!(!blame);
assert_eq!(is_dir, relative.is_dir());
}
p => assert!(false, "Unexpected result: {:?}", p),
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ fn file_path_with_file_path() {
.to_string_lossy()
.into_owned();
let hash = "561848bad7164d7568658456088b107ec9efd9f3".to_string();
let is_dir = false;

let page = move |line: Option<Line>, blame: bool| Page::FileOrDir {
relative_path: relative_path.clone(),
hash: hash.clone(),
line,
blame,
is_dir,
};

for (
Expand Down

0 comments on commit 3743eea

Please sign in to comment.