build: support build without git (#3309)

* build: support build without git

Signed-off-by: tison <wander4096@gmail.com>

* chore

Signed-off-by: tison <wander4096@gmail.com>

* address comment

Signed-off-by: tison <wander4096@gmail.com>

* fix syntax

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
This commit is contained in:
tison
2024-02-18 18:30:01 +08:00
committed by GitHub
parent b889d57b32
commit 4e04a4e48f
35 changed files with 154 additions and 116 deletions

View File

@@ -5,8 +5,8 @@ edition.workspace = true
license.workspace = true
[dependencies]
arrow-schema.workspace = true
arrow.workspace = true
arrow-schema.workspace = true
async-compression = { version = "0.3", features = [
"bzip2",
"gzip",
@@ -34,8 +34,8 @@ regex = "1.7"
serde.workspace = true
snafu.workspace = true
strum.workspace = true
tokio-util.workspace = true
tokio.workspace = true
tokio-util.workspace = true
url = "2.3"
[dev-dependencies]

View File

@@ -8,7 +8,6 @@ license.workspace = true
api.workspace = true
arc-swap = "1.0"
async-trait.workspace = true
build-data = "0.1"
chrono-tz = "0.6"
common-error.workspace = true
common-macro.workspace = true
@@ -16,6 +15,7 @@ common-query.workspace = true
common-runtime.workspace = true
common-telemetry.workspace = true
common-time.workspace = true
common-version.workspace = true
datafusion.workspace = true
datatypes.workspace = true
libc = "0.2"

View File

@@ -22,8 +22,6 @@ use datatypes::vectors::{StringVector, VectorRef};
use crate::function::{Function, FunctionContext};
const DEFAULT_VALUE: &str = "unknown";
/// Generates build information
#[derive(Clone, Debug, Default)]
pub struct BuildFunction;
@@ -52,15 +50,7 @@ impl Function for BuildFunction {
}
fn eval(&self, _func_ctx: FunctionContext, _columns: &[VectorRef]) -> Result<VectorRef> {
let build_info = format!(
"branch: {}\ncommit: {}\ncommit short: {}\ndirty: {}\nversion: {}",
build_data::get_git_branch().unwrap_or_else(|_| DEFAULT_VALUE.to_string()),
build_data::get_git_commit().unwrap_or_else(|_| DEFAULT_VALUE.to_string()),
build_data::get_git_commit_short().unwrap_or_else(|_| DEFAULT_VALUE.to_string()),
build_data::get_git_dirty().map_or(DEFAULT_VALUE.to_string(), |v| v.to_string()),
env!("CARGO_PKG_VERSION")
);
let build_info = common_version::build_info().to_string();
let v = Arc::new(StringVector::from(vec![build_info]));
Ok(v)
}
@@ -87,14 +77,7 @@ mod tests {
volatility: Volatility::Immutable
} if valid_types == vec![ConcreteDataType::string_datatype()]
));
let build_info = format!(
"branch: {}\ncommit: {}\ncommit short: {}\ndirty: {}\nversion: {}",
build_data::get_git_branch().unwrap_or_else(|_| DEFAULT_VALUE.to_string()),
build_data::get_git_commit().unwrap_or_else(|_| DEFAULT_VALUE.to_string()),
build_data::get_git_commit_short().unwrap_or_else(|_| DEFAULT_VALUE.to_string()),
build_data::get_git_dirty().map_or(DEFAULT_VALUE.to_string(), |v| v.to_string()),
env!("CARGO_PKG_VERSION")
);
let build_info = common_version::build_info().to_string();
let vector = build.eval(FunctionContext::default(), &[]).unwrap();
let expect: VectorRef = Arc::new(StringVector::from(vec![build_info]));
assert_eq!(expect, vector);

View File

@@ -13,5 +13,5 @@
// limitations under the License.
fn main() {
common_version::setup_git_versions();
common_version::setup_build_info();
}

View File

@@ -28,8 +28,8 @@ common-wal.workspace = true
datatypes.workspace = true
derive_builder.workspace = true
etcd-client.workspace = true
futures-util.workspace = true
futures.workspace = true
futures-util.workspace = true
hex = { version = "0.4" }
humantime-serde.workspace = true
lazy_static.workspace = true

View File

@@ -11,9 +11,9 @@ common-error.workspace = true
common-macro.workspace = true
common-recordbatch.workspace = true
common-time.workspace = true
datafusion.workspace = true
datafusion-common.workspace = true
datafusion-expr.workspace = true
datafusion.workspace = true
datatypes.workspace = true
serde.workspace = true
snafu.workspace = true

View File

@@ -9,8 +9,8 @@ arc-swap = "1.6"
common-base.workspace = true
common-error.workspace = true
common-macro.workspace = true
datafusion-common.workspace = true
datafusion.workspace = true
datafusion-common.workspace = true
datatypes.workspace = true
futures.workspace = true
paste = "1.0"

View File

@@ -14,10 +14,10 @@ once_cell.workspace = true
paste.workspace = true
prometheus.workspace = true
snafu.workspace = true
tokio.workspace = true
tokio-metrics = "0.3"
tokio-metrics-collector = "0.2"
tokio-util.workspace = true
tokio.workspace = true
[dev-dependencies]
tokio-test = "0.4"

View File

@@ -13,10 +13,10 @@ common-catalog.workspace = true
common-error.workspace = true
common-macro.workspace = true
common-telemetry.workspace = true
datafusion.workspace = true
datafusion-common.workspace = true
datafusion-expr.workspace = true
datafusion-substrait.workspace = true
datafusion.workspace = true
datatypes.workspace = true
futures = "0.3"
promql.workspace = true

View File

@@ -6,8 +6,8 @@ license.workspace = true
[dependencies]
arrow.workspace = true
chrono-tz = "0.8"
chrono.workspace = true
chrono-tz = "0.8"
common-error.workspace = true
common-macro.workspace = true
once_cell.workspace = true

View File

@@ -12,24 +12,89 @@
// See the License for the specific language governing permissions and
// limitations under the License.
const DEFAULT_VALUE: &str = "unknown";
use std::borrow::Cow;
use std::fmt::Display;
use std::sync::OnceLock;
const UNKNOWN: &str = "unknown";
pub struct BuildInfo {
pub branch: Cow<'static, str>,
pub commit: Cow<'static, str>,
pub commit_short: Cow<'static, str>,
pub dirty: Cow<'static, str>,
pub timestamp: Cow<'static, str>,
/// Rustc Version
pub rustc: Cow<'static, str>,
/// GreptimeDB Version
pub version: Cow<'static, str>,
}
impl Display for BuildInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
[
format!("branch: {}", self.branch),
format!("commit: {}", self.commit),
format!("commit_short: {}", self.commit_short),
format!("dirty: {}", self.dirty),
format!("version: {}", self.version),
]
.join("\n")
)
}
}
static BUILD: OnceLock<BuildInfo> = OnceLock::new();
pub fn build_info() -> &'static BuildInfo {
BUILD.get_or_init(|| {
let branch = build_data::get_git_branch()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let commit = build_data::get_git_commit()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let commit_short = build_data::get_git_commit_short()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let dirty = build_data::get_git_dirty()
.map(|b| Cow::Owned(b.to_string()))
.unwrap_or(Cow::Borrowed(UNKNOWN));
let timestamp = build_data::get_source_time()
.map(|ts| Cow::Owned(build_data::format_timestamp(ts)))
.unwrap_or(Cow::Borrowed(UNKNOWN));
let rustc = build_data::get_rustc_version()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let version = Cow::Borrowed(env!("CARGO_PKG_VERSION"));
BuildInfo {
branch,
commit,
commit_short,
dirty,
timestamp,
rustc,
version,
}
})
}
#[allow(clippy::print_stdout)]
pub fn setup_git_versions() {
println!(
"cargo:rustc-env=GIT_COMMIT={}",
build_data::get_git_commit().unwrap_or_else(|_| DEFAULT_VALUE.to_string())
);
pub fn setup_build_info() {
let build_info = build_info();
println!("cargo:rustc-env=GIT_COMMIT={}", build_info.commit);
println!(
"cargo:rustc-env=GIT_COMMIT_SHORT={}",
build_data::get_git_commit_short().unwrap_or_else(|_| DEFAULT_VALUE.to_string())
);
println!(
"cargo:rustc-env=GIT_BRANCH={}",
build_data::get_git_branch().unwrap_or_else(|_| DEFAULT_VALUE.to_string())
);
println!(
"cargo:rustc-env=GIT_DIRTY={}",
build_data::get_git_dirty().map_or(DEFAULT_VALUE.to_string(), |v| v.to_string())
build_info.commit_short
);
println!("cargo:rustc-env=GIT_BRANCH={}", build_info.branch);
println!("cargo:rustc-env=GIT_DIRTY={}", build_info.dirty);
println!("cargo:rustc-env=GIT_DIRTY={}", build_info.dirty);
println!("cargo:rustc-env=RUSTC_VERSION={}", build_info.rustc);
println!("cargo:rustc-env=SOURCE_TIMESTAMP={}", build_info.timestamp);
}