-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-compare-cherried
More file actions
executable file
·45 lines (34 loc) · 1.56 KB
/
git-compare-cherried
File metadata and controls
executable file
·45 lines (34 loc) · 1.56 KB
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
#!/bin/bash
# Usage: ./compare_branches.sh <upstream_branch> <head_branch>
# Example: ./compare_branches.sh main feature/my-branch
if [ $# -lt 2 ]; then
echo "Usage: $0 <upstream_branch> <head_branch>"
exit 1
fi
UPSTREAM=$1
HEAD=$2
echo "Analyzing commits between $UPSTREAM and $HEAD..."
# Get commits that are in HEAD but not in UPSTREAM (similar to git cherry)
UNIQUE_COMMITS=$(git rev-list "$UPSTREAM".."$HEAD")
if [ -z "$UNIQUE_COMMITS" ]; then
echo "No unique commits found in $HEAD that aren't in $UPSTREAM."
exit 0
fi
# Normalize a commit message by stripping all PR number suffixes like (#123)
normalize_msg() {
echo "$1" | sed 's/ (#[0-9]*)\b//g; s/(#[0-9]*)//g' | sed 's/[[:space:]]*$//'
}
# Get all normalized commit messages from upstream for comparison
UPSTREAM_MSGS=$(git log --format="%s" "$UPSTREAM" | while IFS= read -r msg; do normalize_msg "$msg"; done)
echo "Commits in $HEAD that aren't in $UPSTREAM based on commit message:"
echo "--------------------------------------------------------------"
while IFS= read -r commit; do
# Get the commit message for this commit
COMMIT_MSG=$(git log -n 1 --format="%s" "$commit")
NORMALIZED_MSG=$(normalize_msg "$COMMIT_MSG")
# Check if this normalized commit message exists in upstream (fixed-string match to handle special regex chars)
if ! echo "$UPSTREAM_MSGS" | grep -qF "$NORMALIZED_MSG"; then
# This commit message doesn't exist in upstream, so show it
git show --no-patch --format="%C(yellow)%h%C(reset) - %s %C(green)(%cr) %C(bold blue)<%an>%C(reset)" "$commit"
fi
done <<< "$UNIQUE_COMMITS"