From 2e72135acb0aa0defa5dc31f01174f4e6c0c09cc Mon Sep 17 00:00:00 2001
From: pierrick_cbl <pchebel@gmail.com>
Date: Tue, 8 Feb 2022 11:45:04 +0100
Subject: [PATCH] feat: users can have their own Git repo that will be
 persisted

Their '.git' and '.gitignore' are copied and renamed before the commit/push on the Gitea repo so that they can be staged.
If a repository already exists, when the code is pulled at the start, the user's git files will also be renamed back.
---
 .../deadlock-extension/src/core/gitMission.ts |  5 ++
 .../deadlock-extension/src/recorder/index.ts  |  5 +-
 .../deadlock-extension/src/recorder/utils.ts  | 70 +++++++++++++++++++
 3 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/deadlock-plugins/deadlock-extension/src/core/gitMission.ts b/deadlock-plugins/deadlock-extension/src/core/gitMission.ts
index 99b2c63e..050b1403 100644
--- a/deadlock-plugins/deadlock-extension/src/core/gitMission.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/gitMission.ts
@@ -34,6 +34,11 @@ export default class GitMission {
 
     }
 
+    get author() {
+
+        return this.userConfig.getUsername();
+    }
+
     async init() {
         try {
 
diff --git a/deadlock-plugins/deadlock-extension/src/recorder/index.ts b/deadlock-plugins/deadlock-extension/src/recorder/index.ts
index 0c3b2f4f..9a4b4f0b 100644
--- a/deadlock-plugins/deadlock-extension/src/recorder/index.ts
+++ b/deadlock-plugins/deadlock-extension/src/recorder/index.ts
@@ -2,7 +2,7 @@ import CommandRecorder from './command-recorder';
 import GitMission from '../core/gitMission';
 import UserConfigNode from './userConfigNode';
 import { PROJECT_SRC_PATH, PROJECT_THEIA_PATH } from '../core/config';
-import { copyProjectSources, clearFilesExceptGit, log, error } from './utils';
+import { copyProjectSources, clearFilesExceptGit, log, error, renameTempToUserGitFiles } from './utils';
 import UserConfig from '../core/userConfig';
 
 
@@ -13,7 +13,10 @@ export default class Recorder {
 
         if (!userConfig.isProfessor()) {
             await copyProjectSources(PROJECT_SRC_PATH, PROJECT_THEIA_PATH, ['.git/']);
+            
             if (gitMission) {
+                renameTempToUserGitFiles(PROJECT_THEIA_PATH, gitMission.author);
+
                 log('Starting CommandRecorder..');
                 new CommandRecorder(gitMission).run();
             } else {
diff --git a/deadlock-plugins/deadlock-extension/src/recorder/utils.ts b/deadlock-plugins/deadlock-extension/src/recorder/utils.ts
index b5eff5ae..74331fbc 100644
--- a/deadlock-plugins/deadlock-extension/src/recorder/utils.ts
+++ b/deadlock-plugins/deadlock-extension/src/recorder/utils.ts
@@ -2,6 +2,7 @@ import GitMission from '../core/gitMission';
 import { PROJECT_SRC_PATH, PROJECT_THEIA_PATH } from '../core/config';
 import { format } from 'date-fns';
 import { execSync } from "child_process";
+import { existsSync, renameSync, copyFileSync, PathLike } from 'fs';
 
 const util = require('util');
 const unlink = util.promisify(require('fs').unlink);
@@ -54,6 +55,74 @@ export async function clearFilesExceptGit(path) {
     });
 }
 
+function renameIfExistsSync(srcPath: PathLike, destPath: PathLike) {
+
+    if (existsSync(srcPath)) {
+        renameSync(srcPath, destPath);
+    } else {
+        log(`Renaming : No ${srcPath} found`);
+    }
+}
+
+function copyFileIfExistsSync(srcPath: PathLike, destPath: PathLike) {
+
+    if (existsSync(srcPath)) {
+        copyFileSync(srcPath, destPath);
+    } else {
+        log(`Copying file: No ${srcPath} found`);
+    }
+}
+
+function copyFolderIfExistsSync(srcPath: PathLike, destPath: PathLike) {
+
+    if (existsSync(srcPath)) {
+        execSync(`rsync -r ${srcPath}/* ${destPath}`);
+    } else {
+        log(`Copying folder: No ${srcPath} found`);
+    }
+}
+
+
+/**
+ * 
+ * Rename temporary git files back to actual git files.
+ * 
+ * @param path Path of the folder containing user's temporary git project files.
+ * @param userId Identifier used to create the temporary files name
+ */
+export function renameTempToUserGitFiles(path: string, userId: string) {
+    
+    renameIfExistsSync(
+        Path.join(path,`user-git-${userId}`),
+        Path.join(path,'.git')
+    );
+    renameIfExistsSync(
+        Path.join(path, `user-gitignore-${userId}`),
+        Path.join(path, '.gitignore')
+    );
+}
+
+/**
+ * 
+ * Copy user's git project files and paste them with renamed filenames so that
+ * they can be saved by the recorder.
+ * 
+ * @param srcPath Path of the folder containing user's git project files to be saved.
+ * @param destPath Path where the user's git project files should be pasted.
+ * @param userId Identifier used to create the temporary files name
+ */
+function copyGitUserFiles(srcPath: string, destPath: string, userId: string) {
+
+    copyFolderIfExistsSync(
+        Path.join(srcPath,'.git'),
+        Path.join(destPath,`user-git-${userId}`)
+    );
+    copyFileIfExistsSync(
+        Path.join(srcPath, '.gitignore'),
+        Path.join(destPath, `user-gitignore-${userId}`)
+    );
+}
+
 export enum CommitFrom {
     Run = 'Run',
     Auto = 'Auto'
@@ -64,6 +133,7 @@ export async function commitAndPushCode(gitMission: GitMission, from: CommitFrom
         log('Commit & push');
         await clearFilesExceptGit(PROJECT_SRC_PATH);
 
+        copyGitUserFiles(PROJECT_THEIA_PATH, PROJECT_SRC_PATH, gitMission.author);
         execSync(`rsync -r --exclude .git --exclude npm --exclude target ${PROJECT_THEIA_PATH}/* ${PROJECT_SRC_PATH} && cp ${Path.join(__dirname, '.gitignore')} ${PROJECT_SRC_PATH} && chown -R root:root /project`);
 
         const currentDate = format(new Date(), "HH'H'mm'_'dd/LL/y");
-- 
GitLab