Ruby Kernel.system 如何读取 Git 的 local config?

nightire · October 12, 2013 · Last by jamchange replied at October 12, 2013 · 3485 hits

比方说一个 rake task 里有一句:

system "git add -A"
system "git commit -m\"#{message}\""
system "git push origin master"

执行是没有问题的,但是该 commit 使用的是我 Git global config 里定义的 user.name 和 user.email。有时候在某项目下重新定义了 local config 的 user.name 和 user.email,那么上述命令该怎样使用正确的 local config?

=====补充=====

刚考虑了一下,我可以把命令改写一下:

system "git add -A"
system "git commit -m\"#{message}\" --author=\"new_name <[email protected]>\""
system "git push origin master"

不过还会有两个问题:

  1. 作者的名字是改了,但是提交者还是 global config 里的那个,可惜 global config 不能改这是个前提。
  2. Hard coded 很不爽,难不成再写一个脚本去读 .git/config 里配置的 user.name 和 user.email?有没有更简单的办法?

是不是 rake task 里设置了什么环境变量,使得 git 命令没去读 .git/config ?

original commit log

› git log
commit c5276cf2d5cca3f54db3bb509b0560089e4a4087
Author: Saito <[email protected]>
Date:   Sat Oct 12 02:26:00 2013 +0800

    add fix

added git local config file and commited to webapp

› git log
commit 3b22977874908f576670ae085fe7bd7c144398d9
Author: otias <[email protected]>
Date:   Sat Oct 12 02:27:59 2013 +0800

    fix config.yml

commit c5276cf2d5cca3f54db3bb509b0560089e4a4087
Author: Saito <[email protected]>
Date:   Sat Oct 12 02:26:00 2013 +0800

    add fix

use system in pry

[1] pry(main)> system"git add -A"
=> true
[2] pry(main)> system"git commit -m\"hello world\""
[master 97deca9] hello world
 1 file changed, 1 deletion(-)
=> true
› git log
commit 97deca90ce17ae9340bf8c9733897ce863e30b75
Author: otias <[email protected]>
Date:   Sat Oct 12 02:29:08 2013 +0800

    hello world

commit 3b22977874908f576670ae085fe7bd7c144398d9
Author: otias <[email protected]>
Date:   Sat Oct 12 02:27:59 2013 +0800

    fix config.yml

commit c5276cf2d5cca3f54db3bb509b0560089e4a4087
Author: Saito <[email protected]>
Date:   Sat Oct 12 02:26:00 2013 +0800

    add fix
Unknow user #3 October 12, 2013

gem Grit

You need to Sign in before reply, if you don't have an account, please Sign up first.