MCPcopy Index your code
hub / github.com/changesets/changesets / getCommitsThatAddFiles

Function getCommitsThatAddFiles

packages/git/src/index.ts:64–137  ·  view source on GitHub ↗
(
  gitPaths: string[],
  { cwd, short = false }: { cwd: string; short?: boolean }
)

Source from the content-addressed store, hash-verified

62 * @param options - `cwd` and `short`
63 */
64export async function getCommitsThatAddFiles(
65 gitPaths: string[],
66 { cwd, short = false }: { cwd: string; short?: boolean }
67): Promise<(string | undefined)[]> {
68 // Maps gitPath to commit SHA
69 const map = new Map<string, string>();
70
71 // Paths we haven't completed processing on yet
72 let remaining = gitPaths;
73
74 do {
75 // Fetch commit information for all paths we don't have yet
76 const commitInfos = await Promise.all(
77 remaining.map(async (gitPath: string) => {
78 const [commitSha, parentSha] = (
79 await spawn(
80 "git",
81 [
82 "log",
83 "--diff-filter=A",
84 "--max-count=1",
85 short ? "--pretty=format:%h:%p" : "--pretty=format:%H:%p",
86 gitPath,
87 ],
88 { cwd }
89 )
90 ).stdout
91 .toString()
92 .split(":");
93 return { path: gitPath, commitSha, parentSha };
94 })
95 );
96
97 // To collect commits without parents (usually because they're absent from
98 // a shallow clone).
99 let commitsWithMissingParents = [];
100
101 for (const info of commitInfos) {
102 if (info.commitSha) {
103 if (info.parentSha) {
104 // We have found the parent of the commit that added the file.
105 // Therefore we know that the commit is legitimate and isn't simply the boundary of a shallow clone.
106 map.set(info.path, info.commitSha);
107 } else {
108 commitsWithMissingParents.push(info);
109 }
110 } else {
111 // No commit for this file, which indicates it doesn't exist.
112 }
113 }
114
115 if (commitsWithMissingParents.length === 0) {
116 break;
117 }
118
119 // The commits we've found may be the real commits or they may be the boundary of
120 // a shallow clone.
121

Callers 2

getReleaseLineFunction · 0.90
index.test.tsFile · 0.85

Calls 2

isRepoShallowFunction · 0.85
deepenCloneByFunction · 0.85

Tested by

no test coverage detected