Skip to content

Commit 88bfa55

Browse files
committed
Add a --keep-untracked option to the install command.
1 parent 54701a1 commit 88bfa55

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

poetry/console/commands/install.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class InstallCommand(EnvCommand):
1919
"Output the operations but do not execute anything "
2020
"(implicitly enables --verbose).",
2121
),
22+
option(
23+
"keep-untracked",
24+
None,
25+
"Does not remove packages not present in the lock file.",
26+
),
2227
option(
2328
"extras",
2429
"E",
@@ -58,6 +63,7 @@ def handle(self):
5863
installer.extras(extras)
5964
installer.dev_mode(not self.option("no-dev"))
6065
installer.dry_run(self.option("dry-run"))
66+
installer.keep_untracked(self.option("keep-untracked"))
6167
installer.verbose(self.option("verbose"))
6268

6369
return_code = installer.run()

poetry/installation/installer.py

+20
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(
3939
self._pool = pool
4040

4141
self._dry_run = False
42+
self._keep_untracked = False
4243
self._update = False
4344
self._verbose = False
4445
self._write_lock = True
@@ -83,6 +84,14 @@ def dry_run(self, dry_run=True): # type: (bool) -> Installer
8384
def is_dry_run(self): # type: () -> bool
8485
return self._dry_run
8586

87+
def keep_untracked(self, keep_untracked=True): # type: (bool) -> Installer
88+
self._keep_untracked = keep_untracked
89+
90+
return self
91+
92+
def is_keep_untracked(self): # type: () -> bool
93+
return self._keep_untracked
94+
8695
def verbose(self, verbose=True): # type: (bool) -> Installer
8796
self._verbose = verbose
8897

@@ -424,6 +433,17 @@ def _get_operations_from_lock(
424433

425434
ops.append(op)
426435

436+
if not self._keep_untracked:
437+
for installed in installed_repo.packages:
438+
is_in_lock_file = False
439+
for locked in locked_repository.packages:
440+
if locked.name == installed.name:
441+
is_in_lock_file = True
442+
break
443+
444+
if not is_in_lock_file:
445+
ops.append(Uninstall(installed))
446+
427447
return ops
428448

429449
def _filter_operations(

0 commit comments

Comments
 (0)