Instagram / Instagram/LibCST

codemod unified-diff generates not applicable patch

Open
#788 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

Hello,

I've written a codemod which rewrites parameters in given function call. Details are not important. Also, I work in a project where we use Perforce as a source control. As a result I can't use default libcst.tool mode where files are just modified in place as I need to know upfront which files need modification. I've decided to use `-u` option which generates unified diff. I can preprocess such diff to extract files which require modification, open them and the apply the diff. Problem is the generated diff can't be applied when file is "short".

### The problem

Generate diff:
```bash
$ python -m libcst.tool codemod transform_call.ConvertListsToListOfDicts commands/tests/integration_test/file.py -u=3 > /tmp/x.diff
Calculating full-repo metadata...
Executing codemod...
reformatted -

All done! ✨ 🍰 ✨
1 file reformatted.
Finished codemodding 1 files!
- Transformed 1 files successfully.
- Skipped 0 files.
- Failed to codemod 0 files.
- 0 warnings were generated.
```
Apply diff:
```bash
$ patch commands/tests/integration_test/file.py /tmp/x.diff
patching file commands/tests/integration_test/file.py
Hunk #1 FAILED at 3.
1 out of 1 hunk FAILED -- saving rejects to file commands/tests/integration_test/file.py.rej
```

I've compared what is the difference between diff generated by libcst.tool (`x.diff`) an real diff (`y.diff`):
```bash
$ python -m libcst.tool codemod transform_call.ConvertListsToListOfDicts commands/tests/integration_test/file.py
```
```bash
$ diff commands/tests/integration_test/file.py.backup commands/tests/integration_test/file.py -u > /tmp/y.diff
```
```bash
$ diff /tmp/x.diff /tmp/y.diff
1,3c1,3
< --- /home/bill/python_codemods/commands/tests/integration_test/file.py
< +++ /home/bill/python_codemods/commands/tests/integration_test/file.py
< @@ -5,8 +5,15 @@
---
> --- commands/tests/integration_test/file.py.backup 2022-09-27 22:59:06.473918900 +0200
> +++ commands/tests/integration_test/file.py 2022-09-28 01:06:42.863918900 +0200
> @@ -5,7 +5,14 @@
20d19
<
```
It seems that `libcst.tool codemod -u` adds one unneccessary line which prevents `patch` to apply such diff.
```bash
< @@ -5,8 +5,15 @@
---
> @@ -5,7 +5,14 @@
```

If I use `-u=1` to limit context diff is generated correctly and can be applied without any problems. When diff happens to span over entire file (`-u=3`) then it *cannot* be applied.

### Conclusion
Am I using wrong options or diff generation in some cases is wrong?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.