gitar

RepoInfo.py

1
""" RepoInfo.py - Contains a series of functions to retrieve info on a given repo.
2
    Copyright © 2016 Maarten "Vngngdn" Vangeneugden
3
4
    This program is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Affero General Public License as
6
    published by the Free Software Foundation, either version 3 of the
7
    License, or (at your option) any later version.
8
9
    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Affero General Public License for more details.
13
14
    You should have received a copy of the GNU Affero General Public License
15
    along with this program. If not, see https://www.gnu.org/licenses/agpl.html.
16
"""
17
18
from ..models import Repository
19
import git
20
21
def get_description(repository):
22
    """ Returns the Git repo description of the given repository.
23
    """
24
    if isinstance(repository, Repository):
25
        repository = git.Repo(repository.directory_path)
26
27
    return repository.description
28
29
def log_message(repository, commit, simple=True):
30
    """ Returns the log message that was attached to the given commit.
31
32
    Keyword arguments:
33
    repository -- the repository in which to search
34
    commit     -- the specific commit hash
35
    simple     -- whether to return a oneliner, or the entire message (default True)
36
    """
37
    pass
38
39
40
def get_repository_model(repository_name):
41
    repository = Repository.objects.get(directory_path__endswith=repository_name+".git")
42
    return repository
43
    
44
def get_repository_object(repository_name):
45
    """ Checks the database for a repository with the same name, and returns a
46
    GitPython Repo object.
47
48
    Given the name of the repository, this function will search the database for
49
    a repository whoms name corresponds with the given name. When it found one, 
50
51
    Keyword arguments:
52
    repository_name -- The name of the repository
53
    """
54
    # Next line raises a Repository.DoesNotExist exception if not found, so it's
55
    # not necessary to check whether it was found or not.
56
    repository = Repository.objects.get(directory_path__endswith=repository_name+".git")
57
58
    return git.Repo(repository.directory_path)
59
60
def get_repository_model_object(repository_name):
61
    """ Functions identical to the get_repository_object, except that this
62
    function returns the Django model representation.
63
64
    Keyword arguments:
65
    repository_name -- The name of the repository
66
    """
67
    return Repository.objects.get(directory_path__endswith=repository_name+".git")
68
69
def read_file(file_blob):
70
    """ Reads the contents of the given file, and returns it in a list of
71
    strings.
72
73
    Reading the contents of a file using GitPython is a bit cumbersome. This
74
    function takes care of the hassle, and returns a list of unicode strings,
75
    allowing easy operations on the file's contents.
76
    """
77
78
    file_data_stream = file_blob.data_stream
79
    file_content = file_data_stream.read().decode("utf-8")
80
    file_formatted_content = []
81
    line = ""
82
    for character in file_content:
83
        if character != "\n":
84
            line = line + character
85
        else:
86
            file_formatted_content.append(line)
87
            line = ""
88
    return file_formatted_content
89
90
def get_branches(repository):
91
    """ Returns all branch objects of the repository.
92
    """
93
    return repository.heads
94
95
def get_branch_by_name(repository, name):
96
    """ Returns the branch of the repository with the given name.
97
    """
98
    heads = repository.heads
99
    for head in heads:
100
        if head.name == name:
101
            return head
102
103
def get_commits_of_all_branches(repository):
104
    """ Returns a dict with the keys being the branch names, and the values
105
    being their commits.
106
    """
107
    heads = repository.heads
108
    branches = dict()
109
    for head in heads:
110
        branches[head.name] = get_commits(repository, head.name)
111
    return branches
112
def get_commits(repository, branch="master"):
113
    """ Returns all commits of the given repository.
114
    If branch is unspecified, the commits of the master branch are returned.
115
    """
116
    return repository.iter_commits(branch)
117