GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — app-ghost/scripts/lib/git.js at main · code/app-ghost

Expand file tree

/

git.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
104 lines (93 loc) · 4.25 KB
/

git.js

Copy path

File metadata and controls

104 lines (93 loc) · 4.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import { $ as $$ } from 'execa';
import pm from 'picomatch';
import { ROOT_DIR } from './constants.js';
// Always run git from the repo root so callers can pass repo-relative paths
// regardless of the process working directory.
const $ = $$({ cwd: ROOT_DIR });
// Git prints one of these when the commit is valid but the path isn't in it —
// including the new-file case, where the working tree has the path but the base
// commit doesn't ("exists on disk, but not in"). Both are distinct from an
// invalid commit ("invalid object name") or any other git failure.
const MISSING_PATH_RE = /does not exist in|exists on disk, but not in/;
function isMissingPathError(error) {
return MISSING_PATH_RE.test(`${error.stderr ?? ''}\n${error.message ?? ''}`);
}
/**
* Retrieves the contents of a file from a specific commit in a Git repository.
*
* @param {string} commitHash - The hash of the commit to retrieve the file from.
* @param {string} filePath - The path to the file within the repository.
* @param {object} [options]
* @param {boolean} [options.allowMissing=false] - When true, resolve to null if
* the path does not exist in the commit instead of throwing. Invalid commits
* and other git failures still throw.
* @returns {Promise<string|null>} - The file contents, or null when the path is
* missing and allowMissing is set.
*/
export async function getFileFromCommit(commitHash, filePath, { allowMissing = false } = {}) {
try {
const { stdout } = await $`git show ${commitHash}:${filePath}`;
return stdout;
} catch (error) {
if (allowMissing && isMissingPathError(error)) {
return null;
}
throw new Error(`Failed to retrieve file from commit: ${error.message}`);
}
}
/**
* Gets the files changed between two commits for a specific path, optionally
* filtered by a prefix.
*
* @param {string} path - The path to check for changes.
* @param {string} baseCommit - The first commit hash to compare.
* @param {string?} headCommit - The second commit hash to compare, defaults to HEAD if not provided.
* @param {boolean?} onlyNew - If true, only returns files that are new in the head commit (not present in the base commit).
*/
export async function getChangedFiles(path, baseCommit, headCommit = 'HEAD', onlyNew = false) {
try {
// An empty headCommit diffs baseCommit against the working tree (git omits
// the second ref), which is what the Renovate/pre-commit flows need —
// they inspect uncommitted changes. A ref (commit/tag/tree) diffs the two.
const filter = onlyNew ? ['--diff-filter=A'] : [];
const refs = headCommit ? [baseCommit, headCommit] : [baseCommit];
const { stdout } = await $`git diff --name-only ${filter} ${refs} -- ${path}`;
return stdout.trim().split('\n').filter(Boolean);
} catch (error) {
throw new Error(`Failed to get changed files: ${error.message}`);
}
}
/**
* Builds a matcher for ignore patterns, which are written relative to a
* directory while git reports repo-relative paths. Matches with `dot: true` so
* `**` reaches into `.claude`-style directories.
*
* @param {string} path - The directory the patterns are relative to.
* @param {string[]} ignorePatterns - Patterns relative to `path`.
* @returns {(file: string) => boolean} - True when a repo-relative path is ignored.
*/
export function buildIgnoreMatcher(path, ignorePatterns) {
return pm(
ignorePatterns.map((pattern) => `${path}/${pattern}`),
{ dot: true },
);
}
/**
* Checks if a given path has any changes between two commits
*
* @param {string} path - The path to check for changes.
* @param {string} baseCommit - The first commit hash to compare.
* @param {string} headCommit - The second commit hash to compare.
* @param {string[]} [ignorePatterns] - Optional patterns to ignore, relative to
* `path`. See {@link buildIgnoreMatcher}.
*
* @returns {Promise<boolean>} - A promise that resolves to true if there are changes, false otherwise.
*/
export async function pathHasChanges(path, baseCommit, headCommit, ignorePatterns = []) {
let changedFiles = await getChangedFiles(path, baseCommit, headCommit);
if (ignorePatterns.length > 0) {
const match = buildIgnoreMatcher(path, ignorePatterns);
changedFiles = changedFiles.filter((file) => !match(file));
}
return changedFiles.length > 0;
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.