Refactor changelog based on git history

This commit is contained in:
Benjamin Bohard 2020-08-25 11:25:34 +02:00
parent 0d5470e1a7
commit 878b8c48ce
2 changed files with 203 additions and 77 deletions

View File

@ -3,7 +3,7 @@
cd src
if [ ! -f debian/changelog ]; then
tamarin_info "No changelog. Skipping..."
tamarin_info "No changelog. Skipping adding suffix to package version"
exit
fi
@ -12,21 +12,28 @@ if [ $(tamarin_db get no_version_suffix 'no') == 'yes' ]; then
exit
fi
tamarin_info "Suffixing package version"
if [ -d .git ]; then
tamarin_info "It seems to be a Git repository. Generating version suffix based on Git history..."
commit_tag=$(git describe --match "pkg/*")
pkg_tag=$(git describe --match "pkg/*" --abbrev=0)
commit_count=$(git rev-list --count ${pkg_tag}..HEAD)
current_commit=$(git log -n1 --pretty=format:%h)
tamarin_info $pkg_tag
tamarin_info $commit_count
tamarin_info $current_commit
version_suffix=~${commit_count}.${current_commit}
release_tag=$(git describe --match "release/*" --abbrev=0 2>/dev/null)
if [ -z "${release_tag}" ]
then
dev_commit_count=$(git rev-list --count --no-merges develop)
else
dev_commit_count=$(git rev-list --count ${release_tag}..master)
fi
if [ ${dev_commit_count} -gt 0 ]
then
build_level=$(tamarin_db get build_level 'dev')
version_suffix="~$build_level+${dev_commit_count}-$(git log -n1 --format=%h)"
else
version_suffix=''
fi
else
tamarin_info "Not a Git project. Fallback to timestamp for suffix generation..."
version_suffix=tamarin$(date +%Y%m%d%H%M)
fi
tamarin_info $version_suffix
tamarin_info "Suffixing package version with $version_suffix"
sed -i "0,/(\(.*\))/s/(\(.*\))/(\1${version_suffix})/" debian/changelog
cp debian/changelog /dist/changelog

View File

@ -1,26 +1,36 @@
#!/usr/bin/env bash
cd src
if [ -f debian/changelog ] || [ ! -d .git ]; then
tamarin_info "Not a Git repository or Debian changelog already exists !"
exit
else
tamarin_info "Creating changelog with commits information."
fi
# Get pkg tags as tie points in commit history
pkg_tags="$(git for-each-ref --format '%(refname)' refs/tags | tac)"
# Set starting commit
start_commit='HEAD'
ceiling_commit=$(git describe --match "build/*" --abbrev=0 2>/dev/null)
if [ -n "ceiling_commit" ]
then
ceiling_commit="HEAD"
fi
first_commit=$(git rev-list --max-parents=0 master)
# Get commits log as changelog
current_release=$(git describe --match "release/*" --abbrev=0)
current_release_tag=$(git describe --match "release/*" --abbrev=0 2>/dev/null)
if [[ -z ${current_release} ]]
if [[ -z ${current_release_tag} ]]
then
tamarin_warn "No release tag found, you repo must have a tag like 'release/*'"
exit 1
tamarin_warn "No release tag found, you repo should have a tag like 'release/*'"
tamarin_info "Assuming tag release/0.0.0 on first commit of branch master"
current_release="release/0.0.0"
current_release_tag=${first_commit}
else
current_release=$current_release_tag
fi
touch debian/changelog
@ -29,90 +39,199 @@ project_name=$(tamarin_db get project_name)
distribution=$(tamarin_db get distribution UNRELEASED)
urgency=$(tamarin_db get urgency low)
function gen_changelog_entry {
start_commit=$1
previous_pkg="$(git describe --long --always --match='pkg/*' $start_commit)"
if [[ "${previous_pkg}" =~ "pkg/" ]]
then
tamarin_info "dernier paquet trouvé : ${previous_pkg}"
extended_version=$(echo $previous_pkg | cut -d "/" -f4)
exploded_version="$(echo $extended_version | sed "s/\([a-z0-9.]\+\)-\([0-9]\+\)\(-\([0-9]\+\)-\(g[a-z0-9]\+\)\)\?$/\1 \2 \4 \5/")"
upstream_version=$(echo $exploded_version | cut -d " " -f1)
package_version=$(echo $exploded_version | cut -d " " -f2)
distance_to_pkg=$(echo $exploded_version | cut -d " " -f3)
if [[ "${distance_to_pkg}" > 0 ]]
then
tamarin_info "${distance_to_pkg} commit depuis le dernier paquet"
stop_commit="$(git describe --abbrev=0 --always --match='pkg/*' $start_commit)"
version=$(expr $package_version + 1)
else
stop_commit="$(git describe --abbrev=0 --always --match='pkg/*' ${start_commit}^1)"
version=$package_version
tamarin_info "version ${tamarin}"
fi
if [[ "${start_commit}" = "${stop_commit}" ]]
then
return 1
fi
elif [[ "${start_commit}" = "${stop_commit}" ]]
then
return 1
else
tamarin_info "Pas encore de paquet pour ce dépôt"
version="1"
upstream_version="${current_release#release/}"
stop_commit="${current_release}"
fi
function get_hash {
# Return commit hash from various pointer
pointer="$1"
echo "$(git log -n1 --format=%H ${pointer})"
}
# Set the top commiter as the maintainer of the project if not defined
# If commit is tagged with pkg/, use tagger name as maintainer
# Else if commit is not tagged with pkg/, use commiter name as maintainer
current_release="$(git describe --abbrev=0 --always --match='release/*' $start_commit)"
tamarin_info "Création de lentrée de changelog entre ${start_commit} et ${stop_commit}"
if [[ "$current_release" = "$start_commit" ]] # No package yet, git describe --always returned commit itself
function get_previous_pkg_tag {
# Return previous pkg/* tag or current tag if no previous pkg/* exists.
commit="$1"
echo "$(git describe --abbrev=0 --always --match='pkg/*' $commit)"
}
function parse_tag {
tag="$1"
flavor="${tag%%/*}"
extended_version="${tag##*/}"
if [ "$flavor" = "pkg" ]
then
echo "on passe bien là"
current_release='UNRELEASED'
exploded_version="$(echo $extended_version | sed "s/\([a-z0-9.]\+\)-\([0-9]\+\)\(-[a-z]\++[0-9]\+\)\?\(-\([0-9]\+\)-\(g[a-z0-9]\+\)\)\?$/version:\1 revision:\2 modification:\3 distance:\5 anchor:\6/")"
elif [ "$flavor" = "release" ]
then
exploded_version="$(echo $extended_version | sed "s/\([a-z0-9.]\+\)\(-\([0-9]\+\)-\(g[a-z0-9]\+\)\)\?$/version:\1 distance:\3 anchor:\4/")"
fi
echo $exploded_version
}
function get_distance_from_tag {
description_tag="$1"
tag=${description_tag%-*-*}
anchor="$2"
if [[ "$(get_hash $tag)" =~ "$(get_hash $anchor)" ]]
then
echo 0
else
distance="$(parse_tag $description_tag)"
distance="${distance#*distance:}"
distance="${distance%% *}"
echo $distance
fi
}
function get_upstream_version_from_tag {
tag="$1"
upstream="$(parse_tag $tag)"
upstream="${upstream#*version:}"
upstream="${upstream%% *}"
echo "$upstream"
}
function get_package_version_from_tag {
tag="$1"
package="$(parse_tag $tag)"
package="${package#*revision:}"
package="${package%% *}"
echo "$package"
}
function get_previous_release_tag {
# Return previous pkg/* tag or current tag if no previous pkg/* exists.
commit="$1"
echo "$(git describe --abbrev=0 --always --match='release/*' $commit)"
}
function on_pkg_tag {
# Return 1 if current commit is tagged with pkg/* tag.
commit="$1"
nearest_old_pkg_tag="$(get_previous_pkg_tag $commit)"
if [ "$(get_hash ${commit})" = "$(get_hash ${nearest_old_pkg_tag})" ]
then
return 0
else
return 1
fi
if [ "${start_commit}" == "HEAD" ]
}
function next_step {
# Return previous pkg/* tag or first commit if no pkg/* tag exists
commit="$1"
if [ "$(get_hash $commit)" = "$(get_hash $first_commit)" ]
then
echo $commit
elif on_pkg_tag $commit
then
nearest_old_pkg_tag="$(get_previous_pkg_tag ${commit}^1)"
else
nearest_old_pkg_tag="$(get_previous_pkg_tag ${commit})"
fi
if [[ "$nearest_old_pkg_tag" =~ 'pkg/' ]]
then
echo $nearest_old_pkg_tag
else
echo $first_commit
fi
}
function date_from_commit {
# Return date suitable for changelog entry signature
commit="$1"
if [ "$(get_hash ${commit})" = "$(get_hash HEAD)" ]
then
package_date=$(date --rfc-2822)
else
maintainer_commit="$(get_previous_pkg_tag $commit)"
package_date="$(git tag -l --format='%(creator)' ${maintainer_commit})"
package_date="${package_date##*> }"
package_date="$(date --rfc-2822 -d @${package_date% *})"
fi
echo "$package_date"
}
function packager_from_commit {
# Return Name <mail> id format, suitable for changelog entry signature
commit="$1"
if [ "${commit}" = "HEAD" ]
then
maintainer="$(git log -n1 --format='%cn <%ce>')"
maintainer=$(tamarin_db get maintainer "${maintainer}")
else
maintainer_commit="$(git describe --abbrev=0 --always --match='pkg/*' $start_commit)"
maintainer_commit="$(get_previous_pkg_tag $commit)"
maintainer="$(git tag -l --format='%(creator)' ${maintainer_commit})"
maintainer="${maintainer%>*}>"
fi
maintainer=$(tamarin_db get maintainer "${maintainer}")
echo "$maintainer"
}
function next_version {
commit="$1"
# upstream version is given by most recent of release or pkg tag
previous_pkg="$(git describe --long --match='pkg/*' $commit 2>/dev/null)"
previous_release="$(git describe --long --match='release/*' $commit 2>/dev/null)"
if [ -n "$previous_release" ] && [ -n "$previous_pkg" ]
then
distance_from_pkg=$(get_distance_from_tag "$previous_pkg" "$commit")
distance_from_release=$(get_distance_from_tag "$previous_release" "$commit")
if [ $distance_from_release -le $distance_from_pkg ]
then
echo "$(get_upstream_version_from_tag $previous_release)-1"
else
echo "$(get_upstream_version_from_tag $previous_pkg)-$(expr $(get_package_version_from_tag $previous_pkg) + 1)"
fi
elif [ -n "$previous_release" ]
then
echo "$(get_upstream_version_from_tag $previous_release)-1"
elif [ -n "$previous_pkg" ]
then
echo "$(get_upstream_version_from_tag $previous_pkg)-$(expr $(get_package_version_from_tag $previous_pkg) + 1)"
else
echo tata
fi
}
function gen_changelog_entry {
ceiling_commit=$1
floor_commit="$(next_step "${ceiling_commit}")"
if [ "$(get_hash ${ceiling_commit})" = "$(get_hash ${floor_commit})" ]
then
return 1
fi
if on_pkg_tag $ceiling_commit
then
version="$(get_upstream_version_from_tag $ceiling_commit)-$(get_package_version_from_tag $ceiling_commit)"
else
tamarin_info "current commit $ceiling_commit"
version=$(next_version $ceiling_commit)
fi
#current_release="$(git describe --abbrev=0 --always --match='release/*' $ceiling_commit)"
tamarin_info "Création de lentrée de changelog entre ${ceiling_commit} et ${floor_commit}"
maintainer="$(packager_from_commit ${ceiling_commit})"
package_date="$(date_from_commit ${ceiling_commit})"
version=${upstream_version}-${version}
version=${version/_/-}
tamarin_warn "traitement de la version $version"
changelog_entry="${project_name} (${version}) ${distribution}; urgency=${urgency}"
echo "$changelog_entry" >> debian/changelog
echo >> debian/changelog
for commit in "$(git log --format='%H' ${stop_commit}..${start_commit})"
for commit in $(git log --no-merges --format='%H' ${floor_commit}..${ceiling_commit})
do
subject="$(git log -n1 --format=%s ${commit})"
tamarin_info "${subject}"
echo " * ${subject}" >> debian/changelog
start_commit="$(git log -n1 --format='%H' ${commit}^1)"
#ceiling_commit="$(git log -n1 --format='%H' ${commit}^1)"
done
echo >> debian/changelog
changelog_sign=" -- ${maintainer} $(date --rfc-2822)"
changelog_sign=" -- ${maintainer} ${package_date}"
echo "$changelog_sign" >> debian/changelog
echo >> debian/changelog
start_commit=${stop_commit}
ceiling_commit=${floor_commit}
}
function gen_changelog() {
# while [[ $current_release != 'UNRELEASED' ]]
# do
# echo $start_commit
# echo $current_release
# gen_changelog_entry $start_commit
# done
while gen_changelog_entry $start_commit
while gen_changelog_entry $ceiling_commit
do
echo $changelog_entry
done