|
| 1 | +import os |
| 2 | +from git import Repo |
| 3 | + |
| 4 | +def generate_license_changelog(): |
| 5 | + # Setup paths |
| 6 | + repo_path = '..' |
| 7 | + output_path = 'docs/changelog.md' |
| 8 | + # We look in etc because that is where the license files live |
| 9 | + search_path = 'etc' |
| 10 | + |
| 11 | + # Open the repository |
| 12 | + print("Opening repository...") |
| 13 | + repo = Repo(repo_path) |
| 14 | + |
| 15 | + # Get the last 50 changes |
| 16 | + print("Fetching recent commits...") |
| 17 | + commits = list(repo.iter_commits(paths=search_path, max_count=50)) |
| 18 | + |
| 19 | + # Start building the Markdown table |
| 20 | + # We use a list called 'lines' to hold each row of our table |
| 21 | + lines = [ |
| 22 | + "# License Changelog", |
| 23 | + "", |
| 24 | + "| Date | Action | License | Commit |", |
| 25 | + "| :--- | :--- | :--- | :--- |" |
| 26 | + ] |
| 27 | + |
| 28 | + # Loop through each commit to find what changed |
| 29 | + for commit in commits: |
| 30 | + # Get a readable date |
| 31 | + date_str = commit.authored_datetime.strftime("%Y-%m-%d") |
| 32 | + commit_hash = commit.hexsha[:7] |
| 33 | + |
| 34 | + # Comparing this commit with the one before it |
| 35 | + if commit.parents: |
| 36 | + diffs = commit.diff(commit.parents[0]) |
| 37 | + else: |
| 38 | + # This is the very first commit in the repo |
| 39 | + diffs = commit.diff(None) |
| 40 | + |
| 41 | + for diff in diffs: |
| 42 | + # Look for the file name |
| 43 | + file_path = diff.b_path or diff.a_path |
| 44 | + |
| 45 | + # Check if it is a license YAML file |
| 46 | + if file_path.endswith('.yml') and 'licenses' in file_path: |
| 47 | + # Get just the name (like 'mit') instead of 'etc/licenses/mit.yml' |
| 48 | + license_name = os.path.basename(file_path).replace('.yml', '') |
| 49 | + |
| 50 | + # Decide if it was Added or Refined |
| 51 | + if diff.change_type == 'A': |
| 52 | + action = "Added" |
| 53 | + else: |
| 54 | + action = "Refined" |
| 55 | + |
| 56 | + # Add the row to our list |
| 57 | + new_row = f"| {date_str} | {action} | {license_name} | `{commit_hash}` |" |
| 58 | + lines.append(new_row) |
| 59 | + |
| 60 | + # Save the list to a file |
| 61 | + print(f"Saving changelog to {output_path}...") |
| 62 | + with open(output_path, 'w') as f: |
| 63 | + # Join all lines with a newline character |
| 64 | + f.write('\n'.join(lines)) |
| 65 | + |
| 66 | + print("Done!") |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + generate_license_changelog() |
0 commit comments