Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: helper script for making ChangeLog entries



[following up on a thread from a few months ago]

Mikael Magnusson wrote on Wed, Dec 03, 2014 at 17:16:55 +0100:
> I got a bit tired of constructing these manually, not sure how you guys
> do it but here's my script for it.

Here's my version of this.  My workflow is as follows: first, commit the
patches to the 'master' branch (using either 'git am' or 'git apply &&
git commit --author'), then run

	% zshdev-add<TAB> 42
	% git push

where 42 is the X-Seq number.  The first command generates a ChangeLog
entry, updates the commit to include it (with 'git commit --amend'), and
updates the commit's log message to include the X-Seq number.  The
ChangeLog entry is based on the first sentence of the commit message.

If there are multiple local commits on top of origin/master, the script
will amend all local commits which haven't already been amended.

Cheers,

Daniel

P.S. I also use
	zstyle ':completion:*:-command-:*:commands' ignored-patterns zshdev-add-nnnnn-and-changelog-internal
#!/usr/bin/env zsh

# This is a helper script, invoked by zshdev-add-nnnnn-and-changelog.

## Declarations
local -i WIDTH=74 TAB_WIDTH=8
local title seqno=$seqno files summary
local logmsg
local entry

zmodload -F zsh/datetime b:strftime p:EPOCHSECONDS

## Validate our caller.
[[ $# -eq 0 && -n $seqno ]] || { echo "Usage error" >&2; exit 1 }

## Helper functions.
# Prepend the string $1 to the file $2.
prepend_to_file() {
  local prependum=$1
  local file=$2
  printf "0r %s\nw\nq\n" =(<<<$prependum) | ed -s -- $file
}

## Update ChangeLog.
rev=HEAD
title="$(strftime "%Y-%m-%d" $EPOCHSECONDS)  $(git log --no-walk --pretty="%aN  <%aE>" $rev --)"
files=( ${(f)$(git show --pretty=%n --name-only $rev --)} )
summary=`git log --no-walk --pretty=%s $rev --`
entry=''
entry+=$title
entry+=$'\n\n'
entry+=$(print -r - "* $seqno: ${(j:, :)files}: $summary" | fmt -w $((WIDTH - TAB_WIDTH)) | sed $'s/^/\t/g')
entry+=$'\n\n'
# Don't duplicate $title
if [[ "$title" == "$(head -n1 < ChangeLog)" ]]; then
  print -l '1,2d' 'w' 'q' | ed -s -- ChangeLog
fi
prepend_to_file $entry ChangeLog

## Commit ChangeLog, amend the commit message.
logmsg="$seqno: `git log --no-walk --pretty=%B HEAD`"
git commit --amend -m "$logmsg" ChangeLog
#!/usr/bin/env zsh

local AUTHOR_NAME=$(git config --get user.name)
local UPSTREAM=origin/master
local ENDPOINT
export seqno

fail() { echo "$1" >&2; exit 1 }

usage() {
  cat >&2 <<EOF
$0: a zsh developer's add-X-Seq-and-ChangeLog script

Usage: $0 NUMBER

Will prefix NUMBER to the log message of each local commit not yet
in $UPSTREAM, and amend the commit with a ChangeLog entry.  The following
formats are accepted for NUMBER: '42', 'users/42', 'unposted'.

Prerequisite: git must have been compiled with PCRE support.
EOF
  exit ${1:-1}
}

## Require a recent zsh
if [[ $( (){ shift -p; echo $# } arg1 arg2 arg3 ) -ne 2 ]]; then
  fail "This script requires a zsh supporting 'shift -p'"
fi

## Argument parsing
if [[ $# -ne 1 ]] ||
   [[ $1 != (<30000->*|users/<->|unposted) ]] ||
   [[ $(git log -1 --pretty=%cn) != *"${AUTHOR_NAME}"* ]] ||
   false
then
  usage
fi

seqno=$1

## Validate worktree state
if [[ -n "`git status --porcelain ChangeLog`" ]]; then
  fail "ChangeLog has local mods"
fi

if ! git merge-base --is-ancestor $UPSTREAM HEAD; then
  fail "'$UPSTREAM' must be an ancestor of HEAD, but isn't"
fi

## Set ENDPOINT to oldest commit such that $ENDPOINT..HEAD consists of commits
## by $AUTHOR_NAME that don't touch ChangeLog.

# We can't just 'git log --not --committer=$AUTHOR_NAME', since the --not is silently ignored.
# Hence we use --perl-regexp, even though it requires PCRE.
[[ $AUTHOR_NAME == *'\'* ]] && fail "bobby tables"
ENDPOINT=$(git log --perl-regexp --committer='^(?!\Q'$AUTHOR_NAME'\E)' -1 --pretty=%H)
() { 
  local arg
  # Arguments are youngest-to-oldest, so:
  while (( $# )); do
    arg=$argv[-1]
    if [[ $(git diff --name-only $arg^..$arg) != *ChangeLog* ]]; then
      ENDPOINT=$argv[-1]
      return
    fi
    shift -p
  done
} $(git rev-list $ENDPOINT..HEAD)

## Main body
seqno=$seqno \
  GIT_EDITOR='true' git rebase -i $ENDPOINT^ --exec \
    "$0-internal"


Messages sorted by: Reverse Date, Date, Thread, Author