Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2fa6b8b6a | ||
|
|
786628a667 | ||
|
|
03169805fd | ||
|
|
ff3055c081 | ||
|
|
0fe270bbf3 | ||
|
|
ded54ebbf1 | ||
|
|
b05b28c0dd | ||
|
|
9935555da2 | ||
|
|
5e229fd859 | ||
|
|
4f97db2a37 | ||
|
|
e67c3fd76c | ||
|
|
6007019007 | ||
|
|
2e01f7bf37 | ||
|
|
c67477200c | ||
|
|
8d4fdcf3ae | ||
|
|
e7507b4eff | ||
|
|
22626c7ff4 | ||
|
|
9dbf850d20 | ||
|
|
ca4500dd02 | ||
|
|
371b4ac497 | ||
|
|
aae8aff6b9 | ||
|
|
87ef82a56b | ||
|
|
871ad7a6f6 | ||
|
|
d6251dd945 | ||
|
|
1ee7d36d15 | ||
|
|
89d9326bc1 | ||
|
|
5e1a60d1d4 | ||
|
|
e4c66a06ff | ||
|
|
0b9259fc9b |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
tests/
|
||||
2
README.md
Normal file
2
README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
# Auto Git updater in bash
|
||||
A small bash project that reads a text file, and automatically pushes, deals with merge conflicts, and does a bunch of shit. Needs improvement.
|
||||
@@ -1,195 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Check if required commands are available
|
||||
for cmd in git nmcli msmtp ssh scp; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
echo "$cmd is required but not installed. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Configuration
|
||||
TXT_FILE="/home/klein/.config/setup/autogitupdate.txt"
|
||||
PHONE_NUMBER="2673479614@vtext.com" # Verizon SMS gateway
|
||||
EMAIL="kleinpanic@gmail.com"
|
||||
SUBJECT="Git Update Report"
|
||||
REPORT="/tmp/git_update_report.txt" # Full detailed report
|
||||
SUMMARY_REPORT="/tmp/git_update_summary.txt" # Full summary report
|
||||
SMS_REPORT="/tmp/git_update_sms.txt" # Minimal SMS summary
|
||||
SSH_HOST="eulerpi" # Alias for the SSH connection
|
||||
SSH_DIR="/home/klein/reports" # Remote directory to store the report
|
||||
|
||||
# Function to ensure a file exists and has the correct permissions
|
||||
ensure_file() {
|
||||
local file="$1"
|
||||
local permissions="$2"
|
||||
|
||||
if [ ! -f "$file" ]; then
|
||||
touch "$file"
|
||||
chmod "$permissions" "$file"
|
||||
echo "Created $file with permissions $permissions"
|
||||
else
|
||||
chmod "$permissions" "$file"
|
||||
echo "Ensured $file has permissions $permissions"
|
||||
fi
|
||||
}
|
||||
|
||||
# Ensure the report files exist and have the correct permissions
|
||||
ensure_file "$REPORT" 600
|
||||
ensure_file "$SUMMARY_REPORT" 600
|
||||
ensure_file "$SMS_REPORT" 600
|
||||
|
||||
# Function to check WiFi connection and internet availability
|
||||
check_wifi() {
|
||||
nmcli -t -f ACTIVE,SSID dev wifi | grep -q "^yes" && ping -c 1 8.8.8.8 &>/dev/null
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to fetch and clean repo directories from text file
|
||||
get_repo_dirs() {
|
||||
local valid_dirs=()
|
||||
local invalid_dirs=()
|
||||
|
||||
while IFS= read -r repo_dir; do
|
||||
if [ -d "$repo_dir" ] && [ -d "$repo_dir/.git" ]; then
|
||||
valid_dirs+=("$repo_dir")
|
||||
else
|
||||
invalid_dirs+=("$repo_dir")
|
||||
fi
|
||||
done < "$TXT_FILE"
|
||||
|
||||
# Log and remove invalid entries from the file
|
||||
if [ ${#invalid_dirs[@]} -gt 0 ]; then
|
||||
for dir in "${invalid_dirs[@]}"; do
|
||||
echo "$dir does not exist or is not a valid git repository. Removing from $TXT_FILE" >> "$REPORT"
|
||||
echo "$dir does not exist or is not a valid git repository. Removing from $TXT_FILE"
|
||||
sed -i "\|$dir|d" "$TXT_FILE"
|
||||
done
|
||||
fi
|
||||
|
||||
# Return valid directories
|
||||
echo "${valid_dirs[@]}"
|
||||
}
|
||||
|
||||
# Function to update, commit, and (if connected) push the changes
|
||||
update_repo() {
|
||||
local repo_dir="$1"
|
||||
local repo_number="$2"
|
||||
cd "$repo_dir" || return
|
||||
|
||||
echo "Processing repository: $repo_dir" >> "$REPORT"
|
||||
echo "Processing repository: $repo_dir"
|
||||
|
||||
git fetch origin
|
||||
if ! git merge origin/main --no-commit --no-ff; then
|
||||
echo "Merge conflict detected in $repo_dir" >> "$REPORT"
|
||||
echo "Merge conflict in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, C" >> "$SMS_REPORT"
|
||||
git merge --abort
|
||||
return 1
|
||||
fi
|
||||
|
||||
if git diff-index --quiet HEAD --; then
|
||||
echo "No changes detected in $repo_dir" >> "$REPORT"
|
||||
echo "No changes in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, NC" >> "$SMS_REPORT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
git add -A
|
||||
git commit -m "Automated update"
|
||||
|
||||
# Check if connected to the internet before pushing
|
||||
if check_wifi; then
|
||||
echo "Internet is available. Attempting to push changes..." >> "$REPORT"
|
||||
if ! git push origin main; then
|
||||
echo "Failed to push changes in $repo_dir" >> "$REPORT"
|
||||
echo "Failed to push changes in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, ERR" >> "$SMS_REPORT"
|
||||
return 1
|
||||
else
|
||||
echo "Changes pushed successfully in $repo_dir" >> "$REPORT"
|
||||
echo "Changes pushed in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, P" >> "$SMS_REPORT"
|
||||
fi
|
||||
else
|
||||
echo "No internet connection. Changes committed locally in $repo_dir" >> "$REPORT"
|
||||
echo "Changes committed locally in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, LC" >> "$SMS_REPORT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Start of the script
|
||||
echo "Starting auto git update script on $(date)" > "$REPORT"
|
||||
echo "Git Update Summary: $(date)" > "$SUMMARY_REPORT"
|
||||
echo "(GIT Report), $(date)" > "$SMS_REPORT"
|
||||
|
||||
# Fetch valid repositories
|
||||
repo_dirs=($(get_repo_dirs)) # Convert to array properly
|
||||
repo_number=1
|
||||
|
||||
# Loop through each valid directory
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
update_repo "$repo_dir" "$repo_number"
|
||||
repo_number=$((repo_number + 1))
|
||||
done
|
||||
|
||||
# Function to send the report via text message
|
||||
send_text() {
|
||||
/usr/bin/msmtp -a default -t <<EOF
|
||||
To: $PHONE_NUMBER
|
||||
From: $EMAIL
|
||||
Subject: $SUBJECT
|
||||
|
||||
$(cat $SMS_REPORT)
|
||||
EOF
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to copy the report over SSH
|
||||
copy_report_ssh() {
|
||||
ssh "$SSH_HOST" "mkdir -p $SSH_DIR"
|
||||
scp "$SUMMARY_REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
local scp_summary_status=$?
|
||||
scp "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
local scp_report_status=$?
|
||||
|
||||
if [[ $scp_summary_status -eq 0 && $scp_report_status -eq 0 ]]; then
|
||||
return 0 # SSH transfer succeeded
|
||||
else
|
||||
return 1 # SSH transfer failed
|
||||
fi
|
||||
}
|
||||
|
||||
# Main logic for text, SSH handling, and local storage
|
||||
if check_wifi; then
|
||||
echo "WiFi is connected. Attempting to send text message and SSH transfer..."
|
||||
send_text && echo "Text message sent successfully." || echo "Failed to send text message."
|
||||
|
||||
if copy_report_ssh; then
|
||||
echo "SSH transfer succeeded. Deleting local reports."
|
||||
rm -f "$REPORT" "$SUMMARY_REPORT"
|
||||
else
|
||||
echo "SSH transfer failed. Keeping local reports for troubleshooting."
|
||||
fi
|
||||
else
|
||||
echo "No WiFi connection detected. Waiting for connection to push changes..."
|
||||
while ! check_wifi; do
|
||||
sleep 60 # Wait for a minute before checking again
|
||||
done
|
||||
|
||||
echo "WiFi is now connected. Pushing changes..."
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
cd "$repo_dir" || continue
|
||||
git push origin main
|
||||
done
|
||||
|
||||
send_text && echo "Text message sent successfully." || echo "Failed to send text message."
|
||||
|
||||
if copy_report_ssh; then
|
||||
echo "SSH transfer succeeded. Deleting local reports."
|
||||
rm -f "$REPORT" "$SUMMARY_REPORT"
|
||||
else
|
||||
echo "SSH transfer failed. Keeping local reports for troubleshooting."
|
||||
fi
|
||||
fi
|
||||
@@ -1,5 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Declare the security variable
|
||||
security_variable=2 # Change this value as needed (0, 1, or 2)
|
||||
|
||||
# Check the value of the security variable
|
||||
if [ "$security_variable" -eq 0 ]; then
|
||||
echo "Error: security_variable is set to 0. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# If the security variable is 2, continue without any conditions
|
||||
if [ "$security_variable" -eq 2 ]; then
|
||||
echo "security_variable is set to 2. Running the script without further checks."
|
||||
else
|
||||
# If the security variable is 1, check if today is Friday
|
||||
DAY_OF_WEEK=$(date +%A)
|
||||
if [ "$DAY_OF_WEEK" = "Friday" ]; then
|
||||
echo "Today is Friday. Continuing with the script."
|
||||
else
|
||||
echo "Today is not Friday (Today is $DAY_OF_WEEK). Exiting the script."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check if required commands are available
|
||||
for cmd in git nmcli msmtp ssh scp gh; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
@@ -10,14 +33,32 @@ done
|
||||
|
||||
# Configuration
|
||||
TXT_FILE="/home/klein/.config/setup/autogitupdate.txt"
|
||||
PHONE_NUMBER="2673479614@vtext.com" # Verizon SMS gateway
|
||||
EMAIL="kleinpanic@gmail.com"
|
||||
SUBJECT="G-U-R"
|
||||
CREDENTIALS_FILE="$HOME/.config/setup/credentials.config"
|
||||
REPORT="/tmp/git_update_report.txt" # Full detailed report
|
||||
SMS_REPORT="/tmp/git_update_sms.txt" # Minimal SMS summary
|
||||
SSH_HOST="eulerpi5" # Alias for the SSH connection
|
||||
SSH_DIR="/home/klein/reports" # Remote directory to store the report
|
||||
SMS_CHAR_LIMIT=160 # Character limit for SMS messages
|
||||
SSH_DIR="/home/klein/reports"
|
||||
|
||||
# Check for the existence of the configuration file
|
||||
if [ ! -f "$TXT_FILE" ]; then
|
||||
echo "Error: Configuration file '$TXT_FILE' not found. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the credentials file exists
|
||||
if [ ! -f "$CREDENTIALS_FILE" ]; then
|
||||
echo "Error: Credentials file '$CREDENTIALS_FILE' not found. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Source the credentials from the config file
|
||||
source "$CREDENTIALS_FILE"
|
||||
|
||||
# Validate that required variables are set
|
||||
if [ -z "$EMAIL" ] || [ -z "$SSH_HOST" ] || [ -z "$PHONE_NUMBER" ]; then
|
||||
echo "Error: Missing required credentials (EMAIL, SSH_HOST, or PHONE_NUMBER) in '$CREDENTIALS_FILE'. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure the report files exist and have the correct permissions
|
||||
ensure_file() {
|
||||
@@ -36,20 +77,22 @@ check_wifi() {
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to fetch and clean repo directories from the text file
|
||||
get_repo_dirs() {
|
||||
local valid_dirs=()
|
||||
local invalid_dirs=()
|
||||
|
||||
while IFS= read -r repo_dir; do
|
||||
# Trim whitespace and skip empty lines
|
||||
repo_dir=$(echo "$repo_dir" | xargs)
|
||||
if [[ -z "$repo_dir" || "$repo_dir" == \#* ]]; then
|
||||
continue
|
||||
fi
|
||||
# Check if the directory is valid and contains a .git folder
|
||||
if [ -d "$repo_dir" ] && [ -d "$repo_dir/.git" ]; then
|
||||
valid_dirs+=("$repo_dir")
|
||||
else
|
||||
invalid_dirs+=("$repo_dir")
|
||||
fi
|
||||
done < "$TXT_FILE"
|
||||
for dir in "${invalid_dirs[@]}"; do
|
||||
sed -i "\|$dir|d" "$TXT_FILE"
|
||||
done
|
||||
|
||||
# Output only the valid directories without any additional text
|
||||
echo "${valid_dirs[@]}"
|
||||
}
|
||||
|
||||
@@ -57,38 +100,47 @@ get_repo_dirs() {
|
||||
create_github_repo() {
|
||||
local repo_name="$1"
|
||||
local repo_dir="$2"
|
||||
echo "Attempting to create GitHub repo for repo $repo_name" >> "$REPORT"
|
||||
local repo_number="$3"
|
||||
echo "Attempting to create GitHub repo for repo $repo_number ($repo_name)" >> "$REPORT"
|
||||
log_msg "Creating GitHub repository for repo $repo_number ($repo_name) using source: $repo_dir"
|
||||
|
||||
# Use GitHub CLI to create the repo
|
||||
if gh repo create "$repo_name" --public --source="$repo_dir" --remote=origin --push; then
|
||||
echo "Successfully created GitHub repository $repo_name" >> "$REPORT"
|
||||
echo "Repo $repo_name: GHS (GitHub Repo Created Successfully)" >> "$SMS_REPORT"
|
||||
|
||||
# Check if the repository has existing commits
|
||||
if [ -d "$repo_dir/.git" ] && [ "$(git -C "$repo_dir" rev-parse HEAD)" ]; then
|
||||
echo "Repository has existing commits. Attempting to add remote and push." >> "$REPORT"
|
||||
|
||||
# Add the GitHub remote if not already added
|
||||
git remote add origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true
|
||||
|
||||
# Set the main branch if necessary
|
||||
git branch -M main
|
||||
|
||||
# Push to GitHub
|
||||
if git push -u origin main; then
|
||||
echo "Existing repository pushed successfully to GitHub." >> "$REPORT"
|
||||
echo "Repo $repo_name: Pushed Successfully" >> "$SMS_REPORT"
|
||||
else
|
||||
echo "Failed to push the existing repository to GitHub." >> "$REPORT"
|
||||
echo "Repo $repo_name: Push Failed" >> "$SMS_REPORT"
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
# Attempt to create the repository via gh.
|
||||
# Capture the output and exit code.
|
||||
local gh_output
|
||||
if gh_output=$(gh repo create "$repo_name" --public --source="$repo_dir" --remote=origin --push 2>&1); then
|
||||
echo "Successfully created GitHub repository #$repo_number: $repo_name" >> "$REPORT"
|
||||
echo "Repo #$repo_number $repo_name: GHS" >> "$SMS_REPORT"
|
||||
echo "Success GHS"
|
||||
log_msg "GitHub repository $repo_name created successfully."
|
||||
else
|
||||
echo "Failed to create GitHub repository $repo_name" >> "$REPORT"
|
||||
echo "Repo $repo_name: GCF (GitHub Repo Creation Failed)" >> "$SMS_REPORT"
|
||||
return 1
|
||||
# Check if the output indicates that the repository already exists.
|
||||
if echo "$gh_output" | grep -qi "already exists"; then
|
||||
echo "Repository $repo_name already exists on GitHub." >> "$REPORT"
|
||||
log_msg "Repository $repo_name already exists on GitHub. Will attempt to add remote."
|
||||
else
|
||||
echo "Failed to create GitHub repository #$repo_number: $repo_name" >> "$REPORT"
|
||||
echo "Repo #$repo_number: GCF" >> "$SMS_REPORT"
|
||||
echo "GCF Repo"
|
||||
log_msg "Failed to create GitHub repository $repo_name. gh output: $gh_output"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# In either case, try to set the remote URL.
|
||||
git remote add origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true
|
||||
git branch -M main
|
||||
if git push -u origin main; then
|
||||
echo "Existing repository #$repo_number pushed successfully to GitHub." >> "$REPORT"
|
||||
echo "Repo #$repo_number: PS" >> "$SMS_REPORT"
|
||||
echo "Repo PS"
|
||||
log_msg "Existing repository $repo_name pushed successfully."
|
||||
else
|
||||
echo "Failed to push the existing repository #$repo_number to GitHub." >> "$REPORT"
|
||||
echo "Repo #$repo_number: PF" >> "$SMS_REPORT"
|
||||
echo "Repo PF"
|
||||
log_msg "Push failed for repository $repo_name."
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check if a remote GitHub origin exists
|
||||
@@ -121,72 +173,197 @@ check_github_remote() {
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to update, commit, and push the changes
|
||||
# Function to check if the repository is up-to-date
|
||||
is_repo_up_to_date() {
|
||||
git remote update &>/dev/null
|
||||
if git status -uno | grep -q "Your branch is up to date"; then
|
||||
echo "Repository is up to date. No fetch needed."
|
||||
return 0
|
||||
else
|
||||
echo "Repository is not up to date. Fetching changes..."
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if the .git folder is valid
|
||||
is_valid_git_repo() {
|
||||
local repo_dir="$1"
|
||||
|
||||
if [ ! -d "$repo_dir/.git" ]; then
|
||||
echo "Error: '.git' directory is missing in '$repo_dir'. Skipping this repository." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check for essential .git files
|
||||
for file in HEAD config; do
|
||||
if [ ! -f "$repo_dir/.git/$file" ]; then
|
||||
echo "Error: Missing '$file' in '.git' directory of '$repo_dir'. Skipping this repository." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Ensure the repository has at least one commit
|
||||
if ! git -C "$repo_dir" rev-parse HEAD &>/dev/null; then
|
||||
echo "Error: The repository in '$repo_dir' appears to be corrupt or does not have any commits." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# A simple logging function to write timestamped messages
|
||||
log_msg() {
|
||||
local msg="$1"
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $msg" >> "$REPORT"
|
||||
}
|
||||
|
||||
update_repo() {
|
||||
local repo_dir="$1"
|
||||
local repo_number="$2"
|
||||
cd "$repo_dir" || return 1
|
||||
|
||||
echo "Processing repository $repo_number located at $repo_dir" >> "$REPORT"
|
||||
# Change directory into the repository.
|
||||
if ! cd "$repo_dir"; then
|
||||
log_msg "Repo #$repo_number: Unable to cd to $repo_dir"
|
||||
return 1
|
||||
fi
|
||||
log_msg "Processing repository #$repo_number at $repo_dir"
|
||||
|
||||
local github_exists=true
|
||||
# Validate that this is a proper git repository.
|
||||
if ! is_valid_git_repo "$repo_dir"; then
|
||||
log_msg "Repo #$repo_number: Invalid repository. Skipping."
|
||||
echo "Invalid .git" >> "$SMS_REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# If there are no commits, create an initial empty commit.
|
||||
if ! git rev-parse HEAD &>/dev/null; then
|
||||
log_msg "Repo #$repo_number: No commits found. Creating an initial empty commit."
|
||||
if ! git commit --allow-empty -m "Initial commit"; then
|
||||
log_msg "Repo #$repo_number: Failed to create initial commit."
|
||||
echo "Repo #$repo_number: No commit" >> "$SMS_REPORT"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check for a valid GitHub remote.
|
||||
local remote_available=true
|
||||
if ! check_github_remote; then
|
||||
echo "No valid GitHub repository found for repo $repo_number." >> "$REPORT"
|
||||
github_exists=false
|
||||
|
||||
log_msg "Repo #$repo_number: No valid GitHub remote detected."
|
||||
remote_available=false
|
||||
if check_wifi; then
|
||||
local repo_name
|
||||
repo_name=$(basename "$repo_dir")
|
||||
create_github_repo "$repo_name" "$repo_dir"
|
||||
github_exists=true
|
||||
fi
|
||||
fi
|
||||
# First, check if a remote named "origin" exists.
|
||||
if git remote get-url origin &>/dev/null; then
|
||||
log_msg "Repo #$repo_number: Remote 'origin' exists; setting URL to git@github.com:kleinpanic/$repo_name.git"
|
||||
git remote set-url origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true
|
||||
else
|
||||
log_msg "Repo #$repo_number: Remote 'origin' does not exist; adding remote."
|
||||
git remote add origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
if [ "$github_exists" = true ]; then
|
||||
git fetch origin &>/dev/null
|
||||
if ! git merge origin/main --no-commit --no-ff; then
|
||||
echo "Merge conflict detected in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: MC" >> "$SMS_REPORT"
|
||||
git merge --abort
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if git diff-index --quiet HEAD --; then
|
||||
echo "No changes detected in repo $repo_number" >> "$REPORT"
|
||||
|
||||
if [ "$github_exists" = false ]; then
|
||||
echo "Repo $repo_number: NC-GDE" >> "$SMS_REPORT"
|
||||
# Recheck if the remote now exists.
|
||||
if check_github_remote; then
|
||||
remote_available=true
|
||||
log_msg "Repo #$repo_number: GitHub remote now exists after setting URL."
|
||||
else
|
||||
log_msg "Repo #$repo_number: Remote still not available; attempting to create GitHub repo via gh."
|
||||
create_github_repo "$repo_name" "$repo_dir" "$repo_number"
|
||||
# Force-set the remote URL regardless.
|
||||
git remote set-url origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true
|
||||
if check_github_remote; then
|
||||
remote_available=true
|
||||
log_msg "Repo #$repo_number: GitHub remote now exists after creation."
|
||||
else
|
||||
log_msg "Repo #$repo_number: Failed to create or add a GitHub remote."
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "Repo $repo_number: NC-GE" >> "$SMS_REPORT"
|
||||
fi
|
||||
|
||||
return 0
|
||||
fi
|
||||
|
||||
git add -A
|
||||
git commit -m "Automated update"
|
||||
|
||||
if [ "$github_exists" = false ]; then
|
||||
echo "No GitHub repository available for repo $repo_number. Committing locally..." >> "$REPORT"
|
||||
echo "Repo $repo_number: lc-ngr" >> "$SMS_REPORT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if check_wifi; then
|
||||
echo "Internet is available. Attempting to push changes..." >> "$REPORT"
|
||||
|
||||
if ! git push origin main; then
|
||||
echo "Failed to push changes in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: PF" >> "$SMS_REPORT"
|
||||
return 1
|
||||
else
|
||||
echo "Changes pushed successfully in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: P" >> "$SMS_REPORT"
|
||||
log_msg "Repo #$repo_number: No WiFi. Cannot add or create remote."
|
||||
fi
|
||||
else
|
||||
echo "No internet connection. Changes committed locally in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: LC" >> "$SMS_REPORT"
|
||||
log_msg "Repo #$repo_number: Valid GitHub remote detected."
|
||||
fi
|
||||
|
||||
# If no local changes exist, log and exit.
|
||||
if [ -z "$(git status --porcelain)" ]; then
|
||||
log_msg "Repo #$repo_number: No local changes detected."
|
||||
if [ "$remote_available" = false ]; then
|
||||
echo "Repo #$repo_number: NC-GDE" >> "$SMS_REPORT"
|
||||
else
|
||||
echo "Repo #$repo_number: NC-GE" >> "$SMS_REPORT"
|
||||
fi
|
||||
echo "NC-GE"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Stage all changes.
|
||||
log_msg "Repo #$repo_number: Staging all changes."
|
||||
git add -A
|
||||
|
||||
# Commit changes; override PGP signing for automation.
|
||||
if git -c commit.gpgSign=false commit -m "Automated update"; then
|
||||
log_msg "Repo #$repo_number: Local commit succeeded."
|
||||
else
|
||||
log_msg "Repo #$repo_number: Commit failed (perhaps nothing to commit)."
|
||||
echo "Repo #$repo_number: No commit" >> "$SMS_REPORT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Pull with rebase if a remote exists and WiFi is available.
|
||||
if [ "$remote_available" = true ] && check_wifi; then
|
||||
log_msg "Repo #$repo_number: Attempting pull --rebase on branch 'main'."
|
||||
if timeout 60s git pull --rebase --no-edit origin main; then
|
||||
log_msg "Repo #$repo_number: Rebase succeeded."
|
||||
else
|
||||
local pull_exit=$?
|
||||
log_msg "Repo #$repo_number: Rebase failed with exit code $pull_exit; aborting rebase."
|
||||
git rebase --abort || log_msg "Repo #$repo_number: Failed to abort rebase cleanly."
|
||||
echo "Repo #$repo_number: Rebase Conflict" >> "$SMS_REPORT"
|
||||
echo "MC"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log_msg "Repo #$repo_number: Skipping pull/rebase (no remote or no internet)."
|
||||
fi
|
||||
|
||||
# Push if a remote exists and WiFi is available.
|
||||
if [ "$remote_available" = true ] && check_wifi; then
|
||||
log_msg "Repo #$repo_number: Attempting push on branch 'main'."
|
||||
if timeout 60s git push origin main; then
|
||||
log_msg "Repo #$repo_number: Push succeeded."
|
||||
echo "Repo #$repo_number: P" >> "$SMS_REPORT"
|
||||
echo "P"
|
||||
else
|
||||
local push_output push_exit
|
||||
push_output=$(timeout 60s git push origin main 2>&1) || push_exit=$?
|
||||
log_msg "Repo #$repo_number: Push failed with exit code ${push_exit:-0} and output: $push_output"
|
||||
echo "Repo #$repo_number: PF" >> "$SMS_REPORT"
|
||||
# Fallback: Force-set the remote URL and try again.
|
||||
local repo_name
|
||||
repo_name=$(basename "$repo_dir")
|
||||
log_msg "Repo #$repo_number: Attempting fallback: setting remote URL to git@github.com:kleinpanic/$repo_name.git"
|
||||
git remote set-url origin "git@github.com:kleinpanic/$repo_name.git" 2>&1 || true
|
||||
local fallback_output fallback_exit
|
||||
fallback_output=$(timeout 60s git push origin main 2>&1) || fallback_exit=$?
|
||||
if [ "${fallback_exit:-0}" -eq 0 ]; then
|
||||
log_msg "Repo #$repo_number: Fallback push succeeded."
|
||||
echo "Repo #$repo_number: P" >> "$SMS_REPORT"
|
||||
echo "P"
|
||||
else
|
||||
log_msg "Repo #$repo_number: Fallback push failed with exit code ${fallback_exit:-0} and output: $fallback_output"
|
||||
echo "Repo #$repo_number: PF" >> "$SMS_REPORT"
|
||||
echo "PF"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
log_msg "Repo #$repo_number: No internet; changes committed locally."
|
||||
echo "Repo #$repo_number: LC" >> "$SMS_REPORT"
|
||||
echo "LC"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Start of the script
|
||||
@@ -209,6 +386,7 @@ total_repos=${#repo_dirs[@]}
|
||||
# Loop through each valid directory
|
||||
repo_number=1
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
echo "Updating repository #$repo_number: $repo_dir"
|
||||
update_repo "$repo_dir" "$repo_number"
|
||||
case "$(tail -n 1 "$SMS_REPORT")" in
|
||||
*P) pushed_count=$((pushed_count + 1)) ;;
|
||||
@@ -225,7 +403,9 @@ done
|
||||
# Check if the detailed SMS report exceeds the SMS character limit
|
||||
sms_detailed_content=$(cat "$SMS_REPORT")
|
||||
if [ "${#sms_detailed_content}" -gt "$SMS_CHAR_LIMIT" ]; then
|
||||
sms_summary="Pushed: $pushed_count/$total_repos, No Change: $no_change_count/$total_repos, Locally Committed: $local_commit_count/$total_repos, Conflicts: $conflict_count/$total_repos, Errors: $error_count/$total_repos, No GitHub Repo: $no_github_repo_count/$total_repos, Created: $created_count/$total_repos"
|
||||
DATE=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
echo "Concat output activated"
|
||||
sms_summary="$DATE. Pushed: $pushed_count/$total_repos, No Change: $no_change_count/$total_repos, Locally Committed: $local_commit_count/$total_repos, Conflicts: $conflict_count/$total_repos, Errors: $error_count/$total_repos, No GitHub Repo: $no_github_repo_count/$total_repos, Created: $created_count/$total_repos"
|
||||
echo "$sms_summary" > "$SMS_REPORT"
|
||||
fi
|
||||
|
||||
@@ -241,8 +421,17 @@ EOF
|
||||
}
|
||||
|
||||
copy_report_ssh() {
|
||||
# Ensure SSH_DIR is defined
|
||||
if [ -z "$SSH_DIR" ]; then
|
||||
echo "Error: SSH_DIR is not set. Please define it in your script."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Create the remote directory if it doesn't exist
|
||||
ssh "$SSH_HOST" "mkdir -p $SSH_DIR"
|
||||
scp "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
|
||||
# Transfer the report file to the specified directory
|
||||
rsync -avz --progress "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
return $?
|
||||
}
|
||||
|
||||
@@ -259,3 +448,16 @@ if check_wifi; then
|
||||
else
|
||||
echo "No WiFi connection detected. Changes committed locally."
|
||||
fi
|
||||
|
||||
update_repo_list() {
|
||||
local repo_list_file="$HOME/.config/setup/autogitupdate.txt"
|
||||
echo "Scanning for Git repositories..."
|
||||
# Adjust the search path as needed (e.g., $HOME or a specific projects directory)
|
||||
find "$HOME" -type d -name ".git" -prune | sed 's/\/.git$//' > "$repo_list_file"
|
||||
echo "Repository list updated at $repo_list_file"
|
||||
}
|
||||
|
||||
if [[ "$1" == "--update-repos" ]]; then
|
||||
update_repo_list
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@@ -1,244 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Check if required commands are available
|
||||
for cmd in git nmcli msmtp ssh scp gh; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
echo "$cmd is required but not installed. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Configuration
|
||||
TXT_FILE="/home/klein/.config/setup/autogitupdate.txt"
|
||||
PHONE_NUMBER="2673479614@vtext.com" # Verizon SMS gateway
|
||||
EMAIL="kleinpanic@gmail.com"
|
||||
SUBJECT="G-U-R"
|
||||
REPORT="/tmp/git_update_report.txt" # Full detailed report
|
||||
SMS_REPORT="/tmp/git_update_sms.txt" # Minimal SMS summary
|
||||
SSH_HOST="eulerpi5" # Alias for the SSH connection
|
||||
SSH_DIR="/home/klein/reports" # Remote directory to store the report
|
||||
SMS_CHAR_LIMIT=160 # Character limit for SMS messages
|
||||
|
||||
# Ensure the report files exist and have the correct permissions
|
||||
ensure_file() {
|
||||
local file="$1"
|
||||
local permissions="$2"
|
||||
[ ! -f "$file" ] && touch "$file" && chmod "$permissions" "$file"
|
||||
chmod "$permissions" "$file"
|
||||
}
|
||||
|
||||
ensure_file "$REPORT" 600
|
||||
ensure_file "$SMS_REPORT" 600
|
||||
|
||||
# Function to check WiFi connection and internet availability
|
||||
check_wifi() {
|
||||
nmcli -t -f ACTIVE,SSID dev wifi | grep -q "^yes" && ping -c 1 8.8.8.8 &>/dev/null
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to fetch and clean repo directories from the text file
|
||||
get_repo_dirs() {
|
||||
local valid_dirs=()
|
||||
local invalid_dirs=()
|
||||
while IFS= read -r repo_dir; do
|
||||
if [ -d "$repo_dir" ] && [ -d "$repo_dir/.git" ]; then
|
||||
valid_dirs+=("$repo_dir")
|
||||
else
|
||||
invalid_dirs+=("$repo_dir")
|
||||
fi
|
||||
done < "$TXT_FILE"
|
||||
for dir in "${invalid_dirs[@]}"; do
|
||||
sed -i "\|$dir|d" "$TXT_FILE"
|
||||
done
|
||||
echo "${valid_dirs[@]}"
|
||||
}
|
||||
|
||||
# Function to create GitHub repo if it doesn't exist
|
||||
create_github_repo() {
|
||||
local repo_name="$1"
|
||||
local repo_dir="$2"
|
||||
echo "Attempting to create GitHub repo for repo $repo_name" >> "$REPORT"
|
||||
|
||||
# Use GitHub CLI to create the repo
|
||||
if gh repo create "$repo_name" --public --source="$repo_dir" --remote=origin --push; then
|
||||
echo "Successfully created GitHub repository $repo_name" >> "$REPORT"
|
||||
echo "Repo $repo_name: GHS (GitHub Repo Created Successfully)" >> "$SMS_REPORT"
|
||||
return 0
|
||||
else
|
||||
echo "Failed to create GitHub repository $repo_name" >> "$REPORT"
|
||||
echo "Repo $repo_name: GCF (GitHub Repo Creation Failed)" >> "$SMS_REPORT"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Enhanced function to check if a remote GitHub origin exists
|
||||
check_github_remote() {
|
||||
# Check if a remote named "origin" exists
|
||||
if ! git remote get-url origin &>/dev/null; then
|
||||
echo "No 'origin' remote found." >> "$REPORT"
|
||||
return 1 # No remote named "origin"
|
||||
fi
|
||||
|
||||
# Check if the "origin" remote points to GitHub
|
||||
local remote_url
|
||||
remote_url=$(git remote get-url origin)
|
||||
if [[ "$remote_url" != *github.com* ]]; then
|
||||
echo "'origin' remote does not point to GitHub." >> "$REPORT"
|
||||
return 1 # Not a GitHub remote
|
||||
fi
|
||||
|
||||
# Verify if the repository exists on GitHub using GitHub CLI
|
||||
local repo_name
|
||||
repo_name=$(basename "$remote_url" .git)
|
||||
local repo_owner
|
||||
repo_owner=$(basename "$(dirname "$remote_url")")
|
||||
local full_repo_name="$repo_owner/$repo_name"
|
||||
|
||||
echo "Checking if GitHub repository '$full_repo_name' exists..." >> "$REPORT"
|
||||
|
||||
if ! gh repo view "$full_repo_name" &>/dev/null; then
|
||||
echo "GitHub repository '$full_repo_name' does not exist or is not accessible." >> "$REPORT"
|
||||
return 1 # GitHub repository does not exist
|
||||
fi
|
||||
|
||||
return 0 # GitHub repository exists
|
||||
}
|
||||
|
||||
# Function to update, commit, and push the changes
|
||||
update_repo() {
|
||||
local repo_dir="$1"
|
||||
local repo_number="$2"
|
||||
cd "$repo_dir" || return
|
||||
|
||||
echo "Processing repository number $repo_number" >> "$REPORT"
|
||||
|
||||
# Check if the GitHub remote exists
|
||||
local github_exists=true
|
||||
if ! check_github_remote; then
|
||||
echo "No valid GitHub repository found for repo $repo_number." >> "$REPORT"
|
||||
github_exists=false
|
||||
fi
|
||||
|
||||
# Fetch from the origin if it exists
|
||||
if [ "$github_exists" = true ]; then
|
||||
git fetch origin &>/dev/null
|
||||
if ! git merge origin/main --no-commit --no-ff; then
|
||||
echo "Merge conflict detected in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: MC" >> "$SMS_REPORT"
|
||||
git merge --abort
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if git diff-index --quiet HEAD --; then
|
||||
echo "No changes detected in repo $repo_number" >> "$REPORT"
|
||||
|
||||
if [ "$github_exists" = false ]; then
|
||||
echo "Repo $repo_number: NC (No changes, but no GitHub repository)" >> "$SMS_REPORT"
|
||||
else
|
||||
echo "Repo $repo_number: NC (No changes, remote exists)" >> "$SMS_REPORT"
|
||||
fi
|
||||
|
||||
return 0
|
||||
fi
|
||||
|
||||
git add -A
|
||||
git commit -m "Automated update"
|
||||
|
||||
if [ "$github_exists" = false ]; then
|
||||
echo "No GitHub repository available for repo $repo_number. Committing locally..." >> "$REPORT"
|
||||
echo "Repo $repo_number: lc-ngr (Locally committed - No GitHub Repo)" >> "$SMS_REPORT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if check_wifi; then
|
||||
echo "Internet is available. Attempting to push changes..." >> "$REPORT"
|
||||
|
||||
if ! git push origin main; then
|
||||
echo "Failed to push changes in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: PF" >> "$SMS_REPORT"
|
||||
return 1
|
||||
else
|
||||
echo "Changes pushed successfully in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: P" >> "$SMS_REPORT"
|
||||
fi
|
||||
else
|
||||
echo "No internet connection. Changes committed locally in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: LC" >> "$SMS_REPORT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Start of the script
|
||||
echo "Starting auto git update script on $(date)" > "$REPORT"
|
||||
echo "(G-R), $(date)" > "$SMS_REPORT"
|
||||
|
||||
# Fetch valid repositories
|
||||
repo_dirs=($(get_repo_dirs)) # Convert to array properly
|
||||
|
||||
# Counters for SMS summary report
|
||||
pushed_count=0
|
||||
no_change_count=0
|
||||
local_commit_count=0
|
||||
conflict_count=0
|
||||
error_count=0
|
||||
created_count=0
|
||||
no_github_repo_count=0
|
||||
total_repos=${#repo_dirs[@]}
|
||||
|
||||
# Loop through each valid directory
|
||||
repo_number=1
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
update_repo "$repo_dir" "$repo_number"
|
||||
case "$(tail -n 1 "$SMS_REPORT")" in
|
||||
*P) pushed_count=$((pushed_count + 1)) ;;
|
||||
*NC*) no_change_count=$((no_change_count + 1)) ;;
|
||||
*LC) local_commit_count=$((local_commit_count + 1)) ;;
|
||||
*MC) conflict_count=$((conflict_count + 1)) ;;
|
||||
*PF) error_count=$((error_count + 1)) ;;
|
||||
*lc-ngr*) no_github_repo_count=$((no_github_repo_count + 1)) ;;
|
||||
*GHS) created_count=$((created_count + 1)) ;;
|
||||
esac
|
||||
repo_number=$((repo_number + 1))
|
||||
done
|
||||
|
||||
# Check if the detailed SMS report exceeds the SMS character limit
|
||||
sms_detailed_content=$(cat "$SMS_REPORT")
|
||||
if [ "${#sms_detailed_content}" -gt "$SMS_CHAR_LIMIT" ]; then
|
||||
sms_summary="Pushed: $pushed_count/$total_repos, No Change: $no_change_count/$total_repos, Locally Committed: $local_commit_count/$total_repos, Conflicts: $conflict_count/$total_repos, Errors: $error_count/$total_repos, No GitHub Repo: $no_github_repo_count/$total_repos, Created: $created_count/$total_repos"
|
||||
echo "$sms_summary" > "$SMS_REPORT"
|
||||
fi
|
||||
|
||||
# Function to send the SMS report
|
||||
send_text() {
|
||||
/usr/bin/msmtp -a default -t <<EOF
|
||||
To: $PHONE_NUMBER
|
||||
From: $EMAIL
|
||||
Subject: $SUBJECT
|
||||
|
||||
$(cat "$SMS_REPORT")
|
||||
EOF
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to copy the report over SSH
|
||||
copy_report_ssh() {
|
||||
ssh "$SSH_HOST" "mkdir -p $SSH_DIR"
|
||||
scp "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
return $?
|
||||
}
|
||||
|
||||
# Main logic for text and SSH handling
|
||||
if check_wifi; then
|
||||
echo "WiFi is connected. Attempting to send text message and SSH transfer..."
|
||||
send_text && echo "Text message sent successfully." || echo "Failed to send text message."
|
||||
|
||||
if copy_report_ssh; then
|
||||
echo "SSH transfer succeeded."
|
||||
rm -f "$REPORT"
|
||||
else
|
||||
echo "SSH transfer failed. Keeping local reports for troubleshooting."
|
||||
fi
|
||||
else
|
||||
echo "No WiFi connection detected. Changes committed locally."
|
||||
fi
|
||||
|
||||
@@ -1,5 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Declare the security variable
|
||||
security_variable=2 # Change this value as needed (0, 1, or 2)
|
||||
|
||||
# Check the value of the security variable
|
||||
if [ "$security_variable" -eq 0 ]; then
|
||||
echo "Error: security_variable is set to 0. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# If the security variable is 2, continue without any conditions
|
||||
if [ "$security_variable" -eq 2 ]; then
|
||||
echo "security_variable is set to 2. Running the script without further checks."
|
||||
else
|
||||
# If the security variable is 1, check if today is Friday
|
||||
DAY_OF_WEEK=$(date +%A)
|
||||
if [ "$DAY_OF_WEEK" = "Friday" ]; then
|
||||
echo "Today is Friday. Continuing with the script."
|
||||
else
|
||||
echo "Today is not Friday (Today is $DAY_OF_WEEK). Exiting the script."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check if required commands are available
|
||||
for cmd in git nmcli msmtp ssh scp gh; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
@@ -10,14 +33,32 @@ done
|
||||
|
||||
# Configuration
|
||||
TXT_FILE="/home/klein/.config/setup/autogitupdate.txt"
|
||||
PHONE_NUMBER="2673479614@vtext.com" # Verizon SMS gateway
|
||||
EMAIL="kleinpanic@gmail.com"
|
||||
SUBJECT="G-U-R"
|
||||
CREDENTIALS_FILE="$HOME/.config/setup/credentials.config"
|
||||
REPORT="/tmp/git_update_report.txt" # Full detailed report
|
||||
SMS_REPORT="/tmp/git_update_sms.txt" # Minimal SMS summary
|
||||
SSH_HOST="eulerpi5" # Alias for the SSH connection
|
||||
SSH_DIR="/home/klein/reports" # Remote directory to store the report
|
||||
SMS_CHAR_LIMIT=160 # Character limit for SMS messages
|
||||
SSH_DIR="/home/klein/reports"
|
||||
|
||||
# Check for the existence of the configuration file
|
||||
if [ ! -f "$TXT_FILE" ]; then
|
||||
echo "Error: Configuration file '$TXT_FILE' not found. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the credentials file exists
|
||||
if [ ! -f "$CREDENTIALS_FILE" ]; then
|
||||
echo "Error: Credentials file '$CREDENTIALS_FILE' not found. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Source the credentials from the config file
|
||||
source "$CREDENTIALS_FILE"
|
||||
|
||||
# Validate that required variables are set
|
||||
if [ -z "$EMAIL" ] || [ -z "$SSH_HOST" ] || [ -z "$PHONE_NUMBER" ]; then
|
||||
echo "Error: Missing required credentials (EMAIL, SSH_HOST, or PHONE_NUMBER) in '$CREDENTIALS_FILE'. Exiting the script."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure the report files exist and have the correct permissions
|
||||
ensure_file() {
|
||||
@@ -36,110 +77,234 @@ check_wifi() {
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to fetch and clean repo directories from the text file
|
||||
get_repo_dirs() {
|
||||
local valid_dirs=()
|
||||
local invalid_dirs=()
|
||||
|
||||
while IFS= read -r repo_dir; do
|
||||
# Trim whitespace and skip empty lines
|
||||
repo_dir=$(echo "$repo_dir" | xargs)
|
||||
if [[ -z "$repo_dir" || "$repo_dir" == \#* ]]; then
|
||||
continue
|
||||
fi
|
||||
# Check if the directory is valid and contains a .git folder
|
||||
if [ -d "$repo_dir" ] && [ -d "$repo_dir/.git" ]; then
|
||||
valid_dirs+=("$repo_dir")
|
||||
else
|
||||
invalid_dirs+=("$repo_dir")
|
||||
fi
|
||||
done < "$TXT_FILE"
|
||||
for dir in "${invalid_dirs[@]}"; do
|
||||
sed -i "\|$dir|d" "$TXT_FILE"
|
||||
done
|
||||
|
||||
# Output only the valid directories without any additional text
|
||||
echo "${valid_dirs[@]}"
|
||||
}
|
||||
|
||||
# Function to create GitHub repo if it doesn't exist
|
||||
# Function to create GitHub repo if it doesn't exist and push changes
|
||||
create_github_repo() {
|
||||
local repo_name="$1"
|
||||
local repo_dir="$2"
|
||||
echo "Attempting to create GitHub repo for $repo_name" >> "$REPORT"
|
||||
local repo_number="$3"
|
||||
echo "Attempting to create GitHub repo for repo $repo_number ($repo_name)" >> "$REPORT"
|
||||
|
||||
# Use GitHub CLI to create the repo
|
||||
if gh repo create "$repo_name" --public --source="$repo_dir" --remote=origin --push; then
|
||||
echo "Successfully created GitHub repository $repo_name" >> "$REPORT"
|
||||
echo "Successfully created GitHub repository #$repo_number: $repo_name" >> "$REPORT"
|
||||
echo "Repo #$repo_number $repo_name: GHS" >> "$SMS_REPORT"
|
||||
echo "Success GHS"
|
||||
|
||||
# Check if the repository has existing commits
|
||||
if [ -d "$repo_dir/.git" ] && [ "$(git -C "$repo_dir" rev-parse HEAD)" ]; then
|
||||
echo "Repository #$repo_number has existing commits. Attempting to add remote and push." >> "$REPORT"
|
||||
|
||||
# Add the GitHub remote if not already added
|
||||
git remote add origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true
|
||||
|
||||
# Set the main branch if necessary
|
||||
git branch -M main
|
||||
|
||||
# Push to GitHub
|
||||
if git push -u origin main; then
|
||||
echo "Existing repository #$repo_number pushed successfully to GitHub." >> "$REPORT"
|
||||
echo "Repo #$repo_number: PS" >> "$SMS_REPORT"
|
||||
echo "Repo PS"
|
||||
else
|
||||
echo "Failed to push the existing repository #$repo_number to GitHub." >> "$REPORT"
|
||||
echo "Repo #$repo_number: PF" >> "$SMS_REPORT"
|
||||
echo "Repo PF"
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
else
|
||||
echo "Failed to create GitHub repository $repo_name" >> "$REPORT"
|
||||
echo "Failed to create GitHub repository #$repo_number: $repo_name" >> "$REPORT"
|
||||
echo "Repo #$repo_number: GCF" >> "$SMS_REPORT"
|
||||
echo "GCF Repo"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if a remote GitHub origin exists
|
||||
check_github_remote() {
|
||||
git remote -v | grep -q "github.com"
|
||||
if ! git remote get-url origin &>/dev/null; then
|
||||
echo "No 'origin' remote found." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local remote_url
|
||||
remote_url=$(git remote get-url origin)
|
||||
if [[ "$remote_url" != *github.com* ]]; then
|
||||
echo "'origin' remote does not point to GitHub." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local repo_name
|
||||
repo_name=$(basename "$remote_url" .git)
|
||||
local repo_owner
|
||||
repo_owner=$(basename "$(dirname "$remote_url")")
|
||||
local full_repo_name="$repo_owner/$repo_name"
|
||||
|
||||
echo "Checking if GitHub repository '$full_repo_name' exists..." >> "$REPORT"
|
||||
|
||||
if ! gh repo view "$full_repo_name" &>/dev/null; then
|
||||
echo "GitHub repository '$full_repo_name' does not exist or is not accessible." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to update, commit, and push the changes
|
||||
update_repo() {
|
||||
# Function to check if the repository is up-to-date
|
||||
is_repo_up_to_date() {
|
||||
git remote update &>/dev/null
|
||||
if git status -uno | grep -q "Your branch is up to date"; then
|
||||
echo "Repository is up to date. No fetch needed."
|
||||
return 0
|
||||
else
|
||||
echo "Repository is not up to date. Fetching changes..."
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if the .git folder is valid
|
||||
is_valid_git_repo() {
|
||||
local repo_dir="$1"
|
||||
local repo_number="$2"
|
||||
cd "$repo_dir" || return
|
||||
|
||||
echo "Processing repository number $repo_number" >> "$REPORT"
|
||||
|
||||
git fetch origin
|
||||
if ! git merge origin/main --no-commit --no-ff; then
|
||||
echo "Merge conflict detected in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: MC" >> "$SMS_REPORT"
|
||||
git merge --abort
|
||||
|
||||
if [ ! -d "$repo_dir/.git" ]; then
|
||||
echo "Error: '.git' directory is missing in '$repo_dir'. Skipping this repository." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if git diff-index --quiet HEAD --; then
|
||||
echo "No changes detected in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: NC" >> "$SMS_REPORT"
|
||||
# Check for essential .git files
|
||||
for file in HEAD config; do
|
||||
if [ ! -f "$repo_dir/.git/$file" ]; then
|
||||
echo "Error: Missing '$file' in '.git' directory of '$repo_dir'. Skipping this repository." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Ensure the repository has at least one commit
|
||||
if ! git -C "$repo_dir" rev-parse HEAD &>/dev/null; then
|
||||
echo "Error: The repository in '$repo_dir' appears to be corrupt or does not have any commits." >> "$REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# A simple logging function to write timestamped messages
|
||||
log_msg() {
|
||||
local msg="$1"
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $msg" >> "$REPORT"
|
||||
}
|
||||
|
||||
update_repo() {
|
||||
local repo_dir="$1"
|
||||
local repo_number="$2"
|
||||
|
||||
cd "$repo_dir" || return 1
|
||||
log_msg "Processing repository #$repo_number at $repo_dir"
|
||||
|
||||
# Validate the .git folder.
|
||||
if ! is_valid_git_repo "$repo_dir"; then
|
||||
log_msg "Repo #$repo_number: Invalid repository. Skipping."
|
||||
echo "Invalid .git" >> "$SMS_REPORT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check for GitHub remote; try creating if absent.
|
||||
local remote_available=true
|
||||
if ! check_github_remote; then
|
||||
log_msg "Repo #$repo_number: No valid GitHub remote found."
|
||||
remote_available=false
|
||||
if check_wifi; then
|
||||
local repo_name
|
||||
repo_name=$(basename "$repo_dir")
|
||||
create_github_repo "$repo_name" "$repo_dir" "$repo_number"
|
||||
# Recheck the remote
|
||||
if check_github_remote; then
|
||||
remote_available=true
|
||||
else
|
||||
log_msg "Repo #$repo_number: Failed to create remote repository."
|
||||
fi
|
||||
else
|
||||
log_msg "Repo #$repo_number: No WiFi. Committing locally only."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check for any local changes (staged, unstaged, or untracked)
|
||||
if [ -z "$(git status --porcelain)" ]; then
|
||||
log_msg "Repo #$repo_number: No local changes detected."
|
||||
if [ "$remote_available" = false ]; then
|
||||
echo "Repo #$repo_number: NC-GDE" >> "$SMS_REPORT"
|
||||
else
|
||||
echo "Repo #$repo_number: NC-GE" >> "$SMS_REPORT"
|
||||
fi
|
||||
echo "NC-GE"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Stage all changes.
|
||||
git add -A
|
||||
git commit -m "Automated update"
|
||||
|
||||
if check_wifi; then
|
||||
echo "Internet is available. Attempting to push changes..." >> "$REPORT"
|
||||
|
||||
if check_github_remote; then
|
||||
if ! git push origin main; then
|
||||
echo "Failed to push changes in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: PF" >> "$SMS_REPORT"
|
||||
|
||||
# Check if the failure is due to a non-existent repository
|
||||
if git remote show origin | grep -q "ERROR: Repository not found"; then
|
||||
echo "Remote GitHub repository missing for repo $repo_number. Attempting to recreate..." >> "$REPORT"
|
||||
if create_github_repo "$repo_number" "$repo_dir"; then
|
||||
git push -u origin main
|
||||
echo "Changes pushed successfully after recreating GitHub repo for repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: P" >> "$SMS_REPORT"
|
||||
else
|
||||
echo "Failed to create GitHub repository for repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: GCF" >> "$SMS_REPORT"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "Changes pushed successfully in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: P" >> "$SMS_REPORT"
|
||||
fi
|
||||
# Commit changes; override PGP signing requirement for automation.
|
||||
if git -c commit.gpgSign=false commit -m "Automated update"; then
|
||||
log_msg "Repo #$repo_number: Local commit succeeded."
|
||||
else
|
||||
log_msg "Repo #$repo_number: Commit failed (perhaps nothing to commit)."
|
||||
echo "Repo #$repo_number: No commit" >> "$SMS_REPORT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# If a remote exists and WiFi is available, perform a pull with rebase.
|
||||
if [ "$remote_available" = true ] && check_wifi; then
|
||||
log_msg "Repo #$repo_number: Attempting pull --rebase."
|
||||
# Use a timeout (60 seconds in this example) and --no-edit to avoid interactive prompts.
|
||||
if timeout 60s git pull --rebase --no-edit origin main; then
|
||||
log_msg "Repo #$repo_number: Rebase succeeded."
|
||||
else
|
||||
if create_github_repo "$repo_number" "$repo_dir"; then
|
||||
git push -u origin main
|
||||
echo "Changes pushed successfully after creating GitHub repo in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: P" >> "$SMS_REPORT"
|
||||
else
|
||||
echo "Failed to create GitHub repository for repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: GCF" >> "$SMS_REPORT"
|
||||
return 1
|
||||
fi
|
||||
log_msg "Repo #$repo_number: Rebase timed out or encountered conflicts; aborting rebase."
|
||||
git rebase --abort
|
||||
echo "Repo #$repo_number: Rebase Conflict" >> "$SMS_REPORT"
|
||||
echo "MC"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "No internet connection. Changes committed locally in repo $repo_number" >> "$REPORT"
|
||||
echo "Repo $repo_number: LC" >> "$SMS_REPORT"
|
||||
log_msg "Repo #$repo_number: Skipping pull/rebase (no remote or no internet)."
|
||||
fi
|
||||
|
||||
# Push changes if a remote is available and there is internet.
|
||||
if [ "$remote_available" = true ] && check_wifi; then
|
||||
log_msg "Repo #$repo_number: Attempting push."
|
||||
if timeout 60s git push origin main; then
|
||||
log_msg "Repo #$repo_number: Push succeeded."
|
||||
echo "Repo #$repo_number: P" >> "$SMS_REPORT"
|
||||
echo "P"
|
||||
else
|
||||
log_msg "Repo #$repo_number: Push timed out or failed."
|
||||
echo "Repo #$repo_number: PF" >> "$SMS_REPORT"
|
||||
echo "PF"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log_msg "Repo #$repo_number: No internet; changes committed locally."
|
||||
echo "Repo #$repo_number: LC" >> "$SMS_REPORT"
|
||||
echo "LC"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -148,7 +313,7 @@ echo "Starting auto git update script on $(date)" > "$REPORT"
|
||||
echo "(G-R), $(date)" > "$SMS_REPORT"
|
||||
|
||||
# Fetch valid repositories
|
||||
repo_dirs=($(get_repo_dirs)) # Convert to array properly
|
||||
repo_dirs=($(get_repo_dirs))
|
||||
|
||||
# Counters for SMS summary report
|
||||
pushed_count=0
|
||||
@@ -156,18 +321,23 @@ no_change_count=0
|
||||
local_commit_count=0
|
||||
conflict_count=0
|
||||
error_count=0
|
||||
created_count=0
|
||||
no_github_repo_count=0
|
||||
total_repos=${#repo_dirs[@]}
|
||||
|
||||
# Loop through each valid directory
|
||||
repo_number=1
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
echo "Updating repository #$repo_number: $repo_dir"
|
||||
update_repo "$repo_dir" "$repo_number"
|
||||
case "$(tail -n 1 "$SMS_REPORT")" in
|
||||
*P) pushed_count=$((pushed_count + 1)) ;;
|
||||
*NC) no_change_count=$((no_change_count + 1)) ;;
|
||||
*NC*) no_change_count=$((no_change_count + 1)) ;;
|
||||
*LC) local_commit_count=$((local_commit_count + 1)) ;;
|
||||
*MC) conflict_count=$((conflict_count + 1)) ;;
|
||||
*PF) error_count=$((error_count + 1)) ;;
|
||||
*lc-ngr*) no_github_repo_count=$((no_github_repo_count + 1)) ;;
|
||||
*GHS) created_count=$((created_count + 1)) ;;
|
||||
esac
|
||||
repo_number=$((repo_number + 1))
|
||||
done
|
||||
@@ -175,11 +345,12 @@ done
|
||||
# Check if the detailed SMS report exceeds the SMS character limit
|
||||
sms_detailed_content=$(cat "$SMS_REPORT")
|
||||
if [ "${#sms_detailed_content}" -gt "$SMS_CHAR_LIMIT" ]; then
|
||||
sms_summary="Pushed: $pushed_count/$total_repos, No Change: $no_change_count/$total_repos, Locally Committed: $local_commit_count/$total_repos, Conflicts: $conflict_count/$total_repos, Errors: $error_count/$total_repos"
|
||||
DATE=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
echo "Concat output activated"
|
||||
sms_summary="$DATE. Pushed: $pushed_count/$total_repos, No Change: $no_change_count/$total_repos, Locally Committed: $local_commit_count/$total_repos, Conflicts: $conflict_count/$total_repos, Errors: $error_count/$total_repos, No GitHub Repo: $no_github_repo_count/$total_repos, Created: $created_count/$total_repos"
|
||||
echo "$sms_summary" > "$SMS_REPORT"
|
||||
fi
|
||||
|
||||
# Function to send the SMS report
|
||||
send_text() {
|
||||
/usr/bin/msmtp -a default -t <<EOF
|
||||
To: $PHONE_NUMBER
|
||||
@@ -191,14 +362,21 @@ EOF
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to copy the report over SSH
|
||||
copy_report_ssh() {
|
||||
# Ensure SSH_DIR is defined
|
||||
if [ -z "$SSH_DIR" ]; then
|
||||
echo "Error: SSH_DIR is not set. Please define it in your script."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Create the remote directory if it doesn't exist
|
||||
ssh "$SSH_HOST" "mkdir -p $SSH_DIR"
|
||||
scp "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
|
||||
# Transfer the report file to the specified directory
|
||||
rsync -avz --progress "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
return $?
|
||||
}
|
||||
|
||||
# Main logic for text and SSH handling
|
||||
if check_wifi; then
|
||||
echo "WiFi is connected. Attempting to send text message and SSH transfer..."
|
||||
send_text && echo "Text message sent successfully." || echo "Failed to send text message."
|
||||
|
||||
49
install.sh
Executable file
49
install.sh
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Check if running as root
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo "Please run as root (e.g., using sudo)."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Required dependencies
|
||||
REQUIRED_COMMANDS=("git" "nmcli" "msmtp" "ssh" "rsync" "gh")
|
||||
|
||||
# Check for required commands
|
||||
missing_packages=()
|
||||
for cmd in "${REQUIRED_COMMANDS[@]}"; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
echo "$cmd is required but not installed."
|
||||
missing_packages+=("$cmd")
|
||||
fi
|
||||
done
|
||||
|
||||
# If any dependencies are missing, inform the user
|
||||
if [ ${#missing_packages[@]} -gt 0 ]; then
|
||||
echo "The following packages are missing and need to be installed:"
|
||||
echo "${missing_packages[@]}"
|
||||
echo "Please install them using your package manager. For example, on Debian/Ubuntu:"
|
||||
echo "sudo apt update && sudo apt install ${missing_packages[@]}"
|
||||
exit 1
|
||||
else
|
||||
echo "All required dependencies are installed."
|
||||
fi
|
||||
|
||||
# Copy the script to /usr/local/bin
|
||||
INSTALL_PATH="/usr/local/bin/auto_git_update"
|
||||
SOURCE_SCRIPT="auto_git_update.sh"
|
||||
|
||||
if [ ! -f "$SOURCE_SCRIPT" ]; then
|
||||
echo "Error: The source script '$SOURCE_SCRIPT' was not found in the current directory."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Make the script executable
|
||||
chmod +x "$SOURCE_SCRIPT"
|
||||
|
||||
# Copy to /usr/local/bin
|
||||
cp "$SOURCE_SCRIPT" "$INSTALL_PATH" && echo "Script installed successfully at $INSTALL_PATH"
|
||||
|
||||
# Provide usage instructions
|
||||
echo "You can now run the script using the command 'auto_git_update'."
|
||||
|
||||
184
testgit.sh
184
testgit.sh
@@ -1,184 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Check if required commands are available
|
||||
for cmd in git nmcli msmtp ssh scp; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
echo "$cmd is required but not installed. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Configuration
|
||||
TXT_FILE="/home/klein/.config/setup/autogitupdate.txt"
|
||||
PHONE_NUMBER="2673479614@vtext.com" # Verizon SMS gateway
|
||||
EMAIL="kleinpanic@gmail.com"
|
||||
SUBJECT="Git Update Report"
|
||||
REPORT="/tmp/git_update_report.txt" # Full detailed report
|
||||
SUMMARY_REPORT="/tmp/git_update_summary.txt" # Full summary report
|
||||
SMS_REPORT="/tmp/git_update_sms.txt" # Minimal SMS summary
|
||||
SSH_HOST="eulerpi" # Alias for the SSH connection
|
||||
SSH_DIR="/home/klein/reports" # Remote directory to store the report
|
||||
|
||||
# Function to ensure a file exists and has the correct permissions
|
||||
ensure_file() {
|
||||
local file="$1"
|
||||
local permissions="$2"
|
||||
|
||||
if [ ! -f "$file" ]; then
|
||||
touch "$file"
|
||||
chmod "$permissions" "$file"
|
||||
echo "Created $file with permissions $permissions"
|
||||
else
|
||||
chmod "$permissions" "$file"
|
||||
echo "Ensured $file has permissions $permissions"
|
||||
fi
|
||||
}
|
||||
|
||||
# Ensure the report files exist and have the correct permissions
|
||||
ensure_file "$REPORT" 600
|
||||
ensure_file "$SUMMARY_REPORT" 600
|
||||
ensure_file "$SMS_REPORT" 600
|
||||
|
||||
# Function to check WiFi connection and internet availability
|
||||
check_wifi() {
|
||||
nmcli -t -f ACTIVE,SSID dev wifi | grep -q "^yes" && ping -c 1 8.8.8.8 &>/dev/null
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to fetch and clean repo directories from text file
|
||||
get_repo_dirs() {
|
||||
local valid_dirs=()
|
||||
local invalid_dirs=()
|
||||
|
||||
while IFS= read -r repo_dir; do
|
||||
if [ -d "$repo_dir" ] && [ -d "$repo_dir/.git" ]; then
|
||||
valid_dirs+=("$repo_dir")
|
||||
else
|
||||
invalid_dirs+=("$repo_dir")
|
||||
fi
|
||||
done < "$TXT_FILE"
|
||||
|
||||
# Log and remove invalid entries from the file
|
||||
if [ ${#invalid_dirs[@]} -gt 0 ]; then
|
||||
for dir in "${invalid_dirs[@]}"; do
|
||||
echo "$dir does not exist or is not a valid git repository. Removing from $TXT_FILE" >> "$REPORT"
|
||||
echo "$dir does not exist or is not a valid git repository. Removing from $TXT_FILE"
|
||||
sed -i "\|$dir|d" "$TXT_FILE"
|
||||
done
|
||||
fi
|
||||
|
||||
# Return valid directories
|
||||
echo "${valid_dirs[@]}"
|
||||
}
|
||||
|
||||
# Function to update, commit, and (if connected) push the changes
|
||||
update_repo() {
|
||||
local repo_dir="$1"
|
||||
local repo_number="$2"
|
||||
cd "$repo_dir" || return
|
||||
|
||||
echo "Processing repository: $repo_dir" >> "$REPORT"
|
||||
echo "Processing repository: $repo_dir"
|
||||
|
||||
git fetch origin
|
||||
if ! git merge origin/main --no-commit --no-ff; then
|
||||
echo "Merge conflict detected in $repo_dir" >> "$REPORT"
|
||||
echo "Merge conflict in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, C" >> "$SMS_REPORT"
|
||||
git merge --abort
|
||||
return 1
|
||||
fi
|
||||
|
||||
if git diff-index --quiet HEAD --; then
|
||||
echo "No changes detected in $repo_dir" >> "$REPORT"
|
||||
echo "No changes in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, NC" >> "$SMS_REPORT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
git add -A
|
||||
git commit -m "Automated update"
|
||||
|
||||
# Check if connected to the internet before pushing
|
||||
if check_wifi; then
|
||||
echo "Internet is available. Attempting to push changes..." >> "$REPORT"
|
||||
if ! git push origin main; then
|
||||
echo "Failed to push changes in $repo_dir" >> "$REPORT"
|
||||
echo "Failed to push changes in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, ERR" >> "$SMS_REPORT"
|
||||
return 1
|
||||
else
|
||||
echo "Changes pushed successfully in $repo_dir" >> "$REPORT"
|
||||
echo "Changes pushed in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, P" >> "$SMS_REPORT"
|
||||
fi
|
||||
else
|
||||
echo "No internet connection. Changes committed locally in $repo_dir" >> "$REPORT"
|
||||
echo "Changes committed locally in $repo_dir" >> "$SUMMARY_REPORT"
|
||||
echo "repo: $repo_number, LC" >> "$SMS_REPORT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Start of the script
|
||||
echo "Starting auto git update script on $(date)" > "$REPORT"
|
||||
echo "Git Update Summary: $(date)" > "$SUMMARY_REPORT"
|
||||
echo "(GIT Report), $(date)" > "$SMS_REPORT"
|
||||
|
||||
# Fetch valid repositories
|
||||
repo_dirs=($(get_repo_dirs)) # Convert to array properly
|
||||
repo_number=1
|
||||
|
||||
# Loop through each valid directory
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
update_repo "$repo_dir" "$repo_number"
|
||||
repo_number=$((repo_number + 1))
|
||||
done
|
||||
|
||||
# Function to send the report via text message
|
||||
send_text() {
|
||||
/usr/bin/msmtp -a default -t <<EOF
|
||||
To: $PHONE_NUMBER
|
||||
From: $EMAIL
|
||||
Subject: $SUBJECT
|
||||
|
||||
$(cat $SMS_REPORT)
|
||||
EOF
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to copy the report over SSH
|
||||
copy_report_ssh() {
|
||||
ssh "$SSH_HOST" "mkdir -p $SSH_DIR"
|
||||
scp "$SUMMARY_REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
scp "$REPORT" "$SSH_HOST:$SSH_DIR/"
|
||||
return $?
|
||||
}
|
||||
|
||||
# Main logic for text, SSH handling, and local storage
|
||||
if check_wifi; then
|
||||
echo "WiFi is connected. Attempting to send text message and SSH transfer..."
|
||||
send_text && echo "Text message sent successfully." || echo "Failed to send text message."
|
||||
copy_report_ssh && echo "Reports successfully copied over SSH." || echo "Failed to copy report over SSH."
|
||||
else
|
||||
echo "No WiFi connection detected. Waiting for connection to push changes..."
|
||||
while ! check_wifi; do
|
||||
sleep 60 # Wait for a minute before checking again
|
||||
done
|
||||
|
||||
echo "WiFi is now connected. Pushing changes..."
|
||||
for repo_dir in "${repo_dirs[@]}"; do
|
||||
cd "$repo_dir" || continue
|
||||
git push origin main
|
||||
done
|
||||
|
||||
send_text && echo "Text message sent successfully." || echo "Failed to send text message."
|
||||
copy_report_ssh && echo "Reports successfully copied over SSH." || echo "Failed to copy report over SSH."
|
||||
fi
|
||||
|
||||
# Clean up local reports if SSH was successful
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo "SSH transfer succeeded. Deleting local reports."
|
||||
rm -f "$REPORT" "$SUMMARY_REPORT"
|
||||
else
|
||||
echo "SSH transfer failed. Keeping local reports for troubleshooting."
|
||||
fi
|
||||
Reference in New Issue
Block a user