-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.sh
More file actions
116 lines (96 loc) · 3.08 KB
/
push.sh
File metadata and controls
116 lines (96 loc) · 3.08 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
coloredEcho () {
local color=$1
shift
printf "$(tput bold)$(tput setaf $color)%s$(tput sgr0)\n" "$@"
}
pushCommits () {
local remote=$1
local branch=$2
local range=$3
local batch_size=$4
local local_commits=$5
local remote_commits=$6
local counter=1
local retries=3
# push each batch
for i in $(seq $local_commits -$batch_size 1); do
# get the hash of the commit to push
local h=$(git log --first-parent --reverse --format=format:%H --skip $i -n1)
coloredEcho 6 "Pushing commit $counter/$local_commits (remote synced $remote_commits): $h..."
# push the commit and check the return value
success=false
while ! $success && [ $retries -gt 0 ]; do
if git push --force-with-lease $remote $h:refs/heads/$branch; then
success=true
else
coloredEcho 1 "Pushing failed. Retrying..."
retries=$((retries-1))
sleep 1
fi
done
# check if the retry count has been exhausted
if ! $success; then
coloredEcho 1 "Pushing failed. Aborting."
exit 1
fi
# increment counters
counter=$((counter+1))
remote_commits=$((remote_commits+1))
retries=3
done
# push the final partial batch
success=false
while ! $success && [ $retries -gt 0 ]; do
if git push --force-with-lease $remote HEAD:refs/heads/$branch; then
success=true
else
coloredEcho 1 "Pushing failed. Retrying..."
retries=$((retries-1))
sleep 1
fi
done
# check if the retry count has been exhausted
if ! $success; then
coloredEcho 1 "Pushing failed. Aborting."
exit 1
fi
coloredEcho 2 "Pushing completed successfully."
}
TARGET_DIR=$1
if [ -z "$TARGET_DIR" ]; then
coloredEcho 1 "Usage: $0 <target_directory>"
exit 1
fi
if [ ! -d "$TARGET_DIR" ]; then
coloredEcho 1 "Error: Directory $TARGET_DIR does not exist."
exit 1
fi
# Change the working directory to the specified directory
cd "$TARGET_DIR"
# Check if git is installed
if ! command -v git &> /dev/null; then
coloredEcho 1 "Git is not installed. Aborting."
exit 1
fi
REMOTE=origin
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Check if the branch exists on the remote
if git show-ref --quiet --verify refs/remotes/$REMOTE/$BRANCH; then
# If so, only push the commits that are not on the remote already
RANGE=$REMOTE/$BRANCH..HEAD
REMOTE_COMMITS=$(git rev-list --count $REMOTE/$BRANCH..HEAD)
LOCAL_COMMITS=$(git rev-list --count HEAD ^$REMOTE/$BRANCH)
else
# Else push all the commits
RANGE=HEAD
REMOTE_COMMITS=0
LOCAL_COMMITS=$(git rev-list --count HEAD)
fi
# Count the number of commits to push
N=$(git log --first-parent --format=format:x $RANGE | wc -l)
# Calculate the batch size
BATCH_SIZE=$((LOCAL_COMMITS / 10 + 1))
coloredEcho 2 "REMOTE:$RANGE BATCH_SIZE:$BATCH_SIZE"
# push the commits
pushCommits $REMOTE $BRANCH $RANGE $BATCH_SIZE $LOCAL_COMMITS $REMOTE_COMMITS