如何创建bash脚本以自动子树拆分存储库的一部分并将其推送到另一个只读存储库

时间:2016-12-09 17:19:47

标签: linux git bash ssh

正如我在对象中写的那样,我有一些我写的组件的一些部分,必须在其他项目中使用。

我不想使用所有组件,只需要使用" common"部分。

所以,我创造了一个" Common"文件夹和其中我放置了我需要在项目中共享的所有常用文件。

现在,我需要自动创建此文件夹的subtree split并将其内容推送到BitBucket上的只读存储库。

仅供参考:为了处理身份验证部分,我创建了一个新的ssh密钥对,并将私钥放在一个环境变量中:bash脚本使用此变量创建要传递的文件到ssh-add。公钥是在我的BitBucket帐户中设置的。

问题是该脚本似乎没有将拆分的代码推送到远程只读存储库。虽然手动执行所有命令都运行良好,但我无法以自动方式完成这项工作。

更多,因为我从未使用bash来编写脚本,所以我确信它中存在很多错误,并且可以对其进行深度重构以使其更好。

遵循脚本代码:

#!/usr/bin/env bash

cd ~/src/bitbucket.org/Aerendir/component-remotes/
echo "> Current working directory: $PWD"

echo '> Creating the key file'
printf %q -v "$ssh_component_remotes_common_key" > bitbucket_key
chmod 400 bitbucket_key
ls ~/src/bitbucket.org/Aerendir/component-remotes/

echo '> Adding the identity for bitbucket.org to config'
cat <<EOT >> ~/.ssh/config
Host bitbucket.org
    IdentityFile ~/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key
EOT
cat ~/.ssh/config

echo '> Adding the key to SSH agent'
eval "$(ssh-agent -s)"
/usr/bin/expect <<EOF
    spawn ssh-add ${HOME}/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key
    expect "Enter passphrase for key '/home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key':"
    send "$ssh_component_remotes_common_pass";
    interact
EOF

echo -e '\n > Creating the subtree repository'
mkdir _component-remotes-common
cd _component-remotes-common
git init --bare
git remote add origin git@bitbucket.org:Aerendir/component-remotes-common.git
/usr/bin/expect <<EOF
    spawn git remote show origin
    expect "Enter passphrase for key '/home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key':"
    send "$ssh_component_remotes_common_pass";
    interact
EOF

echo -e '\n > Splitting in the subtree repository'
cd ../
echo "> Current working directory: $PWD"
git subtree split --prefix=src/Remotes/Common -b split
git push _component-remotes-common split:master

echo '> Pushing to the remote repo'
cd _component-remotes-common
echo "> Current working directory: $PWD"
/usr/bin/expect <<EOF
    spawn git push origin master
    expect "Enter passphrase for key '/home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key':"
    send "$ssh_component_remotes_common_pass";
    interact
EOF

echo "$expect_out(0, string)"

这是输出:

> Current working directory: /home/rof/src/bitbucket.org/Aerendir/component-remotes
> Creating the key file
bin            composer.json    docs             phpunit.xml.dist  src
bitbucket_key  composer.lock    log              readmegen.yml     tmp
CHANGELOG.md   CONTRIBUTING.md  phpdoc.xml.dist  README.md
> Adding the identity for bitbucket.org to config
UserKnownHostsFile=/dev/null
StrictHostKeyChecking=no
ServerAliveInterval 3
ServerAliveCountMax 600
Host bitbucket.org
IdentityFile ~/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key
> Adding the key to SSH agent
Agent pid 5919
spawn ssh-add /home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key
Enter passphrase for /home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key: 
> Creating the subtree repository
Initialized empty Git repository in /home/rof/src/bitbucket.org/Aerendir/component-remotes/_component-remotes-common/
spawn git remote show origin
Warning: Permanently added 'bitbucket.org,104.192.143.2' (RSA) to the list of known hosts.
Enter passphrase for key '/home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key': 
> Splitting in the subtree repository
> Current working directory: /home/rof/src/bitbucket.org/Aerendir/component-remotes
Created branch 'split'
490b1f471932a308075c568f21c36bab5f102818
Counting objects: 8, done.
Delta compression using up to 36 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), 667 bytes | 0 bytes/s, done.
Total 8 (delta 0), reused 5 (delta 0)
To _component-remotes-common
* [new branch]      split -> master
> Pushing to the remote repo
> Current working directory: /home/rof/src/bitbucket.org/Aerendir/component-remotes/_component-remotes-common
spawn git push origin master
Warning: Permanently added 'bitbucket.org,104.192.143.3' (RSA) to the list of known hosts.
Enter passphrase for key '/home/rof/src/bitbucket.org/Aerendir/component-remotes/bitbucket_key': (0, string)

push似乎工作正常:在那一点上,我没有收到任何其他输出。显然远程仓库仍然是空的,但我不知道哪个是问题,因为我没有收到任何错误作为输出。

我大约5个小时就开始使用这个脚本了,我非常厌倦了:非常感谢任何帮助使我的工作变得更好!三江源...

0 个答案:

没有答案