0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gitにcommitするときにブランチからメッセージを追加するメモ

Posted at
commit-msg
#!/bin/sh

if [ -n "${GIT_DIR}" ]; then
    hooksdir="./${GIT_DIR}/hooks/"
else
    hooksdir="./.git/hooks/"
fi

. "${hooksdir}common.sh"

ticket="$(extractTicketId)"
if [ -n "${ticket}" ]; then
    appendMsgTo1stLine "$1" "${ticket}"
fi
common.sh
#! /bin/sh

getGitBranchName()
{
    branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
            "$(git describe --contains --all HEAD)"
    echo ${branch##refs/heads/}
}

isOnMasterBranch()
{
    if [ "$(getGitBranchName)" = "master" ]; then
        return 0
    fi
    return 1
}

isOnDevelopBranch()
{
    if [ "$(getGitBranchName)" = "develop" ]; then
        return 0
    fi
    return 1
}

isOnFixBranch()
{
    if [[ "$(getGitBranchName)" =~ ^feature/#[0-9]+.* ]]; then
        return 0
    fi
    return 1
}


appendMsgTo1stLine()
{
    mv $1 $1.$$
    if [ -s "$1.$$" ]; then
    if head -1 "$1.$$" | grep "$2" > /dev/null; then
        cp "$1.$$" "$1"
    else
            sed '1s/$/ '"$2"'/' "$1.$$" > $1
    fi
    else
        echo "$2" > "$1"
    fi
    rm -f $1.$$
}

extractTicketId()
{
    echo "$(getGitBranchName)" \
    | awk 'BEGIN{ FS="[/]"}
           $1 == "feature" || $1 == "id" { printf "#%s", $2, $3 }
           $2 == "feature" || $2 == "id" { printf "#%s", $3, $4 }
           '
}

hasTicketId()
{
    first="$(git cat-file -p $1 \
    | sed '1,/^$/d' | head -1 \
    | sed '/.* #[0-9][0-9]*.*/!d')"

    if [ -n "${first}" ]; then
        echo "true"
    else
        echo "false"
    fi
}

extractParents()
{
    parents="$(git cat-file -p $1 \
    | grep '^parent [0-9a-f]\{40\}$')"
    echo "${parents##parent }"
}

オリジン2つ登録

git remote add foofoo git@github.com:redamoon/foofoo.git

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?