1

I'm working on the post-receive of my git server so that it automatically uploads the latest version of my website in a folder named after the branch that the commit was in, but somehow doing this code doesn't work as intended.

Somehow my value branch gets the values of all the branches instead of the branch I'm trying to get. Hash does get the right hash code. I have tested it outside of the program, so does branch when I type the right hash. Did I use the wrong syntax in this program?

#!/bin/sh
hash=$(git log -n 1 --pretty=format:"%h")
branch=$(git branch --contains $(git log -n 1 --pretty=format:"%H"))
if [ branch ]
then
    GIT_WORK_TREE="/data/site/'$branch'"
    echo "/data/site/'$branch'"
    git checkout -f $branch
fi
PixieCatSupreme
  • 61
  • 1
  • 1
  • 12
  • 3
    You're looking at it the wrong way: in general, there is no unique branch to which a commit unambiguously belongs. See this answer of mine: http://stackoverflow.com/questions/29257491/which-branch-do-commits-from-a-deleted-branch-belong-to/29258814#29258814 – jub0bs Apr 01 '15 at 13:46
  • 2
    As Jubobs says a commit doesn't have "**a** branch". A commit exists on some number of branches (whichever can reach it). – Etan Reisner Apr 01 '15 at 13:53
  • 1
    Why are you doing all this work, when git post-receive hook gets _oldrev newrev refname_ triplets on stdin? You still need to choose which of those refnames you want if there are more than one, but it should be a start. – Useless Apr 01 '15 at 13:54
  • @Jubobs Ah I see. I didn't look at branches like that before. Thanks for the information. – PixieCatSupreme Apr 01 '15 at 13:59
  • @Useless I had no idea that post-receive did get a refname value. I'll have to look at my code again. – PixieCatSupreme Apr 01 '15 at 14:02
  • Docs say: the [post-receive](https://git-scm.herokuapp.com/docs/githooks#post-receive) hook gets the same information as the [pre-receive](https://git-scm.herokuapp.com/docs/githooks#pre-receive) hook on stdin. – Useless Apr 01 '15 at 14:11
  • I'll edit my program when I get home. Hopefully I can fix it when I try some things with the refname. – PixieCatSupreme Apr 01 '15 at 14:37

1 Answers1

1

Alright, I got it to work as I wanted! After hearing that post-receive got refname as stdin, found out that I had to trim down refname to get only the branch name and came up with this bit of code. Thanks guys. :)

#!/bin/sh
while read oldrev newrev refname
do
    branch=${refname##*/}
    if [ branch ]
    then
        path="/data/site/$branch"
        mkdir $path
        unset GIT_INDEX_FILE
        export GIT_WORK_TREE=$path
        git checkout -f $refname
        echo "Successfully pushed to $path"
    fi
done
PixieCatSupreme
  • 61
  • 1
  • 1
  • 12
  • You have what appears to be a syntax error - you're doing `if [ branch ]` but this just gives `[` the fixed string `branch` as the first argument, not the value of the variable `$branch`. – Josip Rodin May 03 '15 at 13:19
  • I use it to check if branch has a value. I read that doing it this way will return false if branch is empty and true if it has a value. – PixieCatSupreme May 04 '15 at 14:35
  • If you have the shell expand the variable branch by saying `$branch`, then yes. Otherwise, you're just testing whether the six letter string "branch" is non-empty, which it is and always will be. You need to add the dollar sign. :) – Josip Rodin May 06 '15 at 08:30
  • You can also run `man [` to read the manual for that test command. – Josip Rodin May 06 '15 at 08:31