1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package RepoRevision;
25
26sub DetectRevisionIdFromFile ($)
27{
28	my $path = shift;
29	my $id = undef;
30
31	open( my $fh, '<', $path ) || return undef;
32	$id = <$fh>;
33	close $fh;
34	return $id;
35}
36
37sub DetectRevisionIdFromGit ($)
38{
39	my $path = shift;
40	my $id = undef;
41
42	$id = `git log -1 --pretty=format:%h --abbrev=10`;
43	return $id;
44}
45
46sub DetectRevisionIdFromSVN ($)
47{
48	my $path = shift;
49	my $id = undef;
50
51	open my $proc, "cd $path && svn info 2>\&1 |";
52	while (<$proc>)
53	{
54		if (/svn: E155007:/ || /svn: '.' is not a working copy/)
55		{
56			# Not in an SVN repository.
57			return undef;
58		}
59		else
60		{
61			if (/Last Changed Rev:\s+([0-9]+)/)
62			{
63				$id = $1;
64				last;
65			}
66		}
67	}
68	close $proc;
69	return $id;
70}
71
72
73sub DetectRevisionId ($)
74{
75	my $path = shift;
76	my $id = undef;
77
78	#NOTE: Magic cookie file 'reporevision.lst' created by aoo_srcrelease
79	$id = DetectRevisionIdFromFile ("$ENV{'SOLARENV'}/inc/reporevision.lst");
80	if ($id) { return $id };
81
82	my $NotGit = `cd $path && git rev-parse --git-dir > /dev/null 2>&1`;
83	if (!$NotGit || -d ".git" || -d "$path/.git")
84	{
85		$id = DetectRevisionIdFromGit ($path);
86	}
87	else
88	{
89		$id = DetectRevisionIdFromSVN ($path);
90	}
91
92	if (!$id) { $id = "unknown-rev" };
93	return $id;
94}
95
961;
97