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