blob: e547822b7edcce8595f4d1c7cd8347c0388136f9 [file] [log] [blame]
Marian Harbachebeb1542019-12-13 10:42:46 +01001:linkattrs:
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002= commit-msg Hook
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -07003
David Pursehousebecc8d42013-08-08 15:06:24 +09004
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08005== NAME
David Pursehousebecc8d42013-08-08 15:06:24 +09006
7
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -07008commit-msg - Edit commit messages to insert a `Change-Id` tag.
9
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080010== DESCRIPTION
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070011
David Pursehousebecc8d42013-08-08 15:06:24 +090012
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070013A Git hook automatically invoked by `git commit`, and most other
14commit creation tools such as `git citool` or `git gui`. The Gerrit
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070015Code Review supplied implementation of this hook is a short shell
David Pursehousebecc8d42013-08-08 15:06:24 +090016script which automatically inserts a globally unique `Change-Id` tag
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -070017in the footer of a commit message. When present, Gerrit uses this
18tag to track commits across cherry-picks and rebases.
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070019
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070020After the hook has been installed in the user's local Git repository
21for a project, the hook will modify a commit message such as:
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070022
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -070023----
David Shevitzb1a96522018-09-24 10:33:11 -070024Improve foo widget by attaching a bar.
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070025
David Shevitzb1a96522018-09-24 10:33:11 -070026We want a bar, because it improves the foo by providing more
27wizbangery to the dowhatimeanery.
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070028
David Shevitzb1a96522018-09-24 10:33:11 -070029Signed-off-by: A. U. Thor <author@example.com>
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -070030----
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070031
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070032by inserting a new `Change-Id: ` line in the footer:
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070033
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -070034----
David Shevitzb1a96522018-09-24 10:33:11 -070035Improve foo widget by attaching a bar.
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070036
David Shevitzb1a96522018-09-24 10:33:11 -070037We want a bar, because it improves the foo by providing more
38wizbangery to the dowhatimeanery.
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070039
David Shevitzb1a96522018-09-24 10:33:11 -070040Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
41Signed-off-by: A. U. Thor <author@example.com>
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -070042----
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070043
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070044The hook implementation is reasonably intelligent at inserting the
David Pursehousebecc8d42013-08-08 15:06:24 +090045`Change-Id` line before any `Signed-off-by` or `Acked-by` lines placed
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070046at the end of the commit message by the author, but if no such
47lines are present then it will just insert a blank line, and add
David Pursehousebecc8d42013-08-08 15:06:24 +090048the `Change-Id` at the bottom of the message.
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070049
David Pursehousebecc8d42013-08-08 15:06:24 +090050If a `Change-Id` line is already present in the message footer, the
51script will do nothing, leaving the existing `Change-Id` unmodified.
Shawn O. Pearce67a09e82009-08-22 13:30:30 -070052This permits amending an existing commit, or allows the user to
53insert the Change-Id manually after copying it from an existing
54change viewed on the web.
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070055
David Pursehousec05da5b52013-08-08 15:09:12 +090056The `Change-Id` will not be added if `gerrit.createChangeId` is set
57to `false` in the git config.
58
Peter Collingbourne8cab9332020-08-18 14:42:44 -070059If `gerrit.reviewUrl` is set to the base URL of the Gerrit server that
60changes are uploaded to (e.g. `https://gerrit-review.googlesource.com/`)
61in the git config, then instead of adding a `Change-Id` trailer, a `Link`
62trailer will be inserted that will look like this:
63
64----
65Improve foo widget by attaching a bar.
66
67We want a bar, because it improves the foo by providing more
68wizbangery to the dowhatimeanery.
69
70Link: https://gerrit-review.googlesource.com/id/Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
71Signed-off-by: A. U. Thor <author@example.com>
72----
73
74This link will become a valid link to the review page once the change is
75uploaded to the Gerrit server. Newer versions of the Gerrit server will read
76the change identifier out of the appropriate `Link` trailer and treat it in
77the same way as the change identifier in a `Change-Id` trailer.
78
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080079== OBTAINING
David Pursehousebecc8d42013-08-08 15:06:24 +090080
David Pursehousebecc8d42013-08-08 15:06:24 +090081To obtain the `commit-msg` script use `scp`, `wget` or `curl` to download
82it to your local system from your Gerrit server.
Fredrik Luthanderfb9536d2011-10-28 07:00:53 +020083
84You can use either of the below commands:
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070085
Michael Ochmannb99feab2016-07-06 14:10:22 +020086----
David Shevitzb1a96522018-09-24 10:33:11 -070087$ scp -p -P 29418 <your username>@<your Gerrit review server>:hooks/commit-msg <local path to your git>/.git/hooks/
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -070088
David Shevitzb1a96522018-09-24 10:33:11 -070089$ curl -Lo <local path to your git>/.git/hooks/commit-msg <your Gerrit http URL>/tools/hooks/commit-msg
Michael Ochmannb99feab2016-07-06 14:10:22 +020090----
Fredrik Luthanderfb9536d2011-10-28 07:00:53 +020091
92A specific example of this might look something like this:
93
Michael Ochmannb99feab2016-07-06 14:10:22 +020094----
David Shevitzb1a96522018-09-24 10:33:11 -070095$ scp -p -P 29418 john.doe@review.example.com:hooks/commit-msg ~/duhproject/.git/hooks/
Fredrik Luthanderfb9536d2011-10-28 07:00:53 +020096
David Shevitzb1a96522018-09-24 10:33:11 -070097$ curl -Lo ~/duhproject/.git/hooks/commit-msg http://review.example.com/tools/hooks/commit-msg
Michael Ochmannb99feab2016-07-06 14:10:22 +020098----
Shawn O. Pearce3e4e8042010-01-29 12:48:21 -080099
David Pursehouse056ed182012-08-16 10:19:14 +0900100Make sure the hook file is executable:
101
Michael Ochmannb99feab2016-07-06 14:10:22 +0200102----
David Shevitzb1a96522018-09-24 10:33:11 -0700103$ chmod u+x ~/duhproject/.git/hooks/commit-msg
Michael Ochmannb99feab2016-07-06 14:10:22 +0200104----
David Pursehouse056ed182012-08-16 10:19:14 +0900105
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800106== SEE ALSO
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700107
David Pursehousebecc8d42013-08-08 15:06:24 +0900108
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700109* link:user-changeid.html[Change-Id Lines]
Marian Harbach34253372019-12-10 18:01:31 +0100110* link:http://www.kernel.org/pub/software/scm/git/docs/git-commit.html[git-commit(1),role=external,window=_blank]
111* link:http://www.kernel.org/pub/software/scm/git/docs/githooks.html[githooks(5),role=external,window=_blank]
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700112
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800113== IMPLEMENTATION
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700114
David Pursehousebecc8d42013-08-08 15:06:24 +0900115
116The hook generates unique `Change-Id` lines by creating a virtual
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700117commit object within the local Git repository, and obtaining the
118SHA-1 hash from it. Like any other Git commit, the following
119properties are included in the computation:
120
121* SHA-1 of the tree being committed
122* SHA-1 of the parent commit
123* Name, email address, timestamp of the author
124* Name, email address, timestamp of the committer
David Pursehousebecc8d42013-08-08 15:06:24 +0900125* Proposed commit message (before `Change-Id` was inserted)
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700126
127Because the names of the tree and parent commit, as well as the
128committer timestamp are included in the hash computation, the output
David Pursehousebecc8d42013-08-08 15:06:24 +0900129`Change-Id` is sufficiently unique.
Shawn O. Pearce518fe3f2009-08-22 15:40:58 -0700130
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -0700131GERRIT
132------
Shawn O. Pearcebb2b5672009-08-21 18:33:10 -0700133Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -0700134
135SEARCHBOX
136---------