Skip to content

Instantly share code, notes, and snippets.

@c-mauderer
Created February 3, 2023 19:37
Show Gist options
  • Save c-mauderer/a82e21eb250cb96c3a36f107b92dab09 to your computer and use it in GitHub Desktop.
Save c-mauderer/a82e21eb250cb96c3a36f107b92dab09 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Copyright (c) 2021 Christian Mauderer <oss@c-mauderer.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import git
import logging
import argparse
import sys
parser = argparse.ArgumentParser(
description=(
"Compare commits on two branches with a common parent\n"
))
parser.add_argument(
"-l", "--loglevel",
help = "Define log level.",
type = str.upper,
choices = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
default = 'INFO'
)
parser.add_argument(
"-1", "--one",
help = "Name of the first branch",
default = 'master',
)
parser.add_argument(
"-2", "--two",
help = "Name of the second branch",
default = '6-freebsd-12',
)
parser.add_argument(
"-r", "--repo",
help = "Location of the repository.",
default = '.',
)
args = parser.parse_args()
# set up logging
numeric_loglevel = getattr(logging, args.loglevel)
logging.basicConfig(level = numeric_loglevel, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
logger.debug(f"Started with the following options: {str(args)}")
# Open repo
repo = git.Repo(args.repo)
def readable(commit):
sha = commit.hexsha[:10]
author = commit.author
subject = commit.message.split("\n")[0]
return f'{sha} ({author}: {subject})'
# find common ancestor of branches
one = repo.commit(args.one)
logger.info(f"Head of {args.one}: {readable(one)}")
two = repo.commit(args.two)
logger.info(f"Head of {args.two}: {readable(two)}")
merge_base = repo.merge_base(one, two)[0]
logger.info(f"Common ancestor: {readable(merge_base)}")
# Go through commits and index them by commit message
def get_commits(commit, merge_base):
info = {}
c = commit
while c != merge_base:
subject = c.message.split("\n")[0]
author = c.author
key = f"{author}: {subject}"
info[key] = c
c = c.parents[0]
return info
one_commits = get_commits(one, merge_base)
two_commits = get_commits(two, merge_base)
for k in one_commits.keys():
if k not in two_commits:
logger.warning(f"Only on {args.one}: {readable(one_commits[k])}")
for k in two_commits.keys():
if k not in one_commits:
logger.warning(f"Only on {args.two}: {readable(two_commits[k])}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment