mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Replace quotes in git command line, too
This commit is contained in:
@@ -45,7 +45,6 @@ import java.io.OutputStream;
|
|||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
|
public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
|
||||||
@@ -78,12 +77,19 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
|
|||||||
|
|
||||||
static class DequoteOutputStream extends OutputStream {
|
static class DequoteOutputStream extends OutputStream {
|
||||||
|
|
||||||
|
private static final String[] DEQUOTE_STARTS = {
|
||||||
|
"--- ",
|
||||||
|
"+++ ",
|
||||||
|
"diff --git "
|
||||||
|
};
|
||||||
|
|
||||||
private final OutputStream target;
|
private final OutputStream target;
|
||||||
|
|
||||||
private boolean afterNL = false;
|
private boolean afterNL = true;
|
||||||
private boolean writeToBuffer = false;
|
private boolean writeToBuffer = false;
|
||||||
private int minusCount = 0;
|
private int numberOfPotentialBeginning = -1;
|
||||||
private int plusCount = 0;
|
private int potentialBeginningCharCount = 0;
|
||||||
|
private boolean inPotentialQuotedLine = false;
|
||||||
|
|
||||||
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||||
|
|
||||||
@@ -93,53 +99,87 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int i) throws IOException {
|
public void write(int i) throws IOException {
|
||||||
if (i == (int) '+' && afterNL) {
|
if (i == (int) '\n') {
|
||||||
plusCount = 1;
|
handleNewLine(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (afterNL) {
|
||||||
afterNL = false;
|
afterNL = false;
|
||||||
|
if (foundPotentialBeginning(i)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
numberOfPotentialBeginning = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inPotentialQuotedLine && i == '"') {
|
||||||
|
handleQuote();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numberOfPotentialBeginning > -1 && checkForFurtherBeginning(i)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (writeToBuffer) {
|
||||||
|
buffer.write(i);
|
||||||
|
} else {
|
||||||
target.write(i);
|
target.write(i);
|
||||||
} else if (i == (int) '+' && plusCount > 0) {
|
}
|
||||||
++plusCount;
|
}
|
||||||
afterNL = false;
|
|
||||||
|
private boolean checkForFurtherBeginning(int i) throws IOException {
|
||||||
|
if (i == DEQUOTE_STARTS[numberOfPotentialBeginning].charAt(potentialBeginningCharCount)) {
|
||||||
|
if (potentialBeginningCharCount + 1 < DEQUOTE_STARTS[numberOfPotentialBeginning].length()) {
|
||||||
|
++potentialBeginningCharCount;
|
||||||
|
} else {
|
||||||
|
inPotentialQuotedLine = true;
|
||||||
|
}
|
||||||
target.write(i);
|
target.write(i);
|
||||||
} else if (i == (int) '-' && afterNL) {
|
return true;
|
||||||
minusCount = 1;
|
} else {
|
||||||
afterNL = false;
|
numberOfPotentialBeginning = -1;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean foundPotentialBeginning(int i) throws IOException {
|
||||||
|
for (int n = 0; n < DEQUOTE_STARTS.length; ++n) {
|
||||||
|
if (i == DEQUOTE_STARTS[n].charAt(0)) {
|
||||||
|
numberOfPotentialBeginning = n;
|
||||||
|
potentialBeginningCharCount = 1;
|
||||||
target.write(i);
|
target.write(i);
|
||||||
} else if (i == (int) '-' && minusCount > 0) {
|
return true;
|
||||||
++minusCount;
|
}
|
||||||
afterNL = false;
|
}
|
||||||
target.write(i);
|
return false;
|
||||||
} else if (i == (int) ' ' && plusCount == 3) {
|
}
|
||||||
|
|
||||||
|
private void handleQuote() throws IOException {
|
||||||
|
if (writeToBuffer) {
|
||||||
|
buffer.write('"');
|
||||||
|
dequoteBuffer();
|
||||||
|
} else {
|
||||||
writeToBuffer = true;
|
writeToBuffer = true;
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
afterNL = false;
|
buffer.write('"');
|
||||||
plusCount = 0;
|
}
|
||||||
target.write(i);
|
}
|
||||||
} else if (i == (int) ' ' && minusCount == 3) {
|
|
||||||
minusCount = 0;
|
private void handleNewLine(int i) throws IOException {
|
||||||
afterNL = false;
|
|
||||||
writeToBuffer = true;
|
|
||||||
buffer.reset();
|
|
||||||
target.write(i);
|
|
||||||
} else if (i == (int) '\n') {
|
|
||||||
afterNL = true;
|
afterNL = true;
|
||||||
if (writeToBuffer) {
|
if (writeToBuffer) {
|
||||||
|
dequoteBuffer();
|
||||||
|
}
|
||||||
|
target.write(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dequoteBuffer() throws IOException {
|
||||||
byte[] bytes = buffer.toByteArray();
|
byte[] bytes = buffer.toByteArray();
|
||||||
String dequote = QuotedString.GIT_PATH.dequote(bytes, 0, bytes.length);
|
String dequote = QuotedString.GIT_PATH.dequote(bytes, 0, bytes.length);
|
||||||
target.write(dequote.getBytes(UTF_8));
|
target.write(dequote.getBytes(UTF_8));
|
||||||
writeToBuffer = false;
|
writeToBuffer = false;
|
||||||
}
|
}
|
||||||
target.write(i);
|
|
||||||
} else if (writeToBuffer) {
|
|
||||||
buffer.write(i);
|
|
||||||
afterNL = false;
|
|
||||||
} else {
|
|
||||||
target.write(i);
|
|
||||||
afterNL = false;
|
|
||||||
minusCount = 0;
|
|
||||||
plusCount = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public class GitDiffCommand_DequoteOutputStreamTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDequoteText() throws IOException {
|
void shouldDequoteText() throws IOException {
|
||||||
String s = "diff --git \"a/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\" \"b/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\"\n" +
|
String s = "diff --git \"a/file \\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 a\" \"b/file \\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 b\"\n" +
|
||||||
"new file mode 100644\n" +
|
"new file mode 100644\n" +
|
||||||
"index 0000000..8cb0607\n" +
|
"index 0000000..8cb0607\n" +
|
||||||
"--- /dev/null\n" +
|
"--- /dev/null\n" +
|
||||||
@@ -24,7 +24,7 @@ public class GitDiffCommand_DequoteOutputStreamTest {
|
|||||||
stream.write(bytes, 0, bytes.length);
|
stream.write(bytes, 0, bytes.length);
|
||||||
stream.flush();
|
stream.flush();
|
||||||
|
|
||||||
Assertions.assertThat(buffer.toString()).isEqualTo("diff --git \"a/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\" \"b/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\"\n" +
|
Assertions.assertThat(buffer.toString()).isEqualTo("diff --git a/file úüþëéåëåé a b/file úüþëéåëåé b\n" +
|
||||||
"new file mode 100644\n" +
|
"new file mode 100644\n" +
|
||||||
"index 0000000..8cb0607\n" +
|
"index 0000000..8cb0607\n" +
|
||||||
"--- /dev/null\n" +
|
"--- /dev/null\n" +
|
||||||
|
|||||||
Reference in New Issue
Block a user