Skip to content

Commit

Permalink
Update git cmd logic in build.xml
Browse files Browse the repository at this point in the history
- Removes prestaged jck material and reclones it, if git command fails

resolves: adoptium#4846

Signed-off-by: Anna Babu Palathingal <[email protected]>
  • Loading branch information
annaibm committed Jan 9, 2024
1 parent 37b48e3 commit 3aca90e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 151 deletions.
160 changes: 9 additions & 151 deletions jck/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<property name="DEST" value="${BUILD_ROOT}/jck" />
<property environment="env" />

<condition property="isZOS" value="true">
<condition property="isZOS" value="true" else="false">
<os family="z/os" />
</condition>

Expand Down Expand Up @@ -87,156 +87,14 @@
</condition>

<target name="stage-jck-material">
<!-- Starting downloading or updating JCK materials based on JCK GIT REPO and JCK VERSION-->
<if>
<available file="${JCK_ROOT_USED}" type="dir" />
<then>
<!--Obtain local and remote SHA to figure out if local materials are up-to-date-->
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="false" outputproperty="localSHA" resultproperty="return.code">
<arg line="rev-parse"/>
<arg line="HEAD"/>
</exec>
<if>
<not>
<equals arg1="${return.code}" arg2="0"/>
</not>
<then>
<echo message="Error: Git command failed with return code ${return.code}. Delete the directory and continue." />
<delete dir="${JCK_ROOT_USED}" failonerror="true" />
</then>
<else>
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="true" outputproperty="remoteSHA">
<arg line="ls-remote"/>
<arg line="${JCK_GIT_REPO_USED}"/>
<arg line="${jck_branch}"/>
</exec>
<loadresource property="localSHATrimmed">
<propertyresource name="localSHA"/>
<filterchain>
<tokenfilter>
<trim/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadresource>

<echo message="LocalSHA = --${localSHATrimmed}--"/>
<echo message="RemoteSHA= --${remoteSHA}--"/>

<condition property="local-material-uptodate">
<contains string="${remoteSHA}" substring="${localSHATrimmed}" />
</condition>
</else>
</if>
</then>
</if>
<if>
<not>
<available file="${JCK_ROOT_USED}" type="dir" />
</not>
<!-- jck materials don't exist, download them -->
<then>
<echo message="${JCK_ROOT_USED} does not exist,
clone from ${JCK_GIT_REPO_USED}, ${jck_branch} branch, to ${JCK_ROOT_USED}" />
<mkdir dir="${JCK_ROOT_USED}/.." />
<exec executable="git" dir="${JCK_ROOT_USED}/.." failonerror="true">
<arg value="clone" />
<arg value="--single-branch" />
<arg value="-b"/>
<arg value="${jck_branch}"/>
<arg value="${JCK_GIT_REPO_USED}" />
<arg value="${JCK_ROOT_USED}" />
</exec>
</then>
<!-- jck materials exist, update jck materials if needed-->
<else>
<if>
<isset property="env.SKIP_JCK_GIT_UPDATE" />
<!-- don't want to update local JCK materials -->
<then>
<echo message="env.SKIP_JCK_GIT_UPDATE is set. Skip Running git update at ${JCK_ROOT_USED}. Continue." />
</then>
<else>
<if>
<isset property="local-material-uptodate" />
<then>
<echo message="Local JCK materials up-to-date. Skipping update"/>
</then>
<else>
<if>
<available file="${JCK_ROOT_USED}/natives" type="dir" />
<then>
<echo message="Deleting ${JCK_ROOT_USED}/natives..." />
<delete includeEmptyDirs="true">
<fileset dir="${JCK_ROOT_USED}/natives"/>
</delete>
</then>
</if>
<echo message="Updating ${JCK_ROOT_USED} with latest..." />
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="false" resultproperty="return.code">
<arg value="pull" />
<arg value="${JCK_GIT_REPO_USED}" />
<arg value="${jck_branch}" />
</exec>
<if>
<not>
<equals arg1="${return.code}" arg2="0"/>
</not>
<then>
<echo message="Performing hard reset..." />
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="true">
<arg value="reset" />
<arg value="--hard" />
<arg value="origin/main" />
</exec>
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="true">
<arg value="clean" />
<arg value="-f" />
<arg value="-d" />
</exec>
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="true">
<arg value="pull" />
<arg value="${JCK_GIT_REPO_USED}" />
<arg value="${jck_branch}" />
</exec>
</then>
</if>
</else>
</if>
</else>
</if>
</else>
</if>
<if>
<isset property="isZOS" />
<then>
<if>
<isset property="local-material-uptodate"/>
<then>
<echo message="Local JCK materials up-to-date. Skipping hard reset"/>
</then>
<else>
<echo message="Performing hard reset of ${JCK_ROOT_USED} using .gitattributes.zos for file conversions..."/>
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${JCK_ROOT_USED}/.git/info" includes="**/*"/>
</delete>
<mkdir dir="${JCK_ROOT_USED}/.git/info" />
<move file="${JCK_ROOT_USED}/.gitattributes.zos" tofile="${JCK_ROOT_USED}/.git/info/attributes" />
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="false">
<arg value="rm" />
<arg value="--cached" />
<arg value="-r" />
<arg value="-q" />
<arg value="." />
</exec>
<exec executable="git" dir="${JCK_ROOT_USED}" failonerror="false">
<arg value="reset" />
<arg value="--hard" />
</exec>
</else>
</if>
</then>
</if>
<exec executable="bash" failonerror="true">
<arg value="update_git_material.sh"/>
<!-- Add other arguments as needed -->
<arg value="${JCK_ROOT_USED}" />
<arg value="${JCK_GIT_REPO_USED}" />
<arg value="${jck_branch}" />
<arg value="${isZOS}" />
</exec>
</target>

<target name="build-jck-natives" depends="setup-native-build-command, build-natives-windows, build-natives-unix">
Expand Down
110 changes: 110 additions & 0 deletions jck/update_git_material.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash
# Now you can use these variables in your script
JCK_ROOT_USED="$1"
JCK_GIT_REPO_USED="$2"
jck_branch="$3"
isZOS="$4"

echo "JCK_ROOT_USED is: $JCK_ROOT_USED"
echo "JCK_GIT_REPO_USED is: $JCK_GIT_REPO_USED"
echo "jck_branch is: $jck_branch"
echo "isZOS is: $isZOS"

delete_native_dirs() {
natives_directory="${JCK_ROOT_USED}/natives"
# Check if the directory exists
if [ -d "$natives_directory" ]; then
echo "Deleting $natives_directory..."
rm -r "$natives_directory"
if [ $? -eq 0 ]; then
echo "Directory deleted successfully."
fi
fi
}

# Function to perform git clone
git_clone() {
echo "Removing test material and git clone again"
rm -rf "$JCK_ROOT_USED"
# Set the working directory before cloning
cd "$(dirname "$JCK_ROOT_USED")" || exit 1
git clone --single-branch -b "${jck_branch}" "${JCK_GIT_REPO_USED}" "${JCK_ROOT_USED}"
# Check if the clone was successful
if [ $? -eq 0 ]; then
echo "Repository cloned successfully into $JCK_ROOT_USED"
else
echo "Failed to clone repository"
exit 1
fi
}

# Function to perform hard reset and pull
perform_hard_reset() {
{ #Try block
echo "Performing hard reset..."
# Fetch changes from the remote repository
git fetch "$JCK_GIT_REPO_USED"
# Reset the local branch to match the remote branch
git reset --hard "FETCH_HEAD"
# Simulate a failure (e.g., by using a non-existent branch)
# Clean untracked files and directories
git clean -dfx
} || {
#Catch block
git_clone
}
# Check if isZOS property is set
if [ -n "$isZOS" ] && [ "$isZOS" = "true" ]; then
# Check if local-material-uptodate property is set
if [ -n "$local_material_uptodate" ]; then
echo "zOS Local JCK materials up-to-date. Skipping hard reset"
else
# Perform hard reset for z/OS
echo "Performing hard reset of ${JCK_ROOT_USED} using .gitattributes.zos for file conversions..."
rm -rf "${JCK_ROOT_USED}/.git/info"
mkdir -p "${JCK_ROOT_USED}/.git/info"
mv "${JCK_ROOT_USED}/.gitattributes.zos" "${JCK_ROOT_USED}/.git/info/attributes"
git rm --cached -r -q .
git reset --hard
fi
fi
}

# Function to check for updates
check_and_handle_updates() {
cd "$JCK_ROOT_USED" || exit
local localSHA=$(git rev-parse HEAD)
localSHATrimmed=$(echo "${localSHA}" | tr -d '[:space:]')
local remoteSHA=$(git ls-remote "$JCK_GIT_REPO_USED" "$jck_branch" | cut -f1)
echo "Current Working Directory: $(pwd)"
echo "LocalSHA = --$localSHA--"
echo "RemoteSHA = --$remoteSHA--"
# Check if SHA match
if [ "$localSHA" != "$remoteSHA" ]; then
# Update materials
delete_native_dirs
perform_hard_reset
else
# Set the property indicating that local materials are up-to-date
echo "Local JCK materials up-to-date. Skipping update"
# Discard unstaged changes
git reset --hard
# Discard staged changes
git clean -fdx
local_material_uptodate="true"
fi
# If SHA match or update succeeded
echo "Local JCK materials updated"
}

# Check if JCK materials exist
if [ -d "$JCK_ROOT_USED" ]; then
# Check if it's a valid Git repository
if [ -d "$JCK_ROOT_USED/.git" ] || git -C "$JCK_ROOT_USED" rev-parse --is-inside-work-tree &>/dev/null; then
echo "The directory is a valid Git repository."
check_and_handle_updates
fi
else
# JCK materials don't exist, git clone
git_clone
fi

0 comments on commit 3aca90e

Please sign in to comment.