From 931cbfe4af6f5d19b443a8ddce425ae0e6e1e207 Mon Sep 17 00:00:00 2001
From: Vincent Rischmann <vincent@rischmann.fr>
Date: Mon, 12 Jul 2021 21:37:39 +0200
Subject: [PATCH] add the ability to disable core.autocrlf

---
 action.yml                 | 3 +++
 src/git-command-manager.ts | 9 +++++++++
 src/git-source-provider.ts | 7 +++++++
 src/git-source-settings.ts | 5 +++++
 src/input-helper.ts        | 4 ++++
 5 files changed, 28 insertions(+)

diff --git a/action.yml b/action.yml
index 91d3982..63bedd7 100644
--- a/action.yml
+++ b/action.yml
@@ -68,6 +68,9 @@ inputs:
       When the `ssh-key` input is not provided, SSH URLs beginning with `git@github.com:` are
       converted to HTTPS.
     default: false
+  autocrlf:
+    description: 'Whether to disable core.autocrlf'
+    default: true
 runs:
   using: node12
   main: dist/index.js
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts
index 409a161..e853841 100644
--- a/src/git-command-manager.ts
+++ b/src/git-command-manager.ts
@@ -44,6 +44,7 @@ export interface IGitCommandManager {
   tryClean(): Promise<boolean>
   tryConfigUnset(configKey: string, globalConfig?: boolean): Promise<boolean>
   tryDisableAutomaticGarbageCollection(): Promise<boolean>
+  tryDisableAutocrlf(): Promise<boolean>
   tryGetFetchUrl(): Promise<string>
   tryReset(): Promise<boolean>
 }
@@ -358,6 +359,14 @@ class GitCommandManager {
     return output.exitCode === 0
   }
 
+  async tryDisableAutocrlf(): Promise<boolean> {
+    const output = await this.execGit(
+      ['config', '--local', 'core.autocrlf', 'false'],
+      true
+    )
+    return output.exitCode === 0
+  }
+
   async tryGetFetchUrl(): Promise<string> {
     const output = await this.execGit(
       ['config', '--local', '--get', 'remote.origin.url'],
diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index 42a12e0..c28a33e 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -96,6 +96,13 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
   }
   core.endGroup()
 
+  if (!settings.autocrlf) {
+    core.startGroup('Disabling autocrlf')
+    if (!(await git.tryDisableAutocrlf())) {
+      throw new Error("Unable to disable autocrlf")
+    }
+  }
+
   const authHelper = gitAuthHelper.createAuthHelper(git, settings)
   try {
     // Configure auth
diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts
index 2786222..619eaf7 100644
--- a/src/git-source-settings.ts
+++ b/src/git-source-settings.ts
@@ -73,4 +73,9 @@ export interface IGitSourceSettings {
    * Indicates whether to persist the credentials on disk to enable scripting authenticated git commands
    */
   persistCredentials: boolean
+
+  /**
+   * Indicates whether to enable autocrlf or not when checking out a repository
+   */
+  autocrlf: boolean
 }
diff --git a/src/input-helper.ts b/src/input-helper.ts
index 4c05d6e..0e97db0 100644
--- a/src/input-helper.ts
+++ b/src/input-helper.ts
@@ -118,5 +118,9 @@ export function getInputs(): IGitSourceSettings {
   result.persistCredentials =
     (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'
 
+  // Autocrlf
+  result.autocrlf = (core.getInput('autocrlf') || 'true').toUpperCase() === 'TRUE'
+  core.debug(`autocrlf = ${result.autocrlf}`)
+
   return result
 }