嵌入git的承诺散在.NET的DLL [英] Embed git commit hash in a .Net dll
问题描述
我建立一个C#应用程序,使用Git作为我的版本控制。
I'm building a C# application, using Git as my version control.
有没有办法来自动嵌入的时候我生成可执行文件的最后一次提交的哈希我的应用程序
Is there a way to automatically embed the last commit hash in the executable when I build my application?
例如,打印提交哈希来安慰看起来是这样的:
For example, printing the commit hash to console would look something like:
class PrintCommitHash
{
private String lastCommitHash = ?? // What do I put here?
static void Main(string[] args)
{
// Display the version number:
System.Console.WriteLine(lastCommitHash );
}
}
请注意,这已经在做的建立的时间,而不是运行后,因为我的部署的可执行文件将不会有访问的混帐回购协议。对于C ++
Note that this has to be done at build time, not runtime, as my deployed executable will not have the git repo accessible.
一个相关的问题都可以在这里找到 。
搜索结果
修改
搜索结果
每@ mattanja的要求,我张贴git的钩子脚本我在我的项目中使用。设置:
A related question for C++ can be found here.
EDIT
Per @mattanja's request, I'm posting the git hook script I use in my projects. The setup:
- 的钩子是Linux的shell脚本,它被放在 path_to_project\.git\hooks
- 如果您使用的是 msysgit ,在挂钩的文件夹已经包含了一些示例脚本。为了让git的打电话给他们,请从脚本名称的.sample扩展名。
- 钩子脚本的名称调用它们的事件相匹配。就我而言,我修改的后提交的和的后合并的
- 我的的AssemblyInfo.cs 的文件是直接在项目路径下(同一级别的的.git 的文件夹)。它包含23行,我使用git来生成24位。
- The hooks are linux shell scripts, which are placed under: path_to_project\.git\hooks
- If you are using msysgit, the hooks folder already contains some sample scripts. In order to make git call them, remove the '.sample' extension from the script name.
- The names of the hook scripts match the event that invokes them. In my case, I modified post-commit and post-merge.
- My AssemblyInfo.cs file is directly under the project path (same level as the .git folder). It contains 23 lines, and I use git to generate the 24th.
由于我的Linux的炮轰有点生疏,脚本简单的读取的AssemblyInfo.cs 的首23行到一个临时文件,回声Git的哈希到最后一行,重命名文件回的的AssemblyInfo.cs 的。我敢肯定有这样做的更好的方法:
As my linux-shelling a bit rusty, the script simply reads the first 23-lines of AssemblyInfo.cs to a temporary file, echos the git hash to the last line, and renames the file back to AssemblyInfo.cs. I'm sure there are better ways of doing this:
#!/bin/sh
cmt=$(git rev-list --max-count=1 HEAD)
head -23 AssemblyInfo.cs > AssemblyInfo.cs.tmp
echo [assembly: AssemblyFileVersion\(\"$cmt\"\)] >> AssemblyInfo.cs.tmp
mv AssemblyInfo.cs.tmp AssemblyInfo.cs
希望这有助于
Hope this helps.
推荐答案
我们使用Git标签来跟踪版本。
We use tags in git to track versions.
git tag -a v13.3.1 -m "version 13.3.1"
You can get the version with hash from git via:
git describe --long
我们的构建过程中把git的哈希AssemblyInfo.cs文件中的AssemblyInformationalVersion属性:
Our build process puts the git hash in the AssemblyInformationalVersion attribute of the AssemblyInfo.cs file:
[assembly: AssemblyInformationalVersion("13.3.1.74-g5224f3b")]
一旦编译,您可以查看Windows资源管理器的版本:
Once you compile, you can view the version from windows explorer:
您还可以得到它通过编程方式:
You can also get it programmatically via:
var build = ((AssemblyInformationalVersionAttribute)Assembly
.GetAssembly(typeof(YOURTYPE))
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)[0])
.InformationalVersion;
其中YOURTYPE是具有AssemblyInformationalVersion属性大会的任何类型。
where YOURTYPE is any Type in the Assembly that has the AssemblyInformationalVersion attribute.
这篇关于嵌入git的承诺散在.NET的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!