Skip to content

Commit a5df55a

Browse files
cpreciosoMartinsos
andauthored
Add *.copy support to dope.sh (#525)
Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>
1 parent 0ebee55 commit a5df55a

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

opensaas-sh/tools/dope.sh

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ ACTION=$3
3939
DIFF_DIR="${DERIVED_DIR}_diff"
4040
DIFF_DIR_DELETIONS="${DIFF_DIR}/deletions"
4141

42+
file_is_binary() {
43+
file --mime "$1" | grep -q "charset=binary"
44+
}
45+
46+
files_are_equal() {
47+
cmp -s "$1" "$2"
48+
}
49+
4250
# Based on base dir and derived dir, creates a diff dir that contains the diff between the two dirs.
4351
recreate_diff_dir() {
4452
mkdir -p "${DIFF_DIR}"
@@ -62,11 +70,17 @@ recreate_diff_dir() {
6270
filepathToBeUsedAsBase="/dev/null"
6371
fi
6472

65-
local DIFF_OUTPUT
66-
DIFF_OUTPUT=$(diff -Nu --label "${baseFilepath}" --label "${derivedFilepath}" "${filepathToBeUsedAsBase}" "${derivedFilepath}")
67-
if [ $? -eq 1 ]; then
68-
mkdir -p "${DIFF_DIR}/$(dirname "${filepath}")"
69-
echo "${DIFF_OUTPUT}" > "${DIFF_DIR}/${filepath}.diff"
73+
if files_are_equal "${filepathToBeUsedAsBase}" "${derivedFilepath}"; then
74+
continue
75+
fi
76+
77+
mkdir -p "${DIFF_DIR}/$(dirname "${filepath}")"
78+
79+
if file_is_binary "${derivedFilepath}"; then
80+
cp "${derivedFilepath}" "${DIFF_DIR}/${filepath}.copy"
81+
echo "Generated ${DIFF_DIR}/${filepath}.copy"
82+
else
83+
diff -Nu --label "${baseFilepath}" --label "${derivedFilepath}" "${filepathToBeUsedAsBase}" "${derivedFilepath}" > "${DIFF_DIR}/${filepath}.diff"
7084
echo "Generated ${DIFF_DIR}/${filepath}.diff"
7185
fi
7286
done <<< "${DERIVED_FILES}"
@@ -91,7 +105,7 @@ recreate_derived_dir() {
91105

92106
# Copy all the source files from the base dir over to the derived dir.
93107
while IFS= read -r filepath; do
94-
mkdir -p "${DERIVED_DIR}/$(dirname ${filepath})"
108+
mkdir -p "${DERIVED_DIR}/$(dirname "${filepath}")"
95109
cp "${BASE_DIR}/${filepath}" "${DERIVED_DIR}/${filepath}"
96110
done <<< "${BASE_FILES}"
97111

@@ -100,7 +114,7 @@ recreate_derived_dir() {
100114
local num_patches_failed=0
101115
while IFS= read -r diff_filepath; do
102116
local derived_filepath
103-
derived_filepath="${diff_filepath#${DIFF_DIR}/}"
117+
derived_filepath="${diff_filepath#"${DIFF_DIR}"/}"
104118
derived_filepath="${derived_filepath%.diff}"
105119

106120
local patch_output
@@ -118,6 +132,19 @@ recreate_derived_dir() {
118132
echo ""
119133
done < <(find "${DIFF_DIR}" -name "*.diff")
120134

135+
# For each .copy file in diff dir, copy it to the corresponding location in the derived dir.
136+
while IFS= read -r copy_filepath; do
137+
local derived_filepath
138+
derived_filepath="${copy_filepath#"${DIFF_DIR}"/}"
139+
derived_filepath="${derived_filepath%.copy}"
140+
141+
mkdir -p "${DERIVED_DIR}/$(dirname "${derived_filepath}")"
142+
cp "${copy_filepath}" "${DERIVED_DIR}/${derived_filepath}"
143+
echo "Copied ${copy_filepath} to ${DERIVED_DIR}/${derived_filepath}"
144+
echo -e "${GREEN_COLOR}[OK]${RESET_COLOR}"
145+
echo ""
146+
done < <(find "${DIFF_DIR}" -name "*.copy")
147+
121148
# Delete any files that exist in the base dir but shouldn't exist in the derived dir.
122149
# TODO: also allow deletion of dirs.
123150
if [ -f "${DIFF_DIR_DELETIONS}" ]; then

0 commit comments

Comments
 (0)