Source code for libka.git_checks
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4
############################################################################
# Copyright © 2016 José Manuel Santamaría Lema <panfaust@gmail.com> #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
############################################################################
import sys
import os
from git import Repo
from debian.debian_support import Version
#FIXME: this code probably should be converted to a KAGitRepo class with methods
# so we could do "git_repo = Repo('.')" in the constructor to avoid loading
# the repository twice, see the ka-debian2kubuntu-merge script for instance
#Check if the current branch is a valid Kubuntu branch
#if executed with interactive=False retrurns the error message or '' on success
[docs]
def kubuntuCheckValidBranch(distribution=None,interactive=True,master_accepted=True):
try:
git_repo = Repo('.')
except:
print("Git repo not found here, 'pwd': " + os.getcwd())
sys.exit(1)
branch_name = str(git_repo.active_branch)
branch_name_parts = branch_name.split('_')
warning = ''
try:
kubuntu = branch_name_parts[0]
branch_dist = branch_name_parts[1]
target = branch_name_parts[2]
if kubuntu != "kubuntu":
warning = "Current branch '%s' is not a Kubuntu branch" % branch_name
#if target not in ["archive","backports","staging","stage","updates","debian-merge"]:
# warning += '\n'+ "Current branch '%s' is not an archive, updates or backports Kubuntu branch" % branch_name
if (distribution != None) and (distribution != branch_dist):
warning = '\n'+ "Current branch '%s' is not a valid branch for distribution '%s'" % (branch_name,distribution)
except Exception:
if master_accepted and (branch_name == "master"):
warning = ""
else:
warning = "Couldn't find out distribution name for branch '%s'" % branch_name
#Ask user for confirmation when executed with interactive=True, otherwise return the warning text
if interactive:
if warning != '':
ui = input(warning + '\nContinue anyway y/N :') + ' '
if ui.lower()[0] != 'y':
sys.exit(1)
else:
return warning
#Check if the current branch has changes not pushed to git
[docs]
def checkUnpushedGitChanges():
try:
git_repo = Repo('.')
except:
print("Git repo not found here, 'pwd': " + os.getcwd())
sys.exit(1)
unpushed_changes = False
#Check that there are no modified files
if git_repo.index.diff(None):
unpushed_changes = True
#Check that there are no changes in the index
if git_repo.index.diff(git_repo.active_branch.commit):
unpushed_changes = True
#Check that the last commit from the current and origin branch are the same
branch_name = str(git_repo.active_branch)
try:
origin_commit = git_repo.refs["origin/"+branch_name].commit
current_commit = git_repo.active_branch.commit
if origin_commit != current_commit:
unpushed_changes = True
except:
unpushed_changes = True
#If there are unpushed changes, abort
if unpushed_changes:
print("You have changes not pushed to git, aborting.")
sys.exit(1)
[docs]
def getMaxTag(prefix=''):
try:
git_repo = Repo('.')
except:
print("Git repo not found here, 'pwd': " + os.getcwd())
sys.exit(1)
max_version_tag = Version('0')
for tag in git_repo.tags:
tag = str(tag)
if tag.startswith(prefix):
#Remove prefix from the string
tag = tag.split(prefix)[1]
#Replace '%' in order to support epochs properly
tag = tag.replace('%',':')
#Build the version object and check if it's the max so far
try:
version = Version(tag)
except ValueError:
#skip if the tag name is not a valid package version
continue
if version > max_version_tag:
max_version_tag = version
return prefix + str(max_version_tag).replace(':','%')