1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 16:49:42 +00:00

add option to show diff before asking to overwrite

This commit is contained in:
deadc0de6
2018-09-29 16:04:03 +02:00
parent a8e9f77606
commit a375876438
6 changed files with 36 additions and 18 deletions

View File

@@ -610,6 +610,7 @@ the following entries:
* `keepdot`: preserve leading dot when importing hidden file in the `dotpath` (default *false*)
* `link_by_default`: when importing a dotfile set `link` to that value per default (default *false*)
* `workdir`: directory where templates are installed before being symlink when using `link` (default *~/.config/dotdrop*)
* `showdiff`: on install show a diff before asking to overwrite (see `--showdiff`) (default *false*)
* **dotfiles** entry: a list of dotfiles
* When `link` is true, dotdrop will create a symlink instead of copying (default *false*).

View File

@@ -29,6 +29,7 @@ class Cfg:
key_banner = 'banner'
key_long = 'longkey'
key_keepdot = 'keepdot'
key_showdiff = 'showdiff'
key_deflink = 'link_by_default'
key_workdir = 'workdir'
@@ -64,6 +65,7 @@ class Cfg:
default_link = False
default_longkey = False
default_keepdot = False
default_showdiff = False
default_link_by_default = False
default_workdir = '~/.config/dotdrop'
@@ -315,6 +317,8 @@ class Cfg:
self.lnk_settings[self.key_deflink] = self.default_link_by_default
if self.key_workdir not in self.lnk_settings:
self.lnk_settings[self.key_workdir] = self.default_workdir
if self.key_showdiff not in self.lnk_settings:
self.lnk_settings[self.key_showdiff] = self.default_showdiff
def abs_dotpath(self, path):
"""transform path to an absolute path based on config path"""

View File

@@ -42,26 +42,26 @@ USAGE = """
{}
Usage:
dotdrop install [-tfndVb] [-c <path>] [-p <profile>] [<key>...]
dotdrop import [-ldVb] [-c <path>] [-p <profile>] <paths>...
dotdrop compare [-Vb] [-c <path>] [-p <profile>]
[-o <opts>] [-i <name>...]
[--files=<files>]
dotdrop update [-fdVb] [-c <path>] <paths>...
dotdrop listfiles [-VTb] [-c <path>] [-p <profile>]
dotdrop list [-Vb] [-c <path>]
dotdrop install [-tfndVbD] [-c <path>] [-p <profile>] [<key>...]
dotdrop import [-ldVb] [-c <path>] [-p <profile>] <paths>...
dotdrop compare [-Vb] [-c <path>] [-p <profile>]
[-o <opts>] [-C <files>] [-i <name>...]
dotdrop update [-fdVb] [-c <path>] <paths>...
dotdrop listfiles [-VTb] [-c <path>] [-p <profile>]
dotdrop list [-Vb] [-c <path>]
dotdrop --help
dotdrop --version
Options:
-p --profile=<profile> Specify the profile to use [default: {}].
-c --cfg=<path> Path to the config [default: config.yaml].
--files=<files> Comma separated list of files to compare.
-C --files=<files> Comma separated list of files to compare.
-i --ignore=<name> File name to ignore when diffing.
-o --dopts=<opts> Diff options [default: ].
-n --nodiff Do not diff when installing.
-t --temp Install to a temporary directory for review.
-T --template Only template dotfiles.
-D --showdiff Show a diff before overwriting.
-l --link Import and link.
-f --force Do not warn if exists.
-V --verbose Be verbose.
@@ -94,9 +94,10 @@ def install(opts, conf, temporary=False, keys=[]):
if temporary:
tmpdir = get_tmpdir()
inst = Installer(create=opts['create'], backup=opts['backup'],
dry=opts['dry'], safe=opts['safe'], base=opts['dotpath'],
workdir=opts['workdir'], diff=opts['installdiff'],
debug=opts['debug'], totemp=tmpdir)
dry=opts['dry'], safe=opts['safe'],
base=opts['dotpath'], workdir=opts['workdir'],
diff=opts['installdiff'], debug=opts['debug'],
totemp=tmpdir, showdiff=opts['showdiff'])
installed = []
for dotfile in dotfiles:
preactions = []
@@ -361,6 +362,7 @@ def main():
opts['link'] = args['--link']
opts['debug'] = args['--verbose']
opts['variables'] = conf.get_variables()
opts['showdiff'] = opts['showdiff'] or args['--showdiff']
if opts['debug']:
LOG.dbg('config file: {}'.format(args['--cfg']))

View File

@@ -20,7 +20,7 @@ class Installer:
def __init__(self, base='.', create=True, backup=True,
dry=False, safe=False, workdir='~/.config/dotdrop',
debug=False, diff=True, totemp=None):
debug=False, diff=True, totemp=None, showdiff=False):
self.create = create
self.backup = backup
self.dry = dry
@@ -30,6 +30,7 @@ class Installer:
self.debug = debug
self.diff = diff
self.totemp = totemp
self.showdiff = showdiff
self.comparing = False
self.action_executed = False
self.log = Logger()
@@ -125,7 +126,7 @@ class Installer:
self.log.err('source dotfile does not exist: {}'.format(src))
return []
st = os.stat(src)
ret = self._write(dst, content, st.st_mode, actions=actions)
ret = self._write(src, dst, content, st.st_mode, actions=actions)
if ret < 0:
self.log.err('installing {} to {}'.format(src, dst))
return []
@@ -164,7 +165,7 @@ class Installer:
cur = f.read()
return cur == content
def _write(self, dst, content, rights, actions=[]):
def _write(self, src, dst, content, rights, actions=[]):
"""write content to file
return 0 for success,
1 when already exists
@@ -178,9 +179,17 @@ class Installer:
if self.debug:
self.log.dbg('{} is the same'.format(dst))
return 1
if self.safe and not self.log.ask('Overwrite \"{}\"'.format(dst)):
self.log.warn('ignoring {}, already present'.format(dst))
return 1
if self.safe:
if self.debug:
self.log.dbg('change detected for {}'.format(dst))
if self.showdiff:
comp = Comparator(debug=self.debug)
diff = comp.compare(src, dst)
self.log.log('diff \"{}\" VS \"{}\"'.format(src, dst))
self.log.emph(diff)
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
self.log.warn('ignoring {}'.format(dst))
return 1
if self.backup and os.path.lexists(dst):
self._backup(dst)
base = os.path.dirname(dst)

View File

@@ -80,6 +80,7 @@ def load_config(confpath, profile):
opts['safe'] = True
opts['installdiff'] = True
opts['link'] = False
opts['showdiff'] = True
opts['debug'] = True
opts['dopts'] = ''
opts['variables'] = {}

View File

@@ -174,6 +174,7 @@ exec bspwm
conf, opts = load_config(confpath, profile)
opts['safe'] = False
opts['debug'] = True
opts['showdiff'] = True
opts['variables'] = {}
install(opts, conf)