2 # Copyright (C) 2005 Fredrik Kuivinen
5 import sys
, re
, os
, traceback
9 printList(args
, sys
.stderr
)
12 def printList(list, file=sys
.stdout
):
24 functionsToDebug
= Set()
28 functionsToDebug
.add(func
)
30 functionsToDebug
.add(func
.func_name
)
34 funcName
= traceback
.extract_stack()[-2][2]
35 if funcName
in functionsToDebug
:
41 class ProgramError(Exception):
42 def __init__(self
, progStr
, error
):
43 self
.progStr
= progStr
47 return self
.progStr
+ ': ' + self
.error
49 addDebug('runProgram')
50 def runProgram(prog
, input=None, returnCode
=False, env
=None, pipeOutput
=True):
51 debug('runProgram prog:', str(prog
), 'input:', str(input))
55 progStr
= ' '.join(prog
)
59 stderr
= subprocess
.STDOUT
60 stdout
= subprocess
.PIPE
64 pop
= subprocess
.Popen(prog
,
65 shell
= type(prog
) is str,
68 stdin
=subprocess
.PIPE
,
71 debug('strerror:', e
.strerror
)
72 raise ProgramError(progStr
, e
.strerror
)
75 pop
.stdin
.write(input)
79 out
= pop
.stdout
.read()
88 if code
!= 0 and not returnCode
:
89 debug('error output:', out
)
91 raise ProgramError(progStr
, out
)
92 # debug('output:', out.replace('\0', '\n'))
95 # Code for computing common ancestors
96 # -----------------------------------
104 # The 'virtual' commit objects have SHAs which are integers
105 shaRE
= re
.compile('^[0-9a-f]{40}$')
107 return (type(obj
) is str and bool(shaRE
.match(obj
))) or \
108 (type(obj
) is int and obj
>= 1)
111 def __init__(self
, sha
, parents
, tree
=None):
112 self
.parents
= parents
113 self
.firstLineMsg
= None
122 self
.sha
= getUniqueId()
124 self
.firstLineMsg
= 'virtual commit'
128 self
.sha
= sha
.rstrip()
129 assert(isSha(self
.sha
))
133 assert(self
._tree
!= None)
138 return str(self
.sha
) + ' ' + self
.firstLineMsg
141 return self
.shortInfo()
144 if self
.virtual
or self
.firstLineMsg
!= None:
147 info
= runProgram(['git-cat-file', 'commit', self
.sha
])
148 info
= info
.split('\n')
152 self
.firstLineMsg
= l
155 if l
.startswith('tree'):
156 self
._tree
= l
[5:].rstrip()
165 def addNode(self
, node
):
166 assert(isinstance(node
, Commit
))
167 self
.shaMap
[node
.sha
] = node
168 self
.commits
.append(node
)
169 for p
in node
.parents
:
170 p
.children
.append(node
)
173 def reachableNodes(self
, n1
, n2
):
184 def fixParents(self
, node
):
185 for x
in range(0, len(node
.parents
)):
186 node
.parents
[x
] = self
.shaMap
[node
.parents
[x
]]
188 # addDebug('buildGraph')
189 def buildGraph(heads
):
190 debug('buildGraph heads:', heads
)
196 out
= runProgram(['git-rev-list', '--parents'] + heads
)
197 for l
in out
.split('\n'):
202 # This is a hack, we temporarily use the 'parents' attribute
203 # to contain a list of SHA1:s. They are later replaced by proper
205 c
= Commit(shas
[0], shas
[1:])
218 # Write the empty tree to the object database and return its SHA1
219 def writeEmptyTree():
220 tmpIndex
= os
.environ
.get('GIT_DIR', '.git') + '/merge-tmp-index'
227 newEnv
= os
.environ
.copy()
228 newEnv
['GIT_INDEX_FILE'] = tmpIndex
229 res
= runProgram(['git-write-tree'], env
=newEnv
).rstrip()
233 def addCommonRoot(graph
):
235 for c
in graph
.commits
:
236 if len(c
.parents
) == 0:
239 superRoot
= Commit(sha
=None, parents
=[], tree
=writeEmptyTree())
240 graph
.addNode(superRoot
)
242 r
.parents
= [superRoot
]
243 superRoot
.children
= roots
246 def getCommonAncestors(graph
, commit1
, commit2
):
247 '''Find the common ancestors for commit1 and commit2'''
248 assert(isinstance(commit1
, Commit
) and isinstance(commit2
, Commit
))
250 def traverse(start
, set):
252 while len(stack
) > 0:
260 traverse(commit1
, h1Set
)
261 traverse(commit2
, h2Set
)
262 shared
= h1Set
.intersection(h2Set
)
265 shared
= [addCommonRoot(graph
)]
270 if len([c
for c
in s
.children
if c
in shared
]) == 0: