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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#!/usr/bin/env perl
# Copyright (c) MediaTek USA Inc., 2022-2024
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see
# <http://www.gnu.org/licenses/>.
#
#
# gitversion [--p4] [--md5] [--local-change] [--prefix path] pathname OR
# gitversion [--p4] [--md5] [--prefix path] --compare old_version new_version pathname
#
# If the '--p4' flag is used:
# we assume that the GIT repo is cloned from Perforce - and look for
# the line in the generated commit log message which tells us the perforce
# changelist ID that we actually want.
# if the '--local-change' flag is used:
# we assume that there may be local changes which are not committed to
# the repo. If flag is not set: do not check for local change
# If specified, 'path' is prependied to 'pathname' (as 'path/pathname')
# before processing.
# This is a sample script which uses git commands to determine
# the version of the filename parameter.
# Version information (if present) is used during ".info" file merging
# to verify that the data the user is attempting to merge is for the same
# source code/same version.
# If the version is not the same - then line numbers, etc. may be different
# and some very strange errors may occur.
package gitversion;
use strict;
use POSIX qw(strftime);
use File::Spec;
use Cwd qw(abs_path);
use File::Basename qw(dirname basename);
use Getopt::Long qw(GetOptionsFromArray);
use annotateutil qw(get_modify_time compute_md5);
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(new extract_version compare_version usage);
use constant {
MD5 => 0,
P4 => 1,
PREFIX => 2,
ALLOW_MISSING => 3,
CHECK_LOCAL_CHANGE => 4,
};
sub usage
{
my $exe = shift;
$exe = basename($exe);
print(STDERR<<EOF);
usage: $exe --compare old_version new_version filename OR
$exe [--md5] [--p4] [--prefix path] [--local-change] \
[--allow-missing] filename
EOF
}
sub new
{
my $class = shift;
my $script = shift;
my @args = @_;
my $compare;
my $use_md5; # if set, append md5 checksum to the P4 version string
my $mapp4;
my $prefix;
my $allow_missing;
my $help;
my $local_change;
if (!GetOptionsFromArray(\@_,
"--compare" => \$compare,
"--md5" => \$use_md5,
"--p4" => \$mapp4,
'--prefix:s' => \$prefix,
'--allow-missing' => \$allow_missing,
'--local-change' => \$local_change,
'--help' => \$help) ||
$help ||
$compare && scalar(@_) != 3
) {
usage($script);
exit(defined($help) ? 0 : 1) if ($script eq $0);
return undef;
}
my $self = [$use_md5, $mapp4, $prefix, $allow_missing, $local_change];
return bless $self, $class;
}
sub extract_version
{
my ($self, $filename) = @_;
if (!File::Spec->file_name_is_absolute($filename) &&
defined($self->[PREFIX])) {
$filename = File::Spec->catfile($self->[PREFIX], $filename);
}
unless (-e $filename) {
if ($self->[ALLOW_MISSING]) {
return ''; # empty string
}
die("Error: $filename does not exist - perhaps you need the '--allow-missing' flag"
);
}
my $pathname = abs_path($filename);
my $null = File::Spec->devnull();
my $dir = dirname($pathname);
my $file = basename($pathname);
-d $dir or die("no such directory '$dir'");
my $version;
if (0 == system("cd $dir ; git rev-parse --show-toplevel >$null 2>&1")) {
# in a git repo - use full SHA.
my $log = `cd $dir ; git log --no-abbrev --oneline -1 $file 2>$null`;
if (0 == $? &&
$log =~ /^(\S+) /) {
$version = $1;
if ($self->[P4]) {
if (open(GITLOG, '-|', "cd $dir ; git show -s $version")) {
while (my $l = <GITLOG>) {
# p4sync puts special comment in commit log.
# pull the CL out of that.
if ($l =~ /git-p4:.+change = ([0-9]+)/) {
$version = "CL $1";
last;
}
}
} else {
die("unable to execute 'git show -s $version': $!");
}
close(GITLOG) or die("unable to close");
if (0 != $?) {
$? & 0x7F &
die("git show died from signal ", ($? & 0x7F), "\n");
die("git show exited with error ", ($? >> 8), "\n");
}
} else {
$version = "SHA $version";
}
if ($self->[CHECK_LOCAL_CHANGE]) {
my $diff = `cd $dir ; git diff $file 2>$null`;
if ('' ne $diff) {
$version .= ' edited ' . get_modify_time($file);
$version .= ' md5:' . compute_md5($pathname)
if $self->[MD5];
}
}
}
}
if (!$version) {
# not in git - just print the modify time, so we have a prayer of
# noticing file differences
$version = get_modify_time($pathname);
$version .= ' md5:' . compute_md5($pathname)
if ($self->[MD5]);
}
return $version;
}
sub compare_version
{
my ($self, $new, $old, $filename) = @_;
if ($self->[MD5] &&
( $old !~ /^SHA/ ||
($self->[P4] &&
$old !~ /^CL/)) &&
$old =~ / md5:(.+)$/
) {
my $o = $1;
if ($new =~ / md5:(.+)$/) {
return ($o ne $1);
}
# otherwise: 'new' was not an MD5 signature - so fall through to exact match
}
return $old ne $new; # just look for exact match
}
1;
|