hyiso / lint_staged

Run linters on git staged files for your Flutter and Dart projects. Inspired by JavaScript lint-staged library
https://pub.dev/packages/lint_staged
Apache License 2.0
6 stars 2 forks source link

Does not work for partially staged files #8

Closed bugnano closed 10 months ago

bugnano commented 10 months ago

If I partially stage a file (that is I only stage some lines of that file), the lint_staged command fails at the:

β ‹ Restore unstaged changes...

stage, more specifically the command:

git - git apply -v --whitespace=nowarn --recount --unidiff-zero .git/lint_staged_unstaged.patch
hyiso commented 10 months ago

Thanks for your report. You can see more verbose logging by adding VERBOSE=true to dart run lint_staged:

VERBOSE=true dart run lint_staged

This will show all the processing information.

bugnano commented 10 months ago

yes, that's exactly what I did in order to tell you exactly which git command failed😁

hyiso commented 10 months ago

yes, that's exactly what I did in order to tell you exactly which git command failed😁

Could you paste all the verbose logging? It's not clear what happened without more information

bugnano commented 10 months ago

Here's the full output:

I] fri@talkjs ~/talkjs-flutter (chore/autoformatter)> VERBOSE=true dart run lint_staged
lit_staged:git - git log -1 --pretty=%B
Added pre-commit hook in Dart
lit_staged:git - git diff --name-only -z --diff-filter=ACMR --staged
example/push_notifications/lib/main.dart
lit_staged:git - Staged files: [example/push_notifications/lib/main.dart]
lint_staged:config - Found config: {**/*.dart: dart format --fix && dart fix --apply}
lint_staged:group - **/*.dart matched files: [example/push_notifications/lib/main.dart]
β ‹ Preparing lint_staged...
lit_staged:git - git status -z
M example/push_notifications/lib/firebase_options.dartMM example/push_notifications/lib/main.dart M lib/assets/index.html M lib/src/chatbox.dart M lib/src/conversationlist.dart M pubspec.lock M pubspec.yaml?? example/push_notifications/android/app/google-services.json?? push_notifications.txt?? todo.txt
lit_staged:git - Found partially staged files: [example/push_notifications/lib/main.dart]
lit_staged:git - git diff --binary --unified=0 --no-color --no-ext-diff --src-prefix=a/ --dst-prefix=b/ --patch --submodule=short --output .git/lint_staged_unstaged.patch -- example/push_notifications/lib/main.dart
lit_staged:git - git ls-files --deleted
lit_staged:git - Deleted files: []
lit_staged:git - git stash create
4ce6266d7fa0d4492a5d4cae5296055359414c08
lit_staged:git - git stash store --quiet -m lint_staged automatic backup 4ce6266d7fa0d4492a5d4cae5296055359414c08
βœ” Prepared lint_staged                    515ms
β ‹ Hide unstaged changes...
lit_staged:git - git checkout --force -- example/push_notifications/lib/main.dart
βœ” Hide unstaged changes                   18ms
β ‹ Run tasks for staged files...
lint_staged:run - dart format --fix example/push_notifications/lib/main.dart
Formatted example/push_notifications/lib/main.dart
Formatted 1 file (1 changed) in 0.39 seconds.
lint_staged:run - dart fix --apply example/push_notifications/lib/main.dart
Computing fixes in main.dart...
Nothing to fix!
βœ” Run tasks for staged files              25402ms
β ‹ Apply modifications...
lit_staged:git - git add -- example/push_notifications/lib/main.dart
lit_staged:git - git diff --name-only -z --diff-filter=ACMR --staged
example/push_notifications/lib/main.dart
lit_staged:git - Staged files: [example/push_notifications/lib/main.dart]
βœ” Apply modifications                     15ms
β ‹ Restore unstaged changes...
lit_staged:git - git apply -v --whitespace=nowarn --recount --unidiff-zero .git/lint_staged_unstaged.patch
Controllo della patch example/push_notifications/lib/main.dart in corso...
error: durante la ricerca di:
    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

error: patch non riuscita: example/push_notifications/lib/main.dart:37
error: example/push_notifications/lib/main.dart: la patch non si applica correttamente
lint_staged:workflow - Error while restoring changes:
lint_staged:workflow - Retrying with 3-way merge
lit_staged:git - git apply -v --whitespace=nowarn --recount --unidiff-zero --3way .git/lint_staged_unstaged.patch
Controllo della patch example/push_notifications/lib/main.dart in corso...
error: durante la ricerca di:
    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

error: patch non riuscita: example/push_notifications/lib/main.dart:37
Falling back to three-way merge...
Patch applicata a 'example/push_notifications/lib/main.dart' con conflitti.
Patch example/push_notifications/lib/main.dart applicata correttamente.
U example/push_notifications/lib/main.dart
lint_staged:workflow - Error while restoring unstaged changes using 3-way merge:
lint_staged:workflow - ProcessException: git apply -v --whitespace=nowarn --recount --unidiff-zero --3way .git/lint_staged_unstaged.patch
Controllo della patch example/push_notifications/lib/main.dart in corso...
error: durante la ricerca di:
    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

error: patch non riuscita: example/push_notifications/lib/main.dart:37
Falling back to three-way merge...
Patch applicata a 'example/push_notifications/lib/main.dart' con conflitti.
Patch example/push_notifications/lib/main.dart applicata correttamente.
U example/push_notifications/lib/main.dart
  Command: git apply -v --whitespace=nowarn --recount --unidiff-zero --3way .git/lint_staged_unstaged.patch
lint_staged:workflow - #0      Git.run (package:lint_staged/src/git.dart:55:7)
<asynchronous suspension>
#1      Workflow.resotreUnstagedChanges (package:lint_staged/src/workflow.dart:232:9)
<asynchronous suspension>
#2      runAll (package:lint_staged/src/run.dart:122:5)
<asynchronous suspension>
#3      lintStaged (package:lint_staged/lint_staged.dart:26:17)
<asynchronous suspension>
#4      main (file:///home/fri/.pub-cache/hosted/pub.dev/lint_staged-0.4.0/bin/lint_staged.dart:25:18)
<asynchronous suspension>

βœ” Restore unstaged changes                26ms
β ‹ Revert because of errors...
lit_staged:git - git reset --hard HEAD
HEAD ora si trova a b764ed9 Added pre-commit hook in Dart
lit_staged:git - git stash list
stash@{0}: lint_staged automatic backup
stash@{1}: WIP on navigation-delegate: a9d0cb5 Fix default return of CANCEL
lit_staged:git - git stash apply --quiet --index 0
βœ” Revert because of errors                325ms
β ‹ Cleanup temporary files...
lit_staged:git - git stash list
stash@{0}: lint_staged automatic backup
stash@{1}: WIP on navigation-delegate: a9d0cb5 Fix default return of CANCEL
lit_staged:git - git stash drop --quiet 0
βœ” Cleanup temporary files                 30ms
βœ— lint_staged failed due to a git error.
  Any lost modifications can be restored from a git stash:
    > git stash list
    stash@{0}: automatic lint_staged backup
    > git stash apply --index stash@{0}

The problem is that this line:

    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

belongs to the file that is found in the previous git commit, but there's an unstaged change that changed this line to something else.

I don't know if we can do something about it, given that the same error occurs when running the javascript version of lint_staged.

hyiso commented 10 months ago

Here's the full output:

I] fri@talkjs ~/talkjs-flutter (chore/autoformatter)> VERBOSE=true dart run lint_staged
lit_staged:git - git log -1 --pretty=%B
Added pre-commit hook in Dart
lit_staged:git - git diff --name-only -z --diff-filter=ACMR --staged
example/push_notifications/lib/main.dart
lit_staged:git - Staged files: [example/push_notifications/lib/main.dart]
lint_staged:config - Found config: {**/*.dart: dart format --fix && dart fix --apply}
lint_staged:group - **/*.dart matched files: [example/push_notifications/lib/main.dart]
β ‹ Preparing lint_staged...
lit_staged:git - git status -z
M example/push_notifications/lib/firebase_options.dartMM example/push_notifications/lib/main.dart M lib/assets/index.html M lib/src/chatbox.dart M lib/src/conversationlist.dart M pubspec.lock M pubspec.yaml?? example/push_notifications/android/app/google-services.json?? push_notifications.txt?? todo.txt
lit_staged:git - Found partially staged files: [example/push_notifications/lib/main.dart]
lit_staged:git - git diff --binary --unified=0 --no-color --no-ext-diff --src-prefix=a/ --dst-prefix=b/ --patch --submodule=short --output .git/lint_staged_unstaged.patch -- example/push_notifications/lib/main.dart
lit_staged:git - git ls-files --deleted
lit_staged:git - Deleted files: []
lit_staged:git - git stash create
4ce6266d7fa0d4492a5d4cae5296055359414c08
lit_staged:git - git stash store --quiet -m lint_staged automatic backup 4ce6266d7fa0d4492a5d4cae5296055359414c08
βœ” Prepared lint_staged                    515ms
β ‹ Hide unstaged changes...
lit_staged:git - git checkout --force -- example/push_notifications/lib/main.dart
βœ” Hide unstaged changes                   18ms
β ‹ Run tasks for staged files...
lint_staged:run - dart format --fix example/push_notifications/lib/main.dart
Formatted example/push_notifications/lib/main.dart
Formatted 1 file (1 changed) in 0.39 seconds.
lint_staged:run - dart fix --apply example/push_notifications/lib/main.dart
Computing fixes in main.dart...
Nothing to fix!
βœ” Run tasks for staged files              25402ms
β ‹ Apply modifications...
lit_staged:git - git add -- example/push_notifications/lib/main.dart
lit_staged:git - git diff --name-only -z --diff-filter=ACMR --staged
example/push_notifications/lib/main.dart
lit_staged:git - Staged files: [example/push_notifications/lib/main.dart]
βœ” Apply modifications                     15ms
β ‹ Restore unstaged changes...
lit_staged:git - git apply -v --whitespace=nowarn --recount --unidiff-zero .git/lint_staged_unstaged.patch
Controllo della patch example/push_notifications/lib/main.dart in corso...
error: durante la ricerca di:
    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

error: patch non riuscita: example/push_notifications/lib/main.dart:37
error: example/push_notifications/lib/main.dart: la patch non si applica correttamente
lint_staged:workflow - Error while restoring changes:
lint_staged:workflow - Retrying with 3-way merge
lit_staged:git - git apply -v --whitespace=nowarn --recount --unidiff-zero --3way .git/lint_staged_unstaged.patch
Controllo della patch example/push_notifications/lib/main.dart in corso...
error: durante la ricerca di:
    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

error: patch non riuscita: example/push_notifications/lib/main.dart:37
Falling back to three-way merge...
Patch applicata a 'example/push_notifications/lib/main.dart' con conflitti.
Patch example/push_notifications/lib/main.dart applicata correttamente.
U example/push_notifications/lib/main.dart
lint_staged:workflow - Error while restoring unstaged changes using 3-way merge:
lint_staged:workflow - ProcessException: git apply -v --whitespace=nowarn --recount --unidiff-zero --3way .git/lint_staged_unstaged.patch
Controllo della patch example/push_notifications/lib/main.dart in corso...
error: durante la ricerca di:
    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

error: patch non riuscita: example/push_notifications/lib/main.dart:37
Falling back to three-way merge...
Patch applicata a 'example/push_notifications/lib/main.dart' con conflitti.
Patch example/push_notifications/lib/main.dart applicata correttamente.
U example/push_notifications/lib/main.dart
  Command: git apply -v --whitespace=nowarn --recount --unidiff-zero --3way .git/lint_staged_unstaged.patch
lint_staged:workflow - #0      Git.run (package:lint_staged/src/git.dart:55:7)
<asynchronous suspension>
#1      Workflow.resotreUnstagedChanges (package:lint_staged/src/workflow.dart:232:9)
<asynchronous suspension>
#2      runAll (package:lint_staged/src/run.dart:122:5)
<asynchronous suspension>
#3      lintStaged (package:lint_staged/lint_staged.dart:26:17)
<asynchronous suspension>
#4      main (file:///home/fri/.pub-cache/hosted/pub.dev/lint_staged-0.4.0/bin/lint_staged.dart:25:18)
<asynchronous suspension>

βœ” Restore unstaged changes                26ms
β ‹ Revert because of errors...
lit_staged:git - git reset --hard HEAD
HEAD ora si trova a b764ed9 Added pre-commit hook in Dart
lit_staged:git - git stash list
stash@{0}: lint_staged automatic backup
stash@{1}: WIP on navigation-delegate: a9d0cb5 Fix default return of CANCEL
lit_staged:git - git stash apply --quiet --index 0
βœ” Revert because of errors                325ms
β ‹ Cleanup temporary files...
lit_staged:git - git stash list
stash@{0}: lint_staged automatic backup
stash@{1}: WIP on navigation-delegate: a9d0cb5 Fix default return of CANCEL
lit_staged:git - git stash drop --quiet 0
βœ” Cleanup temporary files                 30ms
βœ— lint_staged failed due to a git error.
  Any lost modifications can be restored from a git stash:
    > git stash list
    stash@{0}: automatic lint_staged backup
    > git stash apply --index stash@{0}

The problem is that this line:

    final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

belongs to the file that is found in the previous git commit, but there's an unstaged change that changed this line to something else.

I don't know if we can do something about it, given that the same error occurs when running the javascript version of lint_staged.

From the verbose log output, it tells that when lint_staged restore the unstaged changes to partially staged file

example/push_notifications/lib/main.dart

, there is conflict at this line

final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);

Neither canlint_staged resolve git conflict automatically, nor should it discard unstaged changes, so it stopped at this step.

hyiso commented 10 months ago

This seems to be a bug/feature of git, and lint_staged itself runs as expected, so I gonna close this issue