3 # Get the SHA1 id of an object associated with the given symbolic id
4 # Copyright (c) Petr Baudis, Pavel Roskin, Philip Pokorny 2005
6 # Resolves a given symbolic id to the raw SHA1 id (or a symbolic
7 # description when passed the '-d' parameter). The symbolic id can be
8 # an SHA1 id, a unique starting segment of an existing SHA1 id, a ref
9 # name, date, empty string, etc. See the 'COMMIT_ID' description in
10 # cogito(7) for the full list.
12 # Normally, you do not use this command directly (except with the '-d'
13 # parameter), but it is used in other Cogito scripts to resolve passed
14 # object identifiers. If the ID is not provided, HEAD is assumed. The
15 # default behavior is to show the commit ID, but you should always
16 # explicitly write '-c' if using this in a script.
20 # -b:: Get branch name
21 # Get name of the current branch.
24 # Get ID of commit matching the object ID (error out if it is not
25 # a commit). This is the default if you do not pass any parameter
26 # as well, but that is only for the human usage. For clarity, all
27 # scripted usage of cg-object-id should use -c explicitly if it
30 # -d:: Get commit string description
31 # Get a commit description in form of a short string. It shows the
32 # most recent tag in past of the commit and if it is not the commit
33 # itself, it appends first few chars of the commit id to id. See
34 # `git-describe`(1) for details.
36 # -n:: Disable object type checking
37 # Normalize only - don't check the object type.
39 # -p:: Get parent commit ID(s)
40 # Get ID of the first parent commit of a given revision or HEAD.
41 # NOTE: Multiple SHA1s separated by newlines will be returned for
42 # commits with multiple parents.
45 # Get ID of tree associated with given commit or HEAD.
48 # An ID resolving to a commit.
50 # Testsuite: Partial (used in many tests but a dedicated testsuite is missing)
52 USAGE
="cg-object-id [-b | -c | -d | -n | -p | -t] [OBJECT_ID]"
55 .
"${COGITO_LIB}"cg-Xlib
58 # Normalize argument. The normalized SHA1 ID is put to $normid,
59 # type is put to $type.
66 if [ ! "$id" ] ||
[ "$id" = "this" ]; then
70 revid
="$(git-rev-parse --verify "$id" 2>/dev/null)"
71 if [ "$revid" ] && [ ${#revid} -eq 40 ] && [ "${revid//[0-9a-f]/}" = "" ]; then
76 # date does the wrong thing for empty and single-letter ids
77 if [ ${#id} -gt 1 ] && [ ! "$valid" ]; then
78 reqsecs
="$(date --date="$id" +'%s' 2>/dev/null)"
80 if [ "$reqsecs" ]; then
81 revid
="$(git-rev-list "--min-age=$reqsecs" --max-count=1 HEAD)"
89 # If we don't have a 40-char ID by now, it's an error
90 if [ ! "$valid" ]; then
91 echo "Invalid id: $id" >&2
95 type="$(git-cat-file -t "$id")"
96 if [ "$type" = "tag" ]; then
97 id
="$(git-cat-file tag "$id" | head -n 1)"
107 show_commit
= # this is really only for the options-exclusivity-checking
117 elif optparse
-c; then
119 elif optparse
-d; then
121 elif optparse
-n; then
123 elif optparse
-p; then
125 elif optparse
-t; then
134 *parent
*) show_parent
=1;;
135 *tree
*) show_tree
=1;;
138 case "$show_commit$describe$show_parent$show_tree$normalize_only" in
145 if [ "$normalize_only" ]; then
149 if [ "$describe" ]; then
150 git-describe
"$normid"
155 if [ "$show_parent" ]; then
156 git-rev-list
--parents -n 1 "$normid" |
tr ' ' '\n' |
tail -n +2
160 [ "$type" ] ||
type="$(git-cat-file -t "$normid")"
161 if [ "$show_tree" ]; then
162 if [ "$type" = "commit" ]; then
163 normid
="$(git-cat-file commit "$normid" | sed -e 's/tree //;q')"
164 type="$(git-cat-file -t "$normid")"
167 if [ "$type" != "tree" ]; then
168 echo "Invalid tree id: $normid" >&2
172 if [ "$type" != "commit" ]; then
173 echo "Invalid commit id: $normid" >&2