Skip to content

Commit 094f240

Browse files
author
John Haley
committed
Merge branch 'mattyclarkson-master'
2 parents 11bf00b + 7b2fd5b commit 094f240

File tree

3 files changed

+172
-2
lines changed

3 files changed

+172
-2
lines changed

generate/input/descriptor.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,42 @@
16051605
"git_tag_foreach": {
16061606
"ignore": true
16071607
},
1608+
"git_tag_create": {
1609+
"args": {
1610+
"oid": {
1611+
"isReturn": true
1612+
}
1613+
},
1614+
"return": {
1615+
"isErrorCode": true
1616+
},
1617+
"isAsync": true
1618+
},
1619+
"git_tag_create_frombuffer": {
1620+
"ignore": true
1621+
},
1622+
"git_tag_create_lightweight": {
1623+
"args": {
1624+
"oid": {
1625+
"isReturn": true
1626+
}
1627+
},
1628+
"return": {
1629+
"isErrorCode": true
1630+
},
1631+
"isAsync": true
1632+
},
1633+
"git_tag_annotation_create": {
1634+
"args": {
1635+
"oid": {
1636+
"isReturn": true
1637+
}
1638+
},
1639+
"return": {
1640+
"isErrorCode": true
1641+
},
1642+
"isAsync": true
1643+
},
16081644
"git_tag_list": {
16091645
"args": {
16101646
"tag_names": {
@@ -1624,6 +1660,12 @@
16241660
"isReturn": true
16251661
}
16261662
}
1663+
},
1664+
"git_tag_delete": {
1665+
"return": {
1666+
"isErrorCode": true
1667+
},
1668+
"isAsync": true
16271669
}
16281670
}
16291671
},

lib/repository.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,51 @@ Repository.prototype.getTree = function(oid, callback) {
268268
}, callback);
269269
};
270270

271+
/**
272+
* Creates a new annotated tag
273+
*
274+
* @async
275+
* @param {String|Oid} String sha or Oid
276+
* @param {String} name the name of the tag
277+
* @param {String} message the description that will be attached to the
278+
* annotated tag
279+
* @return {Tag}
280+
*/
281+
Repository.prototype.createTag = function(oid, name, message, callback) {
282+
var repository = this;
283+
var signature = repository.defaultSignature();
284+
285+
return Commit.lookup(repository, oid)
286+
.then(function(commit) {
287+
// Final argument is `force` which overwrites any previous tag
288+
return Tag.create(repository, name, commit, signature, message, 0);
289+
})
290+
.then(function(tagOid) {
291+
return repository.getTag(tagOid, callback);
292+
});
293+
};
294+
295+
/**
296+
* Creates a new lightweight tag
297+
*
298+
* @async
299+
* @param {String|Oid} String sha or Oid
300+
* @param {String} name the name of the tag
301+
* @return {Reference}
302+
*/
303+
Repository.prototype.createLightweightTag = function(oid, name, callback) {
304+
var repository = this;
305+
306+
return Commit.lookup(repository, oid)
307+
.then(function(commit) {
308+
// Final argument is `force` which overwrites any previous tag
309+
return Tag.createLightweight(repository, name, commit, 0);
310+
})
311+
.then(function() {
312+
return Reference.lookup(repository, "refs/tags/" + name);
313+
});
314+
};
315+
271316
/**
272317
* Retrieve the tag represented by the oid.
273318
*
@@ -314,6 +359,20 @@ Repository.prototype.getTagByName = function(name, callback) {
314359
}, callback);
315360
};
316361

362+
/**
363+
* Deletes a tag from a repository by the tag name.
364+
*
365+
* @async
366+
* @param {String} Short or full tag name
367+
*/
368+
Repository.prototype.deleteTagByName = function(name) {
369+
var repository = this;
370+
371+
name = ~name.indexOf("refs/tags/") ? name.substr(10) : name;
372+
373+
return Tag.delete(repository, name);
374+
};
375+
317376
/**
318377
* Instantiate a new revision walker for browsing the Repository"s history.
319378
* See also `Commit.prototype.history()`

test/tests/tag.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe("Tag", function() {
77
var Tag = require(local("../../lib/tag"));
88
var Obj = require(local("../../lib/object"));
99
var Oid = require(local("../../lib/oid"));
10+
var Reference = require(local("../../lib/reference"));
11+
var Promise = require("nodegit-promise");
1012

1113
var reposPath = local("../repos/workdir/.git");
1214
var tagName = "annotated-tag";
@@ -15,8 +17,8 @@ describe("Tag", function() {
1517
var commitPointedTo = "32789a79e71fbc9e04d3eff7425e1771eb595150";
1618
var tagMessage = "This is an annotated tag\n";
1719

18-
function testTag(tag) {
19-
assert.equal(tag.name(), tagName);
20+
function testTag(tag, name) {
21+
assert.equal(tag.name(), name || tagName);
2022
assert.equal(tag.targetType(), Obj.TYPE.COMMIT);
2123
assert.equal(tag.message(), tagMessage);
2224

@@ -75,4 +77,71 @@ describe("Tag", function() {
7577
assert.equal(tagNames.length, 1);
7678
});
7779
});
80+
81+
it("can create a new annotated tag in a repo and delete it", function() {
82+
var oid = Oid.fromString(commitPointedTo);
83+
var name = "created-annotated-tag";
84+
var repository = this.repository;
85+
86+
return repository.createTag(oid, name, tagMessage)
87+
.then(function(tag) {
88+
testTag(tag, name);
89+
})
90+
.then(function() {
91+
return repository.createTag(oid, name, tagMessage);
92+
})
93+
.then(function() {
94+
return Promise.reject(new Error("should not be able to create the '" +
95+
name + "' tag twice"));
96+
}, function() {
97+
return Promise.resolve();
98+
})
99+
.then(function() {
100+
return repository.deleteTagByName(name);
101+
})
102+
.then(function() {
103+
return Reference.lookup(repository, "refs/tags/" + name);
104+
})
105+
.then(function() {
106+
return Promise.reject(new Error("the tag '" + name +
107+
"' should not exist"));
108+
}, function() {
109+
return Promise.resolve();
110+
});
111+
});
112+
113+
it("can create a new lightweight tag in a repo and delete it", function() {
114+
var oid = Oid.fromString(commitPointedTo);
115+
var name = "created-lightweight-tag";
116+
var repository = this.repository;
117+
118+
return repository.createLightweightTag(oid, name)
119+
.then(function(reference) {
120+
return reference.target();
121+
})
122+
.then(function(refOid) {
123+
assert.equal(refOid.toString(), oid.toString());
124+
})
125+
.then(function() {
126+
return repository.createLightweightTag(oid, name);
127+
})
128+
.then(function() {
129+
return Promise.reject(new Error("should not be able to create the '" +
130+
name + "' tag twice"));
131+
}, function() {
132+
return Promise.resolve();
133+
})
134+
.then(function() {
135+
return repository.deleteTagByName(name);
136+
})
137+
.then(function() {
138+
return Reference.lookup(repository, "refs/tags/" + name);
139+
})
140+
.then(function() {
141+
return Promise.reject(new Error("the tag '" + name +
142+
"' should not exist"));
143+
}, function() {
144+
return Promise.resolve();
145+
});
146+
});
78147
});

0 commit comments

Comments
 (0)