public
Description: Collection of scripts for performing generic git tasks
Clone URL: git://github.com/farktronix/gittools.git
gittools / git-bbdiff
100755 298 lines (241 sloc) 10.99 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
#
# git-bbdiff
#
# Created by Dan Preston on 4/09/08.
#
 
import sys, commands, os, tempfile, filecmp
from optparse import OptionParser
 
gToolName = "git-bbdiff"
gdiffVersion = gToolName + " version 1.2"
gTempDir = "/tmp/"
gConflictMarker = ".CONFLICT"
gTheirsMarker = ".THEIRS"
 
#----------------------------------------------------------------------------------------------------------------------------
#  fileNotPresentError
#----------------------------------------------------------------------------------------------------------------------------
 
def fileNotPresentError(repositoryPath, revision, chatty=True):
  if chatty:
    print "\tFile \"%s\" does not exist in repository at revision \"%s\"." % (repositoryPath, revision)
 
 
#----------------------------------------------------------------------------------------------------------------------------
#  createTempFileForRevision
#----------------------------------------------------------------------------------------------------------------------------
 
def createTempFileForRevision( revision, repositoryPath, chatty=True ):
  # Escape the path with quotes to handle the case where we have spaces.
  command = "git show " + revision + ':"' + repositoryPath + '"'
  
  # Use git show to get the contents of the head of the file.
  status, output = commands.getstatusoutput( command )
  if status != 0:
    fileNotPresentError( repositoryPath, revision, chatty )
    return None
  
  # git show doesn't seem to append the final carriage return.
  output = output + "\n"
  
  # Create the temp file name and write out the temp file.
  nameRoot, extension = os.path.splitext( repositoryPath )
  _, fileName = os.path.split( nameRoot )
  temp = tempfile.mktemp( "_" + fileName + extension, gToolName + "-" + revision + "_", gTempDir )
  f = open( temp, 'w' )
  f.write( output )
  f.close()
  
  return temp
 
 
#----------------------------------------------------------------------------------------------------------------------------
#  performDiff
#----------------------------------------------------------------------------------------------------------------------------
 
def performDiff(compareFile, revision, chatty=True):
  # Check that we have enough arguments.
  
  revision, _, revision2 = revision.partition("..")
  
  realpath = os.path.abspath( compareFile )
  
  head, relativePath = os.path.split( realpath )
  fileName = relativePath
  
  if os.path.isdir( realpath ):
    if chatty:
      print "\t\"" + relativePath + "\" is a directory. " + gToolName + " only compares files."
    return
  
  found = False;
  # Figure out what the relative path of the file is from the .git folder.
  while (found == False) and (head != "/"):
    gitPath = os.path.join( head, ".git" )
    found = os.path.exists( gitPath )
    if found == False:
      head, tail = os.path.split( head )
      relativePath = os.path.join( tail, relativePath )
  
  # Make sure we are actually in a git repository.
  if (found == False) and (head == "/"):
    if chatty:
      print "\tThat file is not in a git repository."
    return
  
  file1 = createTempFileForRevision( revision, relativePath, chatty )
  if revision2:
    file2 = createTempFileForRevision( revision2, relativePath, chatty )
  else:
    file2 = realpath
    if os.path.exists(file2) == False:
      fileNotPresentError( relativePath, "HEAD", chatty )
      file2 = None
    
  if (file1 == None) or (file2 == None):
    return
  
  same = filecmp.cmp( file2, file1 )
  if same:
    if chatty:
      print "\tThe file \"" + fileName + "\" has no differences."
  else:
    # Escape the files with quotes to allow for files with spaces.
    command = 'bbdiff "%s" "%s"' % ( file2, file1 )
    status, output = commands.getstatusoutput( command )
 
 
#----------------------------------------------------------------------------------------------------------------------------
#  cleanTmpDirectory
#----------------------------------------------------------------------------------------------------------------------------
 
def cleanTmpDirectory(chatty=True):
  tempContents = os.listdir( gTempDir )
  for tmpFile in tempContents:
    if tmpFile.startswith( gToolName + "-" ):
      filePath = gTempDir + tmpFile
      if chatty:
        print "\tRemoving temp file:\"%s\"" % filePath
      os.remove( filePath )
 
#----------------------------------------------------------------------------------------------------------------------------
#  numConflicts
#----------------------------------------------------------------------------------------------------------------------------
 
def numConflicts(conflictFile):
  foundStart = False
  foundMiddle = False
  
  conflicts = 0
  f = open(conflictFile, "r")
  line = f.readline()
  while line:
    if line.startswith("<<<<<<<") and (foundStart == False) and (foundMiddle == False):
      foundStart = True
    if line.startswith("=======") and foundStart and (foundMiddle == False):
      foundMiddle = True
    if line.startswith(">>>>>>>") and foundStart and foundMiddle:
      conflicts = conflicts + 1
      foundStart = False
      foundMiddle = False
    line = f.readline()
  f.close()
  
  return conflicts
 
#----------------------------------------------------------------------------------------------------------------------------
#  renameConflictFile
#----------------------------------------------------------------------------------------------------------------------------
 
def uniqueName(path, uniqueLabel):
  base, extension = os.path.splitext(path)
  digit = 2
  uniqueDigit = ""
  if os.path.exists(base + uniqueLabel + uniqueDigit + extension):
    uniqueDigit = str(digit)
    digit = digit + 1
  
  newPath = base + uniqueLabel + uniqueDigit + extension
  
  return newPath
 
#----------------------------------------------------------------------------------------------------------------------------
#  createConflictFiles
#----------------------------------------------------------------------------------------------------------------------------
 
def createConflictFiles(minePath, theirPath, conflictFile):
  orig = open(conflictFile, "r")
  mine = open(minePath, "w")
  theirs = open(theirPath, "w")
  
  placeInTheirs = True
  placeInMine = True
  
  line = orig.readline()
  while line:
    if line.startswith("<<<<<<<") and placeInTheirs and placeInMine:
      placeInMine = False
    elif line.startswith("=======") and placeInTheirs and (placeInMine == False):
      placeInTheirs = False
      placeInMine = True
    elif line.startswith(">>>>>>>") and (placeInTheirs == False) and placeInMine:
      placeInTheirs = True
    else:
      if placeInMine:
        mine.write(line)
      if placeInTheirs:
        theirs.write(line)
    line = orig.readline()
    
  orig.close()
  mine.close()
  theirs.close()
 
#----------------------------------------------------------------------------------------------------------------------------
#  diffConflict
#----------------------------------------------------------------------------------------------------------------------------
 
def diffConflict(conflictFile, chatty=True):
  conflicts = numConflicts(conflictFile)
  if (conflicts == 0) and chatty:
    basePath, fileName = os.path.split( conflictFile )
    print "\tThe file \"%s\" does not contain any conflicts. SKIPPING." % fileName
  else:
    newConflictPath = uniqueName(conflictFile, gConflictMarker)
    os.rename(conflictFile, newConflictPath)
    
    theirPath = uniqueName(conflictFile, gTheirsMarker)
    
    createConflictFiles(conflictFile, theirPath, newConflictPath)
    command = 'bbdiff "%s" "%s"' % ( conflictFile, theirPath )
    status, output = commands.getstatusoutput( command )
 
#----------------------------------------------------------------------------------------------------------------------------
#  cleanConflictRelatedFiles
#----------------------------------------------------------------------------------------------------------------------------
 
def cleanConflictRelatedFiles(conflictFile, chatty=True):
  filePath, extension = os.path.splitext( conflictFile )
  
  # Remove .CONFLICT files.
  tempfile = filePath + gConflictMarker + extension
  if os.path.exists( tempfile ):
    if chatty:
      _, base = os.path.split( filePath )
      base = base + gConflictMarker + extension
      print "\tDeleting file \"%s\"." % base
    command = "rm " + tempfile
    os.remove( tempfile )
  
  #remove .THEIRS files.
  tempfile = filePath + gTheirsMarker + extension
  if os.path.exists( tempfile ):
    if chatty:
      _, base = os.path.split( filePath )
      base = base + gTheirsMarker + extension
      print "\tDeleting file \"%s\"." % base
    command = "rm " + tempfile
    os.remove( tempfile )
 
#----------------------------------------------------------------------------------------------------------------------------
#  main
#----------------------------------------------------------------------------------------------------------------------------
 
def main(argv=None):
  if argv is None:
    argv = sys.argv
  
  # Set up options for the command line that we support.
  description="A utility to compare files in a git repository using bbedit."
  usage = "usage: %prog [--version] | [-h | --help] | [-q | --quiet] [--clean] [[-c | --conflict] | [-r | --revision <revision(s)>]] file1 [file2 ...]"
  
  parser = OptionParser(version=gdiffVersion, description=description, usage=usage)
  parser.add_option("-r", "--revision", dest="revision", help="Pass a revision or a revision range using the git syntax (ie: 98d4cf..bfced5) for " + gToolName + " to compare. This option is mutually exclusive to the '-c' option.")
  parser.add_option("--clean", action="store_true", dest="cleanUp", default=False, help="Delete all of the temp files that " + gToolName + " has created. If any files are passed as arguments, their --conflict related files (\"" + gConflictMarker + "\", \"" + gTheirsMarker + "\") will be deleted.")
  parser.add_option("-q", "--quiet", action="store_false", dest="chatty", default=True, help="Reduce the chatter of " + gToolName + ".")
  parser.add_option("-c", "--conflict", action="store_true", dest="conflict", default=False, help="The files being diffed have conflict markers that need to be resolved. This option is mutually exclusive to the '-r' option.")
  
  printUsage = True
  (options, args) = parser.parse_args(argv[1:])
  if options.cleanUp:
    cleanTmpDirectory(options.chatty)
    for gitFile in args:
      cleanConflictRelatedFiles(gitFile, options.chatty)
    return 0
  if len(args) > 0:
    printUsage = False
  
  # These options are mutually exclusive.
  if options.revision and options.conflict:
    printUsage = True
  
  # See if we can see any reason that we need to print the usage for the app.
  if printUsage:
    parser.print_help()
    return 2
  else:
    if options.conflict:
      for gitFile in args:
        if options.chatty:
          print "Diffing Conflict \"%s\"." % gitFile
        diffConflict(gitFile, options.chatty)
    else:
      if options.revision:
        rev = options.revision
      else:
        rev = "HEAD"
      for gitFile in args:
        if options.chatty:
          print "Comparing \"%s\"." % gitFile
        performDiff(gitFile, rev, options.chatty)
  return 0
 
if __name__ == "__main__":
  sys.exit(main())