Skip to content

Commit 5328054

Browse files
committed
Update
1 parent e6efc0f commit 5328054

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

src/git.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ export const commitChangesFromRepo = async ({
1717
}: CommitChangesFromRepoArgs): Promise<CommitFilesResult> => {
1818
const ref = base?.commit ?? "HEAD";
1919
const cwd = path.resolve(workingDirectory);
20-
const repoRoot = recursivelyFindRoot
21-
? ((await findGitRoot(cwd)) ?? cwd)
22-
: cwd;
20+
const repoRoot = recursivelyFindRoot ? await findGitRoot(cwd) : cwd;
2321

24-
const refOid = await getOidForRef(ref, repoRoot);
22+
const refOid = await getOidForRef(repoRoot, ref);
2523
if (!refOid) {
2624
throw new Error(`Could not determine oid for ref ${ref}`);
2725
}
@@ -74,8 +72,8 @@ export async function getFileChanges(
7472
`Unexpected symlink at ${filePath}, GitHub API only supports files and directories. You may need to add this file to .gitignore`,
7573
);
7674
}
77-
// Test executable files
78-
if (stat.mode & 0o111) {
75+
const isFileExecutable = (stat.mode & 0o111) !== 0;
76+
if (isFileExecutable) {
7977
throw new Error(
8078
`Unexpected executable file at ${filePath}, GitHub API only supports non-executable files and directories. You may need to add this file to .gitignore`,
8179
);
@@ -141,13 +139,10 @@ export async function getFileChanges(
141139
additions.sort((a, b) => (a.path > b.path ? 1 : -1));
142140
deletions.sort((a, b) => (a.path > b.path ? 1 : -1));
143141

144-
additions.sort((a, b) => (a.path > b.path ? 1 : -1));
145-
deletions.sort((a, b) => (a.path > b.path ? 1 : -1));
146-
147142
return { additions, deletions };
148143
}
149144

150-
async function getOidForRef(ref: string, cwd: string): Promise<string | null> {
145+
async function getOidForRef(cwd: string, ref: string): Promise<string | null> {
151146
try {
152147
const { stdout } = await exec("git", ["rev-parse", ref], {
153148
throwOnError: true,
@@ -159,20 +154,14 @@ async function getOidForRef(ref: string, cwd: string): Promise<string | null> {
159154
}
160155
}
161156

162-
async function findGitRoot(start: string): Promise<string | null> {
163-
let current = start;
164-
while (true) {
165-
try {
166-
const stat = await fs.stat(path.join(current, ".git"));
167-
if (stat.isDirectory()) {
168-
return current;
169-
}
170-
} catch {
171-
// Ignore errors and continue searching
172-
}
173-
const parent = path.dirname(current);
174-
if (parent === current) break;
175-
current = parent;
157+
async function findGitRoot(cwd: string): Promise<string> {
158+
try {
159+
const { stdout } = await exec("git", ["rev-parse", "--git-dir"], {
160+
throwOnError: true,
161+
nodeOptions: { cwd },
162+
});
163+
return path.resolve(cwd, stdout.trim());
164+
} catch {
165+
return cwd;
176166
}
177-
return null;
178167
}

0 commit comments

Comments
 (0)