my customized lib
esp8266-driver/.git/hooks/pre-rebase.sample@0:8094b249013c, 2017-11-26 (annotated)
- Committer:
- DuyLionTran
- Date:
- Sun Nov 26 15:08:14 2017 +0000
- Revision:
- 0:8094b249013c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:8094b249013c | 1 | #!/bin/sh |
DuyLionTran | 0:8094b249013c | 2 | # |
DuyLionTran | 0:8094b249013c | 3 | # Copyright (c) 2006, 2008 Junio C Hamano |
DuyLionTran | 0:8094b249013c | 4 | # |
DuyLionTran | 0:8094b249013c | 5 | # The "pre-rebase" hook is run just before "git rebase" starts doing |
DuyLionTran | 0:8094b249013c | 6 | # its job, and can prevent the command from running by exiting with |
DuyLionTran | 0:8094b249013c | 7 | # non-zero status. |
DuyLionTran | 0:8094b249013c | 8 | # |
DuyLionTran | 0:8094b249013c | 9 | # The hook is called with the following parameters: |
DuyLionTran | 0:8094b249013c | 10 | # |
DuyLionTran | 0:8094b249013c | 11 | # $1 -- the upstream the series was forked from. |
DuyLionTran | 0:8094b249013c | 12 | # $2 -- the branch being rebased (or empty when rebasing the current branch). |
DuyLionTran | 0:8094b249013c | 13 | # |
DuyLionTran | 0:8094b249013c | 14 | # This sample shows how to prevent topic branches that are already |
DuyLionTran | 0:8094b249013c | 15 | # merged to 'next' branch from getting rebased, because allowing it |
DuyLionTran | 0:8094b249013c | 16 | # would result in rebasing already published history. |
DuyLionTran | 0:8094b249013c | 17 | |
DuyLionTran | 0:8094b249013c | 18 | publish=next |
DuyLionTran | 0:8094b249013c | 19 | basebranch="$1" |
DuyLionTran | 0:8094b249013c | 20 | if test "$#" = 2 |
DuyLionTran | 0:8094b249013c | 21 | then |
DuyLionTran | 0:8094b249013c | 22 | topic="refs/heads/$2" |
DuyLionTran | 0:8094b249013c | 23 | else |
DuyLionTran | 0:8094b249013c | 24 | topic=`git symbolic-ref HEAD` || |
DuyLionTran | 0:8094b249013c | 25 | exit 0 ;# we do not interrupt rebasing detached HEAD |
DuyLionTran | 0:8094b249013c | 26 | fi |
DuyLionTran | 0:8094b249013c | 27 | |
DuyLionTran | 0:8094b249013c | 28 | case "$topic" in |
DuyLionTran | 0:8094b249013c | 29 | refs/heads/??/*) |
DuyLionTran | 0:8094b249013c | 30 | ;; |
DuyLionTran | 0:8094b249013c | 31 | *) |
DuyLionTran | 0:8094b249013c | 32 | exit 0 ;# we do not interrupt others. |
DuyLionTran | 0:8094b249013c | 33 | ;; |
DuyLionTran | 0:8094b249013c | 34 | esac |
DuyLionTran | 0:8094b249013c | 35 | |
DuyLionTran | 0:8094b249013c | 36 | # Now we are dealing with a topic branch being rebased |
DuyLionTran | 0:8094b249013c | 37 | # on top of master. Is it OK to rebase it? |
DuyLionTran | 0:8094b249013c | 38 | |
DuyLionTran | 0:8094b249013c | 39 | # Does the topic really exist? |
DuyLionTran | 0:8094b249013c | 40 | git show-ref -q "$topic" || { |
DuyLionTran | 0:8094b249013c | 41 | echo >&2 "No such branch $topic" |
DuyLionTran | 0:8094b249013c | 42 | exit 1 |
DuyLionTran | 0:8094b249013c | 43 | } |
DuyLionTran | 0:8094b249013c | 44 | |
DuyLionTran | 0:8094b249013c | 45 | # Is topic fully merged to master? |
DuyLionTran | 0:8094b249013c | 46 | not_in_master=`git rev-list --pretty=oneline ^master "$topic"` |
DuyLionTran | 0:8094b249013c | 47 | if test -z "$not_in_master" |
DuyLionTran | 0:8094b249013c | 48 | then |
DuyLionTran | 0:8094b249013c | 49 | echo >&2 "$topic is fully merged to master; better remove it." |
DuyLionTran | 0:8094b249013c | 50 | exit 1 ;# we could allow it, but there is no point. |
DuyLionTran | 0:8094b249013c | 51 | fi |
DuyLionTran | 0:8094b249013c | 52 | |
DuyLionTran | 0:8094b249013c | 53 | # Is topic ever merged to next? If so you should not be rebasing it. |
DuyLionTran | 0:8094b249013c | 54 | only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` |
DuyLionTran | 0:8094b249013c | 55 | only_next_2=`git rev-list ^master ${publish} | sort` |
DuyLionTran | 0:8094b249013c | 56 | if test "$only_next_1" = "$only_next_2" |
DuyLionTran | 0:8094b249013c | 57 | then |
DuyLionTran | 0:8094b249013c | 58 | not_in_topic=`git rev-list "^$topic" master` |
DuyLionTran | 0:8094b249013c | 59 | if test -z "$not_in_topic" |
DuyLionTran | 0:8094b249013c | 60 | then |
DuyLionTran | 0:8094b249013c | 61 | echo >&2 "$topic is already up-to-date with master" |
DuyLionTran | 0:8094b249013c | 62 | exit 1 ;# we could allow it, but there is no point. |
DuyLionTran | 0:8094b249013c | 63 | else |
DuyLionTran | 0:8094b249013c | 64 | exit 0 |
DuyLionTran | 0:8094b249013c | 65 | fi |
DuyLionTran | 0:8094b249013c | 66 | else |
DuyLionTran | 0:8094b249013c | 67 | not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` |
DuyLionTran | 0:8094b249013c | 68 | /usr/bin/perl -e ' |
DuyLionTran | 0:8094b249013c | 69 | my $topic = $ARGV[0]; |
DuyLionTran | 0:8094b249013c | 70 | my $msg = "* $topic has commits already merged to public branch:\n"; |
DuyLionTran | 0:8094b249013c | 71 | my (%not_in_next) = map { |
DuyLionTran | 0:8094b249013c | 72 | /^([0-9a-f]+) /; |
DuyLionTran | 0:8094b249013c | 73 | ($1 => 1); |
DuyLionTran | 0:8094b249013c | 74 | } split(/\n/, $ARGV[1]); |
DuyLionTran | 0:8094b249013c | 75 | for my $elem (map { |
DuyLionTran | 0:8094b249013c | 76 | /^([0-9a-f]+) (.*)$/; |
DuyLionTran | 0:8094b249013c | 77 | [$1 => $2]; |
DuyLionTran | 0:8094b249013c | 78 | } split(/\n/, $ARGV[2])) { |
DuyLionTran | 0:8094b249013c | 79 | if (!exists $not_in_next{$elem->[0]}) { |
DuyLionTran | 0:8094b249013c | 80 | if ($msg) { |
DuyLionTran | 0:8094b249013c | 81 | print STDERR $msg; |
DuyLionTran | 0:8094b249013c | 82 | undef $msg; |
DuyLionTran | 0:8094b249013c | 83 | } |
DuyLionTran | 0:8094b249013c | 84 | print STDERR " $elem->[1]\n"; |
DuyLionTran | 0:8094b249013c | 85 | } |
DuyLionTran | 0:8094b249013c | 86 | } |
DuyLionTran | 0:8094b249013c | 87 | ' "$topic" "$not_in_next" "$not_in_master" |
DuyLionTran | 0:8094b249013c | 88 | exit 1 |
DuyLionTran | 0:8094b249013c | 89 | fi |
DuyLionTran | 0:8094b249013c | 90 | |
DuyLionTran | 0:8094b249013c | 91 | <<\DOC_END |
DuyLionTran | 0:8094b249013c | 92 | |
DuyLionTran | 0:8094b249013c | 93 | This sample hook safeguards topic branches that have been |
DuyLionTran | 0:8094b249013c | 94 | published from being rewound. |
DuyLionTran | 0:8094b249013c | 95 | |
DuyLionTran | 0:8094b249013c | 96 | The workflow assumed here is: |
DuyLionTran | 0:8094b249013c | 97 | |
DuyLionTran | 0:8094b249013c | 98 | * Once a topic branch forks from "master", "master" is never |
DuyLionTran | 0:8094b249013c | 99 | merged into it again (either directly or indirectly). |
DuyLionTran | 0:8094b249013c | 100 | |
DuyLionTran | 0:8094b249013c | 101 | * Once a topic branch is fully cooked and merged into "master", |
DuyLionTran | 0:8094b249013c | 102 | it is deleted. If you need to build on top of it to correct |
DuyLionTran | 0:8094b249013c | 103 | earlier mistakes, a new topic branch is created by forking at |
DuyLionTran | 0:8094b249013c | 104 | the tip of the "master". This is not strictly necessary, but |
DuyLionTran | 0:8094b249013c | 105 | it makes it easier to keep your history simple. |
DuyLionTran | 0:8094b249013c | 106 | |
DuyLionTran | 0:8094b249013c | 107 | * Whenever you need to test or publish your changes to topic |
DuyLionTran | 0:8094b249013c | 108 | branches, merge them into "next" branch. |
DuyLionTran | 0:8094b249013c | 109 | |
DuyLionTran | 0:8094b249013c | 110 | The script, being an example, hardcodes the publish branch name |
DuyLionTran | 0:8094b249013c | 111 | to be "next", but it is trivial to make it configurable via |
DuyLionTran | 0:8094b249013c | 112 | $GIT_DIR/config mechanism. |
DuyLionTran | 0:8094b249013c | 113 | |
DuyLionTran | 0:8094b249013c | 114 | With this workflow, you would want to know: |
DuyLionTran | 0:8094b249013c | 115 | |
DuyLionTran | 0:8094b249013c | 116 | (1) ... if a topic branch has ever been merged to "next". Young |
DuyLionTran | 0:8094b249013c | 117 | topic branches can have stupid mistakes you would rather |
DuyLionTran | 0:8094b249013c | 118 | clean up before publishing, and things that have not been |
DuyLionTran | 0:8094b249013c | 119 | merged into other branches can be easily rebased without |
DuyLionTran | 0:8094b249013c | 120 | affecting other people. But once it is published, you would |
DuyLionTran | 0:8094b249013c | 121 | not want to rewind it. |
DuyLionTran | 0:8094b249013c | 122 | |
DuyLionTran | 0:8094b249013c | 123 | (2) ... if a topic branch has been fully merged to "master". |
DuyLionTran | 0:8094b249013c | 124 | Then you can delete it. More importantly, you should not |
DuyLionTran | 0:8094b249013c | 125 | build on top of it -- other people may already want to |
DuyLionTran | 0:8094b249013c | 126 | change things related to the topic as patches against your |
DuyLionTran | 0:8094b249013c | 127 | "master", so if you need further changes, it is better to |
DuyLionTran | 0:8094b249013c | 128 | fork the topic (perhaps with the same name) afresh from the |
DuyLionTran | 0:8094b249013c | 129 | tip of "master". |
DuyLionTran | 0:8094b249013c | 130 | |
DuyLionTran | 0:8094b249013c | 131 | Let's look at this example: |
DuyLionTran | 0:8094b249013c | 132 | |
DuyLionTran | 0:8094b249013c | 133 | o---o---o---o---o---o---o---o---o---o "next" |
DuyLionTran | 0:8094b249013c | 134 | / / / / |
DuyLionTran | 0:8094b249013c | 135 | / a---a---b A / / |
DuyLionTran | 0:8094b249013c | 136 | / / / / |
DuyLionTran | 0:8094b249013c | 137 | / / c---c---c---c B / |
DuyLionTran | 0:8094b249013c | 138 | / / / \ / |
DuyLionTran | 0:8094b249013c | 139 | / / / b---b C \ / |
DuyLionTran | 0:8094b249013c | 140 | / / / / \ / |
DuyLionTran | 0:8094b249013c | 141 | ---o---o---o---o---o---o---o---o---o---o---o "master" |
DuyLionTran | 0:8094b249013c | 142 | |
DuyLionTran | 0:8094b249013c | 143 | |
DuyLionTran | 0:8094b249013c | 144 | A, B and C are topic branches. |
DuyLionTran | 0:8094b249013c | 145 | |
DuyLionTran | 0:8094b249013c | 146 | * A has one fix since it was merged up to "next". |
DuyLionTran | 0:8094b249013c | 147 | |
DuyLionTran | 0:8094b249013c | 148 | * B has finished. It has been fully merged up to "master" and "next", |
DuyLionTran | 0:8094b249013c | 149 | and is ready to be deleted. |
DuyLionTran | 0:8094b249013c | 150 | |
DuyLionTran | 0:8094b249013c | 151 | * C has not merged to "next" at all. |
DuyLionTran | 0:8094b249013c | 152 | |
DuyLionTran | 0:8094b249013c | 153 | We would want to allow C to be rebased, refuse A, and encourage |
DuyLionTran | 0:8094b249013c | 154 | B to be deleted. |
DuyLionTran | 0:8094b249013c | 155 | |
DuyLionTran | 0:8094b249013c | 156 | To compute (1): |
DuyLionTran | 0:8094b249013c | 157 | |
DuyLionTran | 0:8094b249013c | 158 | git rev-list ^master ^topic next |
DuyLionTran | 0:8094b249013c | 159 | git rev-list ^master next |
DuyLionTran | 0:8094b249013c | 160 | |
DuyLionTran | 0:8094b249013c | 161 | if these match, topic has not merged in next at all. |
DuyLionTran | 0:8094b249013c | 162 | |
DuyLionTran | 0:8094b249013c | 163 | To compute (2): |
DuyLionTran | 0:8094b249013c | 164 | |
DuyLionTran | 0:8094b249013c | 165 | git rev-list master..topic |
DuyLionTran | 0:8094b249013c | 166 | |
DuyLionTran | 0:8094b249013c | 167 | if this is empty, it is fully merged to "master". |
DuyLionTran | 0:8094b249013c | 168 | |
DuyLionTran | 0:8094b249013c | 169 | DOC_END |