11#!/usr/bin/env python3
22
33# Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
4+ # Copyright (c) 2025 Siemens
45# License: MIT <http://www.opensource.org/licenses/mit-license.php>
56
67from hg2git import setup_repo ,fixup_user ,get_branch ,get_changeset
1112import os
1213from binascii import hexlify
1314import pluginloader
15+ from hgext .largefiles import lfutil
1416
1517# silly regex to catch Signed-off-by lines in log message
1618sob_re = re .compile (b'^Signed-[Oo]ff-[Bb]y: (.+)$' )
@@ -162,6 +164,32 @@ def refresh_gitmodules(ctx):
162164 wr (b'M 100644 inline .gitmodules' )
163165 wr_data (gitmodules )
164166
167+ def is_largefile (filename ):
168+ return filename [:6 ] == b'.hglf/'
169+
170+ def largefile_orig_name (filename ):
171+ return filename [6 :]
172+
173+ def largefile_data (ctx , file , filename ):
174+ lf_file_ctx = ctx .filectx (file )
175+ lf_hash = lf_file_ctx .data ().strip (b'\n ' )
176+ sys .stderr .write ("Detected large file hash %s\n " % lf_hash .decode ())
177+ #should detect where the large files are located
178+ file_with_data = lfutil .findfile (ctx .repo (), lf_hash )
179+ if file_with_data is None :
180+ # Autodownloading from the mercurial repository would be an issue as there
181+ # is a good chance that we may need to input some username and password.
182+ # This will surely break fast-export as there will be some unexpected
183+ # output.
184+ sys .stderr .write ("Large file wasn't found in local cache.\n " )
185+ sys .stderr .write ("Please clone with --all-largefiles\n " )
186+ sys .stderr .write ("or pull all large files with 'hg lfpull --rev "
187+ "\" all()\" '\n " )
188+ # closing in the middle of import will revert everything to the last checkpoint
189+ sys .exit (3 )
190+ with open (os .path .normpath (file_with_data ), 'rb' ) as file_with_data_handle :
191+ return file_with_data_handle .read ()
192+
165193def export_file_contents (ctx ,manifest ,files ,hgtags ,encoding = '' ,plugins = {}):
166194 count = 0
167195 max = len (files )
@@ -183,8 +211,12 @@ def export_file_contents(ctx,manifest,files,hgtags,encoding='',plugins={}):
183211 b'Ignoring file %s which cannot be tracked by git\n ' % filename
184212 )
185213 continue
186- file_ctx = ctx .filectx (file )
187- d = file_ctx .data ()
214+ if is_largefile (filename ):
215+ filename = largefile_orig_name (filename )
216+ d = largefile_data (ctx , file , filename )
217+ else :
218+ file_ctx = ctx .filectx (file )
219+ d = file_ctx .data ()
188220
189221 if plugins and plugins ['file_data_filters' ]:
190222 file_data = {'filename' :filename ,'file_ctx' :file_ctx ,'data' :d }
@@ -327,6 +359,8 @@ def get_branchname(name):
327359 filename = strip_leading_slash (filename )
328360 if filename == b'.hgsub' :
329361 remove_gitmodules (ctx )
362+ if is_largefile (filename ):
363+ filename = largefile_orig_name (filename )
330364 wr (b'D %s' % filename )
331365
332366 export_file_contents (ctx ,man ,modified ,hgtags ,fn_encoding ,plugins )
0 commit comments