1 | 'use strict'
|
2 |
|
3 | var la = require('lazy-ass')
|
4 | var is = require('check-more-types')
|
5 | var exec = require('./exec')
|
6 | var Q = require('q')
|
7 | var debug = require('debug')('ggit')
|
8 | const sortTagsByVersion = require('./sort-tags-by-version')
|
9 |
|
10 | la(is.fn(sortTagsByVersion), 'missing sort tags by version')
|
11 |
|
12 | function toLines (text) {
|
13 | return text.split('\n')
|
14 | }
|
15 |
|
16 | function trim (lines) {
|
17 | return lines.map(function (s) {
|
18 | return s.trim()
|
19 | })
|
20 | }
|
21 |
|
22 | function hasV (line) {
|
23 | la(is.string(line), 'expected string', line)
|
24 | return /^v/.test(line)
|
25 | }
|
26 |
|
27 |
|
28 | function 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 |
|
42 | function getCommitIds (tags) {
|
43 | return Q.all(tags.map(getTagCommit))
|
44 | }
|
45 |
|
46 | function 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 |
|
56 | function getBranchTags (vTagsOnly) {
|
57 |
|
58 |
|
59 |
|
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 |
|
65 | function getTagsSortByVersion () {
|
66 | const cmd = 'git tag'
|
67 |
|
68 | return exec(cmd).then(sortTagsByVersion).then(tags => tags.join('\n'))
|
69 | }
|
70 |
|
71 | function getTags (vTagsOnly) {
|
72 |
|
73 |
|
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 |
|
82 | getTags.getBranchTags = getBranchTags
|
83 |
|
84 | module.exports = getTags
|