summarylogtreecommitdiffstats
path: root/update_aur.sh
blob: 91e78cdd3e08a967a165d14fe040b79f2eccacbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash

# AUR Package Update Script
# Automates the process of updating AUR packages

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Logging function
log() {
    echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

# Usage information
usage() {
    cat << EOF
Usage: $0 <new_version>

This script automates the AUR package update process.

Arguments:
    new_version    The new version to update to

Example:
    $0 1.2.3

The script will:
1. Update pkgver in PKGBUILD
2. Reset pkgrel to 1
3. Update checksums with updpkgsums
4. Regenerate .SRCINFO
5. Test build the package
6. Show changes and ask for confirmation
7. Commit and push to AUR

EOF
}

# Check if we're in an AUR git repository
validate_aur_repo() {
    if [[ ! -d .git ]]; then
        error "Not in a git repository"
        exit 1
    fi

    if [[ ! -f PKGBUILD ]]; then
        error "PKGBUILD not found in current directory"
        exit 1
    fi

    local remote_url=$(git remote get-url origin 2>/dev/null || echo "")
    if [[ ! "$remote_url" =~ aur\.archlinux\.org ]]; then
        warning "This doesn't appear to be an AUR repository"
        read -p "Continue anyway? (y/N): " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            exit 1
        fi
    fi
}

# Update pkgver in PKGBUILD
update_pkgver() {
    local new_version="$1"
    log "Updating pkgver to $new_version"

    if ! grep -q "^pkgver=" PKGBUILD; then
        error "pkgver not found in PKGBUILD"
        exit 1
    fi

    sed -i "s/^pkgver=.*/pkgver=$new_version/" PKGBUILD
    sed -i "s/^pkgrel=.*/pkgrel=1/" PKGBUILD
}

# Update checksums
update_checksums() {
    log "Updating checksums with updpkgsums"
    if command -v updpkgsums >/dev/null 2>&1; then
        updpkgsums
    else
        error "updpkgsums not found. Please install pkgbuild-introspection"
        exit 1
    fi
}

# Regenerate .SRCINFO
regenerate_srcinfo() {
    log "Regenerating .SRCINFO"
    makepkg --printsrcinfo > .SRCINFO
}

# Test build
test_build() {
    log "Testing package build"
    makepkg -sr --noconfirm
}

# Show changes
show_changes() {
    info "Changes made:"
    git diff --color=always
}

# Cleanup function
cleanup() {
    if [[ -f *.pkg.tar.* ]]; then
        log "Cleaning up built packages"
        rm -f *.pkg.tar.*
    fi
}

# Trap for cleanup on exit
trap cleanup EXIT

# Ask for confirmation
confirm() {
    local message="$1"
    read -p "$message (y/N): " -n 1 -r
    echo
    [[ $REPLY =~ ^[Yy]$ ]]
}

# Commit changes
commit_changes() {
    local version="$1"
    local commit_message="Update to version $version"

    git add PKGBUILD .SRCINFO
    git commit -m "$commit_message"
    log "Changes committed with message: '$commit_message'"
}

# Push to AUR
push_to_aur() {
    log "Pushing changes to AUR"
    git push origin master
}

# Main function
main() {
    # Check arguments
    if [[ $# -ne 1 ]]; then
        usage
        exit 1
    fi

    local new_version="$1"

    # Validate version format (basic check)
    if [[ ! "$new_version" =~ ^[0-9]+(\.[0-9]+)*([a-zA-Z0-9._-]*)?$ ]]; then
        error "Invalid version format: $new_version"
        exit 1
    fi

    log "Starting AUR package update to version $new_version"

    # Validate environment
    validate_aur_repo

    # Check for uncommitted changes
    if [[ -n $(git status --porcelain) ]]; then
        warning "You have uncommitted changes"
        if ! confirm "Continue anyway?"; then
            exit 1
        fi
    fi

    # Update package
    update_pkgver "$new_version"
    update_checksums
    regenerate_srcinfo

    # Test build
    if confirm "Test build the package?"; then
        test_build
        log "Package built successfully"
    fi

    # Show changes
    show_changes

    # Confirm changes
    if ! confirm "Commit these changes?"; then
        log "Update cancelled"
        exit 1
    fi

    # Commit
    commit_changes "$new_version"

    # Push to AUR
    if confirm "Push changes to AUR?"; then
        push_to_aur
        log "Package successfully updated to version $new_version"
    else
        info "Changes committed locally but not pushed to AUR"
        info "Run 'git push origin master' when ready to publish"
    fi
}

# Run main function with all arguments
main "$@"