From f41f09d98ecbec928d12fcd893aaf823eda7a4c0 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Thu, 30 Oct 2025 09:59:07 +0100 Subject: [PATCH] up path analyze # Conflicts: # .gitignore --- .gitignore | 3 +- analyze_git_history.py | 218 ++++++++++++++++++++--------------------- 2 files changed, 106 insertions(+), 115 deletions(-) diff --git a/.gitignore b/.gitignore index b9fe307a..6ed2a60d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea build/* venv -__pycache__ \ No newline at end of file +__pycache__ +*.json \ No newline at end of file diff --git a/analyze_git_history.py b/analyze_git_history.py index ea325cce..8f9e3966 100755 --- a/analyze_git_history.py +++ b/analyze_git_history.py @@ -12,6 +12,7 @@ from collections import defaultdict import os import sys from typing import Dict, List, Tuple, Any +import argparse def run_git_command(args: List[str]) -> str: """Exécute une commande git et retourne la sortie""" @@ -148,125 +149,114 @@ def format_line_for_csv(line_content: str, max_length: int = 100) -> str: return line_content[:max_length] + '...' return line_content -def analyze_git_history(): - """Fonction principale pour analyser l'historique Git""" +def analyze_git_history(folder_path: str): + """Fonction principale pour analyser l'historique Git dans un dossier donné""" files_to_track = ['livre.org', 'intrigues.org', 'personnages.org'] - - # Vérifier que nous sommes dans un dépôt Git - try: - run_git_command(['rev-parse', '--git-dir']) - except subprocess.CalledProcessError: - print("Erreur: Ce répertoire n'est pas un dépôt Git", file=sys.stderr) + + if not os.path.isdir(folder_path): + print(f"Erreur: le dossier {folder_path} n'existe pas.", file=sys.stderr) sys.exit(1) - - all_changes = [] - - # Pour chaque fichier - for filename in files_to_track: - if not os.path.exists(filename): - print(f"Attention: Le fichier {filename} n'existe pas, il sera ignoré", file=sys.stderr) - continue - - print(f"Analyse de {filename}...") - - # Récupérer l'historique - history = get_file_history(filename) - - if not history: - print(f"Aucun historique trouvé pour {filename}") - continue - - # Analyser chaque commit - for i, commit in enumerate(history): - commit_hash = commit['hash'] - commit_date = commit['date'] - commit_author = commit['author'] - commit_message = commit['message'] - - # Récupérer les changements par rapport au commit précédent - if i < len(history) - 1: - prev_hash = history[i + 1]['hash'] - changes = get_changes_between_commits(commit_hash, prev_hash, filename) - else: - # Premier commit, récupérer le contenu initial - content = get_file_content_at_commit(commit_hash, filename) - changes = { - 'added_lines': [{'line': line, 'context': 'initial'} for line in content.split('\n') if line.strip()], - 'removed_lines': [], - 'modified_sections': [], - 'stats': { - 'added_count': len(content.split('\n')), - 'removed_count': 0, - 'net_change': len(content.split('\n')) + + # 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 + try: + run_git_command(['rev-parse', '--git-dir']) + except subprocess.CalledProcessError: + print("Erreur: Ce répertoire n'est pas un dépôt Git", file=sys.stderr) + sys.exit(1) + + all_changes = [] + + for filename in files_to_track: + if not os.path.exists(filename): + print(f"Attention: Le fichier {filename} n'existe pas dans {folder_path}, il sera ignoré", file=sys.stderr) + continue + print(f"Analyse de {filename}...") + history = get_file_history(filename) + if not history: + print(f"Aucun historique trouvé pour {filename}") + continue + for i, commit in enumerate(history): + commit_hash = commit['hash'] + commit_date = commit['date'] + commit_author = commit['author'] + commit_message = commit['message'] + if i < len(history) - 1: + prev_hash = history[i + 1]['hash'] + changes = get_changes_between_commits(commit_hash, prev_hash, filename) + else: + content = get_file_content_at_commit(commit_hash, filename) + changes = { + 'added_lines': [{'line': line, 'context': 'initial'} for line in content.split('\n') if line.strip()], + 'removed_lines': [], + 'modified_sections': [], + 'stats': { + 'added_count': len(content.split('\n')), + 'removed_count': 0, + 'net_change': len(content.split('\n')) + } } - } - - if changes and changes['stats']['added_count'] + changes['stats']['removed_count'] > 0: - change_entry = { - 'filename': filename, - 'date': commit_date, - 'commit_hash': commit_hash, - 'author': commit_author, - 'message': commit_message, - 'changes': changes - } - all_changes.append(change_entry) - - # Trier par date - all_changes.sort(key=lambda x: x['date']) - - # Générer le JSON - print("Génération du fichier JSON...") - with open('git_changes_history.json', 'w', encoding='utf-8') as f: - json.dump(all_changes, f, ensure_ascii=False, indent=2) - - # Générer le CSV - print("Génération du fichier CSV...") - with open('git_changes_history.csv', 'w', newline='', encoding='utf-8') as csvfile: - writer = csv.writer(csvfile) - - # En-têtes - writer.writerow([ - 'Date', 'Fichier', 'Commit Hash', 'Auteur', 'Message', - 'Lignes Ajoutées', 'Lignes Supprimées', 'Changement Net', - 'Détails Ajouts', 'Détails Suppressions' - ]) - - # Données - for change in all_changes: - # Préparer les détails des ajouts et suppressions - added_details = "; ".join([ - format_line_for_csv(item['line'], 50) - for item in change['changes']['added_lines'][:10] # Limiter à 10 exemples - ]) - if len(change['changes']['added_lines']) > 10: - added_details += f"... (+{len(change['changes']['added_lines']) - 10} autres)" - - removed_details = "; ".join([ - format_line_for_csv(item['line'], 50) - for item in change['changes']['removed_lines'][:10] # Limiter à 10 exemples - ]) - if len(change['changes']['removed_lines']) > 10: - removed_details += f"... (+{len(change['changes']['removed_lines']) - 10} autres)" - - stats = change['changes']['stats'] - + if changes and changes['stats']['added_count'] + changes['stats']['removed_count'] > 0: + change_entry = { + 'filename': filename, + 'date': commit_date, + 'commit_hash': commit_hash, + 'author': commit_author, + 'message': commit_message, + 'changes': changes + } + all_changes.append(change_entry) + all_changes.sort(key=lambda x: x['date']) + # Générer le JSON + print("Génération du fichier JSON...") + with open('git_changes_history.json', 'w', encoding='utf-8') as f: + json.dump(all_changes, f, ensure_ascii=False, indent=2) + # Générer le CSV + print("Génération du fichier CSV...") + with open('git_changes_history.csv', 'w', newline='', encoding='utf-8') as csvfile: + writer = csv.writer(csvfile) writer.writerow([ - change['date'], - change['filename'], - change['commit_hash'], - change['author'], - change['message'], - stats['added_count'], - stats['removed_count'], - stats['net_change'], - added_details, - removed_details + 'Date', 'Fichier', 'Commit Hash', 'Auteur', 'Message', + 'Lignes Ajoutées', 'Lignes Supprimées', 'Changement Net', + 'Détails Ajouts', 'Détails Suppressions' ]) - - 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") + for change in all_changes: + added_details = "; ".join([ + format_line_for_csv(item['line'], 50) + for item in change['changes']['added_lines'][:10] + ]) + if len(change['changes']['added_lines']) > 10: + added_details += f"... (+{len(change['changes']['added_lines']) - 10} autres)" + removed_details = "; ".join([ + format_line_for_csv(item['line'], 50) + for item in change['changes']['removed_lines'][:10] + ]) + if len(change['changes']['removed_lines']) > 10: + removed_details += f"... (+{len(change['changes']['removed_lines']) - 10} autres)" + stats = change['changes']['stats'] + writer.writerow([ + change['date'], + change['filename'], + change['commit_hash'], + change['author'], + change['message'], + stats['added_count'], + stats['removed_count'], + stats['net_change'], + added_details, + removed_details + ]) + 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") + finally: + os.chdir(original_cwd) 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)