def delete(self): """Remove this resource (recursive).""" self._checkWriteAccess() filepath = self._getFilePath() commands.remove(self.provider.ui, self.provider.repo, filepath, force=True)
def test_remove_file_no_parser(self): """Change by removing a file, without parser""" ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) (open(hgrepo.pathto("file.txt"), "w").write("line 1\nline 2\n")) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() hgcommands.remove(ui, hgrepo, "path:file.txt") hgcommands.commit(ui, hgrepo, user="******", message="Second commit") rev1 = hgrepo[1].hex() repo_url = "http://localhost:8001/%s/" % self.repo_name Repository.objects.create(name=self.repo_name, url=repo_url) url = reverse("pushes.views.diff") response = self.client.get(url, {"repo": self.repo_name, "from": rev0, "to": rev1}) eq_(response.status_code, 200) html_diff = response.content.split("Changed files:")[1] ok_(re.findall(">\s*file\.txt\s*<", html_diff)) # 1 removed file eq_(html_diff.count('<div class="diff file-removed">'), 1) # also, expect a link to the old revision of the file change_ref = 'href="%sfile/%s/file.txt"' % (repo_url, rev0) ok_(change_ref in html_diff) ok_(not re.findall(">\s*line 1\s*<", html_diff)) ok_(not re.findall(">\s*line 2\s*<", html_diff))
def delete(self): """Remove this resource (recursive).""" self._checkWriteAccess() filepath = self._getFilePath() commands.remove(self.provider.ui, self.provider.repo, filepath, force=True)
def test_remove_file(self): """Change by removing a file, with parser""" ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) ( open(hgrepo.pathto("file.dtd"), "w").write( """ <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> """ ) ) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() hgcommands.remove(ui, hgrepo, "path:file.dtd") hgcommands.commit(ui, hgrepo, user="******", message="Second commit") rev1 = hgrepo[1].hex() Repository.objects.create(name=self.repo_name, url="http://localhost:8001/%s/" % self.repo_name) url = reverse("pushes.views.diff") response = self.client.get(url, {"repo": self.repo_name, "from": rev0, "to": rev1}) eq_(response.status_code, 200) html_diff = response.content.split("Changed files:")[1] ok_(re.findall(">\s*file\.dtd\s*<", html_diff)) # 2 entities with 2 rows each eq_(html_diff.count('<tr class="line-removed">'), 4) ok_(re.findall(">\s*key1\s*<", html_diff)) ok_(re.findall(">\s*Hello\s*<", html_diff)) ok_(re.findall(">\s*key2\s*<", html_diff)) ok_(re.findall(">\s*Cruel\s*<", html_diff))
def remove(self, paths): # XXX: support for after ? commands.remove( self.ui, self.repo, after=False, # only hg 0.9.5 needs that explicit force=False, *self.joined(paths))
def commit_deletions(self, repo, file_names, msg='no comment'): for fname in file_names: commands.remove(self.ui_, repo, os.path.join(repo.root, fname)) commands.commit(self.ui_, repo, logfile=None, addremove=None, user=None, date=None, message=msg)
def __handle_directory_changes(self, trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed): repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) content_alert_str = '' files_to_remove = [] filenames_in_archive = [ os.path.join(full_path, name) for name in filenames_in_archive ] if remove_repo_files_not_in_tar and not repository.is_new(trans.app): # We have a repository that is not new (it contains files), so discover # those files that are in the repository, but not in the uploaded archive. for root, dirs, files in os.walk(full_path): if root.find('.hg') < 0 and root.find('hgrc') < 0: for undesirable_dir in undesirable_dirs: if undesirable_dir in dirs: dirs.remove(undesirable_dir) undesirable_dirs_removed += 1 for undesirable_file in undesirable_files: if undesirable_file in files: files.remove(undesirable_file) undesirable_files_removed += 1 for name in files: full_name = os.path.join(root, name) if full_name not in filenames_in_archive: files_to_remove.append(full_name) for repo_file in files_to_remove: # Remove files in the repository (relative to the upload point) that are not in the uploaded archive. try: commands.remove(repo.ui, repo, repo_file, force=True) except Exception, e: log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str(e)) relative_selected_file = selected_file.split( 'repo_%d' % repository.id)[1].lstrip('/') repo.dirstate.remove(relative_selected_file) repo.dirstate.write() absolute_selected_file = os.path.abspath(selected_file) if os.path.isdir(absolute_selected_file): try: os.rmdir(absolute_selected_file) except OSError, e: # The directory is not empty pass elif os.path.isfile(absolute_selected_file): os.remove(absolute_selected_file) dir = os.path.split(absolute_selected_file)[0] try: os.rmdir(dir) except OSError, e: # The directory is not empty pass
def hg_remove(source): face = ui.ui() repo = hg.repository(face, blog_dir) location = get_location(os.path.basename(source)) commands.remove(face, repo, source) commands.commit(face, repo, source, user=g.blog['AUTHOR'], message='deleted post {}'.format(os.path.basename(source))) if not os.path.exists(os.path.dirname(source)): os.mkdir(os.path.dirname(source))
def run_try(ui, repo, *args, **opts): """Push the current head to try """ if not opts['build'] or not opts['platform']: raise util.Abort('Both -b and -p are required') # We rely on the try server to validate anything beyond that simple # check above, so let's just blindly go about our business! tryopts = [] tryopts.append('-b') tryopts.extend(opts['build']) tryopts.append('-p') tryopts.extend(opts['platform']) if opts.get('unit'): tryopts.append('-u') tryopts.extend(opts['unit']) if opts.get('talos'): tryopts.append('-t') tryopts.extend(opts['talos']) trymsg = 'try: %s' % (' '.join(tryopts),) if repo[None].dirty(): raise util.Abort('You have outstanding changes') try: strip = extensions.find('strip') except KeyError: ui.warn('strip extension not found, use the following syntax:\n') ui.write('%s\n' % (trymsg,)) return ui.write('setting try selections...\n') # This next bit here is a hack to get an empty commit cwd = os.getcwd() junkfile = tempfile.mktemp(prefix='hgjunk', dir='') os.chdir(repo.root) file(junkfile, 'w').close() commands.add(ui, repo, junkfile) commands.commit(ui, repo, message='add junk file (will be gone)') commands.remove(ui, repo, junkfile) commands.commit(ui, repo, amend=True, message=trymsg, logfile=None) os.chdir(cwd) # Get the revision of our try commit so we can strip it later node = repo[None].p1().hex() ui.write('pushing to try...\n') commands.push(ui, repo, 'try', force=True) # Now we must clean up after ourslves by stripping the try commit strip.stripcmd(ui, repo, node, rev=[], no_backup=True)
def commit_results(self, msg_id, submission_tuple, results): """ INTERNAL: Commit the results of a submission to the local repo. """ print "RESULTS: ", results if len(results[3]) > 0 and sum([len(results[index]) for index in (0, 1, 2, 4)]) == 0: #HACK, fix order! raise NoChangesError() assert sum([len(results[index]) for index in (0, 1, 2, 4)]) > 0 wikitext_dir = os.path.join(self.full_base_path(), 'wikitext') raised = True # grrr pylint gives spurious #pylint: disable-msg=E1101 self.ui_.pushbuffer() try: # hg add new files. for name in results[0]: full_path = os.path.join(wikitext_dir, name) commands.add(self.ui_, self.repo, full_path) # hg add fork files for name in results[4]: full_path = os.path.join(wikitext_dir, name) commands.add(self.ui_, self.repo, full_path) # hg remove removed files. for name in results[2]: full_path = os.path.join(wikitext_dir, name) commands.remove(self.ui_, self.repo, full_path) # Writes to/prunes special file used to generate RemoteChanges. self.update_change_log(msg_id, submission_tuple, results, True) # REDFLAG: LATER, STAKING? later allow third field for staker. # fms_id|chk commit_msg = "%s|%s" % (submission_tuple[0], submission_tuple[3]) # hg commit commands.commit(self.ui_, self.repo, logfile=None, addremove=None, user=None, date=None, message=commit_msg) self.fixup_accepted_log() # Fix version in accepted.txt self.notify_committed(True) raised = False finally: text = self.ui_.popbuffer() if raised: self.logger.debug("commit_results -- popped log:\n%s" % text)
def handle_directory_changes( trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed ): repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) content_alert_str = '' files_to_remove = [] filenames_in_archive = [ os.path.join( full_path, name ) for name in filenames_in_archive ] if remove_repo_files_not_in_tar and not repository.is_new( trans.app ): # We have a repository that is not new (it contains files), so discover those files that are in the repository, but not in the uploaded archive. for root, dirs, files in os.walk( full_path ): if root.find( '.hg' ) < 0 and root.find( 'hgrc' ) < 0: for undesirable_dir in UNDESIRABLE_DIRS: if undesirable_dir in dirs: dirs.remove( undesirable_dir ) undesirable_dirs_removed += 1 for undesirable_file in UNDESIRABLE_FILES: if undesirable_file in files: files.remove( undesirable_file ) undesirable_files_removed += 1 for name in files: full_name = os.path.join( root, name ) if full_name not in filenames_in_archive: files_to_remove.append( full_name ) for repo_file in files_to_remove: # Remove files in the repository (relative to the upload point) that are not in the uploaded archive. try: commands.remove( repo.ui, repo, repo_file, force=True ) except Exception, e: log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str( e )) relative_selected_file = selected_file.split( 'repo_%d' % repository.id )[1].lstrip( '/' ) repo.dirstate.remove( relative_selected_file ) repo.dirstate.write() absolute_selected_file = os.path.abspath( selected_file ) if os.path.isdir( absolute_selected_file ): try: os.rmdir( absolute_selected_file ) except OSError, e: # The directory is not empty. pass elif os.path.isfile( absolute_selected_file ): os.remove( absolute_selected_file ) dir = os.path.split( absolute_selected_file )[0] try: os.rmdir( dir ) except OSError, e: # The directory is not empty. pass
def remove(parent, ui, repo, files): commands.remove(ui, repo, *files) return True
def remove(self, files): commands.remove(self.ui, self.repository, *[os.path.join(self.path, fn) for fn in files])
def remove_file(repo_ui, repo, selected_file, force=True): commands.remove(repo_ui, repo, selected_file, force=force)
def remove_file( repo_ui, repo, selected_file, force=True ): commands.remove( repo_ui, repo, selected_file, force=force )
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])
def upload_tar( self, trans, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert, ): # Upload a tar archive of files. repo_dir = repository.repo_path repo = hg.repository(get_configured_ui(), repo_dir) files_to_remove = [] content_alert_str = "" undesirable_dirs_removed = 0 undesirable_files_removed = 0 ok, message = self.__check_archive(tar) if not ok: tar.close() uploaded_file.close() return ok, message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed else: if upload_point is not None: full_path = os.path.abspath(os.path.join(repo_dir, upload_point)) else: full_path = os.path.abspath(repo_dir) filenames_in_archive = [] for tarinfo_obj in tar.getmembers(): ok = os.path.basename(tarinfo_obj.name) not in undesirable_files if ok: for file_path_item in tarinfo_obj.name.split("/"): if file_path_item in undesirable_dirs: undesirable_dirs_removed += 1 ok = False break else: undesirable_files_removed += 1 if ok: filenames_in_archive.append(tarinfo_obj.name) filenames_in_archive = [os.path.join(full_path, name) for name in filenames_in_archive] # Extract the uploaded tar to the load_point within the repository hierarchy. tar.extractall(path=full_path) tar.close() uploaded_file.close() if remove_repo_files_not_in_tar and not repository.is_new: # We have a repository that is not new (it contains files), so discover # those files that are in the repository, but not in the uploaded archive. for root, dirs, files in os.walk(full_path): if root.find(".hg") < 0 and root.find("hgrc") < 0: for undesirable_dir in undesirable_dirs: if undesirable_dir in dirs: dirs.remove(undesirable_dir) undesirable_dirs_removed += 1 for undesirable_file in undesirable_files: if undesirable_file in files: files.remove(undesirable_file) undesirable_files_removed += 1 for name in files: full_name = os.path.join(root, name) if full_name not in filenames_in_archive: files_to_remove.append(full_name) for repo_file in files_to_remove: # Remove files in the repository (relative to the upload point) that are not in the uploaded archive. try: commands.remove(repo.ui, repo, repo_file, force=True) except Exception, e: log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str(e) ) relative_selected_file = selected_file.split("repo_%d" % repository.id)[1].lstrip("/") repo.dirstate.remove(relative_selected_file) repo.dirstate.write() absolute_selected_file = os.path.abspath(selected_file) if os.path.isdir(absolute_selected_file): try: os.rmdir(absolute_selected_file) except OSError, e: # The directory is not empty pass elif os.path.isfile(absolute_selected_file): os.remove(absolute_selected_file) dir = os.path.split(absolute_selected_file)[0] try: os.rmdir(dir) except OSError, e: # The directory is not empty pass
def remove(parent, ui, repo, files): commands.remove(ui, repo, *files) return True
def upload_tar(self, trans, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert): # Upload a tar archive of files. repo_dir = repository.repo_path repo = hg.repository(get_configured_ui(), repo_dir) files_to_remove = [] content_alert_str = '' undesirable_dirs_removed = 0 undesirable_files_removed = 0 ok, message = self.__check_archive(tar) if not ok: tar.close() uploaded_file.close() return ok, message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed else: if upload_point is not None: full_path = os.path.abspath( os.path.join(repo_dir, upload_point)) else: full_path = os.path.abspath(repo_dir) filenames_in_archive = [] for tarinfo_obj in tar.getmembers(): ok = os.path.basename( tarinfo_obj.name) not in undesirable_files if ok: for file_path_item in tarinfo_obj.name.split('/'): if file_path_item in undesirable_dirs: undesirable_dirs_removed += 1 ok = False break else: undesirable_files_removed += 1 if ok: filenames_in_archive.append(tarinfo_obj.name) filenames_in_archive = [ os.path.join(full_path, name) for name in filenames_in_archive ] # Extract the uploaded tar to the load_point within the repository hierarchy. tar.extractall(path=full_path) tar.close() uploaded_file.close() if remove_repo_files_not_in_tar and not repository.is_new: # We have a repository that is not new (it contains files), so discover # those files that are in the repository, but not in the uploaded archive. for root, dirs, files in os.walk(full_path): if root.find('.hg') < 0 and root.find('hgrc') < 0: for undesirable_dir in undesirable_dirs: if undesirable_dir in dirs: dirs.remove(undesirable_dir) undesirable_dirs_removed += 1 for undesirable_file in undesirable_files: if undesirable_file in files: files.remove(undesirable_file) undesirable_files_removed += 1 for name in files: full_name = os.path.join(root, name) if full_name not in filenames_in_archive: files_to_remove.append(full_name) for repo_file in files_to_remove: # Remove files in the repository (relative to the upload point) that are not in the uploaded archive. try: commands.remove(repo.ui, repo, repo_file, force=True) except Exception, e: log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str(e)) relative_selected_file = selected_file.split( 'repo_%d' % repository.id)[1].lstrip('/') repo.dirstate.remove(relative_selected_file) repo.dirstate.write() absolute_selected_file = os.path.abspath(selected_file) if os.path.isdir(absolute_selected_file): try: os.rmdir(absolute_selected_file) except OSError, e: # The directory is not empty pass elif os.path.isfile(absolute_selected_file): os.remove(absolute_selected_file) dir = os.path.split(absolute_selected_file)[0] try: os.rmdir(dir) except OSError, e: # The directory is not empty pass
def dohgremove(): commands.remove(self.stat.ui, self.stat.repo, *wfiles, **removeopts)