-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanged.ts
More file actions
226 lines (220 loc) · 7.04 KB
/
changed.ts
File metadata and controls
226 lines (220 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* @fileoverview "Anything different from HEAD" helpers — the broad-strokes
* `git status --porcelain` view that lumps staged, unstaged, and untracked
* paths together. Use these when you want a single answer to "did anything
* change?"; reach for `staged.ts` / `unstaged.ts` when the distinction
* matters.
*/
import { normalizePath } from '../paths/normalize'
import { ArrayPrototypeIncludes } from '../primordials/array'
import { getGitDiffSpawnArgs, innerDiff, innerDiffSync } from './_internal'
import { getCachedRealpath, getCwd, getPath } from './repo'
import type { GitDiffOptions } from './types'
/**
* Get all changed files including staged, unstaged, and untracked files.
*
* Uses `git status --porcelain` which returns the full working tree status
* with status codes:
* - `M` - Modified
* - `A` - Added
* - `D` - Deleted
* - `??` - Untracked
* - `R` - Renamed
* - `C` - Copied
*
* This is the most comprehensive check - captures everything that differs
* from the last commit, including:
* - Files modified and staged with `git add`
* - Files modified but not staged
* - New files not yet tracked by git
*
* Status codes are automatically stripped from the output.
*
* @param options - Options controlling path format and filtering.
* @returns Promise resolving to array of changed file paths.
*
* @example
* ```typescript
* // Get all changed files as relative paths
* const files = await getChangedFiles()
* // => ['src/foo.ts', 'src/bar.ts', 'newfile.ts']
*
* // Get absolute paths
* const files = await getChangedFiles({ absolute: true })
* // => ['/path/to/repo/src/foo.ts', ...]
*
* // Get changed files in specific directory
* const files = await getChangedFiles({ cwd: '/path/to/repo/src' })
* // => ['foo.ts', 'bar.ts']
* ```
*/
export async function getChangedFiles(
options?: GitDiffOptions | undefined,
): Promise<string[]> {
const args = getGitDiffSpawnArgs(options?.cwd).all
return await innerDiff(args, {
__proto__: null,
...options,
porcelain: true,
})
}
/**
* Get all changed files including staged, unstaged, and untracked files.
*
* Synchronous version of `getChangedFiles()`. Uses `git status --porcelain`
* which returns the full working tree status with status codes:
* - `M` - Modified
* - `A` - Added
* - `D` - Deleted
* - `??` - Untracked
* - `R` - Renamed
* - `C` - Copied
*
* This is the most comprehensive check - captures everything that differs
* from the last commit, including:
* - Files modified and staged with `git add`
* - Files modified but not staged
* - New files not yet tracked by git
*
* Status codes are automatically stripped from the output.
*
* @param options - Options controlling path format and filtering.
* @returns Array of changed file paths.
*
* @example
* ```typescript
* // Get all changed files as relative paths
* const files = getChangedFilesSync()
* // => ['src/foo.ts', 'src/bar.ts', 'newfile.ts']
*
* // Get absolute paths
* const files = getChangedFilesSync({ absolute: true })
* // => ['/path/to/repo/src/foo.ts', ...]
*
* // Get changed files in specific directory
* const files = getChangedFilesSync({ cwd: '/path/to/repo/src' })
* // => ['foo.ts', 'bar.ts']
* ```
*/
export function getChangedFilesSync(
options?: GitDiffOptions | undefined,
): string[] {
const args = getGitDiffSpawnArgs(options?.cwd).all
return innerDiffSync(args, {
__proto__: null,
...options,
porcelain: true,
})
}
/**
* Check if a file or directory has any git changes.
*
* Checks if the given pathname has any changes including:
* - Staged modifications (added with `git add`)
* - Unstaged modifications (not yet staged)
* - Untracked status (new file/directory not in git)
*
* For directories, returns `true` if ANY file within the directory has changes.
*
* Symlinks in the pathname and cwd are automatically resolved using
* `fs.realpathSync()` before comparison.
*
* @param pathname - File or directory path to check.
* @param options - Options for the git status check.
* @returns Promise resolving to `true` if path has any changes, `false` otherwise.
*
* @example
* ```typescript
* // Check if file is changed
* const changed = await isChanged('src/foo.ts')
* // => true
*
* // Check if directory has any changes
* const changed = await isChanged('src/')
* // => true (if any file in src/ is changed)
*
* // Check from different cwd
* const changed = await isChanged(
* '/path/to/repo/src/foo.ts',
* { cwd: '/path/to/repo' }
* )
* ```
*/
export async function isChanged(
pathname: string,
options?: GitDiffOptions | undefined,
): Promise<boolean> {
const files = await getChangedFiles({
__proto__: null,
...options,
absolute: false,
})
const path = getPath()
// Resolve pathname to handle symlinks before computing relative path (using cache).
const resolvedPathname = getCachedRealpath(pathname)
// options.cwd-passed arm exercised when caller specifies cwd; default getCwd().
/* c8 ignore start */
const baseCwd = options?.cwd ? getCachedRealpath(options['cwd']) : getCwd()
/* c8 ignore stop */
const relativePath = normalizePath(path.relative(baseCwd, resolvedPathname))
return ArrayPrototypeIncludes(files, relativePath)
}
/**
* Check if a file or directory has any git changes.
*
* Synchronous version of `isChanged()`. Checks if the given pathname has
* any changes including:
* - Staged modifications (added with `git add`)
* - Unstaged modifications (not yet staged)
* - Untracked status (new file/directory not in git)
*
* For directories, returns `true` if ANY file within the directory has changes.
*
* Symlinks in the pathname and cwd are automatically resolved using
* `fs.realpathSync()` before comparison.
*
* @param pathname - File or directory path to check.
* @param options - Options for the git status check.
* @returns `true` if path has any changes, `false` otherwise.
*
* @example
* ```typescript
* // Check if file is changed
* const changed = isChangedSync('src/foo.ts')
* // => true
*
* // Check if directory has any changes
* const changed = isChangedSync('src/')
* // => true (if any file in src/ is changed)
*
* // Check from different cwd
* const changed = isChangedSync(
* '/path/to/repo/src/foo.ts',
* { cwd: '/path/to/repo' }
* )
* ```
*/
export function isChangedSync(
pathname: string,
options?: GitDiffOptions | undefined,
): boolean {
const files = getChangedFilesSync({
__proto__: null,
...options,
absolute: false,
})
const path = getPath()
// Resolve pathname to handle symlinks before computing relative path (using cache).
try {
const resolvedPathname = getCachedRealpath(pathname)
// options.cwd-passed arm exercised when caller specifies cwd; default getCwd().
/* c8 ignore start */
const baseCwd = options?.cwd ? getCachedRealpath(options['cwd']) : getCwd()
/* c8 ignore stop */
const relativePath = normalizePath(path.relative(baseCwd, resolvedPathname))
return ArrayPrototypeIncludes(files, relativePath)
} catch {
// Path doesn't exist or can't be resolved - it can't be changed
return false
}
}