##// END OF EJS Templates
subrepo: update and merge works with any git branch
Eric Eisner -
r12995:d90fc91c default
parent child Browse files
Show More
@@ -634,6 +634,26 b' class gitsubrepo(object):'
634 634 out, code = self._gitdir(['cat-file', '-e', revision])
635 635 return code == 0
636 636
637 def _gitbranchmap(self):
638 'returns the current branch and a map from git revision to branch[es]'
639 bm = {}
640 redirects = {}
641 current = None
642 out = self._gitcommand(['branch', '-a', '--no-color',
643 '--verbose', '--abbrev=40'])
644 for line in out.split('\n'):
645 if not line:
646 continue
647 if line[2:].startswith('(no branch)'):
648 continue
649 branch, revision = line[2:].split()[:2]
650 if revision == '->':
651 continue # ignore remote/HEAD redirects
652 if line[0] == '*':
653 current = branch
654 bm.setdefault(revision, []).append(branch)
655 return current, bm
656
637 657 def _fetch(self, source, revision):
638 658 if not os.path.exists('%s/.git' % self._path):
639 659 self._ui.status(_('cloning subrepo %s\n') % self._relpath)
@@ -658,12 +678,32 b' class gitsubrepo(object):'
658 678 def get(self, state):
659 679 source, revision, kind = state
660 680 self._fetch(source, revision)
661 if self._gitstate() != revision:
681 if self._gitstate() == revision:
682 return
683 current, bm = self._gitbranchmap()
684 if revision not in bm:
685 # no branch to checkout, check it out with no branch
662 686 self._ui.warn(_('checking out detached HEAD in subrepo %s\n') %
663 687 self._relpath)
664 688 self._ui.warn(_('check out a git branch if you intend '
665 689 'to make changes\n'))
666 690 self._gitcommand(['checkout', '-q', revision])
691 return
692 branches = bm[revision]
693 firstlocalbranch = None
694 for b in branches:
695 if b == 'master':
696 # master trumps all other branches
697 self._gitcommand(['checkout', 'master'])
698 return
699 if not firstlocalbranch and not b.startswith('remotes/'):
700 firstlocalbranch = b
701 if firstlocalbranch:
702 self._gitcommand(['checkout', firstlocalbranch])
703 else:
704 remote = branches[0]
705 local = remote.split('/')[-1]
706 self._gitcommand(['checkout', '-b', local, remote])
667 707
668 708 def commit(self, text, user, date):
669 709 cmd = ['commit', '-a', '-m', text]
@@ -692,10 +732,15 b' class gitsubrepo(object):'
692 732 cmd = ['push']
693 733 if force:
694 734 cmd.append('--force')
695 # as subrepos have no notion of "where to push to" we
696 # assume origin master. This is git's default
697 self._gitcommand(cmd + ['origin', 'master', '-q'])
698 return True
735 # push the currently checked out branch
736 current, bm = self._gitbranchmap()
737 if current:
738 self._gitcommand(cmd + ['origin', current, '-q'])
739 return True
740 else:
741 self._ui.warn(_('no branch checked out in subrepo %s\n'
742 'nothing to push') % self._relpath)
743 return False
699 744
700 745 types = {
701 746 'hg': hgsubrepo,
@@ -40,14 +40,19 b' add subrepo clone'
40 40 source ../gitroot
41 41 revision da5f5b1d8ffcf62fb8327bcd3c89a4367a6018e7
42 42
43 record a new commit from upstream
43 record a new commit from upstream from a different branch
44 44
45 45 $ cd ../gitroot
46 $ git checkout -b testing
47 Switched to a new branch 'testing'
46 48 $ echo gg >> g
47 49 $ git commit -q -a -m gg
48 50
49 51 $ cd ../t/s
50 52 $ git pull -q
53 $ git checkout -b testing origin/testing
54 Switched to a new branch 'testing'
55 Branch testing set up to track remote branch testing from origin.
51 56
52 57 $ cd ..
53 58 $ hg commit -m 'update git subrepo'
@@ -72,8 +77,7 b' clone root'
72 77 update to previous substate
73 78
74 79 $ hg update 1
75 checking out detached HEAD in subrepo s
76 check out a git branch if you intend to make changes
80 Switched to a new branch 'master'
77 81 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
78 82 $ cat s/g
79 83 g
General Comments 0
You need to be logged in to leave comments. Login now