@@ -3,7 +3,7 @@ import os from "node:os";
33import path from "node:path" ;
44import crypto from "node:crypto" ;
55import { temporaryDirectory } from "./constants.ts" ;
6- import spawn from "nano-spawn" ;
6+ import spawn , { SubprocessError } from "nano-spawn" ;
77import assert from "node:assert/strict" ;
88
99export async function createTemporaryDirectory ( ) {
@@ -42,21 +42,51 @@ export async function readFile(file: string) {
4242
4343export const unique = < T > ( array : T [ ] ) : T [ ] => [ ...new Set ( array ) ] ;
4444
45- export const commitChanges = async ( directory : string , message : string ) => {
46- await spawn ( "git" , [ "add" , "." ] , { cwd : directory } ) ;
47- const { stdout } = await spawn (
45+ async function gitAddFiles ( cwd : string ) {
46+ try {
47+ await spawn ( "git" , [ "add" , "." ] , { cwd } ) ;
48+ return ;
49+ } catch ( error ) {
50+ if ( error instanceof SubprocessError ) {
51+ const { stderr } = error ;
52+ const match = stderr . match (
53+ / e r r o r : o p e n \( " (?< filename > .* ) " \) : F i l e n a m e t o o l o n g / ,
54+ ) ;
55+ const filename = match ?. groups ?. filename ;
56+ if ( filename ) {
57+ console . log ( `File '${ filename } ' can't be added, ignored.` ) ;
58+ await fs . rm ( path . join ( cwd , filename ) , { force : true } ) ;
59+ return gitAddFiles ( cwd ) ;
60+ }
61+ }
62+
63+ throw error ;
64+ }
65+ }
66+
67+ export const commitChanges = async (
68+ directory : string ,
69+ message : string ,
70+ ignoreFilesCannotAdded ?: boolean ,
71+ ) => {
72+ await fs . rm ( path . join ( directory , ".gitattributes" ) , { force : true } ) ;
73+ await spawn ( "git" , [ "config" , "set" , "core.autocrlf" , "false" ] , {
74+ cwd : directory ,
75+ } ) ;
76+
77+ if ( ignoreFilesCannotAdded ) {
78+ await gitAddFiles ( directory ) ;
79+ } else {
80+ await spawn ( "git" , [ "add" , "." ] , { cwd : directory } ) ;
81+ }
82+
83+ await spawn (
4884 "git" ,
4985 [ "commit" , "--allow-empty" , "--no-verify" , "-m" , message ] ,
5086 { cwd : directory } ,
5187 ) ;
5288
53- const match = stdout . match ( / ^ \[ (?< commitHash > [ a - f 0 - 9 ] { 40 } ) [ a - f 0 - 9 ] { 7 , 8 } ] / ) ;
54- if ( ! match ?. groups ! . commitHash ) {
55- throw new Error ( `Unexpected commit hash '${ stdout } '` ) ;
56- }
57-
58- const commitHash = match . groups . commitHash ;
59- return commitHash ;
89+ return getCommitHash ( directory ) ;
6090} ;
6191
6292export async function getCommitHash ( directory : string ) {
0 commit comments