def _createBranch(self, branch_name, message, from_branch = None): if not from_branch is None: #first update to from_branch commands.update(self.ui, self.repo, from_branch) commands.branch(self.ui, self.repo, branch_name) commands.commit(self.ui, self.repo, message = message)
def close(ui, repo, branch=None, **opts): '''close a branch without updating to the branch Takes a branch and closes it with a commit message. This is extremely helpful in large repositories, where updating to different branches can take seconds. ''' # check repository if not repo: raise util.Abort( _("there is no Mercurial repository here " "(.hg not found)")) # check for a branch if not branch: raise util.Abort('no branch provided') # get message m = opts.get('message') # get current context originalctx = repo[None] # the current working directory might have two parents (merge scenario) # check for a single parent and then pick correct parent if len(originalctx.parents()) != 1: raise util.Abort('current directory has an outstanding merge') originalctx = originalctx.parents()[0] # get branch change context branchctx = scmutil.revsingle(repo, branch) # move to other node and branch commands.debugsetparents(ui, repo, branchctx.rev()) old_quiet = ui.quiet ui.quiet = True commands.branch(ui, repo, label=branchctx.branch()) ui.quiet = old_quiet # commit close node commands.commit(ui, repo, close_branch=True, message=m, exclude="*") # switch back to original commands.debugsetparents(ui, repo, originalctx.rev()) old_quiet = ui.quiet ui.quiet = True commands.branch(ui, repo, label=originalctx.branch()) ui.quiet = old_quiet
def mkpatch(ui, repo, *pats, **opts): """Saves the current patch to a file called <patch_name>-<repo_name>.patch in your patch directory (defaults to ~/patches) """ repo_name = os.path.basename(ui.config('paths', 'default')) if opts.get('patchdir'): patch_dir = opts.get('patchdir') del opts['patchdir'] else: patch_dir = os.path.expanduser(ui.config('mkpatch', 'patchdir', "~/patches")) if not os.path.exists(patch_dir): os.makedirs(patch_dir) elif not os.path.isdir(patch_dir): raise util.Abort("%s is not a directory" % patch_dir) ui.pushbuffer() mq.top(ui, repo) patch_name = ui.popbuffer().strip() if patch_name == 'no patches applied': # Try pdiff ui.pushbuffer() commands.branch(ui, repo) patch_name = ui.popbuffer().strip() ui.pushbuffer() commands.table['^pdiff'][0](ui, repo, *pats, **opts) else: ui.pushbuffer() mq.diff(ui, repo, *pats, **opts) patch_data = ui.popbuffer() patch_hash = hashlib.new('sha1', patch_data).digest() full_name = os.path.join(patch_dir, "%s-%s.patch" % (patch_name, repo_name)) i = 0 while os.path.exists(full_name): file_hash = hashlib.new('sha1', open(full_name).read()).digest() if file_hash == patch_hash: ui.status("Patch is identical to ", full_name, "; not saving\n") return full_name = os.path.join(patch_dir, "%s-%s.patch.%i" % (patch_name, repo_name, i)) i += 1 open(full_name, "w").write(patch_data) ui.status("Patch saved to ", full_name, "\n")
def test_branch(self): ''' Test 'clone --branch' ''' ui = self.ui() _dispatch(ui, ['init', self.wc_path]) repo = self.repo repo.ui.setconfig('ui', 'username', 'anonymous') fpath = os.path.join(self.wc_path, 'it') f = file(fpath, 'w') f.write('C1') f.flush() commands.add(ui, repo) commands.branch(ui, repo, label="B1") commands.commit(ui, repo, message="C1") f.write('C2') f.flush() commands.branch(ui, repo, label="default") commands.commit(ui, repo, message="C2") f.write('C3') f.flush() commands.branch(ui, repo, label="B2") commands.commit(ui, repo, message="C3") self.assertEqual(len(repo), 3) branch = 'B1' _dispatch(ui, ['clone', self.wc_path, self.wc_path + '2', '--branch', branch]) repo2 = hg.repository(ui, self.wc_path + '2') self.assertEqual(repo[branch].hex(), repo2['.'].hex())
def topen(ui, repo, *args, **opts): """ open (create) a new topic branch """ mustBeTopicRepo(repo) # Check the arguments if 'tmenu' in opts: resp = ui.prompt("Name for new branch:", None) if not resp: return 1 args = [resp] elif len(args) < 1: ui.warn("Error: You must specify a name for the new branch.\n") return 1 # Pull new changes from the central repo if not opts.get('nopull', False): if tryCommand(ui, "pull", lambda:commands.pull(ui, repo, **opts) >= 2): return 1 # Validate the name target = args[0] if target in topicBranchNames(repo, closed=True) + [repo.topicProdBranch]: ui.warn("Error: a branch with that name already exists; try choosing a different name.\n") return 1 # Make sure we're at the top of the prod branch if repo.dirstate.parents()[0] not in repo.branchheads('prod') or \ repo.dirstate.branch() != repo.topicProdBranch: if tryCommand(ui, "update %s" % quoteBranch(repo.topicProdBranch), \ lambda:commands.update(ui, repo, node=repo.topicProdBranch, check=True)): return 1 # Create the new branch and commit it. if tryCommand(ui, 'branch %s' % target, lambda:commands.branch(ui, repo, target)): return 1 text = "Opening branch %s" % quoteBranch(target) return tryCommand(ui, "commit", lambda:repo.commit(text) is None)
def test_branch(self): ''' Test 'clone --branch' ''' ui = self.ui() _dispatch(ui, ['init', '--quiet', self.wc_path]) repo = self.repo repo.ui.setconfig('ui', 'username', 'anonymous') fpath = os.path.join(self.wc_path, 'it') f = file(fpath, 'w') f.write('C1') f.flush() commands.add(ui, repo) commands.branch(ui, repo, label="B1") commands.commit(ui, repo, message="C1") f.write('C2') f.flush() commands.branch(ui, repo, label="default") commands.commit(ui, repo, message="C2") f.write('C3') f.flush() commands.branch(ui, repo, label="B2") commands.commit(ui, repo, message="C3") self.assertEqual(test_util.repolen(repo), 3) branch = 'B1' _dispatch(ui, [ 'clone', '--quiet', self.wc_path, self.wc_path + '2', '--branch', branch ]) repo2 = hg.repository(ui, self.wc_path + '2') self.assertEqual( revsymbol(repo, branch).hex(), revsymbol(repo2, '.').hex())
def demo(ui, repo, *args, **opts): '''print [keywordmaps] configuration and an expansion example Show current, custom, or default keyword template maps and their expansions. Extend the current configuration by specifying maps as arguments and using -f/--rcfile to source an external hgrc file. Use -d/--default to disable current configuration. See "hg help templates" for information on templates and filters. ''' def demoitems(section, items): ui.write('[%s]\n' % section) for k, v in sorted(items): ui.write('%s = %s\n' % (k, v)) msg = 'hg keyword config and expansion example' fn = 'demo.txt' branchname = 'demobranch' tmpdir = tempfile.mkdtemp('', 'kwdemo.') ui.note(_('creating temporary repository at %s\n') % tmpdir) repo = localrepo.localrepository(ui, tmpdir, True) ui.setconfig('keyword', fn, '') uikwmaps = ui.configitems('keywordmaps') if args or opts.get('rcfile'): ui.status(_('\n\tconfiguration using custom keyword template maps\n')) if uikwmaps: ui.status(_('\textending current template maps\n')) if opts.get('default') or not uikwmaps: ui.status(_('\toverriding default template maps\n')) if opts.get('rcfile'): ui.readconfig(opts.get('rcfile')) if args: # simulate hgrc parsing rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args] fp = repo.opener('hgrc', 'w') fp.writelines(rcmaps) fp.close() ui.readconfig(repo.join('hgrc')) kwmaps = dict(ui.configitems('keywordmaps')) elif opts.get('default'): ui.status(_('\n\tconfiguration using default keyword template maps\n')) kwmaps = kwtemplater.templates if uikwmaps: ui.status(_('\tdisabling current template maps\n')) for k, v in kwmaps.iteritems(): ui.setconfig('keywordmaps', k, v) else: ui.status(_('\n\tconfiguration using current keyword template maps\n')) kwmaps = dict(uikwmaps) or kwtemplater.templates uisetup(ui) reposetup(ui, repo) for k, v in ui.configitems('extensions'): if k.endswith('keyword'): extension = '%s = %s' % (k, v) break ui.write('[extensions]\n%s\n' % extension) demoitems('keyword', ui.configitems('keyword')) demoitems('keywordmaps', kwmaps.iteritems()) keywords = '$' + '$\n$'.join(sorted(kwmaps.keys())) + '$\n' repo.wopener(fn, 'w').write(keywords) repo.add([fn]) path = repo.wjoin(fn) ui.note(_('\nkeywords written to %s:\n') % path) ui.note(keywords) ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname)) # silence branch command if not verbose quiet = ui.quiet ui.quiet = not ui.verbose commands.branch(ui, repo, branchname) ui.quiet = quiet for name, cmd in ui.configitems('hooks'): if name.split('.', 1)[0].find('commit') > -1: repo.ui.setconfig('hooks', name, '') ui.note(_('unhooked all commit hooks\n')) ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg)) repo.commit(text=msg) ui.status(_('\n\tkeywords expanded\n')) ui.write(repo.wread(fn)) ui.debug('\nremoving temporary repository %s\n' % tmpdir) shutil.rmtree(tmpdir, ignore_errors=True)
def hg_branch(branch='test'): commands.branch(_ui, get_sandbox_repo(), branch)
def create_branch(self, branchName): commands.branch(ui.ui(), self.repo, branchName)
def demo(ui, repo, *args, **opts): '''print [keywordmaps] configuration and an expansion example Show current, custom, or default keyword template maps and their expansions. Extend the current configuration by specifying maps as arguments and using -f/--rcfile to source an external hgrc file. Use -d/--default to disable current configuration. See "hg help templates" for information on templates and filters. ''' def demoitems(section, items): ui.write('[%s]\n' % section) for k, v in sorted(items): ui.write('%s = %s\n' % (k, v)) fn = 'demo.txt' branchname = 'demobranch' tmpdir = tempfile.mkdtemp('', 'kwdemo.') ui.note(_('creating temporary repository at %s\n') % tmpdir) repo = localrepo.localrepository(ui, tmpdir, True) ui.setconfig('keyword', fn, '') uikwmaps = ui.configitems('keywordmaps') if args or opts.get('rcfile'): ui.status(_('\n\tconfiguration using custom keyword template maps\n')) if uikwmaps: ui.status(_('\textending current template maps\n')) if opts.get('default') or not uikwmaps: ui.status(_('\toverriding default template maps\n')) if opts.get('rcfile'): ui.readconfig(opts.get('rcfile')) if args: # simulate hgrc parsing rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args] fp = repo.opener('hgrc', 'w') fp.writelines(rcmaps) fp.close() ui.readconfig(repo.join('hgrc')) kwmaps = dict(ui.configitems('keywordmaps')) elif opts.get('default'): ui.status(_('\n\tconfiguration using default keyword template maps\n')) kwmaps = kwtemplater.templates if uikwmaps: ui.status(_('\tdisabling current template maps\n')) for k, v in kwmaps.iteritems(): ui.setconfig('keywordmaps', k, v) else: ui.status(_('\n\tconfiguration using current keyword template maps\n')) kwmaps = dict(uikwmaps) or kwtemplater.templates uisetup(ui) reposetup(ui, repo) for k, v in ui.configitems('extensions'): if k.endswith('keyword'): extension = '%s = %s' % (k, v) break ui.write('[extensions]\n%s\n' % extension) demoitems('keyword', ui.configitems('keyword')) demoitems('keywordmaps', kwmaps.iteritems()) keywords = '$' + '$\n$'.join(sorted(kwmaps.keys())) + '$\n' repo.wopener(fn, 'w').write(keywords) repo.add([fn]) path = repo.wjoin(fn) ui.note(_('\nkeywords written to %s:\n') % path) ui.note(keywords) ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname)) # silence branch command if not verbose quiet = ui.quiet ui.quiet = not ui.verbose commands.branch(ui, repo, branchname) ui.quiet = quiet for name, cmd in ui.configitems('hooks'): if name.split('.', 1)[0].find('commit') > -1: repo.ui.setconfig('hooks', name, '') ui.note(_('unhooked all commit hooks\n')) msg = _('hg keyword configuration and expansion example') ui.note("hg -R '%s' ci -m '%s'\n" % (tmpdir, msg)) repo.commit(text=msg) ui.status(_('\n\tkeywords expanded\n')) ui.write(repo.wread(fn)) ui.debug('\nremoving temporary repository %s\n' % tmpdir) shutil.rmtree(tmpdir, ignore_errors=True)
def demo(ui, repo, *args, **opts): '''print [keywordmaps] configuration and an expansion example Show current, custom, or default keyword template maps and their expansions. Extend current configuration by specifying maps as arguments and optionally by reading from an additional hgrc file. Override current keyword template maps with "default" option. ''' def demoitems(section, items): ui.write('[%s]\n' % section) for k, v in items: ui.write('%s = %s\n' % (k, v)) msg = 'hg keyword config and expansion example' kwstatus = 'current' fn = 'demo.txt' branchname = 'demobranch' tmpdir = tempfile.mkdtemp('', 'kwdemo.') ui.note(_('creating temporary repository at %s\n') % tmpdir) repo = localrepo.localrepository(ui, tmpdir, True) ui.setconfig('keyword', fn, '') if args or opts.get('rcfile'): kwstatus = 'custom' if opts.get('rcfile'): ui.readconfig(opts.get('rcfile')) if opts.get('default'): kwstatus = 'default' kwmaps = kwtemplater.templates if ui.configitems('keywordmaps'): # override maps from optional rcfile for k, v in kwmaps.iteritems(): ui.setconfig('keywordmaps', k, v) elif args: # simulate hgrc parsing rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args] fp = repo.opener('hgrc', 'w') fp.writelines(rcmaps) fp.close() ui.readconfig(repo.join('hgrc')) if not opts.get('default'): kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates uisetup(ui) reposetup(ui, repo) for k, v in ui.configitems('extensions'): if k.endswith('keyword'): extension = '%s = %s' % (k, v) break ui.status(_('\n\tconfig using %s keyword template maps\n') % kwstatus) ui.write('[extensions]\n%s\n' % extension) demoitems('keyword', ui.configitems('keyword')) demoitems('keywordmaps', kwmaps.iteritems()) keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n' repo.wopener(fn, 'w').write(keywords) repo.add([fn]) path = repo.wjoin(fn) ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path)) ui.note(keywords) ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname)) # silence branch command if not verbose quiet = ui.quiet ui.quiet = not ui.verbose commands.branch(ui, repo, branchname) ui.quiet = quiet for name, cmd in ui.configitems('hooks'): if name.split('.', 1)[0].find('commit') > -1: repo.ui.setconfig('hooks', name, '') ui.note(_('unhooked all commit hooks\n')) ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg)) repo.commit(text=msg) fmt = ui.verbose and ' in %s' % path or '' ui.status(_('\n\t%s keywords expanded%s\n') % (kwstatus, fmt)) ui.write(repo.wread(fn)) ui.debug(_('\nremoving temporary repository %s\n') % tmpdir) shutil.rmtree(tmpdir, ignore_errors=True)
def create_and_switch_to_branch(self, branchname): commands.branch(self.ui, self.repo, label=branchname)
def demo(ui, repo, *args, **opts): '''print [keywordmaps] configuration and an expansion example Show current, custom, or default keyword template maps and their expansion. Extend current configuration by specifying maps as arguments and optionally by reading from an additional hgrc file. Override current keyword template maps with "default" option. ''' def demostatus(stat): ui.status(_('\n\t%s\n') % stat) def demoitems(section, items): ui.write('[%s]\n' % section) for k, v in items: ui.write('%s = %s\n' % (k, v)) msg = 'hg keyword config and expansion example' kwstatus = 'current' fn = 'demo.txt' branchname = 'demobranch' tmpdir = tempfile.mkdtemp('', 'kwdemo.') ui.note(_('creating temporary repo at %s\n') % tmpdir) repo = localrepo.localrepository(ui, path=tmpdir, create=True) ui.setconfig('keyword', fn, '') if args or opts.get('rcfile'): kwstatus = 'custom' if opts.get('rcfile'): ui.readconfig(opts.get('rcfile')) if opts.get('default'): kwstatus = 'default' kwmaps = kwtemplater.templates if ui.configitems('keywordmaps'): # override maps from optional rcfile for k, v in kwmaps.iteritems(): ui.setconfig('keywordmaps', k, v) elif args: # simulate hgrc parsing rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args] fp = repo.opener('hgrc', 'w') fp.writelines(rcmaps) fp.close() ui.readconfig(repo.join('hgrc')) if not opts.get('default'): kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates reposetup(ui, repo) for k, v in ui.configitems('extensions'): if k.endswith('keyword'): extension = '%s = %s' % (k, v) break demostatus('config using %s keyword template maps' % kwstatus) ui.write('[extensions]\n%s\n' % extension) demoitems('keyword', ui.configitems('keyword')) demoitems('keywordmaps', kwmaps.iteritems()) keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n' repo.wopener(fn, 'w').write(keywords) repo.add([fn]) path = repo.wjoin(fn) ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path)) ui.note(keywords) ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname)) # silence branch command if not verbose quiet = ui.quiet ui.quiet = not ui.verbose commands.branch(ui, repo, branchname) ui.quiet = quiet for name, cmd in ui.configitems('hooks'): if name.split('.', 1)[0].find('commit') > -1: repo.ui.setconfig('hooks', name, '') ui.note(_('unhooked all commit hooks\n')) ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg)) repo.commit(text=msg) format = ui.verbose and ' in %s' % path or '' demostatus('%s keywords expanded%s' % (kwstatus, format)) ui.write(repo.wread(fn)) ui.debug(_('\nremoving temporary repo %s\n') % tmpdir) shutil.rmtree(tmpdir, ignore_errors=True)