UNPKG

2.09 kBJavaScriptView Raw
1'use strict'
2
3var la = require('lazy-ass')
4var is = require('check-more-types')
5var exec = require('./exec')
6var Q = require('q')
7var debug = require('debug')('ggit')
8const sortTagsByVersion = require('./sort-tags-by-version')
9
10la(is.fn(sortTagsByVersion), 'missing sort tags by version')
11
12function toLines (text) {
13 return text.split('\n')
14}
15
16function trim (lines) {
17 return lines.map(function (s) {
18 return s.trim()
19 })
20}
21
22function hasV (line) {
23 la(is.string(line), 'expected string', line)
24 return /^v/.test(line)
25}
26
27// returns commit SHA for the given tag
28function getTagCommit (tag) {
29 la(is.unemptyString(tag), 'wrong tag', tag)
30 var cmd = 'git rev-list -n 1 ' + tag
31 debug('getting commit for tag "%s"', tag)
32 debug('using command "%s"', cmd)
33
34 return exec(cmd).then(function (commit) {
35 return {
36 commit: commit.trim(),
37 tag: tag
38 }
39 })
40}
41
42function getCommitIds (tags) {
43 return Q.all(tags.map(getTagCommit))
44}
45
46function parseTags (vTagsOnly, text) {
47 la(is.string(text), 'expected text', text)
48
49 var lines = trim(toLines(text)).filter(is.unemptyString)
50 if (vTagsOnly) {
51 lines = lines.filter(hasV)
52 }
53 return lines
54}
55
56function getBranchTags (vTagsOnly) {
57 // returns each tag on its own line
58 // oldest tags first, latest tags last]
59 // only tags accessible from the current branch are returned
60 var cmd = 'git tag --sort version:refname --merged'
61 var parseSomeTags = parseTags.bind(null, vTagsOnly)
62 return exec(cmd).then(parseSomeTags).then(getCommitIds)
63}
64
65function getTagsSortByVersion () {
66 const cmd = 'git tag'
67 // return single string so other pieces work as expected
68 return exec(cmd).then(sortTagsByVersion).then(tags => tags.join('\n'))
69}
70
71function getTags (vTagsOnly) {
72 // returns each tag on its own line
73 // oldest tags first, latest tags last
74 var cmd = 'git tag --sort version:refname'
75 var parseSomeTags = parseTags.bind(null, vTagsOnly)
76 return exec(cmd)
77 .catch(getTagsSortByVersion)
78 .then(parseSomeTags)
79 .then(getCommitIds)
80}
81
82getTags.getBranchTags = getBranchTags
83
84module.exports = getTags