[PATCH] Add --keepbranches option to rebase

Augie Fackler durin42 at gmail.com
Tue Nov 18 22:03:10 CST 2008


# HG changeset patch
# User Augie Fackler <durin42 at gmail.com>
# Date 1227066633 21600
# Node ID 7bfcd13a1c3cfdb038e7f489369a43b4adb8f89b
# Parent  86bfd65532ac4a6fa3568823218b0cede19ce03e
rebase: add support to keep branch names

diff --git a/hgext/rebase.py b/hgext/rebase.py
--- a/hgext/rebase.py
+++ b/hgext/rebase.py
@@ -64,6 +64,7 @@
         contf = opts.get('continue')
         abortf = opts.get('abort')
         collapsef = opts.get('collapse', False)
+        keepbranches = opts.get('keepbranches', False)
         if contf or abortf:
             if contf and abortf:
                 raise dispatch.ParseError('rebase',
@@ -101,14 +102,14 @@
                 storestatus(repo, originalwd, target, state, collapsef,
                                                                  
external)
                 rebasenode(repo, rev, target, state, skipped,  
targetancestors,
-                                                                 
collapsef)
+                                                       collapsef,  
keepbranches)
         ui.note(_('rebase merging completed\n'))

         if collapsef:
             p1, p2 = defineparents(repo, min(state), target,
                                                         state,  
targetancestors)
             concludenode(repo, rev, p1, external, state, collapsef,
-                                                last=True,  
skipped=skipped)
+                         last=True, skipped=skipped,  
keepbranches=keepbranches)

         if 'qtip' in repo.tags():
             updatemq(repo, state, skipped, **opts)
@@ -131,7 +132,8 @@
     finally:
         del lock, wlock

-def concludenode(repo, rev, p1, p2, state, collapse, last=False,  
skipped={}):
+def concludenode(repo, rev, p1, p2, state, collapse, last=False,  
skipped={},
+                 keepbranches=False):
     """Skip commit if collapsing has been required and rev is not the  
last
     revision, commit otherwise
     """
@@ -155,18 +157,22 @@
         else:
             commitmsg = repo[rev].description()
         # Commit might fail if unresolved files exist
+        extra = {'rebase_source': repo[rev].hex()}
+        if keepbranches:
+            extra['branch'] = repo[rev].extra()['branch']
         newrev = repo.commit(m+a+r,
                             text=commitmsg,
                             user=repo[rev].user(),
                             date=repo[rev].date(),
-                            extra={'rebase_source': repo[rev].hex()})
+                            extra=extra)
         return newrev
     except util.Abort:
         # Invalidate the previous setparents
         repo.dirstate.invalidate()
         raise

-def rebasenode(repo, rev, target, state, skipped, targetancestors,  
collapse):
+def rebasenode(repo, rev, target, state, skipped, targetancestors,  
collapse,
+               keepbranches):
     'Rebase a single revision'
     repo.ui.debug(_("rebasing %d:%s\n") % (rev, repo[rev]))

@@ -195,7 +201,8 @@
         repo.ui.debug(_('resuming interrupted rebase\n'))


-    newrev = concludenode(repo, rev, p1, p2, state, collapse)
+    newrev = concludenode(repo, rev, p1, p2, state, collapse,
+                          keepbranches=keepbranches)

     # Update the state
     if newrev is not None:
@@ -409,6 +416,7 @@
         (rebase,
         [
         ('', 'keep', False, _('keep original revisions')),
+        ('', 'keepbranches', False, _('keep original branches')),
         ('s', 'source', '', _('rebase from a given revision')),
         ('b', 'base', '', _('rebase from the base of a given  
revision')),
         ('d', 'dest', '', _('rebase onto a given revision')),
diff --git a/tests/test-rebase-keep-branch b/tests/test-rebase-keep- 
branch
new file mode 100755
--- /dev/null
+++ b/tests/test-rebase-keep-branch
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+echo "[extensions]" >> $HGRCPATH
+echo "graphlog=" >> $HGRCPATH
+echo "rebase=" >> $HGRCPATH
+
+addcommit () {
+    echo $1 > $1
+    hg add $1
+    hg commit -d "${2} 0" -u test -m $1
+}
+
+hg init a
+cd a
+addcommit "c1" 0
+addcommit "c2" 1
+
+addcommit "l1" 2
+addcommit "l2" 3
+
+hg update -C 1
+hg branch 'notdefault'
+addcommit "r1" 4
+hg glog --template '{rev}:{desc}:{branches}\n'
+
+echo
+echo '% Rebase a branch while preserving the branch name'
+hg update -C 3
+hg rebase -b 4 -d 3 --keepbranches 2>&1 | sed 's/\(saving bundle to  
\).*/\1/'
+hg glog --template '{rev}:{desc}:{branches}\n'
diff --git a/tests/test-rebase-keep-branch.out b/tests/test-rebase- 
keep-branch.out
new file mode 100644
--- /dev/null
+++ b/tests/test-rebase-keep-branch.out
@@ -0,0 +1,33 @@
+0 files updated, 0 files merged, 2 files removed, 0 files unresolved
+marked working directory as branch notdefault
+created new head
+@  4:r1:notdefault
+|
+| o  3:l2:
+| |
+| o  2:l1:
+|/
+o  1:c2:
+|
+o  0:c1:
+
+
+% Rebase a branch while preserving the branch name
+2 files updated, 0 files merged, 1 files removed, 0 files unresolved
+saving bundle to
+adding branch
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 1 changes to 1 files
+rebase completed
+@  4:r1:notdefault
+|
+o  3:l2:
+|
+o  2:l1:
+|
+o  1:c2:
+|
+o  0:c1:
+


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20081118/6d3fe1b9/attachment.bin 


More information about the Mercurial-devel mailing list