引用github中的当前分支readme.md

在我的github回购的readme.md文件中,我有一个Travis-CI徽章。 我使用以下链接:

joegattnet/joegattnet_v3.png 

显而易见的问题是分支是硬编码的。 是否有可能使用某种variables,以便分支是当前正在查看的分支?

从来没听说过。
GitHub支持确认(通过OP Joe Joe的评论 )

唯一的方法就是通过我自己的服务传递链接,使用github的http referrer头来确定哪个分支被引用,然后从Travis CI中获取合适的图像

我宁愿为每个分支制作一个Travis-CI徽章,供读者在看README.md时select或考虑适当的。


2016年更新(3年后):虽然GitHub没有任何变化, 但是fedorqui在Andrie报告的“ 在Github上获取特拉维斯盾来反映选定分支状态 ”中提到的解决方法。
只需显示所有分支及其各自的TravisCI徽章。

如果你只有两到三个分支,那就足够了。

我用一个git pre-commit钩子解决了这个问题,它用当前分支重写了README.md中的Travis行。 下面是一个使用和预先提交(Python)代码示例(针对所问的问题)。

用法

 dandye$ git checkout -b feature123 origin/master Branch feature123 set up to track remote branch master from origin. Switched to a new branch 'feature123' dandye$ echo "* Feature123" >> README.md dandye$ git add README.md dandye$ git commit -m "Added Feature123" Starting pre-commit hook... Replacing: [![Build Status](joegattnet/joegattnet_v3.png)][travis] with: [![Build Status](joegattnet/joegattnet_v31.png)][travis] pre-commit hook complete. [feature123 54897ee] Added Feature123 1 file changed, 2 insertions(+), 1 deletion(-) dandye$ cat README.md |grep "Build Status" [![Build Status](joegattnet/joegattnet_v31.png)][travis] dandye$ 

用于预先提交代码的Python代码

 dandye$ cat .git/hooks/pre-commit 
 #!/usr/bin/python """ Referencing current branch in github readme.md[1] This pre-commit hook[2] updates the README.md file's Travis badge with the current branch. Gist at[4]. [1] http://stackoverflow.com/questions/18673694/referencing-current-branch-in-github-readme-md [2] http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks [3] https://docs.travis-ci.com/user/status-images/ [4] https://gist.github.com/dandye/dfe0870a6a1151c89ed9 """ import subprocess # Hard-Coded for your repo (ToDo: get from remote?) GITHUB_USER="joegattnet" REPO="joegattnet_v3" print "Starting pre-commit hook..." BRANCH=subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip() # String with hard-coded values # See Embedding Status Images[3] for alternate formats (private repos, svg, etc) # [![Build Status](https://travis-ci.org/ # joegattnet/joegattnet_v3.png? # branch=staging)][travis] # Output String with Variable substitution travis="[![Build Status](https://travis-ci.org/" \ "{GITHUB_USER}/{REPO}.png?" \ "branch={BRANCH})][travis]\n".format(BRANCH=BRANCH, GITHUB_USER=GITHUB_USER, REPO=REPO) sentinal_str="[![Build Status]" readmelines=open("README.md").readlines() with open("README.md", "w") as fh: for aline in readmelines: if sentinal_str in aline and travis != aline: print "Replacing:\n\t{aline}\nwith:\n\t{travis}".format( aline=aline, travis=travis) fh.write(travis) else: fh.write(aline) subprocess.check_output(["git", "add", "README.md" ]) print "pre-commit hook complete." dandye$