Skip to content

Commit 5431dc1

Browse files
committed
Merge pull request #6 from rwjblue/annotated-tags
Implement annotated tag support.
2 parents 8aa59c0 + 6a5eb78 commit 5431dc1

Some content is hidden

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

49 files changed

+1130
-13
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ sudo: false
33
cache:
44
directories:
55
- node_modules
6+
7+
env:
8+
- NODE_VERSION=0.10
9+
- NODE_VERSION=0.12
10+
- NODE_VERSION=iojs
11+
12+
branches:
13+
only:
14+
- master

index.js

Lines changed: 38 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,46 @@ 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+
52+
if (!zlib.inflateSync) {
53+
// we cannot support annotated tags on node v0.10 because
54+
// zlib does not allow sync access
55+
return taggedObject;
56+
}
57+
58+
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
59+
60+
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson <[email protected]> 1429100021 -0400\n\nI am making an annotated tag.\n'
61+
if (objectContents.slice(0,3) === 'tag') {
62+
var sections = objectContents.split(/\0|\n/);
63+
var sha = sections[1].slice(7);
64+
65+
return sha;
66+
} else {
67+
// this will return the tag for lightweight tags
68+
return taggedObject;
69+
}
70+
}
71+
4672
function findTag(gitPath, sha) {
4773
var tag = findPackedTag(gitPath, sha);
4874
if (tag) { return tag; }
4975

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

5379
var tags = fs.readdirSync(tagsPath);
5480

5581
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];
82+
tag = tags[i];
83+
var commitAtTag = commitForTag(gitPath, tags[i]);
84+
85+
if (commitAtTag === sha) {
86+
return tag;
5987
}
6088
}
6189
}
@@ -97,11 +125,16 @@ module.exports = function(gitPath) {
97125
}
98126
}
99127
} catch (e) {
100-
// eat it
128+
if (!module.exports._suppressErrors) {
129+
throw e; // helps with testing and scenarios where we do not expect errors
130+
} else {
131+
// eat the error
132+
}
101133
}
102134

103135
return result;
104136
};
105137

138+
module.exports._suppressErrors = true;
106139
module.exports._findRepo = findRepo;
107140
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+
:

0 commit comments

Comments
 (0)