コード例 #1
0
        public virtual void TestBuildOneFile_Commit_IndexChangedEvent()
        {
            // empty
            string   path = "a-file-path";
            FileMode mode = FileMode.REGULAR_FILE;
            // "old" date in 2008
            long                 lastModified = 1218123387057L;
            int                  length       = 1342;
            DirCacheEntry        entOrig;
            bool                 receivedEvent = false;
            DirCache             dc            = db.LockDirCache();
            IndexChangedListener listener      = new _IndexChangedListener_212();
            ListenerList         l             = db.Listeners;

            l.AddIndexChangedListener(listener);
            DirCacheBuilder b = dc.Builder();

            entOrig              = new DirCacheEntry(path);
            entOrig.FileMode     = mode;
            entOrig.LastModified = lastModified;
            entOrig.SetLength(length);
            b.Add(entOrig);
            try
            {
                b.Commit();
            }
            catch (_T123327308)
            {
                receivedEvent = true;
            }
            if (!receivedEvent)
            {
                NUnit.Framework.Assert.Fail("did not receive IndexChangedEvent");
            }
            // do the same again, as this doesn't change index compared to first
            // round we should get no event this time
            dc       = db.LockDirCache();
            listener = new _IndexChangedListener_239();
            l        = db.Listeners;
            l.AddIndexChangedListener(listener);
            b                    = dc.Builder();
            entOrig              = new DirCacheEntry(path);
            entOrig.FileMode     = mode;
            entOrig.LastModified = lastModified;
            entOrig.SetLength(length);
            b.Add(entOrig);
            try
            {
                b.Commit();
            }
            catch (_T123327308)
            {
                NUnit.Framework.Assert.Fail("unexpected IndexChangedEvent");
            }
        }
コード例 #2
0
        /// <summary>
        /// Updates the file in the working tree with content and mode from an entry
        /// in the index.
        /// </summary>
        /// <remarks>
        /// Updates the file in the working tree with content and mode from an entry
        /// in the index. The new content is first written to a new temporary file in
        /// the same directory as the real file. Then that new file is renamed to the
        /// final filename.
        /// TODO: this method works directly on File IO, we may need another
        /// abstraction (like WorkingTreeIterator). This way we could tell e.g.
        /// Eclipse that Files in the workspace got changed
        /// </remarks>
        /// <param name="repo"></param>
        /// <param name="f">
        /// the file to be modified. The parent directory for this file
        /// has to exist already
        /// </param>
        /// <param name="entry">the entry containing new mode and content</param>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public static void CheckoutEntry(Repository repo, FilePath f, DirCacheEntry entry
                                         )
        {
            ObjectLoader     ol        = repo.Open(entry.GetObjectId());
            FilePath         parentDir = f.GetParentFile();
            FilePath         tmpFile   = FilePath.CreateTempFile("._" + f.GetName(), null, parentDir);
            FileOutputStream channel   = new FileOutputStream(tmpFile);

            try
            {
                ol.CopyTo(channel);
            }
            finally
            {
                channel.Close();
            }
            FS fs = repo.FileSystem;
            WorkingTreeOptions opt = repo.GetConfig().Get(WorkingTreeOptions.KEY);

            if (opt.IsFileMode() && fs.SupportsExecute())
            {
                if (FileMode.EXECUTABLE_FILE.Equals(entry.RawMode))
                {
                    if (!fs.CanExecute(tmpFile))
                    {
                        fs.SetExecute(tmpFile, true);
                    }
                }
                else
                {
                    if (fs.CanExecute(tmpFile))
                    {
                        fs.SetExecute(tmpFile, false);
                    }
                }
            }
            if (!tmpFile.RenameTo(f))
            {
                // tried to rename which failed. Let' delete the target file and try
                // again
                FileUtils.Delete(f);
                if (!tmpFile.RenameTo(f))
                {
                    throw new IOException(MessageFormat.Format(JGitText.Get().couldNotWriteFile, tmpFile
                                                               .GetPath(), f.GetPath()));
                }
            }
            entry.LastModified = f.LastModified();
            entry.SetLength((int)ol.GetSize());
        }
コード例 #3
0
        /// <summary>Update any smudged entries with information from the working tree.</summary>
        /// <remarks>Update any smudged entries with information from the working tree.</remarks>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        private void UpdateSmudgedEntries()
        {
            TreeWalk       walk  = new TreeWalk(repository);
            IList <string> paths = new AList <string>(128);

            try
            {
                for (int i = 0; i < entryCnt; i++)
                {
                    if (sortedEntries[i].IsSmudged)
                    {
                        paths.AddItem(sortedEntries[i].PathString);
                    }
                }
                if (paths.IsEmpty())
                {
                    return;
                }
                walk.Filter = PathFilterGroup.CreateFromStrings(paths);
                DirCacheIterator iIter = new DirCacheIterator(this);
                FileTreeIterator fIter = new FileTreeIterator(repository);
                walk.AddTree(iIter);
                walk.AddTree(fIter);
                walk.Recursive = true;
                while (walk.Next())
                {
                    iIter = walk.GetTree <DirCacheIterator>(0);
                    if (iIter == null)
                    {
                        continue;
                    }
                    fIter = walk.GetTree <FileTreeIterator>(1);
                    if (fIter == null)
                    {
                        continue;
                    }
                    DirCacheEntry entry = iIter.GetDirCacheEntry();
                    if (entry.IsSmudged && iIter.IdEqual(fIter))
                    {
                        entry.SetLength(fIter.GetEntryLength());
                        entry.LastModified = fIter.GetEntryLastModified();
                    }
                }
            }
            finally
            {
                walk.Release();
            }
        }
コード例 #4
0
 public virtual void TestBuildOneFile_FinishWriteCommit()
 {
     string        path         = "a-file-path";
     FileMode      mode         = FileMode.REGULAR_FILE;
     long          lastModified = 1218123387057L;
     int           length       = 1342;
     DirCacheEntry entOrig;
     {
         DirCache        dc = db.LockDirCache();
         DirCacheBuilder b  = dc.Builder();
         NUnit.Framework.Assert.IsNotNull(b);
         entOrig              = new DirCacheEntry(path);
         entOrig.FileMode     = mode;
         entOrig.LastModified = lastModified;
         entOrig.SetLength(length);
         NUnit.Framework.Assert.AreNotSame(path, entOrig.PathString);
         NUnit.Framework.Assert.AreEqual(path, entOrig.PathString);
         NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, entOrig.GetObjectId());
         NUnit.Framework.Assert.AreEqual(mode.GetBits(), entOrig.RawMode);
         NUnit.Framework.Assert.AreEqual(0, entOrig.Stage);
         NUnit.Framework.Assert.AreEqual(lastModified, entOrig.LastModified);
         NUnit.Framework.Assert.AreEqual(length, entOrig.Length);
         NUnit.Framework.Assert.IsFalse(entOrig.IsAssumeValid);
         b.Add(entOrig);
         b.Finish();
         NUnit.Framework.Assert.AreEqual(1, dc.GetEntryCount());
         NUnit.Framework.Assert.AreSame(entOrig, dc.GetEntry(0));
         dc.Write();
         NUnit.Framework.Assert.IsTrue(dc.Commit());
     }
     {
         DirCache dc = db.ReadDirCache();
         NUnit.Framework.Assert.AreEqual(1, dc.GetEntryCount());
         DirCacheEntry entRead = dc.GetEntry(0);
         NUnit.Framework.Assert.AreNotSame(entOrig, entRead);
         NUnit.Framework.Assert.AreEqual(path, entRead.PathString);
         NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, entOrig.GetObjectId());
         NUnit.Framework.Assert.AreEqual(mode.GetBits(), entOrig.RawMode);
         NUnit.Framework.Assert.AreEqual(0, entOrig.Stage);
         NUnit.Framework.Assert.AreEqual(lastModified, entOrig.LastModified);
         NUnit.Framework.Assert.AreEqual(length, entOrig.Length);
         NUnit.Framework.Assert.IsFalse(entOrig.IsAssumeValid);
     }
 }
コード例 #5
0
        private void CopyMetaDataHelper(bool keepStage)
        {
            DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_2);

            e.IsAssumeValid = false;
            e.SetCreationTime(2L);
            e.FileMode     = FileMode.EXECUTABLE_FILE;
            e.LastModified = 3L;
            e.SetLength(100L);
            e.SetObjectId(ObjectId.FromString("0123456789012345678901234567890123456789"));
            e.IsUpdateNeeded = true;
            DirCacheEntry f = new DirCacheEntry("someother/path", DirCacheEntry.STAGE_1);

            f.IsAssumeValid = true;
            f.SetCreationTime(10L);
            f.FileMode     = FileMode.SYMLINK;
            f.LastModified = 20L;
            f.SetLength(100000000L);
            f.SetObjectId(ObjectId.FromString("1234567890123456789012345678901234567890"));
            f.IsUpdateNeeded = true;
            e.CopyMetaData(f, keepStage);
            NUnit.Framework.Assert.IsTrue(e.IsAssumeValid);
            NUnit.Framework.Assert.AreEqual(10L, e.GetCreationTime());
            NUnit.Framework.Assert.AreEqual(ObjectId.FromString("1234567890123456789012345678901234567890"
                                                                ), e.GetObjectId());
            NUnit.Framework.Assert.AreEqual(FileMode.SYMLINK, e.FileMode);
            NUnit.Framework.Assert.AreEqual(20L, e.LastModified);
            NUnit.Framework.Assert.AreEqual(100000000L, e.Length);
            if (keepStage)
            {
                NUnit.Framework.Assert.AreEqual(DirCacheEntry.STAGE_2, e.Stage);
            }
            else
            {
                NUnit.Framework.Assert.AreEqual(DirCacheEntry.STAGE_1, e.Stage);
            }
            NUnit.Framework.Assert.IsTrue(e.IsUpdateNeeded);
            NUnit.Framework.Assert.AreEqual("some/path", e.PathString);
        }
コード例 #6
0
 public override void Apply(DirCacheEntry ent)
 {
     ent.FileMode = FileMode.REGULAR_FILE;
     ent.SetLength(length);
     ent.SetObjectId(data);
 }
コード例 #7
0
 public override void Apply(DirCacheEntry ent)
 {
     ent.FileMode = FileMode.REGULAR_FILE;
     ent.SetLength(1);
     ent.SetObjectId(ObjectId.ZeroId);
 }
コード例 #8
0
ファイル: AddCommandTest.cs プロジェクト: stinos/ngit
        /// <exception cref="System.IO.IOException"></exception>
        private DirCacheEntry AddEntryToBuilder(string path, FilePath file, ObjectInserter
			 newObjectInserter, DirCacheBuilder builder, int stage)
        {
            FileInputStream inputStream = new FileInputStream(file);
            ObjectId id = newObjectInserter.Insert(Constants.OBJ_BLOB, file.Length(), inputStream
                );
            inputStream.Close();
            DirCacheEntry entry = new DirCacheEntry(path, stage);
            entry.SetObjectId(id);
            entry.FileMode = FileMode.REGULAR_FILE;
            entry.LastModified = file.LastModified();
            entry.SetLength((int)file.Length());
            builder.Add(entry);
            return entry;
        }
コード例 #9
0
ファイル: ResolveMerger.cs プロジェクト: kenji-tan/ngit
        /// <summary>Updates the index after a content merge has happened.</summary>
        /// <remarks>
        /// Updates the index after a content merge has happened. If no conflict has
        /// occurred this includes persisting the merged content to the object
        /// database. In case of conflicts this method takes care to write the
        /// correct stages to the index.
        /// </remarks>
        /// <param name="base"></param>
        /// <param name="ours"></param>
        /// <param name="theirs"></param>
        /// <param name="result"></param>
        /// <param name="of"></param>
        /// <exception cref="System.IO.FileNotFoundException">System.IO.FileNotFoundException
        /// 	</exception>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        private void UpdateIndex(CanonicalTreeParser @base, CanonicalTreeParser ours, CanonicalTreeParser
			 theirs, MergeResult<RawText> result, FilePath of)
        {
            if (result.ContainsConflicts())
            {
                // a conflict occurred, the file will contain conflict markers
                // the index will be populated with the three stages and only the
                // workdir (if used) contains the halfways merged content
                Add(tw.RawPath, @base, DirCacheEntry.STAGE_1);
                Add(tw.RawPath, ours, DirCacheEntry.STAGE_2);
                Add(tw.RawPath, theirs, DirCacheEntry.STAGE_3);
                mergeResults.Put(tw.PathString, result.Upcast ());
            }
            else
            {
                // no conflict occurred, the file will contain fully merged content.
                // the index will be populated with the new merged version
                DirCacheEntry dce = new DirCacheEntry(tw.PathString);
                int newMode = MergeFileModes(tw.GetRawMode(0), tw.GetRawMode(1), tw.GetRawMode(2)
                    );
                // set the mode for the new content. Fall back to REGULAR_FILE if
                // you can't merge modes of OURS and THEIRS
                dce.FileMode = (newMode == FileMode.MISSING.GetBits()) ? FileMode.REGULAR_FILE :
                    FileMode.FromBits(newMode);
                dce.LastModified = of.LastModified();
                dce.SetLength((int)of.Length());
                InputStream @is = new FileInputStream(of);
                try
                {
                    dce.SetObjectId(oi.Insert(Constants.OBJ_BLOB, of.Length(), @is));
                }
                finally
                {
                    @is.Close();
                    if (inCore)
                    {
                        FileUtils.Delete(of);
                    }
                }
                builder.Add(dce);
            }
        }
コード例 #10
0
ファイル: ResolveMerger.cs プロジェクト: stinos/ngit
 /// <summary>
 /// adds a entry to the index builder which is a copy of the specified
 /// DirCacheEntry
 /// </summary>
 /// <param name="e">the entry which should be copied</param>
 /// <returns>the entry which was added to the index</returns>
 private DirCacheEntry Keep(DirCacheEntry e)
 {
     DirCacheEntry newEntry = new DirCacheEntry(e.PathString, e.Stage);
     newEntry.FileMode = e.FileMode;
     newEntry.SetObjectId(e.GetObjectId());
     newEntry.LastModified = e.LastModified;
     newEntry.SetLength(e.Length);
     builder.Add(newEntry);
     return newEntry;
 }
コード例 #11
0
ファイル: ResolveMerger.cs プロジェクト: stinos/ngit
        /// <summary>adds a new path with the specified stage to the index builder</summary>
        /// <param name="path"></param>
        /// <param name="p"></param>
        /// <param name="stage"></param>
        /// <param name="lastMod"></param>
        /// <param name="len"></param>
        /// <returns>the entry which was added to the index</returns>
        private DirCacheEntry Add(byte[] path, CanonicalTreeParser p, int stage, long lastMod
			, long len)
        {
            if (p != null && !p.EntryFileMode.Equals(FileMode.TREE))
            {
                DirCacheEntry e = new DirCacheEntry(path, stage);
                e.FileMode = p.EntryFileMode;
                e.SetObjectId(p.EntryObjectId);
                e.LastModified = lastMod;
                e.SetLength(len);
                builder.Add(e);
                return e;
            }
            return null;
        }
コード例 #12
0
		private void CopyMetaDataHelper(bool keepStage)
		{
			DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_2);
			e.IsAssumeValid = false;
			e.SetCreationTime(2L);
			e.FileMode = FileMode.EXECUTABLE_FILE;
			e.LastModified = 3L;
			e.SetLength(100L);
			e.SetObjectId(ObjectId.FromString("0123456789012345678901234567890123456789"));
			e.IsUpdateNeeded = true;
			DirCacheEntry f = new DirCacheEntry("someother/path", DirCacheEntry.STAGE_1);
			f.IsAssumeValid = true;
			f.SetCreationTime(10L);
			f.FileMode = FileMode.SYMLINK;
			f.LastModified = 20L;
			f.SetLength(100000000L);
			f.SetObjectId(ObjectId.FromString("1234567890123456789012345678901234567890"));
			f.IsUpdateNeeded = true;
			e.CopyMetaData(f, keepStage);
			NUnit.Framework.Assert.IsTrue(e.IsAssumeValid);
			NUnit.Framework.Assert.AreEqual(10L, e.GetCreationTime());
			NUnit.Framework.Assert.AreEqual(ObjectId.FromString("1234567890123456789012345678901234567890"
				), e.GetObjectId());
			NUnit.Framework.Assert.AreEqual(FileMode.SYMLINK, e.FileMode);
			NUnit.Framework.Assert.AreEqual(20L, e.LastModified);
			NUnit.Framework.Assert.AreEqual(100000000L, e.Length);
			if (keepStage)
			{
				NUnit.Framework.Assert.AreEqual(DirCacheEntry.STAGE_2, e.Stage);
			}
			else
			{
				NUnit.Framework.Assert.AreEqual(DirCacheEntry.STAGE_1, e.Stage);
			}
			NUnit.Framework.Assert.IsTrue(e.IsUpdateNeeded);
			NUnit.Framework.Assert.AreEqual("some/path", e.PathString);
		}
コード例 #13
0
			public override void Apply(DirCacheEntry ent)
			{
				ent.FileMode = FileMode.REGULAR_FILE;
				ent.SetLength(1);
				ent.SetObjectId(ObjectId.ZeroId);
			}
コード例 #14
0
ファイル: DirCacheBuilderTest.cs プロジェクト: shoff/ngit
		public virtual void TestBuildOneFile_FinishWriteCommit()
		{
			string path = "a-file-path";
			FileMode mode = FileMode.REGULAR_FILE;
			long lastModified = 1218123387057L;
			int length = 1342;
			DirCacheEntry entOrig;
			{
				DirCache dc = db.LockDirCache();
				DirCacheBuilder b = dc.Builder();
				NUnit.Framework.Assert.IsNotNull(b);
				entOrig = new DirCacheEntry(path);
				entOrig.FileMode = mode;
				entOrig.LastModified = lastModified;
				entOrig.SetLength(length);
				NUnit.Framework.Assert.AreNotSame(path, entOrig.PathString);
				NUnit.Framework.Assert.AreEqual(path, entOrig.PathString);
				NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, entOrig.GetObjectId());
				NUnit.Framework.Assert.AreEqual(mode.GetBits(), entOrig.RawMode);
				NUnit.Framework.Assert.AreEqual(0, entOrig.Stage);
				NUnit.Framework.Assert.AreEqual(lastModified, entOrig.LastModified);
				NUnit.Framework.Assert.AreEqual(length, entOrig.Length);
				NUnit.Framework.Assert.IsFalse(entOrig.IsAssumeValid);
				b.Add(entOrig);
				b.Finish();
				NUnit.Framework.Assert.AreEqual(1, dc.GetEntryCount());
				NUnit.Framework.Assert.AreSame(entOrig, dc.GetEntry(0));
				dc.Write();
				NUnit.Framework.Assert.IsTrue(dc.Commit());
			}
			{
				DirCache dc = db.ReadDirCache();
				NUnit.Framework.Assert.AreEqual(1, dc.GetEntryCount());
				DirCacheEntry entRead = dc.GetEntry(0);
				NUnit.Framework.Assert.AreNotSame(entOrig, entRead);
				NUnit.Framework.Assert.AreEqual(path, entRead.PathString);
				NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, entOrig.GetObjectId());
				NUnit.Framework.Assert.AreEqual(mode.GetBits(), entOrig.RawMode);
				NUnit.Framework.Assert.AreEqual(0, entOrig.Stage);
				NUnit.Framework.Assert.AreEqual(lastModified, entOrig.LastModified);
				NUnit.Framework.Assert.AreEqual(length, entOrig.Length);
				NUnit.Framework.Assert.IsFalse(entOrig.IsAssumeValid);
			}
		}
コード例 #15
0
		/// <exception cref="System.IO.FileNotFoundException"></exception>
		/// <exception cref="System.InvalidOperationException"></exception>
		/// <exception cref="System.IO.IOException"></exception>
		private bool ContentMerge(CanonicalTreeParser @base, CanonicalTreeParser ours, CanonicalTreeParser
			 theirs)
		{
			MergeFormatter fmt = new MergeFormatter();
			RawText baseText = @base == null ? RawText.EMPTY_TEXT : GetRawText(@base.EntryObjectId
				, db);
			// do the merge
			MergeResult<RawText> result = mergeAlgorithm.Merge(RawTextComparator.DEFAULT, baseText
				, GetRawText(ours.EntryObjectId, db), GetRawText(theirs.EntryObjectId, db));
			FilePath of = null;
			FileOutputStream fos;
			if (!inCore)
			{
				FilePath workTree = db.WorkTree;
				if (workTree == null)
				{
					// TODO: This should be handled by WorkingTreeIterators which
					// support write operations
					throw new NotSupportedException();
				}
				of = new FilePath(workTree, tw.PathString);
				fos = new FileOutputStream(of);
				try
				{
					fmt.FormatMerge(fos, result, Arrays.AsList(commitNames), Constants.CHARACTER_ENCODING
						);
				}
				finally
				{
					fos.Close();
				}
			}
			else
			{
				if (!result.ContainsConflicts())
				{
					// When working inCore, only trivial merges can be handled,
					// so we generate objects only in conflict free cases
					of = FilePath.CreateTempFile("merge_", "_temp", null);
					fos = new FileOutputStream(of);
					try
					{
						fmt.FormatMerge(fos, result, Arrays.AsList(commitNames), Constants.CHARACTER_ENCODING
							);
					}
					finally
					{
						fos.Close();
					}
				}
			}
			if (result.ContainsConflicts())
			{
				// a conflict occured, the file will contain conflict markers
				// the index will be populated with the three stages and only the
				// workdir (if used) contains the halfways merged content
				Add(tw.RawPath, @base, DirCacheEntry.STAGE_1);
				Add(tw.RawPath, ours, DirCacheEntry.STAGE_2);
				Add(tw.RawPath, theirs, DirCacheEntry.STAGE_3);
				mergeResults.Put(tw.PathString, result.Upcast ());
				return false;
			}
			else
			{
				// no conflict occured, the file will contain fully merged content.
				// the index will be populated with the new merged version
				DirCacheEntry dce = new DirCacheEntry(tw.PathString);
				dce.FileMode = tw.GetFileMode(0);
				dce.LastModified = of.LastModified();
				dce.SetLength((int)of.Length());
				InputStream @is = new FileInputStream(of);
				try
				{
					dce.SetObjectId(oi.Insert(Constants.OBJ_BLOB, of.Length(), @is));
				}
				finally
				{
					@is.Close();
					if (inCore)
					{
						FileUtils.Delete(of);
					}
				}
				builder.Add(dce);
				return true;
			}
		}
コード例 #16
0
ファイル: DirCacheCheckout.cs プロジェクト: stinos/ngit
        /// <summary>
        /// Updates the file in the working tree with content and mode from an entry
        /// in the index.
        /// </summary>
        /// <remarks>
        /// Updates the file in the working tree with content and mode from an entry
        /// in the index. The new content is first written to a new temporary file in
        /// the same directory as the real file. Then that new file is renamed to the
        /// final filename.
        /// <p>
        /// TODO: this method works directly on File IO, we may need another
        /// abstraction (like WorkingTreeIterator). This way we could tell e.g.
        /// Eclipse that Files in the workspace got changed
        /// </p>
        /// </remarks>
        /// <param name="repo"></param>
        /// <param name="f">
        /// the file to be modified. The parent directory for this file
        /// has to exist already
        /// </param>
        /// <param name="entry">the entry containing new mode and content</param>
        /// <param name="or">object reader to use for checkout</param>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public static void CheckoutEntry(Repository repo, FilePath f, DirCacheEntry entry
			, ObjectReader or)
        {
            ObjectLoader ol = or.Open(entry.GetObjectId());
            FilePath parentDir = f.GetParentFile();
            FilePath tmpFile = FilePath.CreateTempFile("._" + f.GetName(), null, parentDir);
            WorkingTreeOptions opt = repo.GetConfig().Get(WorkingTreeOptions.KEY);
            FileOutputStream rawChannel = new FileOutputStream(tmpFile);
            OutputStream channel;
            if (opt.GetAutoCRLF() == CoreConfig.AutoCRLF.TRUE)
            {
                channel = new AutoCRLFOutputStream(rawChannel);
            }
            else
            {
                channel = rawChannel;
            }
            try
            {
                ol.CopyTo(channel);
            }
            finally
            {
                channel.Close();
            }
            FS fs = repo.FileSystem;
            if (opt.IsFileMode() && fs.SupportsExecute())
            {
                if (FileMode.EXECUTABLE_FILE.Equals(entry.RawMode))
                {
                    if (!fs.CanExecute(tmpFile))
                    {
                        fs.SetExecute(tmpFile, true);
                    }
                }
                else
                {
                    if (fs.CanExecute(tmpFile))
                    {
                        fs.SetExecute(tmpFile, false);
                    }
                }
            }
            if (!tmpFile.RenameTo(f))
            {
                // tried to rename which failed. Let' delete the target file and try
                // again
                FileUtils.Delete(f);
                if (!tmpFile.RenameTo(f))
                {
                    throw new IOException(MessageFormat.Format(JGitText.Get().couldNotWriteFile, tmpFile
                        .GetPath(), f.GetPath()));
                }
            }
            entry.LastModified = f.LastModified();
            if (opt.GetAutoCRLF() != CoreConfig.AutoCRLF.FALSE)
            {
                entry.SetLength(f.Length());
            }
            else
            {
                // AutoCRLF wants on-disk-size
                entry.SetLength((int)ol.GetSize());
            }
        }
コード例 #17
0
ファイル: RepositoryTestCase.cs プロジェクト: shoff/ngit
		/// <summary>Resets the index to represent exactly some filesystem content.</summary>
		/// <remarks>
		/// Resets the index to represent exactly some filesystem content. E.g. the
		/// following call will replace the index with the working tree content:
		/// <p>
		/// <code>resetIndex(new FileSystemIterator(db))</code>
		/// <p>
		/// This method can be used by testcases which first prepare a new commit
		/// somewhere in the filesystem (e.g. in the working-tree) and then want to
		/// have an index which matches their prepared content.
		/// </remarks>
		/// <param name="treeItr">
		/// a
		/// <see cref="NGit.Treewalk.FileTreeIterator">NGit.Treewalk.FileTreeIterator</see>
		/// which determines which files should
		/// go into the new index
		/// </param>
		/// <exception cref="System.IO.FileNotFoundException">System.IO.FileNotFoundException
		/// 	</exception>
		/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
		protected internal virtual void ResetIndex(FileTreeIterator treeItr)
		{
			ObjectInserter inserter = db.NewObjectInserter();
			DirCacheBuilder builder = db.LockDirCache().Builder();
			DirCacheEntry dce;
			while (!treeItr.Eof)
			{
				long len = treeItr.GetEntryLength();
				dce = new DirCacheEntry(treeItr.EntryPathString);
				dce.FileMode = treeItr.EntryFileMode;
				dce.LastModified = treeItr.GetEntryLastModified();
				dce.SetLength((int)len);
				FileInputStream @in = new FileInputStream(treeItr.GetEntryFile());
				dce.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, len, @in));
				@in.Close();
				builder.Add(dce);
				treeItr.Next(1);
			}
			builder.Commit();
			inserter.Flush();
			inserter.Release();
		}
コード例 #18
0
		public virtual void TestBuildOneFile_Commit_IndexChangedEvent()
		{
			// empty
			string path = "a-file-path";
			FileMode mode = FileMode.REGULAR_FILE;
			// "old" date in 2008
			long lastModified = 1218123387057L;
			int length = 1342;
			DirCacheEntry entOrig;
			bool receivedEvent = false;
			DirCache dc = db.LockDirCache();
			IndexChangedListener listener = new _IndexChangedListener_212();
			ListenerList l = db.Listeners;
			l.AddIndexChangedListener(listener);
			DirCacheBuilder b = dc.Builder();
			entOrig = new DirCacheEntry(path);
			entOrig.FileMode = mode;
			entOrig.LastModified = lastModified;
			entOrig.SetLength(length);
			b.Add(entOrig);
			try
			{
				b.Commit();
			}
			catch (_T123327308)
			{
				receivedEvent = true;
			}
			if (!receivedEvent)
			{
				NUnit.Framework.Assert.Fail("did not receive IndexChangedEvent");
			}
			// do the same again, as this doesn't change index compared to first
			// round we should get no event this time
			dc = db.LockDirCache();
			listener = new _IndexChangedListener_239();
			l = db.Listeners;
			l.AddIndexChangedListener(listener);
			b = dc.Builder();
			entOrig = new DirCacheEntry(path);
			entOrig.FileMode = mode;
			entOrig.LastModified = lastModified;
			entOrig.SetLength(length);
			b.Add(entOrig);
			try
			{
				b.Commit();
			}
			catch (_T123327308)
			{
				NUnit.Framework.Assert.Fail("unexpected IndexChangedEvent");
			}
		}