Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions BRANCH_MANAGEMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Git Branch Management for Automation Scripts

This repository contains automation scripts that were previously hardcoded to push to the `main` branch. This created issues when:

1. The `main` branch was deleted from the remote (`git push origin --delete main`)
2. Working on feature branches where pushing to `main` would fail
3. Repository structure changes that affect branch availability

## Solution

The solution implements a `git-helper.sh` library that provides:

### Safe Push Function

```bash
safe_push [branch_name]
```

- **Detects branch existence**: Checks if target branch exists on remote before attempting push
- **Provides fallback options**: Suggests alternatives when target branch is missing
- **Branch mismatch warnings**: Alerts when current branch differs from target
- **Error handling**: Graceful failure with helpful error messages

### Updated Scripts

The following scripts have been updated to use the safe push functionality:

- `update-main.sh` - Main update automation script
- `createWeek.sh` - Weekly content creation script
- `move-ghpages-to-main.sh` - Content migration script
- `update-be128.sh` - BE128 course content update script

### Usage

All scripts now automatically:

1. Load the `git-helper.sh` library
2. Use `safe_push` instead of direct `git push origin main`
3. Provide fallback to current branch if main branch is unavailable
4. Display informative messages about branch operations

### Handling Main Branch Deletion

When `git push origin --delete main` is executed:

1. **Before**: Scripts would fail with `error: src refspec main does not match any`
2. **After**: Scripts detect missing main branch and offer alternatives:
- Push to current branch instead
- Create new main branch
- Skip push operation

### Backward Compatibility

- Scripts work normally when main branch exists
- Graceful degradation when git-helper.sh is not available
- No changes to script interfaces or parameters

### Testing

The solution has been tested for:

- ✅ Normal operation with existing main branch
- ✅ Operation when main branch is deleted
- ✅ Branch mismatch scenarios
- ✅ Error handling and user feedback
- ✅ Fallback to current branch operations
17 changes: 16 additions & 1 deletion createWeek.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
set -euo pipefail

# Load git helper functions for safe branch operations
source "$(dirname "$0")/git-helper.sh" 2>/dev/null || {
echo -e "\e[33m⚠️ git-helper.sh not found, using basic git operations\e[0m"
}

WEEK=$1
COURSE_DIR="BE128"
WEEK_DIR="$COURSE_DIR/${WEEK}.Hafta"
Expand Down Expand Up @@ -46,6 +51,16 @@ echo "$(date '+%Y-%m-%d %H:%M:%S') - ${WEEK}.Hafta klasörü ve index.html eklen
git add "$WEEK_DIR" README.md update.log
git commit -m "$(date '+%Y-%m-%d') - ${WEEK}.Hafta: klasör, index.html, README.md ve update.log güncellendi"
git pull --rebase
git push origin main

# Use safe_push function if available, fallback to direct push
if command -v safe_push >/dev/null 2>&1; then
safe_push main || {
echo -e "\e[33m⚠️ Failed to push to main, trying current branch...\e[0m"
current_branch=$(git branch --show-current)
safe_push "$current_branch"
}
else
git push origin main
fi

echo -e "\e[32m✅ ${WEEK}.Hafta klasörü başarıyla oluşturuldu ve GitHub'a gönderildi!\e[0m"
73 changes: 73 additions & 0 deletions git-helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
# Git Helper Library for handling branch operations safely
# This helps scripts work correctly even if main branch is deleted

# Function to get the current branch name
get_current_branch() {
git branch --show-current 2>/dev/null || echo ""
}

# Function to check if a branch exists locally
branch_exists_local() {
local branch_name="$1"
git show-ref --verify --quiet "refs/heads/$branch_name" 2>/dev/null
}

# Function to check if a branch exists on remote
branch_exists_remote() {
local branch_name="$1"
git ls-remote --heads origin "$branch_name" 2>/dev/null | grep -q "refs/heads/$branch_name"
}

# Function to safely push to a branch
safe_push() {
local target_branch="${1:-main}"
local current_branch
current_branch=$(get_current_branch)

echo -e "\e[36m🔄 Attempting to push to $target_branch...\e[0m"

# Check if target branch exists on remote
if ! branch_exists_remote "$target_branch"; then
echo -e "\e[31m❌ Remote branch '$target_branch' does not exist!\e[0m"
echo -e "\e[33m💡 You may need to create the branch first or push to current branch '$current_branch'\e[0m"
return 1
fi

# If we're not on the target branch, warn user
if [ "$current_branch" != "$target_branch" ]; then
echo -e "\e[33m⚠️ Warning: Current branch '$current_branch' differs from target '$target_branch'\e[0m"
echo -e "\e[33m🔄 Pushing current branch to remote $target_branch...\e[0m"
fi

# Attempt the push with error handling
if git push origin "HEAD:$target_branch"; then
echo -e "\e[32m✅ Successfully pushed to $target_branch!\e[0m"
return 0
else
echo -e "\e[31m❌ Failed to push to $target_branch\e[0m"
echo -e "\e[33m💡 Consider pushing to current branch instead: git push origin $current_branch\e[0m"
return 1
fi
}

# Function to handle the case where main branch might be deleted
handle_missing_main() {
echo -e "\e[31m❌ Main branch appears to be missing!\e[0m"
echo -e "\e[36m🔄 Checking available options...\e[0m"

local current_branch
current_branch=$(get_current_branch)

echo -e "\e[33m📝 Available options:\e[0m"
echo -e "\e[33m 1. Push to current branch: $current_branch\e[0m"
echo -e "\e[33m 2. Create and push new main branch\e[0m"
echo -e "\e[33m 3. Skip push operation\e[0m"

# For automation, we'll push to current branch as safest option
echo -e "\e[36m🔄 Auto-selecting option 1: Push to current branch\e[0m"
safe_push "$current_branch"
}

# Export functions for use in other scripts
export -f get_current_branch branch_exists_local branch_exists_remote safe_push handle_missing_main
16 changes: 15 additions & 1 deletion move-ghpages-to-main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ set -euo pipefail

echo -e "\e[36m🔄 gh-pages dalından main dalına geçiş başlatılıyor...\e[0m"

# Load git helper functions for safe branch operations
source "$(dirname "$0")/git-helper.sh" 2>/dev/null || {
echo -e "\e[33m⚠️ git-helper.sh not found, using basic git operations\e[0m"
}

# Proje klasörüne git
cd ~/Fullstack-Developer-Repo || { echo -e "\e[31m❌ Klasör bulunamadı\e[0m"; exit 1; }

Expand Down Expand Up @@ -33,7 +38,16 @@ fi
git add BE128
COMMIT_DATE=$(date '+%Y-%m-%d %H:%M')
git commit -m "BE128 klasörü gh-pages dalından main dalına taşındı - $COMMIT_DATE"
git push origin main
# Use safe_push function if available, fallback to direct push
if command -v safe_push >/dev/null 2>&1; then
safe_push main || {
echo -e "\e[33m⚠️ Failed to push to main, trying current branch...\e[0m"
current_branch=$(git branch --show-current)
safe_push "$current_branch"
}
else
git push origin main
fi

echo -e "\e[32m✅ Taşıma işlemi tamamlandı ve GitHub'a gönderildi.\e[0m"
echo -e "\e[33m🌐 GitHub Pages ayarlarını main dalına göre güncellemeyi unutma!\e[0m"
17 changes: 16 additions & 1 deletion update-be128.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
set -euo pipefail

# Load git helper functions for safe branch operations
source "$(dirname "$0")/git-helper.sh" 2>/dev/null || {
echo -e "\e[33m⚠️ git-helper.sh not found, using basic git operations\e[0m"
}

cd ~/Fullstack-Developer-Repo || { echo -e "\e[31m❌ Klasör bulunamadı\e[0m"; exit 1; }

if git status | grep -q "BE128"; then
Expand Down Expand Up @@ -41,7 +46,17 @@ if git status | grep -q "BE128"; then

# Commit ve push
git commit -m "BE128 tablo ve log güncellemesi - $UPDATE_DATE"
git push origin main

# Use safe_push function if available, fallback to direct push
if command -v safe_push >/dev/null 2>&1; then
safe_push main || {
echo -e "\e[33m⚠️ Failed to push to main, trying current branch...\e[0m"
current_branch=$(git branch --show-current)
safe_push "$current_branch"
}
else
git push origin main
fi

echo -e "\a"
echo -e "\e[32m✅ index.html başarıyla güncellendi!\e[0m"
Expand Down
32 changes: 28 additions & 4 deletions update-main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ set -euo pipefail
# cd /c/Users/Monster/Fullstack-Developer-Repo || { echo -e "\e[31m❌ Klasör bulunamadı\e[0m"; exit 1; }
# Yukarıdaki satırı kaldırdık, script bulunduğu dizinde çalışacak

# Load git helper functions for safe branch operations
source "$(dirname "$0")/git-helper.sh" 2>/dev/null || {
echo -e "\e[33m⚠️ git-helper.sh not found, using basic git operations\e[0m"
}

if git status BE128 | grep -q "modified\|new file\|deleted"; then
COMMIT_DATE=$(date '+%Y-%m-%d %H:%M')
README="BE128/README.md"
Expand Down Expand Up @@ -159,10 +164,19 @@ if git status BE128 | grep -q "modified\|new file\|deleted"; then
# 7️⃣ Commit & Push
git commit -m "📦 BE128 Güncellemesi - $COMMIT_DATE"

if git push origin main; then
echo -e "\e[32m✅ Git push başarılı!\e[0m"
# Use safe_push function if available, fallback to direct push
if command -v safe_push >/dev/null 2>&1; then
safe_push main || {
echo -e "\e[33m⚠️ Failed to push to main, trying current branch...\e[0m"
current_branch=$(git branch --show-current)
safe_push "$current_branch"
}
else
echo -e "\e[31m❌ Git push başarısız. Lütfen remote çatışmalarını kontrol et.\e[0m"
if git push origin main; then
echo -e "\e[32m✅ Git push başarılı!\e[0m"
else
echo -e "\e[31m❌ Git push başarısız. Lütfen remote çatışmalarını kontrol et.\e[0m"
fi
fi

else
Expand All @@ -171,4 +185,14 @@ fi

git add .
git commit -m "README ve otomasyon script güncellendi"
git push origin main

# Use safe_push function if available, fallback to direct push
if command -v safe_push >/dev/null 2>&1; then
safe_push main || {
echo -e "\e[33m⚠️ Failed to push to main, trying current branch...\e[0m"
current_branch=$(git branch --show-current)
safe_push "$current_branch"
}
else
git push origin main
fi