Skip to content

Commit

Permalink
Fix testing revision for being empty
Browse files Browse the repository at this point in the history
Revision 0 is a valid value, but evaluates as False the same way as None or "".
This change threats 0 as a proper revision.
  • Loading branch information
dmach committed Oct 5, 2023
1 parent 62e4817 commit 0bb7e89
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
8 changes: 4 additions & 4 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ def do_list(self, subcmd, opts, *args):
if li.haserror():
raise oscerr.LinkExpandError(project, package, li.error)
project, package, rev = li.project, li.package, li.rev
if rev:
if not revision_is_empty(rev):

Check warning on line 1384 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L1384

Added line #L1384 was not covered by tests
print('# -> %s %s (%s)' % (project, package, rev))
else:
print('# -> %s %s (latest)' % (project, package))
Expand Down Expand Up @@ -5274,10 +5274,10 @@ def do_checkout(self, subcmd, opts, *args):
raise oscerr.WrongOptions('-D | --deleted can only be used with a package')

rev, dummy = parseRevisionOption(opts.revision)
if rev is None:
if revision_is_empty(rev):

Check warning on line 5277 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L5277

Added line #L5277 was not covered by tests
rev = "latest"

if rev and rev != "latest" and not checkRevision(project, package, rev):
if not revision_is_empty(rev) and rev != "latest" and not checkRevision(project, package, rev):

Check warning on line 5280 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L5280

Added line #L5280 was not covered by tests
print('Revision \'%s\' does not exist' % rev, file=sys.stderr)
sys.exit(1)

Expand Down Expand Up @@ -7607,7 +7607,7 @@ def do_log(self, subcmd, opts, *args):
)

rev, rev_upper = parseRevisionOption(opts.revision)
if rev and not checkRevision(project, package, rev, apiurl, opts.meta):
if not revision_is_empty(rev) and not checkRevision(project, package, rev, apiurl, opts.meta):

Check warning on line 7610 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L7610

Added line #L7610 was not covered by tests
print('Revision \'%s\' does not exist' % rev, file=sys.stderr)
sys.exit(1)

Expand Down
50 changes: 28 additions & 22 deletions osc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ def os_path_samefile(path1, path2):
return os.path.realpath(path1) == os.path.realpath(path2)


def revision_is_empty(rev: Union[None, str, int]):
return rev in (None, "")


@total_ordering
class File:
"""represent a file, including its metadata"""
Expand Down Expand Up @@ -2186,15 +2190,15 @@ def diff_add_delete(fname, add, revision):
if add:
diff.append(b'--- %s\t(revision 0)\n' % fname.encode())
rev = 'revision 0'
if revision and fname not in self.to_be_added:
if not revision_is_empty(revision) and fname not in self.to_be_added:
rev = 'working copy'
diff.append(b'+++ %s\t(%s)\n' % (fname.encode(), rev.encode()))
fname = os.path.join(self.absdir, fname)
if not os.path.isfile(fname):
raise oscerr.OscIOError(None, 'file \'%s\' is marked as \'A\' but does not exist\n'
'(either add the missing file or revert it)' % fname)
else:
if revision:
if not revision_is_empty(revision):
b_revision = str(revision).encode()
else:
b_revision = self.rev.encode()
Expand All @@ -2205,7 +2209,7 @@ def diff_add_delete(fname, add, revision):
fd = None
tmpfile = None
try:
if revision is not None and not add:
if not revision_is_empty(revision) and not add:
(fd, tmpfile) = tempfile.mkstemp(prefix='osc_diff')
get_source_file(self.apiurl, self.prjname, self.name, origname, tmpfile, revision)
fname = tmpfile
Expand Down Expand Up @@ -2270,7 +2274,7 @@ def diff_add_delete(fname, add, revision):
continue
elif state == ' ' and revision is None:
continue
elif revision and self.findfilebyname(f.name).md5 == f.md5 and state != 'M':
elif not revision_is_empty(revision) and self.findfilebyname(f.name).md5 == f.md5 and state != 'M':
continue
yield [diff_hdr % f.name.encode()]
if revision is None:
Expand Down Expand Up @@ -3698,7 +3702,7 @@ def meta_get_filelist(
query['expand'] = 1
if meta:
query['meta'] = 1
if revision:
if not revision_is_empty(revision):

Check warning on line 3705 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L3705

Added line #L3705 was not covered by tests
query['rev'] = revision
else:
query['rev'] = 'latest'
Expand Down Expand Up @@ -3739,7 +3743,7 @@ def show_project_meta(apiurl: str, prj: str, rev=None, blame=None):
query = {}
if blame:
query['view'] = "blame"
if rev:
if not revision_is_empty(rev):

Check warning on line 3746 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L3746

Added line #L3746 was not covered by tests
query['rev'] = rev
url = makeurl(apiurl, ['source', prj, '_project', '_meta'], query)
try:
Expand All @@ -3765,7 +3769,7 @@ def show_project_meta(apiurl: str, prj: str, rev=None, blame=None):
def show_project_conf(apiurl: str, prj: str, rev=None, blame=None):
query = {}
url = None
if rev:
if not revision_is_empty(rev):

Check warning on line 3772 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L3772

Added line #L3772 was not covered by tests
query['rev'] = rev
if blame:
query['view'] = "blame"
Expand Down Expand Up @@ -4200,11 +4204,11 @@ def show_files_meta(
deleted=False,
):
query = {}
if revision:
if not revision_is_empty(revision):
query['rev'] = revision
else:
query['rev'] = 'latest'
if linkrev:
if not revision_is_empty(linkrev):
query['linkrev'] = linkrev
elif conf.config['linkcontrol']:
query['linkrev'] = 'base'
Expand Down Expand Up @@ -4303,7 +4307,9 @@ def get_project_sourceinfo(apiurl: str, project: str, nofilename: bool, *package
def show_upstream_rev_vrev(apiurl: str, prj, pac, revision=None, expand=False, meta=False):
m = show_files_meta(apiurl, prj, pac, revision=revision, expand=expand, meta=meta)
et = ET.fromstring(m)
return et.get('rev'), et.get('vrev')
rev = et.get("rev") or None
vrev = et.get("vrev") or None
return rev, vrev


def show_upstream_rev(
Expand Down Expand Up @@ -5232,7 +5238,7 @@ def get_source_file(
query = {}
if meta:
query['meta'] = 1
if revision:
if not revision_is_empty(revision):
query['rev'] = revision
u = makeurl(
apiurl,
Expand Down Expand Up @@ -5404,9 +5410,9 @@ def server_diff(
query['oproject'] = old_project
if old_package:
query['opackage'] = old_package
if old_revision:
if not revision_is_empty(old_revision):
query['orev'] = old_revision
if new_revision:
if not revision_is_empty(new_revision):
query['rev'] = new_revision
if unified:
query['unified'] = 1
Expand Down Expand Up @@ -5779,7 +5785,7 @@ def link_pac(
if src_project == dst_project and src_package == dst_package:
raise oscerr.OscValueError("Cannot link package. Source and target are the same.")

if rev and not checkRevision(src_project, src_package, rev):
if not revision_is_empty(rev) and not checkRevision(src_project, src_package, rev):

Check warning on line 5788 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L5788

Added line #L5788 was not covered by tests
raise oscerr.OscValueError(f"Revision doesn't exist: {rev}")

meta_change = False
Expand Down Expand Up @@ -5839,7 +5845,7 @@ def link_pac(
print('_link file already exists...! Aborting', file=sys.stderr)
sys.exit(1)

if rev:
if not revision_is_empty(rev):

Check warning on line 5848 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L5848

Added line #L5848 was not covered by tests
rev = ' rev="%s"' % rev
else:
rev = ''
Expand Down Expand Up @@ -6124,9 +6130,9 @@ def branch_pkg(
query['newinstance'] = "1"
if extend_package_names:
query['extend_package_names'] = "1"
if rev:
if not revision_is_empty(rev):

Check warning on line 6133 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L6133

Added line #L6133 was not covered by tests
query['rev'] = rev
if linkrev:
if not revision_is_empty(linkrev):

Check warning on line 6135 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L6135

Added line #L6135 was not covered by tests
query['linkrev'] = linkrev
if target_project:
query['target_project'] = target_project
Expand Down Expand Up @@ -6232,7 +6238,7 @@ def copy_pac(
query['expand'] = '1'
if keep_link:
query['keeplink'] = '1'
if revision:
if not revision_is_empty(revision):

Check warning on line 6241 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L6241

Added line #L6241 was not covered by tests
query['orev'] = revision
if comment:
query['comment'] = comment
Expand Down Expand Up @@ -7113,7 +7119,7 @@ def get_source_rev(apiurl: str, project: str, package: str, revision=None):
# but not rev=current,rev=latest,rev=top, or anything like this.
# CAUTION: We have to loop through all rev and find the highest one, if none given.

if revision:
if not revision_is_empty(revision):

Check warning on line 7122 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L7122

Added line #L7122 was not covered by tests
url = makeurl(apiurl, ['source', project, package, '_history'], {'rev': revision})
else:
url = makeurl(apiurl, ['source', project, package, '_history'])
Expand Down Expand Up @@ -7200,10 +7206,10 @@ def get_commitlog(
rev = int(node.get('rev'))
# vrev = int(node.get('vrev')) # what is the meaning of vrev?
try:
if revision is not None and revision_upper is not None:
if not revision_is_empty(revision) and revision_upper is not None:

Check warning on line 7209 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L7209

Added line #L7209 was not covered by tests
if rev > int(revision_upper) or rev < int(revision):
continue
elif revision is not None and rev != int(revision):
elif not revision_is_empty(revision) and rev != int(revision):

Check warning on line 7212 in osc/core.py

View check run for this annotation

Codecov / codecov/patch

osc/core.py#L7212

Added line #L7212 was not covered by tests
continue
except ValueError:
if revision != srcmd5:
Expand Down Expand Up @@ -7753,7 +7759,7 @@ def _set_link_rev(apiurl: str, project: str, package: str, root, revision="", ex
if revision:
root.set('rev', revision)
# add vrev when revision is a srcmd5
if vrev is not None and revision is not None and len(revision) >= 32:
if not revision_is_empty(vrev) and not revision_is_empty(revision) and len(revision) >= 32:
root.set('vrev', vrev)
return revision

Expand Down

0 comments on commit 0bb7e89

Please sign in to comment.