将 git commit hash 作为字符串包含到 Rust 程序中 [英] Include git commit hash as string into Rust program

查看:48
本文介绍了将 git commit hash 作为字符串包含到 Rust 程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 git 存储库中托管了一个 Rust 项目,我想让它在某个命令上打印版本.如何将版本包含在程序中?我认为构建脚本可以设置可以在编译项目本身时使用的环境变量,但它不起作用:

I host a Rust project in git repository and I want to make it print the version on some command. How can I include the version into the program? I thought that the build script could set environment variables which can be used while compiling the project itself, but it does not work:

build.rs:

use std::env;

fn get_git_hash() -> Option<String> {
    use std::process::Command;

    let branch = Command::new("git")
                         .arg("rev-parse")
                         .arg("--abbrev-ref")
                         .arg("HEAD")
                         .output();
    if let Ok(branch_output) = branch {
        let branch_string = String::from_utf8_lossy(&branch_output.stdout);
        let commit = Command::new("git")
                             .arg("rev-parse")
                             .arg("--verify")
                             .arg("HEAD")
                             .output();
        if let Ok(commit_output) = commit {
            let commit_string = String::from_utf8_lossy(&commit_output.stdout);

            return Some(format!("{}, {}",
                        branch_string.lines().next().unwrap_or(""),
                        commit_string.lines().next().unwrap_or("")))
        } else {
            panic!("Can not get git commit: {}", commit_output.unwrap_err());
        }
    } else {
        panic!("Can not get git branch: {}", branch.unwrap_err());
    }
    None
}

fn main() {
    if let Some(git) = get_git_hash() {
        env::set_var("GIT_HASH", git);
    }
}

src/main.rs:

pub const GIT_HASH: &'static str = env!("GIT_HASH");

fm main() {
    println!("Git hash: {}", GIT_HASH);
}

错误信息:

error: environment variable `GIT_HASH` not defined
  --> src/main.rs:10:25
   |
10 | pub const GIT_HASH: &'static str = env!("GIT_HASH");
   |   
                                        ^^^^^^^^^^^^^^^^

有没有办法在编译时传递这样的数据?如果不使用环境变量,如何在构建脚本和源代码之间进行通信?我只能考虑将数据写入某个文件,但我认为这对于这种情况来说太过分了.

Is there a way to pass such data at compile time? How can I communicate between the build script and the source code if not with environment variables? I can only think about writing data to some file, but I think this is overkill for this case.

推荐答案

我只能考虑将数据写入某个文件,但我认为这对于这种情况来说太过分了.

I can only think about writing data to some file, but I think this is overkill for this case.

这很不幸,因为这是唯一的方法.环境变量无法工作,因为对环境的更改无法泄漏"到其他非子进程中.

That's unfortunate, because that is the only way of doing it. Environment variables can't work because changes to the environment can't "leak" into other, non-child processes.

对于更简单的事情,您可以指示 Cargo 定义条件编译标志,但这些标志不足以传达字符串 [1].

For simpler things, you can instruct Cargo to define conditional compilation flags, but those aren't powerful enough to communicate a string [1].

从构建脚本生成代码的细节在Cargo 文档的代码生成部分.

The details of generating code from a build script is detailed in the code generation section of the Cargo documentation.

[1]:我的意思是,除非您想将散列分解为 160 个配置标志,然后在正在编译的源代码中重新组装它们,但这甚至更多矫枉过正.

[1]: I mean, unless you feel like breaking the hash into 160 config flags and then re-assembling them in the source being compiled, but that's even more overkill.

这篇关于将 git commit hash 作为字符串包含到 Rust 程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆