Skip to content
Merged
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
7 changes: 6 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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: |
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...

Expand Down
65 changes: 65 additions & 0 deletions scripts/update-linux-auto-commit.sh
Original file line number Diff line number Diff line change
@@ -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<bar_width; i++)); do echo -n "."; done
echo -n "] 0%"

curl -L "$url" --output "$output" --progress-bar | \
awk -v bar_width=$bar_width -v total=$content_length '
BEGIN { done=0 }
{
if (match($0, /([0-9]+)%/)) {
percent = substr($0, RSTART, RLENGTH-1)
filled = int(bar_width * percent / 100)
empty = bar_width - filled
bar = sprintf("%s%s", sprintf("%*s", filled, "*"), sprintf("%*s", empty, "."))
printf "\rauto-commit update [%s] %d%%", bar, percent
fflush()
}
}
END { print "" }
'
}

download_with_progress "$URL" "$HOOK_PATH"
chmod +x "$HOOK_PATH"

echo "$TAG" > "$VERSION_FILE"

git config --local alias.auto '!./.git/hooks/auto-commit'
echo "successful upgrade to version $TAG"
70 changes: 70 additions & 0 deletions scripts/update-windows-auto-commit.ps1
Original file line number Diff line number Diff line change
@@ -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
44 changes: 23 additions & 21 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

Expand All @@ -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
Expand All @@ -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))
}
6 changes: 3 additions & 3 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
}