git - Check to make sure on up to date master in bash script -


i trying write function in bash script check make sure files rsynced part of script date master copy git. found this question seemed cover situation. maybe have misunderstood should doesn't seem work hoped.

i have noticed if make commit on separate branch, , merge in master, when change bask master , forget pull (which forget do) script doesn't notice behind master , allows rsync. can advise why function doesn't work hoped?

startup_check()  {     # check make sure on master branch     currentbranch=$(git status|awk 'nr==1{print $3}')      if [ ! "$currentbranch" == "master" ];         echo -e "not on master - cannot proceed, please change master using:\ngit checkout master"         exit 1     fi        # check whether current working branch ahead, behind or diverged remote master, , exit if we're not using current remote master     local=$(git rev-parse @)     remote=$(git rev-parse @{u})     base=$(git merge-base @ @{u})      if [ "$local" == "$remote" ];         echo "working branch up-to-date remote master, proceeding...."     elif [ "$local" == "$base" ];         echo -e "your working branch behind remote branch, need run:\ngit pull"         exit 1     elif [ "$remote" == "$base" ];         echo -e "your working branch ahead of remote branch, need run:\ngit push origin master"         exit 1     else         echo "your working branch has diverged remote master, cannot continue"         exit 1     fi   } 

i'm using git-2.6.2 , bash-4.2

from comment on op had @ code in git-bash-completion code. looking think original code may have worked if add a git fetch in method.

however, have written following wanted, i've tested works if you're not on master , if there remote changes aren't reflected locally, , believe other parts work:

startup_check()  {   # need git fecth first download remote changes compare against   git fetch   # of code taken __git_ps1_changes method of https://github.com/markgandolfo/git-bash-completion   local branch_ref   branch_ref="$(git symbolic-ref -q head 2>/dev/null)";   if [ -n "$branch_ref" ];     local branch_origin     branch_origin="$(git for-each-ref --format='%(upstream:short)' $branch_ref)";     if [ -n "$branch_origin" ];       local branch       branch=${branch_ref##refs/heads/};        if [ "$branch" != "master" ];         echo "not working on master - cannot proceed"         exit 1       fi        local unpush       unpush=$(git rev-list $branch_origin..$branch --count);       local unpull       unpull=$(git rev-list $branch..$branch_origin --count);       local staged       staged=$(git diff --staged --name-status | wc -l);       local uncommits       uncommits=$(git status -s -uall --porcelain);        if [[ $unpush -gt 0 ]];         echo "there changes have not been pushed - cannot proceed. following commits need pushed:"         local unpushed         unpushed=$(git rev-list $branch_origin..$branch);         commit in $unpushed;           git --no-pager log --pretty=format:"%h - %an, %ar : %s" -n 1 $commit         done         exit 1       fi        if [[ $unpull -gt 0 ]];         echo "there changes have not been pulled - cannot proceed. following commits have been added master since last pull:"         local unpulled         unpulled=$(git rev-list $branch..$branch_origin);         commit in $unpulled;           git --no-pager log --pretty=format:"%h - %an, %ar : %s" -n 1 $commit         done         exit 1       fi        if [[ $staged -gt 0 ]];         local staging         staging=$(git diff --staged --name-status);         echo "there changes staged have been commited - cannot proceed"         echo $staging         exit 1       fi        local unstaged       unstaged=$(echo "$uncommits" | grep -c "^ [a-z]");       if [[ $unstaged -gt 0 ]];         echo "there unstaged changes - cannot proceed"         echo $(echo "$uncommits" | grep "^ [a-z]")         exit 1       fi        local untracked       untracked=$(echo "$uncommits" | grep -c "^??");       if [[ $untracked -gt 0 ]];         echo "there untracked changes - cannot proceed"         echo $(echo "$uncommits" | grep "^??")         exit 1       fi     fi   else     echo "working folder isn't git folder"     exit 1   fi } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -