up path analyze

# Conflicts:
#	.gitignore
This commit is contained in:
Tykayn 2025-10-30 09:59:07 +01:00 committed by tykayn
parent e530904873
commit f41f09d98e
2 changed files with 106 additions and 115 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
build/* build/*
venv venv
__pycache__ __pycache__
*.json

View file

@ -12,6 +12,7 @@ from collections import defaultdict
import os import os
import sys import sys
from typing import Dict, List, Tuple, Any from typing import Dict, List, Tuple, Any
import argparse
def run_git_command(args: List[str]) -> str: def run_git_command(args: List[str]) -> str:
"""Exécute une commande git et retourne la sortie""" """Exécute une commande git et retourne la sortie"""
@ -148,11 +149,19 @@ def format_line_for_csv(line_content: str, max_length: int = 100) -> str:
return line_content[:max_length] + '...' return line_content[:max_length] + '...'
return line_content return line_content
def analyze_git_history(): def analyze_git_history(folder_path: str):
"""Fonction principale pour analyser l'historique Git""" """Fonction principale pour analyser l'historique Git dans un dossier donné"""
files_to_track = ['livre.org', 'intrigues.org', 'personnages.org'] files_to_track = ['livre.org', 'intrigues.org', 'personnages.org']
if not os.path.isdir(folder_path):
print(f"Erreur: le dossier {folder_path} n'existe pas.", file=sys.stderr)
sys.exit(1)
# Sauver le cwd courant et se placer dans le bon dossier pour les commandes git
original_cwd = os.getcwd()
os.chdir(folder_path)
try:
# Vérifier que nous sommes dans un dépôt Git # Vérifier que nous sommes dans un dépôt Git
try: try:
run_git_command(['rev-parse', '--git-dir']) run_git_command(['rev-parse', '--git-dir'])
@ -162,34 +171,24 @@ def analyze_git_history():
all_changes = [] all_changes = []
# Pour chaque fichier
for filename in files_to_track: for filename in files_to_track:
if not os.path.exists(filename): if not os.path.exists(filename):
print(f"Attention: Le fichier {filename} n'existe pas, il sera ignoré", file=sys.stderr) print(f"Attention: Le fichier {filename} n'existe pas dans {folder_path}, il sera ignoré", file=sys.stderr)
continue continue
print(f"Analyse de {filename}...") print(f"Analyse de {filename}...")
# Récupérer l'historique
history = get_file_history(filename) history = get_file_history(filename)
if not history: if not history:
print(f"Aucun historique trouvé pour {filename}") print(f"Aucun historique trouvé pour {filename}")
continue continue
# Analyser chaque commit
for i, commit in enumerate(history): for i, commit in enumerate(history):
commit_hash = commit['hash'] commit_hash = commit['hash']
commit_date = commit['date'] commit_date = commit['date']
commit_author = commit['author'] commit_author = commit['author']
commit_message = commit['message'] commit_message = commit['message']
# Récupérer les changements par rapport au commit précédent
if i < len(history) - 1: if i < len(history) - 1:
prev_hash = history[i + 1]['hash'] prev_hash = history[i + 1]['hash']
changes = get_changes_between_commits(commit_hash, prev_hash, filename) changes = get_changes_between_commits(commit_hash, prev_hash, filename)
else: else:
# Premier commit, récupérer le contenu initial
content = get_file_content_at_commit(commit_hash, filename) content = get_file_content_at_commit(commit_hash, filename)
changes = { changes = {
'added_lines': [{'line': line, 'context': 'initial'} for line in content.split('\n') if line.strip()], 'added_lines': [{'line': line, 'context': 'initial'} for line in content.split('\n') if line.strip()],
@ -201,7 +200,6 @@ def analyze_git_history():
'net_change': len(content.split('\n')) 'net_change': len(content.split('\n'))
} }
} }
if changes and changes['stats']['added_count'] + changes['stats']['removed_count'] > 0: if changes and changes['stats']['added_count'] + changes['stats']['removed_count'] > 0:
change_entry = { change_entry = {
'filename': filename, 'filename': filename,
@ -212,46 +210,34 @@ def analyze_git_history():
'changes': changes 'changes': changes
} }
all_changes.append(change_entry) all_changes.append(change_entry)
# Trier par date
all_changes.sort(key=lambda x: x['date']) all_changes.sort(key=lambda x: x['date'])
# Générer le JSON # Générer le JSON
print("Génération du fichier JSON...") print("Génération du fichier JSON...")
with open('git_changes_history.json', 'w', encoding='utf-8') as f: with open('git_changes_history.json', 'w', encoding='utf-8') as f:
json.dump(all_changes, f, ensure_ascii=False, indent=2) json.dump(all_changes, f, ensure_ascii=False, indent=2)
# Générer le CSV # Générer le CSV
print("Génération du fichier CSV...") print("Génération du fichier CSV...")
with open('git_changes_history.csv', 'w', newline='', encoding='utf-8') as csvfile: with open('git_changes_history.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile) writer = csv.writer(csvfile)
# En-têtes
writer.writerow([ writer.writerow([
'Date', 'Fichier', 'Commit Hash', 'Auteur', 'Message', 'Date', 'Fichier', 'Commit Hash', 'Auteur', 'Message',
'Lignes Ajoutées', 'Lignes Supprimées', 'Changement Net', 'Lignes Ajoutées', 'Lignes Supprimées', 'Changement Net',
'Détails Ajouts', 'Détails Suppressions' 'Détails Ajouts', 'Détails Suppressions'
]) ])
# Données
for change in all_changes: for change in all_changes:
# Préparer les détails des ajouts et suppressions
added_details = "; ".join([ added_details = "; ".join([
format_line_for_csv(item['line'], 50) format_line_for_csv(item['line'], 50)
for item in change['changes']['added_lines'][:10] # Limiter à 10 exemples for item in change['changes']['added_lines'][:10]
]) ])
if len(change['changes']['added_lines']) > 10: if len(change['changes']['added_lines']) > 10:
added_details += f"... (+{len(change['changes']['added_lines']) - 10} autres)" added_details += f"... (+{len(change['changes']['added_lines']) - 10} autres)"
removed_details = "; ".join([ removed_details = "; ".join([
format_line_for_csv(item['line'], 50) format_line_for_csv(item['line'], 50)
for item in change['changes']['removed_lines'][:10] # Limiter à 10 exemples for item in change['changes']['removed_lines'][:10]
]) ])
if len(change['changes']['removed_lines']) > 10: if len(change['changes']['removed_lines']) > 10:
removed_details += f"... (+{len(change['changes']['removed_lines']) - 10} autres)" removed_details += f"... (+{len(change['changes']['removed_lines']) - 10} autres)"
stats = change['changes']['stats'] stats = change['changes']['stats']
writer.writerow([ writer.writerow([
change['date'], change['date'],
change['filename'], change['filename'],
@ -264,9 +250,13 @@ def analyze_git_history():
added_details, added_details,
removed_details removed_details
]) ])
print(f"Analyse terminée: {len(all_changes)} changements enregistrés") print(f"Analyse terminée: {len(all_changes)} changements enregistrés")
print("Fichiers générés: git_changes_history.json et git_changes_history.csv") print("Fichiers générés: git_changes_history.json et git_changes_history.csv")
finally:
os.chdir(original_cwd)
if __name__ == '__main__': if __name__ == '__main__':
analyze_git_history() parser = argparse.ArgumentParser(description="Analyse historique git pour livre.org, intrigues.org et personnages.org")
parser.add_argument('folder', nargs='?', default='.', help="Chemin du dossier contenant les fichiers org (défaut: dossier courant)")
args = parser.parse_args()
analyze_git_history(args.folder)