charleso / git-cc

Bridge for Git and Clearcase

This URL has Read+Write access

strotz (author)
Mon Jul 13 14:34:08 -0700 2009
charleso (committer)
Mon Jul 13 14:40:55 -0700 2009
git-cc / common.py
100644 137 lines (115 sloc) 3.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from distutils import __version__
v30 = __version__.find("3.") == 0
 
from subprocess import Popen, PIPE
import os, sys
from os.path import join, exists, abspath, dirname
if v30:
    from configparser import SafeConfigParser
else:
    from ConfigParser import SafeConfigParser
 
CFG_CC = 'clearcase'
CC_DIR = None
ENCODING = sys.stdin.encoding
DEBUG = False
 
def fail(string):
    print(string)
    sys.exit(2)
 
def doStash(f, stash):
    if(stash):
        git_exec(['stash'])
    f()
    if(stash):
        git_exec(['stash', 'pop'])
 
def debug(string):
    if DEBUG:
        print(string)
 
def git_exec(cmd, **args):
    return popen('git', cmd, GIT_DIR, **args)
 
def cc_exec(cmd, **args):
    return popen('cleartool', cmd, CC_DIR, **args)
 
def popen(exe, cmd, cwd, env=None, decode=True, errors=True):
    cmd.insert(0, exe)
    if DEBUG:
        debug('> ' + ' '.join(cmd))
    pipe = Popen(cmd, cwd=cwd, stdout=PIPE, stderr=PIPE, env=env)
    (stdout, stderr) = pipe.communicate()
    if errors and pipe.returncode > 0:
        raise Exception((stderr + stdout).decode(ENCODING))
    return stdout if not decode else stdout.decode(ENCODING)
 
def tag(tag, id="HEAD"):
    git_exec(['tag', '-f', tag, id])
 
def reset(tag=None):
    git_exec(['reset', '--hard', tag or CC_TAG])
 
def getBlob(sha, file):
    return git_exec(['ls-tree', '-z', sha, file]).split(' ')[2].split('\t')[0]
 
def gitDir():
    def findGitDir(dir):
        if not exists(dir) or dirname(dir) == dir:
            return '.'
        if exists(join(dir, '.git')):
            return dir
        return findGitDir(dirname(dir))
    return findGitDir(abspath('.'))
 
def getCurrentBranch():
    for branch in git_exec(['branch']).split('\n'):
        if branch.startswith('*'):
            branch = branch[2:]
            if branch == '(no branch)':
                fail("Why aren't you on a branch?")
            return branch
    return ""
 
class GitConfigParser():
    CORE = 'core'
    def __init__(self, branch):
        self.section = branch
        self.file = join(GIT_DIR, '.git', 'gitcc')
        self.parser = SafeConfigParser();
        self.parser.add_section(self.section)
    def set(self, name, value):
        self.parser.set(self.section, name, value)
    def read(self):
        self.parser.read(self.file)
    def write(self):
        self.parser.write(open(self.file, 'w'))
    def getCore(self, name, *args):
        return self._get(self.CORE, name, *args)
    def get(self, name, *args):
        return self._get(self.section, name, *args)
    def _get(self, section, name, default=None):
        if not self.parser.has_option(section, name):
            return default
        return self.parser.get(section, name)
    def getList(self, name, default=None):
        return self.get(name, default).split('|')
    def getInclude(self):
        return self.getCore('include', '.').split('|')
    def getBranches(self):
        return self.getList('branches', 'main')
    def getExtraBranches(self):
        return self.getList('_branches', 'main')
 
def write(file, blob):
    _write(file, blob)
 
def _write(file, blob):
    f = open(file, 'wb')
    f.write(blob)
    f.close()
 
def mkdirs(file):
    dir = dirname(file)
    if not exists(dir):
        os.makedirs(dir)
 
def removeFile(file):
    if exists(file):
        os.remove(file)
 
def validateCC():
    if not CC_DIR:
        fail("No 'clearcase' variable found for branch '%s'" % CURRENT_BRANCH)
 
GIT_DIR = gitDir()
if not exists(join(GIT_DIR, '.git')):
    fail("fatal: Not a git repository (or any of the parent directories): .git")
CURRENT_BRANCH = getCurrentBranch() or 'master'
cfg = GitConfigParser(CURRENT_BRANCH)
cfg.read()
CC_DIR = cfg.get(CFG_CC)
DEBUG = cfg.getCore('debug', True)
CC_TAG = CURRENT_BRANCH + '_cc'
CI_TAG = CURRENT_BRANCH + '_ci'