git:在发布/标签中获取*新*贡献者作者列表

问题描述

我正在准备发布的变更日志并做一些统计。

列出以前版本的贡献者非常容易:

git shortlog -s -e -n TAG..

审稿人:

git log TAG.. | grep -Ei '(reviewed|acked)-by:' |sed 's/.*by: //' | sort | uniq -c | sort -n -r

提交者:

git shortlog -s -e -n -c TAG..

但是如何列出自 TAG 以来的新贡献者(作者)(例如那些在 TAG 之前尚未提交的人)?

解决方法

假设您可以使用 bash,至少有两种方法:

#!/bin/bash

set -e

OLD_COMMIT="$1"
NEW_COMMIT="$2"
if test -z "$OLD_COMMIT" || test -z "$NEW_COMMIT"; then
    echo 'fatal: must provide OLD_COMMIT and NEW_COMMIT commits' >&2
    exit 1
fi
shift 2

第一个,使用在 bash 4 中引入的声明性数组(以命令方式模拟右外连接):

declare -A OLD_AUTHORS
while read -r OLD_AUTHOR; do
    OLD_AUTHORS["$OLD_AUTHOR"]=1
done < <(git log --pretty=format:%aN "$OLD_COMMIT")

declare -A NEW_AUTHORS
while read -r NEW_AUTHOR; do
    if test -z ${OLD_AUTHORS["$NEW_AUTHOR"]}; then
        NEW_AUTHORS["$NEW_AUTHOR"]=1
    fi
done < <(git log --pretty=format:%aN "$OLD_COMMIT"~1.."$NEW_COMMIT")

for NEW_AUTHOR in "${!NEW_AUTHORS[@]}"; do
    echo "$NEW_AUTHOR"
done | sort

或者,第二种,使用管道和更具声明性的方式:

diff \
    <(git log --pretty=format:%aN "$OLD_COMMIT" | sort | uniq) \
    <(git log --pretty=format:%aN "$OLD_COMMIT"~1.."$NEW_COMMIT" | sort | uniq) \
    | grep '^> ' \
    | cut -c 3-

以上两种解决方案都可以处理以下历史记录(git log --pretty=format:'%h %d% aN'):

c3d766e  (tag: v2) Keith
cffddc6  New John
ee3cc52  Dan
c307f13  (tag: v1) New John
ae3c4a3  New John
9ed948e  Old John
7eb548a  Old John

像这样 (show-new-authors.sh v1 v2):

Dan
Keith

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...