Referencing current branch in github readme.md

2019-01-21 12:13发布

问题:

In my github repo's readme.md file I have a Travis-CI badge. I use the following link:

https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=staging

The obvious problem is that the branch is hardcoded. Is it possible to use some sort of variable so that the branch is the one currently being viewed?

回答1:

Not that I know of.
GitHub support confirms (through OP Joe Gatt 's comment)

The only way to do this would be to pass the link through my own service which would use the github's http referrer header to determine which branch is being referenced and then fetch the appropriate image from Travis CI

I would rather make one Travis-CI badge per branch, for the reader to choose or consider the appropriate when seeing the README.md.


Update 2016 (3 years later): while nothing has changed on the GitHub side, fedorqui reports in the workaround mentioned in "Get Travis Shield on Github to Reflect Selected Branch Status" by Andrie.
Simply display all the branches and their respective TravisCI badges.

If you have only two or three branches, that could be enough.



回答2:

I worked around this issue with a git pre-commit hook that re-writes the Travis line in the README.md with the current branch. An example of usage and pre-commit (Python) code (for the question as asked) are below.

Usage

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](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=master)][travis]

with:
    [![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=feature123)][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](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=feature123)][travis]
dandye$ 

Python code for the pre-commit code

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)

sentinel_str="[![Build Status]"

readmelines=open("README.md").readlines()
with open("README.md", "w") as fh:
    for aline in readmelines:
        if sentinel_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."


回答3:

The best solution for me was to create a server where I send a query with username and repo's name and get a svg image with the build status for all branches.