@@ -130,10 +130,7 @@ def _process_packages_v2(
130
130
integrity = integrity , resolved = info ['resolved' ]
131
131
)
132
132
elif resolved_url .scheme .startswith ('git+' ):
133
- raise NotImplementedError (
134
- 'Git sources in lockfile v2 format are not supported yet'
135
- f' (package { install_path } in { lockfile } )'
136
- )
133
+ source = self .parse_git_source (info ['resolved' ])
137
134
else :
138
135
raise NotImplementedError (
139
136
f"Don't know how to handle package { install_path } in { lockfile } "
@@ -425,72 +422,79 @@ def _finalize(self) -> None:
425
422
)
426
423
427
424
if self .git_sources :
428
- # Generate jq scripts to patch the package*.json files.
429
- scripts = {
430
- 'package.json' : r"""
431
- walk(
432
- if type == "object"
433
- then
434
- to_entries | map(
435
- if (.value | type == "string") and $data[.value]
436
- then .value = "git+file:\($buildroot)/\($data[.value])"
437
- else .
438
- end
439
- ) | from_entries
440
- else .
441
- end
442
- )
443
- """ ,
444
- 'package-lock.json' : r"""
445
- walk(
446
- if type == "object" and (.version | type == "string") and $data[.version]
447
- then
448
- .version = "git+file:\($buildroot)/\($data[.version])"
449
- else .
450
- end
451
- )
452
- """ ,
453
- }
425
+ # Generate jq script to patch the package.json file
426
+ script = r"""
427
+ walk(
428
+ if type == "object"
429
+ then
430
+ to_entries | map(
431
+ if (.value | type == "string") and $data[.value]
432
+ then .value = "git+file:\($buildroot)/\($data[.value])"
433
+ else .
434
+ end
435
+ ) | from_entries
436
+ else .
437
+ end
438
+ )
439
+ """
454
440
455
441
for lockfile , sources in self .git_sources .items ():
456
442
prefix = self .relative_lockfile_dir (lockfile )
457
- data : Dict [str , Dict [str , str ]] = {
458
- 'package.json' : {},
459
- 'package-lock.json' : {},
460
- }
443
+ data : Dict [str , str ] = {}
461
444
462
445
for path , source in sources .items ():
463
446
GIT_URL_PREFIX = 'git+'
464
447
465
448
new_version = f'{ path } #{ source .commit } '
466
- assert source .from_ is not None
467
- data ['package.json' ][source .from_ ] = new_version
468
- data ['package-lock.json' ][source .original ] = new_version
469
-
470
- if source .from_ .startswith (GIT_URL_PREFIX ):
471
- data ['package.json' ][
472
- source .from_ [len (GIT_URL_PREFIX ) :]
473
- ] = new_version
474
-
475
- if source .original .startswith (GIT_URL_PREFIX ):
476
- data ['package-lock.json' ][
477
- source .original [len (GIT_URL_PREFIX ) :]
478
- ] = new_version
479
-
480
- for filename , script in scripts .items ():
481
- target = Path ('$FLATPAK_BUILDER_BUILDDIR' ) / prefix / filename
482
- script = (
483
- textwrap .dedent (script .lstrip ('\n ' )).strip ().replace ('\n ' , '' )
484
- )
485
- json_data = json .dumps (data [filename ])
486
- patch_commands [lockfile ].append (
487
- 'jq'
488
- ' --arg buildroot "$FLATPAK_BUILDER_BUILDDIR"'
489
- f' --argjson data { shlex .quote (json_data )} '
490
- f' { shlex .quote (script )} { target } '
491
- f' > { target } .new'
492
- )
493
- patch_commands [lockfile ].append (f'mv { target } {{.new,}}' )
449
+ targets = []
450
+ source_url = urllib .parse .urlparse (source .from_ or source .original )
451
+
452
+ # https://github.com/npm/hosted-git-info
453
+ hosted_git = [
454
+ ('@github.com' , 'github' ),
455
+ ('@bitbucket.org' , 'bitbucket' ),
456
+ ('@gitlab.com' , 'gitlab' ),
457
+ ('@gist.github.com' , 'gist' ),
458
+ ('@git.sr.ht' , 'sourcehut' ),
459
+ ]
460
+ for domain , shortcut in hosted_git :
461
+ if domain in source_url .netloc .lower ():
462
+ targets .append (
463
+ f"{ shortcut } :{ source_url .path [1 :].replace ('.git' , '' )} "
464
+ f'#{ source_url .fragment } '
465
+ )
466
+ break
467
+
468
+ if (
469
+ source_url .scheme .startswith (GIT_URL_PREFIX + 'ssh' )
470
+ and ':' not in source_url .netloc
471
+ ):
472
+ path_match = re .compile (r'^/([^/]+)(.*)' ).match (source_url .path )
473
+ if path_match :
474
+ parent , child = path_match .groups ()
475
+ source_url = source_url ._replace (
476
+ netloc = f'{ source_url .netloc } :{ parent } ' , path = child
477
+ )
478
+ elif source_url .scheme .startswith (GIT_URL_PREFIX ):
479
+ targets .append (source_url .geturl ()[len (GIT_URL_PREFIX ) :])
480
+
481
+ targets .append (source_url .geturl ())
482
+ for t in targets :
483
+ data [t ] = new_version
484
+ data [t .replace ('#' + source .commit , '' )] = new_version
485
+
486
+ filename = 'package.json'
487
+ target = Path ('$FLATPAK_BUILDER_BUILDDIR' ) / prefix / filename
488
+ script = textwrap .dedent (script .lstrip ('\n ' )).strip ().replace ('\n ' , '' )
489
+ json_data = json .dumps (data )
490
+ patch_commands [lockfile ].append (
491
+ 'jq'
492
+ ' --arg buildroot "$FLATPAK_BUILDER_BUILDDIR"'
493
+ f' --argjson data { shlex .quote (json_data )} '
494
+ f' { shlex .quote (script )} { target } '
495
+ f' > { target } .new'
496
+ )
497
+ patch_commands [lockfile ].append (f'mv { target } {{.new,}}' )
494
498
495
499
patch_all_commands : List [str ] = []
496
500
for lockfile in self .all_lockfiles :
0 commit comments