mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-27 15:06:15 +01:00
- reworked the diff functionality:
* some new things like showing newly added files * moved the whole code into a new methode (make_diff()) * behave more like svn when doing a diff against a certain revision - changed the storedir attribut of the Package() class to an absolute filename - normally this shouldn't have any impact on existing methods, functionality etc.
This commit is contained in:
parent
5518e15673
commit
7b9f85f97f
@ -591,98 +591,14 @@ class Osc(cmdln.Cmdln):
|
|||||||
args = parseargs(args)
|
args = parseargs(args)
|
||||||
pacs = findpacs(args)
|
pacs = findpacs(args)
|
||||||
|
|
||||||
difference_found = False
|
|
||||||
d = []
|
|
||||||
|
|
||||||
rev1, rev2 = parseRevisionOption(opts.revision)
|
rev1, rev2 = parseRevisionOption(opts.revision)
|
||||||
pac = pacs[0]
|
diff = ''
|
||||||
|
for pac in pacs:
|
||||||
if rev1 and rev2 and (len(pacs) == 1):
|
diff += ''.join(make_diff(pac, rev1))
|
||||||
# this is currently not implemented
|
if len(diff) > 0:
|
||||||
print >>sys.stderr, 'this feature isn\'t implemented yet'
|
print diff
|
||||||
sys.exit(1)
|
|
||||||
elif rev1 and (pac.rev != rev1) and (len(pacs) == 1):
|
|
||||||
if not checkRevision(pac.prjname, pac.name, rev1):
|
|
||||||
print >>sys.stderr, 'Revision \'%s\' does not exist' % rev1
|
|
||||||
sys.exit(1)
|
|
||||||
# make a temp dir for checking out the project
|
|
||||||
import tempfile
|
|
||||||
tmpdir = tempfile.mkdtemp(rev1, pac.name, '/tmp')
|
|
||||||
curdir = os.getcwd()
|
|
||||||
os.chdir(tmpdir)
|
|
||||||
init_package_dir(conf.config['apiurl'], pac.prjname, pac.name, tmpdir, rev1)
|
|
||||||
os.chdir(curdir)
|
|
||||||
tmppac = Package(tmpdir)
|
|
||||||
|
|
||||||
changed_files = []
|
|
||||||
added_files = []
|
|
||||||
removed_files = []
|
|
||||||
if pac.todo:
|
|
||||||
for file in pac.todo:
|
|
||||||
if file in tmppac.filenamelist:
|
|
||||||
if dgst(os.path.join(pac.dir, file)) != tmppac.findfilebyname(file).md5:
|
|
||||||
changed_files.append(file)
|
|
||||||
else:
|
|
||||||
added_files.append(file)
|
|
||||||
else:
|
|
||||||
changed_files, added_files, removed_files = pac.comparePac(tmppac)
|
|
||||||
|
|
||||||
for file in changed_files:
|
|
||||||
tmppac.updatefile(file, rev1)
|
|
||||||
d.append('Index: %s\n' % file)
|
|
||||||
d.append('===================================================================\n')
|
|
||||||
d.append(get_source_file_diff(pac.dir, file, rev1, file, tmppac.dir))
|
|
||||||
tmppac.delete_localfile(file)
|
|
||||||
tmppac.delete_storefile(file)
|
|
||||||
|
|
||||||
# this tempfile is used as a dummy file for difflib
|
|
||||||
(fd, filename) = tempfile.mkstemp(dir=tmppac.storedir)
|
|
||||||
|
|
||||||
for file in added_files:
|
|
||||||
d.append('Index: %s\n' % file)
|
|
||||||
d.append('===================================================================\n')
|
|
||||||
d.append(get_source_file_diff(pac.dir, file, rev1, \
|
|
||||||
os.path.basename(filename), \
|
|
||||||
tmppac.storedir, file))
|
|
||||||
|
|
||||||
for file in removed_files:
|
|
||||||
tmppac.updatefile(file, rev1)
|
|
||||||
d.append('Index: %s\n' % file)
|
|
||||||
d.append('===================================================================\n')
|
|
||||||
d.append(get_source_file_diff(tmppac.storedir, \
|
|
||||||
os.path.basename(filename), \
|
|
||||||
rev1, file, tmppac.dir, file))
|
|
||||||
tmppac.delete_localfile(file)
|
|
||||||
tmppac.delete_storefile(file)
|
|
||||||
|
|
||||||
# clean up
|
|
||||||
os.unlink(filename)
|
|
||||||
for dir, dirnames, files in os.walk(tmppac.storedir):
|
|
||||||
for file in files:
|
|
||||||
os.unlink(os.path.join(dir, file))
|
|
||||||
os.rmdir(tmppac.storedir)
|
|
||||||
os.rmdir(tmppac.dir)
|
|
||||||
else:
|
|
||||||
for p in pacs:
|
|
||||||
if p.todo == []:
|
|
||||||
for i in p.filenamelist:
|
|
||||||
s = p.status(i)
|
|
||||||
if s == 'M' or s == 'C':
|
|
||||||
p.todo.append(i)
|
|
||||||
|
|
||||||
for filename in p.todo:
|
|
||||||
d.append('Index: %s\n' % filename)
|
|
||||||
d.append('===================================================================\n')
|
|
||||||
d.append(get_source_file_diff(p.dir, filename, p.rev))
|
|
||||||
|
|
||||||
|
|
||||||
if d:
|
|
||||||
print ''.join(d)
|
|
||||||
difference_found = True
|
|
||||||
|
|
||||||
if difference_found:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def do_repourls(self, subcmd, opts, *args):
|
def do_repourls(self, subcmd, opts, *args):
|
||||||
"""${cmd_name}: shows URLs of .repo files
|
"""${cmd_name}: shows URLs of .repo files
|
||||||
|
|
||||||
|
102
osc/core.py
102
osc/core.py
@ -201,7 +201,7 @@ class Package:
|
|||||||
def __init__(self, workingdir):
|
def __init__(self, workingdir):
|
||||||
self.dir = workingdir
|
self.dir = workingdir
|
||||||
self.absdir = os.path.abspath(self.dir)
|
self.absdir = os.path.abspath(self.dir)
|
||||||
self.storedir = os.path.join(self.dir, store)
|
self.storedir = os.path.join(self.absdir, store)
|
||||||
|
|
||||||
check_store_version(self.dir)
|
check_store_version(self.dir)
|
||||||
|
|
||||||
@ -498,7 +498,7 @@ class Package:
|
|||||||
exists_in_store = False
|
exists_in_store = False
|
||||||
if n in self.filenamelist:
|
if n in self.filenamelist:
|
||||||
known_by_meta = True
|
known_by_meta = True
|
||||||
if os.path.exists(os.path.join(self.dir, n)):
|
if os.path.exists(os.path.join(self.absdir, n)):
|
||||||
exists = True
|
exists = True
|
||||||
if os.path.exists(os.path.join(self.storedir, n)):
|
if os.path.exists(os.path.join(self.storedir, n)):
|
||||||
exists_in_store = True
|
exists_in_store = True
|
||||||
@ -512,7 +512,7 @@ class Package:
|
|||||||
state = 'C'
|
state = 'C'
|
||||||
elif exists and exists_in_store and known_by_meta:
|
elif exists and exists_in_store and known_by_meta:
|
||||||
#print self.findfilebyname(n)
|
#print self.findfilebyname(n)
|
||||||
if dgst(os.path.join(self.dir, n)) != self.findfilebyname(n).md5:
|
if dgst(os.path.join(self.absdir, n)) != self.findfilebyname(n).md5:
|
||||||
state = 'M'
|
state = 'M'
|
||||||
else:
|
else:
|
||||||
state = ' '
|
state = ' '
|
||||||
@ -1306,7 +1306,7 @@ def get_source_file_diff(dir, filename, rev, oldfilename = None, olddir = None,
|
|||||||
|
|
||||||
if not origfilename:
|
if not origfilename:
|
||||||
origfilename = filename
|
origfilename = filename
|
||||||
|
|
||||||
file1 = os.path.join(olddir, oldfilename) # old/stored original
|
file1 = os.path.join(olddir, oldfilename) # old/stored original
|
||||||
file2 = os.path.join(dir, filename) # working copy
|
file2 = os.path.join(dir, filename) # working copy
|
||||||
|
|
||||||
@ -1338,6 +1338,100 @@ def get_source_file_diff(dir, filename, rev, oldfilename = None, olddir = None,
|
|||||||
|
|
||||||
return ''.join(d)
|
return ''.join(d)
|
||||||
|
|
||||||
|
def make_diff(wc, revision):
|
||||||
|
import tempfile
|
||||||
|
changed_files = []
|
||||||
|
added_files = []
|
||||||
|
removed_files = []
|
||||||
|
cmp_pac = None
|
||||||
|
diff_hdr = 'Index: %s\n'
|
||||||
|
diff_hdr += '===================================================================\n'
|
||||||
|
if not revision:
|
||||||
|
# normal diff
|
||||||
|
for file in wc.filenamelist+wc.filenamelist_unvers:
|
||||||
|
state = wc.status(file)
|
||||||
|
if state == 'M' or state == 'C':
|
||||||
|
changed_files.append(file)
|
||||||
|
elif state == 'A':
|
||||||
|
added_files.append(file)
|
||||||
|
elif state == 'D':
|
||||||
|
removed_files.append(file)
|
||||||
|
else:
|
||||||
|
olddir = os.getcwd()
|
||||||
|
tmpdir = tempfile.mkdtemp(revision, wc.name, '/tmp')
|
||||||
|
os.chdir(tmpdir)
|
||||||
|
init_package_dir(conf.config['apiurl'], wc.prjname, wc.name, tmpdir, revision)
|
||||||
|
cmp_pac = Package(tmpdir)
|
||||||
|
if wc.todo:
|
||||||
|
for file in wc.todo:
|
||||||
|
if file in cmp_pac.filenamelist:
|
||||||
|
changed_files.append(file)
|
||||||
|
# behave like svn
|
||||||
|
#else:
|
||||||
|
# added_files.append(file)
|
||||||
|
else:
|
||||||
|
for file in wc.filenamelist+wc.filenamelist_unvers:
|
||||||
|
state = wc.status(file)
|
||||||
|
if wc.status(file) == 'A' and (not file in cmp_pac.filenamelist):
|
||||||
|
added_files.append(file)
|
||||||
|
elif file in cmp_pac.filenamelist and wc.status(file) == 'D':
|
||||||
|
removed_files.append(file)
|
||||||
|
elif wc.status(file) == ' ' and not file in cmp_pac.filenamelist:
|
||||||
|
added_files.append(file)
|
||||||
|
elif file in cmp_pac.filenamelist and state != 'A' and state != '?':
|
||||||
|
if dgst(os.path.join(wc.absdir, file)) != cmp_pac.findfilebyname(file).md5:
|
||||||
|
changed_files.append(file)
|
||||||
|
for file in cmp_pac.filenamelist:
|
||||||
|
if not file in wc.filenamelist:
|
||||||
|
removed_files.append(file)
|
||||||
|
|
||||||
|
diff = []
|
||||||
|
for file in changed_files:
|
||||||
|
diff.append(diff_hdr % file)
|
||||||
|
if cmp_pac == None:
|
||||||
|
diff.append(get_source_file_diff(wc.absdir, file, wc.rev))
|
||||||
|
else:
|
||||||
|
cmp_pac.updatefile(file, revision)
|
||||||
|
diff.append(get_source_file_diff(wc.absdir, file, revision, file,
|
||||||
|
cmp_pac.absdir, file))
|
||||||
|
(fd, tmpfile) = tempfile.mkstemp(dir='/tmp')
|
||||||
|
for file in added_files:
|
||||||
|
diff.append(diff_hdr % file)
|
||||||
|
if cmp_pac == None:
|
||||||
|
diff.append(get_source_file_diff(wc.absdir, file, wc.rev, os.path.basename(tmpfile),
|
||||||
|
os.path.dirname(tmpfile), file))
|
||||||
|
else:
|
||||||
|
diff.append(get_source_file_diff(wc.absdir, file, revision, os.path.basename(tmpfile),
|
||||||
|
os.path.dirname(tmpfile), file))
|
||||||
|
|
||||||
|
# FIXME: this is ugly but it cannot be avoided atm
|
||||||
|
# if a file is deleted via "osc rm file" we should keep the storefile.
|
||||||
|
tmp_pac = None
|
||||||
|
if cmp_pac == None:
|
||||||
|
olddir = os.getcwd()
|
||||||
|
tmpdir = tempfile.mkdtemp(dir='/tmp')
|
||||||
|
os.chdir(tmpdir)
|
||||||
|
init_package_dir(conf.config['apiurl'], wc.prjname, wc.name, tmpdir, wc.rev)
|
||||||
|
tmp_pac = Package(tmpdir)
|
||||||
|
os.chdir(olddir)
|
||||||
|
|
||||||
|
for file in removed_files:
|
||||||
|
diff.append(diff_hdr % file)
|
||||||
|
if cmp_pac == None:
|
||||||
|
tmp_pac.updatefile(file, tmp_pac.rev)
|
||||||
|
diff.append(get_source_file_diff(os.path.dirname(tmpfile), os.path.basename(tmpfile),
|
||||||
|
wc.rev, file, tmp_pac.storedir, file))
|
||||||
|
else:
|
||||||
|
cmp_pac.updatefile(file, revision)
|
||||||
|
diff.append(get_source_file_diff(os.path.dirname(tmpfile), os.path.basename(tmpfile),
|
||||||
|
revision, file, cmp_pac.storedir, file))
|
||||||
|
|
||||||
|
os.chdir(olddir)
|
||||||
|
if cmp_pac != None:
|
||||||
|
delete_tmpdir(cmp_pac.absdir)
|
||||||
|
if tmp_pac != None:
|
||||||
|
delete_tmpdir(tmp_pac.absdir)
|
||||||
|
return diff
|
||||||
|
|
||||||
def make_dir(apiurl, project, package):
|
def make_dir(apiurl, project, package):
|
||||||
#print "creating directory '%s'" % project
|
#print "creating directory '%s'" % project
|
||||||
|
Loading…
Reference in New Issue
Block a user