Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.21 KB | None | 0 0
  1. #! /usr/bin/zsh -f
  2.  
  3. branch=$1
  4. tag='old|'$branch
  5.  
  6.  
  7. tag_exists()
  8. {
  9.     tag=$1
  10.     git show --oneline $tag &> /dev/null;
  11. }
  12.  
  13.  
  14. remake_merge()
  15. {
  16.     merge=$1
  17.     # see its parents (either TAGS or branches)
  18.     # for TAGS map to updated branch
  19.     # 1/ leave a tag
  20.     oldtag="old|$merge"
  21.     git co $merge
  22.     tags=$(merge_parents $merge )
  23.  
  24.     # echo $tags
  25.     typeset -a refs
  26.     foreach t ($tags)
  27.     {
  28.     if  git tag |grep -F $t >/dev/null;
  29.     then
  30.         # echo $t "is a TAG ..."
  31.         refs[$(expr $#refs + 1)]=$(echo ${t##old|})
  32.     elif git br |grep -F $t >/dev/null;
  33.     then
  34.         # echo $t "is a branch...ok"
  35.         refs[$(expr $#refs + 1)]=$t
  36.     else
  37.         echo $t  "is UNKNOWN" >&2
  38.     fi
  39.     }
  40.     echo "MERGE $merge: " $refs
  41.     echo " (press y)"
  42.     read pause
  43.     if [ "$pause" = "y" ];
  44.     then
  45.     ~/start_rewriting $merge
  46.     git reset --hard $refs[1]
  47.     git merge $refs[2,-1]
  48.     ~/post_rewriting $merge
  49.     fi
  50.     # if all ok
  51. }
  52.  
  53.  
  54. # Find direct descendants!
  55. parents()
  56. {
  57.     commit=$1
  58.     # skip the 1:
  59.     git rev-list --no-walk  --parents $commit | tail -c +42
  60. }
  61.  
  62. is_not_merge ()
  63. {
  64.     commit=$1
  65.     count=$(git rev-list --no-walk  --parents $commit |wc --w)
  66.     test $count = 2
  67. }
  68.  
  69. # is a merge of:
  70. # one of parents == .
  71. direct()
  72. {
  73.     from=$1
  74.     to=$2
  75.     between=$(git log --pretty=format:%d $from..$to |grep -v '^$' |wc -l)
  76.     test $between = 1;
  77. }
  78.  
  79. direct_merge()
  80. {
  81.     hash=$(git rev-list --no-walk $1)
  82.     git rev-list --no-walk  --parents $commit |grep $hash > /dev/null
  83.  
  84.     # foreach parent_of $merge
  85.     # {
  86.     #   # if it is one of the parents!
  87.     #   if direct $from $parent;
  88.     #   then
  89.     #       return 0;
  90.     #   fi
  91.     # }
  92.     # false
  93. }
  94.  
  95.  
  96. #describe merge (in terms of tags & refs):
  97. # given a hash: its ref:
  98. merge_parents()
  99. {
  100.     hashes=$(parents $1)
  101. # git rev-list --pretty=format:%d --no-walk b40683d467a9dc85a076ec10d418e11e1eac79e6
  102.     foreach hash ($hashes)
  103.     {
  104.     git rev-list --pretty=format:%d --no-walk $hash |\
  105.           tail -n +2|sed -e 's/^ (//;s/)$//'
  106.     # some are TAG REF ...
  107.     # tag?  git tag |grep -F
  108.     # tag -> ref  strip old|
  109.     #
  110.     }
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. # tag exists?
  118. # git tag -l
  119. if tag_exists $tag;
  120. then
  121.     echo "getting all branches down from tag $tag"
  122.  
  123.     echo "the tag is on commit:"
  124.     git log --pretty=format:%H  $tag |head -n 1
  125.  
  126.     dbranches=$(git branch --no-color --contains $tag|sed -e "s/^\*\? \+//")
  127.     setopt SH_WORD_SPLIT
  128.  
  129.     echo "need to rebase: " $dbranches
  130.     echo '====>'
  131.     foreach br ($dbranches)
  132.     {
  133.     if is_not_merge $br;
  134.     then
  135.         if direct $tag $br;
  136.         then
  137.         echo "REBASE " $br " (press y)"
  138.         read pause
  139.         if [ "$pause" = "y" ];
  140.         then
  141.             echo rebasing $br
  142.             ~/start_rewriting $br
  143.             git checkout $br;
  144.             echo git rebase --onto $branch $tag
  145.             git rebase --onto $branch $tag
  146.             ~/post_rewriting $br
  147.         fi
  148.         else
  149.         echo $br " is a NOT DIRECT descendant!"
  150.         fi
  151.     else
  152.         if direct_merge $tag $br;
  153.         then
  154.         echo "we should remake the MERGE $br"
  155.         remake_merge $br
  156.         # merge_parents $br;
  157.         else
  158.         echo $br " is not direct descendant"
  159.         fi
  160.     fi
  161.     }
  162.  
  163.     git tag -d $tag
  164. else
  165.     echo "Tag $tag does not exist. no need to rebase descendants"
  166. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement