In a ruby script, how to ask git to open its messa

2019-08-12 08:33发布

问题:

I'm currently working on a command-line ruby gem that automates the "rebase + no-ff merging" workflow discussed at https://gist.github.com/jbenet/ee6c9ac48068889b0912. You can find the WIP code for this gem at https://github.com/gsmendoza/git_pretty_accept/tree/git_pretty_accept. The gem would do something like this:

git co master
git pull
git co pull_request
git rebase master
git co master
git merge --edit --no-ff pull_request
git push
git branch -d pull_request
git push origin:pull_request

When I try to run these git commands via ruby, git merge --edit --no-ff pull_request doesn't open the git commit message editor like I hope it would. Instead, I think git just receives a blank merge message from the editor and ruby continues with the rest of the script.

Any ideas?

回答1:

Just read your code.

`git merge --no-ff --message "#{merge_message}" #{branch}`

In Ruby, this backtick syntax redirects the STDIN and STDOUT.

Simply try this instead:

system("git merge --edit --no-ff #{branch}")

The system function just forks a process and wait for the subprocess finish, without touching STDIN/STDOUT. I tested and it works.



标签: ruby git shell