Skip to content

Commit

Permalink
Fix -d,--diff option by using git-diff.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jun 4, 2024
1 parent 86e52bb commit 80a9753
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/Check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 12 additions & 8 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 80a9753

Please sign in to comment.