up path analyze
# Conflicts: # .gitignore
This commit is contained in:
parent
e530904873
commit
f41f09d98e
2 changed files with 106 additions and 115 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -3,3 +3,4 @@
|
||||||
build/*
|
build/*
|
||||||
venv
|
venv
|
||||||
__pycache__
|
__pycache__
|
||||||
|
*.json
|
||||||
|
|
@ -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,125 +149,114 @@ 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']
|
||||||
|
|
||||||
# Vérifier que nous sommes dans un dépôt Git
|
if not os.path.isdir(folder_path):
|
||||||
try:
|
print(f"Erreur: le dossier {folder_path} n'existe pas.", file=sys.stderr)
|
||||||
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)
|
sys.exit(1)
|
||||||
|
|
||||||
all_changes = []
|
# 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)
|
||||||
|
|
||||||
# Pour chaque fichier
|
all_changes = []
|
||||||
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}...")
|
for filename in files_to_track:
|
||||||
|
if not os.path.exists(filename):
|
||||||
# Récupérer l'historique
|
print(f"Attention: Le fichier {filename} n'existe pas dans {folder_path}, il sera ignoré", file=sys.stderr)
|
||||||
history = get_file_history(filename)
|
continue
|
||||||
|
print(f"Analyse de {filename}...")
|
||||||
if not history:
|
history = get_file_history(filename)
|
||||||
print(f"Aucun historique trouvé pour {filename}")
|
if not history:
|
||||||
continue
|
print(f"Aucun historique trouvé pour {filename}")
|
||||||
|
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']
|
if i < len(history) - 1:
|
||||||
|
prev_hash = history[i + 1]['hash']
|
||||||
# Récupérer les changements par rapport au commit précédent
|
changes = get_changes_between_commits(commit_hash, prev_hash, filename)
|
||||||
if i < len(history) - 1:
|
else:
|
||||||
prev_hash = history[i + 1]['hash']
|
content = get_file_content_at_commit(commit_hash, filename)
|
||||||
changes = get_changes_between_commits(commit_hash, prev_hash, filename)
|
changes = {
|
||||||
else:
|
'added_lines': [{'line': line, 'context': 'initial'} for line in content.split('\n') if line.strip()],
|
||||||
# Premier commit, récupérer le contenu initial
|
'removed_lines': [],
|
||||||
content = get_file_content_at_commit(commit_hash, filename)
|
'modified_sections': [],
|
||||||
changes = {
|
'stats': {
|
||||||
'added_lines': [{'line': line, 'context': 'initial'} for line in content.split('\n') if line.strip()],
|
'added_count': len(content.split('\n')),
|
||||||
'removed_lines': [],
|
'removed_count': 0,
|
||||||
'modified_sections': [],
|
'net_change': len(content.split('\n'))
|
||||||
'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 = {
|
||||||
if changes and changes['stats']['added_count'] + changes['stats']['removed_count'] > 0:
|
'filename': filename,
|
||||||
change_entry = {
|
'date': commit_date,
|
||||||
'filename': filename,
|
'commit_hash': commit_hash,
|
||||||
'date': commit_date,
|
'author': commit_author,
|
||||||
'commit_hash': commit_hash,
|
'message': commit_message,
|
||||||
'author': commit_author,
|
'changes': changes
|
||||||
'message': commit_message,
|
}
|
||||||
'changes': changes
|
all_changes.append(change_entry)
|
||||||
}
|
all_changes.sort(key=lambda x: x['date'])
|
||||||
all_changes.append(change_entry)
|
# Générer le JSON
|
||||||
|
print("Génération du fichier JSON...")
|
||||||
# Trier par date
|
with open('git_changes_history.json', 'w', encoding='utf-8') as f:
|
||||||
all_changes.sort(key=lambda x: x['date'])
|
json.dump(all_changes, f, ensure_ascii=False, indent=2)
|
||||||
|
# Générer le CSV
|
||||||
# Générer le JSON
|
print("Génération du fichier CSV...")
|
||||||
print("Génération du fichier JSON...")
|
with open('git_changes_history.csv', 'w', newline='', encoding='utf-8') as csvfile:
|
||||||
with open('git_changes_history.json', 'w', encoding='utf-8') as f:
|
writer = csv.writer(csvfile)
|
||||||
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']
|
|
||||||
|
|
||||||
writer.writerow([
|
writer.writerow([
|
||||||
change['date'],
|
'Date', 'Fichier', 'Commit Hash', 'Auteur', 'Message',
|
||||||
change['filename'],
|
'Lignes Ajoutées', 'Lignes Supprimées', 'Changement Net',
|
||||||
change['commit_hash'],
|
'Détails Ajouts', 'Détails Suppressions'
|
||||||
change['author'],
|
|
||||||
change['message'],
|
|
||||||
stats['added_count'],
|
|
||||||
stats['removed_count'],
|
|
||||||
stats['net_change'],
|
|
||||||
added_details,
|
|
||||||
removed_details
|
|
||||||
])
|
])
|
||||||
|
for change in all_changes:
|
||||||
print(f"Analyse terminée: {len(all_changes)} changements enregistrés")
|
added_details = "; ".join([
|
||||||
print("Fichiers générés: git_changes_history.json et git_changes_history.csv")
|
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__':
|
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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue