Marian Harbach | ebeb154 | 2019-12-13 10:42:46 +0100 | [diff] [blame] | 1 | :linkattrs: |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2 | = commit-msg Hook |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 3 | |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 4 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 5 | == NAME |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 6 | |
| 7 | |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 8 | commit-msg - Edit commit messages to insert a `Change-Id` tag. |
| 9 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 10 | == DESCRIPTION |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 11 | |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 12 | |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 13 | A Git hook automatically invoked by `git commit`, and most other |
| 14 | commit creation tools such as `git citool` or `git gui`. The Gerrit |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 15 | Code Review supplied implementation of this hook is a short shell |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 16 | script which automatically inserts a globally unique `Change-Id` tag |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 17 | in the footer of a commit message. When present, Gerrit uses this |
| 18 | tag to track commits across cherry-picks and rebases. |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 19 | |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 20 | After the hook has been installed in the user's local Git repository |
| 21 | for a project, the hook will modify a commit message such as: |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 22 | |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 23 | ---- |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 24 | Improve foo widget by attaching a bar. |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 25 | |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 26 | We want a bar, because it improves the foo by providing more |
| 27 | wizbangery to the dowhatimeanery. |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 28 | |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 29 | Signed-off-by: A. U. Thor <author@example.com> |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 30 | ---- |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 31 | |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 32 | by inserting a new `Change-Id: ` line in the footer: |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 33 | |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 34 | ---- |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 35 | Improve foo widget by attaching a bar. |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 36 | |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 37 | We want a bar, because it improves the foo by providing more |
| 38 | wizbangery to the dowhatimeanery. |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 39 | |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 40 | Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b |
| 41 | Signed-off-by: A. U. Thor <author@example.com> |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 42 | ---- |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 43 | |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 44 | The hook implementation is reasonably intelligent at inserting the |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 45 | `Change-Id` line before any `Signed-off-by` or `Acked-by` lines placed |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 46 | at the end of the commit message by the author, but if no such |
| 47 | lines are present then it will just insert a blank line, and add |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 48 | the `Change-Id` at the bottom of the message. |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 49 | |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 50 | If a `Change-Id` line is already present in the message footer, the |
| 51 | script will do nothing, leaving the existing `Change-Id` unmodified. |
Shawn O. Pearce | 67a09e8 | 2009-08-22 13:30:30 -0700 | [diff] [blame] | 52 | This permits amending an existing commit, or allows the user to |
| 53 | insert the Change-Id manually after copying it from an existing |
| 54 | change viewed on the web. |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 55 | |
David Pursehouse | c05da5b5 | 2013-08-08 15:09:12 +0900 | [diff] [blame] | 56 | The `Change-Id` will not be added if `gerrit.createChangeId` is set |
| 57 | to `false` in the git config. |
| 58 | |
Peter Collingbourne | 8cab933 | 2020-08-18 14:42:44 -0700 | [diff] [blame] | 59 | If `gerrit.reviewUrl` is set to the base URL of the Gerrit server that |
| 60 | changes are uploaded to (e.g. `https://gerrit-review.googlesource.com/`) |
| 61 | in the git config, then instead of adding a `Change-Id` trailer, a `Link` |
| 62 | trailer will be inserted that will look like this: |
| 63 | |
| 64 | ---- |
| 65 | Improve foo widget by attaching a bar. |
| 66 | |
| 67 | We want a bar, because it improves the foo by providing more |
| 68 | wizbangery to the dowhatimeanery. |
| 69 | |
| 70 | Link: https://gerrit-review.googlesource.com/id/Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b |
| 71 | Signed-off-by: A. U. Thor <author@example.com> |
| 72 | ---- |
| 73 | |
| 74 | This link will become a valid link to the review page once the change is |
| 75 | uploaded to the Gerrit server. Newer versions of the Gerrit server will read |
| 76 | the change identifier out of the appropriate `Link` trailer and treat it in |
| 77 | the same way as the change identifier in a `Change-Id` trailer. |
| 78 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 79 | == OBTAINING |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 80 | |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 81 | To obtain the `commit-msg` script use `scp`, `wget` or `curl` to download |
| 82 | it to your local system from your Gerrit server. |
Fredrik Luthander | fb9536d | 2011-10-28 07:00:53 +0200 | [diff] [blame] | 83 | |
| 84 | You can use either of the below commands: |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 85 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 86 | ---- |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 87 | $ scp -p -P 29418 <your username>@<your Gerrit review server>:hooks/commit-msg <local path to your git>/.git/hooks/ |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 88 | |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 89 | $ curl -Lo <local path to your git>/.git/hooks/commit-msg <your Gerrit http URL>/tools/hooks/commit-msg |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 90 | ---- |
Fredrik Luthander | fb9536d | 2011-10-28 07:00:53 +0200 | [diff] [blame] | 91 | |
| 92 | A specific example of this might look something like this: |
| 93 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 94 | ---- |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 95 | $ scp -p -P 29418 john.doe@review.example.com:hooks/commit-msg ~/duhproject/.git/hooks/ |
Fredrik Luthander | fb9536d | 2011-10-28 07:00:53 +0200 | [diff] [blame] | 96 | |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 97 | $ curl -Lo ~/duhproject/.git/hooks/commit-msg http://review.example.com/tools/hooks/commit-msg |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 98 | ---- |
Shawn O. Pearce | 3e4e804 | 2010-01-29 12:48:21 -0800 | [diff] [blame] | 99 | |
David Pursehouse | 056ed18 | 2012-08-16 10:19:14 +0900 | [diff] [blame] | 100 | Make sure the hook file is executable: |
| 101 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 102 | ---- |
David Shevitz | b1a9652 | 2018-09-24 10:33:11 -0700 | [diff] [blame] | 103 | $ chmod u+x ~/duhproject/.git/hooks/commit-msg |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 104 | ---- |
David Pursehouse | 056ed18 | 2012-08-16 10:19:14 +0900 | [diff] [blame] | 105 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 106 | == SEE ALSO |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 107 | |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 108 | |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 109 | * link:user-changeid.html[Change-Id Lines] |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 110 | * 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. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 112 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 113 | == IMPLEMENTATION |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 114 | |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 115 | |
| 116 | The hook generates unique `Change-Id` lines by creating a virtual |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 117 | commit object within the local Git repository, and obtaining the |
| 118 | SHA-1 hash from it. Like any other Git commit, the following |
| 119 | properties 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 Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 125 | * Proposed commit message (before `Change-Id` was inserted) |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 126 | |
| 127 | Because the names of the tree and parent commit, as well as the |
| 128 | committer timestamp are included in the hash computation, the output |
David Pursehouse | becc8d4 | 2013-08-08 15:06:24 +0900 | [diff] [blame] | 129 | `Change-Id` is sufficiently unique. |
Shawn O. Pearce | 518fe3f | 2009-08-22 15:40:58 -0700 | [diff] [blame] | 130 | |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 131 | GERRIT |
| 132 | ------ |
Shawn O. Pearce | bb2b567 | 2009-08-21 18:33:10 -0700 | [diff] [blame] | 133 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 134 | |
| 135 | SEARCHBOX |
| 136 | --------- |