Skip to content

Commit

Permalink
Merge pull request #32 from bakoontz2/master
Browse files Browse the repository at this point in the history
Get latest tag (not tag associated with latest commit)
  • Loading branch information
WyriHaximus authored Oct 24, 2022
2 parents 14e22f7 + 739b735 commit 67f0cb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Get previous tag


[![Continuous Integration](https://github.com/WyriHaximus/github-action-get-previous-tag/actions/workflows/ci.yml/badge.svg)](https://github.com/WyriHaximus/github-action-get-previous-tag/actions/workflows/ci.yml)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/WyriHaximus/github-action-get-previous-tag?logo=github&sort=semver)](https://github.com/WyriHaximus/github-action-get-previous-tag/releases)

Expand Down
49 changes: 19 additions & 30 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
const { exec } = require('child_process');
exec('git rev-list --tags --max-count=1', (err, rev, stderr) => {
exec(`git for-each-ref --sort=-taggerdate --count 1 --format="%(refname:short)" "refs/tags/*"`, (err, tag, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any revisions because: ');
console.log('\x1b[33m%s\x1b[0m', 'Could not find any tags because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
process.exit(1);
} else if(tag === "") {
let timestamp = Math.floor(new Date().getTime() / 1000);
console.log('\x1b[33m%s\x1b[0m', 'Falling back to default tag');
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${process.env.INPUT_FALLBACK}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${process.env.INPUT_FALLBACK}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
}

rev = rev.trim()
tag = tag.trim()

exec(`git describe --tags ${rev}`, (err, tag, stderr) => {
exec(`git log -1 --format=%at ${tag}`, (err, timestamp, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any tags because: ');
console.log('\x1b[33m%s\x1b[0m', 'Could not find any timestamp because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
if (process.env.INPUT_FALLBACK) {
let timestamp = Math.floor(new Date().getTime() / 1000);
console.log('\x1b[33m%s\x1b[0m', 'Falling back to default tag');
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${process.env.INPUT_FALLBACK}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${process.env.INPUT_FALLBACK}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
}
process.exit(1);
}

tag = tag.trim()
timestamp = timestamp.trim()

exec(`git log -1 --format=%at ${tag}`, (err, timestamp, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any timestamp because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
process.exit(1);
}

timestamp = timestamp.trim()

console.log('\x1b[32m%s\x1b[0m', `Found tag: ${tag}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${tag}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
});
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${tag}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${tag}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
});
});

0 comments on commit 67f0cb1

Please sign in to comment.