File: svn_repo_revision.pl

package info (click to toggle)
postgis 2.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,660 kB
  • ctags: 10,181
  • sloc: ansic: 132,858; sql: 131,148; xml: 46,460; sh: 4,832; perl: 4,476; makefile: 2,749; python: 1,198; yacc: 442; lex: 131
file content (133 lines) | stat: -rwxr-xr-x 2,967 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl

$ENV{"LC_ALL"} = "C";

use warnings;
use strict;

my $top_srcdir = ".";
my $rev_file = $top_srcdir.'/postgis_svn_revision.h';

my $target = 'local';
$target = $ARGV[0] if $ARGV[0];

# Read the svn revision number
my $svn_rev = &read_rev($target);

# Write it 
&write_defn($svn_rev);


sub read_rev {

  my $target = shift;

  #print STDERR "Target: $target\n";

  my $svn_info;

  if ( $target eq "local" ) {
    if ( -d $top_srcdir."/.svn" ) {
      #print STDERR "There's a ". $top_srcdir."/.svn dir\n";
      $svn_info  = &read_rev_svn($target);
    } elsif ( -d $top_srcdir."/.git" ) {
      #print STDERR "There's a ". $top_srcdir."/.git dir\n";
      $svn_info  = &read_rev_git();
    } else {
      print STDERR "Can't fetch local revision (neither .svn nor .git found)\n";
      $svn_info = 0;
    }
  } else {
    $svn_info  = &read_rev_svn($target);
  }

  return $svn_info;

}

sub read_rev_git {

  # TODO: test on old systems, I think I saw some `which`
  #       implementations returning "nothing found" or something
  #       like that, making the later if ( ! $svn_exe ) always false
  #
  my $git_exe = `which git`;
  if ( ! $git_exe ) {
    print STDERR "Can't fetch SVN revision: no git executable found\n";
    return 0;
  }
  chop($git_exe);

  my $cmd = "\"${git_exe}\" log --grep=git-svn -1 | grep git-svn | cut -d@ -f2 | cut -d' ' -f1";
  #print STDERR "cmd: ${cmd}\n";
  my $rev  = `$cmd`;

  if ( ! $rev ) {
    print STDERR "Can't fetch SVN revision from git log\n";
    $rev = 0;
  } else {
    chop($rev);
  }

  return $rev;
}

sub read_rev_svn {

  my $target = shift;

  # TODO: test on old systems, I think I saw some `which`
  #       implementations returning "nothing found" or something
  #       like that, making the later if ( ! $svn_exe ) always false
  #
  my $svn_exe = `which svn`;
  if ( ! $svn_exe ) {
    print STDERR "Can't fetch SVN revision: no svn executable found\n";
    return 0;
  }
  chop($svn_exe);


  my $svn_info;
  if ( $target eq "local" ) {
    $svn_info  = `"${svn_exe}" info`;
  } else {
    $svn_info  = `"${svn_exe}" info $target`;
  }

  my $rev;
  if ( $svn_info =~ /Last Changed Rev: (\d+)/ ) {
    $rev = $1;
  } else {
    print STDERR "Can't fetch SVN revision: no 'Loast Changed Rev' in `svn info` output\n";
    $rev = 0;
  }

  return $rev;
}

sub write_defn {
  my $rev = shift;
  my $oldrev = 0;

  # Do not override the file if new detected
  # revision isn't zero nor different from the existing one
  if ( -f $rev_file ) {
    open(IN, "<$rev_file");
    my $oldrevline = <IN>;
    if ( $oldrevline =~ /POSTGIS_SVN_REVISION (.*)/ ) {
      $oldrev = $1;
    }
    close(IN);
    if ( $rev == 0 or $rev == $oldrev ) {
      print STDERR "Not updating existing rev file at $oldrev\n";
      return;
    }
  }

  my $string = "#define POSTGIS_SVN_REVISION $rev\n";
  open(OUT,">$rev_file");
  print OUT $string;
  close(OUT);
  print STDERR "Wrote rev file at $rev\n";
}