-
Notifications
You must be signed in to change notification settings - Fork 66
/
sync_commit.sh
executable file
·101 lines (83 loc) · 2.66 KB
/
sync_commit.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
94
95
96
97
98
99
100
101
#!/bin/bash
# Configuration for source Git repository (directory where this script is located)
SOURCE_REPO="$(dirname "$0")"
# Configuration for destination Git repository
# Set the path of the CE repo based on you local setup
DEST_REPO="UPDATE_YOUR_COMMUNITY_REPO_PATH"
# Check if destination repository exists
if [ ! -d "$DEST_REPO" ]; then
echo "Error: Destination repository does not exist."
exit 1
fi
# Check if the source directory is a git repository
cd "$SOURCE_REPO"
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Error: Source directory is not a Git repository."
exit 1
fi
# Specify the branch where the commit exists
COMMIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
git checkout $COMMIT_BRANCH
if [ $? -ne 0 ]; then
echo "Error: Failed to checkout the branch with the commit."
exit 1
fi
# Accept commit ID as the first input
COMMIT_ID=$1
# Accept optional branch name as the second input, default to COMMIT_ID-branch if not provided
BRANCH_NAME=${2:-$COMMIT_ID-branch}
if [ -z "$COMMIT_ID" ]; then
echo "Error: No commit ID provided."
exit 1
fi
# Generating the patch in the source repository
cd "$SOURCE_REPO"
if ! git log -1 $COMMIT_ID > /tmp/commit_message.txt; then
echo "Error: Commit ID $COMMIT_ID not found in source repository."
exit 1
fi
git format-patch -1 $COMMIT_ID --stdout > /tmp/$COMMIT_ID.patch
if [ $? -ne 0 ]; then
echo "Error: Failed to generate patch for commit $COMMIT_ID."
exit 1
fi
# Updating the main branch and creating a new branch in the destination repo
cd "$DEST_REPO"
git checkout main
if [ $? -ne 0 ]; then
echo "Error: Main branch does not exist in the destination repository."
exit 1
fi
git pull
if [ $? -ne 0 ]; then
echo "Error: Failed to pull latest updates from the main branch."
exit 1
fi
git checkout -b "$BRANCH_NAME"
if [ $? -ne 0 ]; then
echo "Error: Failed to create a new branch in the destination repository."
exit 1
fi
# Applying the patch to the destination repository
cd "$DEST_REPO"
git apply /tmp/$COMMIT_ID.patch
if [ $? -ne 0 ]; then
echo "Error: Failed to apply the patch to the destination repository."
exit 1
fi
# Add all changes to the staging area
git add .
if [ $? -ne 0 ]; then
echo "Error: Failed to add changes to the staging area."
exit 1
fi
# Committing the changes in the destination repository
git commit -F /tmp/commit_message.txt
if [ $? -ne 0 ]; then
echo "Error: Failed to commit the patch in the destination repository."
exit 1
fi
echo "Patch applied successfully and new branch $BRANCH_NAME created in destination repository."
# Cleaning up temporary files
rm /tmp/$COMMIT_ID.patch
rm /tmp/commit_message.txt