comparison mercurial/subrepo.py @ 13178:c4d857f5405d

subrepo: backout 519ac79d680b Unfortunately git 1.6 does not support the upstream parameter in for-each-ref.
author Eric Eisner <ede@mit.edu>
date Mon, 20 Dec 2010 13:59:19 -0500
parents be7e8e9bc5e5
children b512a7074349
comparison
equal deleted inserted replaced
13177:04c8f3787546 13178:c4d857f5405d
676 def _gitisancestor(self, r1, r2): 676 def _gitisancestor(self, r1, r2):
677 base = self._gitcommand(['merge-base', r1, r2]) 677 base = self._gitcommand(['merge-base', r1, r2])
678 return base == r1 678 return base == r1
679 679
680 def _gitbranchmap(self): 680 def _gitbranchmap(self):
681 '''returns 3 things: 681 '''returns 2 things:
682 a map from git branch to revision 682 a map from git branch to revision
683 a map from revision to branches 683 a map from revision to branches'''
684 a map from remote branch to local tracking branch'''
685 branch2rev = {} 684 branch2rev = {}
686 rev2branch = {} 685 rev2branch = {}
687 tracking = {} 686
688 out = self._gitcommand(['for-each-ref', '--format', 687 out = self._gitcommand(['for-each-ref', '--format',
689 '%(objectname) %(refname) %(upstream) end']) 688 '%(objectname) %(refname)'])
690 for line in out.split('\n'): 689 for line in out.split('\n'):
691 revision, ref, upstream = line.split(' ')[:3] 690 revision, ref = line.split(' ')
692 if ref.startswith('refs/tags/'): 691 if ref.startswith('refs/tags/'):
693 continue 692 continue
694 if ref.startswith('refs/remotes/') and ref.endswith('/HEAD'): 693 if ref.startswith('refs/remotes/') and ref.endswith('/HEAD'):
695 continue # ignore remote/HEAD redirects 694 continue # ignore remote/HEAD redirects
696 branch2rev[ref] = revision 695 branch2rev[ref] = revision
697 rev2branch.setdefault(revision, []).append(ref) 696 rev2branch.setdefault(revision, []).append(ref)
698 if upstream: 697 return branch2rev, rev2branch
699 # assumes no more than one local tracking branch for a remote 698
700 tracking[upstream] = ref 699 def _gittracking(self, branches):
701 return branch2rev, rev2branch, tracking 700 'return map of remote branch to local tracking branch'
701 # assumes no more than one local tracking branch for each remote
702 tracking = {}
703 for b in branches:
704 if b.startswith('refs/remotes/'):
705 continue
706 remote = self._gitcommand(['config', 'branch.%s.remote' % b])
707 if remote:
708 ref = self._gitcommand(['config', 'branch.%s.merge' % b])
709 tracking['refs/remotes/%s/%s' %
710 (remote, ref.split('/', 2)[2])] = b
711 return tracking
702 712
703 def _fetch(self, source, revision): 713 def _fetch(self, source, revision):
704 if not os.path.exists('%s/.git' % self._path): 714 if not os.path.exists('%s/.git' % self._path):
705 self._ui.status(_('cloning subrepo %s\n') % self._relpath) 715 self._ui.status(_('cloning subrepo %s\n') % self._relpath)
706 self._gitnodir(['clone', source, self._path]) 716 self._gitnodir(['clone', source, self._path])
733 if self._gitstate() == revision: 743 if self._gitstate() == revision:
734 self._gitcommand(['reset', '--hard', 'HEAD']) 744 self._gitcommand(['reset', '--hard', 'HEAD'])
735 return 745 return
736 elif self._gitstate() == revision: 746 elif self._gitstate() == revision:
737 return 747 return
738 branch2rev, rev2branch, tracking = self._gitbranchmap() 748 branch2rev, rev2branch = self._gitbranchmap()
739 749
740 def rawcheckout(): 750 def rawcheckout():
741 # no branch to checkout, check it out with no branch 751 # no branch to checkout, check it out with no branch
742 self._ui.warn(_('checking out detached HEAD in subrepo %s\n') % 752 self._ui.warn(_('checking out detached HEAD in subrepo %s\n') %
743 self._relpath) 753 self._relpath)
759 firstlocalbranch = b 769 firstlocalbranch = b
760 if firstlocalbranch: 770 if firstlocalbranch:
761 self._gitcommand(['checkout', firstlocalbranch]) 771 self._gitcommand(['checkout', firstlocalbranch])
762 return 772 return
763 773
774 tracking = self._gittracking(branch2rev.keys())
764 # choose a remote branch already tracked if possible 775 # choose a remote branch already tracked if possible
765 remote = branches[0] 776 remote = branches[0]
766 if remote not in tracking: 777 if remote not in tracking:
767 for b in branches: 778 for b in branches:
768 if b in tracking: 779 if b in tracking:
811 elif base != self._state[1]: 822 elif base != self._state[1]:
812 self._gitcommand(['merge', '--no-commit', revision]) 823 self._gitcommand(['merge', '--no-commit', revision])
813 824
814 def push(self, force): 825 def push(self, force):
815 # if a branch in origin contains the revision, nothing to do 826 # if a branch in origin contains the revision, nothing to do
816 branch2rev, rev2branch, tracking = self._gitbranchmap() 827 branch2rev, rev2branch = self._gitbranchmap()
817 if self._state[1] in rev2branch: 828 if self._state[1] in rev2branch:
818 for b in rev2branch[self._state[1]]: 829 for b in rev2branch[self._state[1]]:
819 if b.startswith('refs/remotes/origin/'): 830 if b.startswith('refs/remotes/origin/'):
820 return True 831 return True
821 for b, revision in branch2rev.iteritems(): 832 for b, revision in branch2rev.iteritems():