[PATCH] purge: use opts.get()

Gregory Szorc gregory.szorc at gmail.com
Mon May 16 21:24:57 UTC 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1463433699 25200
#      Mon May 16 14:21:39 2016 -0700
# Node ID a2a87f12649b7485f42b4d4d25e8f27e5a0c3f07
# Parent  90d84e1e427a9d65aedd870cdb7283f84bb30141
purge: use opts.get()

Most commands use opts.get() to retrieve values for options
that may not be explicitly passed. purge wasn't.

This makes it easier to call purge() from 3rd party extensions.

diff --git a/hgext/purge.py b/hgext/purge.py
--- a/hgext/purge.py
+++ b/hgext/purge.py
@@ -79,44 +79,44 @@ def purge(ui, repo, *dirs, **opts):
     If directories are given on the command line, only files in these
     directories are considered.
 
     Be careful with purge, as you could irreversibly delete some files
     you forgot to add to the repository. If you only want to print the
     list of files that this program would delete, use the --print
     option.
     '''
-    act = not opts['print']
+    act = not opts.get('print')
     eol = '\n'
-    if opts['print0']:
+    if opts.get('print0'):
         eol = '\0'
         act = False # --print0 implies --print
-    removefiles = opts['files']
-    removedirs = opts['dirs']
+    removefiles = opts.get('files')
+    removedirs = opts.get('dirs')
     if not removefiles and not removedirs:
         removefiles = True
         removedirs = True
 
     def remove(remove_func, name):
         if act:
             try:
                 remove_func(repo.wjoin(name))
             except OSError:
                 m = _('%s cannot be removed') % name
-                if opts['abort_on_err']:
+                if opts.get('abort_on_err'):
                     raise error.Abort(m)
                 ui.warn(_('warning: %s\n') % m)
         else:
             ui.write('%s%s' % (name, eol))
 
     match = scmutil.match(repo[None], dirs, opts)
     if removedirs:
         directories = []
         match.explicitdir = match.traversedir = directories.append
-    status = repo.status(match=match, ignored=opts['all'], unknown=True)
+    status = repo.status(match=match, ignored=opts.get('all'), unknown=True)
 
     if removefiles:
         for f in sorted(status.unknown + status.ignored):
             if act:
                 ui.note(_('removing file %s\n') % f)
             remove(util.unlink, f)
 
     if removedirs:


More information about the Mercurial-devel mailing list