comparison hggit/git_handler.py @ 441:35e2813f58a5

- add "branch_bookmark_names" parameter. this allows bookmarks that mimic a branchname to be maintained on the git side without a particular suffix - e.g. if the hg repo had a branch "release_05", and a bookmark created onto it "release_05_bookmark", the branch on the git side would be named "release_05". When pulling branches back from git, if an hg named branch of that name exists, the suffix is appended back onto the name before creating a bookmark on the hg side. This is strictly so that a git repo can be generated that has the same "branch names" as an older hg repo that has named branches, and has had bookmarks added in to mirror the branch names. This is given the restrictions that A. hg named branches can never be renamed and B. hg-git only supports hg bookmarks, not branches
author Mike Bayer <mike_mp@zzzcomputing.com>
date Sun, 18 Dec 2011 18:54:16 -0500
parents 3f45c88100e8
children 553dd7078058
comparison
equal deleted inserted replaced
440:32afa497834d 441:35e2813f58a5
79 self.gitdir = self.repo.wjoin('.git') 79 self.gitdir = self.repo.wjoin('.git')
80 else: 80 else:
81 self.gitdir = self.repo.join('git') 81 self.gitdir = self.repo.join('git')
82 82
83 self.paths = ui.configitems('paths') 83 self.paths = ui.configitems('paths')
84
85 self.branch_bookmark_suffix = ui.config('git', 'branch_bookmark_suffix')
84 86
85 self.load_map() 87 self.load_map()
86 self.load_tags() 88 self.load_tags()
87 89
88 # make the git data directory 90 # make the git data directory
719 new_refs['refs/heads/master'] = self.map_git_get(tip) 721 new_refs['refs/heads/master'] = self.map_git_get(tip)
720 722
721 for rev in revs: 723 for rev in revs:
722 ctx = self.repo[rev] 724 ctx = self.repo[rev]
723 if getattr(ctx, 'bookmarks', None): 725 if getattr(ctx, 'bookmarks', None):
724 labels = lambda c: ctx.tags() + ctx.bookmarks() 726 labels = lambda c: ctx.tags() + [
727 fltr for fltr, bm
728 in self._filter_for_bookmarks(ctx.bookmarks())
729 ]
725 else: 730 else:
726 labels = lambda c: ctx.tags() 731 labels = lambda c: ctx.tags()
727 prep = lambda itr: [i.replace(' ', '_') for i in itr] 732 prep = lambda itr: [i.replace(' ', '_') for i in itr]
728 733
729 heads = [t for t in prep(labels(ctx)) if t in self.local_heads()] 734 heads = [t for t in prep(labels(ctx)) if t in self.local_heads()]
837 if self.repo.tagtype(tag) in ('global', 'git'): 842 if self.repo.tagtype(tag) in ('global', 'git'):
838 tag = tag.replace(' ', '_') 843 tag = tag.replace(' ', '_')
839 self.git.refs['refs/tags/' + tag] = self.map_git_get(hex(sha)) 844 self.git.refs['refs/tags/' + tag] = self.map_git_get(hex(sha))
840 self.tags[tag] = hex(sha) 845 self.tags[tag] = hex(sha)
841 846
847 def _filter_for_bookmarks(self, bms):
848 if not self.branch_bookmark_suffix:
849 return [(bm, bm) for bm in bms]
850 else:
851 def _filter_bm(bm):
852 if bm.endswith(self.branch_bookmark_suffix):
853 return bm[0:-(len(self.branch_bookmark_suffix))]
854 else:
855 return bm
856 return [(_filter_bm(bm), bm) for bm in bms]
857
842 def local_heads(self): 858 def local_heads(self):
843 try: 859 try:
844 if getattr(bookmarks, 'parse', None): 860 if getattr(bookmarks, 'parse', None):
845 bms = bookmarks.parse(self.repo) 861 bms = bookmarks.parse(self.repo)
846 else: 862 else:
847 bms = self.repo._bookmarks 863 bms = self.repo._bookmarks
848 return dict([(bm, hex(bms[bm])) for bm in bms]) 864 return dict([(filtered_bm, hex(bms[bm])) for
865 filtered_bm, bm in self._filter_for_bookmarks(bms)])
849 except AttributeError: #pragma: no cover 866 except AttributeError: #pragma: no cover
850 return {} 867 return {}
851 868
852 def import_tags(self, refs): 869 def import_tags(self, refs):
853 keys = refs.keys() 870 keys = refs.keys()
884 oldbm = getattr(bookmarks, 'parse', None) 901 oldbm = getattr(bookmarks, 'parse', None)
885 if oldbm: 902 if oldbm:
886 bms = bookmarks.parse(self.repo) 903 bms = bookmarks.parse(self.repo)
887 else: 904 else:
888 bms = self.repo._bookmarks 905 bms = self.repo._bookmarks
906
889 heads = dict([(ref[11:],refs[ref]) for ref in refs 907 heads = dict([(ref[11:],refs[ref]) for ref in refs
890 if ref.startswith('refs/heads/')]) 908 if ref.startswith('refs/heads/')])
891 909
892 for head, sha in heads.iteritems(): 910 for head, sha in heads.iteritems():
893 # refs contains all the refs in the server, not just 911 # refs contains all the refs in the server, not just
901 else: 919 else:
902 bm = self.repo[bms[head]] 920 bm = self.repo[bms[head]]
903 if bm.ancestor(self.repo[hgsha]) == bm: 921 if bm.ancestor(self.repo[hgsha]) == bm:
904 # fast forward 922 # fast forward
905 bms[head] = hgsha 923 bms[head] = hgsha
924
925 # if there's a branch bookmark suffix,
926 # then add it on to all bookmark names
927 # that would otherwise conflict with a branch
928 # name
929 if self.branch_bookmark_suffix:
930 real_branch_names = self.repo.branchmap()
931 bms = dict(
932 (
933 bm_name + self.branch_bookmark_suffix
934 if bm_name in real_branch_names
935 else bm_name,
936 bms[bm_name]
937 )
938 for bm_name in bms
939 )
906 if heads: 940 if heads:
907 if oldbm: 941 if oldbm:
908 bookmarks.write(self.repo, bms) 942 bookmarks.write(self.repo, bms)
909 else: 943 else:
910 self.repo._bookmarks = bms 944 self.repo._bookmarks = bms