Debian Codegraph @

post-install.d/50vcs-commit

raw
1 #!/bin/sh
2 set -e
3
4 pl="/var/cache/etckeeper/packagelist"
5
6 # Find the pid whose parent is the command line that the user ran.
7 # Depending on how the highlevel package manager starts etckeeper,
8 # this can be either the parent process, or the parent of the parent
9 # process.
10 case $HIGHLEVEL_PACKAGE_MANAGER in
11 pacman|zypper)
12 ETCKEEPER_PID="${PPID}"
13 ;;
14 apt|yum|dnf)
15 # (Only procps ps is currently supported, others will fail,
16 # so this may end up empty.)
17 ETCKEEPER_PID=$( ps --no-headers -o ppid "${PPID}" 2>/dev/null | sed 's/^ *//' )
18 ;;
19 esac
20
21 if ! [ -z "${ETCKEEPER_PID}" ]; then
22 # Find the parent to get the command line.
23 # (Only procps ps is currently supported, others will fail,
24 # so this may end up empty.)
25 ETCKEEPER_PPID=$( ps --no-headers -o ppid "${ETCKEEPER_PID}" 2>/dev/null | sed 's/^ *//' )
26 if ! [ -z "${ETCKEEPER_PPID}" ]; then
27 ETCKEEPER_PARENT_COMMAND_LINE=$( ps --no-headers -o args "${ETCKEEPER_PPID}" )
28 fi
29 fi
30
31 get_changes () {
32 if [ "$VCS" = git ]; then
33 git diff --stat | grep '|' | cut -d'|' -f1 | cut -b2-
34 git ls-files --exclude-standard --others
35 fi
36 if [ "$VCS" = hg ]; then
37 hg status --no-status
38 fi
39 if [ "$VCS" = bzr ]; then
40 bzr status -S | cut -b5-
41 fi
42 if [ "$VCS" = darcs ]; then
43 # ignore ' file -> file' lines for moved files
44 # trim ' -M +N rP' from change summary
45 darcs whatsnew --summary | grep -v '^ .* -> ' | cut -d' ' -f2- | sed 's/ [-+r][0-9]\+//g;s/^\.\///'
46 # lines beginning with 'a' show unversioned files
47 darcs whatsnew --look-for-adds --boring --summary | grep '^a' | cut -d' ' -f2- | sed 's/^\.\///'
48 fi
49 }
50
51 get_changed_packages () {
52 if [ "$LOWLEVEL_PACKAGE_MANAGER" = dpkg ]; then
53 get_changes | sed 's/^/\/etc\//;s/\s*$//' | xargs -d '\n' dpkg 2>/dev/null -S | cut -d':' -f1 | sed 's/, /\n/g'
54 fi
55 if [ "$LOWLEVEL_PACKAGE_MANAGER" = rpm ]; then
56 # if output contains file path, file was not found
57 get_changes | sed 's/^/\/etc\//;s/\s*$//' | xargs -d '\n' rpm --qf '%{NAME}\n' -qf | grep -v "/etc/"
58 fi
59 # is it even possible to use pacmatic without pacman?
60 if [ "$LOWLEVEL_PACKAGE_MANAGER" = pacman -o "$LOWLEVEL_PACKAGE_MANAGER" = pacmatic ]; then
61 get_changes | sed 's/^/\/etc\//;s/\s*$//' | xargs -d '\n' pacman 2>/dev/null -Qo | rev | cut -d' ' -f1-2 | rev | cut -d' ' -f1
62 fi
63 if [ "$LOWLEVEL_PACKAGE_MANAGER" = pkgng ]; then
64 get_changes | sed 's/^/\/etc\//;s/\s*$//' | xargs -d '\n' pkg which --quiet | rev | cut -d'-' -f2- | rev
65 fi
66 if [ "$LOWLEVEL_PACKAGE_MANAGER" = xbps ]; then
67 get_changes | sed -E 's/^/\/etc\//;s/\s*$//;s/([[:punct:]])/\\\1/g' | (echo -n '^(' && sed -zE 's/[\r\n]+/|/g' | sed -zE 's/\|$//' && echo ')$') | xargs xbps-query -o --regex | cut -d':' -f1
68 fi
69 }
70
71 if etckeeper unclean; then
72 if [ -z "${ETCKEEPER_PARENT_COMMAND_LINE}" ]; then
73 message="committing changes in /etc after $HIGHLEVEL_PACKAGE_MANAGER run"
74 else
75 message="committing changes in /etc made by \"$ETCKEEPER_PARENT_COMMAND_LINE\""
76 fi
77
78 set +e
79 if [ -e $pl.pre-install ] && [ "$(cat $pl.fmt 2>/dev/null || true)" = "$(etckeeper list-installed fmt)" ]; then
80 (
81 echo "$message"
82 echo
83 get_changed_packages | sort | uniq > $pl.found-pkgs
84 if [ -s $pl.found-pkgs ]; then
85 sed -i 's/^/^[-+]/;s/$/ /' $pl.found-pkgs
86 etckeeper list-installed | diff -U0 $pl.pre-install - | tail -n+4 | grep -E '^[-+]' | grep -f $pl.found-pkgs > $pl.found-packages
87 if [ -s $pl.found-packages ]; then
88 echo "Packages with configuration changes:"
89 cat $pl.found-packages || true
90 echo
91 fi
92 fi
93 echo "Package changes:"
94 etckeeper list-installed | diff -U0 $pl.pre-install - | tail -n+4 | grep -E '^[-+]' || true
95 ) | etckeeper commit --stdin
96 else
97 etckeeper commit "$(printf "$message")"
98 fi
99 status=$?
100 set -e
101
102 if [ "$status" != 0 ]; then
103 echo "warning: etckeeper failed to commit changes in /etc using $VCS" >&2
104 fi
105 fi
106
107 rm -f $pl.pre-install $pl.fmt
108 rm -f $pl.found-pkgs $pl.found-packages