Browse Source

Fix -d,--diff option by using git-diff.

pull/19/head
Fredrik Ekre 2 years ago
parent
commit
80a9753d94
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 9
      .github/workflows/Check.yml
  2. 20
      src/Runic.jl
  3. 21
      src/main.jl

9
.github/workflows/Check.yml

@ -33,18 +33,15 @@ jobs: @@ -33,18 +33,15 @@ jobs:
- uses: julia-actions/cache@v2
- name: Install dependencies
run: |
julia --project --code-coverage=${{ env.coverage_flag }} -e 'using Pkg; Pkg.instantiate(); Pkg.precompile()'
julia --color=yes --project --code-coverage=${{ env.coverage_flag }} -e 'using Pkg; Pkg.instantiate(); Pkg.precompile()'
- name: Run Runic
run: |
julia --project --code-coverage=${{ env.coverage_flag }} -m Runic -i $(git ls-files -- '*.jl')
julia --color=yes --project --code-coverage=${{ env.coverage_flag }} -m Runic --check --diff $(git ls-files -- '*.jl')
if: ${{ matrix.version == 'nightly' }}
- name: Run Runic
run: |
julia --project --code-coverage=${{ env.coverage_flag }} -e 'using Runic; exit(Runic.main(ARGS))' -- -i $(git ls-files -- '*.jl')
julia --color=yes --project --code-coverage=${{ env.coverage_flag }} -e 'using Runic; exit(Runic.main(ARGS))' -- --check --diff $(git ls-files -- '*.jl')
if: ${{ matrix.version != 'nightly' }}
- name: Check formatting diff
run: |
git diff --color=always --exit-code
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
with:

20
src/Runic.jl

@ -125,6 +125,16 @@ function Context( @@ -125,6 +125,16 @@ function Context(
)
fmt_io = IOBuffer()
fmt_tree = nothing
# Set up buffers
src_pos = position(src_io)
@assert src_pos == 0
fmt_pos = position(fmt_io)
@assert fmt_pos == 0
nb = write(fmt_io, read(src_io, span(src_tree)))
@assert nb == span(src_tree)
# Reset IO positions to the beginning
seek(src_io, src_pos)
seek(fmt_io, fmt_pos)
# Debug mode enforces verbose and assert
verbose = debug ? true : verbose
assert = debug ? true : assert
@ -440,16 +450,10 @@ end @@ -440,16 +450,10 @@ end
# Entrypoint
function format_tree!(ctx::Context)
root = ctx.src_tree
# Write the root node to the output IO so that the formatter can read it if needed
src_pos = position(ctx.src_io)
@assert src_pos == 0
# Verify buffers
@assert position(ctx.src_io) == 0
fmt_pos = position(ctx.fmt_io)
@assert fmt_pos == 0
nb = write(ctx.fmt_io, read(ctx.src_io, span(root)))
@assert nb == span(root)
# Reset IOs so that the offsets are correct
seek(ctx.src_io, src_pos)
seek(ctx.fmt_io, fmt_pos)
# Set the root to the current node
root′ = root
itr = 0

21
src/main.jl

@ -173,6 +173,15 @@ function main(argv) @@ -173,6 +173,15 @@ function main(argv)
return panic("option `-i` is incompatible with stdin as input")
end
# --diff currently requires git
git = nothing
if diff
git = Sys.which("git")
if git === nothing
return panic("option `-d, --diff` requires `git` to be installed")
end
end
# Loop over the input files
for inputfile in inputfiles
# Read the input
@ -266,7 +275,7 @@ function main(argv) @@ -266,7 +275,7 @@ function main(argv)
end
elseif changed || !inplace
try
write(output, take!(ctx.fmt_io))
write(output, seekstart(ctx.fmt_io))
catch err
print_progress && errln()
panic("could not write to output: ", err)
@ -276,7 +285,15 @@ function main(argv) @@ -276,7 +285,15 @@ function main(argv)
print_progress && okln()
end
if diff
error("todo")
mktempdir() do dir
A = joinpath(dir, "A.jl")
B = joinpath(dir, "B.jl")
write(A, ctx.src_str)
write(B, seekstart(ctx.fmt_io))
# ignorestatus because --no-index implies --exit-code
cmd = `$(git) --no-pager diff --no-index --color=always A.jl B.jl`
run(setenv(ignorestatus(cmd); dir = dir))
end
end
end # inputfile loop

Loading…
Cancel
Save