This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 248002
Collapse All | Expand All

(-)libs.git/apichanges.xml (+17 lines)
Lines 111-116 Link Here
111
        
111
        
112
        <change>
112
        <change>
113
            <api name="gitlibrary_api"/>
113
            <api name="gitlibrary_api"/>
114
            <summary>New method exporting a diff between two arbitrary commits or trees.</summary>
115
            <version major="1" minor="29"/>
116
            <date day="17" month="10" year="2014"/>
117
            <author login="ovrabec"/>
118
            <compatibility addition="yes"/>
119
            <description>
120
                <ul>
121
                    <li>Adding new variant of GitClient.exportDiff which works for any
122
                    two arbitrary commits. It is also possible to diff between a commit and the working tree
123
                    or the Index.</li>
124
                </ul>
125
            </description>
126
            <class package="org.netbeans.libs.git" name="GitClient"/>
127
            <issue number="248002"/>
128
        </change>
129
        <change>
130
            <api name="gitlibrary_api"/>
114
            <summary>New method for updating a reference (branch) to a new commit id.</summary>
131
            <summary>New method for updating a reference (branch) to a new commit id.</summary>
115
            <version major="1" minor="27"/>
132
            <version major="1" minor="27"/>
116
            <date day="1" month="7" year="2014"/>
133
            <date day="1" month="7" year="2014"/>
(-)libs.git/src/org/netbeans/libs/git/GitClient.java (-1 / +52 lines)
Lines 301-306 Link Here
301
        };
301
        };
302
    }
302
    }
303
    
303
    
304
    /**
305
     * "Commit" identifier representing the state of the working tree. May be
306
     * used in {@link #exportDiff(java.io.File[], java.lang.String, java.lang.String, java.io.OutputStream, org.netbeans.libs.git.progress.ProgressMonitor)
307
     * }
308
     * to diff a working tree state to another commit.
309
     *
310
     * @since 1.29
311
     */
312
    public static final String WORKING_TREE = "WORKING_TREE"; // NOI18N
313
    /**
314
     * "Commit" identifier representing the state in the Index. May be used in {@link #exportDiff(java.io.File[], java.lang.String, java.lang.String, java.io.OutputStream, org.netbeans.libs.git.progress.ProgressMonitor)
315
     * }
316
     * to diff the Index state to another commit.
317
     *
318
     * @since 1.29
319
     */
320
    public static final String INDEX = "INDEX"; // NOI18N
321
    
304
    private final JGitRepository gitRepository;
322
    private final JGitRepository gitRepository;
305
    private final Set<NotificationListener> listeners;
323
    private final Set<NotificationListener> listeners;
306
    private JGitCredentialsProvider credentialsProvider;
324
    private JGitCredentialsProvider credentialsProvider;
Lines 574-580 Link Here
574
     * @throws GitException an unexpected error occurs
592
     * @throws GitException an unexpected error occurs
575
     */
593
     */
576
    public void exportDiff (File[] roots, DiffMode mode, OutputStream out, ProgressMonitor monitor) throws GitException {
594
    public void exportDiff (File[] roots, DiffMode mode, OutputStream out, ProgressMonitor monitor) throws GitException {
577
        ExportDiffCommand cmd = new ExportDiffCommand(gitRepository.getRepository(), getClassFactory(), roots, mode, out, monitor, delegateListener);
595
        switch (mode) {
596
            case HEAD_VS_INDEX:
597
                exportDiff(roots, Constants.HEAD, INDEX, out, monitor);
598
                break;
599
            case HEAD_VS_WORKINGTREE:
600
                exportDiff(roots, Constants.HEAD, WORKING_TREE, out, monitor);
601
                break;
602
            case INDEX_VS_WORKINGTREE:
603
                exportDiff(roots, INDEX, WORKING_TREE, out, monitor);
604
                break;
605
            default:
606
                throw new IllegalArgumentException("Unknown diff mode: " + mode);
607
        }
608
    }
609
    
610
    /**
611
     * Exports diff of changes between two trees identified by files and two
612
     * commit/revision identifiers.
613
     *
614
     * @param roots the diff will be exported only for files under these roots,
615
     * can be empty to export all modifications in the whole tree.
616
     * @param commitBase first commit identifier, may be also
617
     * {@link #WORKING_TREE} or {@link #INDEX}.
618
     * @param commitOther second commit identifier, may be also
619
     * {@link #WORKING_TREE} or {@link #INDEX}.
620
     * @param out output stream the diff will be printed to
621
     * @param monitor progress monitor
622
     * @throws GitException an unexpected error occurs
623
     * @since 1.29
624
     */
625
    public void exportDiff (File[] roots, String commitBase, String commitOther, OutputStream out, ProgressMonitor monitor) throws GitException {
626
        ExportDiffCommand cmd = new ExportDiffCommand(
627
                gitRepository.getRepository(), getClassFactory(), roots,
628
                commitBase, commitOther, out, monitor, delegateListener);
578
        cmd.execute();
629
        cmd.execute();
579
    }
630
    }
580
    
631
    
(-)libs.git/src/org/netbeans/libs/git/jgit/commands/ExportDiffCommand.java (-27 / +27 lines)
Lines 67-73 Link Here
67
import org.eclipse.jgit.treewalk.WorkingTreeOptions;
67
import org.eclipse.jgit.treewalk.WorkingTreeOptions;
68
import org.eclipse.jgit.treewalk.filter.PathFilter;
68
import org.eclipse.jgit.treewalk.filter.PathFilter;
69
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
69
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
70
import org.netbeans.libs.git.GitClient.DiffMode;
70
import org.netbeans.libs.git.GitClient;
71
import org.netbeans.libs.git.GitException;
71
import org.netbeans.libs.git.GitException;
72
import org.netbeans.libs.git.jgit.GitClassFactory;
72
import org.netbeans.libs.git.jgit.GitClassFactory;
73
import org.netbeans.libs.git.jgit.Utils;
73
import org.netbeans.libs.git.jgit.Utils;
Lines 82-97 Link Here
82
public class ExportDiffCommand extends GitCommand {
82
public class ExportDiffCommand extends GitCommand {
83
    private final File[] roots;
83
    private final File[] roots;
84
    private final ProgressMonitor monitor;
84
    private final ProgressMonitor monitor;
85
    private final DiffMode mode;
86
    private final OutputStream out;
85
    private final OutputStream out;
87
    private final FileListener listener;
86
    private final FileListener listener;
87
    private final String firstCommit;
88
    private final String secondCommit;
88
89
89
    public ExportDiffCommand (Repository repository, GitClassFactory gitFactory, File[] roots, DiffMode mode, OutputStream out, ProgressMonitor monitor, FileListener listener) {
90
    public ExportDiffCommand (Repository repository, GitClassFactory gitFactory,
91
            File[] roots, String firstCommit, String secondCommit,
92
            OutputStream out, ProgressMonitor monitor, FileListener listener) {
90
        super(repository, gitFactory, monitor);
93
        super(repository, gitFactory, monitor);
91
        this.roots = roots;
94
        this.roots = roots;
92
        this.monitor = monitor;
95
        this.monitor = monitor;
93
        this.listener = listener;
96
        this.listener = listener;
94
        this.mode = mode;
97
        this.firstCommit = firstCommit;
98
        this.secondCommit = secondCommit;
95
        this.out = out;
99
        this.out = out;
96
    }
100
    }
97
101
Lines 111-136 Link Here
111
                // work-around for autocrlf
115
                // work-around for autocrlf
112
                formatter.setDiffComparator(new AutoCRLFComparator());
116
                formatter.setDiffComparator(new AutoCRLFComparator());
113
            }
117
            }
114
            AbstractTreeIterator firstTree;
118
            or = repository.newObjectReader();
115
            AbstractTreeIterator secondTree;
119
            AbstractTreeIterator firstTree = getIterator(firstCommit, or);
116
            switch (mode) {
120
            AbstractTreeIterator secondTree = getIterator(secondCommit, or);
117
                case HEAD_VS_INDEX:
118
                    or = repository.newObjectReader();
119
                    firstTree = getHeadIterator(or);
120
                    secondTree = new DirCacheIterator(repository.readDirCache());
121
                    break;
122
                case HEAD_VS_WORKINGTREE:
123
                    or = repository.newObjectReader();
124
                    firstTree = getHeadIterator(or);
125
                    secondTree = new FileTreeIterator(repository);
126
                    break;
127
                case INDEX_VS_WORKINGTREE:
128
                    firstTree = new DirCacheIterator(repository.readDirCache());
129
                    secondTree = new FileTreeIterator(repository);
130
                    break;
131
                default:
132
                    throw new IllegalArgumentException("Unknown diff mode: " + mode);
133
            }
134
            List<DiffEntry> diffEntries;
121
            List<DiffEntry> diffEntries;
135
            if (secondTree instanceof WorkingTreeIterator) {
122
            if (secondTree instanceof WorkingTreeIterator) {
136
                // remote when fixed in JGit, see ExportDiffTest.testDiffRenameDetectionProblem
123
                // remote when fixed in JGit, see ExportDiffTest.testDiffRenameDetectionProblem
Lines 140-148 Link Here
140
                RenameDetector detector = formatter.getRenameDetector();
127
                RenameDetector detector = formatter.getRenameDetector();
141
                detector.reset();
128
                detector.reset();
142
                detector.addAll(diffEntries);
129
                detector.addAll(diffEntries);
143
                if (or == null) {
144
                    or = repository.newObjectReader();
145
                }
146
		diffEntries = detector.compute(new ContentSource.Pair(ContentSource.create(or), ContentSource.create((WorkingTreeIterator) secondTree)), NullProgressMonitor.INSTANCE);
130
		diffEntries = detector.compute(new ContentSource.Pair(ContentSource.create(or), ContentSource.create((WorkingTreeIterator) secondTree)), NullProgressMonitor.INSTANCE);
147
            } else {
131
            } else {
148
                formatter.setDetectRenames(true);
132
                formatter.setDetectRenames(true);
Lines 166-171 Link Here
166
        }
150
        }
167
    }
151
    }
168
152
153
    private AbstractTreeIterator getIterator (String commit, ObjectReader or) throws IOException, GitException {
154
        Repository repository = getRepository();
155
        switch (commit) {
156
            case Constants.HEAD:
157
                return getHeadIterator(or);
158
            case GitClient.INDEX:
159
                return new DirCacheIterator(repository.readDirCache());
160
            case GitClient.WORKING_TREE:
161
                return new FileTreeIterator(repository);
162
            default:
163
                CanonicalTreeParser p = new CanonicalTreeParser();
164
                p.reset(or, Utils.findCommit(repository, commit).getTree());
165
                return p;
166
        }
167
    }
168
169
    @Override
169
    @Override
170
    protected String getCommandDescription () {
170
    protected String getCommandDescription () {
171
        StringBuilder sb = new StringBuilder("git diff"); //NOI18N
171
        StringBuilder sb = new StringBuilder("git diff"); //NOI18N
(-)libs.git/test/unit/data/goldenfiles/org/netbeans/libs/git/jgit/commands/ExportDiffTest/diffTwoCommits.patch (+14 lines)
Line 0 Link Here
1
diff --git a/file b/file
2
index c760c24..a3ea492 100644
3
--- a/file
4
+++ b/file
5
@@ -1 +1 @@
6
-FILE 1
7
+FILE 1 CHANGE
8
diff --git a/folder/file2 b/folder/file2
9
index e2035ce..1f26718 100644
10
--- a/folder/file2
11
+++ b/folder/file2
12
@@ -1 +1 @@
13
-FILE 2
14
+FILE 2 CHANGE
(-)libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/ExportDiffTest.java (+27 lines)
Lines 233-238 Link Here
233
        }
233
        }
234
        
234
        
235
    }
235
    }
236
    
237
    public void testDiffTwoCommits () throws Exception {
238
        File file = new File(workDir, "file");
239
        File file2 = new File(workDir, "folder/file2");
240
        file2.getParentFile().mkdirs();
241
        File patchFile = new File(workDir.getParentFile(), "diff.patch");
242
        File[] files = new File[] { file, file2 };
243
        
244
        write(file, "FILE 1\n");
245
        write(file2, "FILE 2\n");
246
        add();
247
        commit();
248
        
249
        write(file, "FILE 1 CHANGE\n");
250
        write(file2, "FILE 2 CHANGE\n");
251
        add();
252
        commit();
253
        
254
        exportDiff(files, patchFile, "master~1", "master");
255
        assertFile(patchFile, getGoldenFile("diffTwoCommits.patch"));
256
    }
236
257
237
    private void exportDiff (File[] files, File patchFile, DiffMode diffMode) throws Exception {
258
    private void exportDiff (File[] files, File patchFile, DiffMode diffMode) throws Exception {
238
        OutputStream out = new BufferedOutputStream(new FileOutputStream(patchFile));
259
        OutputStream out = new BufferedOutputStream(new FileOutputStream(patchFile));
Lines 240-245 Link Here
240
        out.close();
261
        out.close();
241
    }
262
    }
242
263
264
    private void exportDiff (File[] files, File patchFile, String base, String to) throws Exception {
265
        OutputStream out = new BufferedOutputStream(new FileOutputStream(patchFile));
266
        getClient(workDir).exportDiff(files, base, to, out, NULL_PROGRESS_MONITOR);
267
        out.close();
268
    }
269
243
    private void makeInitialCommit () throws Exception {
270
    private void makeInitialCommit () throws Exception {
244
        File f = new File(workDir, "dummy");
271
        File f = new File(workDir, "dummy");
245
        f.createNewFile();
272
        f.createNewFile();

Return to bug 248002