changeset 305:fac01a1bcedf

add an apply-patch function
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 14 May 2013 18:29:24 -0700
parents 7ed290454f50
children 1166c65328f7
files .bash_overrides .bashrc
diffstat 2 files changed, 59 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/.bash_overrides	Tue May 14 16:35:56 2013 -0700
+++ b/.bash_overrides	Tue May 14 18:29:24 2013 -0700
@@ -1,5 +1,6 @@
 #!/bin/bash
-# command overrides too elaborate for aliases
+
+### command overrides too elaborate for aliases
 
 cd() {
 
@@ -73,7 +74,8 @@
 
     if (( ! $# ))
     then
-	return
+	command find
+        return
     fi
 
     if [ -d $1 ]
@@ -96,7 +98,7 @@
 }
 
 function lsdiff {
-
+    # TODO: extract this general pattern as a bash "decorator"
     if expr "$1" : 'http[s]\?://.*' &> /dev/null
     then
         curl --location "$1" 2> /dev/null | command lsdiff
--- a/.bashrc	Tue May 14 16:35:56 2013 -0700
+++ b/.bashrc	Tue May 14 18:29:24 2013 -0700
@@ -53,8 +53,55 @@
 
 ### functions
 
+apply-patch() {
+    # apply a patch
+    # TODO:
+    # - extract this general pattern as a bash "decorator" like `lsdiff` in .bash_overrides
+    # - right now level=1; make this configurable (somehow)
+    if (( ! $# ))
+    then
+        echo "No patch supplied"
+        return 1
+    fi
+
+    for patch in $@
+    do
+        if expr "$1" : 'http[s]\?://.*' &> /dev/null
+        then
+            IS_URL="true"
+        else
+            IS__URL="false"
+        fi
+
+        if [[ ${IS_URL} == "true" ]]
+        then
+            if curl --location "${patch}" 2> /dev/null | (command patch -p1 --dry-run &> /dev/null)
+            then
+                curl --location "${patch}" 2> /dev/null | command patch -p1
+                continue
+            else
+                echo "curl --location ${patch} 2> /dev/null | command patch -p1 --dry-run"
+                curl --location "${patch}" 2> /dev/null | command patch -p1 --dry-run
+                return $?
+            fi
+        else
+            if patch -p1 --dry-run < ${patch}
+            then
+                patch -p1 < ${patch}
+                continue
+            else
+                echo "patch -p1 --dry-run < ${patch}"
+                patch -p1 --dry-run < ${patch}
+                return $?
+            fi
+        fi
+    done
+}
+
 cdwin() {
-    # change directory to a window's location using its title
+    # change directory to a window's location using its title,
+    # as that is set to the cwd by PS1 [?]
+    # TODO: ssh windows
     DIR=$(xwininfo | dictify.py xwininfo | awk '{ print $NF }' | sed 's/"//g')
     DIR=${DIR/\~/$HOME}
     cd $DIR
@@ -156,8 +203,8 @@
 
 }
 
-# make a temporary file if `tempfile` not available
 tmpfile() {
+    # make a temporary file if `tempfile` not available
 
     if [ "$#" == "0" ]
     then
@@ -206,11 +253,6 @@
     python -c "import os; print os.path.realpath('$*')"
 }
 
-pyfile() {
-    # python file name
-    python -c "import $1; print $1.__file__"
-}
-
 swap() {
     # swap two files
     if [ "$#" != "2" ]
@@ -252,6 +294,11 @@
 
 ### functions for python
 
+pyfile() {
+    # python file path
+    python -c "import $1; print $1.__file__"
+}
+
 setup-all() {
     # setup all for development
     # TODO: flowerbed?