-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-check-file-changes-across-branches.sh
executable file
·54 lines (48 loc) · 1.56 KB
/
git-check-file-changes-across-branches.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Function to display help text
show_help() {
echo "Usage: $0 -f <file-path> -b <base-branch> -s <branch-filter>"
echo " -f Relative path to the file you want to check across branches"
echo " -b Base branch name to compare other branches against"
echo " -s String to filter branch names"
echo "Example: $0 -f src/main.c -b master -s feature"
echo " "
echo "NOTE: it is recommended to add this script to /usr/local/bin "
echo "so it can be run from anywhere"
}
# Initialize variables
FILE_PATH=""
BASE_BRANCH=""
BRANCH_FILTER=""
# Parse command-line options
while getopts 'hf:b:s:' flag; do
case "${flag}" in
f) FILE_PATH="${OPTARG}" ;;
b) BASE_BRANCH="${OPTARG}" ;;
s) BRANCH_FILTER="${OPTARG}" ;;
h) show_help
exit 0 ;;
*) show_help
exit 1 ;;
esac
done
# Check if required options are provided
if [ -z "$FILE_PATH" ] || [ -z "$BASE_BRANCH" ] || [ -z "$BRANCH_FILTER" ]; then
echo "Error: Missing required arguments"
show_help
exit 1
fi
# Fetch all remote branches
git fetch --all
# List all branches and loop through them
for branch in $(git branch -r | grep -v HEAD); do
# Check if the branch name contains the filter string
if [[ $branch == *"$BRANCH_FILTER"* ]]; then
# Check if the file differs from the base branch
if git diff --quiet $BASE_BRANCH..$branch -- $FILE_PATH; then
echo "No changes in $branch for file $FILE_PATH"
else
echo "Changes in $branch for file $FILE_PATH"
fi
fi
done