简体   繁体   中英

How to get a list of all files that are different to what's on origin in a pre-push git hook

I'm trying (unsuccessfully) to get a list of all files that are different than what's on origin in a pre-push hook.

It needs to work under the following conditions:

  1. Branch not yet pushed to remote, so all files in all local commits should be output
  2. Branch exists on remote, but local is ahead by X commits, so show the files that are different only.

The range in the sample pre-commit hook looked promising, so I combined it with diff , but it doesn't satisfy condition 2. All changed files are returned, even if they're already on origin.

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Handle delete
        :
    else
        if [ "$remote_sha" = $z40 ]
        then
            # New branch, examine all commits
            range="$local_sha"
        else
            # Update to existing branch, examine new commits
            range="$remote_sha..$local_sha"
        fi

        diffFiles=`git diff --diff-filter=d --name-only origin "$range" | grep ".js$"`
        echo "$diffFiles"
    fi
done

I also tried diff ing the branch explicitly, which works when the remote branch exists, but errors when it doesn't. Is there a way to know whether it exists beforehand?

branchName=`git symbolic-ref --short -q HEAD`
diffFiles=`git diff --diff-filter=d --name-only origin/$branchName $branchName`

echo "$diffFiles"

You can check for the existence of a branch in remote with:

git ls-remote <yourremote> | grep -q <branchname>

it will exit with status code 0 if it exists

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM