Skip to content

Commit 9cd4d41

Browse files
authored
Merge pull request #114 from fosslight/develop
Exclude untracked files in input path
2 parents 7199996 + a83a15b commit 9cd4d41

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

src/fosslight_prechecker/_precheck.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
def exclude_untracked_files(path):
3838
global DEFAULT_EXCLUDE_EXTENSION_FILES
3939
try:
40-
cmd_result = subprocess.check_output(['git', 'ls-files', '-o', f'{path}'], universal_newlines=True)
40+
cmd_result = subprocess.check_output(['git', 'ls-files', '-o'], universal_newlines=True)
4141
cmd_result = cmd_result.split('\n')
4242
cmd_result.remove('')
4343
if not path.endswith("/"):
@@ -48,35 +48,54 @@ def exclude_untracked_files(path):
4848
logger.error(f"Error to get git untracked files : {ex}")
4949

5050

51-
def exclude_gitignore_files(path):
51+
def exclude_gitignore_files(root_path, path):
5252
global DEFAULT_EXCLUDE_EXTENSION_FILES
5353
try:
54-
cmd_result = subprocess.check_output(['git',
55-
'ls-files',
56-
'-i',
57-
f"--exclude-from={os.path.join(path, '.gitignore')}"],
58-
universal_newlines=True)
59-
cmd_result = cmd_result.split('\n')
60-
cmd_result.remove('')
61-
if not path.endswith("/"):
62-
path += "/"
63-
cmd_result = [file.replace(path, '', 1) for file in cmd_result]
64-
DEFAULT_EXCLUDE_EXTENSION_FILES.extend(cmd_result)
54+
if os.path.isfile(os.path.join(root_path, '.gitignore')):
55+
cmd_result = subprocess.check_output(['git',
56+
'ls-files',
57+
'-i',
58+
'--exclude-from=.gitignore'],
59+
universal_newlines=True)
60+
cmd_result = cmd_result.split('\n')
61+
cmd_result.remove('')
62+
if not path.endswith("/"):
63+
path += "/"
64+
cmd_result = [file.replace(path, '', 1) for file in cmd_result]
65+
DEFAULT_EXCLUDE_EXTENSION_FILES.extend(cmd_result)
66+
else:
67+
return
68+
6569
except Exception as ex:
6670
logger.error(f"Error to get git ignored files : {ex}")
6771

6872

73+
def exclude_git_related_files(path):
74+
try:
75+
root_path = VCSStrategyGit.find_root(path)
76+
77+
# Change currnt path for git command
78+
current_path = os.getcwd()
79+
os.chdir(path)
80+
81+
# Exclude untracked files
82+
exclude_untracked_files(path)
83+
# Exclude ignore files
84+
exclude_gitignore_files(root_path, path)
85+
86+
# Restore path
87+
os.chdir(current_path)
88+
except Exception as ex:
89+
logger.error(f"Error to get git related files : {ex}")
90+
91+
6992
def find_oss_pkg_info_and_exlcude_file(path):
7093
global DEFAULT_EXCLUDE_EXTENSION_FILES
7194
oss_pkg_info = []
7295
git_present = shutil.which("git")
7396

74-
# Exlcude untracked files
7597
if _turn_on_exclude_config and git_present and VCSStrategyGit.in_repo(path):
76-
exclude_untracked_files(path)
77-
# Exclude all files from .gitignore
78-
if os.path.isfile(os.path.join(path, '.gitignore')):
79-
exclude_gitignore_files(path)
98+
exclude_git_related_files(path)
8099

81100
try:
82101
for root, dirs, files in os.walk(path):

src/fosslight_prechecker/_result.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files,
288288
license_missing_files, copyright_missing_files = exclude_file_in_yaml(path_to_find, oss_yaml_files,
289289
set(license_missing_files) - set(oss_pkg_info_files),
290290
set(copyright_missing_files) - set(oss_pkg_info_files))
291+
# Subtract excluded files(untracked or ignored file)
292+
oss_pkg_info_files = list(set(oss_pkg_info_files) - set(exclude_files))
291293

292294
if len(license_missing_files) == 0 and len(copyright_missing_files) == 0:
293295
prechecker_compliant = True

0 commit comments

Comments
 (0)