Skip to content

Commit 95c50e5

Browse files
authored
[3.6] bpo-27593: Get SCM build info from git instead of hg. (#446) (#454)
* bpo-27593: Get SCM build info from git instead of hg. (#446) sys.version and the platform module python_build(), python_branch(), and python_revision() functions now use git information rather than hg when building from a repo. Based on original patches by Brett Cannon and Steve Dower. (cherry picked from commit 5c4b0d0)
1 parent 793f822 commit 95c50e5

File tree

9 files changed

+86
-78
lines changed

9 files changed

+86
-78
lines changed

Include/pylifecycle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ PyAPI_FUNC(const char *) Py_GetCopyright(void);
7070
PyAPI_FUNC(const char *) Py_GetCompiler(void);
7171
PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
7272
#ifndef Py_LIMITED_API
73-
PyAPI_FUNC(const char *) _Py_hgidentifier(void);
74-
PyAPI_FUNC(const char *) _Py_hgversion(void);
73+
PyAPI_FUNC(const char *) _Py_gitidentifier(void);
74+
PyAPI_FUNC(const char *) _Py_gitversion(void);
7575
#endif
7676

7777
/* Internal -- various one-time initializations */

Lib/platform.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,9 @@ def _sys_version(sys_version=None):
11981198
elif buildtime:
11991199
builddate = builddate + ' ' + buildtime
12001200

1201-
if hasattr(sys, '_mercurial'):
1201+
if hasattr(sys, '_git'):
1202+
_, branch, revision = sys._git
1203+
elif hasattr(sys, '_mercurial'):
12021204
_, branch, revision = sys._mercurial
12031205
elif hasattr(sys, 'subversion'):
12041206
# sys.subversion was added in Python 2.5

Lib/test/test_platform.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ def test_processor(self):
6767

6868
def setUp(self):
6969
self.save_version = sys.version
70-
self.save_mercurial = sys._mercurial
70+
self.save_git = sys._git
7171
self.save_platform = sys.platform
7272

7373
def tearDown(self):
7474
sys.version = self.save_version
75-
sys._mercurial = self.save_mercurial
75+
sys._git = self.save_git
7676
sys.platform = self.save_platform
7777

7878
def test_sys_version(self):
@@ -102,7 +102,7 @@ def test_sys_version(self):
102102
('CPython', '2.4.3', '', '', 'truncation', '', 'GCC')),
103103
):
104104
# branch and revision are not "parsed", but fetched
105-
# from sys._mercurial. Ignore them
105+
# from sys._git. Ignore them
106106
(name, version, branch, revision, buildno, builddate, compiler) \
107107
= platform._sys_version(input)
108108
self.assertEqual(
@@ -149,10 +149,10 @@ def test_sys_version(self):
149149
sys_versions.items():
150150
sys.version = version_tag
151151
if subversion is None:
152-
if hasattr(sys, "_mercurial"):
153-
del sys._mercurial
152+
if hasattr(sys, "_git"):
153+
del sys._git
154154
else:
155-
sys._mercurial = subversion
155+
sys._git = subversion
156156
if sys_platform is not None:
157157
sys.platform = sys_platform
158158
self.assertEqual(platform.python_implementation(), info[0])

Makefile.pre.in

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ RANLIB= @RANLIB@
4141
READELF= @READELF@
4242
SOABI= @SOABI@
4343
LDVERSION= @LDVERSION@
44-
HGVERSION= @HGVERSION@
45-
HGTAG= @HGTAG@
46-
HGBRANCH= @HGBRANCH@
44+
GITVERSION= @GITVERSION@
45+
GITTAG= @GITTAG@
46+
GITBRANCH= @GITBRANCH@
4747
PGO_PROF_GEN_FLAG=@PGO_PROF_GEN_FLAG@
4848
PGO_PROF_USE_FLAG=@PGO_PROF_USE_FLAG@
4949
LLVM_PROF_MERGER=@LLVM_PROF_MERGER@
@@ -740,9 +740,9 @@ Modules/getbuildinfo.o: $(PARSER_OBJS) \
740740
$(MODOBJS) \
741741
$(srcdir)/Modules/getbuildinfo.c
742742
$(CC) -c $(PY_CORE_CFLAGS) \
743-
-DHGVERSION="\"`LC_ALL=C $(HGVERSION)`\"" \
744-
-DHGTAG="\"`LC_ALL=C $(HGTAG)`\"" \
745-
-DHGBRANCH="\"`LC_ALL=C $(HGBRANCH)`\"" \
743+
-DGITVERSION="\"`LC_ALL=C $(GITVERSION)`\"" \
744+
-DGITTAG="\"`LC_ALL=C $(GITTAG)`\"" \
745+
-DGITBRANCH="\"`LC_ALL=C $(GITBRANCH)`\"" \
746746
-o $@ $(srcdir)/Modules/getbuildinfo.c
747747

748748
Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Core and Builtins
2323
- Issue #28598: Support __rmod__ for subclasses of str being called before
2424
str.__mod__. Patch by Martijn Pieters.
2525

26+
- bpo-29572: Update Windows build and OS X installers to use OpenSSL 1.0.2k.
27+
2628
- bpo-29607: Fix stack_effect computation for CALL_FUNCTION_EX.
2729
Patch by Matthieu Dartiailh.
2830

@@ -276,6 +278,10 @@ Tests
276278
Build
277279
-----
278280

281+
- bpo-27593: sys.version and the platform module python_build(),
282+
python_branch(), and python_revision() functions now use
283+
git information rather than hg when building from a repo.
284+
279285
- bpo-29572: Update Windows build and OS X installers to use OpenSSL 1.0.2k.
280286

281287
- Issue #26851: Set Android compilation and link flags.

Modules/getbuildinfo.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,47 @@
2121
#endif
2222

2323
/* XXX Only unix build process has been tested */
24-
#ifndef HGVERSION
25-
#define HGVERSION ""
24+
#ifndef GITVERSION
25+
#define GITVERSION ""
2626
#endif
27-
#ifndef HGTAG
28-
#define HGTAG ""
27+
#ifndef GITTAG
28+
#define GITTAG ""
2929
#endif
30-
#ifndef HGBRANCH
31-
#define HGBRANCH ""
30+
#ifndef GITBRANCH
31+
#define GITBRANCH ""
3232
#endif
3333

3434
const char *
3535
Py_GetBuildInfo(void)
3636
{
37-
static char buildinfo[50 + sizeof(HGVERSION) +
38-
((sizeof(HGTAG) > sizeof(HGBRANCH)) ?
39-
sizeof(HGTAG) : sizeof(HGBRANCH))];
40-
const char *revision = _Py_hgversion();
37+
static char buildinfo[50 + sizeof(GITVERSION) +
38+
((sizeof(GITTAG) > sizeof(GITBRANCH)) ?
39+
sizeof(GITTAG) : sizeof(GITBRANCH))];
40+
const char *revision = _Py_gitversion();
4141
const char *sep = *revision ? ":" : "";
42-
const char *hgid = _Py_hgidentifier();
43-
if (!(*hgid))
44-
hgid = "default";
42+
const char *gitid = _Py_gitidentifier();
43+
if (!(*gitid))
44+
gitid = "default";
4545
PyOS_snprintf(buildinfo, sizeof(buildinfo),
46-
"%s%s%s, %.20s, %.9s", hgid, sep, revision,
46+
"%s%s%s, %.20s, %.9s", gitid, sep, revision,
4747
DATE, TIME);
4848
return buildinfo;
4949
}
5050

5151
const char *
52-
_Py_hgversion(void)
52+
_Py_gitversion(void)
5353
{
54-
return HGVERSION;
54+
return GITVERSION;
5555
}
5656

5757
const char *
58-
_Py_hgidentifier(void)
58+
_Py_gitidentifier(void)
5959
{
60-
const char *hgtag, *hgid;
61-
hgtag = HGTAG;
62-
if ((*hgtag) && strcmp(hgtag, "tip") != 0)
63-
hgid = hgtag;
60+
const char *gittag, *gitid;
61+
gittag = GITTAG;
62+
if ((*gittag) && strcmp(gittag, "undefined") != 0)
63+
gitid = gittag;
6464
else
65-
hgid = HGBRANCH;
66-
return hgid;
65+
gitid = GITBRANCH;
66+
return gitid;
6767
}

Python/sysmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,9 +1942,9 @@ _PySys_Init(void)
19421942
PyUnicode_FromString(Py_GetVersion()));
19431943
SET_SYS_FROM_STRING("hexversion",
19441944
PyLong_FromLong(PY_VERSION_HEX));
1945-
SET_SYS_FROM_STRING("_mercurial",
1946-
Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(),
1947-
_Py_hgversion()));
1945+
SET_SYS_FROM_STRING("_git",
1946+
Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(),
1947+
_Py_gitversion()));
19481948
SET_SYS_FROM_STRING("dont_write_bytecode",
19491949
PyBool_FromLong(Py_DontWriteBytecodeFlag));
19501950
SET_SYS_FROM_STRING("api_version",

configure

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,10 @@ build_os
760760
build_vendor
761761
build_cpu
762762
build
763-
HAS_HG
764-
HGBRANCH
765-
HGTAG
766-
HGVERSION
763+
HAS_GIT
764+
GITBRANCH
765+
GITTAG
766+
GITVERSION
767767
BASECPPFLAGS
768768
target_alias
769769
host_alias
@@ -2698,17 +2698,17 @@ fi
26982698

26992699

27002700

2701-
if test -e $srcdir/.hg/dirstate
2701+
if test -e $srcdir/.git/HEAD
27022702
then
2703-
# Extract the first word of "hg", so it can be a program name with args.
2704-
set dummy hg; ac_word=$2
2703+
# Extract the first word of "git", so it can be a program name with args.
2704+
set dummy git; ac_word=$2
27052705
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
27062706
$as_echo_n "checking for $ac_word... " >&6; }
2707-
if ${ac_cv_prog_HAS_HG+:} false; then :
2707+
if ${ac_cv_prog_HAS_GIT+:} false; then :
27082708
$as_echo_n "(cached) " >&6
27092709
else
2710-
if test -n "$HAS_HG"; then
2711-
ac_cv_prog_HAS_HG="$HAS_HG" # Let the user override the test.
2710+
if test -n "$HAS_GIT"; then
2711+
ac_cv_prog_HAS_GIT="$HAS_GIT" # Let the user override the test.
27122712
else
27132713
as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
27142714
for as_dir in $PATH
@@ -2717,39 +2717,39 @@ do
27172717
test -z "$as_dir" && as_dir=.
27182718
for ac_exec_ext in '' $ac_executable_extensions; do
27192719
if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
2720-
ac_cv_prog_HAS_HG="found"
2720+
ac_cv_prog_HAS_GIT="found"
27212721
$as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
27222722
break 2
27232723
fi
27242724
done
27252725
done
27262726
IFS=$as_save_IFS
27272727

2728-
test -z "$ac_cv_prog_HAS_HG" && ac_cv_prog_HAS_HG="not-found"
2728+
test -z "$ac_cv_prog_HAS_GIT" && ac_cv_prog_HAS_GIT="not-found"
27292729
fi
27302730
fi
2731-
HAS_HG=$ac_cv_prog_HAS_HG
2732-
if test -n "$HAS_HG"; then
2733-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $HAS_HG" >&5
2734-
$as_echo "$HAS_HG" >&6; }
2731+
HAS_GIT=$ac_cv_prog_HAS_GIT
2732+
if test -n "$HAS_GIT"; then
2733+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $HAS_GIT" >&5
2734+
$as_echo "$HAS_GIT" >&6; }
27352735
else
27362736
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
27372737
$as_echo "no" >&6; }
27382738
fi
27392739

27402740

27412741
else
2742-
HAS_HG=no-repository
2742+
HAS_GIT=no-repository
27432743
fi
2744-
if test $HAS_HG = found
2744+
if test $HAS_GIT = found
27452745
then
2746-
HGVERSION="hg id -i \$(srcdir)"
2747-
HGTAG="hg id -t \$(srcdir)"
2748-
HGBRANCH="hg id -b \$(srcdir)"
2746+
GITVERSION="git -C \$(srcdir) rev-parse HEAD"
2747+
GITTAG="git -C \$(srcdir) name-rev --tags --name-only HEAD"
2748+
GITBRANCH="git -C \$(srcdir) name-rev --name-only HEAD"
27492749
else
2750-
HGVERSION=""
2751-
HGTAG=""
2752-
HGBRANCH=""
2750+
GITVERSION=""
2751+
GITTAG=""
2752+
GITBRANCH=""
27532753
fi
27542754

27552755

configure.ac

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ else
2525
BASECPPFLAGS=""
2626
fi
2727

28-
AC_SUBST(HGVERSION)
29-
AC_SUBST(HGTAG)
30-
AC_SUBST(HGBRANCH)
28+
AC_SUBST(GITVERSION)
29+
AC_SUBST(GITTAG)
30+
AC_SUBST(GITBRANCH)
3131

32-
if test -e $srcdir/.hg/dirstate
32+
if test -e $srcdir/.git/HEAD
3333
then
34-
AC_CHECK_PROG(HAS_HG, hg, found, not-found)
34+
AC_CHECK_PROG(HAS_GIT, git, found, not-found)
3535
else
36-
HAS_HG=no-repository
36+
HAS_GIT=no-repository
3737
fi
38-
if test $HAS_HG = found
38+
if test $HAS_GIT = found
3939
then
40-
HGVERSION="hg id -i \$(srcdir)"
41-
HGTAG="hg id -t \$(srcdir)"
42-
HGBRANCH="hg id -b \$(srcdir)"
40+
GITVERSION="git -C \$(srcdir) rev-parse HEAD"
41+
GITTAG="git -C \$(srcdir) name-rev --tags --name-only HEAD"
42+
GITBRANCH="git -C \$(srcdir) name-rev --name-only HEAD"
4343
else
44-
HGVERSION=""
45-
HGTAG=""
46-
HGBRANCH=""
44+
GITVERSION=""
45+
GITTAG=""
46+
GITBRANCH=""
4747
fi
4848

4949
AC_CONFIG_SRCDIR([Include/object.h])

0 commit comments

Comments
 (0)