Page MenuHomeDevCentral

Automate staging commit craft
Open, Needs TriagePublic

Description

When a submodule is bumped in rSTAGING, commit messages should summarize
changes in this repository.

There is a script wrote in October 2018 to automate such commit messages:

1#!/usr/bin/env python3
2
3# -------------------------------------------------------------
4# Staging :: write a commit message for submodule update
5# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6# Project: Nasqueron
7# Created: 2018-10-27
8# License: Trivial work, not eligible to copyright
9#
10# Thanks to joki for the git diff-files and ls-tree hint
11# https://stackoverflow.com/a/52908906/1930997
12# -------------------------------------------------------------
13
14
15from git import Repo
16from os import path
17import re
18import subprocess
19
20class SubmoduleCommit:
21
22 def __init__(self, repo_path, submodule_path):
23 self.repo_path = repo_path
24 self.submodule_path = submodule_path
25 self.repo = Repo(self.submodule_path)
26
27 def craft_commit(self):
28 lines = []
29
30 old_hash = self.get_old_hash()
31 new_hash = self.get_new_hash()
32
33 lines.append("Bump " + path.basename(self.submodule_path)
34 + " to " + new_hash[:12])
35 lines.append("")
36 lines.extend(self.get_commits_lines(old_hash, new_hash))
37
38 return "\n".join(lines)
39
40 def get_old_hash(self):
41 output = subprocess.check_output(['git', 'ls-tree',
42 '@', self.submodule_path],
43 cwd=self.repo_path,
44 encoding="utf-8")
45 matches = re.search('.*commit ([a-f0-9]*).*', output.strip())
46 return matches.group(1)
47
48 def get_new_hash(self):
49 return str(self.repo.head.commit)
50
51 def format_commit_line(self, commit):
52 hash = str(commit)[:12]
53 title = commit.message.split("\n")[0]
54
55 return " * {} {}".format(hash, title)
56
57 def get_commits_lines(self, hash_base, hash_head):
58 commits_lines = []
59
60 for commit in self.repo.iter_commits(hash_head):
61 if str(commit) == hash_base:
62 break
63
64 line = self.format_commit_line(commit)
65 commits_lines.append(line)
66
67 return commits_lines
68
69 def has_submodule_been_updated(self):
70 process = subprocess.run(['git', 'diff-files', '--quiet',
71 self.submodule_path],
72 cwd=self.repo_path)
73 return process.returncode != 0
74
75
76def run(repo_path):
77 repo = Repo(repo_path)
78
79 submodules = [SubmoduleCommit(repo_path, submodule.name)
80 for submodule in repo.submodules]
81
82 commits = [submodule.craft_commit()
83 for submodule in submodules
84 if submodule.has_submodule_been_updated()]
85
86 print("\n\n".join(commits))
87
88
89def determine_current_repo():
90 return Repo('.', search_parent_directories=True).working_tree_dir
91
92
93if __name__ == "__main__":
94 current_repo_path = determine_current_repo()
95 run(current_repo_path)

It's intended to be used as git commit -a -m "$(staging-commit-message)"

Could we provision that on development servers?

Then, perhaps create a page on agora wiki for the staging repository and explain there this command.

Event Timeline

dereckson moved this task from Backlog to Ops on the easy board.

Requires py37-GitPython (map.jinja offers a packages dictionary to get the py37- prefix)

dereckson updated the task description. (Show Details)
dereckson added a project: documentation.