Skip to content

Commit 60024ab

Browse files
committed
prefer fixing commit hash on browsing files
1 parent 113078b commit 60024ab

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/page.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub enum Page {
1515
},
1616
FileOrDir {
1717
relative_path: String,
18+
hash: String,
1819
},
1920
}
2021

@@ -53,8 +54,9 @@ impl<'a> BrowsePageParser<'a> {
5354
}
5455

5556
fn try_parse_file_or_dir(&self) -> ParseResult {
56-
if self.opts.args.len() != 1 {
57-
return Err(" Invalid number of arguments for file or directory (1 is expected)".to_string());
57+
let len = self.opts.args.len();
58+
if len != 1 && len != 2 {
59+
return Err(" Invalid number of arguments for file or directory (1..2 is expected)".to_string());
5860
}
5961

6062
let path = &self.opts.args[0];
@@ -67,8 +69,14 @@ impl<'a> BrowsePageParser<'a> {
6769
.ok_or(" Failed to convert path into UTF-8 string")?
6870
.to_string();
6971

72+
let hash = if len == 2 {
73+
self.git.hash(&self.opts.args[1].as_str())
74+
} else {
75+
self.git.hash(&"HEAD")
76+
};
7077
Ok(Page::FileOrDir {
7178
relative_path: relative_path,
79+
hash: hash?,
7280
})
7381
}
7482
}

src/service.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn parse_github_url(user: &str, repo: &str, branch: &String, page: &Page) -> Str
1111
&Page::Open => format!("https://github.com/{}/{}/tree/{}", user, repo, branch),
1212
&Page::Diff {ref lhs, ref rhs} => format!("https://github.com/{}/{}/compare/{}...{}", user, repo, lhs, rhs),
1313
&Page::Commit {ref hash} => format!("https://github.com/{}/{}/commit/{}", user, repo, hash),
14-
&Page::FileOrDir {ref relative_path} => format!("https://github.com/{}/{}/blob/{}/{}", user, repo, branch, relative_path),
14+
&Page::FileOrDir {ref relative_path, ref hash} => format!("https://github.com/{}/{}/blob/{}/{}", user, repo, hash, relative_path),
1515
}
1616
}
1717

@@ -20,7 +20,7 @@ fn parse_bitbucket_url(user: &str, repo: &str, branch: &String, page: &Page) ->
2020
&Page::Open => Ok(format!("https://bitbucket.org/{}/{}/branch/{}", user, repo, branch)),
2121
&Page::Diff {..} => Err("BitBucket does not support diff between commits (see https://bitbucket.org/site/master/issues/4779/ability-to-diff-between-any-two-commits)".to_string()),
2222
&Page::Commit {ref hash} => Ok(format!("https://bitbucket.org/{}/{}/commits/{}", user, repo, hash)),
23-
&Page::FileOrDir {ref relative_path} => Err(format!("Not implemented! Cannot open file or directory {}. It needs commit hash", relative_path)),
23+
&Page::FileOrDir {ref relative_path, ref hash} => Ok(format!("https://bitbucket.org/{}/{}/src/{}/{}", user, repo, hash, relative_path)),
2424
}
2525
}
2626

src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ macro_rules! errorln(
99
// Note:
1010
// string::String::insert_str() is not available because it's unstable.
1111
// https://github.com/rust-lang/rust/issues/35553
12-
pub fn insert(target: &mut String, index: usize, replaced: &str) {
13-
for c in replaced.chars().rev() {
12+
pub fn insert(target: &mut String, index: usize, inserted: &str) {
13+
for c in inserted.chars().rev() {
1414
target.insert(index, c);
1515
}
1616
}

0 commit comments

Comments
 (0)