annotate vendor/drupal/coder/.git/hooks/pre-rebase.sample @ 19:fa3358dc1485 tip

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