Beispiel #1
0
        private void UpdateSingleFile(string file)
        {
            TreeEntry treeEntry = null;
            var       commit    = Repository.Head.CurrentCommit;

            _tree = commit != null ? commit.Tree : null;
            if (_tree != null)
            {
                treeEntry = _tree.FindBlobMember(file);
            }

            _index = Repository.Index.GitIndex;
            _index.RereadIfNecessary();
            GitIndex.Entry indexEntry = _index.GetEntry(file);

            TreeEntry wdirEntry = null;
            FileInfo  fileInfo  = new FileInfo(Path.Combine(Repository.WorkingDirectory, file.Replace('/', Path.DirectorySeparatorChar)));

            if (fileInfo.Exists && !IgnoreHandler.IsIgnored(file))
            {
                var tree = new Core.Tree(Repository._internal_repo);
                wdirEntry = tree.AddFile(file);
            }

            OnVisitEntry(treeEntry, wdirEntry, indexEntry, fileInfo);
        }
        public GitFileStatus GetFileStatusNoCacheOld(string fileName)
        {
            //Debug.WriteLine(string.Format("===+ GetFileStatusNoCache {0}", fileName));

            var fileNameRel = GetRelativeFileName(fileName);

            TreeEntry treeEntry = this.commitTree == null ? null : this.commitTree.FindBlobMember(fileNameRel);

            GitIndex.Entry indexEntry = this.index == null? null : this.index.GetEntry(fileNameRel);

            //the order of 'if' below is important
            if (indexEntry != null)
            {
                if (treeEntry == null)
                {
                    return(GitFileStatus.Added);
                }
                if (!File.Exists(fileName))
                {
                    return(GitFileStatus.Deleted);
                }
                if (File.Exists(fileName) && indexEntry.IsModified(repository.WorkTree, true))
                {
                    return(GitFileStatus.Modified);
                }
                if (treeEntry != null && !treeEntry.GetId().Equals(indexEntry.GetObjectId()))
                {
                    return(GitFileStatus.Staged);
                }
                if (indexEntry.GetStage() != 0)
                {
                    return(GitFileStatus.Conflict);
                }
                if (treeEntry != null && treeEntry.GetId().Equals(indexEntry.GetObjectId()))
                {
                    return(GitFileStatus.Tracked);
                }
            }
            else // <-- index entry == null
            {
                if (treeEntry != null && !(treeEntry is Tree))
                {
                    return(GitFileStatus.Removed);
                }

                if (File.Exists(fileName))
                {
                    string target = fileNameRel.Replace('\\', '/');
                    if (ignoreRules != null && ignoreRules.Any(rule => rule.IsMatch(target, false)))
                    {
                        return(GitFileStatus.Ignored);
                    }

                    return(GitFileStatus.New);
                }
            }

            return(GitFileStatus.NotControlled);
        }
Beispiel #3
0
        public void CanReadMsysgitIndex()
        {
            //setup of .git directory
            var resource =
                new DirectoryInfo(Path.Combine(Path.Combine(Environment.CurrentDirectory, "Resources"),
                                               "OneFileRepository"));
            var tempRepository =
                new DirectoryInfo(Path.Combine(trash.FullName, "OneFileRepository" + Path.GetRandomFileName()));

            CopyDirectory(resource.FullName, tempRepository.FullName);

            var repositoryPath = new DirectoryInfo(Path.Combine(tempRepository.FullName, Constants.DOT_GIT));

            Directory.Move(repositoryPath.FullName + "ted", repositoryPath.FullName);



            using (var repository = new Core.Repository(repositoryPath))
            {
                GitIndex index = repository.Index;

                Assert.IsNotNull(index);
                List <GitIndex.Entry> entries = index.Members.ToList();
                Assert.AreEqual(1, entries.Count);

                GitIndex.Entry entry = entries[0];
                Assert.AreEqual("dummy.txt", entry.Name);

                Core.Ref headRef = repository.Head;
                Assert.AreEqual("refs/heads/master", headRef.Target.Name);
                Assert.AreEqual("f3ca78a01f1baa4eaddcc349c97dcab95a379981", headRef.ObjectId.Name);

                object obj = repository.MapObject(headRef.ObjectId, headRef.Name);
#pragma warning disable 0612
                Assert.IsInstanceOfType(typeof(Core.Commit), obj);  // [henon] IsInstanceOfType is obsolete
#pragma warning restore 0612
                var commit = (Core.Commit)obj;

                Assert.AreEqual("f3ca78a01f1baa4eaddcc349c97dcab95a379981", commit.CommitId.Name);
                Assert.AreEqual(commit.Committer, commit.Author);
                Assert.AreEqual("nulltoken <*****@*****.**> 1255117188 +0200",
                                commit.Committer.ToExternalString());

                Assert.AreEqual(0, commit.ParentIds.Length);
            }
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="treeEntry"></param>
        /// <param name="wdirEntry">Note: wdirEntry is the non-ignored working directory entry.</param>
        /// <param name="indexEntry"></param>
        /// <param name="file">Note: gitignore patterns do not influence this parameter</param>
        private void OnVisitEntry(TreeEntry treeEntry, TreeEntry wdirEntry, GitIndex.Entry indexEntry, FileInfo file)
        {
            //Console.WriteLine(" ----------- ");
            //if (treeEntry != null)
            //   Console.WriteLine("tree: " + treeEntry.Name);
            //if (wdirEntry != null)
            //   Console.WriteLine("w-dir: " + wdirEntry.Name);
            //if (indexEntry != null)
            //   Console.WriteLine("index: " + indexEntry.Name);
            //Console.WriteLine("file: " + file.Name);

            string subdir_prefix = !string.IsNullOrEmpty(_root_path) ? _root_path + "/" : null;

            PathStatus path_status = null;

            if (indexEntry != null)
            {
                if (subdir_prefix != null && !indexEntry.Name.StartsWith(subdir_prefix))
                {
                    return;                     // File outside the directory
                }
                if (treeEntry == null)
                {
                    path_status = OnAdded(indexEntry.Name, path_status);
                }
                if (treeEntry != null && !treeEntry.Id.Equals(indexEntry.ObjectId))
                {
                    Debug.Assert(treeEntry.FullName == indexEntry.Name);
                    path_status = OnStaged(indexEntry.Name, path_status);
                }
                if (!file.Exists)
                {
                    path_status = OnMissing(indexEntry.Name, path_status);
                }
                if (file.Exists && indexEntry.IsModified(new DirectoryInfo(Repository.WorkingDirectory), Options.ForceContentCheck))
                {
                    path_status = OnModified(indexEntry.Name, path_status);
                }
                if (indexEntry.Stage != 0)
                {
                    path_status = OnMergeConflict(indexEntry.Name, path_status);
                }
            }
            else             // <-- index entry == null
            {
                if (treeEntry != null && subdir_prefix != null && !treeEntry.FullName.StartsWith(subdir_prefix))
                {
                    return;                     // File outside the directory
                }
                if (treeEntry != null && !(treeEntry is Core.Tree))
                {
                    path_status = OnRemoved(treeEntry.FullName, path_status);
                }
                if (wdirEntry != null)                 // actually, we should enforce (treeEntry == null ) here too but original git does not, may be a bug.
                {
                    path_status = OnUntracked(wdirEntry.FullName, path_status);
                }
            }
            if (Options.PerPathNotificationCallback != null && path_status != null)
            {
                Options.PerPathNotificationCallback(path_status);
            }
        }
Beispiel #5
0
        private void UpdateDirectoryNotRecursive(string path)
        {
            _index = Repository.Index.GitIndex;

            // Tree that will hold the working dir file entries
            var wtree = new Core.Tree(Repository._internal_repo);

            // Get a list of a leaves in the path

            Tree commitTree = null;
            var  commit     = Repository.Head.CurrentCommit;

            commitTree = commit != null ? commit.Tree : null;
            if (commitTree != null)
            {
                commitTree = commitTree[path] as Tree;
            }

            Dictionary <string, Leaf> commitEntries;

            if (commitTree != null)
            {
                commitEntries = commitTree.Leaves.ToDictionary(l => l.Path);
            }
            else
            {
                commitEntries = new Dictionary <string, Leaf> ();
            }

            HashSet <string> visited = new HashSet <string> ();

            // Compare commited files and working tree files

            DirectoryInfo dir = new DirectoryInfo(Repository.FromGitPath(path));

            if (dir.Exists)
            {
                foreach (FileInfo fileInfo in dir.GetFiles())
                {
                    string file = path + "/" + fileInfo.Name;

                    Leaf lf;
                    if (commitEntries.TryGetValue(file, out lf))
                    {
                        // Remove from the collection. At the end of the loop, entries still remaining in the
                        // collection will be processed as not having a corresponding working dir file
                        commitEntries.Remove(file);
                    }

                    TreeEntry wdirEntry = null;
                    if (!IgnoreHandler.IsIgnored(file) && fileInfo.Exists)
                    {
                        wdirEntry = wtree.AddFile(file);
                    }

                    GitIndex.Entry indexEntry = _index.GetEntry(file);

                    OnVisitEntry(lf != null ? lf.InternalEntry : null, wdirEntry, indexEntry, fileInfo);
                    visited.Add(file);
                }
            }

            // Now visit entries for which a working dir file was not found

            foreach (var lf in commitEntries)
            {
                string         file       = lf.Key;
                FileInfo       fileInfo   = new FileInfo(Repository.FromGitPath(file));
                GitIndex.Entry indexEntry = _index.GetEntry(file);
                OnVisitEntry(lf.Value.InternalEntry, null, indexEntry, fileInfo);
                visited.Add(file);
            }

            // Finally, visit remaining index entries which are not in the working dir nor in the commit

            foreach (var ie in _index.Members)
            {
                string file = ie.Name;
                // Exclude entries in subdirectories of _root_path
                int    i    = file.LastIndexOf('/');
                string fdir = i != -1 ? file.Substring(0, i) : string.Empty;
                if (fdir == _root_path && !visited.Contains(file))
                {
                    OnVisitEntry(null, null, ie, new FileInfo(Repository.FromGitPath(file)));
                }
            }
        }