如何可靠地运行 bash 脚本的特定 git 分支版本?

问题描述 投票:0回答:1

我有一个脚本

loop.sh
每隔几个小时运行另一个脚本
task.sh
。它们都是同一个 git 存储库的一部分。

目前,我在一个单独的

screen
实例中运行(即它自己的
bash
进程)

(main) user@computer:repo-dir > ./loop.sh

在哪里

loop.sh
看起来像:

for i in 1 2 3 4 5 6; do
   ./task.sh
   sleep 3h
done

我最近在 VS Code 中对

task.sh
进行了修改,它们被程序自动保存了。这使得
loop.sh
拾取损坏版本的
task.sh
并失败。

如果我要从

main
git 分支切换到一个新的
new-feature
分支以对
task.sh
进行修改,我如何可靠地同时拥有
loop.sh
和它对
task.sh
的调用使用
main
文件的版本?

  1. 一些不太自信的消息人士告诉我,只是跑步

    loop.sh
    main
    task.sh
    也会自动
    main
    ?

  2. 一些消息来源非常确信在

    git checkout main
    中包含
    loop.sh
    是错误的。这是真的吗?为什么?

  3. 我见过一个

git show main:task.sh | bash 

这可以用作替代品吗?原来的

./task.sh
接受参数,我有时会处理脚本管道的输出。

bash git branching-and-merging
1个回答
0
投票

我如何可靠地让 loop.sh 及其对 task.sh 的调用都使用文件的主要版本?

让脚本在单独的worktree中运行。这样你就可以在

new-feature
或你想要在主工作树中的任何分支上工作,而不会打扰你的脚本。

cd $HOME/repo-dir
git worktree add -b main.mirror ../scripts-worktree main

并将

loop.sh
更新为

cd "$HOME/scripts-worktree"
git switch main.mirror

for i in 1 2 3 4 5 6; do
   git merge main
   ./task.sh
   sleep 3h
done

(尽管正如其他人提到的那样,从 cron 触发它是更好的选择)。

© www.soinside.com 2019 - 2024. All rights reserved.