[skip ci] Merge pull request #1541 from Technius/ci/new-diff-lines

Improve format_diff_lines.pl utility script
presentation
Bryan Tan 7 years ago committed by GitHub
commit 246e5a17d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      azure-pipelines/util/format_diff_lines.pl
  2. 28
      azure-pipelines/util/sample.cpp

@ -13,6 +13,20 @@ my @hunks;
# Current file
my $current_file;
my @CLANG_FMT_CMDS = ("clang-format", "clang-format-8");
my $clang_format_cmd = "";
for my $c (@CLANG_FMT_CMDS) {
if (`$c --version`) {
$clang_format_cmd = $c;
last;
}
}
if ($clang_format_cmd eq "") {
printf "$0: clang-format not found\n";
exit 1;
}
# Push the pending hunks to the changed file table.
sub push_changes {
if (scalar @hunks > 0) {
@ -36,14 +50,18 @@ push_changes;
# Now, run clang format on the changed lines.
while (my ($file_name, $hunks_ref) = each %changed_files) {
my @ranges = ();
foreach my $hunk_ref (@{$hunks_ref}) {
my $start_pos = @{$hunk_ref}[0];
my $line_count = @{$hunk_ref}[1];
my $end_pos = $start_pos + $line_count - 1;
if ($line_count > 0) {
printf "Running clang-format-8 on %s, lines %i-%i\n", $file_name, $start_pos, $end_pos;
`clang-format-8 -i -style=file -lines=$start_pos:$end_pos $file_name`;
die "Failed to run clang-format-8: exit code $?" if $? != 0;
push @ranges, [$start_pos, $end_pos];
}
}
my $ranges_str = join(", ", map { "@{$_}[0]-@{$_}[1]" } @ranges);
my $lines_arg = join(" ", map { "-lines=@{$_}[0]:@{$_}[1]" } @ranges);
printf "Running $clang_format_cmd on %s, lines %s (inclusive)\n", $file_name, $ranges_str;
`$clang_format_cmd -i -style=file $lines_arg $file_name`;
die "Failed to run $clang_format_cmd: exit code $?" if $? != 0;
}

@ -0,0 +1,28 @@
void foo() {
int bar;
}
class Hi
{
Hi(
) : a(a), b(b)
{
}
int a;
int b; int c; int d;
}
;
void goodFormatting()
{
int a = 0;
int b;
int c;
int d;
if (a) {
while (true) {
b = 1;
}
}
}
Loading…
Cancel
Save