1313from django .db .migrations .loader import MigrationLoader
1414from git import InvalidGitRepositoryError , Repo
1515
16- from migration_fixer .utils import (
17- fix_named_migration ,
18- fix_numbered_migration ,
19- no_translations ,
20- )
16+ from migration_fixer .utils import fix_numbered_migration , no_translations
2117
2218
2319class Command (BaseCommand ):
@@ -69,8 +65,8 @@ def handle(self, *app_labels, **options):
6965 self .stdout .write ("Verifying git repository..." )
7066
7167 try :
72- self .repo .git_dir
73- except InvalidGitRepositoryError :
68+ self .repo .git_dir and self . repo . head . commit
69+ except ( ValueError , InvalidGitRepositoryError ) :
7470 is_git_repo = False
7571 else :
7672 is_git_repo = True
@@ -87,15 +83,27 @@ def handle(self, *app_labels, **options):
8783 if self .verbosity >= 2 :
8884 self .stdout .write ("Retrieving the current branch..." )
8985
90- current_branch = self .repo .head .name
86+ current_branch = self .repo .head .ref .name
87+
88+ if self .repo .is_dirty (): # pragma: no cover
89+ raise CommandError (
90+ self .style .ERROR (
91+ "Git repository has uncommitted changes. "
92+ "Please commit any outstanding changes."
93+ )
94+ )
9195
9296 if self .verbosity >= 2 :
9397 self .stdout .write (
9498 f"Fetching git remote origin changes on: { self .default_branch } "
9599 )
96100
97- if current_branch == self .default_branch :
98- self .repo .remotes [self .default_branch ].origin .pull ()
101+ if current_branch == self .default_branch : # pragma: no cover
102+ for remote in self .repo .remotes :
103+ remote .pull (
104+ self .default_branch ,
105+ force = self .force_update ,
106+ )
99107 else :
100108 for remote in self .repo .remotes :
101109 remote .fetch (
@@ -184,16 +192,16 @@ def handle(self, *app_labels, **options):
184192 )
185193
186194 last_remote = [
187- fname
188- for fname in conflict
189- if fname not in local_filenames
195+ name for name in conflict if name not in local_filenames
190196 ]
191197
192- if not last_remote :
198+ if not last_remote : # pragma: no cover
193199 raise CommandError (
194200 self .style .ERROR (
195201 f"Unable to determine the last migration on: "
196- f"{ self .default_branch } " ,
202+ f"{ self .default_branch } . "
203+ "Please verify the target branch using"
204+ '"-b [target branch]".' ,
197205 )
198206 )
199207
@@ -221,16 +229,16 @@ def handle(self, *app_labels, **options):
221229 seed = int (seed_split [0 ]),
222230 start_name = last_remote_filename ,
223231 changed_files = changed_files ,
232+ writer = (
233+ lambda message : self .stdout .write (message )
234+ if self .verbosity >= 2
235+ else lambda x : x
236+ ),
224237 )
225- else :
226- if self .verbosity >= 2 :
227- self .stdout .write ("Fixing named migration..." )
228-
229- fix_named_migration (
230- app_label = app_label ,
231- migration_path = migration_path ,
232- start_name = last_remote_filename ,
233- changed_files = changed_files ,
238+ else : # pragma: no cover
239+ raise ValueError (
240+ f"Unable to fix migration: { last_remote_filename } . \n "
241+ f"NOTE: It needs to begin with a number. eg. 0001_*" ,
234242 )
235243 except (ValueError , IndexError , TypeError ) as e :
236244 self .stderr .write (f"Error: { e } " )
0 commit comments