如何在Rake任务中执行git提交/推送

4

在尝试从 Ruby rake 任务内执行一些 git 命令时,我遇到了以下问题(我已经尝试过不带 bash --login -c,结果相同)。

tmpid='TueJan26014125UTC2010'
cmd=["git add .",
  "git commit -a -m \'Migrate nanoc3 co output to gh-pages #{tmpid}\'",
  "git push --force origin gh-pages:gh-pages"
]

FileUtils.chdir @gh_pages_repo_path.to_s do
  puts `pwd`
  cmd.each do |cmdi|
      puts "bash --login -c '#{cmdi}'"
      res=Kernel.send(:`, "bash --login -c '#{cmdi}'")
      puts res
    end
end

这个输出显示了当前的工作目录,如果git push失败了

/home/mv/Documents/Workspaces/scar/ruby/scar/gh-pages
bash --login -c 'git add .'

bash --login -c 'git commit -a -m 'Migrate nanoc3 co output to gh-pages TueJan26014125UTC2010''
# On branch master
nothing to commit (working directory clean)
bash --login -c 'git push --force origin gh-pages:gh-pages'
error: src refspec gh-pages does not match any.
error: failed to push some refs to 'git@github.com:hedgehog/scar.git'
To prevent you from losing history, non-fast-forward updates were rejected.
Merge the remote changes before pushing again.
See 'non-fast forward' section of 'git push --help' for details.

如果我在bash shell(konsole)中立即运行相同的命令,则一切都会顺利进行:
$ pushd /home/mv/Documents/Workspaces/scar/ruby/scar/gh-pages
/usr/src ~
$ git add .
$ git commit -a -m 'Migrate nanoc3 co output to gh-pages TueJan26014125UTC2010'
[gh-pages 14cbe34] Migrate nanoc3 co output to gh-pages TueJan26014125UTC2010
 20 files changed, 100 insertions(+), 0 deletions(-)
$ git push --force origin gh-pages:gh-pages
Counting objects: 73, done.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (38/38), 11.32 KiB, done.
Total 38 (delta 16), reused 0 (delta 0)
To git@github.com:hedgehog/scar.git
   e0f0370..14cbe34  gh-pages -> gh-pages

感谢任何见解。
1个回答

1

你可能想尝试使用一个库来处理 git 调用。一个例子是 ruby-git


不幸的是,我在使用ruby-git时也遇到了这个错误,这促使我使用shell命令。我还没有时间复现这个问题,但是通过运行git branch -agit remote -v以及git checkout --track -b gh-pages origin/gh-pages来解决了这个问题,然后再进行git add。然后上面的脚本就可以正常工作了。再次强调,我还没有复现这个问题...但我认为这可能与Github建议的设置gh-pages子模块的方式有关,即rm .git/index等。 - Hedgehog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接