为了进行代码审查,我想要:
- 挑选特定的提交
- 创建一个包含它们的新分支,并将其
- 推送到远程仓库
这样,我就可以把分支链接给同事进行审查。
我想要创建一个shell脚本,只需输入简单命令即可。
git-review <trackingID>
它的输出如下所示。
Branch <currentgitusername>_<trackingID> created and pushed to remote.
我编写了一个脚本,执行上述步骤。
#!/bin/bash
if [ -z $1 ]; then
echo "Rationale: Cherry pick all commits in master, matching the tracking ID and create a new branch.";
echo "";
echo "Usage: $0 traackingID";
echo "";
exit 1;
fi
#If $1 doesn't match a AGW-<number> pattern, thrown an error
#Best of luck if you did not add add tracking ID in commits.
user="$(id -u -n)" > /dev/null
echo "You are - $user"
branchname=$user"_"$1"_review"
echo "Creating branch - $branchname"
git checkout -b $branchname > /dev/null
git log master --pretty=%H --grep="$1" | while read rev
do
echo $rev
# git cherry-pick $rev > /dev/null
done
#git push &> /dev/null
echo "Created branch, cherry picked, pushed to remote. Now switching back to master. Bye."
git checkout master
但在几个地方卡住了。
- I want to validate the trackingID format. It should be
AGW-<somenumber>
Looks like the cherry-picks have merge issues.
myname@mymachine ~/myproj $ ../git-review.sh AGW-1234 You are - myname Creating branch - myname_AGW-1234_review Switched to a new branch 'myname_AGW-1234_review' 2dfafa89e10062e7cfccb1d7a947ebb848c40cc6 The previous cherry-pick is now empty, possibly due to conflict resolution. If you wish to commit it anyway, use: git commit --allow-empty Otherwise, please use 'git reset' 1e295000bc3d80880518c9cac5e34ef3b28fc29e error: Your local changes to the following files would be overwritten by merge: rest-service/src/main/java/package/HealthCheckDAOImpl.java Please, commit your changes or stash them before you can merge.
git log
将最新的提交放在输出的顶部。此外,有一个名为git review
的工具可用于与 Gerrit 交互,因此您可能需要选择不同的名称以避免混淆。 - Phil Millergit rev-list
而不是git log --pretty=%H
。它接受与git log
相同的选项,但直接打印提交的SHA1。 - Sascha Wolf