6 if not os
.environ
.has_key('GIT_DIR'):
7 os
.environ
['GIT_DIR'] = '.git'
10 print "'git-cat-file -t HEAD' failed: " + msg
11 print "Make sure that the current working directory contains a '.git' directory, or\nthat GIT_DIR is set appropriately."
15 runProgram('git-cat-file -t HEAD')
17 basicsFailed(e
.strerror
)
18 except ProgramError
, e
:
23 parseDiffRE
= re
.compile(':([0-9]+) ([0-9]+) ([0-9a-f]{40}) ([0-9a-f]{40}) ([MCRNADUT])([0-9]*)')
25 inp
= runProgram(prog
)
28 recs
= inp
.split("\0")
29 recs
.pop() # remove last entry (which is '')
33 m
= parseDiffRE
.match(rec
)
36 print "Unknown output from " + str(prog
) + "!: " + rec
+ "\n"
40 f
.srcMode
= m
.group(1)
41 f
.dstMode
= m
.group(2)
49 f
.srcName
= f
.dstName
= it
.next()
51 if f
.change
== 'C' or f
.change
== 'R':
53 f
.patch
= getPatch(f
.srcName
, f
.dstName
)
55 f
.patch
= getPatch(f
.srcName
)
63 # HEAD is src in the returned File objects. That is, srcName is the
64 # name in HEAD and dstName is the name in the cache.
66 files
= parseDiff('git-diff-files -z')
68 doUpdateCache(f
.srcName
)
70 files
= parseDiff('git-diff-cache -z -M --cached HEAD')
74 f
.text
= 'Copy from ' + f
.srcName
+ ' to ' + f
.dstName
76 f
.text
= 'Rename from ' + f
.srcName
+ ' to ' + f
.dstName
78 f
.text
= 'New file: ' + f
.srcName
80 f
.text
= 'Deleted file: ' + f
.srcName
82 f
.text
= 'Type change: ' + f
.srcName
88 def getPatch(file, otherFile
= None):
93 return runProgram(['git-diff-cache', '-p', '-M', '--cached', 'HEAD'] + f
)
95 def doUpdateCache(filename
):
96 runProgram(['git-update-cache', '--remove', '--add', '--replace', filename
])
98 def doCommit(filesToKeep
, fileRealNames
, msg
):
99 for file in filesToKeep
:
100 # If we have a new file in the cache which we do not want to
101 # commit we have to remove it from the cache. We will add this
102 # cache entry back in to the cache at the end of this
104 if file.change
== 'A':
105 runProgram(['git-update-cache', '--force-remove', file.srcName
])
107 runProgram(['git-update-cache', '--add', '--replace', '--cacheinfo',
108 file.srcMode
, file.srcSHA
, file.srcName
])
110 tree
= runProgram(['git-write-tree'])
112 commit
= runProgram(['git-commit-tree', tree
, '-p', 'HEAD'], msg
)
113 commit
= commit
.rstrip()
116 f
= open(os
.environ
['GIT_DIR'] + '/HEAD', 'w+')
120 raise CommitError('write to ' + os
.environ
['GIT_DIR'] + '/HEAD', e
.strerror
)
123 os
.unlink(os
.environ
['GIT_DIR'] + '/MERGE_HEAD')
127 for file in filesToKeep
:
128 # Don't add files that are going to be deleted back to the cache
129 if file.change
!= 'D':
130 runProgram(['git-update-cache', '--add', '--replace', '--cacheinfo',
131 file.dstMode
, file.dstSHA
, file.dstName
])