Skip to content

Commit 509e3bc

Browse files
committed
Enhance git commit handling by improving error handling.
1 parent 5c7ec68 commit 509e3bc

1 file changed

Lines changed: 56 additions & 50 deletions

File tree

sfs_processor.py

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -244,69 +244,75 @@ def _create_markdown_document(data: Dict[str, Any], output_path: Path, enable_gi
244244
# Only create main commits if there are no amendments (they handle their own commits)
245245
if not amendments and utfardad_datum:
246246
# Ensure commits are made in a different branch
247-
original_branch = ensure_git_branch_for_commits()
247+
original_branch, commit_branch = ensure_git_branch_for_commits()
248248

249-
try:
250-
# First commit: without ikraft_datum in front matter, dated utfardad_datum
251-
commit_message = rubrik if rubrik else f"SFS {beteckning}"
249+
# Only proceed with git commits if branch creation was successful
250+
if original_branch is not None and commit_branch is not None:
251+
try:
252+
# First commit: without ikraft_datum in front matter, dated utfardad_datum
253+
commit_message = rubrik if rubrik else f"SFS {beteckning}"
252254

253-
# Write file for first commit (without ikraft_datum)
254-
save_to_disk(output_file, markdown_content)
255-
256-
# Stage the current file (which doesn't have ikraft_datum yet)
257-
subprocess.run(['git', 'add', str(output_file)], check=True, capture_output=True)
258-
259-
# Check if there are any changes to commit
260-
result = subprocess.run(['git', 'diff', '--cached', '--quiet'], capture_output=True)
261-
if result.returncode != 0: # Non-zero means there are changes
262-
# Create first commit with utfardad_datum as date
263-
subprocess.run([
264-
'git', 'commit',
265-
'-m', commit_message,
266-
'--date', utfardad_datum
267-
], check=True, capture_output=True)
268-
print(f"Git-commit skapad: '{commit_message}' daterad {utfardad_datum}")
269-
else:
270-
print(f"Inga ändringar att commita för första commit av {beteckning}")
271-
272-
# Second commit: add ikraft_datum to front matter if it exists
273-
if ikraft_datum:
274-
# Add ikraft_datum and sort front matter
275-
markdown_content_with_ikraft = add_ikraft_datum_to_frontmatter(markdown_content, ikraft_datum, beteckning)
276-
277-
# Write updated file with ikraft_datum
278-
save_to_disk(output_file, markdown_content_with_ikraft)
255+
# Write file for first commit (without ikraft_datum)
256+
save_to_disk(output_file, markdown_content)
279257

280-
# Stage the updated file
258+
# Stage the current file (which doesn't have ikraft_datum yet)
281259
subprocess.run(['git', 'add', str(output_file)], check=True, capture_output=True)
282260

283261
# Check if there are any changes to commit
284262
result = subprocess.run(['git', 'diff', '--cached', '--quiet'], capture_output=True)
285263
if result.returncode != 0: # Non-zero means there are changes
286-
# Create second commit with ikraft_datum as date
264+
# Create first commit with utfardad_datum as date
287265
subprocess.run([
288266
'git', 'commit',
289-
'-m', f"{beteckning} träder i kraft", # TODO: Se till att committa förarbeten först
290-
'--date', ikraft_datum
267+
'-m', commit_message,
268+
'--date', utfardad_datum
291269
], check=True, capture_output=True)
292-
print(f"Git-commit skapad: '{beteckning} träder i kraft' daterad {ikraft_datum}")
270+
print(f"Git-commit skapad: '{commit_message}' daterad {utfardad_datum}")
293271
else:
294-
print(f"Inga ändringar att commita för ikraft_datum av {beteckning}")
295-
296-
# Use the content with ikraft_datum as final content
297-
final_content = markdown_content_with_ikraft
298-
299-
except subprocess.CalledProcessError as e:
300-
print(f"Varning: Git-commit misslyckades för {beteckning}: {e}")
301-
# Write the file anyway, without git commits
302-
save_to_disk(output_file, markdown_content)
303-
except FileNotFoundError:
304-
print("Varning: Git hittades inte. Hoppar över Git-commits.")
305-
# Write the file anyway, without git commits
272+
print(f"Inga ändringar att commita för första commit av {beteckning}")
273+
274+
# Second commit: add ikraft_datum to front matter if it exists
275+
if ikraft_datum:
276+
# Add ikraft_datum and sort front matter
277+
markdown_content_with_ikraft = add_ikraft_datum_to_frontmatter(markdown_content, ikraft_datum, beteckning)
278+
279+
# Write updated file with ikraft_datum
280+
save_to_disk(output_file, markdown_content_with_ikraft)
281+
282+
# Stage the updated file
283+
subprocess.run(['git', 'add', str(output_file)], check=True, capture_output=True)
284+
285+
# Check if there are any changes to commit
286+
result = subprocess.run(['git', 'diff', '--cached', '--quiet'], capture_output=True)
287+
if result.returncode != 0: # Non-zero means there are changes
288+
# Create second commit with ikraft_datum as date
289+
subprocess.run([
290+
'git', 'commit',
291+
'-m', f"{beteckning} träder i kraft", # TODO: Se till att committa förarbeten först
292+
'--date', ikraft_datum
293+
], check=True, capture_output=True)
294+
print(f"Git-commit skapad: '{beteckning} träder i kraft' daterad {ikraft_datum}")
295+
else:
296+
print(f"Inga ändringar att commita för ikraft_datum av {beteckning}")
297+
298+
# Use the content with ikraft_datum as final content
299+
final_content = markdown_content_with_ikraft
300+
301+
except subprocess.CalledProcessError as e:
302+
print(f"Varning: Git-commit misslyckades för {beteckning}: {e}")
303+
# Write the file anyway, without git commits
304+
save_to_disk(output_file, markdown_content)
305+
except FileNotFoundError:
306+
print("Varning: Git hittades inte. Hoppar över Git-commits.")
307+
# Write the file anyway, without git commits
308+
save_to_disk(output_file, markdown_content)
309+
finally:
310+
# Always restore original branch
311+
restore_original_branch(original_branch)
312+
else:
313+
# Branch creation failed, write file without git commits
314+
print(f"Hoppar över Git-commits för {beteckning} på grund av branch-problem")
306315
save_to_disk(output_file, markdown_content)
307-
finally:
308-
# Always restore original branch
309-
restore_original_branch(original_branch)
310316
else:
311317
# Write file if git is enabled but no commits needed
312318
save_to_disk(output_file, markdown_content)

0 commit comments

Comments
 (0)