Statistics
| Branch: | Tag: | Revision:

root / devflow / utils.py @ d0c4fc17

History | View | Annotate | Download (3.9 kB)

1
# Copyright (C) 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import os
35
import git
36
from collections import namedtuple
37

    
38

    
39
def get_repository():
40
    """Load the repository from the current working dir."""
41

    
42
    try:
43
        return git.Repo(".")
44
    except git.InvalidGitRepositoryError:
45
        msg = "Cound not retrivie git information. Directory '%s'"\
46
              " is not a git repository!" % os.getcwd()
47
        raise RuntimeError(msg)
48

    
49

    
50
def get_vcs_info():
51
    """Return current git HEAD commit information.
52

53
    Returns a tuple containing
54
        - branch name
55
        - commit id
56
        - commit count
57
        - git describe output
58
        - path of git toplevel directory
59

60
    """
61

    
62
    repo = get_repository()
63
    branch = repo.head.reference
64
    revid = get_commit_id(branch.commit, branch)
65
    revno = len(list(repo.iter_commits()))
66
    toplevel = repo.working_dir
67

    
68
    info = namedtuple("vcs_info", ["branch", "revid", "revno",
69
                                   "toplevel"])
70

    
71
    return info(branch=branch.name, revid=revid, revno=revno,
72
                toplevel=toplevel)
73

    
74

    
75
def get_commit_id(commit, current_branch):
76
    """Return the commit ID
77

78
    If the commit is a 'merge' commit, and one of the parents is a
79
    debian branch we return a compination of the parents commits.
80

81
    """
82
    def short_id(commit):
83
        return commit.hexsha[0:7]
84

    
85
    parents = commit.parents
86
    cur_br_name = current_branch.name
87
    if len(parents) == 1:
88
        return short_id(commit)
89
    elif len(parents) == 2:
90
        if cur_br_name.startswith("debian-") or cur_br_name == "debian":
91
            pr1, pr2 = parents
92
            return short_id(pr1) + "_" + short_id(pr2)
93
        else:
94
            return short_id(commit)
95
    else:
96
        raise RuntimeError("Commit %s has more than 2 parents!" % commit)
97

    
98

    
99
def get_debian_branch(branch):
100
    """Find the corresponding debian- branch"""
101
    if branch == "master":
102
        return "debian"
103
    # Check if debian-branch exists (local or origin)
104
    deb_branch = "debian-" + branch
105
    if _get_branch(deb_branch) or _get_branch("origin/" + deb_branch):
106
        return deb_branch
107
    return "debian"
108

    
109

    
110
def _get_branch(branch):
111
    repo = get_repository()
112
    if branch in repo.branches:
113
        return branch
114
    origin_branch = "origin/" + branch
115
    if origin_branch in repo.refs:
116
        print "Creating branch '%s' to track '%s'" (branch, origin_branch)
117
        repo.git.branch(branch, origin_branch)
118
        return branch
119
    else:
120
        return None