git.py 3.42 KB
Newer Older
Samuel Sirois's avatar
Samuel Sirois committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from os import path

from fabric.api import task, env, local
from fabric.colors import red, green, yellow
from fabric.api import task, env, execute
from .environments import e

import helpers as h
import re

@task(alias='is_dirty')
def check_status():
    """
    Check workspace's git repositories status.
    """

    if (isGitDirty()):
      print red('Your workspace is not clean.')
    else:
      print green('Your workspace is clean.')
21

Samuel Sirois's avatar
Samuel Sirois committed
22
def isGitDirty():
23
    repos = local('find ' + path.normpath(env.workspace) + ' -name ".git"', capture=True).splitlines()
Samuel Sirois's avatar
Samuel Sirois committed
24
25
    nbWarnings = 0
    for repo in repos:
26
        repoLocalPath = path.normpath(path.join(repo, '..'))
Samuel Sirois's avatar
Samuel Sirois committed
27
        with h.fab_cd('local', repoLocalPath):
28
            print green('---')
Samuel Sirois's avatar
Samuel Sirois committed
29
30
31
32
            print green('Verify repo in ' + repoLocalPath)

            remoteName = local('git remote', capture=True)

Philippe Mouchel's avatar
Philippe Mouchel committed
33
            print green('Verify local branches exist on remote...');
Samuel Sirois's avatar
Samuel Sirois committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
            localBranchesRawInfo = _getLocalBranchesInformation()
            for localBranchRawInfo in localBranchesRawInfo:
                localBranchName = _getBranchName(localBranchRawInfo)
                if ((localBranchName is not None) and (not _remoteBranchExists(localBranchName))):
                    nbWarnings += 1
                    print yellow('Local branch "' + localBranchName + '" is not present on "' + remoteName + '" remote.')

            print green('Verify branches status against remote...');
            pattern = re.compile('.*\[.* ahead .*\].*')
            for localBranchRawInfo in localBranchesRawInfo:
                if (pattern.match(localBranchRawInfo)):
                    nbWarnings += 1
                    print yellow('Local branch "' + _getBranchName(localBranchRawInfo) + '" is ahead of remote branch.');

            print green('Verify local files status against current HEAD commit...')
            filesStatusRawInfo = _getFilesStatusInformation()
            if (len(filesStatusRawInfo) > 0):
                for fileStatus in filesStatusRawInfo:
                    fileStatusData = fileStatus.split()
53
54
55
                    # Break loop if filename is "fabfile"
                    if fileStatusData[1] == 'fabfile':
                        break
Samuel Sirois's avatar
Samuel Sirois committed
56
57
58
59
60
61
                    nbWarnings += 1
                    print yellow('File "' + fileStatusData[1] + '" ' + {
                        'M': 'has un-commited modifications.',
                        '??': 'is not indexed.',
                    }.get(fileStatusData[0], 'is in an unknown state (' + fileStatusData[0] + ')'))

62
    return (nbWarnings > 0)
Samuel Sirois's avatar
Samuel Sirois committed
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


    # STEP 2
    # plutot que s'arreter ou continuer betement, on est intelligent et on demande quoi faire:
    # - s'il y a du code non-stage, en faire un commit
    # - si la Branch n'est pas trackee, la pusher
    # - s'il y a des commits non pushes, les pusher

def _getLocalBranchesInformation():
    return local('git branch --list -vv', capture=True).splitlines()

def _getBranchName(branchRawData):
    branchName = branchRawData.replace('*', '').strip()
    if (_isBranchDetached(branchName)):
        return None
    else:
        return branchName.split()[0]

def _isBranchDetached(branchName):
    pattern = re.compile('\(.*\)')
    return pattern.match(branchName)


def _remoteBranchExists(branchName):
    return (len(local('git branch --list --remote "*' + branchName + '"', capture=True).splitlines()) > 0)


def _getFilesStatusInformation():
    return local('git status -s', capture=True).splitlines()