|
| 1 | +#!/usr/bin/env perl |
| 2 | +# -*- perl -*- |
| 3 | + |
| 4 | +use if !$ENV{DOIT_IN_REMOTE}, lib => "$ENV{HOME}/src/Doit/lib"; |
| 5 | +use Doit; |
| 6 | +use Doit::Log; |
| 7 | +use Getopt::Long; |
| 8 | +use POSIX qw(strftime); |
| 9 | + |
| 10 | +sub generate_git_tag { |
| 11 | + my $base = "deployment/bbbikede/" . strftime("%Y%m%d", localtime); |
| 12 | + my $tag = $base; |
| 13 | + my $doit = shift; |
| 14 | + |
| 15 | + for my $i (0..9) { |
| 16 | + my $try = $i == 0 ? $tag : "${tag}_$i"; |
| 17 | + my $exists = eval { |
| 18 | + $doit->info_system(qw(git rev-parse --verify), $try); |
| 19 | + 1; |
| 20 | + }; |
| 21 | + if (!$exists) { |
| 22 | + return $try; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + error("Too many tags exist with base $tag (_0 to _9)"); |
| 27 | +} |
| 28 | + |
| 29 | +return 1 if caller; |
| 30 | + |
| 31 | +my $doit = Doit->init; |
| 32 | +GetOptions("ignore-dirty-workdir" => \my $ignore_dirty_workdir) |
| 33 | + or error "usage: $0 [--ignore-dirty-workdir] [--dry-run]"; |
| 34 | + |
| 35 | +# Ensure working tree is clean |
| 36 | +$doit->system(qw(git diff-index --quiet --cached HEAD)); |
| 37 | + |
| 38 | +my $dirty_git_error = $ignore_dirty_workdir ? sub { warning @_ } : sub { error @_ }; |
| 39 | + |
| 40 | +my $changed = eval { |
| 41 | + $doit->system(qw(git diff-files --quiet)); |
| 42 | + 0; |
| 43 | +}; |
| 44 | +if ($changed) { |
| 45 | + my @diff = split /\n/, $doit->info_qx(qw(git diff-files)); |
| 46 | + $dirty_git_error->("There are uncommitted changes:\n" . join("\n", @diff) . "\nPlease commit or stash your changes first."); |
| 47 | +} |
| 48 | + |
| 49 | +my @untracked = split /\n/, $doit->info_qx(qw(git ls-files --exclude-standard --others)); |
| 50 | +if (@untracked) { |
| 51 | + $dirty_git_error->("There are untracked files:\n" . join("\n", @untracked) . "\nPlease clean up untracked files first."); |
| 52 | +} |
| 53 | + |
| 54 | +# Generate tag |
| 55 | +my $tag = generate_git_tag($doit); |
| 56 | +info("Using tag: $tag"); |
| 57 | + |
| 58 | +# Create and push tag |
| 59 | +$doit->system(qw(git tag -a -m), "* $tag", $tag); |
| 60 | +$doit->system(qw(git push)); |
| 61 | +$doit->system(qw(git push origin), $tag); |
| 62 | + |
| 63 | +__END__ |
0 commit comments