Skip to content

Commit 1ca65ea

Browse files
authored
Merge pull request #34 from jrowlingson/deterministic-tag
Support deterministic findTag
2 parents a09590c + 7a64a06 commit 1ca65ea

File tree

12 files changed

+45
-21
lines changed

12 files changed

+45
-21
lines changed

index.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,27 @@ function findRepo(startingPath) {
6969
return null;
7070
}
7171

72-
function findPackedTag(gitPath, refPath) {
72+
function findPackedTags(gitPath, refPath) {
7373
return getPackedRefsForType(gitPath, refPath, 'tag');
7474
}
7575

7676
function findPackedCommit(gitPath, refPath) {
77-
return getPackedRefsForType(gitPath, refPath, 'commit');
77+
return getPackedRefsForType(gitPath, refPath, 'commit')[0];
7878
}
7979

8080
function getPackedRefsForType(gitPath, refPath, type) {
81-
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
82-
if (fs.existsSync(packedRefsFilePath)) {
83-
var packedRefsFile = fs.readFileSync(packedRefsFilePath, { encoding: 'utf8' });
84-
var shaLine = getLineForRefPath(packedRefsFile, type, refPath);
85-
86-
if (shaLine) {
81+
var packedRefsFile = getPackedRefsFile(gitPath);
82+
if (packedRefsFile) {
83+
return getLinesForRefPath(packedRefsFile, type, refPath).map(function(shaLine) {
8784
return getShaBasedOnType(type, shaLine);
88-
}
85+
});
8986
}
87+
return [];
9088
}
9189

92-
function getLineForRefPath(packedRefsFile, type, refPath) {
93-
return getLinesForRefPath(packedRefsFile, type, refPath)[0];
90+
function getPackedRefsFile(gitPath) {
91+
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
92+
return fs.existsSync(packedRefsFilePath) ? fs.readFileSync(packedRefsFilePath, { encoding: 'utf8' }) : false;
9493
}
9594

9695
function getLinesForRefPath(packedRefsFile, type, refPath) {
@@ -143,22 +142,27 @@ function commitForTag(gitPath, tag) {
143142
}
144143

145144
function findTag(gitPath, sha) {
146-
var tag = findPackedTag(gitPath, sha);
147-
if (tag) { return tag; }
148-
149-
var tagsPath = path.join(gitPath, 'refs', 'tags');
150-
if (!fs.existsSync(tagsPath)) { return false; }
151-
152-
var tags = fs.readdirSync(tagsPath);
145+
var tags = findPackedTags(gitPath, sha)
146+
.concat(findUnpackedTags(gitPath, sha));
147+
tags.sort();
148+
return tags.length ? tags[0] : false;
149+
}
153150

151+
function findUnpackedTags(gitPath, sha) {
152+
var unpackedTags = [];
153+
var tags = findLooseRefsForType(gitPath, 'tags');
154154
for (var i = 0, l = tags.length; i < l; i++) {
155-
tag = tags[i];
156155
var commitAtTag = commitForTag(gitPath, tags[i]);
157-
158156
if (commitAtTag === sha) {
159-
return tag;
157+
unpackedTags.push(tags[i]);
160158
}
161159
}
160+
return unpackedTags;
161+
}
162+
163+
function findLooseRefsForType(gitPath, type) {
164+
var refsPath = path.join(gitPath, 'refs', type);
165+
return fs.existsSync(refsPath) ? fs.readdirSync(refsPath) : [];
162166
}
163167

164168
module.exports = function(gitPath) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

tests/fixtures/tagged-commit-mixed-packing/dot-git/objects/90/4de697a2c441e3db28f5c531ccfdab5f4435e3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x=�K� @]s��@�Kc\���0�V��L��^ܸ|�%O0��!��I���xB?{�ý�L����zM���jk��|6�1.��>������o*�\��H���sI���u�Å˾�� ��f�g3���Ak���q{2D.�/%�6�
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
37ece7ad9ded5f2312bb6be8d0c21ecebca088ac refs/tags/b-lightweight-tag
2+
c1ee41c325d54f410b133e0018c7a6b1316f6cda refs/tags/-lightweight-tag
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
37ece7ad9ded5f2312bb6be8d0c21ecebca088ac
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c1ee41c325d54f410b133e0018c7a6b1316f6cda
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
37ece7ad9ded5f2312bb6be8d0c21ecebca088ac

0 commit comments

Comments
 (0)