3737def 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+
6992def 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 ):
0 commit comments