1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::print_stdout)]

use std::fmt::Display;

shadow_rs::shadow!(build);

#[derive(Clone, Debug, PartialEq)]
pub struct BuildInfo {
    pub branch: &'static str,
    pub commit: &'static str,
    pub commit_short: &'static str,
    pub clean: bool,
    pub source_time: &'static str,
    pub build_time: &'static str,
    pub rustc: &'static str,
    pub target: &'static str,
    pub version: &'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!("clean: {}", self.clean),
                format!("version: {}", self.version),
            ]
            .join("\n")
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "codec",
    derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)
)]
pub struct OwnedBuildInfo {
    pub branch: String,
    pub commit: String,
    pub commit_short: String,
    pub clean: bool,
    pub source_time: String,
    pub build_time: String,
    pub rustc: String,
    pub target: String,
    pub version: String,
}

impl From<BuildInfo> for OwnedBuildInfo {
    fn from(info: BuildInfo) -> Self {
        OwnedBuildInfo {
            branch: info.branch.to_string(),
            commit: info.commit.to_string(),
            commit_short: info.commit_short.to_string(),
            clean: info.clean,
            source_time: info.source_time.to_string(),
            build_time: info.build_time.to_string(),
            rustc: info.rustc.to_string(),
            target: info.target.to_string(),
            version: info.version.to_string(),
        }
    }
}

impl Display for OwnedBuildInfo {
    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!("clean: {}", self.clean),
                format!("version: {}", self.version),
            ]
            .join("\n")
        )
    }
}

pub const fn build_info() -> BuildInfo {
    BuildInfo {
        branch: build::BRANCH,
        commit: build::COMMIT_HASH,
        commit_short: build::SHORT_COMMIT,
        clean: build::GIT_CLEAN,
        source_time: env!("SOURCE_TIMESTAMP"),
        build_time: env!("BUILD_TIMESTAMP"),
        rustc: build::RUST_VERSION,
        target: build::BUILD_TARGET,
        version: build::PKG_VERSION,
    }
}

const BUILD_INFO: BuildInfo = build_info();

pub const fn version() -> &'static str {
    const_format::formatcp!(
        "\nbranch: {}\ncommit: {}\nclean: {}\nversion: {}",
        BUILD_INFO.branch,
        BUILD_INFO.commit,
        BUILD_INFO.clean,
        BUILD_INFO.version,
    )
}

pub const fn short_version() -> &'static str {
    const BRANCH: &str = BUILD_INFO.branch;
    const COMMIT_ID: &str = BUILD_INFO.commit_short;

    // When git checkout to a commit, the branch is empty.
    #[allow(clippy::const_is_empty)]
    if !BRANCH.is_empty() {
        const_format::formatcp!("{}-{}", BRANCH, COMMIT_ID)
    } else {
        COMMIT_ID
    }
}