Referencing current branch in github readme.md - github

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?

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.

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."

I updated the work of Dan Dye so it's now able to change any git variable into a readme. It also works now with python 3. For example, handling badges by branch for Github actions:
[![Integration Tests](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml/badge.svg?branch={{ current.branch }})](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml?query=branch%3A{{ current.branch }})
And in your pre-commit file add:
.githooks/replace_by_git_vars.py readme.md README.md -v
-v displays the available variables and more
https://gist.github.com/jclaveau/af2271b9fdf05f7f1983f492af5592f8
Thanks a lot for the solution and inspiration!

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.

Related

How to patch remote source code locally in yocto project?

Sometimes, We meet a situation that remote source code fetched by a recipe need to be modified so that suit a specific machine.
How do we create a patch for remote source code locally? After that everytime we build the recipe (even clean it all) we can patch the remote source code automatically.
For example, I have a special machine with architecture A which is not common, so the remote source code need to be modified so that support architecture A.
Suppose there was a file called utils.h (which is code that we fetched by example.bb from remote git repository)
#if defined(__x86_64__) || \
defined(__mips__) || \
defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) \
#define SOME_FUNCTIONALITY 1
Apparently I need to add archtecture A support in the file.
#if defined(__x86_64__) || \
defined(__mips__) || \
defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \
defined(__A__) \
#define SOME_FUNCTIONALITY 1
But if we just modified like that, next time we execute
bitbake -c cleanall example
bitbake example
then we get a unchanged copies again(which means we have to modify it again).
How do we create a Add-architecture-A-support.patch locally so that we can patch the remote source code automatically?
This is a simple one from answers.
(Note: If there was no git in the source code directory, before modifying the source code, you need to create a git repository and commit all in the top directory of the source code.)
git init # create a git repository
git add .
git commit -m "First commit" # first commit
After change the utils.h as above, we can check the git status. It usually looks like that.
$ git status
HEAD detached from 87b933c420
Changes not staged for commit:
(use "git add <file>..." to update what will be comitted)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: ../../utils.h
...
no changes added to commit (use "git add" and/or "git commit -a")
Then we add and commit the change locally (usually we don't have the permission to push to upper stream).
$ git add utils.h
$ git commit -m "Patch test"
After that we can use git to create a patch for the recent commit.
$ git show >Add-architecture-A-support.patch
It will creat a patch in the current directory with contents looks like that
commit a79e523...
Author: 杨...
Date: ...
Patch test
diff --git a/somedir/utils.h b/somedir/utils.h
index 20bfd36c84..
--- a/somedir/utils.h
+++ b/somedir/utils.h
...
+ defined(__A__) \
...
Then we can move the patch to the local layer where the recipe stayed.
recipe-example
|-- example
| |-- Add-architecture-A-support.patch
|-- example.bb
And add the patch in example.bb with this.
SRC_URI += "\
file://Add-architecture-A-support.patch \
"
Work finished. (Also, if want to undo the local commit after creating the patch, you can use git reset HEAD^ utils.h. emmm, I think so, maybe there are some faults, just google it)

How to get source branch from google cloud build trigger on push when merging

I am merging 'feature-branch' onto 'dev-branch' from a github pull request. I am using a google cloud build trigger that is triggered on push to 'dev-branch'. From what I can tell in the documentation of substitution variables, there are no substitution variables to get the name of the branch that I am merging from - 'feature-branch' and only the branch that I merging to - 'dev-branch'. Is there a way or a workaround to get information (name, sha, id, etc.) of the branch that is being merged from on a google cloud build trigger from a push to branch event?
Presumably you can have some naming conventions on the first line of the commit message (happens at merge pull request "event"), so that this line includes the source (or head) branch name (the source of the merge - in your words - 'feature-branch').
Then, you can create a substitution variable:
substitutions:
_COMMIT_MESSAGE: $(commit.commit.message)
here is a documentation link: Creating substitutions using payload bindings
And use that variable in some build step to get the the head branch name:
mapfile -t commit_lines <<< "${_COMMIT_MESSAGE}"
source_branch="$(echo ${commit_lines[0]} | <<add your command here following naming convention for the commit message>> )"
echo "=> My source branch name: ${source_branch}"
After that you can use the source branch name.

unable to delete git branch due to "renamed branch"

I've scoured several different posts but there doesn't appear to be any that match with this exact issue of an "apparent" branch renaming occurring but nothing seeming to line up.
Essentially, I've been trying to delete a remote branch off of an enterprise git version but I've been getting rejected and I was wondering if there was any additional steps I can try out?
here is the following CLI information:
| => git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/releases/v1.7.2_log4j2
(base)
| ~/Documents/<repo> # (user)
| => git push origin -d releases/v1.7.2_log4j2
To https://github.<company>.com/<org>/<repo>.git
! [remote rejected] releases/v1.7.2_log4j2 (branch releases/v1.7.2_log4j2 is being renamed)
error: failed to push some refs to 'https://github.<company>.com/<org>/<repo>.git'
My git version: 2.24.3 (Apple Git-128).
EDIT: there are no branch protection rules that apply to this branch and I have tried the command in the suggestions of git push -d origin releases<1.7.2_log4j2 with the same result
This looks like a github issue. There is a github-community thread where someone got the exact same message and it turned out to be a flag that was set within the github system that marked the branch as being renamed at the moment. They had to ask the github-support to clear that flag and then were able to delete the branch.
You seem to have your parameters backwards. Try
git push -d origin releases/v1.7.2_log4j2

Customize comment in Jira - GitHub integration

Is it possible to customize what is commented in Jira when a commit is pushed in GitHub?
Currently the following information is commented:
Triggered by: John Doe
Comment: #TST-1234: dummy commit to test jira-github integration
Repository: testing
Branch: refs/heads/master
Git Issue URL: https://github.com/...
And I want it to comment only the URL part, all the rest is not interesting information for me, and I want the comments to be clean.
a possible solution could be to customize the commit message itself
ensure that commit messages include the branch name, which in JIRA are most likely derived from the issue title.
To achieve this, coworkers have to include in their .git/hooks directory a file named commit-msg with the following contents :
#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
tmp=$(mktemp) || exit
echo "$current_branch $(cat "$1")" > "$tmp"
mv "$tmp" "$1"
Then when someone is committing on the feature branch ABC-1234-customers-cant-log-in, a commit command like this :
git commit -m "Awesome changes"
...will actually produce the following commit message :
ABC-1234-customers-cant-log-in Awesome changes
...and JIRA will then link the commit to the issue.
solution and credits goes to answer here: https://stackoverflow.com/a/55008618/7540322

GitHub Api: How to get Root :tree_sha of a repository?

How do I get the Root :tree_sha of a GitHub repository via the GitHub API?
The GitHib API help pages don't seem to explain this critical piece of information:
http://develop.github.com/p/object.html
Can get the contents of a tree by tree
SHA
tree/show/:user/:repo/:tree_sha
To get a listing of the root tree for
the facebox project from our commit
listing, we can call this:
$ curl
http://github.com/api/v2/yaml/tree/show/defunkt/facebox/a47803c9ba26213ff194f042ab686a7749b17476
Each commit contains the sha of the entire tree as of that commit.
Use the API to get a JSON object representing the master branch.
https://api.github.com/repos/:owner/:repo/branches/master
That branch's last commit includes the tree's sha that I think you're asking for.
This bit of code demonstrates how to get the head_tree_sha in Python.
import requests
token = '0...f'
key = {'Authorization':'token '+token}
master = requests.get('https://api.github.com/repos/'+owner+'/' + repo '/branches/master', headers=key)
master = master.json()
head_tree_sha = master['commit']['commit']['tree']['sha']
https://developer.github.com/v3/git/commits/
http://develop.github.com/p/commits.html
The commit tells you its tree sha.
[EDIT]
If you want the tree sha of a subfolder cd into the parent folder of the one you're interested in and run:
git ls-tree HEAD
If you want Root tree sha:
git show HEAD --format=raw
1st line has commit sha
2nd line has tree sha
I'm not sure about the GitHub API — however if you want just the hash you can use this command in your clone:
git show HEAD --format=%T | head -1
Or use %t for the abbreviated hash.