Git development
 help / color / mirror / Atom feed
From: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
To: Petr Baudis <pasky@ucw.cz>
Cc: git@vger.kernel.org
Subject: git-pasky: gitXnormid.sh overhaul
Date: Sat, 16 Apr 2005 18:51:29 +0200	[thread overview]
Message-ID: <20050416165129.GA13152@lsrfire.ath.cx> (raw)

Hello,

I just couldn't stand all the calls to grep and other external tools in
gitXnormid.sh and started rewriting it in a knee-jerk reaction.

You said in a private conversation that you don't like to include things
like ${var#stuff} to stay "sh compatible", while OTOH you favour $(cmd)
over `cmd`.  Both are POSIX extensions of the classical Bourne Shell
syntax (see e.g. http://docs.hp.com/en/B2355-90046/ch15s03.html for a
feature comparision between POSIX shell, Bourne Shell and Korn Shells on
HP-UX).  For reference, The Open Group publishes its IEEE Std 1003.1
standard (vulgo: POSIX) on this website:
http://www.opengroup.org/onlinepubs/009695399/toc.htm.  So which shell
do you want to target with your git scripts?

This time I tested the script. :]  It copes with invalid IDs,
non-existing valid IDs, abbreviated IDs, an omitted ID, valid IDs, with
tags and branch names.  I also made sure the script runs with bash, ash,
pdksh, zsh and bash --posix (all on SuSE 9.2).

I changed the way an ID is verified.  The script now tries to find tags
and branches first by looking for .git/tags/<id> and .git/HEAD.<id> and
after that looking inside .git/objects for a match.  That's faster and
now I can safely give a branch a name consisting of 40 hex digits. :-)

The script follows in plain text format, not as a patch.  Your and my
version share only very few lines, so this way it's easier to review.
I'll send you a patch if and when you're ready to apply it, ok?

Thanks,
Rene


--- 8< ---
#!/bin/sh
#
# Internal: Normalize the given ID to a tree ID.
# Copyright (c) Petr Baudis, 2005
#
# Takes an arbitrary ID as a parameter. -c tells it to give
# a commit id rather than tree id.

usage() {
	echo "Usage: $0 [-c] [tree-id | commit-id | tag | branch]"
	exit 2
}

get_first_word() {
	if read one two; then
		echo "$one"
		return 0
	fi
	return 1
}

expand_hash() {
	hashdir=${SHA1_FILE_DIRECTORY:-.git/objects}
	filename=${1#??}
	dirname=${1%${filename}}
	first=true
	for file in "${hashdir}/${dirname}/${filename}"*; do
		[ -f "$file" ] || return 1
		if $first; then
			hash=${dirname}${file##*/}
			first=false
		else
			return 1
		fi
	done
	$first && return 1
	echo "$hash"
}

get_tree_id() {
	cat-file commit "$1" | while read tag hash; do
		if [ "$tag" = "tree" ]; then
			echo "$hash"
			return
		fi
	done
}

type=tree
case "$1" in
-c) type=commit; shift;;
-*) usage;;
esac

if [ ! "$1" ]; then
	if [ ! -f ".git/HEAD" ]; then
		echo "$0: file .git/HEAD not found"
		usage
	fi
	id=$(get_first_word <".git/HEAD")
elif [ -f ".git/tags/$1" ]; then
	id=$(get_first_word <".git/tags/$1")
elif [ -f ".git/HEAD.$1" ]; then
	id=$(get_first_word <".git/HEAD.$1")
else
	id=$(expand_hash "$1")
fi
if [ $? != 0 ]; then
	echo "$0: invalid ID: $1" >&2
	exit 1
fi

if [ "$type" = "tree" ]; then
	tree_id=$(get_tree_id "$id" 2>/dev/null)
	[ "$tree_id" ] && id=$tree_id
fi

if [ $(cat-file -t "$id") != "$type" ]; then
	echo "$0: invalid ID: $id" >&2
	exit 1
fi
echo "$id"

                 reply	other threads:[~2005-04-16 16:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050416165129.GA13152@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=git@vger.kernel.org \
    --cc=pasky@ucw.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox