public
Description: Simple shell script to import svn:externals into a local git-svn repository
Homepage:
Clone URL: git://github.com/andrep/git-svn-clone-externals.git
git-svn-clone-externals / git-svn-check-unpushed
100755 94 lines (78 sloc) 2.806 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
 
from commands import getoutput
import sys
import logging
 
logger = logging.getLogger()
console = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)-8s %(message)s')
console.setFormatter(formatter)
logger.addHandler(console)
logger.level = logging.INFO
 
 
def list_references():
    """List the references in the local repo.
 
Return a tuple with the SHA1 of HEAD and a dictionary with the
SHA1 of the references as keys and the reference name as value.
"""
    references = getoutput('git show-ref --head')
    HEAD_ref = None
    refs = {}
    for item in references.split('\n'):
        sha1, name = item.split()
        if name == 'HEAD':
            HEAD_ref = sha1
        refs[sha1] = name
    return HEAD_ref, refs
 
 
def commit_objects():
    """List commit objects in reverse chronological order.
 
Return a dict with the SHA1 of the commits as keys and the SHA1 of
the parents as values.
"""
    commit_list = getoutput('git rev-list --all --parents')
    commits = {}
    for item in commit_list.split('\n'):
        splitted_item = item.split()
        if len(splitted_item) != 2:
            commit = splitted_item[0]
            parent = None
        else:
            commit, parent = splitted_item
        commits[commit] = parent
    return commits
 
 
def find_svn_branch_name():
    """Return the reference name of the current remote branch."""
    head, references = list_references()
    commits = commit_objects()
 
    current_commit = head
    while current_commit:
        if current_commit in references:
            reference_name = references[current_commit]
            if ('remote' in reference_name or
                'trunk' in reference_name or
                'git-svn' in reference_name):
                logger.debug('Found remote: %s', reference_name)
                return reference_name
        # find the parent of the current commit
        # and make it the next commit to investigate
        if current_commit in commits:
            current_commit = commits[current_commit]
    return None
 
 
def find_uncommitted(svn_branch):
    """Given the name of the remote branch, show log of the commits."""
    output = getoutput('git log --pretty="format:%%h %%s" %s..HEAD' %
                       svn_branch)
    if output:
        print 'Possible unpushed commits (against %s):' % svn_branch
        print output
    else:
        print 'No unpushed commits found.'
 
 
if __name__ == '__main__':
    status = getoutput('git status')
    if status.startswith('fatal'):
        print status
        sys.exit(1)
    svn_branch = find_svn_branch_name()
    if svn_branch is None:
        print "No svn branch found"
        sys.exit(1)
    logger.debug('Found branch: %s', svn_branch)
    find_uncommitted(svn_branch)