Skip to content

Commit bfce0d5

Browse files
authored
Add git precommit hook and github actions (#67)
1 parent 54be723 commit bfce0d5

File tree

8 files changed

+181
-8
lines changed

8 files changed

+181
-8
lines changed

.githooks/pre-commit

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
# Get list of files that are staged for commit
4+
KT_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '^android/.*\.kt$' || true)
5+
GRADLE_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '.*\.gradle\.kts$' || true)
6+
CPP_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '^(jni/.*\.(cpp|h|c|hpp|cc)|cli/.*\.(cpp|h|c|hpp|cc)|cpp/.*\.(cpp|h|c|hpp|cc))$' || true)
7+
8+
# Run spotless checks if needed
9+
if [ -n "$KT_FILES" ]; then
10+
echo "Running spotlessKotlinCheck..."
11+
./gradlew spotlessKotlinCheck
12+
if [ $? -ne 0 ]; then
13+
echo "❌ Kotlin files need formatting. Please run './gradlew spotlessKotlinApply' or 'make format' and commit again."
14+
exit 1
15+
fi
16+
fi
17+
18+
if [ -n "$GRADLE_FILES" ]; then
19+
echo "Running spotlessKotlinGradleCheck..."
20+
./gradlew spotlessKotlinGradleCheck
21+
if [ $? -ne 0 ]; then
22+
echo "❌ Gradle files need formatting. Please run './gradlew spotlessKotlinGradleApply' or 'make format' and commit again."
23+
exit 1
24+
fi
25+
fi
26+
27+
if [ -n "$CPP_FILES" ]; then
28+
echo "Running spotlessCppCheck..."
29+
./gradlew spotlessCppCheck
30+
if [ $? -ne 0 ]; then
31+
echo "❌ C++ files need formatting. Please run './gradlew spotlessCppApply' or 'make format' and commit again."
32+
exit 1
33+
fi
34+
fi

.github/workflows/pr-checks.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: PR Checks
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
check-format:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Set up JDK 17
17+
uses: actions/setup-java@v3
18+
with:
19+
java-version: '17'
20+
distribution: 'temurin'
21+
cache: gradle
22+
23+
- name: Install clang-format 14 (native)
24+
run: |
25+
sudo apt update
26+
sudo apt install -y clang-format-14
27+
sudo ln -sf /usr/bin/clang-format-14 /usr/local/bin/clang-format
28+
clang-format --version
29+
30+
- name: Verify clang-format installation
31+
run: |
32+
if ! command -v clang-format >/dev/null; then
33+
echo "❌ clang-format not found"
34+
exit 1
35+
fi
36+
clang-format --version
37+
38+
- name: Grant execute permission for gradlew
39+
run: chmod +x gradlew
40+
41+
- name: Run spotlessCheck
42+
run: |
43+
echo "Running spotlessCheck..."
44+
./gradlew spotlessCheck
45+
if [ $? -ne 0 ]; then
46+
echo "❌ spotlessCheck failed. Please run './gradlew spotlessApply' locally to fix formatting issues."
47+
exit 1
48+
fi
49+
50+
build-test-kotlin:
51+
runs-on: ubuntu-latest
52+
steps:
53+
- uses: actions/checkout@v4
54+
55+
- name: Set up JDK 17
56+
uses: actions/setup-java@v3
57+
with:
58+
java-version: '17'
59+
distribution: 'temurin'
60+
cache: gradle
61+
62+
- name: Grant execute permission for gradlew
63+
run: chmod +x gradlew
64+
65+
- name: Run detekt
66+
run: |
67+
echo "Running detekt..."
68+
./gradlew detekt
69+
if [ $? -ne 0 ]; then
70+
echo "❌ detekt found code style issues. Please fix them locally."
71+
exit 1
72+
fi
73+
74+
- name: Run unit tests
75+
run: |
76+
echo "Running unit tests..."
77+
./gradlew testDebugUnitTest
78+
if [ $? -ne 0 ]; then
79+
echo "❌ Unit tests failed. Please fix the failing tests locally."
80+
exit 1
81+
fi

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,64 @@ With `all` option, it will conduct deep clean including open source components.
230230

231231
WhisperKit Android is currently in the beta stage. We are actively developing the project and welcome contributions from the community.
232232

233+
## Development Setup
234+
235+
### Installing clang-format
236+
237+
The project uses clang-format 14 for C++ code formatting. You'll need to install it based on your operating system:
238+
239+
#### macOS
240+
```bash
241+
# Install LLVM 14
242+
brew install llvm@14
243+
244+
# Create symlink for clang-format
245+
sudo ln -sf /opt/homebrew/opt/llvm@14/bin/clang-format /opt/homebrew/bin/clang-format
246+
```
247+
248+
#### Linux (Ubuntu/Debian)
249+
```bash
250+
sudo apt update
251+
sudo apt install -y clang-format-14
252+
sudo ln -sf /usr/bin/clang-format-14 /usr/local/bin/clang-format
253+
```
254+
255+
Verify the installation:
256+
```bash
257+
clang-format --version
258+
```
259+
260+
To check C++ code formatting:
261+
```bash
262+
./gradlew spotlessCppCheck
263+
```
264+
265+
## Git Hooks
266+
267+
This project uses Git hooks to maintain code quality. These hooks help ensure consistent code formatting and quality standards.
268+
269+
### Setup
270+
271+
To use the Git hooks, run the following command in your repository root:
272+
273+
```bash
274+
git config core.hooksPath .githooks
275+
```
276+
277+
### Available Hooks
278+
279+
#### pre-commit
280+
- Runs `spotlessApply` to automatically fix code formatting issues
281+
- If formatting fixes are applied, they are automatically staged
282+
- The commit will be blocked if `spotlessApply` fails
283+
284+
### Troubleshooting
285+
286+
If you need to bypass the hooks temporarily (not recommended), you can use:
287+
```bash
288+
git commit --no-verify
289+
```
290+
233291
# License
234292

235293
- We release WhisperKit Android under [MIT License](LICENSE).

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ spotless {
3737
"cpp/**/*.cpp", "cpp/**/*.h", "cpp/**/*.c", "cpp/**/*.hpp", "cpp/**/*.cc",
3838
)
3939
targetExclude("**/build/**", "**/external/**")
40-
clangFormat("20.1.5")
40+
clangFormat("14.0.6")
4141
}
4242
}

cpp/src/Models/TextDecoder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ bool is_exact_match_for_separate_kv_cache_no_alignment_heads(const tflite::Model
101101
}
102102

103103
/* helper functions to calculate the number of inputs and outputs for a given number of layers */
104-
auto calculate_num_inputs_for_variant_with_layers = [=](const int num_layers) -> auto {
104+
auto calculate_num_inputs_for_variant_with_layers = [=](const int num_layers) -> auto{
105105
return num_shared_inputs + num_layers * kv_factor;
106106
};
107107

108-
auto calculate_num_outputs_for_variant_with_layers = [=](const int num_layers) -> auto {
108+
auto calculate_num_outputs_for_variant_with_layers = [=](const int num_layers) -> auto{
109109
return kv_factor * num_layers + 1;
110110
};
111111

112-
auto output_names_for_variant_with_layers = [=](const int num_layers) -> auto {
112+
auto output_names_for_variant_with_layers = [=](const int num_layers) -> auto{
113113
std::unordered_set<std::string> output_names;
114114
output_names.insert(std::string("logits"));
115115
for (int i = 0; i < num_layers; ++i) {
@@ -119,7 +119,7 @@ bool is_exact_match_for_separate_kv_cache_no_alignment_heads(const tflite::Model
119119
return output_names;
120120
};
121121

122-
auto input_names_for_variant_with_layers = [](const int num_layers) -> auto {
122+
auto input_names_for_variant_with_layers = [](const int num_layers) -> auto{
123123
std::unordered_set<std::string> input_names;
124124
input_names.insert(std::string("x"));
125125
input_names.insert(std::string("index"));

cpp/src/Text/post_proc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define LOGITS_TO_NEG_INF(start, end) \
66
for (auto iter = (start); iter != (end); iter++) *iter = -1e9;
77

8-
#define DEC_2_ROUND(x) (round((x) * 100.0) / 100.0)
8+
#define DEC_2_ROUND(x) (round((x)*100.0) / 100.0)
99

1010
using namespace std;
1111
using json = nlohmann::json;

cpp/src/Text/post_proc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ constexpr const uint32_t SAMPLE_BEGIN = 1;
1414
class PostProcModel : public MODEL_SUPER_CLASS {
1515
public:
1616
PostProcModel(Tokenizer* tokenizer, bool timestamp_text = false);
17-
virtual ~PostProcModel() {};
17+
virtual ~PostProcModel(){};
1818

1919
bool initialize(bool debug = false);
2020
virtual void invoke(bool measure_time = false);

cpp/src/WhisperKitConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "WhisperKitPipeline.hpp"
55
#include "backend_class.hpp"
66

7-
whisperkit_configuration_t::whisperkit_configuration_t() {};
7+
whisperkit_configuration_t::whisperkit_configuration_t(){};
88

99
void whisperkit_configuration_t::set_audio_encoder(const char* audio_encoder) noexcept {
1010
this->audio_encoder = audio_encoder;

0 commit comments

Comments
 (0)