internal IList <GitFile> GetChangedFiles() { if (!HasGitRepository) { return(new List <GitFile>()); } if (GitBash.Exists) { var output = GitBash.Run("status --porcelain -z --untracked-files", this.GitWorkingDirectory); return(ParseGitStatus(output)); } else { var list = new List <GitFile>(); var treeWalk = new TreeWalk(this.repository); treeWalk.Recursive = true; treeWalk.Filter = TreeFilter.ANY_DIFF; var id = repository.Resolve(Constants.HEAD); if (id != null) { treeWalk.AddTree(new RevWalk(repository).ParseTree(id)); } else { treeWalk.AddTree(new EmptyTreeIterator()); } treeWalk.AddTree(new DirCacheIterator(this.repository.ReadDirCache())); treeWalk.AddTree(new FileTreeIterator(this.repository)); var filters = new TreeFilter[] { new SkipWorkTreeFilter(INDEX), new IndexDiffFilter(INDEX, WORKDIR) }; treeWalk.Filter = AndTreeFilter.Create(filters); while (treeWalk.Next()) { WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(WORKDIR); if (workingTreeIterator != null && workingTreeIterator.IsEntryIgnored()) { continue; } var fileName = GetFullPath(treeWalk.PathString); if (Directory.Exists(fileName)) { continue; // this excludes sub modules } var status = GetFileStatus(treeWalk); list.Add(new GitFile { FileName = GetRelativeFileName(fileName), Status = status }); this.cache[GetCacheKey(fileName)] = status; } return(list); } }
/// <exception cref="System.IO.IOException"></exception> private void AssertEntry(FileMode type, bool entryIgnored, string pathName) { NUnit.Framework.Assert.IsTrue(walk.Next(), "walk has entry"); NUnit.Framework.Assert.AreEqual(pathName, walk.PathString); NUnit.Framework.Assert.AreEqual(type, walk.GetFileMode(0)); WorkingTreeIterator itr = walk.GetTree <WorkingTreeIterator>(0); NUnit.Framework.Assert.IsNotNull(itr, "has tree"); NUnit.Framework.Assert.AreEqual(entryIgnored, itr.IsEntryIgnored(), "is ignored"); if (D.Equals(type)) { walk.EnterSubtree(); } }
/// <summary> /// Executes the /// <code>Add</code> /// command. Each instance of this class should only /// be used for one invocation of the command. Don't call this method twice /// on an instance. /// </summary> /// <returns>the DirCache after Add</returns> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> /// <exception cref="NGit.Api.Errors.NoFilepatternException"></exception> public override DirCache Call() { if (filepatterns.IsEmpty()) { throw new NoFilepatternException(JGitText.Get().atLeastOnePatternIsRequired); } CheckCallable(); DirCache dc = null; bool addAll = false; if (filepatterns.Contains(".")) { addAll = true; } ObjectInserter inserter = repo.NewObjectInserter(); try { dc = repo.LockDirCache(); DirCacheIterator c; DirCacheBuilder builder = dc.Builder(); TreeWalk tw = new TreeWalk(repo); tw.AddTree(new DirCacheBuildIterator(builder)); if (workingTreeIterator == null) { workingTreeIterator = new FileTreeIterator(repo); } tw.AddTree(workingTreeIterator); tw.Recursive = true; if (!addAll) { tw.Filter = PathFilterGroup.CreateFromStrings(filepatterns); } string lastAddedFile = null; while (tw.Next()) { string path = tw.PathString; WorkingTreeIterator f = tw.GetTree <WorkingTreeIterator>(1); if (tw.GetTree <DirCacheIterator>(0) == null && f != null && f.IsEntryIgnored()) { } else { // file is not in index but is ignored, do nothing // In case of an existing merge conflict the // DirCacheBuildIterator iterates over all stages of // this path, we however want to add only one // new DirCacheEntry per path. if (!(path.Equals(lastAddedFile))) { if (!(update && tw.GetTree <DirCacheIterator>(0) == null)) { c = tw.GetTree <DirCacheIterator>(0); if (f != null) { // the file exists long sz = f.GetEntryLength(); DirCacheEntry entry = new DirCacheEntry(path); if (c == null || c.GetDirCacheEntry() == null || !c.GetDirCacheEntry().IsAssumeValid) { FileMode mode = f.GetIndexFileMode(c); entry.FileMode = mode; if (FileMode.GITLINK != mode) { entry.SetLength(sz); entry.LastModified = f.GetEntryLastModified(); long contentSize = f.GetEntryContentLength(); InputStream @in = f.OpenEntryStream(); try { entry.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, contentSize, @in)); } finally { @in.Close(); } } else { entry.SetObjectId(f.EntryObjectId); } builder.Add(entry); lastAddedFile = path; } else { builder.Add(c.GetDirCacheEntry()); } } else { if (c != null && (!update || FileMode.GITLINK == c.EntryFileMode)) { builder.Add(c.GetDirCacheEntry()); } } } } } } inserter.Flush(); builder.Commit(); SetCallable(false); } catch (IOException e) { throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfAddCommand , e); } finally { inserter.Release(); if (dc != null) { dc.Unlock(); } } return(dc); }
private GitFileStatus GetFileStatus(TreeWalk treeWalk) { AbstractTreeIterator treeIterator = treeWalk.GetTree <AbstractTreeIterator>(TREE); DirCacheIterator dirCacheIterator = treeWalk.GetTree <DirCacheIterator>(INDEX); WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(WORKDIR); if (dirCacheIterator != null) { DirCacheEntry dirCacheEntry = dirCacheIterator.GetDirCacheEntry(); if (dirCacheEntry != null && dirCacheEntry.Stage > 0) { return(GitFileStatus.Conflict); } if (workingTreeIterator == null) { // in index, not in workdir => missing return(GitFileStatus.Deleted); } else { if (workingTreeIterator.IsModified(dirCacheIterator.GetDirCacheEntry(), true)) { // in index, in workdir, content differs => modified return(GitFileStatus.Modified); } } } if (treeIterator != null) { if (dirCacheIterator != null) { if (!treeIterator.IdEqual(dirCacheIterator) || treeIterator.EntryRawMode != dirCacheIterator.EntryRawMode) { // in repo, in index, content diff => changed return(GitFileStatus.Staged); } } else { return(GitFileStatus.Removed); } } else { if (dirCacheIterator != null) { // not in repo, in index => added return(GitFileStatus.Added); } else { // not in repo, not in index => untracked if (workingTreeIterator != null) { return(!workingTreeIterator.IsEntryIgnored() ? GitFileStatus.New : GitFileStatus.Ignored); } } } return(GitFileStatus.NotControlled); }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> public override bool Include(TreeWalk tw) { int cnt = tw.TreeCount; int wm = tw.GetRawMode(workingTree); string path = tw.PathString; if (!tw.PostOrderTraversal) { // detect untracked Folders // Whenever we enter a folder in the workingtree assume it will // contain only untracked files and add it to // untrackedParentFolders. If we later find tracked files we will // remove it from this list if (FileMode.TREE.Equals(wm)) { // Clean untrackedParentFolders. This potentially moves entries // from untrackedParentFolders to untrackedFolders CopyUntrackedFolders(path); // add the folder we just entered to untrackedParentFolders untrackedParentFolders.AddFirst(path); } // detect untracked Folders // Whenever we see a tracked file we know that all of its parent // folders do not belong into untrackedParentFolders anymore. Clean // it. for (int i = 0; i < cnt; i++) { int rmode = tw.GetRawMode(i); if (i != workingTree && rmode != 0 && FileMode.TREE.Equals(rmode)) { untrackedParentFolders.Clear(); break; } } } // If the working tree file doesn't exist, it does exist for at least // one other so include this difference. if (wm == 0) { return(true); } // If the path does not appear in the DirCache and its ignored // we can avoid returning a result here, but only if its not in any // other tree. int dm = tw.GetRawMode(dirCache); WorkingTreeIterator wi = WorkingTree(tw); if (dm == 0) { if (honorIgnores && wi.IsEntryIgnored()) { ignoredPaths.AddItem(wi.EntryPathString); int i = 0; for (; i < cnt; i++) { if (i == dirCache || i == workingTree) { continue; } if (tw.GetRawMode(i) != 0) { break; } } // If i is cnt then the path does not appear in any other tree, // and this working tree entry can be safely ignored. return(i == cnt ? false : true); } else { // In working tree and not ignored, and not in DirCache. return(true); } } // Always include subtrees as WorkingTreeIterator cannot provide // efficient elimination of unmodified subtrees. if (tw.IsSubtree) { return(true); } // Try the inexpensive comparisons between index and all real trees // first. Only if we don't find a diff here we have to bother with // the working tree for (int i_1 = 0; i_1 < cnt; i_1++) { if (i_1 == dirCache || i_1 == workingTree) { continue; } if (tw.GetRawMode(i_1) != dm || !tw.IdEqual(i_1, dirCache)) { return(true); } } // Only one chance left to detect a diff: between index and working // tree. Make use of the WorkingTreeIterator#isModified() method to // avoid computing SHA1 on filesystem content if not really needed. DirCacheIterator di = tw.GetTree <DirCacheIterator>(dirCache); return(wi.IsModified(di.GetDirCacheEntry(), true)); }
/// <summary> /// Run the diff operation. Until this is called, all lists will be empty /// </summary> /// <returns>true if anything is different between index, tree, and workdir</returns> private void UpdateDirectory(IEnumerable <string> paths, bool recursive) { RevWalk rw = new RevWalk(Repository); ObjectId id = Repository.Resolve(Constants.HEAD); var commit = id != null?rw.ParseCommit(id) : null; TreeWalk treeWalk = new TreeWalk(Repository); treeWalk.Reset(); treeWalk.Recursive = false; if (commit != null) { treeWalk.AddTree(commit.Tree); } else { treeWalk.AddTree(new EmptyTreeIterator()); } DirCache dc = Repository.ReadDirCache(); treeWalk.AddTree(new DirCacheIterator(dc)); FileTreeIterator workTree = new FileTreeIterator(Repository.WorkTree, Repository.FileSystem, WorkingTreeOptions.KEY.Parse(Repository.GetConfig())); treeWalk.AddTree(workTree); List <TreeFilter> filters = new List <TreeFilter> (); filters.Add(new SkipWorkTreeFilter(1)); var pathFilters = paths.Where(p => p != ".").Select(p => PathFilter.Create(p)).ToArray(); if (pathFilters.Length > 1) { filters.Add(OrTreeFilter.Create(pathFilters)); // Use an OR to join all path filters } else if (pathFilters.Length == 1) { filters.Add(pathFilters[0]); } if (filters.Count > 1) { treeWalk.Filter = AndTreeFilter.Create(filters); } else { treeWalk.Filter = filters[0]; } while (treeWalk.Next()) { AbstractTreeIterator treeIterator = treeWalk.GetTree <AbstractTreeIterator>(0); DirCacheIterator dirCacheIterator = treeWalk.GetTree <DirCacheIterator>(1); WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(2); NGit.FileMode fileModeTree = treeWalk.GetFileMode(0); if (treeWalk.IsSubtree) { treeWalk.EnterSubtree(); continue; } int stage = dirCacheIterator != null?dirCacheIterator.GetDirCacheEntry().Stage : 0; if (stage > 1) { continue; } else if (stage == 1) { MergeConflict.Add(dirCacheIterator.EntryPathString); changesExist = true; continue; } if (treeIterator != null) { if (dirCacheIterator != null) { if (!treeIterator.EntryObjectId.Equals(dirCacheIterator.EntryObjectId)) { // in repo, in index, content diff => changed Modified.Add(dirCacheIterator.EntryPathString); changesExist = true; } } else { // in repo, not in index => removed if (!fileModeTree.Equals(NGit.FileMode.TYPE_TREE)) { Removed.Add(treeIterator.EntryPathString); changesExist = true; } } } else { if (dirCacheIterator != null) { // not in repo, in index => added Added.Add(dirCacheIterator.EntryPathString); changesExist = true; } else { // not in repo, not in index => untracked if (workingTreeIterator != null && !workingTreeIterator.IsEntryIgnored()) { Untracked.Add(workingTreeIterator.EntryPathString); changesExist = true; } } } if (dirCacheIterator != null) { if (workingTreeIterator == null) { // in index, not in workdir => missing Missing.Add(dirCacheIterator.EntryPathString); changesExist = true; } else { // Workaround to file time resolution issues long itime = dirCacheIterator.GetDirCacheEntry().LastModified; long ftime = workingTreeIterator.GetEntryLastModified(); if (itime / 1000 != ftime / 1000) { if (!dirCacheIterator.IdEqual(workingTreeIterator)) { // in index, in workdir, content differs => modified Modified.Add(dirCacheIterator.EntryPathString); changesExist = true; } } } } } }
/// <summary> /// Processing an entry in the context of /// <see cref="PrescanOneTree()">PrescanOneTree()</see> /// when only /// one tree is given /// </summary> /// <param name="m">the tree to merge</param> /// <param name="i">the index</param> /// <param name="f">the working tree</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> internal virtual void ProcessEntry(CanonicalTreeParser m, DirCacheBuildIterator i , WorkingTreeIterator f) { if (m != null) { if (!IsValidPath(m)) { throw new InvalidPathException(m.EntryPathString); } // There is an entry in the merge commit. Means: we want to update // what's currently in the index and working-tree to that one if (i == null) { // The index entry is missing if (f != null && !FileMode.TREE.Equals(f.EntryFileMode) && !f.IsEntryIgnored()) { // don't overwrite an untracked and not ignored file conflicts.AddItem(walk.PathString); } else { Update(m.EntryPathString, m.EntryObjectId, m.EntryFileMode); } } else { if (f == null || !m.IdEqual(i)) { // The working tree file is missing or the merge content differs // from index content Update(m.EntryPathString, m.EntryObjectId, m.EntryFileMode); } else { if (i.GetDirCacheEntry() != null) { // The index contains a file (and not a folder) if (f.IsModified(i.GetDirCacheEntry(), true) || i.GetDirCacheEntry().Stage != 0) { // The working tree file is dirty or the index contains a // conflict Update(m.EntryPathString, m.EntryObjectId, m.EntryFileMode); } else { // update the timestamp of the index with the one from the // file if not set, as we are sure to be in sync here. DirCacheEntry entry = i.GetDirCacheEntry(); if (entry.LastModified == 0) { entry.LastModified = f.GetEntryLastModified(); } Keep(entry); } } else { // The index contains a folder Keep(i.GetDirCacheEntry()); } } } } else { // There is no entry in the merge commit. Means: we want to delete // what's currently in the index and working tree if (f != null) { // There is a file/folder for that path in the working tree if (walk.IsDirectoryFileConflict()) { conflicts.AddItem(walk.PathString); } else { // No file/folder conflict exists. All entries are files or // all entries are folders if (i != null) { // ... and the working tree contained a file or folder // -> add it to the removed set and remove it from // conflicts set Remove(i.EntryPathString); conflicts.Remove(i.EntryPathString); } } } } }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> public override bool Include(TreeWalk tw) { WorkingTreeIterator i = tw.GetTree <WorkingTreeIterator>(index); return(i == null || !i.IsEntryIgnored()); }
/// <summary> /// Processing an entry in the context of /// <see cref="PrescanOneTree()">PrescanOneTree()</see> /// when only /// one tree is given /// </summary> /// <param name="m">the tree to merge</param> /// <param name="i">the index</param> /// <param name="f">the working tree</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> internal virtual void ProcessEntry(CanonicalTreeParser m, DirCacheBuildIterator i , WorkingTreeIterator f) { if (m != null) { // There is an entry in the merge commit. Means: we want to update // what's currently in the index and working-tree to that one if (i == null) { // The index entry is missing if (f != null && !FileMode.TREE.Equals(f.EntryFileMode) && !f.IsEntryIgnored()) { // don't overwrite an untracked and not ignored file conflicts.AddItem(walk.PathString); } else { Update(m.EntryPathString, m.EntryObjectId, m.EntryFileMode); } } else { if (f == null || !m.IdEqual(i)) { // The working tree file is missing or the merge content differs // from index content Update(m.EntryPathString, m.EntryObjectId, m.EntryFileMode); } else { if (i.GetDirCacheEntry() != null) { // The index contains a file (and not a folder) if (f.IsModified(i.GetDirCacheEntry(), true) || i.GetDirCacheEntry().Stage != 0) { // The working tree file is dirty or the index contains a // conflict Update(m.EntryPathString, m.EntryObjectId, m.EntryFileMode); } else { Keep(i.GetDirCacheEntry()); } } else { // The index contains a folder Keep(i.GetDirCacheEntry()); } } } } else { // There is no entry in the merge commit. Means: we want to delete // what's currently in the index and working tree if (f != null) { // There is a file/folder for that path in the working tree if (walk.IsDirectoryFileConflict()) { conflicts.AddItem(walk.PathString); } else { // No file/folder conflict exists. All entries are files or // all entries are folders if (i != null) { // ... and the working tree contained a file or folder // -> add it to the removed set and remove it from // conflicts set Remove(i.EntryPathString); conflicts.Remove(i.EntryPathString); } } } else { // untracked file, neither contained in tree to merge // nor in index // There is no file/folder for that path in the working tree. // The only entry we have is the index entry. If that entry is a // conflict simply remove it. Otherwise keep that entry in the // index if (i.GetDirCacheEntry().Stage == 0) { Keep(i.GetDirCacheEntry()); } } } }
/// <summary>Run the diff operation.</summary> /// <remarks> /// Run the diff operation. Until this is called, all lists will be empty. /// <p> /// The operation may be aborted by the progress monitor. In that event it /// will report what was found before the cancel operation was detected. /// Callers should ignore the result if monitor.isCancelled() is true. If a /// progress monitor is not needed, callers should use /// <see cref="Diff()">Diff()</see> /// instead. Progress reporting is crude and approximate and only intended /// for informing the user. /// </remarks> /// <param name="monitor">for reporting progress, may be null</param> /// <param name="estWorkTreeSize">number or estimated files in the working tree</param> /// <param name="estIndexSize">number of estimated entries in the cache</param> /// <param name="title"></param> /// <returns>if anything is different between index, tree, and workdir</returns> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public virtual bool Diff(ProgressMonitor monitor, int estWorkTreeSize, int estIndexSize , string title) { dirCache = repository.ReadDirCache(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.Recursive = true; // add the trees (tree, dirchache, workdir) if (tree != null) { treeWalk.AddTree(tree); } else { treeWalk.AddTree(new EmptyTreeIterator()); } treeWalk.AddTree(new DirCacheIterator(dirCache)); treeWalk.AddTree(initialWorkingTreeIterator); ICollection <TreeFilter> filters = new AList <TreeFilter>(4); if (monitor != null) { // Get the maximum size of the work tree and index // and add some (quite arbitrary) if (estIndexSize == 0) { estIndexSize = dirCache.GetEntryCount(); } int total = Math.Max(estIndexSize * 10 / 9, estWorkTreeSize * 10 / 9); monitor.BeginTask(title, total); filters.AddItem(new IndexDiff.ProgressReportingFilter(monitor, total)); } if (filter != null) { filters.AddItem(filter); } filters.AddItem(new SkipWorkTreeFilter(INDEX)); filters.AddItem(new IndexDiffFilter(INDEX, WORKDIR)); treeWalk.Filter = AndTreeFilter.Create(filters); while (treeWalk.Next()) { AbstractTreeIterator treeIterator = treeWalk.GetTree <AbstractTreeIterator>(TREE); DirCacheIterator dirCacheIterator = treeWalk.GetTree <DirCacheIterator>(INDEX); WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(WORKDIR ); if (dirCacheIterator != null) { DirCacheEntry dirCacheEntry = dirCacheIterator.GetDirCacheEntry(); if (dirCacheEntry != null && dirCacheEntry.Stage > 0) { conflicts.AddItem(treeWalk.PathString); continue; } } if (treeIterator != null) { if (dirCacheIterator != null) { if (!treeIterator.IdEqual(dirCacheIterator) || treeIterator.EntryRawMode != dirCacheIterator .EntryRawMode) { // in repo, in index, content diff => changed changed.AddItem(treeWalk.PathString); } } else { // in repo, not in index => removed removed.AddItem(treeWalk.PathString); if (workingTreeIterator != null) { untracked.AddItem(treeWalk.PathString); } } } else { if (dirCacheIterator != null) { // not in repo, in index => added added.AddItem(treeWalk.PathString); } else { // not in repo, not in index => untracked if (workingTreeIterator != null && !workingTreeIterator.IsEntryIgnored()) { untracked.AddItem(treeWalk.PathString); } } } if (dirCacheIterator != null) { if (workingTreeIterator == null) { // in index, not in workdir => missing missing.AddItem(treeWalk.PathString); } else { if (workingTreeIterator.IsModified(dirCacheIterator.GetDirCacheEntry(), true)) { // in index, in workdir, content differs => modified modified.AddItem(treeWalk.PathString); } } } } // consume the remaining work if (monitor != null) { monitor.EndTask(); } if (added.IsEmpty() && changed.IsEmpty() && removed.IsEmpty() && missing.IsEmpty( ) && modified.IsEmpty() && untracked.IsEmpty()) { return(false); } else { return(true); } }