-
Notifications
You must be signed in to change notification settings - Fork 0
/
pom-merge-driver.sh
executable file
·93 lines (67 loc) · 2.01 KB
/
pom-merge-driver.sh
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
getVersion() {
file=$1
version=$(xmlstarlet sel -t -m _:project -v _:version "${file}")
if [ -z "$version" ]; then
version=$(xmlstarlet sel -t -m _:project/_:parent -v _:version "${file}")
fi
echo "$version"
}
setVersion() {
file=$1
oldVersion=$2
newVersion=$3
tempFile=$(mktemp)
insideDependency=false
# Loop through each line in the input file
while IFS= read -r line; do
if [[ $line == *"<dependency>"* ]]; then
insideDependency=true
fi
if [[ $line == *"</dependency>"* ]]; then
insideDependency=false
fi
if [[ $insideDependency == false ]]; then
# Check if line contains the old version and adjust if necessary
if [[ $line == *"<version>${oldVersion}</version>"* ]]; then
# Replace the version
line=$(echo "$line" | sed "s/<version>${oldVersion}<\/version>/<version>${newVersion}<\/version>/")
fi
fi
# Append the modified line to the output file
echo "$line" >> $tempFile
done < $file
mv $tempFile $file
}
# ----------------------- start -----------------------------
echo "Running POM merge driver"
ours=$1
base=$2
theirs=$3
if ! command -v xmlstarlet &> /dev/null; then
echo "xmlstarlet is not installed. Installing it now..."
sudo apt -y install xmlstarlet
fi
ourVersion=$(getVersion $ours)
echo "Our version: ${ourVersion}"
baseVersion=$(getVersion $base)
echo "Base version: ${baseVersion}"
theirVersion=$(getVersion $theirs)
echo "Their version: ${theirVersion}"
if [ "$ourVersion" != "$baseVersion" ] && [ "$theirVersion" != "$baseVersion" ] && [ "$ourVersion" != "$theirVersion" ]; then
echo "Found a version conflict."
foundVersionConflict=true
setVersion $ours $ourVersion $theirVersion
else
echo "No version conflict found."
foundVersionConflict=false
fi
git merge-file $ours $base $theirs
exitCode=$?
if [ $exitCode -ne 0 ]; then
echo "Error: Command 'git merge file' failed with exit code $exitCode. Exiting."
exit 1
fi
if [ "$foundVersionConflict" = "true" ]; then
setVersion $ours $theirVersion $ourVersion
fi