0

I have a toolchain which builds my documentation of a project everytime when a new push arrives to my Git server.

When there is no specific version, the documentation should be build with the reference called "latest". But when I decice to set a version with the use of a Git tag, the tag should be used in the Git hook to build the documentation with this version number.

The pseudo-code of the hook should look like this:

if (tag_for_this_commit_exists):
    build_docs(str(tag_of_this_commit))
else:
    build_docs("latest")

Question 1:

How can i extract in the post-receive hook the information, if there is a tag assigned to the commit and if there was, which one?

Question 2:

How can I add the tag on the command line in a way that it belongs exactly to the push event and is pushed to the server together with the push of the software?

A. L
  • 131
  • 2
  • 12
  • For the first part, listing tags, and checking if the last tag points to your HEAD seems the easiest way. For the second part please clarify what you mean. Pushing just means synchronizing your local repository with a remote, so if you make changes, create a tag, make more changes, then push, they all go into the same push. – midor Jul 09 '18 at 12:15
  • @midor Thank you. Then on the server I use: git describe --tags and compare the content of the returned tag with the output of git rev-parse HEAD ? On the client's side I would do following: git commit -am "mytext"; git tag "ver1.0"; git push origin master Would it work this way or do I have to make some extra commands like git push --tags – A. L Jul 09 '18 at 12:21
  • I can't tell you exactly because I don't know which server you use, but generally it should work, if both the commit and the tag are there when you push (likely there would be a difference between a pre- and a post-hook, but if you have a post-receive it should be fine). I think you are currently missing the step where you resolve the commit that a tag points to: `git rev-list -n 1 $TAG` (from https://stackoverflow.com/questions/1862423/how-to-tell-which-commit-a-tag-points-to-in-git) – midor Jul 09 '18 at 12:31
  • @midor Thanks for the hint! I was not aware of the fact, that cat prints out the sha of the tag itself and not of the referenced commit. Now I use `git rev-list -n 1` and compare its content with the content of refs/heads/master. The content of can be fetched with `ls refs/tags | sort -n | head -1` – A. L Jul 09 '18 at 12:52
  • `git rev-list -n 1 ` is more directly expressed as `git rev-parse ^{commit}`. – torek Jul 09 '18 at 13:53

1 Answers1

0

Ok, thanks for all replies. I managed the server side with the post-receive script which is located in the hooks folder.

In case that somebody else needs this, here is the script:

#!/bin/bash
#Get the newest tag
NEWEST_TAG=$(ls ../refs/tags | sort -n | head -1)
if [ -z "$NEWEST_TAG" ]
then
    #No tags exist yet
    DOCU_VERSION="latest"
else
    #Check if the tag is referencing the current commit
    TAG_SHA=$(git rev-list  ${NEWEST_TAG} -n 1)
    COMMIT_SHA=$(cat ../refs/heads/master)
    if [ "$TAG_SHA" == "$COMMIT_SHA" ]
    then
        #Tag matches current commit
        DOCU_VERSION=$(ls ../refs/tags | sort -n | head -1)
    else
        #Tag does not match current commit
        DOCU_VERSION="latest"
    fi
fi

#Trigger the build of the documentation on the read-the-docs server
curl -X POST -d "ref=$DOCU_VERSION" 
http://192.168.1.106:8000/api/v2/webhook/myproject/1/
exit 0
A. L
  • 131
  • 2
  • 12