nodes: expose line counts in node information. This would be used in full text search
This commit is contained in:
parent
6bb83d3692
commit
7cf63ac4ad
2 changed files with 27 additions and 16 deletions
|
|
@ -607,27 +607,32 @@ class FileNode(Node):
|
|||
if self.commit:
|
||||
return self.commit.get_largefile_node(self.path)
|
||||
|
||||
def count_lines(self, content, count_empty=False):
|
||||
|
||||
if count_empty:
|
||||
all_lines = 0
|
||||
empty_lines = 0
|
||||
for line in content.splitlines(True):
|
||||
if line == '\n':
|
||||
empty_lines += 1
|
||||
all_lines += 1
|
||||
|
||||
return all_lines, all_lines - empty_lines
|
||||
else:
|
||||
# fast method
|
||||
empty_lines = all_lines = content.count('\n')
|
||||
if all_lines == 0 and content:
|
||||
# one-line without a newline
|
||||
empty_lines = all_lines = 1
|
||||
|
||||
return all_lines, empty_lines
|
||||
|
||||
def lines(self, count_empty=False):
|
||||
all_lines, empty_lines = 0, 0
|
||||
|
||||
if not self.is_binary:
|
||||
content = self.content
|
||||
if count_empty:
|
||||
all_lines = 0
|
||||
empty_lines = 0
|
||||
for line in content.splitlines(True):
|
||||
if line == '\n':
|
||||
empty_lines += 1
|
||||
all_lines += 1
|
||||
|
||||
return all_lines, all_lines - empty_lines
|
||||
else:
|
||||
# fast method
|
||||
empty_lines = all_lines = content.count('\n')
|
||||
if all_lines == 0 and content:
|
||||
# one-line without a newline
|
||||
empty_lines = all_lines = 1
|
||||
|
||||
all_lines, empty_lines = self.count_lines(content, count_empty=count_empty)
|
||||
return all_lines, empty_lines
|
||||
|
||||
def __repr__(self):
|
||||
|
|
|
|||
|
|
@ -665,11 +665,14 @@ class ScmModel(BaseModel):
|
|||
size = file_node.size
|
||||
over_size_limit = (max_file_bytes is not None and size > max_file_bytes)
|
||||
full_content = None
|
||||
all_lines = 0
|
||||
if not file_node.is_binary and not over_size_limit:
|
||||
full_content = safe_unicode(file_node.content)
|
||||
all_lines, empty_lines = file_node.count_lines(full_content)
|
||||
|
||||
file_data.update({
|
||||
"content": full_content,
|
||||
"lines": all_lines
|
||||
})
|
||||
elif content:
|
||||
# get content *without* cache
|
||||
|
|
@ -678,11 +681,14 @@ class ScmModel(BaseModel):
|
|||
|
||||
over_size_limit = (max_file_bytes is not None and size > max_file_bytes)
|
||||
full_content = None
|
||||
all_lines = 0
|
||||
if not is_binary and not over_size_limit:
|
||||
full_content = safe_unicode(_content)
|
||||
all_lines, empty_lines = file_node.count_lines(full_content)
|
||||
|
||||
file_data.update({
|
||||
"content": full_content,
|
||||
"lines": all_lines
|
||||
})
|
||||
|
||||
except RepositoryError:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue