Merge pull request #2 from kirill-markin:fix-full-path

Fix-full-path
This commit is contained in:
Kirill Markin 2024-06-10 08:35:24 +02:00 committed by GitHub
commit 43beff4666
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 6 deletions

View file

@ -56,6 +56,12 @@ You can customize the behavior of `repo-to-text` with the following options:
repo-to-text --debug repo-to-text --debug
``` ```
or to save the debug log to a file:
```bash
repo-to-text --debug > debug_log.txt 2>&1
```
## Settings ## Settings
`repo-to-text` also supports configuration via a `.repo-to-text-settings.yaml` file. By default, the tool works without this file, but you can use it to customize what gets included in the final text file. `repo-to-text` also supports configuration via a `.repo-to-text-settings.yaml` file. By default, the tool works without this file, but you can use it to customize what gets included in the final text file.

View file

@ -24,15 +24,18 @@ def get_tree_structure(path='.', gitignore_spec=None, tree_and_content_ignore_sp
logging.debug('Filtering tree output based on .gitignore and ignore-tree-and-content specification') logging.debug('Filtering tree output based on .gitignore and ignore-tree-and-content specification')
filtered_lines = [] filtered_lines = []
for line in tree_output.splitlines(): for line in tree_output.splitlines():
parts = line.strip().split() stripped_line = line.strip()
if parts: if stripped_line:
full_path = parts[-1] # Extract the path by removing the leading tree branch symbols
full_path = stripped_line.split(maxsplit=1)[-1]
relative_path = os.path.relpath(full_path, path) relative_path = os.path.relpath(full_path, path)
if not should_ignore_file(full_path, relative_path, gitignore_spec, None, tree_and_content_ignore_spec): if not should_ignore_file(full_path, relative_path, gitignore_spec, None, tree_and_content_ignore_spec):
filtered_lines.append(line.replace('./', '', 1)) filtered_lines.append(line.replace('./', '', 1))
filtered_tree_output = '\n'.join(filtered_lines)
logging.debug(f'Filtered tree structure: {filtered_tree_output}')
logging.debug('Tree structure filtering complete') logging.debug('Tree structure filtering complete')
return '\n'.join(filtered_lines) return filtered_tree_output
def load_ignore_specs(path='.'): def load_ignore_specs(path='.'):
gitignore_spec = None gitignore_spec = None
@ -61,7 +64,7 @@ def load_ignore_specs(path='.'):
return gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec return gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec
def should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec): def should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec):
return ( result = (
is_ignored_path(file_path) or is_ignored_path(file_path) or
(gitignore_spec and gitignore_spec.match_file(relative_path)) or (gitignore_spec and gitignore_spec.match_file(relative_path)) or
(content_ignore_spec and content_ignore_spec.match_file(relative_path)) or (content_ignore_spec and content_ignore_spec.match_file(relative_path)) or
@ -69,6 +72,9 @@ def should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_
os.path.basename(file_path).startswith('repo-to-text_') os.path.basename(file_path).startswith('repo-to-text_')
) )
logging.debug(f'Checking if file should be ignored: {file_path}, relative path: {relative_path}, result: {result}')
return result
def is_ignored_path(file_path: str) -> bool: def is_ignored_path(file_path: str) -> bool:
ignored_dirs = ['.git'] ignored_dirs = ['.git']
ignored_files_prefix = ['repo-to-text_'] ignored_files_prefix = ['repo-to-text_']
@ -113,6 +119,7 @@ def save_repo_to_text(path='.', output_dir=None) -> str:
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(path) gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(path)
tree_structure = get_tree_structure(path, gitignore_spec, tree_and_content_ignore_spec) tree_structure = get_tree_structure(path, gitignore_spec, tree_and_content_ignore_spec)
tree_structure = remove_empty_dirs(tree_structure, path) tree_structure = remove_empty_dirs(tree_structure, path)
logging.debug(f'Final tree structure to be written: {tree_structure}')
# Add timestamp to the output file name with a descriptive name # Add timestamp to the output file name with a descriptive name
timestamp = datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-UTC') timestamp = datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-UTC')