-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_push.sh
More file actions
executable file
·107 lines (89 loc) · 3.74 KB
/
git_push.sh
File metadata and controls
executable file
·107 lines (89 loc) · 3.74 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
#!/bin/bash
# Checking if commit message is supplied and warn about additional arguments
if [ -z "$1" ]
then
echo "No commit message provided, exiting."
exit 1
elif [ ! -z "$2" ]
then
echo "Additional input found after the commit message, ignoring it."
fi
# Proceed only with the first argument
commit_msg=$1
echo "==== Current Working Branch ===="
# Check the current working branch
currentBranch=$(git rev-parse --abbrev-ref HEAD)
echo "Current working branch: $currentBranch"
echo "==== Git Status Before Changes are Added ===="
# Print git status
git status
echo "==== Changes in Files ===="
# List changed files
git diff --name-only
echo "==== Adding Changes to Staging Area ===="
# Add all changes to the staging area
git add .
echo "==== Committing Changes ===="
# Commit the changes
commit_result=$(git commit -m "$commit_msg")
if [[ $commit_result == *"nothing to commit"* ]]; then
echo -e "\033[0;33mAlert! No changes detected in the files, nothing to commit or push.\033[0m" # Yellow
exit 1
fi
echo "==== Details of Latest Commit ===="
# Print the most recent commit
git log -1 --pretty=format:"%h%x09%an%x09%ad%x09%s"
echo "==== Pushing Changes to '$currentBranch' Branch ===="
# Push the changes to the current branch
git push origin $currentBranch
push_exit_status=$? # save the exit status of the git push command
# if the push was not successful, try to pull the latest changes
if [[ $push_exit_status -ne 0 ]]; then
echo "Push failed, trying to pull the latest changes..."
git pull origin $currentBranch
# after pulling the latest changes, try pushing again
git push origin $currentBranch
push_exit_status=$? # save the exit status again
# check if the push was successful this time
if [[ $push_exit_status -ne 0 ]]; then
echo -e "\033[0;31mError occurred! Could not push the changes even after pulling the latest changes. Please resolve conflicts manually.\033[0m" # Red
exit 1
fi
fi
echo "==== Git Status After Push ===="
# Print git status after push
git status
echo "==== Log of Last 5 Commits ===="
# Print the log of the latest 5 commits
git log --pretty=format:"%h%x09%an%x09%ad%x09%s" -5
echo "==== Verifying Everything Worked as Planned ===="
uncommitted_changes=$(git status --porcelain)
if [[ -z "$uncommitted_changes" ]]; then
echo -e "\033[0;32mAll changes were successfully committed and pushed!\033[0m" # Green
else
echo -e "\033[0;31mError occurred! There are uncommitted changes. Process did not complete successfully.\033[0m" # Red
fi
# Print the latest commit hash
latest_commit=$(git rev-parse --short HEAD)
echo "Latest commit hash: $latest_commit"
# Print the current local time
current_time=$(date)
echo "Current local time: $current_time"
echo "==== Time Since Last Save ===="
current_time=$(date +%s) # Current timestamp in seconds
last_saved_time=$(git show -s --format=%ct HEAD) # Timestamp of the last commit
time_diff=$((current_time - last_saved_time)) # Difference in timestamps
# Calculate days, hours, minutes and seconds
days=$((time_diff/(60*60*24)))
hours=$((time_diff/(60*60)%24))
minutes=$((time_diff/60%60))
seconds=$((time_diff%60))
if [[ $days -gt 0 ]]; then
echo "Time since last save: $days days, $hours hours, $minutes minutes, $seconds seconds."
elif [[ $hours -gt 0 ]]; then
echo "Time since last save: $hours hours, $minutes minutes, $seconds seconds."
elif [[ $minutes -gt 0 ]]; then
echo "Time since last save: $minutes minutes, $seconds seconds."
else
echo "Time since last save: $seconds seconds."
fi