Kaynağa Gözat

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>
tags/v4.11.0.201803080745-r
David Pursehouse 6 yıl önce
ebeveyn
işleme
bc9e3a31b2

+ 5
- 4
org.eclipse.jgit/src/org/eclipse/jgit/api/AddNoteCommand.java Dosyayı Görüntüle

map = NoteMap.read(walk.getObjectReader(), notesCommit); map = NoteMap.read(walk.getObjectReader(), notesCommit);
} }
map.set(id, message, inserter); 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$ "Notes added by 'git notes add'"); //$NON-NLS-1$
return map.getNote(id); return map.getNote(id);
} catch (IOException e) { } catch (IOException e) {
return this; return this;
} }


private void commitNoteMap(RevWalk walk, NoteMap map,
static void commitNoteMap(Repository r, String ref, RevWalk walk,
NoteMap map,
RevCommit notesCommit, RevCommit notesCommit,
ObjectInserter inserter, ObjectInserter inserter,
String msg) String msg)
// commit the note // commit the note
CommitBuilder builder = new CommitBuilder(); CommitBuilder builder = new CommitBuilder();
builder.setTreeId(map.writeTree(inserter)); builder.setTreeId(map.writeTree(inserter));
builder.setAuthor(new PersonIdent(repo));
builder.setAuthor(new PersonIdent(r));
builder.setCommitter(builder.getAuthor()); builder.setCommitter(builder.getAuthor());
builder.setMessage(msg); builder.setMessage(msg);
if (notesCommit != null) if (notesCommit != null)
builder.setParentIds(notesCommit); builder.setParentIds(notesCommit);
ObjectId commit = inserter.insert(builder); ObjectId commit = inserter.insert(builder);
inserter.flush(); inserter.flush();
RefUpdate refUpdate = repo.updateRef(notesRef);
RefUpdate refUpdate = r.updateRef(ref);
if (notesCommit != null) if (notesCommit != null)
refUpdate.setExpectedOldObjectId(notesCommit); refUpdate.setExpectedOldObjectId(notesCommit);
else else

+ 2
- 29
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java Dosyayı Görüntüle



import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.Note; import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.notes.NoteMap; import org.eclipse.jgit.notes.NoteMap;
map = NoteMap.read(walk.getObjectReader(), notesCommit); map = NoteMap.read(walk.getObjectReader(), notesCommit);
} }
map.set(id, null, inserter); 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$ "Notes removed by 'git notes remove'"); //$NON-NLS-1$
return map.getNote(id); return map.getNote(id);
} catch (IOException e) { } catch (IOException e) {
return this; 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. * Set the name of the <code>Ref</code> to remove a note from.
* *

Loading…
İptal
Kaydet