Skip to content

Commit

Permalink
internal: add clang-format git hook to devcontainer (#2226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Nov 22, 2024
1 parent 99742ce commit 122f539
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM mcr.microsoft.com/devcontainers/javascript-node:22

RUN apt-get update \
&& apt-get install --no-install-recommends -y \
clang-format-15 \
# Clean cache
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/* \
&& mv /usr/bin/clang-format-15 /usr/bin/clang-format
10 changes: 6 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"name": "Node.js Codespace",
"image": "mcr.microsoft.com/devcontainers/javascript-node:22",
"postCreateCommand": "yarn",
"remoteUser": "node"
"name": "SkyMP Codespace",
"build": {
"dockerfile": "Dockerfile"
},
"postCreateCommand": "mkdir -p .git/hooks && ln -sf $(pwd)/misc/git-hooks/pre-commit .git/hooks/pre-commit && chmod +x misc/git-hooks/pre-commit && cd misc/git-hooks && npm install simple-git",
"remoteUser": "node"
}
1 change: 1 addition & 0 deletions misc/git-hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
36 changes: 36 additions & 0 deletions misc/git-hooks/clang-format-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { execSync } = require("child_process");
const simpleGit = require("simple-git");

const extensions = [".cpp", ".h", ".hpp", ".cxx", ".cc"];

(async () => {
try {
const git = simpleGit();
const changedFiles = await git.diff(["--name-only", "--cached"]);

const filesToFormat = changedFiles
.split("\n")
.filter(
(file) =>
extensions.some((ext) => file.endsWith(ext)) && file.trim() !== ""
);

if (filesToFormat.length === 0) {
console.log("No files to format.");
process.exit(0);
}

console.log("Formatting files:");
filesToFormat.forEach((file) => {
console.log(` - ${file}`);
execSync(`clang-format -i ${file}`, { stdio: "inherit" });
execSync(`git add ${file}`);
});

console.log("Formatting completed and changes staged.");
} catch (err) {
console.error("Error during formatting:", err.message);
console.error("Autofix failed. Commit aborted.");
process.exit(1);
}
})();
65 changes: 65 additions & 0 deletions misc/git-hooks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions misc/git-hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"simple-git": "^3.27.0"
}
}
2 changes: 2 additions & 0 deletions misc/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
node misc/git-hooks/clang-format-hook.js

0 comments on commit 122f539

Please sign in to comment.