diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d2a4182..a2aa71f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,7 +21,10 @@ jobs: go-version: '1.23.0' - name: Build auto-commit binary - run: go build -o ./bin/auto-commit . + run: | + sudo apt-get update && sudo apt-get install upx -y + go build -ldflags="-s -w" -trimpath -o ./bin/auto-commit . + upx --best --lzma ./bin/auto-commit - name: Set up Git run: | @@ -204,6 +207,8 @@ jobs: ./bin/auto-commit ./scripts/install-linux-auto-commit.sh ./scripts/install-windows-auto-commit.ps1 + ./scripts/update-windows-auto-commit.ps1 + ./scripts/update-linux-auto-commit.sh - name: Create new branch for next version run: | diff --git a/Makefile b/Makefile index ddb8e04..a6a0421 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,11 @@ buildrelease: @go build -ldflags="-s -w" -trimpath -o bin/auto-commit . @upx.exe --best --lzma bin/auto-commit +buildrelease-update: + @echo "Running release build update..." + @go build -ldflags="-s -w" -trimpath -o bin/auto-commit.update . + @upx.exe --best --lzma bin/auto-commit.update + test: @go test -v ./... diff --git a/scripts/update-linux-auto-commit.sh b/scripts/update-linux-auto-commit.sh new file mode 100644 index 0000000..83dc5a6 --- /dev/null +++ b/scripts/update-linux-auto-commit.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +set -e + +GIT_ROOT=$(git rev-parse --show-toplevel) +cd "$GIT_ROOT" + +HOOKS_DIR=".git/hooks" +HOOK_NAME="auto-commit" +HOOK_PATH="$HOOKS_DIR/$HOOK_NAME" +VERSION_FILE="$HOOKS_DIR/auto-commit.version.txt" + +if pgrep -f "$HOOK_PATH" > /dev/null; then + pkill -f "$HOOK_PATH" + sleep 2 +fi + +VERSION_URL="https://api.github.com/repos/thefuture-industries/git-auto-commit/releases/latest" +TAG=$(curl -s "$VERSION_URL" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') +URL="https://github.com/thefuture-industries/git-auto-commit/releases/download/$TAG/auto-commit" + +if [ -f "$VERSION_FILE" ]; then + CURRENT_TAG=$(cat "$VERSION_FILE" | tr -d ' \n\r') + if [ "$CURRENT_TAG" = "$TAG" ]; then + echo -e "\033[33m[!] you have the latest version installed $TAG\033[0m" + exit 0 + fi +fi + +download_with_progress() { + local url="$1" + local output="$2" + local bar_width=60 + + content_length=$(curl -sI "$url" | grep -i Content-Length | awk '{print $2}' | tr -d '\r') + [ -z "$content_length" ] && content_length=0 + + echo -n "auto-commit update [" + for ((i=0; i "$VERSION_FILE" + +git config --local alias.auto '!./.git/hooks/auto-commit' +echo "successful upgrade to version $TAG" diff --git a/scripts/update-windows-auto-commit.ps1 b/scripts/update-windows-auto-commit.ps1 new file mode 100644 index 0000000..b063bfb --- /dev/null +++ b/scripts/update-windows-auto-commit.ps1 @@ -0,0 +1,70 @@ +# auto-commit.psm1 +$ErrorActionPreference = "Stop" + +$gitRoot = & git rev-parse --show-toplevel +Set-Location $gitRoot + +$proc = Get-Process "auto-commit" -ErrorAction SilentlyContinue +if ($proc) { + $proc | Stop-Process -Force + Start-Sleep -Seconds 2 +} + +$versionUrl = "https://api.github.com/repos/thefuture-industries/git-auto-commit/releases/latest" +$tag = (Invoke-RestMethod -Uri $versionUrl -UseBasicParsing).tag_name + +$Url = "https://github.com/thefuture-industries/git-auto-commit/releases/download/$tag/auto-commit" + +$HookName = "auto-commit" +$hookPath = Join-Path -Path ".git/hooks" -ChildPath $HookName +$versionFile = Join-Path -Path ".git/hooks" -ChildPath "auto-commit.version.txt" + +if (Test-Path $versionFile) { + $currentTag = Get-Content $versionFile | ForEach-Object { $_.Trim() } + if ($currentTag -eq $tag) { + Write-Host "[!] you have the latest version installed $tag" -ForegroundColor Yellow + exit 0 + } +} + +function Download-WithProgress { + param ( + [string]$url, + [string]$output + ) + + $req = [System.Net.HttpWebRequest]::Create($url) + $req.UserAgent = "git-auto-commit" + $resp = $req.GetResponse() + $total = $resp.ContentLength + $stream = $resp.GetResponseStream() + $outStream = [System.IO.File]::Open($output, [System.IO.FileMode]::Create) + + $buffer = New-Object byte[] 8192 + $read = 0 + $downloaded = 0 + $barWidth = 50 + + while (($read = $stream.Read($buffer, 0, $buffer.Length)) -gt 0) { + $outStream.Write($buffer, 0, $read) + $downloaded += $read + $percent = [math]::Round(($downloaded / $total) * 100) + $filled = [math]::Floor($barWidth * $percent / 100) + $empty = $barWidth - $filled + $bar = ('*' * $filled) + ('.' * $empty) + Write-Host -NoNewline "`rauto-commit update [$bar] $percent% " + } + + $outStream.Close() + $stream.Close() + Write-Host "" +} + +Download-WithProgress -url $Url -output $hookPath + +Set-Content -Path $versionFile -Value $tag + +git config --local alias.auto '!./.git/hooks/auto-commit' + +Write-Host "successful upgrade to version $tag" +exit 0 diff --git a/update.go b/update.go index 384f15a..06e7c50 100644 --- a/update.go +++ b/update.go @@ -4,7 +4,9 @@ import ( "fmt" "net/http" "os" + "os/exec" "path/filepath" + "runtime" "strings" ) @@ -23,7 +25,7 @@ func AutoCommitUpdate() { return } - resp, err := http.Get(GITHUB_REPO_URL + "/releases/latest") + resp, err := http.Get(GITHUB_API_REPO_URL + "/releases/latest") if err != nil { ErrorLogger(fmt.Errorf("could not check latest version: %w", err)) return @@ -39,31 +41,31 @@ func AutoCommitUpdate() { } if strings.TrimSpace(string(version)) == strings.TrimSpace(data.TagName) { - fmt.Printf("\033[92myou have the latest version installed %s\033[0m\n", strings.TrimSpace(data.TagName)) + fmt.Printf("\033[33m[!] you have the latest version installed %s\033[0m\n", strings.TrimSpace(data.TagName)) return } fmt.Printf("updating to version %s...\n", strings.TrimSpace(data.TagName)) - binaryURL := GITHUB_REPO_URL + "/releases/download/" + strings.TrimSpace(data.TagName) + "/" + BINARY_AUTO_COMMIT - destPath := filepath.Join(root, ".git", "hooks", "auto-commit") + // ps1 || bash + if runtime.GOOS == "windows" { + script := fmt.Sprintf("%s/scripts/update-windows-auto-commit.ps1", root) + cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr - if err := DownloadBinAutoCommit(binaryURL, destPath); err != nil { - ErrorLogger(fmt.Errorf("failed to download new binary: %w", err)) - return - } - - err = os.Chmod(destPath, 0755) - if err != nil { - ErrorLogger(fmt.Errorf("failed to set executable permission: %w", err)) - return + if err := cmd.Run(); err != nil { + ErrorLogger(fmt.Errorf("failed to run update script: %w", err)) + return + } + } else { + script := fmt.Sprintf("%s/scripts/update-linux-auto-commit.sh", root) + cmd := exec.Command("bash", script) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + ErrorLogger(fmt.Errorf("failed to run bash script: %w", err)) + return + } } - - err = os.WriteFile(versionFile, []byte(strings.TrimSpace(data.TagName)), 0644) - if err != nil { - ErrorLogger(fmt.Errorf("failed to update version file: %w", err)) - return - } - - fmt.Println("successful upgrade to version ", strings.TrimSpace(data.TagName)) } diff --git a/version.go b/version.go index 9342f4d..0c40302 100644 --- a/version.go +++ b/version.go @@ -23,7 +23,7 @@ func GetVersion() { return } - fmt.Println("[git auto-commit] current version:", string(version)) + fmt.Println("[git auto-commit] current version:", strings.TrimSpace(string(version))) resp, err := http.Get(GITHUB_API_REPO_URL + "/releases/latest") if err != nil { @@ -41,7 +41,7 @@ func GetVersion() { } if strings.TrimSpace(string(version)) != strings.TrimSpace(data.TagName) { - fmt.Printf("\033[94ma new version is available: %s\033[0m\n", strings.TrimSpace(data.TagName)) - fmt.Printf("\033[92mplease update! 'git auto -u'\033[0m\n") + fmt.Printf("\033[33m[!] a new version is available: %s\033[0m\n", strings.TrimSpace(data.TagName)) + fmt.Printf("\033[33m[!] please update! 'git auto -u'\033[0m\n") } }