如何在每个 git 分支上运行 makefile

发布于 2024-12-11 09:42:13 字数 349 浏览 0 评论 0原文

我有一个包含多个分支的 git 存储库。存储库有一个在分支之间相同的 makefile。我想创建一个可以定期运行的 bash 脚本,该脚本从远程存储库中提取所有分支,然后为每个已更改或添加的分支运行 make 。然后,创建的可执行文件被移动到服务器上的另一个位置,并且在开始处理下一个分支之前将运行 make clean。

我运行的命令的顺序是:

make
cp executable /some/other/directory/branch_name
make clean

这是否可以用 shell 脚本来完成,如果是,我该如何实现它。如果 shell 脚本不适合此任务,我还能如何实现相同的结果?

I have a git repository with multiple branches. The repository has a makefile which is identical between branches. I'd like to create a bash script that can be run on a regular basis that pulls all branches from the remote repository and then runs make for every branch that has changed, or been added. The executable created is then moved to another location on the server, and a make clean would be run before beginning on the next branch to be processed.

The order of commands I'd be running would be

make
cp executable /some/other/directory/branch_name
make clean

Is this even possible to do with a shell script, if it is how could I go about implementing this. If a shell script is ill suited to this task, how else can I go about achieving the same result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-12-18 09:42:13

我会做这样的事情:

# fetch updates, but don't merge yet
git fetch    # origin is default

# list all branches
git for-each-ref --format='%(refname:short)' refs/heads |
while read branch; do
    # if the branch is not the same as the remote branch...
    if [ "$(git rev-parse $branch)" != "$(git rev-parse origin/$branch)" ]; then
         # only continue to the next step on successful results
         git checkout $branch &&
         git pull &&
         (make &&
         cp executable /somewhere/else;
         make clean)
         # except always make clean as long as we tried to make
         #
         # you might also consider a hard reset and clean, to guarantee things
         # are safe for the next checkout
    fi
done

首先获取的好处是您不必不必要地检查没有更改的分支;如果你的仓库很大,这可以节省时间。

显然,在处理错误方面需要做出选择;我只是选择了最简单但仍然有意义的事情。对于更复杂的处理,您可能需要执行 if make; 形式的操作。然后 ...;别的 ...; fi,例如:

# die hard on unexpected checkout/pull failures
if ! (git checkout $branch && git pull); then
    exit 1
fi
if make; then
    cp executable /somewhere/else
else
    # report a build failure?
fi
# clean up no matter what
git clean -xdf
git reset --hard

I'd do something like this:

# fetch updates, but don't merge yet
git fetch    # origin is default

# list all branches
git for-each-ref --format='%(refname:short)' refs/heads |
while read branch; do
    # if the branch is not the same as the remote branch...
    if [ "$(git rev-parse $branch)" != "$(git rev-parse origin/$branch)" ]; then
         # only continue to the next step on successful results
         git checkout $branch &&
         git pull &&
         (make &&
         cp executable /somewhere/else;
         make clean)
         # except always make clean as long as we tried to make
         #
         # you might also consider a hard reset and clean, to guarantee things
         # are safe for the next checkout
    fi
done

The nice thing about fetching first is that you don't have to unnecessarily check out branches with no changes; if your repo is big this can be a time-saver.

Obviously there are choices to be made about handling errors; I just went with the simplest thing that still made sense. For more complex handling, you might instead want to do things of the form if make; then ...; else ...; fi, e.g.:

# die hard on unexpected checkout/pull failures
if ! (git checkout $branch && git pull); then
    exit 1
fi
if make; then
    cp executable /somewhere/else
else
    # report a build failure?
fi
# clean up no matter what
git clean -xdf
git reset --hard
我一向站在原地 2024-12-18 09:42:13

嗯, git pull 确实会生成输出,可以使用 grep 来确定更新了哪些引用。之后,只需使用 for ... in ... 循环循环 grep 返回的匹配即可。

Well, git pull does generate output that could be grep'd to determine what refs were updated. After that it's a simple matter of looping over the matches returned by grep with a for ... in ... loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文