mirror of https://github.com/smithy-lang/smithy-rs
Fix git repo root detection in tools (#1169)
This commit is contained in:
parent
2cf4a3bbd9
commit
03a2692fd2
|
@ -1234,6 +1234,7 @@ dependencies = [
|
|||
"anyhow",
|
||||
"async-trait",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -161,6 +161,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c"
|
||||
|
||||
[[package]]
|
||||
name = "pretty_assertions"
|
||||
version = "1.0.0"
|
||||
|
@ -257,6 +263,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -333,6 +340,38 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d8d93354fe2a8e50d5953f5ae2e47a3fc2ef03292e7ea46e3cc38f549525fb9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.8.0"
|
||||
|
|
|
@ -258,6 +258,12 @@ version = "2.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c"
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.24"
|
||||
|
@ -383,6 +389,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -454,6 +461,38 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d8d93354fe2a8e50d5953f5ae2e47a3fc2ef03292e7ea46e3cc38f549525fb9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.7"
|
||||
|
|
|
@ -19,6 +19,18 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.8"
|
||||
|
@ -50,6 +62,7 @@ dependencies = [
|
|||
"anyhow",
|
||||
"async-trait",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -84,6 +97,38 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d8d93354fe2a8e50d5953f5ae2e47a3fc2ef03292e7ea46e3cc38f549525fb9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
|
|
|
@ -15,3 +15,4 @@ async-shell = ["tokio"]
|
|||
anyhow = "1"
|
||||
async-trait = "0.1"
|
||||
tokio = { version = "1", features = ["rt", "macros"], optional = true }
|
||||
tracing = "0.1"
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/bash
|
||||
if [[ "$1" != "rev-parse" || "$2" != "--show-toplevel" ]]; then
|
||||
echo "wrong arguments" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "/git/repo/root/path"
|
|
@ -3,9 +3,11 @@
|
|||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use crate::shell::ShellOperation;
|
||||
use anyhow::Result;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tracing::warn;
|
||||
|
||||
mod checkout_revision;
|
||||
pub use checkout_revision::CheckoutRevision;
|
||||
|
@ -16,26 +18,17 @@ pub use get_current_tag::GetCurrentTag;
|
|||
mod get_last_commit;
|
||||
pub use get_last_commit::GetLastCommit;
|
||||
|
||||
mod get_repo_root;
|
||||
pub use get_repo_root::GetRepoRoot;
|
||||
|
||||
/// Attempts to find git repository root from the given location.
|
||||
pub fn find_git_repository_root(repo_name: &str, location: impl AsRef<Path>) -> Result<PathBuf> {
|
||||
let mut current_dir = location.as_ref().canonicalize()?;
|
||||
let os_name = OsStr::new(repo_name);
|
||||
loop {
|
||||
if is_git_root(¤t_dir) {
|
||||
if let Some(file_name) = current_dir.file_name() {
|
||||
if os_name == file_name {
|
||||
return Ok(current_dir);
|
||||
}
|
||||
}
|
||||
break;
|
||||
} else if !current_dir.pop() {
|
||||
break;
|
||||
}
|
||||
let path = GetRepoRoot::new(location.as_ref()).run()?;
|
||||
if path.file_name() != Some(OsStr::new(repo_name)) {
|
||||
warn!(
|
||||
"repository root {:?} doesn't have expected name '{}'",
|
||||
path, repo_name
|
||||
);
|
||||
}
|
||||
bail!("failed to find {0} repository root", repo_name)
|
||||
}
|
||||
|
||||
fn is_git_root(path: &Path) -> bool {
|
||||
let path = path.join(".git");
|
||||
path.exists() && path.is_dir()
|
||||
Ok(path)
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn get_last_commit_faijlure() {
|
||||
fn get_last_commit_failure() {
|
||||
let result = GetLastCommit {
|
||||
program: "./git_fails",
|
||||
repo_path: "./fake_git".into(),
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
use crate::shell::{handle_failure, output_text, ShellOperation};
|
||||
use anyhow::Result;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
pub struct GetRepoRoot {
|
||||
program: &'static str,
|
||||
start_path: PathBuf,
|
||||
}
|
||||
|
||||
impl GetRepoRoot {
|
||||
pub fn new(start_path: impl Into<PathBuf>) -> GetRepoRoot {
|
||||
GetRepoRoot {
|
||||
program: "git",
|
||||
start_path: start_path.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ShellOperation for GetRepoRoot {
|
||||
type Output = PathBuf;
|
||||
|
||||
fn run(&self) -> Result<PathBuf> {
|
||||
let mut command = Command::new(self.program);
|
||||
command.arg("rev-parse");
|
||||
command.arg("--show-toplevel");
|
||||
command.current_dir(&self.start_path);
|
||||
|
||||
let output = command.output()?;
|
||||
handle_failure("determine git repo root", &output)?;
|
||||
let (stdout, _) = output_text(&output);
|
||||
Ok(stdout.trim().into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(target_os = "windows")))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn get_repo_root_success() {
|
||||
let last_commit = GetRepoRoot {
|
||||
program: "./git_revparse_show_toplevel",
|
||||
start_path: "./fake_git".into(),
|
||||
}
|
||||
.run()
|
||||
.unwrap();
|
||||
assert_eq!(PathBuf::from("/git/repo/root/path"), last_commit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_repo_root_failure() {
|
||||
let result = GetRepoRoot {
|
||||
program: "./git_fails",
|
||||
start_path: "./fake_git".into(),
|
||||
}
|
||||
.run();
|
||||
|
||||
assert!(result.is_err(), "expected error, got {:?}", result);
|
||||
assert_eq!(
|
||||
"Failed to determine git repo root:\n\
|
||||
Status: 1\n\
|
||||
Stdout: some stdout failure message\n\n\
|
||||
Stderr: some stderr failure message\n\n",
|
||||
format!("{}", result.err().unwrap())
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue