Last active
February 7, 2025 11:14
-
-
Save Tobiaqs/3b37a3ce35d81237b597c6a2f9ce8e3b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# SBDiff v25.02 | |
# See https://gist.github.com/Tobiaqs/3b37a3ce35d81237b597c6a2f9ce8e3b for the current version | |
import os | |
import shlex | |
import subprocess | |
import sys | |
if len(sys.argv) != 4: | |
print("Usage: sbdiff.py <folder> <older_tag> <newer_tag>") | |
exit(1) | |
repo_folder = sys.argv[1] | |
if not os.path.isdir(repo_folder): | |
print(f"Folder {repo_folder} does not exist.") | |
exit(1) | |
tag_older = sys.argv[2] | |
tag_newer = sys.argv[3] | |
subprocess.run(shlex.split("rm -rf /tmp/_sbdiff_1 /tmp/_sbdiff_2")) | |
print("Copying repo...") | |
subprocess.run(["cp", "-r", repo_folder, "/tmp/_sbdiff_1"]) | |
print("Cleaning repo...") | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 clean -fdx"), stdout=subprocess.DEVNULL | |
) | |
print("GC...") | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 gc --prune=now --aggressive"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 submodule deinit --all"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 reset --hard HEAD"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
print("Fetching from remote...") | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 fetch"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
subprocess.run( | |
["cp", "-r", "/tmp/_sbdiff_1", "/tmp/_sbdiff_2"], stdout=subprocess.DEVNULL | |
) | |
def count_files(folder): | |
return int( | |
subprocess.run( | |
"find . -type f | grep -v ./.git/ | wc -l", | |
shell=True, | |
cwd=folder, | |
stdout=subprocess.PIPE, | |
) | |
.stdout.decode() | |
.strip() | |
) | |
subprocess.run( | |
shlex.split(f"git -C /tmp/_sbdiff_1 checkout -f {tag_older}"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
if ( | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 status -s"), stdout=subprocess.PIPE | |
) | |
.stdout.decode() | |
.strip() | |
): | |
print("E: Repo is not clean") | |
exit(1) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 submodule init"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 submodule update"), stdout=subprocess.DEVNULL | |
) | |
subprocess.run( | |
shlex.split(f"git -C /tmp/_sbdiff_2 checkout -f {tag_newer}"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
if ( | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_2 status -s"), stdout=subprocess.PIPE | |
) | |
.stdout.decode() | |
.strip() | |
): | |
print("E: Repo is not clean") | |
exit(1) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_2 submodule init"), | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL, | |
) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_2 submodule update"), stdout=subprocess.DEVNULL | |
) | |
file_count_older = count_files("/tmp/_sbdiff_1") | |
file_count_newer = count_files("/tmp/_sbdiff_2") | |
subprocess.run(shlex.split("rm -rf /tmp/_sbdiff_1/.git"), stdout=subprocess.DEVNULL) | |
subprocess.run(shlex.split("git -C /tmp/_sbdiff_1 init"), stdout=subprocess.DEVNULL) | |
subprocess.run(shlex.split("git -C /tmp/_sbdiff_1 add ."), stdout=subprocess.DEVNULL) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_1 commit -m 1st"), stdout=subprocess.DEVNULL | |
) | |
subprocess.run(shlex.split("rm -rf /tmp/_sbdiff_2/.git"), stdout=subprocess.DEVNULL) | |
subprocess.run( | |
shlex.split("mv /tmp/_sbdiff_1/.git /tmp/_sbdiff_2/.git"), stdout=subprocess.DEVNULL | |
) | |
subprocess.run(shlex.split("git -C /tmp/_sbdiff_2 add ."), stdout=subprocess.DEVNULL) | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_2 commit -m 2nd"), stdout=subprocess.DEVNULL | |
) | |
diff = ( | |
subprocess.run( | |
shlex.split("git -C /tmp/_sbdiff_2 diff --raw HEAD^ HEAD"), | |
stdout=subprocess.PIPE, | |
) | |
.stdout.decode() | |
.strip() | |
) | |
subprocess.run(shlex.split("rm -rf /tmp/_sbdiff_1"), stdout=subprocess.DEVNULL) | |
subprocess.run(shlex.split("rm -rf /tmp/_sbdiff_2"), stdout=subprocess.DEVNULL) | |
modified_files = 0 | |
added_files = 0 | |
deleted_files = 0 | |
for line in diff.split("\n"): | |
if line: | |
if line[31] == "M": | |
modified_files += 1 | |
if line[31] == "A": | |
added_files += 1 | |
if line[31] == "D": | |
deleted_files += 1 | |
print(f"Modified files: {modified_files}") | |
print(f"Added files: {added_files}") | |
print(f"Deleted files: {deleted_files}") | |
print() | |
print(f"Files count in {tag_older}: {file_count_older}") | |
print(f"Files count in {tag_newer}: {file_count_newer}") | |
print() | |
print(f"Modified percentage: {round(modified_files / min(file_count_older, file_count_newer) * 1000) / 10}%") | |
print(f"Added percentage: {round(added_files / min(file_count_older, file_count_newer) * 1000) / 10}%") | |
print(f"Deleted percentage: {round(deleted_files / min(file_count_older, file_count_newer) * 1000) / 10}%") | |
print(f"Added + deleted + modified percentage: {round((modified_files + added_files + deleted_files) / min(file_count_older, file_count_newer) * 1000) / 10}%") | |
print(f"Percentual different in file count: {round((file_count_newer - file_count_older) / file_count_older * 1000) / 10}%") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment