Skip to content

Commit

Permalink
[IMP] git-diff-branch: allow to diff against major version
Browse files Browse the repository at this point in the history
  • Loading branch information
KangOl committed May 23, 2023
1 parent c969b78 commit f5bee83
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions git-diff-branch
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@ set -euo pipefail

if [[ $# == 0 ]]; then
echo "Usage:" >&2
echo " git diff-branch BRANCH [GIT_DIFF_ARGUMENTS]" >&2
echo " git diff-branch [-m] BRANCH [GIT_DIFF_ARGUMENTS]" >&2
exit 1
fi

MATCH=$1
shift

MAJOR=N
if [[ "$MATCH" == "-m" ]]; then
MAJOR=Y
MATCH=$1
shift
fi;

: "${REMOTE:=origin}"
REMOTE_URL=$(git remote get-url "$REMOTE")
if [[ "${REMOTE_URL%/*}" != "[email protected]:odoo" ]]; then
echo "🚫 This tool only works on github.com/odoo repositories. URL of remote ${REMOTE} is '${REMOTE_URL}'." >&2
exit 1
fi

AWK="awk"
if command -v gawk >/dev/null; then
AWK="gawk"
fi

PREV=$(
git branch --remotes --list "${REMOTE}"'/*' | awk -F/ '!/tmp|staging|nightly|HEAD/ {
# shellcheck disable=SC2016
git branch --remotes --list "${REMOTE}"'/*' | $AWK -F/ '!/tmp|staging|nightly|HEAD/ {
branch=$2;
len = split(gensub("saas-", "", 1, branch), a, ".");
if(len == 1) {
Expand Down Expand Up @@ -48,18 +61,23 @@ PREV=$(
if (a[1] != -1)
printf("%d.%02d:%s\n", a[1], a[2], branch);
}' | sort -n | awk -F: --assign "MATCH=$MATCH" '{
}' | sort -n | $AWK -F: --assign "MATCH=$MATCH" --assign "MAJOR=$MAJOR" '{
branch = $2;
if (branch == MATCH) {
print prev
}
}
{ prev = branch }'
{
if (MAJOR == "N" || index($1, ".00") != 0) {
prev = branch;
};
}'
)

if [[ -z "$PREV" ]]; then
echo "🙁 Cannot determine previous branch of $MATCH" >&2
exit 1
fi

set -x
git diff --no-prefix "${REMOTE}/${PREV}..${REMOTE}/${MATCH}" "$@"

0 comments on commit f5bee83

Please sign in to comment.