Completed
Push — master ( d5feb1...7c9762 )
by Charles
02:37
created

GitHandler.get_message()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2 1
"""
3
    Git manipulation
4
"""
5 1
import re
6
7 1
from git import GitCommandError, Repo
0 ignored issues
show
Configuration introduced by
The import git could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8 1
from git.exc import InvalidGitRepositoryError, NoSuchPathError
0 ignored issues
show
Configuration introduced by
The import git.exc could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
10 1
import git_app_version.helper.date as dthelper
11
12
13 1
class GitHandler(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
14
15 1
    def __init__(self, path):
16 1
        try:
17 1
            self.repo = Repo(path)
18 1
        except (InvalidGitRepositoryError, NoSuchPathError):
19 1
            raise ValueError(
20
                'The directory \'{}\' is not a git repository.'.format(path))
21
22 1
    def get_deploy_date(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
23 1
        return dthelper.utcnow()
24
25 1
    def get_version(self, commit='HEAD', default=''):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
26 1
        try:
27 1
            version = self.repo.git.describe(
28
                '--tag', '--always', commit).strip()
29 1
        except GitCommandError:
30 1
            version = ''
31
32 1
        if not version:
33 1
            version = default
34
35 1
        return version
36
37 1
    def get_branches(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
38 1
        raw = self.repo.git.branch("--no-color",
39
                                   "--remote",
40
                                   "--contains=" + commit)
41 1
        raw_branches = raw.splitlines()
42
43 1
        regex_point = re.compile(r'->')  # remove git reference pointing
44
45 1
        branches = []
46 1
        for raw_branch in raw_branches:
47 1
            branch = raw_branch.strip()
48 1
            match = regex_point.search(branch)
49 1
            if not match:
50 1
                branches.append(branch)
51
52 1
        return branches
53
54 1
    def get_top_branches(self, branches, abbrev_commit=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
55 1
        top_branch = []
56
57 1
        for branch in branches:
58 1
            if abbrev_commit == self.get_abbrev_commit(branch):
59 1
                top_branch.append(branch)
60
61 1
        return top_branch
62
63 1
    def remove_remote_prefix(self, branches):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
64 1
        regex_remote = re.compile(r'^[^/]+/')  # remove git remote prefix
65
66 1
        clean_branches = []
67 1
        for branch in branches:
68 1
            clean_branches.append(regex_remote.sub('', branch))
69
70 1
        return clean_branches
71
72 1
    def get_committer_name(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
73 1
        return self.repo.commit(commit).committer.name
74
75 1
    def get_committer_email(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
76 1
        return self.repo.commit(commit).committer.email
77
78 1
    def get_author_name(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
79 1
        return self.repo.commit(commit).author.name
80
81 1
    def get_author_email(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
82 1
        return self.repo.commit(commit).author.email
83
84 1
    def get_commit_date(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
85 1
        return self.repo.commit(commit).committed_datetime
86
87 1
    def get_author_date(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
88 1
        return self.repo.commit(commit).authored_datetime
89
90 1
    def get_full_commit(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
91 1
        return self.repo.commit(commit).hexsha
92
93 1
    def get_abbrev_commit(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
94 1
        return self.repo.commit(commit).hexsha[0:7]
95
96 1
    def get_message(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
97 1
        return self.repo.commit(commit).message.strip()
98
99 1
    def get_infos(self, commit='HEAD'):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
100 1
        deploy_date = self.get_deploy_date()
101 1
        abbrev_commit = self.get_abbrev_commit(commit)
102 1
        commit_date = self.get_commit_date(commit)
103 1
        author_date = self.get_author_date(commit)
104
105 1
        branches = self.get_branches(commit)
106 1
        top_branch = self.get_top_branches(
107
            branches=branches, abbrev_commit=abbrev_commit)
108
109 1
        return {
110
            'branches': self.remove_remote_prefix(branches),
111
            'top_branches': self.remove_remote_prefix(top_branch),
112
            'version': self.get_version(commit, default=abbrev_commit),
113
            'abbrev_commit': abbrev_commit,
114
            'full_commit': self.get_full_commit(commit),
115
            'message': self.get_message(commit),
116
            'author_name': self.get_author_name(commit),
117
            'author_email': self.get_author_email(commit),
118
            'committer_name': self.get_committer_name(commit),
119
            'committer_email': self.get_committer_email(commit),
120
            'commit_date': dthelper.iso8601_from_datetime(commit_date),
121
            'commit_timestamp': dthelper.timestamp_from_datetime(commit_date),
122
            'author_date': dthelper.iso8601_from_datetime(author_date),
123
            'author_timestamp': dthelper.timestamp_from_datetime(author_date),
124
            'deploy_date': dthelper.iso8601_from_datetime(deploy_date),
125
            'deploy_timestamp': dthelper.timestamp_from_datetime(deploy_date),
126
        }
127