-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-push.sh
executable file
·64 lines (55 loc) · 1.86 KB
/
git-push.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
#!/bin/bash
force=false
while [[ $# -gt 0 ]]; do
case "$1" in
--force)
force=true
shift
;;
*)
printf "\e[31mUnknown option: $1\e[0m\n"
exit 1
;;
esac
done
if ! command -v git &> /dev/null; then
printf "\e[31mError: 'git' is not installed. Please install it first.\e[0m\n"
exit 1
fi
git_repositories=$(find . -maxdepth 2 -type d -name '.git')
if [ -z "$git_repositories" ]; then
printf "\e[33mNo Git repositories found in the specified directory.\e[0m\n"
exit 0
fi
echo "$git_repositories" | while read git_dir; do
(
repo_path=$(dirname "$git_dir")
if [ "$repo_path" = "." ]; then
repo_name_display="${PWD##*/}"
else
repo_name_display=$(basename "$repo_path")
fi
cd "$repo_path" || exit
if [ "$force" = true ]; then
if push_output=$(git push --force origin HEAD 2>&1); then
if ! printf "$push_output" | grep -q "Everything up-to-date"; then
printf "\e[32mGit push completed for $repo_name_display\e[0m\n"
else
printf "\e[33mNo changes to push for $repo_name_display. Skipping...\e[0m\n"
fi
else
printf "\e[31mError: Git push failed for $repo_name_display:\n$push_output\e[0m\n\n"
fi
else
if push_output=$(git push origin HEAD 2>&1); then
if ! printf "$push_output" | grep -q "Everything up-to-date"; then
printf "\e[32mGit push completed for $repo_name_display\e[0m\n"
else
printf "\e[33mNo changes to push for $repo_name_display. Skipping...\e[0m\n"
fi
else
printf "\e[31mError: Git push failed for $repo_name_display:\n$push_output\e[0m\n\n"
fi
fi
)
done