11const simpleGit = require ( "simple-git" ) ;
2+ const minimatch = require ( "minimatch" ) ;
3+ import { FILE_EXCLUDE_PATTERNS } from "../config/constants" ;
4+
5+ export interface UncommittedFile {
6+ path : string ;
7+ status : string ; // 'modified', 'added', 'deleted', etc.
8+ }
29
310export default class GitManager {
11+ private static isFileExcluded ( filePath : string , excludePatterns : string [ ] = FILE_EXCLUDE_PATTERNS ) : boolean {
12+ return excludePatterns . some ( pattern => {
13+ try {
14+ // Normalize the file path to use forward slashes
15+ const normalizedPath = filePath . replace ( / \\ / g, '/' ) ;
16+ const result = minimatch ( normalizedPath , pattern , {
17+ matchBase : true ,
18+ dot : true ,
19+ nocase : true // Case insensitive matching for Windows
20+ } ) ;
21+ if ( result ) {
22+ console . log ( `File ${ normalizedPath } matched pattern ${ pattern } ` ) ;
23+ }
24+ return result ;
25+ } catch ( error ) {
26+ console . warn ( `Invalid glob pattern: ${ pattern } ` , error ) ;
27+ return false ;
28+ }
29+ } ) ;
30+ }
31+
432 public static async getRemoteUrls ( folderPath : string ) : Promise < string [ ] > {
533 try {
634 const git = simpleGit ( folderPath ) ;
@@ -13,4 +41,48 @@ export default class GitManager {
1341 }
1442 return [ ] ;
1543 }
44+
45+ public static async getUncommittedFiles ( folderPath : string , includeIgnored : boolean = false ) : Promise < UncommittedFile [ ] > {
46+ try {
47+ const git = simpleGit ( folderPath ) ;
48+ const status = await git . status ( ) ;
49+
50+ const uncommittedFiles : UncommittedFile [ ] = [ ] ;
51+
52+ // Helper function to add files if they're not excluded
53+ const addFileIfNotExcluded = ( file : string , fileStatus : string ) => {
54+ const isExcluded = this . isFileExcluded ( file ) ;
55+ console . log ( `Processing file: ${ file } , excluded: ${ isExcluded } , includeIgnored: ${ includeIgnored } ` ) ;
56+
57+ if ( includeIgnored || ! isExcluded ) {
58+ uncommittedFiles . push ( { path : file , status : fileStatus } ) ;
59+ }
60+ } ;
61+
62+ // Add modified files
63+ status . modified . forEach ( ( file : string ) => {
64+ addFileIfNotExcluded ( file , 'modified' ) ;
65+ } ) ;
66+
67+ // Add new files
68+ status . not_added . forEach ( ( file : string ) => {
69+ addFileIfNotExcluded ( file , 'untracked' ) ;
70+ } ) ;
71+
72+ // Add deleted files
73+ status . deleted . forEach ( ( file : string ) => {
74+ addFileIfNotExcluded ( file , 'deleted' ) ;
75+ } ) ;
76+
77+ // Add staged files
78+ status . staged . forEach ( ( file : string ) => {
79+ addFileIfNotExcluded ( file , 'staged' ) ;
80+ } ) ;
81+
82+ return uncommittedFiles ;
83+ } catch ( error ) {
84+ console . error ( 'Error getting uncommitted files:' , error ) ;
85+ return [ ] ;
86+ }
87+ }
1688}
0 commit comments