Py学习  »  Git

如何将除前两个参数外的所有bash参数设置为git alias

Joao Albuquerque • 4 年前 • 564 次点击  

我想创建一个git别名,它将使用我定义的模板创建一个提交消息。

“[$firstVariable]$secondVariable:$thirdVariable”

  1. 分支机构编号 ,
  2. 提交类型
  3. 最后一个论点是 提交消息的主题

示例:

“[1000]专长:添加新的销售控制器”

这是我的bash函数 .gitconfig 文件:

[alias]
    c = "!f() { branchNumber=${1}; type=${2}; shift; shift; subject=${*}; git commit -m '['"$branchNumber"'] '"$type"': '"$subject"' '; }; f"

如果我执行这个命令,我会得到:

命令: $ git c 1000 feat add new sales controller

返回:

错误:pathspec'sales'与git已知的任何文件都不匹配。


观察:

  • 如果我在 $subject

新销售总监

错误:pathspec'controller'与git已知的任何文件都不匹配。

  • 如果我在 $主题 别名正在工作
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56753
 
564 次点击  
文章 [ 1 ]  |  最新文章 4 年前
torek
Reply   •   1 楼
torek    4 年前

Git在解析 .gitconfig 行。因此,当你写道:

"!f() { branchNumber=${1}; type=${2}; shift; shift; subject=${*}; git commit -m '['"$branchNumber"'] '"$type"': '"$subject"' '; }; f"

什么 看到的是:

!f() { branchNumber=${1}; type=${2}; shift; shift; subject=${*}; git commit -m '['$branchNumber'] '$type': '$subject' '; }; f

$subject 展开后,它变成单独的单词:

git c 1000 feat add new sales controller

git
commit
-m
[1000] feat: add
new
sales
controller 

(我把每个“词”单独画在一条线上)。(单词后面还有一个空白 controller ,因为这是由单引号保护的。)如果您愿意,另一种用于StackOverflow过帐的方法可能是:

git commit -m [1000] feat: add new sales

也就是说,命令本身将每一个都视为一个单独的“单词”:因此提交日志消息是 [1000]专长:添加 ; , 出售 控制器 被视为 路径规范 论据。

一个简单的解决方法是用反斜杠双引号替换要保留的每个双引号:

    c = "!f() { branchNumber=${1}; type=${2}; shift; shift; subject=${*}; git commit -m '['\"$branchNumber\"'] '\"$type\"': '\"$subject\"' '; }; f"

为了使它更易读,请注意双引号足以 参数(这里的不同地方不需要大括号):

    c = "!f() { branchNumber=$1; type=$2; shift 2; subject=$*; git commit -m \"[$branchNumber] ${type}: $subject\"; }; f"