summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2015-07-02 17:55:38 +0200
committerMatthias Beyer <mail@beyermatthias.de>2015-07-02 17:55:38 +0200
commit1c7ebbb0cc217c86df9b58e6ff2d2b50f03531bb (patch)
treef8741a9f8bc34ced2c6e263fd6d55e2424f9dbf4
parent9ca5dca1529b9a24b3066be2d11596cd31180548 (diff)
parentd00bb7226a0c1de039118d7b2c14be2a7fd0d14e (diff)
Merge pull request #25 from matthiasbeyer/add-update_package_def
Add update package def
-rwxr-xr-xnix-script-update-package-def.sh120
-rw-r--r--nix-utils.sh29
2 files changed, 149 insertions, 0 deletions
diff --git a/nix-script-update-package-def.sh b/nix-script-update-package-def.sh
new file mode 100755
index 0000000..ce44326
--- /dev/null
+++ b/nix-script-update-package-def.sh
@@ -0,0 +1,120 @@
+#!/usr/bin/env bash
+
+#
+#
+# Updates a package definition by downloading the patch from monitor.nixos.org,
+# creating a branch in the nixpkgs repo for it, applying the patch and
+# test-building the package if you want to.
+#
+#
+
+source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh
+
+usage() {
+ cat <<EOS
+ $(help_synopsis "${BASH_SOURCE[0]}" "[-y] [-b] [-g <nixpkgs path>] -u <url>")
+
+ -y Don't ask before executing things (optional) (not implemented yet)
+ -b Also test-build the package (optional)
+ -u <url> Download and apply this url
+ -g <path> Path of nixpkgs clone (defaults to ./)
+ -h Show this help and exit
+
+$(help_end)
+EOS
+}
+
+YES=0
+TESTBUILD=0
+NIXPKGS=
+URL=
+
+while getopts "ybu:g:h" OPTION
+do
+ case $OPTION in
+ y)
+ YES=1
+ stdout "Setting YES"
+ ;;
+
+ b)
+ TESTBUILD=1
+ stdout "Test-building enabled"
+ ;;
+
+ u)
+ URL="$OPTARG"
+ stdout "URL = $URL"
+ ;;
+
+ g)
+ NIXPKGS="$OPTARG"
+ stdout "NIXPKGS = $NIXPKGS"
+ ;;
+
+ h)
+ usage
+ exit 0
+ ;;
+
+ esac
+done
+
+if [ -z "$URL" ]
+then
+ stderr "No URL for the patch"
+ exit 1
+fi
+
+if [ -z "$NIXPKGS" ]
+then
+ stderr "No nixpkgs passed."
+ stderr "Checking whether the current directory is a git repository!"
+ stderr "(this could possibly blow up pretty badly)"
+ continue_question "Continue execution" || exit 1
+
+ if [[ -d ./.git ]]
+ then
+ stdout "Git directory found"
+ NIXPKGS="."
+ else
+ stderr "No git directory"
+ exit 1
+ fi
+fi
+
+stdout "Making temp directory"
+TMP=$(mktemp)
+stdout "TMP = $TMP"
+
+stdout "Fetching patch"
+curl $URL > $TMP
+
+stdout "Parsing subject to branch name"
+PKG=$(cat $TMP | grep Subject | cut -d: -f 2 | sed -r 's,(\ *)(.*)(\ *),\2,')
+
+CURRENT_BRANCH=$(__git_current_branch "$NIXPKGS")
+__git "$NIXPKGS" checkout -b update-$PKG
+if [[ $? -ne 0 ]]
+then
+ stderr "Switching to branch update-$PKG failed."
+ exit 1
+fi
+
+cat $TMP | __git "$NIXPKGS" am
+stdout "Patch applied."
+
+if [[ $TESTBUILD -eq 1 ]]
+then
+ ask_execute "Build '$PKG' in nixpkgs clone at '$NIXPKGS'" nix-build -A $PKG -I $NIXPKGS
+fi
+
+stdout "Switching back to old commit which was current before we started."
+stdout "Switching to '$CURRENT_BRANCH'"
+__git "$NIXPKGS" checkout $CURRENT_BRANCH
+if [[ $? -ne 0 ]]
+then
+ stderr "Switching back to '$CURRENT_BRANCH' failed. Please check manually"
+ exit 1
+fi
+
diff --git a/nix-utils.sh b/nix-utils.sh
index d087bdf..2d84a43 100644
--- a/nix-utils.sh
+++ b/nix-utils.sh
@@ -2,6 +2,7 @@
Color_Off='\e[0m'
Red='\e[0;31m'
+Yellow='\e[0;33m'
Green='\e[0;32m'
stderr() {
@@ -47,3 +48,31 @@ current_user_generation() {
grep_generation "nix-env --list-generations"
}
+continue_question() {
+ local answer
+ echo -ne "${Yellow}$1 [yN]?:${Color_Off} " >&2
+ read answer
+ echo ""
+ [[ "${answer}" =~ ^[Yy]$ ]] || return 1
+}
+
+ask_execute() {
+ q="$1"; shift
+ local answer
+ echo -ne "${Yellow}$q${Color_Off} [Yn]? "
+ read answer; echo
+ [[ ! "${answer}" =~ ^[Nn]$ ]] && eval $*
+}
+
+__git() {
+ DIR=$1; shift
+ explain git --git-dir="$DIR/.git" --work-tree="$DIR" $*
+}
+
+# Gets the current branch name or the hash of the current rev if there is no
+# branch
+__git_current_branch() {
+ REV=$(git --git-dir="$1/.git" --work-tree="$1" rev-parse --abbrev-ref HEAD)
+ ([[ "$REV" -eq "HEAD" ]] && \
+ git --git-dir="$1/.git" --work-tree="$1" rev-parse HEAD) || echo "$REV"
+}