-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import List, Optional, Dict, Any | ||
from modernmetric import process_diff_content | ||
from verinfast.utils.utils import std_exec | ||
|
||
class MetricArgs: | ||
def __init__(self): | ||
self.ignore_lexer_errors = True | ||
self.dump = False | ||
self.halstead_bug_predict_method = "default" | ||
self.maintenance_index_calc_method = "default" | ||
|
||
def analyze_git_diff(commit_id: str, file_name: str, log=None) -> Optional[Dict[str, Any]]: | ||
cmd: List[str] = [ | ||
"git", "show", "-p", | ||
f"--format=", | ||
commit_id, | ||
"--", | ||
file_name | ||
] | ||
|
||
try: | ||
diff_output = std_exec(cmd, log=log) | ||
|
||
# Skip the first 4 lines (diff headers) | ||
diff_lines = diff_output.splitlines()[4:] | ||
diff_content = '\n'.join(diff_lines) | ||
|
||
args = MetricArgs() | ||
|
||
results = process_diff_content( | ||
_content=diff_content, | ||
_file=file_name, | ||
_args=args, | ||
_importer={} | ||
) | ||
|
||
return { | ||
'metrics': results[0], | ||
'file': results[1], | ||
'language': results[2], | ||
'store': results[4] | ||
} | ||
|
||
except Exception as e: | ||
if log is not None: | ||
log(tag="git_metrics Error", msg=f"Error analyzing git diff: {e}") | ||
else: | ||
print(f"Error analyzing git diff: {e}") | ||
return None |