Skip to content

Commit

Permalink
check branch name is not empty (fix #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 31, 2020
1 parent ff468e7 commit 735c986
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/argv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ impl Parsed {
let cwd = get_cwd(matches.opt_str("d"))?;
let git = Git::new(&cwd, &env.git_command);
let branch = if let Some(b) = matches.opt_str("b") {
if b.is_empty() {
return Error::err(ErrorKind::BranchNameEmpty);
}
Some(b)
} else if matches.opt_present("c") {
Some(git.current_branch()?)
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub enum ErrorKind {
SpecifiedDirNotExist {
dir: String,
},
BranchNameEmpty,
}

impl fmt::Display for ErrorKind {
Expand Down Expand Up @@ -223,6 +224,7 @@ impl fmt::Display for ErrorKind {
CannotBlameDirectory{dir} => write!(f, "Cannot blame directory '{}'. Please specify file path", dir),
UserBrowseCommandFailed{cmd, url, msg} => write!(f, "Command '{}' failed to open URL {}. Please check $GIT_BRWS_BROWSE_COMMAND. stderr: {}", cmd, url, msg),
SpecifiedDirNotExist{dir} => write!(f, "Specified directory '{}' with -d option does not exist", dir),
BranchNameEmpty => write!(f, "Branch name cannot be empty"),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/argv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,11 @@ fn current_branch_flag() {
p => panic!("{:?}", p),
}
}

// #26
#[test]
fn branch_name_is_empty() {
let e = Parsed::parse_iter(&["git-brws", "-b", ""]).unwrap_err();
let k = e.kind();
assert!(matches!(k, ErrorKind::BranchNameEmpty), "{:?}", k);
}

0 comments on commit 735c986

Please sign in to comment.