社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

修改变量时出现bash脚本错误的git别名

Joshua Soileau • 5 年前 • 1688 次点击  

我的Git分支名称:

feature/ACD-1706_new-api-call
如果我在控制台中运行此脚本,它将正常工作:
bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch##*/} && no_underscore=${no_feature%%_*} && history -s "git commit -m \"$no_underscore | " && echo "Press UP to start commit command:" && echo "" && read -e -p "> " && eval "$REPLY"'
它会将我放入一个提示中,帮助我根据功能分支名称自动生成提交消息。
> bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch##*/} && no_underscore=${no_feature%%_*} && history -s "git commit -m \"$no_underscore | " && echo "Press UP to start commit command:" && echo "" && read -e -p "> " && eval "$REPLY"'
Press UP to start commit command:

> 

但是如果我把它变成git别名,脚本就会出错。

auto-message = !bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch##*/} && no_underscore=${no_feature%%_*} && history -s "git commit -m \"$no_underscore | " && echo "Press UP to start commit command:" && echo "" && read -e -p "> " && eval "$REPLY"'
> git auto-message
bash -l -c 'current_branch=$(git rev-parse --abbrev-ref HEAD) && no_feature=${current_branch: -c: line 0: unexpected EOF while looking for matching `''
bash -l -c 'current_branch=$(git rev-parse --abbrev-ref HEAD) && no_feature=${current_branch: -c: line 1: syntax error: unexpected end of file
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40694
 
1688 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Ralf
Reply   •   1 楼
Ralf    6 年前

一个不同的解决方案。这不使用别名,而是使用脚本。

在路径中创建一个名为 git-auto-message (没有分机!)内容如下:

#!/bin/bash

#current_branch="feature/ACD-1706_new-api-call"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
no_feature=${current_branch##*/}
no_underscore=${no_feature%%_*}

read -p "Complete commit message (empty cancels) \"$no_underscore | " msg
echo
# remove leading whitespaces
msg="${msg#"${msg%%[![:space:]]*}"}"
# remove trailing whitespaces
msg="${msg%"${msg##*[![:space:]]}"}"
if [ -n "$msg" ]; then
    git commit -m "$no_underscore | $msg"
fi

使脚本可执行。

然后你可以打电话 git auto-message . git意识到,它没有一个名为 auto-message 而且这个名字没有别名。所以它在路径中搜索一个名为 Git自动消息 . 所以脚本被执行。

虽然别名对于较小的事情很好,但是这个特性允许创建相当复杂的功能。


更新的脚本,正如我所错过的,你想添加额外的文本。

joanis
Reply   •   2 楼
joanis    6 年前

这当然是一个必须在这里或那里逃离正确角色的情况。

我建议一个更简单的解决方案,也是由@user7369280建议的,同时我发布了这个答案的原始版本:create file git-auto-message 你路上的某个地方有这些内容:

#!/bin/bash

set -o errexit

current_branch="$(git rev-parse --abbrev-ref HEAD)"
no_feature=${current_branch##*/}
no_underscore=${no_feature%%_*}
history -s "git commit -m \"$no_underscore | "
echo "Press UP to start commit command:"
echo ""
read -e -p "> "
eval "$REPLY"

使此文件可执行

chmod 755 /path/to/git-auto-message

然后

git auto-message

将以与原始命令行相同的行为运行该脚本。

我刚刚对您的实际别名进行了故障排除,发现git别名语法不喜欢使用以下符号: # , > , | .

此变体可以工作,但会丢失一些功能:

  • 它不会删除分支名称开头的路径。我还没有找到一种方法来搜索 / 使用git别名接受的bash语法。
  • 它使用 - 而不是 γ 将分支名称与提交的其余部分分开。
  • 它使用 $ 而不是 > 为了提示。

修订别名:

auto-message = !bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch} && no_underscore=${no_feature%%_*} && history -s "git commit -m $no_underscore - " && echo "Press UP to start commit command:" && echo "" && read -e -p "_ " && eval "$REPLY"'
chepner
Reply   •   3 楼
chepner    6 年前

使用 git commit -e 如果您只想在提交消息中添加一些初始文本,则选项会简单得多。它还允许用户使用他们选择的文本编辑器,而不是强制 readline 在他们身上。

git commit -em "$(git rev-parse --abbrev-ref HEAD | sed 's|.*/||; s|_.*$||;' )"

(一个缺点是,它以包含 -m 参数,因此不保存就退出编辑器不足以中止提交。为此,必须显式保存空文件。)

因此,这更容易正确引用:

auto-message = !git commit -em "$(git rev-parse --abbrev-ref HEAD | sed 's|.*/||; s|_.*$||;')"