summaryrefslogtreecommitdiff
path: root/.local/bin/emacs-update-packages
blob: ecf5267aa5b6421f4d550385bb07ff6a48255c8e (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
#!/bin/bash

# Backup package-user-dir, update packages, compute diffs.
#
# Might be worth re-implementing as advice for package.el commands:
# would avoid the "sacrificial" Emacs sessions…

set -euo pipefail
test "${DEBUG:-}" && set -x

cd ~/.config/emacs

test -L elpa || {
    echo >&2 "'elpa' directory must be a symlink."
    exit 1
}

OLDELPADIR=$(readlink elpa)
NEWELPADIR=.elpa.$(date +%F-%T)

read-packages ()
{
    local -n pkgarray=$1

    local pkgel name version
    for pkgel in elpa/*/*-pkg.el
    do
        # Extract NAME & VERSION out of elpa/NAME-VERSION/NAME-pkg.el.
        pkgel=${pkgel#elpa/}

        name=${pkgel}
        name=${name##*/}
        name=${name%-pkg.el}

        version=${pkgel}
        version=${version%/${name}-pkg.el}
        version=${version#${name}-}

        pkgarray[${name}]=${version}
    done
}

enum-packages ()
{
    IFS=$'\n' eval '
        echo "${!OLDPACKAGES[*]}"
        echo "${!NEWPACKAGES[*]}"
    ' | sort -u
}

compare-packages ()
{
    local pkg oldv newv

    while read pkg
    do
        oldv=${OLDPACKAGES[${pkg}]:-}
        newv=${NEWPACKAGES[${pkg}]:-}

        test "${oldv}" = "${newv}" &&
            # No changes, skip.
            continue

        test "${oldv}" -a "${newv}" &&
            # Make a patch for later review.
            (
                set +e
                diff > ${NEWELPADIR}/update-${pkg}.patch \
                    -ru ${OLDELPADIR}/${pkg}-${oldv} ${NEWELPADIR}/${pkg}-${newv}


                diffstatus=$?
                case ${diffstatus}
                in
                    0|1) exit 0 ;;
                    *)   exit ${diffstatus} ;;
                esac
            )

        echo -e "${pkg}\t${oldv:-[NEW]}\t${newv:-[REMOVED]}"
    done
}

cp -a ${OLDELPADIR} ${NEWELPADIR}

declare -A OLDPACKAGES NEWPACKAGES

read-packages OLDPACKAGES
ln -fsT ${NEWELPADIR} elpa

emacs -f package-upgrade-all -f package-autoremove
read-packages NEWPACKAGES

enum-packages | compare-packages | column -ts$'\t' |
    tee ${NEWELPADIR}/update.sum