/usr/bin/svn-foreign is in xxdiff-scripts 1:4.0.1+dfsg-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | #!/usr/bin/python
# This file is part of the xxdiff package. See xxdiff for license and details.
# If this file is found independently as svn-foreign, here are the licensing
# terms:
#
# svn-foreign
# Copyright (C) 2006 Martin Blais
# URL: http://furius.ca/xxdiff/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""svn-foreign [<options>] [<dir> ...]
This script allows you to quickly add or delete files that are foreign to a
Subversion checkout, and to easily resolve conflicts. It deals with the common
case where you have developed something using Subversion and you have forgotten
some unregistered files before committing.
svn-foreign runs 'svn status' on the given Subversion-managed directories, to
find out which files are unaccounted for and for each of these files, it asks
you what to do with it::
- add it
- delete it
- ignore/mask it (sets an svn:ignore property on its parent directory)
- skip it for now (leave it where it is)
Other actions::
- quit / [x] exit, this interrupts the process
- delete with no backups (for large files)
- view the file with a pager (more)
- resolve conflict (only valid on a file with an unresolved conflict)
- revert conflict (only valid on a file with an unresolved conflict)
The script works interactively and is meant to allow you to quickly deal with
the forgotten files in a subversion checkout. It works with directories as
well.
Also, files in conflicts are properly ignored and not counted as foreign files.
When unresolved conflicts are found, you are queried about them, answer with 'r'
to resolve it and delete the temporary files.
"""
# Future Work
# -----------
#
# - Make backups work without the xxdiff.backup module.
# - svn-foreign: add support for editing conflictual file
#
# Credits
# -------
# * To Sean Reifschneider (Jafo) for providing example code for raw tty input
__version__ = '$Revision$'
__author__ = 'Martin Blais <blais@furius.ca>'
# stdlib imports
import sys, os, termios, tty, tempfile, datetime, re
from subprocess import Popen, PIPE, call
from os.path import *
# xxdiff imports (optional)
try:
from xxdiff import backup
except ImportError:
backup = None
debug = False
def read_one():
"""
Reads a single character from the terminal.
"""
# Set terminal in raw mode for faster input.
orig_term_attribs = termios.tcgetattr(sys.stdin.fileno())
tty.setraw(sys.stdin.fileno())
try:
c = sys.stdin.read(1)
if c == chr(3): # INTR
raise KeyboardInterrupt
finally:
termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH,
orig_term_attribs)
return c
def add(fn):
"""
Add the file into Subversion.
"""
call(['svn', 'add', fn])
def delete(fn):
"""
Delete the file from Subversion.
"""
call(['svn', 'delete', fn])
def rmrf(fnodn):
"""
Delete the given directory and all its contents.
"""
if not exists(fnodn):
return
if isdir(fnodn):
for root, dirs, files in os.walk(fnodn, topdown=False):
for fn in files:
os.remove(join(root, fn))
for dn in dirs:
adn = join(root, dn)
if islink(adn):
os.remove(adn)
else:
os.rmdir(adn)
os.rmdir(root)
else:
os.remove(fnodn)
def ignore_prop(dn, ignores):
"""
Set svn:ignore on directory 'dn'.
"""
f = tempfile.NamedTemporaryFile('w')
f.write(ignores)
f.flush()
call(['svn', 'propset', 'svn:ignore', '-F', f.name, dn])
f.close()
def filter2(predicate, *arguments):
ein, eout = [], []
for args in zip(*arguments):
add_args = args
if len(args) == 1:
add_args = args[0]
if predicate(*args):
ein.append(add_args)
else:
eout.append(add_args)
return ein, eout
def parse_options():
"""
Parse the options.
"""
import optparse
parser = optparse.OptionParser(__doc__.strip())
parser.add_option('-c', '--commit', action='store_true',
help="Ask to commit after running.")
parser.add_option('-m', '--comments', action='store',
help="Specify comments for commit.")
parser.add_option('-v', '--verbose',
action='store_const', const=2, dest='verbose',
default=1,
help=optparse.SUPPRESS_HELP)
parser.add_option('-q', '--quiet', '--silent',
action='store_const', const=0, dest='verbose',
help="Suppress certain harmless warnings.")
# Add backups disabling option if the xxdiff libraries are available.
if backup is not None:
backup.options_graft(parser,
"These options affect automatic backup of "
"deleted files.")
opts, args = parser.parse_args()
if backup is not None:
backup.options_validate(opts, parser)
if opts.backup_prefix is None:
# Set a better default for the backup prefix.
opts.backup_prefix = (
datetime.datetime.now().replace(microsecond=0).isoformat('T'))
if opts.comments and not opts.commit:
parser.error("You cannot specify comments if you're not going to "
"commit!")
return opts, args
def view(fn, write):
"Call on 'more' to view the file."
write('-' * 80 + '\n')
pager = os.environ.get('PAGER', '/bin/more')
call([pager, fn])
def query_unregistered_svn_files(filenames, opts, output=sys.stdout,
ignore=[]):
"""
Runs an 'svn status' command, and then loops over all the files that are not
registered, asking the user one-by-one what action to take (see this
module's docstring for more details).
'ignore' specifies a list of files to completely ignore.
Return True upon completion. Anything else signals that the user has
interrupted the procedure.
"""
write = output.write
if not hasattr(opts, 'verbose'):
# svnforeign requires a verbose option.
raise RuntimeError(
"Internal Error: You are missing a 'verbose' option.")
ignore = map(abspath, ignore)
# If there are multiple args--such as *--filter out the non svn directories.
dirs, files = filter2(isdir, filenames)
if len(dirs) > 1:
svndirs = [x for x in dirs if isdir(join(x, '.svn'))]
if svndirs != dirs:
if opts.verbose >= 1:
for dn in set(dirs) - set(svndirs):
write("Warning: skipping non-checkout dir '%s'\n" % dn)
filenames = svndirs + files
# Run svn status to find the files that are unaccounted for
cmd = ['svn', 'status'] + filenames
if debug:
write(' '.join(cmd))
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
if p.returncode != 0:
raise SystemExit("Error: %s" % err)
# Get out if there is nothing to do.
if not out.strip():
write('(Nothing to do.)\n')
return True
# Print the output of the status command for the user's excitement
write('Status\n------\n')
write(out)
write('\n')
# Figure out which files are in conflict so that we can ignore the unknown
# merge result files.
conflicts = []
for line in out.splitlines():
if not line or re.match('^Performing', line):
continue
if line[0] == 'C':
fn = line[8:]
cre = re.compile('%s\\.(mine|r\\d+)' % re.escape(fn)).match
conflicts.append( (fn, cre) )
# Command loop (one command)
while True:
write('=> [Resolve|Skip|View|Quit|Redo] %s ? ' % fn)
# Read command
c = read_one()
write(c)
write('\n')
if c == 'r': # Add
call(['svn', 'resolved', fn])
break
elif c == 's': # Skip
break
elif c in 'v': # View
view(fn, write)
elif c in ['q', 'x']: # Quit/exit
write('(Quitting.)\n')
return False
elif c == 'r': # Restart from scratch
# (perhaps you invoke that after adding an ignore wildcard.)
return query_unregistered_svn_files(filenames, opts, output, ignore)
# Process foreign files
for line in out.splitlines():
if not line or re.match('^(Performing|Status)', line):
continue
if line[0] in '?~':
fn = line[8:]
if filter(lambda x: x[1](fn), conflicts):
continue
if abspath(fn) in ignore:
continue
# Get the file size.
try:
size = os.lstat(fn).st_size
except OSError, e:
if e.errno == 2:
continue # File not found, was probably a temp file.
else:
raise
ftype = ''
if islink(fn):
ftype = '(symlink)'
# Command loop (one command)
while True:
write('=> [Add|Delete|Ignore|Skip|View|Quit|Redo] %10d %s %s ? ' %
(size, fn, ftype))
# Read command
c = read_one()
write(c)
write('\n')
if c == 'a': # Add
add(fn)
break
elif c == 'd': # Delete
# Do backups if requested.
if backup is not None:
backupfn = backup.backup_file(fn, opts, output)
if backupfn:
write("Backed up to: '%s'\n" % backupfn)
elif opts.verbose >= 1:
write('(Warning: no backups.)\n')
rmrf(fn)
break
elif c == 'D': # Delete with no backups
rmrf(fn)
break
elif c in ['m', 'i', 'I']: # Ignore (Mask, svn:ignore)
dn, bn = split(fn)
if dn == '':
dn = '.'
# Get the properties first
p = Popen(['svn', 'propget', 'svn:ignore', dn],
stdout=PIPE, stderr=PIPE)
svnign, err = p.communicate()
if p.returncode != 0:
raise SystemExit(err)
# Sanitize the svn:ignore contents a bit
if svnign:
svnign = svnign.rstrip() + '\n'
write("--(current svn:ignore on '%s')--\n" % dn)
write(svnign)
write('----------------------------\n')
write("Add pattern (! to cancel, * to edit, default: [%s]):"
% bn)
pat = raw_input()
if pat == '':
pat = bn
if pat == '!':
print '(cancelled.)'
elif pat == '*':
call(['svn', 'propedit', 'svn:ignore', dn])
break
else:
svnign += pat + '\n'
ignore_prop(dn, svnign)
write('----------------------------\n')
write(svnign)
write('----------------------------\n')
break
elif c == 's': # Skip
break
elif c in ['q', 'x']: # Quit/exit
write('(Quitting.)\n')
return False
elif c == 'r': # Restart from scratch
# (perhaps you invoke that after adding an ignore wildcard.)
return query_unregistered_svn_files(filenames, opts, output, ignore)
elif c in 'v': # View
view(fn, write)
elif c == chr(12): # Ctrl-L: Clear
# FIXME: is there a way to do this directly on the terminal?
call(['clear'])
else: # Loop again
write('(Invalid answer.)\n')
elif line[0] == '!':
fn = line[8:]
if abspath(fn) in ignore:
continue
# Command loop (one command)
while True:
write('=> [Delete|Skip|Quit] %10d %s %s ? ' %
(-1, fn, ''))
# Read command
c = read_one()
write(c)
write('\n')
if c == 'd': # Delete
delete(abspath(fn))
break
elif c == 's': # Skip
break
elif c in ['q', 'x']: # Quit/exit
write('(Quitting.)\n')
return False
elif c == chr(12): # Ctrl-L: Clear
# FIXME: is there a way to do this directly on the terminal?
call(['clear'])
else: # Loop again
write('(Invalid answer.)\n')
write('(Done.)\n')
return True
def main():
"""
Main program.
"""
opts, args = parse_options()
# Run the main loop...
if query_unregistered_svn_files(args, opts, sys.stdout) is not True:
# The user has quit.
sys.exit(1)
# Commit the files if requested.
if opts.commit is not None:
# Command loop (one command)
sys.stdout.write('Commit? [Yes|No/Quit] ')
# Read command
c = read_one().lower()
print c
if c == 'y': # Commit.
comments = opts.comments or ''
call(['svn', 'commit', '-m', comments] + args)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print
sys.exit(1)
|