def reposetup(ui, repo, **kwargs): """ Automatically adds Bitbucket->GitHub mirror paths to the repo. Also creates a `master` bookmark for the `default` branch. """ if len(getattr(repo, "changelog", [])) == 0: return hggit_reposetup(ui, repo, **kwargs) bb = "ssh://[email protected]/" for pathname, path in ui.configitems("paths"): if path.startswith(bb): user, project = path.replace(bb, "").split("/", 1) # Strip slash and everything after it, # such as mq patch queue path. project = project.split("/")[0] for k, v in ui.configitems("github"): if k == "username": user = v gh_path = "git+ssh://[email protected]/%s/%s.git" % (user, project) if pathname == "default": if "master" not in repo._bookmarks: from mercurial.commands import bookmark bookmark(ui, repo, "master", rev="default") gh_pathname = "github" else: gh_pathname = "github-" + pathname ui.setconfig("paths", gh_pathname, gh_path)
def post_push(ui, repo, pats, opts, *args, **kwargs): """ Push to Github after pushing to BitBucket """ dest = pats and pats[0] dest = ui.expandpath(dest or 'default-push', dest or 'default') if 'bitbucket.org' in dest: dest = dest.rstrip('/') reponame = dest.split('/')[-1] username = dest.split('/')[-2] github = 'git+ssh://[email protected]/{username}/{reponame}.git' github = github.format(username=username, reponame=reponame) # Move "master" bookmark to point to tip commands.bookmark(ui, repo, mark='master', rev='tip', force=True) return commands.push(ui, repo, github, **opts)
def check(dir, only): mercurial_ui = ui.ui() for layer1 in os.listdir(dir): # Public, private if only and layer1 != only: continue inlayer1full = os.path.join(dir, layer1) for layer2 in os.listdir(inlayer1full): # Repo names inlayer2full = os.path.join(inlayer1full, layer2) for layer3 in os.listdir(inlayer2full): # Repo artifacts (repo, wiki etc) inlayer3full = os.path.join(inlayer2full, layer3) if layer3 != "hg" or os.path.isfile(inlayer3full): continue repo = hg.repository(mercurial_ui, inlayer3full.encode('utf-8')) print("-----------\nrepo " + layer2 + "\n") commands.summary(mercurial_ui, repo) commands.bookmark(mercurial_ui, repo) print("-- Heads --") commands.heads(mercurial_ui, repo) print("")
def bookmark(self, wire, bookmark, revision=None): repo = self._factory.repo(wire) baseui = self._factory._create_config(wire['config']) commands.bookmark(baseui, repo, bookmark, rev=revision, force=True)
def get_changed_refs(self, refs, revs, force): new_refs = refs.copy() #The remote repo is empty and the local one doesn't have bookmarks/tags if refs.keys()[0] == 'capabilities^{}': del new_refs['capabilities^{}'] if not self.local_heads(): tip = hex(self.repo.lookup('tip')) try: commands.bookmark(self.ui, self.repo, 'master', tip, force=True) except NameError: bookmarks.bookmark(self.ui, self.repo, 'master', tip, force=True) bookmarks.setcurrent(self.repo, 'master') new_refs['refs/heads/master'] = self.map_git_get(tip) for rev in revs: ctx = self.repo[rev] if getattr(ctx, 'bookmarks', None): labels = lambda c: ctx.tags() + ctx.bookmarks() else: labels = lambda c: ctx.tags() prep = lambda itr: [i.replace(' ', '_') for i in itr] heads = [t for t in prep(labels(ctx)) if t in self.local_heads()] tags = [t for t in prep(labels(ctx)) if t in self.tags] if not (heads or tags): raise hgutil.Abort("revision %s cannot be pushed since" " it doesn't have a ref" % ctx) # Check if the tags the server is advertising are annotated tags, # by attempting to retrieve it from the our git repo, and building a # list of these tags. # # This is possible, even though (currently) annotated tags are # dereferenced and stored as lightweight ones, as the annotated tag # is still stored in the git repo. uptodate_annotated_tags = [] for r in tags: ref = 'refs/tags/'+r # Check tag. if not ref in refs: continue try: # We're not using Repo.tag(), as it's deprecated. tag = self.git.get_object(refs[ref]) if not isinstance(tag, Tag): continue except KeyError: continue # If we've reached here, the tag's good. uptodate_annotated_tags.append(ref) for r in heads + tags: if r in heads: ref = 'refs/heads/'+r else: ref = 'refs/tags/'+r if ref not in refs: new_refs[ref] = self.map_git_get(ctx.hex()) elif new_refs[ref] in self._map_git: rctx = self.repo[self.map_hg_get(new_refs[ref])] if rctx.ancestor(ctx) == rctx or force: new_refs[ref] = self.map_git_get(ctx.hex()) else: raise hgutil.Abort("pushing %s overwrites %s" % (ref, ctx)) elif ref in uptodate_annotated_tags: # we already have the annotated tag. pass else: raise hgutil.Abort("%s changed on the server, please pull " "and merge before pushing" % ref) return new_refs
def hg_deactivate_bookmark(): commands.bookmark(_ui, get_sandbox_repo(), inactive=True)
def hg_bookmark(bookmark='test'): commands.bookmark(_ui, get_sandbox_repo(), bookmark)
def subpull(ui, repo, name='', **opts): """Pull subtree(s)""" # change to root directory if repo.getcwd() != '': ui.warn( "Working directory is not repository root. At best, this directory won't exist when subpull is done.\n" ) repo.dirstate._cwd = repo.root os.chdir(repo.root) # if there are uncommitted change, abort --- we will be modifying the working copy quite drammatically modified, added, removed, deleted, _unknown, _ignored, _clean = repo.status( ) if modified or added or removed or deleted: raise error.Abort( "Uncommitted changes in the working copy. Subtree extension needs to modify the working copy, so it cannot proceed." ) # parse .hgsubtree hgsubtree = ui.config('subtree', 'hgsubtree', default=default_hgsubtree) subtrees = _parse_hgsubtree(os.path.join(repo.root, hgsubtree)) # if names not in .hgsubtree, abort # if names is empty, go through all repos in .hgsubtree if name: if name not in subtrees: raise error.Abort("Cannot find %s in %s." % (name, hgsubtree)) names = [name] else: if opts['source']: raise error.Abort( "Cannot use --source without specifying a repository") names = subtrees.keys() null = repo['null'] origin = str(repo[None]) commit_opts = {'edit': opts['edit']} bookmark_prefix = ui.config('subtree', 'bookmark', default=default_bookmark_prefix) nocache = ui.config('subtree', 'nocache', default='') for name in names: subtree = subtrees[name] if 'destination' not in subtree: raise error.Abort('No destination found for %s' % name) collapse = 'collapse' in subtree and subtree['collapse'] # figure out the revision to pull pull_opts = {} if 'rev' in subtree: pull_opts['rev'] = [subtree['rev']] if opts['rev']: pull_opts['rev'] = opts['rev'] # clone or pull into cache source = subtree['source'] if not opts['source'] else opts['source'] if not nocache: source = _clone_or_pull(ui, repo, name, source, pull_opts) # pull tip = repo['tip'] commands.pull(ui, repo, source=source, force=True, **pull_opts) if tip == repo['tip']: ui.status("no changes: nothing for subtree to do\n") continue # collapse or update -C if collapse: # find a matching bookmark bookmark_name = bookmark_prefix + name if bookmark_name in repo._bookmarks: commands.update(ui, repo, bookmark_name, clean=True) else: commands.update(ui, repo, 'null', clean=True) # set up the correct file state and commit as a new changeset pulled_tip = repo['tip'] commands.revert(ui, repo, rev='tip', all=True) hgsubrepo_meta = [ os.path.join(repo.root, '.hgsubstate'), os.path.join(repo.root, '.hgsub') ] for fn in hgsubrepo_meta: if os.path.exists(fn): ui.debug("removing %s\n" % fn) commands.remove(ui, repo, fn, force=True) os.remove(fn) changed = commands.commit(ui, repo, message=ui.config( 'subtree', 'collapse', default_collapse_comment).format( name=name, rev=str(pulled_tip)[:12]), **commit_opts) commands.bookmark(ui, repo, bookmark_name, inactive=True) if not opts['no_strip']: # delete bookmarks on the changesets that will be stripped; not # the most efficient procedure to find them, but will do for now remove_bookmarks = [] for k in repo._bookmarks.keys(): ctx = repo[k] if pulled_tip.ancestor(ctx) != null: remove_bookmarks.append(k) for bookmark in remove_bookmarks: commands.bookmark(ui, repo, bookmark, delete=True) strip.stripcmd(ui, repo, rev=['ancestors(%s)' % str(pulled_tip)], bookmark=[]) if changed == 1: # nothing changed ui.status("no changes: nothing for subtree to do\n") commands.update(ui, repo, origin[:12]) continue else: commands.update(ui, repo, 'tip', clean=True) # move or delete destinations = _destinations(subtree['destination']) # process destinations keep_list = [] for dest in destinations: if dest[0] == 'mkdir': if not os.path.exists(dest[1]): os.makedirs(dest[1]) elif dest[0] == 'mv': commands.rename(ui, repo, *dest[1:], force=False) elif dest[0] == 'cp': commands.copy(ui, repo, *dest[1:], force=False) elif dest[0] == 'rm': commands.remove(ui, repo, *dest[1:], force=False) elif dest[0] == 'keep': keep_list.append(' '.join(dest[1:])) # remove all untouched files, unless instructed to keep them if 'keep' not in subtree or not subtree['keep']: _modified, _added, _removed, _deleted, _unknown, _ignored, clean = repo.status( clean=True) for fn in clean: for keep_pattern in keep_list: if fnmatch(fn, keep_pattern): break else: commands.remove(ui, repo, fn) commands.commit( ui, repo, message=ui.config('subtree', 'move', default_move_comment).format(name=name), **commit_opts) merge_commit = str(repo[None]) # update to original and merge with the new commands.update(ui, repo, origin[:12]) commands.merge(ui, repo, merge_commit[:12]) commands.commit( ui, repo, message=ui.config('subtree', 'merge', default_merge_comment).format(name=name), **commit_opts) origin = str(repo[None])