Exemple #1
0
        // ------------------------------------------------------------------------
        // Check if the watched file is the hg/dirstate and set _bRebuildStatusCacheRequred to true if required
        // Check if the file state must be refreshed
        // Return: true if the file is dirty, false if not
        // ------------------------------------------------------------------------
        bool PrepareWatchedFile(string fileName)
        {
            bool isDirty = true;

            if (DirectoryWatcher.DirectoryExists(fileName))
            {
                // directories are not controlled
                isDirty = false;
            }
            else if (fileName.IndexOf(".hg\\dirstate") > -1)
            {
                if (!_SkipDirstate)
                {
                    _bRebuildStatusCacheRequired = true;
                    Trace.WriteLine("   ... rebuild of status cache required");
                }
                isDirty = false;
            }
            else if (fileName.IndexOf("\\.hg") != -1)
            {
                // all other .hg files are ignored
                isDirty = false;
            }
            else
            {
                HGFileStatusInfo hgFileStatusInfo;

                lock (_fileStatusDictionary)
                {
                    _fileStatusDictionary.TryGetValue(fileName, out hgFileStatusInfo);
                }

                if (hgFileStatusInfo != null)
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    if (fileInfo.Exists)
                    {
                        // see if the file states are equal
                        if ((hgFileStatusInfo.timeStamp == fileInfo.LastWriteTime &&
                             hgFileStatusInfo.size == fileInfo.Length))
                        {
                            isDirty = false;
                        }
                    }
                    else
                    {
                        if (hgFileStatusInfo.status == HGFileStatus.scsRemoved || hgFileStatusInfo.status == HGFileStatus.scsUncontrolled)
                        {
                            isDirty = false;
                        }
                    }
                }
            }
            return(isDirty);
        }
        // ------------------------------------------------------------------------
        /// update watcher objects for the given directory
        // ------------------------------------------------------------------------
        public bool WatchDirectory(string directory)
        {
            bool retval = DirectoryWatcher.DirectoryExists(directory);

            if (retval)
            {
                lock (dict)
                {
                    string           key = directory.ToLower();
                    DirectoryWatcher value;
                    if (!dict.TryGetValue(key, out value))
                    {
                        bool addNewWatcher = true;
                        List <DirectoryWatcher> removeWatcher = new List <DirectoryWatcher>();
                        string directorySlash = directory + "\\";
                        foreach (DirectoryWatcher watcher in dict.Values)
                        {
                            string watcherDirectorySlash = watcher._directory + "\\";
                            if (watcherDirectorySlash.IndexOf(directorySlash) == 0)
                            {
                                removeWatcher.Add(watcher); // sub-directory of new watcher
                            }
                            else if (directorySlash.IndexOf(watcherDirectorySlash) == 0)
                            {
                                addNewWatcher = false; // directory already watched
                            }
                        }

                        if (addNewWatcher)
                        {
                            // remove no longer used watcher objects
                            for (int pos = 0; pos < removeWatcher.Count; ++pos)
                            {
                                DirectoryWatcher watcher = removeWatcher[pos];
                                dict.Remove(watcher._directory);
                                watcher.EnableDirectoryWatching(false);
                                watcher = null;
                            }

                            dict[directory] = new DirectoryWatcher(directory);
                        }
                    }
                }
            }
            return(retval);
        }