Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

In the commit dialog, deal with Signed-off-by not on the last line

The previous implementation made the assumption that
the Signed-off-by is always on the last line.

Correct this assumption and deal with Signed-off-by everywhere in
the commit message.

Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
  • Loading branch information...
commit 5aaed94e487d1bc633f503bfdd692f5697ade123 1 parent 3880a47
@yanns yanns authored robinrosenberg committed
View
65 org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/CommitDialog.java
@@ -179,14 +179,16 @@ public void keyPressed(KeyEvent arg0) {
if (committer != null)
committerText.setText(committer);
committerText.addModifyListener(new ModifyListener() {
+ String oldCommitter = committerText.getText();
public void modifyText(ModifyEvent e) {
if (signedOffButton.getSelection()) {
// the commit message is signed
// the signature must be updated
- String oldCommitText = commitText.getText();
- oldCommitText = removeLastLine(oldCommitText);
- oldCommitText = signOff(oldCommitText);
- commitText.setText(oldCommitText);
+ String newCommitter = committerText.getText();
+ String oldSignOff = getSignedOff(oldCommitter);
+ String newSignOff = getSignedOff(newCommitter);
+ commitText.setText(replaceSignOff(commitText.getText(), oldSignOff, newSignOff));
+ oldCommitter = newCommitter;
}
}
});
@@ -230,12 +232,16 @@ public void widgetDefaultSelected(SelectionEvent arg0) {
signedOffButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
+ String curText = commitText.getText();
if (signedOffButton.getSelection()) {
// add signed off line
- commitText.setText(signOff(commitText.getText()));
+ commitText.setText(signOff(curText));
} else {
// remove signed off line
- commitText.setText(removeLastLine(commitText.getText()));
+ curText = replaceSignOff(curText, getSignedOff(), "");
+ if (curText.endsWith(Text.DELIMITER + Text.DELIMITER))
+ curText = curText.substring(0, curText.length() - Text.DELIMITER.length());
+ commitText.setText(curText);
}
}
@@ -279,11 +285,19 @@ public void modifyText(ModifyEvent e) {
}
private void updateSignedOffButton() {
- signedOffButton.setSelection(getLastLine(commitText.getText()).equals(getSignedOff()));
+ String curText = commitText.getText();
+ if (!curText.endsWith(Text.DELIMITER))
+ curText += Text.DELIMITER;
+
+ signedOffButton.setSelection(curText.indexOf(getSignedOff() + Text.DELIMITER) != -1);
}
private String getSignedOff() {
- return Constants.SIGNED_OFF_BY_TAG + committerText.getText();
+ return getSignedOff(committerText.getText());
+ }
+
+ private String getSignedOff(String signer) {
+ return Constants.SIGNED_OFF_BY_TAG + signer;
}
private String signOff(String input) {
@@ -299,32 +313,33 @@ private String signOff(String input) {
}
private String getLastLine(String input) {
- String output = removeLastLineBreak(input);
+ String output = input;
int breakLength = Text.DELIMITER.length();
- // get the last line
+ // remove last line break if exist
int lastIndexOfLineBreak = output.lastIndexOf(Text.DELIMITER);
+ if (lastIndexOfLineBreak != -1 && lastIndexOfLineBreak == output.length() - breakLength)
+ output = output.substring(0, output.length() - breakLength);
+
+ // get the last line
+ lastIndexOfLineBreak = output.lastIndexOf(Text.DELIMITER);
return lastIndexOfLineBreak == -1 ? output : output.substring(lastIndexOfLineBreak + breakLength, output.length());
}
- private String removeLastLine(String input) {
- String output = removeLastLineBreak(input);
+ private String replaceSignOff(String input, String oldSignOff, String newSignOff) {
+ assert input != null;
+ assert oldSignOff != null;
+ assert newSignOff != null;
- // remove the last line if possible
- int lastIndexOfLineBreak = output.lastIndexOf(Text.DELIMITER);
- return lastIndexOfLineBreak == -1 ? "" : output.substring(0, lastIndexOfLineBreak); //$NON-NLS-1$
- }
-
- private String removeLastLineBreak(String input) {
- String output = input;
- int breakLength = Text.DELIMITER.length();
+ String curText = input;
+ if (!curText.endsWith(Text.DELIMITER))
+ curText += Text.DELIMITER;
- // remove last line break if exist
- int lastIndexOfLineBreak = output.lastIndexOf(Text.DELIMITER);
- if (lastIndexOfLineBreak != -1 && lastIndexOfLineBreak == output.length() - breakLength)
- output = output.substring(0, output.length() - breakLength);
+ int indexOfSignOff = curText.indexOf(oldSignOff + Text.DELIMITER);
+ if (indexOfSignOff == -1)
+ return input;
- return output;
+ return input.substring(0, indexOfSignOff) + newSignOff + input.substring(indexOfSignOff + oldSignOff.length(), input.length());
}
private Menu getContextMenu() {
Please sign in to comment.
Something went wrong with that request. Please try again.