本文共 2318 字,大约阅读时间需要 7 分钟。
[root@master GitTest]# cat hello.txt hello worldhello worldhello world. I am Looking------------------------------------------------------------[root@master GitTest]# cat test.rbhello_world = `cat hello.txt`puts hello_world------------------------------------------------------------[root@master GitTest]# ruby test.rbhello worldhello worldhello world. I am Looking
# test.rbauthor = `git log --pretty=format:"%an" HEAD -1`print('I am the HEAD commit author: ' + author)puts
# 在你的 Git 仓库下运行哟[root@master GitTest]# ls -altotal 32drwxr-xr-x. 4 root root 165 Aug 29 18:17 .dr-xr-x---. 22 root root 4096 Aug 29 18:17 ..-rw-r--r--. 1 root root 133 Aug 11 22:59 b.txt-rw-r--r--. 1 root root 94 Jul 29 23:19 c.txtdrwxr-xr-x. 8 root root 220 Aug 29 16:42 .git-rw-r--r--. 1 root root 50 Aug 29 15:52 hello.txt-rw-r--r--. 1 root root 36 Aug 11 22:44 looking.txt-rw-r--r--. 1 root root 19 Mar 23 17:18 myGit.txt-rw-r--r--. 1 root root 0 Mar 23 17:18 newBranch.txt-rw-r--r--. 1 root root 116 Mar 23 17:18 readme.txtdrwxr-xr-x. 2 root root 35 Jul 29 23:32 test-rw-r--r--. 1 root root 47 Mar 23 17:18 world.txt[root@master GitTest]# ruby test.rbI am the HEAD commit author: looking
此时 test.rb 脚本可以不在对应的 git 仓库目录下,但是脚本里需要指定具体仓库目录。
# test.rbauthor = `git log --pretty=format:"%an" HEAD -1`print('I am the HEAD commit author: ' + author)puts------------------------------------------------------------[root@master ~]# ruby test.rbfatal: not a git repository (or any of the parent directories): .gitI am the HEAD commit author:
# test.rbauthor = `git -C /root/GitTest log --pretty=format:"%an" HEAD -1`print('I am the HEAD commit author: ' + author)puts------------------------------------------------------------[root@master ~]# ruby test.rbI am the HEAD commit author: looking
其实用 x%() 也是可以达到相同效果的(%x 使用方法执行一段 shell 脚本并返回标准输出内容):
# test.rbauthor = `git -C /root/GitTest log --pretty=format:"%an" HEAD -1`author2 = %x(git -C /root/GitTest log --pretty=format:"%an" HEAD -1)print('I am the HEAD commit author: ' + author.to_s)putsprint('I am the HEAD commit author: ' + author2.to_s)puts------------------------------------------------------------[root@master ruby_learning]# ruby test.rbI am the HEAD commit author: lookingI am the HEAD commit author: looking
转载地址:http://rcjqi.baihongyu.com/