81 lines
2.6 KiB
PowerShell
81 lines
2.6 KiB
PowerShell
# Git Auto Commit and Push Script
|
|
# Auto save, commit and push to GitHub every hour
|
|
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# Switch to project directory (parent of this scripts directory)
|
|
$projectPath = Resolve-Path (Join-Path $PSScriptRoot "..")
|
|
Set-Location $projectPath
|
|
|
|
function Ensure-GitOnPath {
|
|
if (Get-Command git -ErrorAction SilentlyContinue) {
|
|
return
|
|
}
|
|
|
|
$candidateDirs = @(
|
|
"C:\Program Files\Git\cmd",
|
|
"C:\Program Files\Git\bin",
|
|
"C:\Program Files (x86)\Git\cmd",
|
|
"C:\Program Files (x86)\Git\bin",
|
|
(Join-Path $env:LOCALAPPDATA "Programs\Git\cmd"),
|
|
(Join-Path $env:LOCALAPPDATA "Programs\Git\bin"),
|
|
"C:\ProgramData\chocolatey\bin"
|
|
)
|
|
|
|
foreach ($dir in $candidateDirs) {
|
|
if ($dir -and (Test-Path -LiteralPath (Join-Path $dir "git.exe"))) {
|
|
$env:Path = "$dir;$env:Path"
|
|
if (Get-Command git -ErrorAction SilentlyContinue) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
throw "Git executable not found. Please install Git or add git.exe to PATH."
|
|
}
|
|
|
|
Ensure-GitOnPath
|
|
|
|
# Get current timestamp
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "Auto Commit Script Running: $timestamp" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
|
|
try {
|
|
# Check for changes
|
|
$status = git status --porcelain
|
|
|
|
if ($status) {
|
|
Write-Host "Changes detected, starting commit..." -ForegroundColor Yellow
|
|
|
|
# Add all modified files
|
|
git add -A
|
|
|
|
# Create commit message with timestamp
|
|
$commitMessage = "Auto commit: $timestamp - Hourly auto save"
|
|
git commit -m $commitMessage
|
|
|
|
Write-Host "Commit successful: $commitMessage" -ForegroundColor Green
|
|
|
|
# Push to GitHub
|
|
Write-Host "Pushing to GitHub..." -ForegroundColor Yellow
|
|
git push origin main
|
|
|
|
Write-Host "Push successful!" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "No changes detected, skipping commit." -ForegroundColor Gray
|
|
}
|
|
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "Script execution completed" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
|
|
} catch {
|
|
Write-Host "Error: $_" -ForegroundColor Red
|
|
Write-Host "Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red
|
|
exit 1
|
|
}
|