Skip to content

Commit 982d423

Browse files
authored
Merge pull request #12145 from velconia/update_version_via_branch_name
Update version via branch name
2 parents a188b03 + 3e30d40 commit 982d423

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

cmake/version.cmake

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,42 @@ set(tmp_version "HEAD")
44
set(TAG_VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(\\.(a|b|rc)\\.[0-9]+)?")
55
set(COMMIT_VERSION_REGEX "[0-9a-f]+[0-9a-f]+[0-9a-f]+[0-9a-f]+[0-9a-f]+")
66
while ("${PADDLE_VERSION}" STREQUAL "")
7+
# Check current branch name
78
execute_process(
8-
COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 --always ${tmp_version}
9+
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref ${tmp_version}
910
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
10-
OUTPUT_VARIABLE GIT_TAG_NAME
11-
RESULT_VARIABLE GIT_RESULT
11+
OUTPUT_VARIABLE GIT_BRANCH_NAME
12+
RESULT_VARIABLE GIT_BRANCH_RESULT
1213
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
13-
if (NOT ${GIT_RESULT})
14-
# Check the tag is a correct version
15-
if (${GIT_TAG_NAME} MATCHES "${COMMIT_VERSION_REGEX}")
16-
# if no tag was found, set PADDLE_VERSION to latest
17-
set(PADDLE_VERSION "latest")
18-
elseif (${GIT_TAG_NAME} MATCHES "v${TAG_VERSION_REGEX}")
19-
string(REPLACE "v" "" PADDLE_VERSION ${GIT_TAG_NAME})
20-
else() # otherwise, get the previous git tag name.
21-
set(tmp_version "${GIT_TAG_NAME}~1")
14+
if (NOT ${GIT_BRANCH_RESULT})
15+
execute_process(
16+
COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 --always ${tmp_version}
17+
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
18+
OUTPUT_VARIABLE GIT_TAG_NAME
19+
RESULT_VARIABLE GIT_RESULT
20+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
21+
if (NOT ${GIT_RESULT})
22+
# Check if current branch is release branch
23+
if (${GIT_BRANCH_NAME} MATCHES "release/${TAG_VERSION_REGEX}")
24+
# Check the tag is a correct version
25+
if (${GIT_TAG_NAME} MATCHES "${COMMIT_VERSION_REGEX}")
26+
# if no tag was found, set PADDLE_VERSION to latest
27+
set(PADDLE_VERSION "latest")
28+
elseif (${GIT_TAG_NAME} MATCHES "v${TAG_VERSION_REGEX}")
29+
string(REPLACE "v" "" PADDLE_VERSION ${GIT_TAG_NAME})
30+
else() # otherwise, get the previous git tag name.
31+
set(tmp_version "${GIT_TAG_NAME}~1")
32+
endif()
33+
else() # otherwise, we always set PADDLE_VERSION to latest
34+
set(PADDLE_VERSION "latest")
35+
endif()
36+
else()
37+
set(PADDLE_VERSION "0.0.0")
38+
message(WARNING "Cannot add paddle version from git tag")
2239
endif()
2340
else()
2441
set(PADDLE_VERSION "0.0.0")
25-
message(WARNING "Cannot add paddle version from git tag")
42+
message(WARNING "Cannot add paddle version for wrong git branch result")
2643
endif()
2744
endwhile()
2845

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
import re
17+
18+
import paddle.version as fluid_version
19+
20+
21+
class VersionTest(unittest.TestCase):
22+
def setUp(self):
23+
self._major_regex = "[0-9]+"
24+
self._minor_regex = "[0-9]+"
25+
self._patch_regex = "[0-9]+(\\.(a|b|rc)\\.[0-9]+)?"
26+
self._rc_regex = "[0-9]+"
27+
self._version_regex = "[0-9]+\\.[0-9]+\\.[0-9]+(\\.(a|b|rc)\\.[0-9]+)?"
28+
self._commit_regex = "[0-9a-f]{5,49}"
29+
30+
def test_check_output(self):
31+
# check commit format
32+
self.assertTrue(re.match(self._commit_regex, fluid_version.commit))
33+
self.assertTrue(isinstance(fluid_version.istaged, bool))
34+
35+
# check version format
36+
if fluid_version.istaged:
37+
self.assertEqual(fluid_version.major, 0)
38+
self.assertEqual(fluid_version.minor, 0)
39+
self.assertEqual(fluid_version.patch, "0")
40+
self.assertEqual(fluid_version.rc, 0)
41+
self.assertEqual(fluid_version.full_version, "0.0.0")
42+
else:
43+
self.assertTrue(re.match(self._major_regex, fluid_version.major))
44+
self.assertTrue(re.match(self._minor_regex, fluid_version.minor))
45+
self.assertTrue(re.match(self._patch_regex, fluid_version.patch))
46+
self.assertTrue(re.match(self._rc_regex, fluid_version.rc))
47+
self.assertTrue(
48+
re.match(self._version_regex, fluid_version.full_version))

0 commit comments

Comments
 (0)