Skip to content

Commit

Permalink
Support git interactive+incremental formatting.
Browse files Browse the repository at this point in the history
Demo:
  $ verible-verilog-format-changed-lines-interactive.sh --rcs=git
  [verible-transform-interactive.sh]  Transformation: verible-verilog-format
  [verible-transform-interactive.sh]  Temporary files in: /tmp/tmp.verible-transform-interactive.sh.fh6NMHq
  [verible-transform-interactive.sh]  Cumulative patch (affects 2 files): /tmp/tmp.verible-transform-interactive.sh.fh6NMHq/interactive.patch
  --- /home/projects/lowRISC/opentitan/hw/ip/aes/rtl/aes_core.sv
  +++ NEW//home/projects/lowRISC/opentitan/hw/ip/aes/rtl/aes_core.sv
  @@ -32,3 +32,3 @@
     // Signals
  -  logic                 foo_bar;
  +  logic foo_bar;
     logic                 ctrl_qe;
  Apply this hunk? [y,n,a,d,s,q,?] n
  @@ -107,3 +107,3 @@

  -      logic   illogical;
  +  logic illogical;

  Apply this hunk? [y,n,a,d,s,q,?] y

PiperOrigin-RevId: 322474970
  • Loading branch information
fangism authored and hzeller committed Jul 22, 2020
1 parent 023f0c0 commit a334702
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,14 @@ script options: (options with arguments can be: --flag=VALUE or --flag VALUE)

#### Incremental Interactive Formatting

##### p4 Users

In your locally modified client run:

```shell
verible-verilog-format-changed-lines-interactive.sh
verible-verilog-format-changed-lines-interactive.sh --rcs=TOOL
```

where `TOOL` is one of `p4,git`.

and follow the prompts.

### Lexical Diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Run this from a version-controlled directory.
script options:
--help, -h : print help and exit
--rcs TOOL : revision control system
Supported: p4
Supported: p4,git
EOF
}
Expand Down Expand Up @@ -87,16 +87,36 @@ done

# TODO(fangism): attempt to infer version control system (p4,svn,git,cvs,hg,...)

function p4_touched_files() {
# Result is already absolute paths to local files.
p4 whatsout
}

function git_touched_files() {
# Get added/modified files that are tracked.
# Parse the short-form of git-status output. See git help status.
# cut: extract the filename
# readlink: resolve absolute path to eliminate sensitivity to "$PWD"
git status -s | grep "^[AM]" | cut -c 4- | xargs readlink -f
}

# Set commands based on version control system
case "$rcs" in
p4) touched_files=(p4 whatsout)
p4) touched_files=p4_touched_files
single_file_diff=(p4 diff -d-u "{}") ;;
git) touched_files=git_touched_files
single_file_diff=(git diff -u --cached "{}") ;;
*) { msg "Unsupported version control system: $rcs" ; exit 1;} ;;
esac

# File extensions are currently hardcoded to Verilog files.
files=($("${touched_files[@]}" | grep -e '\.sv$' -e '\.v$' -e '\.svh$' -e '\.vh$' ))
files=($("$touched_files" | grep -e '\.sv$' -e '\.v$' -e '\.svh$' -e '\.vh$' ))

# Note about --per-file-transform-flags:
# The file name is stripped away to yield only line numbers, which is why
# this still works with git-diffs, which contain a/ and b/ prefixes in
# filenames. The file name used for applying changes is still the one from
# "${files[@]}".
interact_command=("$interactive_patch_script" \
--patch-tool "$patch_tool" \
--per-file-transform-flags='--lines=$('"${single_file_diff[*]}"' | '"$patch_tool"' changed-lines - | cut -d" " -f2)' \
Expand Down

0 comments on commit a334702

Please sign in to comment.