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