comparison fetch_command.py @ 103:f9d0154e4d11

fetch_command: simplify diff code with opener objects
author Patrick Mezard <pmezard@gmail.com>
date Tue, 25 Nov 2008 09:18:26 -0600
parents 3d56b8c53979
children f046564b2916
comparison
equal deleted inserted replaced
102:3d56b8c53979 103:f9d0154e4d11
338 our_tempdir = None 338 our_tempdir = None
339 diff_path = make_diff_path(b) 339 diff_path = make_diff_path(b)
340 parent_rev, br_p = hg_editor.get_parent_svn_branch_and_rev(r.revnum, b) 340 parent_rev, br_p = hg_editor.get_parent_svn_branch_and_rev(r.revnum, b)
341 parent_ha = hg_editor.get_parent_revision(r.revnum, b) 341 parent_ha = hg_editor.get_parent_revision(r.revnum, b)
342 files_touched = set() 342 files_touched = set()
343 link_files = {}
344 exec_files = {}
345 try: 343 try:
346 if br_p == b: 344 if br_p == b:
347 # letting patch handle binaries sounded 345 # letting patch handle binaries sounded
348 # cool, but it breaks patch in sad ways 346 # cool, but it breaks patch in sad ways
349 d = svn.get_unified_diff(diff_path, r.revnum, deleted=False, 347 d = svn.get_unified_diff(diff_path, r.revnum, deleted=False,
356 if d: 354 if d:
357 ui.status('Branch creation with mods, pulling full rev.\n') 355 ui.status('Branch creation with mods, pulling full rev.\n')
358 raise BadPatchApply() 356 raise BadPatchApply()
359 357
360 our_tempdir = tempfile.mkdtemp('svn_fetch_temp', dir=temp_location) 358 our_tempdir = tempfile.mkdtemp('svn_fetch_temp', dir=temp_location)
359 opener = merc_util.opener(our_tempdir)
361 for m in binary_file_re.findall(d): 360 for m in binary_file_re.findall(d):
362 # we have to pull each binary file by hand as a fulltext, 361 # we have to pull each binary file by hand as a fulltext,
363 # which sucks but we've got no choice 362 # which sucks but we've got no choice
364 file_path = os.path.join(our_tempdir, m)
365 files_touched.add(m) 363 files_touched.add(m)
366 try: 364 try:
367 try: 365 f = opener(m, 'w')
368 os.makedirs(os.path.dirname(file_path))
369 except OSError, e:
370 pass
371 f = open(file_path, 'w')
372 f.write(svn.get_file(diff_path+'/'+m, r.revnum)[0]) 366 f.write(svn.get_file(diff_path+'/'+m, r.revnum)[0])
373 f.close() 367 f.close()
374 except IOError: 368 except IOError:
375 pass 369 pass
376 d2 = empty_file_patch_wont_make_re.sub('', d) 370 d2 = empty_file_patch_wont_make_re.sub('', d)
377 d2 = property_exec_set_re.sub('', d2) 371 d2 = property_exec_set_re.sub('', d2)
378 d2 = property_exec_removed_re.sub('', d2) 372 d2 = property_exec_removed_re.sub('', d2)
379 old_cwd = os.getcwd()
380 os.chdir(our_tempdir)
381 for f in any_file_re.findall(d): 373 for f in any_file_re.findall(d):
374 if f in files_touched:
375 # this check is here because modified binary files will get
376 # created before here.
377 continue
382 files_touched.add(f) 378 files_touched.add(f)
383 # this check is here because modified binary files will get 379 data = ''
384 # created before here. 380 if f in hg_editor.repo[parent_ha]:
385 if os.path.exists(f): 381 data = hg_editor.repo[parent_ha][f].data()
386 continue 382 fp = opener(f, 'w')
387 dn = os.path.dirname(f) 383 fp.write(data)
388 if dn and not os.path.exists(dn): 384 fp.close()
389 os.makedirs(dn)
390 if f in hg_editor.repo[parent_ha].manifest():
391 data = hg_editor.repo[parent_ha].filectx(f).data()
392 fi = open(f, 'w')
393 fi.write(data)
394 fi.close()
395 else:
396 open(f, 'w').close()
397 os.chdir(old_cwd)
398 if d2.strip() and len(re.findall('\n[-+]', d2.strip())) > 0: 385 if d2.strip() and len(re.findall('\n[-+]', d2.strip())) > 0:
399 old_cwd = os.getcwd() 386 old_cwd = os.getcwd()
400 os.chdir(our_tempdir) 387 os.chdir(our_tempdir)
401 changed = {} 388 changed = {}
402 try: 389 try:
440 r.revnum) 427 r.revnum)
441 428
442 # we create the files if they don't exist here because we know 429 # we create the files if they don't exist here because we know
443 # that we'll never have diff info for a deleted file, so if the 430 # that we'll never have diff info for a deleted file, so if the
444 # property is set, we should force the file to exist no matter what. 431 # property is set, we should force the file to exist no matter what.
432 exec_files = {}
445 for m in property_exec_removed_re.findall(d): 433 for m in property_exec_removed_re.findall(d):
434 exec_files[m] = False
435 for m in property_exec_set_re.findall(d):
436 exec_files[m] = True
437 for m in exec_files:
438 files_touched.add(m)
446 f = os.path.join(our_tempdir, m) 439 f = os.path.join(our_tempdir, m)
447 if not os.path.exists(f): 440 if not os.path.exists(f):
448 d = os.path.dirname(f) 441 data = ''
449 if not os.path.exists(d): 442 if m in hg_editor.repo[parent_ha]:
450 os.makedirs(d) 443 data = hg_editor.repo[parent_ha][m].data()
451 if not m in hg_editor.repo[parent_ha].manifest(): 444 fp = opener(m, 'w')
452 open(f, 'w').close() 445 fp.write(data)
453 else: 446 fp.close()
454 data = hg_editor.repo[parent_ha].filectx(m).data() 447 link_files = {}
455 fp = open(f, 'w')
456 fp.write(data)
457 fp.close()
458 exec_files[m] = False
459 files_touched.add(m)
460 for m in property_exec_set_re.findall(d):
461 f = os.path.join(our_tempdir, m)
462 if not os.path.exists(f):
463 d = os.path.dirname(f)
464 if not os.path.exists(d):
465 os.makedirs(d)
466 if m not in hg_editor.repo[parent_ha].manifest():
467 open(f, 'w').close()
468 else:
469 data = hg_editor.repo[parent_ha].filectx(m).data()
470 fp = open(f, 'w')
471 fp.write(data)
472 fp.close()
473 exec_files[m] = True
474 files_touched.add(m)
475 for m in property_special_set_re.findall(d): 448 for m in property_special_set_re.findall(d):
476 # TODO(augie) when a symlink is removed, patching will fail. 449 # TODO(augie) when a symlink is removed, patching will fail.
477 # We're seeing that above - there's gotta be a better 450 # We're seeing that above - there's gotta be a better
478 # workaround than just bailing like that. 451 # workaround than just bailing like that.
479 path = os.path.join(our_tempdir, m) 452 path = os.path.join(our_tempdir, m)