Exemplo n.º 1
0
def from_git(git_directory):
    with utils.ChDir(git_directory):
        try:
            with open(os.devnull, 'w') as devnull:
                version_string = subprocess.check_output(
                    ["git", "describe", "--tags", "--long", "--abbrev=7"],
                    stderr=devnull).decode()
            return _parse_git_version(version_string,
                                      _is_modified_since_commit_in_cwd())
        except subprocess.CalledProcessError:
            # If there is no git tag, then the commits_since_tag returned by git is wrong
            # (because they consider the branch HEAD the tag and there are 0 commits since the branch head).
            # We want to return the total number of commits in the branch if there is no tag.
            total_num_commits = _total_number_of_commits_in_cwd()
            if total_num_commits > 0:
                # There is no git tag, but there are commits
                branch_name = _branch_name_in_cwd()
                commit_id = _commit_id_in_cwd()
                return versioninfo.VersionInfo(
                    git_tag_name=branch_name,
                    git_commits_since_tag=total_num_commits,
                    git_commit_id=commit_id,
                    git_tag_exists=False,
                    modified_since_commit=_is_modified_since_commit_in_cwd())
            else:
                # There are no commits yet
                branch_name = "HEAD"
                commit_id = "0"
                return versioninfo.VersionInfo(
                    git_tag_name=branch_name,
                    git_commits_since_tag=total_num_commits,
                    git_commit_id=commit_id,
                    git_tag_exists=False,
                    modified_since_commit=_cwd_is_not_empty())
    def test_output_cpp_with_version_info_with_leading_zero(self):
        expected = """
                // ---------------------------------------------------
                // This file is autogenerated by git-version.
                // DO NOT MODIFY!
                // ---------------------------------------------------

                #pragma once
                #ifndef MESSMER_GITVERSION_VERSION_H
                #define MESSMER_GITVERSION_VERSION_H

                namespace version {
                    constexpr const char *VERSION_STRING = "1.06.dev2+rev230a";
                    constexpr const char *GIT_TAG_NAME = "1.06";
                    constexpr const unsigned int GIT_COMMITS_SINCE_TAG = 2;
                    constexpr const char *GIT_COMMIT_ID = "230a";
                    constexpr bool MODIFIED_SINCE_COMMIT = false;
                    constexpr bool IS_DEV_VERSION = true;
                    constexpr bool IS_STABLE_VERSION = false;

                    constexpr const char *VERSION_COMPONENTS[] = {"1", "06"};
                    constexpr const char *VERSION_TAG = "";
                }

                #endif
            """
        actual = versioninfooutputter.to_cpp(
            versioninfo.VersionInfo("1.06", 2, "230a", True, False))
        self.assertCodeEqual(expected, actual)
Exemplo n.º 3
0
def _parse_git_version(git_version_string, modified_since_commit):
    assert (isstring(git_version_string))
    matched = re.match("^([a-zA-Z0-9\.\-/]+)-([0-9]+)-g([0-9a-f]+)$",
                       git_version_string)
    if matched:
        tag = matched.group(1)
        commits_since_tag = int(matched.group(2))
        commit_id = matched.group(3)
        return versioninfo.VersionInfo(
            git_tag_name=tag,
            git_commits_since_tag=commits_since_tag,
            git_commit_id=commit_id,
            git_tag_exists=True,
            modified_since_commit=modified_since_commit)
    else:
        raise VersionParseError(git_version_string)
    def test_output_python(self):
        expected = """
                # ---------------------------------------------------
                # This file is autogenerated by git-version.
                # DO NOT MODIFY!
                # ---------------------------------------------------

                VERSION_STRING = "versiontwo.dev2+rev230a"
                GIT_TAG_NAME = "versiontwo"
                GIT_COMMITS_SINCE_TAG = 2
                GIT_COMMIT_ID = "230a"
                MODIFIED_SINCE_COMMIT = False
                IS_DEV_VERSION = True
            """
        actual = versioninfooutputter.to_python(
            versioninfo.VersionInfo("versiontwo", 2, "230a", True, False))
        self.assertCodeEqual(expected, actual)
    def test_output_python_modified(self):
        expected = """
                # ---------------------------------------------------
                # This file is autogenerated by git-version.
                # DO NOT MODIFY!
                # ---------------------------------------------------

                VERSION_STRING = "v1.0-stable-modified"
                GIT_TAG_NAME = "v1.0-stable"
                GIT_COMMITS_SINCE_TAG = 0
                GIT_COMMIT_ID = "230a"
                MODIFIED_SINCE_COMMIT = True
                IS_DEV_VERSION = True
                IS_STABLE_VERSION = False

                VERSION_COMPONENTS = ["1", "0"]
                VERSION_TAG = "stable"
            """
        actual = versioninfooutputter.to_python(
            versioninfo.VersionInfo("v1.0-stable", 0, "230a", True, True))
        self.assertCodeEqual(expected, actual)
    def test_output_python_with_version_info_and_version_tag(self):
        expected = """
                # ---------------------------------------------------
                # This file is autogenerated by git-version.
                # DO NOT MODIFY!
                # ---------------------------------------------------

                VERSION_STRING = "v1.0alpha.dev2+rev230a"
                GIT_TAG_NAME = "v1.0alpha"
                GIT_COMMITS_SINCE_TAG = 2
                GIT_COMMIT_ID = "230a"
                MODIFIED_SINCE_COMMIT = False
                IS_DEV_VERSION = True
                IS_STABLE_VERSION = False

                VERSION_COMPONENTS = ["1", "0"]
                VERSION_TAG = "alpha"
            """
        actual = versioninfooutputter.to_python(
            versioninfo.VersionInfo("v1.0alpha", 2, "230a", True, False))
        self.assertCodeEqual(expected, actual)