forked from charleso/git-cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin.py
105 lines (98 loc) · 2.98 KB
/
checkin.py
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
"""Checkin new git changesets to Clearcase"""
from common import *
from clearcase import cc
from status import Modify, Add, Delete, Rename
import filecmp
from os import listdir
from os.path import isdir
IGNORE_CONFLICTS=False
ARGS = {
'force': 'ignore conflicts and check-in anyway',
}
def main(force=False):
global IGNORE_CONFLICTS
if force:
IGNORE_CONFLICTS=True
cc_exec(['update', '.'])
log = git_exec(['log', '--first-parent', '--reverse', '--pretty=format:%H%n%s%n%b', CI_TAG + '..'])
comment = []
id = None
def _commit():
if not id:
return
statuses = getStatuses(id)
checkout(statuses, '\n'.join(comment))
tag(CI_TAG, id)
for line in log.splitlines():
if line == "":
_commit()
comment = []
id = None
if not id:
id = line
else:
comment.append(line)
_commit()
def getStatuses(id):
status = git_exec(['diff','--name-status', '-M', '-z', '%s^..%s' % (id, id)])
types = {'M':Modify, 'R':Rename, 'D':Delete, 'A':Add, 'C':Add}
list = []
split = status.split('\x00')
while len(split) > 1:
char = split.pop(0)[0] # first char
args = [split.pop(0)]
if char == 'R':
args.append(split.pop(0))
elif char == 'C':
args = [split.pop(0)]
type = types[char](args)
type.id = id
list.append(type)
return list
def checkout(stats, comment):
"""Poor mans two-phase commit"""
failed = None
transaction = Transaction(comment)
for stat in stats:
try:
stat.stage(transaction)
except:
failed = True
break;
if failed:
transaction.rollback()
raise
for stat in stats:
stat.commit(transaction)
transaction.commit(comment);
class Transaction:
def __init__(self, comment):
self.checkedout = []
cc.mkact(comment)
def add(self, file):
self.checkedout.append(file)
def co(self, file):
cc_exec(['co', '-reserved', '-nc', file])
self.add(file)
def stageDir(self, file):
file = file if file else '.'
if file not in self.checkedout:
self.co(file)
def stage(self, file):
global IGNORE_CONFLICTS
self.co(file)
ccid = git_exec(['hash-object', join(CC_DIR, file)])[0:-1]
gitid = getBlob(git_exec(['merge-base', CI_TAG, 'HEAD']).strip(), file)
if ccid != gitid:
if not IGNORE_CONFLICTS:
raise Exception('File has been modified: %s. Try rebasing.' % file)
else:
print ('WARNING: Detected possible confilct with',file,'...ignoring...')
def rollback(self):
for file in self.checkedout:
cc_exec(['unco', '-rm', file])
cc.rmactivity()
def commit(self, comment):
for file in self.checkedout:
cc_exec(['ci', '-identical', '-c', comment, file])
cc.commit()