xref: /OK3568_Linux_fs/device/rockchip/common/scripts/save-patches.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/bin/bash -e
2
3[ -z "$DEBUG" ] || set -x
4
5[ -d .git ] || exit 1
6
7PATCH_DIR="${1:-$PWD}"
8PROJECT="${2:-${PWD#$SDK_DIR/}}"
9BASE_COMMIT=$(git log --pretty="%H" -1 "${3:-HEAD}" --)
10
11echo "[$PROJECT] Base commit: $(git log --oneline $BASE_COMMIT -1)"
12
13# Clean
14rm -rf "$PATCH_DIR"
15mkdir -p "$PATCH_DIR"
16
17# Saving header of apply script
18cat << EOF > "$PATCH_DIR/apply-patches.sh"
19#!/bin/bash -e
20
21[ -z "\$DEBUG" ] || set -x
22
23PATCH_DIR="\$(dirname "\$(realpath "\$0")")"
24
25cd "\$PATCH_DIR/$(realpath "$PWD" --relative-to="$PATCH_DIR")"
26
27echo "[$PROJECT] Applying patches from \$PATCH_DIR"
28
29git add .
30if git diff HEAD -- | grep -q ""; then
31	git stash >/dev/null || git reset --hard
32fi
33
34EOF
35chmod a+x "$PATCH_DIR/apply-patches.sh"
36
37# Check files
38git reset &>/dev/null
39if git status -s | grep -q ""; then
40	if [ "$RK_SAVE_COMMITTED" ]; then
41		echo "[$PROJECT] Uncommitted changes ignored:"
42		git status -s
43	elif [ "$RK_SAVE_TRACKED" ]; then
44		if git status -s | grep -q "^?? "; then
45			echo "[$PROJECT] Untracked file changes ignored:"
46			git status -s | grep "^?? "
47		fi
48		git add -u
49	else
50		git add -A
51	fi
52fi
53
54# Nothing to save
55if ! git diff $BASE_COMMIT ${RK_SAVE_COMMITTED:+HEAD --} | grep -q ""; then
56	git reset &>/dev/null
57	echo "[$PROJECT] No patch to save"
58
59	# Perform a clean checkout
60	echo "git checkout $BASE_COMMIT" >> "$PATCH_DIR/apply-patches.sh"
61	exit 0
62fi
63
64echo "[$PROJECT] Saving patches into $PATCH_DIR"
65echo "[$PROJECT] Patches:"
66
67# Saving commits
68MERGE_BASE=$(git merge-base HEAD $BASE_COMMIT || true)
69if [ -n "$MERGE_BASE" ]; then
70	git diff $MERGE_BASE HEAD -- | grep -q "" &&
71		git format-patch $MERGE_BASE..HEAD -o "$PATCH_DIR"
72else
73	# Orphan tree
74	git format-patch -$(git log --oneline | wc -l) -o "$PATCH_DIR"
75fi
76
77# Saving uncommited changes
78if [ -z "$RK_SAVE_COMMITTED" ] && git status -s | grep -q ""; then
79	echo "$PATCH_DIR/local.diff"
80	git diff --binary HEAD -- > "$PATCH_DIR/local.diff"
81fi
82git reset &>/dev/null
83
84# Update apply script
85cat << EOF >> "$PATCH_DIR/apply-patches.sh"
86if [ -n "$MERGE_BASE" ]; then
87	echo "Base commit: $(git log --oneline $MERGE_BASE -1)"
88	git checkout $MERGE_BASE
89else
90	git checkout HEAD^ &>/dev/null || true
91	git branch -D orphan &>/dev/null || true
92	git checkout --orphan orphan >/dev/null
93	git add -f .
94	git reset --hard
95fi
96
97for f in \$(find "\$PATCH_DIR" -name "*.patch" | sort); do
98	git am "\$f"
99done
100
101if [ -e "\$PATCH_DIR/local.diff" ]; then
102	echo "Applying: \$PATCH_DIR/local.diff"
103	git apply "\$PATCH_DIR/local.diff"
104fi
105EOF
106