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

« back to all changes in this revision

Viewing changes to external/ngit/NGit.Test/NGit.Api/CommitCommandTest.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:
41
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
42
*/
43
43
 
 
44
using System;
44
45
using System.Collections.Generic;
45
46
using System.Diagnostics;
46
47
using NGit;
47
48
using NGit.Api;
48
49
using NGit.Diff;
 
50
using NGit.Dircache;
49
51
using NGit.Revwalk;
50
52
using NGit.Storage.File;
51
53
using NGit.Submodule;
59
61
        /// <summary>
60
62
        /// Unit tests of
61
63
        /// <see cref="CommitCommand">CommitCommand</see>
 
64
        /// .
62
65
        /// </summary>
63
66
        [NUnit.Framework.TestFixture]
64
67
        public class CommitCommandTest : RepositoryTestCase
71
74
                        config.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE
72
75
                                , true);
73
76
                        config.Save();
74
 
                        FS executableFs = new _FS_81();
 
77
                        FS executableFs = new _FS_87();
75
78
                        Git git = Git.Open(db.Directory, executableFs);
76
79
                        string path = "a.txt";
77
80
                        WriteTrashFile(path, "content");
80
83
                        TreeWalk walk = TreeWalk.ForPath(db, path, commit1.Tree);
81
84
                        NUnit.Framework.Assert.IsNotNull(walk);
82
85
                        NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, walk.GetFileMode(0));
83
 
                        FS nonExecutableFs = new _FS_121();
 
86
                        FS nonExecutableFs = new _FS_132();
84
87
                        config = ((FileBasedConfig)db.GetConfig());
85
88
                        config.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE
86
89
                                , false);
93
96
                        NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, walk.GetFileMode(0));
94
97
                }
95
98
 
96
 
                private sealed class _FS_81 : FS
 
99
                private sealed class _FS_87 : FS
97
100
                {
98
 
                        public _FS_81()
 
101
                        public _FS_87()
99
102
                        {
100
103
                        }
101
104
 
133
136
                        {
134
137
                                return true;
135
138
                        }
 
139
 
 
140
                        public override bool IsCaseSensitive()
 
141
                        {
 
142
                                return true;
 
143
                        }
136
144
                }
137
145
 
138
 
                private sealed class _FS_121 : FS
 
146
                private sealed class _FS_132 : FS
139
147
                {
140
 
                        public _FS_121()
 
148
                        public _FS_132()
141
149
                        {
142
150
                        }
143
151
 
174
182
                        public override bool CanExecute(FilePath f)
175
183
                        {
176
184
                                return false;
 
185
                        }
 
186
 
 
187
                        public override bool IsCaseSensitive()
 
188
                        {
 
189
                                return true;
177
190
                        }
178
191
                }
179
192
 
192
205
                        command.SetURI(uri);
193
206
                        Repository repo = command.Call();
194
207
                        NUnit.Framework.Assert.IsNotNull(repo);
 
208
                        AddRepoToClose(repo);
195
209
                        SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);
196
210
                        NUnit.Framework.Assert.IsTrue(generator.Next());
197
211
                        NUnit.Framework.Assert.AreEqual(path, generator.GetPath());
199
213
                        NUnit.Framework.Assert.AreEqual(uri, generator.GetModulesUrl());
200
214
                        NUnit.Framework.Assert.AreEqual(path, generator.GetModulesPath());
201
215
                        NUnit.Framework.Assert.AreEqual(uri, generator.GetConfigUrl());
202
 
                        NUnit.Framework.Assert.IsNotNull(generator.GetRepository());
 
216
                        Repository subModRepo = generator.GetRepository();
 
217
                        AddRepoToClose(subModRepo);
 
218
                        NUnit.Framework.Assert.IsNotNull(subModRepo);
203
219
                        NUnit.Framework.Assert.AreEqual(commit, repo.Resolve(Constants.HEAD));
204
220
                        RevCommit submoduleCommit = git.Commit().SetMessage("submodule add").SetOnly(path
205
221
                                ).Call();
236
252
                        command.SetURI(uri);
237
253
                        Repository repo = command.Call();
238
254
                        NUnit.Framework.Assert.IsNotNull(repo);
 
255
                        AddRepoToClose(repo);
239
256
                        SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);
240
257
                        NUnit.Framework.Assert.IsTrue(generator.Next());
241
258
                        NUnit.Framework.Assert.AreEqual(path, generator.GetPath());
243
260
                        NUnit.Framework.Assert.AreEqual(uri, generator.GetModulesUrl());
244
261
                        NUnit.Framework.Assert.AreEqual(path, generator.GetModulesPath());
245
262
                        NUnit.Framework.Assert.AreEqual(uri, generator.GetConfigUrl());
246
 
                        NUnit.Framework.Assert.IsNotNull(generator.GetRepository());
 
263
                        Repository subModRepo = generator.GetRepository();
 
264
                        AddRepoToClose(subModRepo);
 
265
                        NUnit.Framework.Assert.IsNotNull(subModRepo);
247
266
                        NUnit.Framework.Assert.AreEqual(commit2, repo.Resolve(Constants.HEAD));
248
267
                        RevCommit submoduleAddCommit = git.Commit().SetMessage("submodule add").SetOnly(path
249
268
                                ).Call();
268
287
                        NUnit.Framework.Assert.AreEqual(path, subDiff.GetNewPath());
269
288
                        NUnit.Framework.Assert.AreEqual(path, subDiff.GetOldPath());
270
289
                }
 
290
 
 
291
                /// <exception cref="System.Exception"></exception>
 
292
                [NUnit.Framework.Test]
 
293
                public virtual void CommitUpdatesSmudgedEntries()
 
294
                {
 
295
                        Git git = new Git(db);
 
296
                        FilePath file1 = WriteTrashFile("file1.txt", "content1");
 
297
                        NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() - 5000));
 
298
                        FilePath file2 = WriteTrashFile("file2.txt", "content2");
 
299
                        NUnit.Framework.Assert.IsTrue(file2.SetLastModified(file2.LastModified() - 5000));
 
300
                        FilePath file3 = WriteTrashFile("file3.txt", "content3");
 
301
                        NUnit.Framework.Assert.IsTrue(file3.SetLastModified(file3.LastModified() - 5000));
 
302
                        NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("file1.txt").AddFilepattern
 
303
                                ("file2.txt").AddFilepattern("file3.txt").Call());
 
304
                        RevCommit commit = git.Commit().SetMessage("add files").Call();
 
305
                        NUnit.Framework.Assert.IsNotNull(commit);
 
306
                        DirCache cache = DirCache.Read(db.GetIndexFile(), db.FileSystem);
 
307
                        int file1Size = cache.GetEntry("file1.txt").Length;
 
308
                        int file2Size = cache.GetEntry("file2.txt").Length;
 
309
                        int file3Size = cache.GetEntry("file3.txt").Length;
 
310
                        ObjectId file2Id = cache.GetEntry("file2.txt").GetObjectId();
 
311
                        ObjectId file3Id = cache.GetEntry("file3.txt").GetObjectId();
 
312
                        NUnit.Framework.Assert.IsTrue(file1Size > 0);
 
313
                        NUnit.Framework.Assert.IsTrue(file2Size > 0);
 
314
                        NUnit.Framework.Assert.IsTrue(file3Size > 0);
 
315
                        // Smudge entries
 
316
                        cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem);
 
317
                        cache.GetEntry("file1.txt").SetLength(0);
 
318
                        cache.GetEntry("file2.txt").SetLength(0);
 
319
                        cache.GetEntry("file3.txt").SetLength(0);
 
320
                        cache.Write();
 
321
                        NUnit.Framework.Assert.IsTrue(cache.Commit());
 
322
                        // Verify entries smudged
 
323
                        cache = DirCache.Read(db.GetIndexFile(), db.FileSystem);
 
324
                        NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file1.txt").Length);
 
325
                        NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file2.txt").Length);
 
326
                        NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file3.txt").Length);
 
327
                        long indexTime = db.GetIndexFile().LastModified();
 
328
                        db.GetIndexFile().SetLastModified(indexTime - 5000);
 
329
                        Write(file1, "content4");
 
330
                        NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() + 2500));
 
331
                        NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("edit file").SetOnly("file1.txt"
 
332
                                ).Call());
 
333
                        cache = db.ReadDirCache();
 
334
                        NUnit.Framework.Assert.AreEqual(file1Size, cache.GetEntry("file1.txt").Length);
 
335
                        NUnit.Framework.Assert.AreEqual(file2Size, cache.GetEntry("file2.txt").Length);
 
336
                        NUnit.Framework.Assert.AreEqual(file3Size, cache.GetEntry("file3.txt").Length);
 
337
                        NUnit.Framework.Assert.AreEqual(file2Id, cache.GetEntry("file2.txt").GetObjectId(
 
338
                                ));
 
339
                        NUnit.Framework.Assert.AreEqual(file3Id, cache.GetEntry("file3.txt").GetObjectId(
 
340
                                ));
 
341
                }
 
342
 
 
343
                /// <exception cref="System.Exception"></exception>
 
344
                [NUnit.Framework.Test]
 
345
                public virtual void CommitIgnoresSmudgedEntryWithDifferentId()
 
346
                {
 
347
                        Git git = new Git(db);
 
348
                        FilePath file1 = WriteTrashFile("file1.txt", "content1");
 
349
                        NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() - 5000));
 
350
                        FilePath file2 = WriteTrashFile("file2.txt", "content2");
 
351
                        NUnit.Framework.Assert.IsTrue(file2.SetLastModified(file2.LastModified() - 5000));
 
352
                        NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("file1.txt").AddFilepattern
 
353
                                ("file2.txt").Call());
 
354
                        RevCommit commit = git.Commit().SetMessage("add files").Call();
 
355
                        NUnit.Framework.Assert.IsNotNull(commit);
 
356
                        DirCache cache = DirCache.Read(db.GetIndexFile(), db.FileSystem);
 
357
                        int file1Size = cache.GetEntry("file1.txt").Length;
 
358
                        int file2Size = cache.GetEntry("file2.txt").Length;
 
359
                        NUnit.Framework.Assert.IsTrue(file1Size > 0);
 
360
                        NUnit.Framework.Assert.IsTrue(file2Size > 0);
 
361
                        WriteTrashFile("file2.txt", "content3");
 
362
                        NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("file2.txt").Call());
 
363
                        WriteTrashFile("file2.txt", "content4");
 
364
                        // Smudge entries
 
365
                        cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem);
 
366
                        cache.GetEntry("file1.txt").SetLength(0);
 
367
                        cache.GetEntry("file2.txt").SetLength(0);
 
368
                        cache.Write();
 
369
                        NUnit.Framework.Assert.IsTrue(cache.Commit());
 
370
                        // Verify entries smudged
 
371
                        cache = db.ReadDirCache();
 
372
                        NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file1.txt").Length);
 
373
                        NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file2.txt").Length);
 
374
                        long indexTime = db.GetIndexFile().LastModified();
 
375
                        db.GetIndexFile().SetLastModified(indexTime - 5000);
 
376
                        Write(file1, "content5");
 
377
                        NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() + 1000));
 
378
                        NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("edit file").SetOnly("file1.txt"
 
379
                                ).Call());
 
380
                        cache = db.ReadDirCache();
 
381
                        NUnit.Framework.Assert.AreEqual(file1Size, cache.GetEntry("file1.txt").Length);
 
382
                        NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file2.txt").Length);
 
383
                }
 
384
 
 
385
                /// <exception cref="System.Exception"></exception>
 
386
                [NUnit.Framework.Test]
 
387
                public virtual void CommitAfterSquashMerge()
 
388
                {
 
389
                        Git git = new Git(db);
 
390
                        WriteTrashFile("file1", "file1");
 
391
                        git.Add().AddFilepattern("file1").Call();
 
392
                        RevCommit first = git.Commit().SetMessage("initial commit").Call();
 
393
                        NUnit.Framework.Assert.IsTrue(new FilePath(db.WorkTree, "file1").Exists());
 
394
                        CreateBranch(first, "refs/heads/branch1");
 
395
                        CheckoutBranch("refs/heads/branch1");
 
396
                        WriteTrashFile("file2", "file2");
 
397
                        git.Add().AddFilepattern("file2").Call();
 
398
                        git.Commit().SetMessage("second commit").Call();
 
399
                        NUnit.Framework.Assert.IsTrue(new FilePath(db.WorkTree, "file2").Exists());
 
400
                        CheckoutBranch("refs/heads/master");
 
401
                        MergeCommandResult result = git.Merge().Include(db.GetRef("branch1")).SetSquash(true
 
402
                                ).Call();
 
403
                        NUnit.Framework.Assert.IsTrue(new FilePath(db.WorkTree, "file1").Exists());
 
404
                        NUnit.Framework.Assert.IsTrue(new FilePath(db.WorkTree, "file2").Exists());
 
405
                        NUnit.Framework.Assert.AreEqual(MergeStatus.FAST_FORWARD_SQUASHED, result.GetMergeStatus
 
406
                                ());
 
407
                        // comment not set, should be inferred from SQUASH_MSG
 
408
                        RevCommit squashedCommit = git.Commit().Call();
 
409
                        NUnit.Framework.Assert.AreEqual(1, squashedCommit.ParentCount);
 
410
                        NUnit.Framework.Assert.IsNull(db.ReadSquashCommitMsg());
 
411
                        NUnit.Framework.Assert.AreEqual("commit: Squashed commit of the following:", db.GetReflogReader
 
412
                                (Constants.HEAD).GetLastEntry().GetComment());
 
413
                        NUnit.Framework.Assert.AreEqual("commit: Squashed commit of the following:", db.GetReflogReader
 
414
                                (db.GetBranch()).GetLastEntry().GetComment());
 
415
                }
 
416
 
 
417
                /// <exception cref="System.Exception"></exception>
 
418
                public virtual void CommitAmendOnInitialShouldFail()
 
419
                {
 
420
                        Git git = new Git(db);
 
421
                        git.Commit().SetAmend(true).SetMessage("initial commit").Call();
 
422
                }
 
423
 
 
424
                /// <exception cref="System.Exception"></exception>
 
425
                [NUnit.Framework.Test]
 
426
                public virtual void CommitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime(
 
427
                        )
 
428
                {
 
429
                        Git git = new Git(db);
 
430
                        WriteTrashFile("file1", "file1");
 
431
                        git.Add().AddFilepattern("file1").Call();
 
432
                        string authorName = "First Author";
 
433
                        string authorEmail = "author@example.org";
 
434
                        DateTime authorDate = Sharpen.Extensions.CreateDate(1349621117000L);
 
435
                        PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail, authorDate, Sharpen.Extensions.GetTimeZone
 
436
                                ("UTC"));
 
437
                        git.Commit().SetMessage("initial commit").SetAuthor(firstAuthor).Call();
 
438
                        RevCommit amended = git.Commit().SetAmend(true).SetMessage("amend commit").Call();
 
439
                        PersonIdent amendedAuthor = amended.GetAuthorIdent();
 
440
                        NUnit.Framework.Assert.AreEqual(authorName, amendedAuthor.GetName());
 
441
                        NUnit.Framework.Assert.AreEqual(authorEmail, amendedAuthor.GetEmailAddress());
 
442
                        NUnit.Framework.Assert.AreEqual(authorDate.GetTime(), amendedAuthor.GetWhen().GetTime
 
443
                                ());
 
444
                }
 
445
 
 
446
                /// <exception cref="System.Exception"></exception>
 
447
                [NUnit.Framework.Test]
 
448
                public virtual void CommitAmendWithAuthorShouldUseIt()
 
449
                {
 
450
                        Git git = new Git(db);
 
451
                        WriteTrashFile("file1", "file1");
 
452
                        git.Add().AddFilepattern("file1").Call();
 
453
                        git.Commit().SetMessage("initial commit").Call();
 
454
                        RevCommit amended = git.Commit().SetAmend(true).SetAuthor("New Author", "newauthor@example.org"
 
455
                                ).SetMessage("amend commit").Call();
 
456
                        PersonIdent amendedAuthor = amended.GetAuthorIdent();
 
457
                        NUnit.Framework.Assert.AreEqual("New Author", amendedAuthor.GetName());
 
458
                        NUnit.Framework.Assert.AreEqual("newauthor@example.org", amendedAuthor.GetEmailAddress
 
459
                                ());
 
460
                }
271
461
        }
272
462
}