diff options
author | David Pursehouse <david.pursehouse@gmail.com> | 2018-03-02 14:14:55 +0900 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2018-03-03 23:58:50 +0100 |
commit | bc9e3a31b267c1ec0a590a7bf298361983a88b0f (patch) | |
tree | 67d52afe75e8ec5a77cf52039f459e416c4ebafa | |
parent | 2d178f5f14366de5a57e94f07e523dffd58451d2 (diff) | |
download | jgit-bc9e3a31b267c1ec0a590a7bf298361983a88b0f.tar.gz jgit-bc9e3a31b267c1ec0a590a7bf298361983a88b0f.zip |
Add|RemoveNoteComand: Reduce duplicated code
The private method commitNoteMap is in both classes with the same
implementation.
Make it static in AddNoteCommand and reuse it from RemoveNoteCommand.
Change-Id: Ia037bf9efdd94ee7b8d33b41321e9cfd6ea4a6a5
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/AddNoteCommand.java | 9 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java | 31 |
2 files changed, 7 insertions, 33 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/AddNoteCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/AddNoteCommand.java index f80c8c76ed..688f5f4f01 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/AddNoteCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/AddNoteCommand.java @@ -99,7 +99,7 @@ public class AddNoteCommand extends GitCommand<Note> { map = NoteMap.read(walk.getObjectReader(), notesCommit); } map.set(id, message, inserter); - commitNoteMap(walk, map, notesCommit, inserter, + commitNoteMap(repo, notesRef, walk, map, notesCommit, inserter, "Notes added by 'git notes add'"); //$NON-NLS-1$ return map.getNote(id); } catch (IOException e) { @@ -134,7 +134,8 @@ public class AddNoteCommand extends GitCommand<Note> { return this; } - private void commitNoteMap(RevWalk walk, NoteMap map, + static void commitNoteMap(Repository r, String ref, RevWalk walk, + NoteMap map, RevCommit notesCommit, ObjectInserter inserter, String msg) @@ -142,14 +143,14 @@ public class AddNoteCommand extends GitCommand<Note> { // commit the note CommitBuilder builder = new CommitBuilder(); builder.setTreeId(map.writeTree(inserter)); - builder.setAuthor(new PersonIdent(repo)); + builder.setAuthor(new PersonIdent(r)); builder.setCommitter(builder.getAuthor()); builder.setMessage(msg); if (notesCommit != null) builder.setParentIds(notesCommit); ObjectId commit = inserter.insert(builder); inserter.flush(); - RefUpdate refUpdate = repo.updateRef(notesRef); + RefUpdate refUpdate = r.updateRef(ref); if (notesCommit != null) refUpdate.setExpectedOldObjectId(notesCommit); else diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java index baae8248f3..cfbf0dd4e7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java @@ -46,13 +46,9 @@ import java.io.IOException; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.JGitInternalException; -import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; -import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.notes.Note; import org.eclipse.jgit.notes.NoteMap; @@ -99,7 +95,8 @@ public class RemoveNoteCommand extends GitCommand<Note> { map = NoteMap.read(walk.getObjectReader(), notesCommit); } map.set(id, null, inserter); - commitNoteMap(walk, map, notesCommit, inserter, + AddNoteCommand.commitNoteMap(repo, notesRef, walk, map, notesCommit, + inserter, "Notes removed by 'git notes remove'"); //$NON-NLS-1$ return map.getNote(id); } catch (IOException e) { @@ -121,30 +118,6 @@ public class RemoveNoteCommand extends GitCommand<Note> { return this; } - private void commitNoteMap(RevWalk walk, NoteMap map, - RevCommit notesCommit, - ObjectInserter inserter, - String msg) - throws IOException { - // commit the note - CommitBuilder builder = new CommitBuilder(); - builder.setTreeId(map.writeTree(inserter)); - builder.setAuthor(new PersonIdent(repo)); - builder.setCommitter(builder.getAuthor()); - builder.setMessage(msg); - if (notesCommit != null) - builder.setParentIds(notesCommit); - ObjectId commit = inserter.insert(builder); - inserter.flush(); - RefUpdate refUpdate = repo.updateRef(notesRef); - if (notesCommit != null) - refUpdate.setExpectedOldObjectId(notesCommit); - else - refUpdate.setExpectedOldObjectId(ObjectId.zeroId()); - refUpdate.setNewObjectId(commit); - refUpdate.update(walk); - } - /** * Set the name of the <code>Ref</code> to remove a note from. * |