【问题标题】:Pre-commit hook modifies AssemblyInfo预提交挂钩修改 AssemblyInfo
【发布时间】:2014-07-26 21:41:37
【问题描述】:

我有一个预提交挂钩,它使用 ruby​​ gem semver2 定义内部版本号。 gem 基本上只是创建一个名为 .semver 的文件,用于存储包的版本信息。

钩子会根据一些日期/提交参数生成一个内部版本号,然后使用此信息更改 AssemblyInfo.cs,然后在提交之前添加更改后的文件。

我有几个问题:

  1. 就 .NET 而言,让钩子修改我的 AssemblyInfo 文件是否存在危险?

  2. 应该使用预提交挂钩还是其他挂钩来完成?

  3. 如何告诉这个钩子在 --amendmergerebase 提交时表现不同?

  4. 如何让这个钩子在一个分支的基础上表现不同?

  5. 您有其他解决方案来自动生成内部版本号吗?

钩子:

#!/bin/sh
#
# Append build number to semver version 
#

# check semver has been initiated
if [ -f .semver ]; then
    echo `semver`
else
    echo `semver init`
    echo `semver inc minor`
    echo `semver pre 'alpha.1'`
    echo `semver`
fi

# grab date string
date_str=`date +%y%m.%d.`

# grab commit count +1
build_num=$(git rev-list --since="00:00:00" HEAD --count)
let "build_num += 1"

# generate build & apply to semver
build=$date_str$build_num
semver meta $build

# define version strings
semver_str=$(semver)
ver_full=${semver_str#"v"}
cut_meta=$(cut -d "+" -f 1 <<<"$ver_full")
ver_small=$(cut -d "-" -f 1 <<<"$cut_meta")

# find AssemblyInfo & line number for AssemblyVersion
line=$(grep -n "AssemblyVersion(" "Properties/AssemblyInfo.cs")
line_num=$(cut -d ":" -f 1 <<<"$line")

# edit AssemblyVersion
new_line='[assembly: AssemblyVersion("'$ver_small'")]'
sed -i "${line_num}s/.*/$new_line/" "Properties/AssemblyInfo.cs"

# find line number for Semver
line=$(grep -n "Semver(" "Properties/AssemblyInfo.cs")
line_num=$(cut -d ":" -f 1 <<<"$line")

# edit Semver
new_line='[assembly: Semver("'$ver_full'")]'
sed -i "${line_num}s/.*/$new_line/" "Properties/AssemblyInfo.cs"

# add files
git add .semver
git add "Properties/AssemblyInfo.cs"

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Authenticator.Properties;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b6f9caad-fbfc-455a-8d69-f795fb9812ad")]

// This assembly uses the Semantic Versioning v2.0.0
// For more information on Semantic Versioning please see http://semver.org/
[assembly: AssemblyVersion("0.1.0")]
[assembly: Semver("0.1.0-alpha.1.0.0+1406.04.15")]

【问题讨论】:

    标签: c# .net git bash pre-commit-hook


    【解决方案1】:

    我的看法。

    1. 不,通常的做法是在编译之前在构建期间编辑 AssemblyInfo 文件
    2. 提交路由的技术很有趣,但也有一些缺点:并非所有 Git 实现都能保证钩子工作(想想 Visual Studio Online)并且您的脚本工作。
    3. 参见 #5。
    4. 参见 #5。
    5. 如果您在构建过程中进行了 AssemblyInfo 文件的编辑工作,无论如何它都可以正常工作。您的脚本需要查询 Git 的当前状态(哪个分支和提交)以选择正确的 SemVer 值。

    I blogged an example 展示了如何连接到 MSBuild 和更改 AssemblyInfo 文件,从中您可以找到许多其他示例、工具和参考资料。

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2021-09-10
      • 2019-05-06
      相关资源
      最近更新 更多