~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ngit/NGit/NGit.Api/ResetCommand.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
                /// twice on an instance.
104
104
                /// </summary>
105
105
                /// <returns>the Ref after reset</returns>
106
 
                /// <exception cref="System.IO.IOException"></exception>
 
106
                /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException
 
107
                ///     </exception>
 
108
                /// <exception cref="NGit.Api.Errors.CheckoutConflictException"></exception>
107
109
                public override Ref Call()
108
110
                {
109
111
                        CheckCallable();
120
122
                                ObjectId commitId;
121
123
                                try
122
124
                                {
123
 
                                        commitId = repo.Resolve(@ref);
 
125
                                        commitId = repo.Resolve(@ref + "^{commit}");
124
126
                                        if (commitId == null)
125
127
                                        {
126
128
                                                // @TODO throw an InvalidRefNameException. We can't do that
166
168
                                        throw new JGitInternalException(MessageFormat.Format(JGitText.Get().cannotLock, ru
167
169
                                                .GetName()));
168
170
                                }
 
171
                                ObjectId origHead = ru.GetOldObjectId();
 
172
                                if (origHead != null)
 
173
                                {
 
174
                                        repo.WriteOrigHead(origHead);
 
175
                                }
169
176
                                switch (mode)
170
177
                                {
171
178
                                        case ResetCommand.ResetType.HARD:
206
213
                                                {
207
214
                                                        ResetCherryPick();
208
215
                                                }
 
216
                                                else
 
217
                                                {
 
218
                                                        if (repo.ReadSquashCommitMsg() != null)
 
219
                                                        {
 
220
                                                                repo.WriteSquashCommitMsg(null);
 
221
                                                        }
 
222
                                                }
209
223
                                        }
210
224
                                }
211
225
                                SetCallable(false);
256
270
                private void ResetIndexForPaths(RevCommit commit)
257
271
                {
258
272
                        DirCache dc = null;
259
 
                        DirCacheEditor edit;
260
273
                        try
261
274
                        {
262
275
                                dc = repo.LockDirCache();
263
 
                                edit = dc.Editor();
 
276
                                DirCacheBuilder builder = dc.Builder();
264
277
                                TreeWalk tw = new TreeWalk(repo);
265
 
                                tw.AddTree(new DirCacheIterator(dc));
 
278
                                tw.AddTree(new DirCacheBuildIterator(builder));
266
279
                                tw.AddTree(commit.Tree);
267
280
                                tw.Filter = PathFilterGroup.CreateFromStrings(filepaths);
268
281
                                tw.Recursive = true;
269
282
                                while (tw.Next())
270
283
                                {
271
 
                                        string path = tw.PathString;
272
 
                                        // DirCacheIterator dci = tw.getTree(0, DirCacheIterator.class);
273
284
                                        CanonicalTreeParser tree = tw.GetTree<CanonicalTreeParser>(1);
274
 
                                        if (tree == null)
275
 
                                        {
276
 
                                                // file is not in the commit, remove from index
277
 
                                                edit.Add(new DirCacheEditor.DeletePath(path));
278
 
                                        }
279
 
                                        else
 
285
                                        // only keep file in index if it's in the commit
 
286
                                        if (tree != null)
280
287
                                        {
281
288
                                                // revert index to commit
282
 
                                                // it seams that there is concurrent access to tree
283
 
                                                // variable, therefore we need to keep references to
284
 
                                                // entryFileMode and entryObjectId in local
285
 
                                                // variables
286
 
                                                FileMode entryFileMode = tree.EntryFileMode;
287
 
                                                ObjectId entryObjectId = tree.EntryObjectId;
288
 
                                                edit.Add(new _PathEdit_294(entryFileMode, entryObjectId, path));
 
289
                                                DirCacheEntry entry = new DirCacheEntry(tw.RawPath);
 
290
                                                entry.FileMode = tree.EntryFileMode;
 
291
                                                entry.SetObjectId(tree.EntryObjectId);
 
292
                                                builder.Add(entry);
289
293
                                        }
290
294
                                }
291
 
                                edit.Commit();
 
295
                                builder.Commit();
292
296
                        }
293
297
                        catch (IOException e)
294
298
                        {
303
307
                        }
304
308
                }
305
309
 
306
 
                private sealed class _PathEdit_294 : DirCacheEditor.PathEdit
307
 
                {
308
 
                        public _PathEdit_294(FileMode entryFileMode, ObjectId entryObjectId, string baseArg1
309
 
                                ) : base(baseArg1)
310
 
                        {
311
 
                                this.entryFileMode = entryFileMode;
312
 
                                this.entryObjectId = entryObjectId;
313
 
                        }
314
 
 
315
 
                        public override void Apply(DirCacheEntry ent)
316
 
                        {
317
 
                                ent.FileMode = entryFileMode;
318
 
                                ent.SetObjectId(entryObjectId);
319
 
                                ent.LastModified = 0;
320
 
                        }
321
 
 
322
 
                        private readonly FileMode entryFileMode;
323
 
 
324
 
                        private readonly ObjectId entryObjectId;
325
 
                }
326
 
 
327
310
                /// <exception cref="System.IO.IOException"></exception>
328
311
                private void ResetIndex(RevCommit commit)
329
312
                {
330
 
                        DirCache dc = null;
 
313
                        DirCache dc = repo.LockDirCache();
 
314
                        TreeWalk walk = null;
331
315
                        try
332
316
                        {
333
 
                                dc = repo.LockDirCache();
334
 
                                dc.Clear();
335
 
                                DirCacheBuilder dcb = dc.Builder();
336
 
                                dcb.AddTree(new byte[0], 0, repo.NewObjectReader(), commit.Tree);
337
 
                                dcb.Commit();
338
 
                        }
339
 
                        catch (IOException e)
340
 
                        {
341
 
                                throw;
 
317
                                DirCacheBuilder builder = dc.Builder();
 
318
                                walk = new TreeWalk(repo);
 
319
                                walk.AddTree(commit.Tree);
 
320
                                walk.AddTree(new DirCacheIterator(dc));
 
321
                                walk.Recursive = true;
 
322
                                while (walk.Next())
 
323
                                {
 
324
                                        AbstractTreeIterator cIter = walk.GetTree<AbstractTreeIterator>(0);
 
325
                                        if (cIter == null)
 
326
                                        {
 
327
                                                // Not in commit, don't add to new index
 
328
                                                continue;
 
329
                                        }
 
330
                                        DirCacheEntry entry = new DirCacheEntry(walk.RawPath);
 
331
                                        entry.FileMode = cIter.EntryFileMode;
 
332
                                        entry.SetObjectIdFromRaw(cIter.IdBuffer, cIter.IdOffset);
 
333
                                        DirCacheIterator dcIter = walk.GetTree<DirCacheIterator>(1);
 
334
                                        if (dcIter != null && dcIter.IdEqual(cIter))
 
335
                                        {
 
336
                                                DirCacheEntry indexEntry = dcIter.GetDirCacheEntry();
 
337
                                                entry.LastModified = indexEntry.LastModified;
 
338
                                                entry.SetLength(indexEntry.Length);
 
339
                                        }
 
340
                                        builder.Add(entry);
 
341
                                }
 
342
                                builder.Commit();
342
343
                        }
343
344
                        finally
344
345
                        {
345
 
                                if (dc != null)
 
346
                                dc.Unlock();
 
347
                                if (walk != null)
346
348
                                {
347
 
                                        dc.Unlock();
 
349
                                        walk.Release();
348
350
                                }
349
351
                        }
350
352
                }
351
353
 
352
354
                /// <exception cref="System.IO.IOException"></exception>
 
355
                /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
353
356
                private void CheckoutIndex(RevCommit commit)
354
357
                {
355
 
                        DirCache dc = null;
 
358
                        DirCache dc = repo.LockDirCache();
356
359
                        try
357
360
                        {
358
 
                                dc = repo.LockDirCache();
359
361
                                DirCacheCheckout checkout = new DirCacheCheckout(repo, dc, commit.Tree);
360
362
                                checkout.SetFailOnConflict(false);
361
 
                                checkout.Checkout();
362
 
                        }
363
 
                        catch (IOException e)
364
 
                        {
365
 
                                throw;
 
363
                                try
 
364
                                {
 
365
                                        checkout.Checkout();
 
366
                                }
 
367
                                catch (NGit.Errors.CheckoutConflictException cce)
 
368
                                {
 
369
                                        throw new NGit.Api.Errors.CheckoutConflictException(checkout.GetConflicts(), cce);
 
370
                                }
366
371
                        }
367
372
                        finally
368
373
                        {
369
 
                                if (dc != null)
370
 
                                {
371
 
                                        dc.Unlock();
372
 
                                }
 
374
                                dc.Unlock();
373
375
                        }
374
376
                }
375
377