git add Signed-off-by line using format.signoff no

2019-02-12 06:26发布

My client git version is 1.7.0.4.

I want to automatically add a "Signed-off-by" line for the committer at the end of the commit log message when commit a message.

  1. When I set git config --global format.signoff true, and run git commit -m "modify something", I see no "Signed-off-by" in git log.
  2. If I use git commit -m -s "modify something", then "Signed-off-by" does show in git log.

Can anyone help?

3条回答
做自己的国王
2楼-- · 2019-02-12 07:07

There is now an easy way to automatically sign-off any commit that is not already signed-off by using hooks and the git-interpret-trailers command. In the upcoming version 2.15 of git the command allows to trivially check for an existing sign-off (no matter what's its value/author is) and add yours if there is non yet. As of October 2017 the required code is not in any git release yet (but in its master branch)!

Save the following as .git/hooks/prepare-commit-msg or .git/hooks/commit-msg (see here for the differences) and make it executable.

#!/bin/sh

NAME=$(git config user.name)
EMAIL=$(git config user.email)

if [ -z "$NAME" ]; then
    echo "empty git config user.name"
    exit 1
fi

if [ -z "$EMAIL" ]; then
    echo "empty git config user.email"
    exit 1
fi

git interpret-trailers --if-exists doNothing --trailer \
    "Signed-off-by: $NAME <$EMAIL>" \
    --in-place "$1"
查看更多
时光不老,我们不散
3楼-- · 2019-02-12 07:19

Update for Git 2.14.x/2.15: as I mentioned in "Git - Detect if commit is signed off programmatically", you will be able to parse a commit message trailer for Signed-off-By line.

"git interpret-trailers" has been taught a "--parse" and a few other options to make it easier for scripts to grab existing trailer lines from a commit log message.

See stefanct's answer for commit-msg client-side hook which would use git interpret-trailers.


Original answer (2013)

format.signoff is about patch (as illustrated, for instance, in this patch):

format.signoff

A boolean value which lets you enable the -s/--signoff option of format-patch by default

It has no bearing on git commit -s.

In other words, you don't have to signoff every commit, but when you are publishing them as patch for others to use (as in "git: submitting patches"), then you should sign them.

For the exact signification of Signed-of-by, see "What is the Sign Off feature in Git for?".

查看更多
闹够了就滚
4楼-- · 2019-02-12 07:25

You can signoff by default in a project by creating a file (say "~/MYPROJECT/git-template") that contains some blank lines and the signed-off-by text like this:

Signed-off-by: Your Name <your.email@example.com>

Then configure git to use that as a commit template. For example:

git config commit.template ~/MYPROJECT/git-template

Make sure your project documents somewhere what signed-off-by means for the project. Here's some text you can copy into your CONTRIBUTING.md file:

All contributions (including pull requests) must agree to the Developer Certificate of Origin (DCO) version 1.1. This is exactly the same one created and used by the Linux kernel developers and posted on http://developercertificate.org/. This is a developer's certification that he or she has the right to submit the patch for inclusion into the project. Simply submitting a contribution implies this agreement, however, please include a "Signed-off-by" tag in every patch (this tag is a conventional way to confirm that you agree to the DCO).

查看更多
登录 后发表回答