diff --git a/gitgud/skills/user_messages.py b/gitgud/skills/user_messages.py index 0fd2ca20..4f952f50 100644 --- a/gitgud/skills/user_messages.py +++ b/gitgud/skills/user_messages.py @@ -177,3 +177,44 @@ def display_entry(index, human_name, code_name, indent): code_name=item.name, indent=indent ) + + +def amending_message(before_ref, after_ref, show_hashes=True, show_files=True, show_refs=True): # noqa: E501 + file_operator = operations.get_operator() + display_data = [ + {"_name": "Before", "_commit": file_operator.repo.commit(before_ref)}, + {"_name": "After", "_commit": file_operator.repo.commit(after_ref)} + ] + + # Necessary for showing branches + if show_refs: + tree = file_operator.get_current_tree() + referred_by = {} + for branch_name in tree['branches']: + target = tree['branches'][branch_name]['target'] + if target not in referred_by: + referred_by.update({target: [branch_name]}) + else: + referred_by[target].append(branch_name) + for target in referred_by: + referred_by[target] = ", ".join(referred_by[target]) + + for snapshot in display_data: + commit = snapshot["_commit"] + snapshot["Message"] = commit.message + if show_hashes: + snapshot["Hash"] = commit.hexsha[:7] + if show_files: + files = file_operator.get_commit_content(commit) + snapshot["File"] = "Present" if files else "Missing" + + for snapshot in display_data: + print(snapshot["_name"], end="") + if show_refs: + print(" ({}):".format(referred_by[snapshot["_commit"].hexsha])) + else: + print(":") + for feature in snapshot: + if not feature.startswith("_"): + print(feature + ":", str(snapshot[feature]).strip()) + print()