Debian Codegraph @

scripts/getp4version

raw
1 #!/usr/bin/env perl
2 # Copyright (c) MediaTek USA Inc., 2022-2023
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or (at
7 # your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, see
16 # <http://www.gnu.org/licenses/>.
17 #
18 #
19 # getp4version
20 #
21 # This is a sample script which uses perforce commands to determine
22 # the version of the filename passed in.
23 # Version information (if present) is used during ".info" file merging
24 # to verify that the data the user is attempting to merge is for the same
25 # source code/same version.
26 # If the version is not the same - then line numbers, etc. may be different
27 # and some very strange errors may occur.
28
29 use strict;
30 use POSIX qw(strftime);
31 use Getopt::Long;
32 use File::Spec;
33 use Cwd qw(abs_path);
34 use FindBin;
35
36 use lib "$FindBin::RealBin";
37 use annotateutil qw(get_modify_time not_in_repo compute_md5);
38
39 sub usage
40 {
41 print(STDERR "usage: $0 --compare old_version new_version filename OR\n" .
42 " $0 [--md5] [--allow-missing] filename\n");
43 }
44
45 my $compare;
46 my $use_md5; # if set, append md5 checksum to the P4 version string
47 my $allow_missing;
48 my $help;
49 if (!GetOptions("--compare" => \$compare,
50 "--md5" => \$use_md5,
51 '--allow-missing' => \$allow_missing,
52 '--help' => \$help) ||
53 $help ||
54 ($compare && scalar(@ARGV) != 3) ||
55 (!$compare && scalar(@ARGV) != 1)
56 ) {
57 usage();
58 exit(defined($help) ? 0 : 1) unless caller;
59 return 1;
60 }
61
62 my $filename = $ARGV[$compare ? 2 : 0];
63
64 if ($compare) {
65 my ($old, $new) = @ARGV;
66 if ($use_md5 &&
67 $old !~ /^(\@head|#[0-9]+)/ &&
68 $old =~ / md5:(.+)$/) {
69 my $o = $1;
70 if ($new =~ / md5:(.+)$/) {
71 exit($o ne $1);
72 }
73 # otherwise: 'new' was not an MD5 signature - so fall through to exact match
74 }
75 exit($old ne $new); # for the moment, just look for exact match
76 }
77
78 unless (-e $filename) {
79 if ($allow_missing) {
80 print("\n"); # empty string
81 exit 0;
82 }
83 die("Error: $filename does not exist - perhaps you need the '--allow-missing' flag"
84 );
85 }
86 my $pathname = abs_path($filename);
87 my $null = File::Spec->devnull(); # more portable way to do it
88
89 my $version;
90 if (0 ==
91 system("p4 files $pathname 2>$null|grep -qv -- '- no such file' >$null")) {
92 my $have = `p4 have $pathname`;
93 if ($have =~ /#([0-9]+) - /) {
94 $version = "#$1";
95 } else {
96 $version = '\@head';
97 }
98
99 my $opened = `p4 opened $pathname 2>$null`;
100 if ($opened =~ /edit (default change|change (\S+)) /) {
101 # file is locally edited...append modify time to the version ID
102 $version .= ' edited ' . get_modify_time($pathname);
103 }
104 $version .= ' md5:' . compute_md5($pathname)
105 if $use_md5;
106 } else {
107 # not in P4 - just print the modify time, so we have a prayer of
108 # noticing file differences
109 $version = get_modify_time($pathname);
110 $version .= ' md5:' . compute_md5($pathname)
111 if ($use_md5);
112 }
113 print($version . "\n");
114