root/build-tools/git-utils.sh
Scott Little 58b942baf1 Capture the git context of the build
First step in build avoidance is the creation of a reference build.
We need to capture the state of all the git trees that contribute
to the reference build.

This update will save the git context to $MY_WORKSPACE/CONTEXT

The context file format is actually a shell script that can be run to
recreate the build context.

   repo init ...
   repo sync
   cd $MY_REPO/..
   source .../CONTEXT

Sample CONTEXT file ....
(cd ./cgcs-root && git checkout -f 12a159594130d75aedf40da3f3b1e6bd7f9ff375)
(cd ./cgcs-root/stx/git/calico && git checkout -f 12a159594130d75aedf40da3f3b1e6bd7f9ff375)
(cd ./cgcs-root/stx/git/ceilometer && git checkout -f 12a159594130d75aedf40da3f3b1e6bd7f9ff375)
...
(cd ./stx-tools && git checkout -f 12a159594130d75aedf40da3f3b1e6bd7f9ff375)
(cd ./.repo/manifests && git checkout -f 12a159594130d75aedf40da3f3b1e6bd7f9ff375)
(cd ./.repo/repo && git checkout -f 12a159594130d75aedf40da3f3b1e6bd7f9ff375)

This update also the git_list function, which populates the previously
introduced GIT_LIST variable, and can be used in several other code
paths where we want to find all gits in a directory tree.

Tested build/clean/edit  parallel/serial

Story: 2002835
Task: 22754
Change-Id: I5009b8a360bdb4a03e5a4e83430b7f2ef135115e
Signed-off-by: Scott Little <scott.little@windriver.com>
2018-08-31 12:17:47 -04:00

100 lines
2.0 KiB
Bash
Executable File

#
# Copyright (c) 2018 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
#
# A place for any functions relating to git, or the git hierarchy created
# by repo manifests.
#
#
# git_list <dir>:
# Return a list of git root directories found under <dir>
#
git_list () {
local DIR=${1}
find "${DIR}" -type d -name '.git' -exec dirname {} \; | sort -V
}
# GIT_LIST: A list of root directories for all the gits under $MY_REPO/..
# as absolute paths.
export GIT_LIST=$(git_list "$(dirname "${MY_REPO}")")
# GIT_LIST_REL: A list of root directories for all the gits under $MY_REPO/..
# as relative paths.
export GIT_LIST_REL=$(for p in $GIT_LIST; do
echo .${p#$(dirname ${MY_REPO})};
done)
#
# git_list_containing_branch <dir> <branch>:
# Return a list of git root directories found under <dir> and
# having branch <branch>. The branch need not be current branch.
#
git_list_containing_branch () {
local DIR="${1}"
local BRANCH="${2}"
local d
for d in $(git_list "${DIR}"); do
(
cd "$d"
git branch --all | grep -q "$BRANCH"
if [ $? -eq 0 ]; then
echo "$d"
fi
)
done
}
#
# git_list_containing_tag <dir> <tag>:
# Return a list of git root directories found under <dir> and
# having tag <tag>.
#
git_list_containing_tag () {
local DIR="${1}"
local TAG="${2}"
local d
for d in $(git_list "${DIR}"); do
(
cd "$d"
git tag | grep -q "$TAG"
if [ $? -eq 0 ]; then
echo "$d"
fi
)
done
}
#
# git_context:
# Returns a bash script that can be used to recreate the current git context,
#
# Note: all paths are relative to $MY_REPO/..
#
git_context () {
(
cd $MY_REPO
for d in $GIT_LIST_REL; do
(
cd ${d}
echo -n "(cd ${d} && git checkout -f "
echo "$(git rev-list HEAD -1))"
)
done
)
}