Skip to content

Commit 211047a

Browse files
committed
Implement annotated tag support.
1 parent 8aa59c0 commit 211047a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1111
-13
lines changed

index.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var fs = require('fs');
44
var path = require('path');
5+
var zlib = require('zlib');
56

67
var GIT_DIR = '.git';
78

@@ -43,19 +44,39 @@ function findPackedTag(gitPath, sha) {
4344
}
4445
}
4546

47+
function commitForTag(gitPath, tag) {
48+
var tagPath = path.join(gitPath, 'refs', 'tags', tag);
49+
var taggedObject = fs.readFileSync(tagPath, { encoding: 'utf8' }).trim();
50+
var objectPath = path.join(gitPath, 'objects', taggedObject.slice(0, 2), taggedObject.slice(2));
51+
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
52+
53+
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson <[email protected]> 1429100021 -0400\n\nI am making an annotated tag.\n'
54+
if (objectContents.slice(0,3) === 'tag') {
55+
var sections = objectContents.split(/\0|\n/);
56+
var sha = sections[1].slice(7);
57+
58+
return sha;
59+
} else {
60+
// this will return the tag for lightweight tags
61+
return taggedObject;
62+
}
63+
}
64+
4665
function findTag(gitPath, sha) {
4766
var tag = findPackedTag(gitPath, sha);
4867
if (tag) { return tag; }
4968

5069
var tagsPath = path.join(gitPath, 'refs', 'tags');
51-
if (!fs.existsSync(tagsPath)) { return; }
70+
if (!fs.existsSync(tagsPath)) { return false; }
5271

5372
var tags = fs.readdirSync(tagsPath);
5473

5574
for (var i = 0, l = tags.length; i < l; i++) {
56-
var tagPath = path.join(tagsPath, tags[i]);
57-
if (fs.readFileSync(tagPath, { encoding: 'utf8' }).indexOf(sha) > -1) {
58-
return tags[i];
75+
tag = tags[i];
76+
var commitAtTag = commitForTag(gitPath, tags[i]);
77+
78+
if (commitAtTag === sha) {
79+
return tag;
5980
}
6081
}
6182
}
@@ -97,11 +118,16 @@ module.exports = function(gitPath) {
97118
}
98119
}
99120
} catch (e) {
100-
// eat it
121+
if (!module.exports._suppressErrors) {
122+
throw e; // helps with testing and scenarios where we do not expect errors
123+
} else {
124+
// eat the error
125+
}
101126
}
102127

103128
return result;
104129
};
105130

131+
module.exports._suppressErrors = true;
106132
module.exports._findRepo = findRepo;
107133
module.exports._changeGitDir = changeGitDir;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Initial commit.
2+
# Please enter the commit message for your changes. Lines starting
3+
# with '#' will be ignored, and an empty message aborts the commit.
4+
# On branch master
5+
#
6+
# Initial commit
7+
#
8+
# Changes to be committed:
9+
# new file: README
10+
#
11+
# ------------------------ >8 ------------------------
12+
# Do not touch the line above.
13+
# Everything below will be removed.
14+
diff --git a/README b/README
15+
new file mode 100644
16+
index 0000000..e69de29
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
test -x "$GIT_DIR/hooks/commit-msg" &&
14+
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
15+
:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
test "" = "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22+
echo >&2 Duplicate Signed-off-by lines.
23+
exit 1
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to prepare a packed repository for use over
4+
# dumb transports.
5+
#
6+
# To enable this hook, rename this file to "post-update".
7+
8+
exec git update-server-info
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed
4+
# by applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit.
8+
#
9+
# To enable this hook, rename this file to "pre-applypatch".
10+
11+
. git-sh-setup
12+
test -x "$GIT_DIR/hooks/pre-commit" &&
13+
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
14+
:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed.
4+
# Called by "git commit" with no arguments. The hook should
5+
# exit with non-zero status after issuing an appropriate message if
6+
# it wants to stop the commit.
7+
#
8+
# To enable this hook, rename this file to "pre-commit".
9+
10+
if git rev-parse --verify HEAD >/dev/null 2>&1
11+
then
12+
against=HEAD
13+
else
14+
# Initial commit: diff against an empty tree object
15+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16+
fi
17+
18+
# If you want to allow non-ASCII filenames set this variable to true.
19+
allownonascii=$(git config --bool hooks.allownonascii)
20+
21+
# Redirect output to stderr.
22+
exec 1>&2
23+
24+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
25+
# them from being added to the repository. We exploit the fact that the
26+
# printable range starts at the space character and ends with tilde.
27+
if [ "$allownonascii" != "true" ] &&
28+
# Note that the use of brackets around a tr range is ok here, (it's
29+
# even required, for portability to Solaris 10's /usr/bin/tr), since
30+
# the square bracket bytes happen to fall in the designated range.
31+
test $(git diff --cached --name-only --diff-filter=A -z $against |
32+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
33+
then
34+
cat <<\EOF
35+
Error: Attempt to add a non-ASCII file name.
36+
37+
This can cause problems if you want to work with people on other platforms.
38+
39+
To be portable it is advisable to rename the file.
40+
41+
If you know what you are doing you can disable this check using:
42+
43+
git config hooks.allownonascii true
44+
EOF
45+
exit 1
46+
fi
47+
48+
# If there are whitespace errors, print the offending file names and fail.
49+
exec git diff-index --check --cached $against --

0 commit comments

Comments
 (0)