git别名内的AWK语句 [英] AWK statement inside a git alias

查看:285
本文介绍了git别名内的AWK语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建git别名,以特定格式打印日志中的所有请求请求。但是我在使用AWK删除双精度空格时遇到问题。

I'm trying to create a git alias to print all pull requests from the log, in a specific format. However I'm having issues with AWK to remove double spaces.

这是使用以下命令从git log中输出的内容: git log --merges --grep = pull request --pretty = format: [%h]%s%b [SHA] .. HEAD

This is the output from git log using this command: git log --merges --grep="pull request" --pretty=format:"[%h] %s %b" [SHA]..HEAD

[5c420cb]合并来自repo_name / branch_name提取请求标题的提取请求#001

[5c420cb] Merge pull request #001 from repo_name/branch_name Pull request title

使用按照AWK命令,我可以根据需要打印它: awk'{$ 2 = $ 3 = $ 4 = $ 6 = $ 7 =; gsub(/ + /,,$ 0); $ 2 = $ 2:;打印}'

Piping with the following AWK command I was able to print it as I wanted: awk '{$2=$3=$4=$6=$7=""; gsub(/ +/," ",$0); $2=$2 ":"; print}'


[5c420cb]#001:提取请求标题

[5c420cb] #001: Pull request title

但是,将git别名中的所有内容粘贴在一起

However, glueing everything in the git alias

[alias]
    release = !"git log --merges --grep=\"pull request\" --pretty=format:\"[%h] %s %b\" $1..HEAD | awk '{$2=$3=$4=$6=$7=\\\"\\\";gsub(/ +/,\\\" \\\",$0);$2=$2 \\\":\\\";print}'"

AWK打印出[SHA]不是文件:

AWK prints that the [SHA] is not a file:

$ git release SHA
awk: can't open file SHA
 source line number 1

怎么了?我愿意使用其他工具,例如 sed cut 或别名中的bash函数。

What could be wrong? I'm open to use other tools like sed, cut or even a bash function inside the alias.

我在OS X Mavericks下。

I'm under OS X Mavericks.

推荐答案

当git调用您的别名时

When git calls your alias it sticks your arguments on the end of the command and runs it.

使用您的别名(手动将其引为引号,因此忽略引号错误):

With your alias that turns in to (manually dequoted so ignore quoting errors):

git log --merges --grep="pull request" --pretty=format:"[%h] %s %b" $1..HEAD | awk '{$2=$3=$4=$6=$7=\\"\\";gsub(/ +/,\\" \\",$0);$2=$2 \\":\\";print}' SHA

此时 $ 1 扩展为空,git看到 .. HEAD (这完全合理)。

At which point $1 expands to nothing, git sees ..HEAD (which is perfectly reasonable).

awk 看到 SHA 的参数不存在并触发错误(并导致错误被忽略)

awk sees an argument of SHA which doesn't exist and triggers the error (and causes it to ignore stdin).

您需要将管道包装在 shell脚本中。

You need to wrap the pipeline in a "shell script".

release = "!bash -c 'git log --merges --grep=\"pull request\" --pretty=format:\"[%h] %s %b\" $1..HEAD | awk \"{\\$2=\\$3=\\$4=\\$6=\\$7=\\\"\\\";gsub(/ +/,\\\" \\\",\\$0);\\$2=\\$2 \\\":\\\";print}\"' -"

可以用不太可怕的引号来做到这一点,但这是对我有用的格式,所以我坚持使用它。

It might be possible to do that in a way that has less horrible quoting but that's the format that's worked for me so I've stuck with it.

-最后的参数是将 $ 0 参数填充为这样的外壳,以便位置参数按照人们期望的方式工作。

The - argument there at the end is to fill the $0 argument to that shell so positional arguments work the way people expect them to.

这篇关于git别名内的AWK语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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