Skip to content

Commit

Permalink
Consistenly using 'head -n' and 'tail -n' to be BSD compatible.
Browse files Browse the repository at this point in the history
Replaced 'head -n -X' with wc+expr as BSD head doesn't support negative arguments.
On Darwin skip running head if N is 0 as it fails with N=0 (!).
Implementing a Darwin compatible sha1 function that relies on openssl.

Signed-off-by: Trygve Laugstøl <[email protected]>
Signed-off-by: Josef 'Jeff' Sipek <[email protected]>
  • Loading branch information
trygvis authored and jeffpc committed Jul 10, 2011
1 parent 9f356a2 commit 1b23e97
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 22 deletions.
73 changes: 60 additions & 13 deletions guilt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ verify_branch()

get_top()
{
tail -1 "$GUILT_DIR/$branch/status"
tail -n 1 "$GUILT_DIR/$branch/status"
}

get_prev()
Expand Down Expand Up @@ -479,10 +479,13 @@ pop_many_patches()
cd_to_toplevel

# remove the patches refs
tail -$2 < "$applied" | remove_patch_refs
tail -n $2 < "$applied" | remove_patch_refs

git reset --hard "$1" > /dev/null
head -n "-$2" < "$applied" > "$applied.tmp"

n=`wc -l < "$applied"`
n=`expr $n - $2`
head -n "$n" < "$applied" > "$applied.tmp"
mv "$applied.tmp" "$applied"
)
}
Expand All @@ -491,7 +494,7 @@ pop_many_patches()
pop_all_patches()
{
pop_many_patches \
`git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
`git rev-parse refs/patches/$branch/$(head -n 1 "$applied")^` \
`wc -l < "$applied"`
}

Expand Down Expand Up @@ -536,7 +539,7 @@ commit()
fi

ct=`git log -1 --pretty=%ct`
if [ $ct -gt `stat -c %Y "$p"` ]; then
if [ $ct -gt `last_modified "$p"` ]; then
ct=`expr $ct + 60`
if [ $ct -gt `date +%s` ]; then
touch "$p"
Expand All @@ -545,12 +548,7 @@ commit()
fi
fi

# must strip nano-second part otherwise git gets very
# confused, and makes up strange timestamps from the past
# (chances are it decides to interpret it as a unix
# timestamp).
export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'`"
export GIT_AUTHOR_DATE="`format_last_modified "$p"`"
export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"

# commit
Expand Down Expand Up @@ -717,9 +715,11 @@ __refresh_patch()
N=`expr "$3" - 1`

# remove the patches refs
tail -$N < "$applied" | remove_patch_refs
tail -n $N < "$applied" | remove_patch_refs

head -n "-$N" < "$applied" > "$applied.tmp"
n=`wc -l < "$applied"`
n=`expr $n - $N`
head -n "$n" < "$applied" > "$applied.tmp"
mv "$applied.tmp" "$applied"
)
}
Expand Down Expand Up @@ -779,6 +779,50 @@ guilt_hook()
return $?
}

# usage: last_modified <file>
last_modified()
{
case $UNAME_S in
Darwin) stat -f "%a" "$1" ;;
Linux) stat -c "%Y" "$1" ;;
*) echo "Unsupported operating system: $UNAME_S" ;;
esac
}

# usage: last_modified <file>
format_last_modified()
{
case $UNAME_S in
Darwin) stat -f "%Sa" -t "%Y-%m-%d %H:%M:%S %z" "$1" ;;
Linux)
# must strip nano-second part otherwise git gets very
# confused, and makes up strange timestamps from the past
# (chances are it decides to interpret it as a unix
# timestamp).
stat -c "%y" "$1" | sed -e '
s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/';;
*) echo "Unsupported operating system: $UNAME_S" ;;
esac
}

# usage: head -n [count]
head() {
case $UNAME_S in
Darwin) if [ "$2" -gt 0 ]; then "$HEAD" -n "$2"; fi ;;
Linux) "$HEAD" -n "$2" ;;
*) echo "Unsupported operating system: $UNAME_S" ;;
esac
}

# usage: sha1 [file]
sha1() {
case $UNAME_S in
Darwin) openssl dgst -sha1 "$1" ;;
Linux) sha1sum "$1" ;;
*) echo "Unsupported operating system: $UNAME_S" ;;
esac
}

#
# Some constants
#
Expand Down Expand Up @@ -829,3 +873,6 @@ guards_file="$GUILT_DIR/$branch/guards"
# determine a pager to use for anything interactive (fall back to more)
pager="more"
[ ! -z "$PAGER" ] && pager="$PAGER"

UNAME_S=`uname -s`
HEAD=`which head`
8 changes: 7 additions & 1 deletion guilt-branch
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ git checkout "$newbranch"
mkdir -p "$GUILT_DIR/`dirname $newbranch`"

# copy the patch dir
cp -a "$GUILT_DIR/$branch" "$GUILT_DIR/$newbranch"
case $UNAME_S in
# Darwin doesn't have the GNU equalient -d (--no-dereference --preserve=links),
# flags
Darwin) cp -p "$GUILT_DIR/$branch" "$GUILT_DIR/$newbranch" ;;
Linux) cp -a "$GUILT_DIR/$branch" "$GUILT_DIR/$newbranch" ;;
*) echo "Unsupported operating system: $UNAME_S" ;;
esac

# copy the refs

Expand Down
2 changes: 1 addition & 1 deletion guilt-files
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ IFS=:
if [ -n "$opt_all" ]; then
cat $applied
else
tail -1 $applied
tail -n 1 $applied
fi | while read patch; do
obj=`git rev-parse refs/patches/$branch/$patch`

Expand Down
4 changes: 2 additions & 2 deletions guilt-graph
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fi

patchname="$1"

bottom=`git rev-parse refs/patches/$branch/$(head -1 < "$applied")`
bottom=`git rev-parse refs/patches/$branch/$(head -n 1 < "$applied")`
base=`git rev-parse $bottom^`

if [ -z "$patchname" ]; then
Expand Down Expand Up @@ -55,7 +55,7 @@ while [ "$current" != "$base" ]; do

getfiles $current | while read f; do
# hash the filename
fh=`echo "$f" | sha1sum | cut -d' ' -f1`
fh=`echo "$f" | sha1 | cut -d' ' -f1`
if [ -e "$cache/$fh" ]; then
# ok, something already touched this file before us
cat "$cache/$fh" >> "$cache/dep"
Expand Down
2 changes: 1 addition & 1 deletion guilt-push
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ if [ "$sidx" -gt "$eidx" ]; then
if [ "$sidx" -le "`get_series | wc -l`" ]; then
disp "Patch is already applied."
else
disp "File series fully applied, ends at patch `get_series | tail -1`"
disp "File series fully applied, ends at patch `get_series | tail -n 1`"
fi
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion guilt-rebase
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ cat "$rebase_dir/status" | while read hash name; do
while read patchid commitid ; do
disp "Applying '$name'"
if [ -f "$rebase_dir/$patchid" ]; then
realcommit=`head -1 "$rebase_dir/$patchid"`
realcommit=`head -n 1 "$rebase_dir/$patchid"`
disp "Matches upstream commit $realcommit"
series_rename_patch "$name" "###rebased###$name"
disp "Patch removed from series."
Expand Down
2 changes: 1 addition & 1 deletion guilt-repair
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ repair_pushed()
{
if [ -s "$applied" ]; then
# there were some patches applied
newrev=`git rev-parse refs/patches/$branch/$(head -1 < "$applied")^`
newrev=`git rev-parse refs/patches/$branch/$(head -n 1 < "$applied")^`
else
# no patches were applied, but let's do all the work anyway
newrev="$oldrev"
Expand Down
4 changes: 2 additions & 2 deletions guilt-series
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ if [ ! -z "$edit" ]; then
git_editor "$series"
elif [ ! -z "$gui" ]; then
[ -z "`get_top`" ] && die "No patches applied."
bottom=`git rev-parse refs/patches/$branch/$(head -1 $applied)`
top=`git rev-parse refs/patches/$branch/$(tail -1 $applied)`
bottom=`git rev-parse refs/patches/$branch/$(head -n 1 $applied)`
top=`git rev-parse refs/patches/$branch/$(tail -n 1 $applied)`
range="$bottom..$top"

# FIXME, this doesn't quite work - it's perfectly fine with
Expand Down

0 comments on commit 1b23e97

Please sign in to comment.