Repository: flutter/flutter
File: bin/internal/content_aware_hash.sh (Shell)

Code before:
CURRENT_BRANCH="$(git -C "$FLUTTER_ROOT" rev-parse --abbrev-ref HEAD)"

# By default, the content hash is based on HEAD.
# For local development branches, we want to base the hash on the merge-base
# with the remote tracking branch, so that we don't rebuild the world every
# time we make a change to the engine.
#
# The following conditions are exceptions where we want to use HEAD.
# 1. The current branch is a release branch (main, master, stable, beta).
# 2. The current branch is a GitHub temporary merge branch.
# 3. The current branch is a release candidate branch.
# 4. The current checkout is a shallow clone.
# 5. There is no current branch. E.g. running on CI/CD.
if [[ "$CURRENT_BRANCH" != "main" && \
      "$CURRENT_BRANCH" != "master" && \
      "$CURRENT_BRANCH" != "stable" && \
      "$CURRENT_BRANCH" != "beta" && \
      "$CURRENT_BRANCH" != "gh-readonly-queue/master/pr-"* && \
      "$CURRENT_BRANCH" != "flutter-"*"-candidate."* && \
      ! ( "$CURRENT_BRANCH" == "HEAD" && -n "$LUCI_CONTEXT" ) && \
      ! -f "$FLUTTER_ROOT/.git/shallow" ]]; then

  # This is a development branch. Find the merge-base.
  # We will fallback to origin if upstream is not detected.
  REMOTE="origin"
  set +e
  git -C "$FLUTTER_ROOT" remote get-url upstream >/dev/null 2>&1
  if [[ $? -eq 0 ]]; then
    REMOTE="upstream"
  fi

  # Try to find the merge-base with master, then main.
  MERGEBASE=$(git -C "$FLUTTER_ROOT" merge-base HEAD "$REMOTE/master" 2>/dev/null)
  if [[ -z "$MERGEBASE" ]]; then
    MERGEBASE=$(git -C "$FLUTTER_ROOT" merge-base HEAD "$REMOTE/main" 2>/dev/null)
  fi
  set -e

  if [[ -n "$MERGEBASE" ]]; then
    BASEREF="$MERGEBASE"
  fi
fi

git -C "$FLUTTER_ROOT" ls-tree "$BASEREF" -- "${TRACKEDFILES[@]}" | git hash-object --stdin


Review comment (suggestion):
if you really want to clean this up; set GIT_WORK_TREE="$FLUTTER_ROOT" and you can remove the `-C` option from all the git commands

Code after:
CURRENT_BRANCH="$(git -C "$FLUTTER_ROOT" rev-parse --abbrev-ref HEAD)"

# By default, the content hash is based on HEAD.
# For local development branches, we want to base the hash on the merge-base
# with the remote tracking branch, so that we don't rebuild the world every
# time we make a change to the engine.
#
# The following conditions are exceptions where we want to use HEAD.
# 1. The current branch is a release branch (main, master, stable, beta).
# 2. The current branch is a GitHub temporary merge branch.
# 3. The current branch is a release candidate branch.
# 4. The current checkout is a shallow clone.
# 5. There is no current branch. E.g. running on CI/CD.
if [[ "$CURRENT_BRANCH" != "main" && \
      "$CURRENT_BRANCH" != "master" && \
      "$CURRENT_BRANCH" != "stable" && \
      "$CURRENT_BRANCH" != "beta" && \
      "$CURRENT_BRANCH" != "gh-readonly-queue/master/pr-"* && \
      "$CURRENT_BRANCH" != "flutter-"*"-candidate."* && \
      ! ( "$CURRENT_BRANCH" == "HEAD" && -n "$LUCI_CONTEXT" ) && \
      ! -f "$FLUTTER_ROOT/.git/shallow" ]]; then

  # This is a development branch. Find the merge-base.
  # We will fallback to origin if upstream is not detected.
  REMOTE="origin"
  if git -C "$FLUTTER_ROOT" remote get-url upstream >/dev/null 2>&1; then
    REMOTE="upstream"
  fi

  # Try to find the merge-base with master, then main.
  MERGEBASE=$(git -C "$FLUTTER_ROOT" merge-base HEAD "$REMOTE/master" 2>/dev/null || true)
  if [[ -z "$MERGEBASE" ]]; then
    MERGEBASE=$(git -C "$FLUTTER_ROOT" merge-base HEAD "$REMOTE/main" 2>/dev/null || true)
  fi

  if [[ -n "$MERGEBASE" ]]; then
    BASEREF="$MERGEBASE"
  fi
fi

git -C "$FLUTTER_ROOT" ls-tree "$BASEREF" -- "${TRACKEDFILES[@]}" | git hash-object --stdin