Exemplo n.º 1
0
        private void ReadElement(string elementName, bool isDir, Cleartool cleartool)
        {
            // canonical name of elements is without the trailing '@@'
            if (elementName.EndsWith("@@"))
            {
                elementName = elementName.Substring(0, elementName.Length - 2);
            }
            string oid;

            lock (cleartool)
                oid = cleartool.GetOid(elementName);
            if (string.IsNullOrEmpty(oid))
            {
                Logger.TraceData(TraceEventType.Warning, (int)TraceId.ReadCleartool, "Could not find oid for element " + elementName);
                return;
            }
            lock (_oidsToCheck)
                _oidsToCheck.Remove(oid);
            lock (ElementsByOid)
                if (ElementsByOid.ContainsKey(oid))
                {
                    return;
                }

            Logger.TraceData(TraceEventType.Start | TraceEventType.Verbose, (int)TraceId.ReadCleartool,
                             "Start reading " + (isDir ? "directory" : "file") + " element", elementName);
            var element = new Element(elementName, isDir)
            {
                Oid = oid
            };

            lock (ElementsByOid)
                ElementsByOid[oid] = element;
            List <string> versionStrings;

            lock (cleartool)
                versionStrings = cleartool.Lsvtree(elementName);
            foreach (string versionString in versionStrings)
            {
                // there is a first "version" for each branch, without a version number
                if (!_isFullVersionRegex.IsMatch(versionString))
                {
                    continue;
                }
                Logger.TraceData(TraceEventType.Verbose, (int)TraceId.ReadCleartool, "Creating version", versionString);
                string[]      versionPath   = versionString.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                string        branchName    = versionPath[versionPath.Length - 2];
                int           versionNumber = int.Parse(versionPath[versionPath.Length - 1]);
                ElementBranch branch;
                if (!element.Branches.TryGetValue(branchName, out branch))
                {
                    // a new branch always start from the last seen version of the parent branch
                    ElementVersion branchingPoint = null;
                    if (versionPath.Length > 2)
                    {
                        // The version could be missing on a parent branch because
                        // it was created (or merged) directly from a grandparent.
                        // We need to traverse the whole branch tree up to the main
                        // branch to find any.
                        int index = versionPath.Length - 3;
                        while (index >= 0)
                        {
                            string b = versionPath[index];
                            if (element.Branches.ContainsKey(b))
                            {
                                branchingPoint = element.Branches[b].Versions.Last();
                                break;
                            }
                            index--;
                        }
                        if (branchingPoint == null)
                        {
                            throw new Exception("Unable to find any branching point for version " + versionString + " and element " + elementName + "!");
                        }
                        // Go back up to the missing one
                        while (index < versionPath.Length - 3)
                        {
                            index++;
                            string        skippedName   = versionPath[index];
                            ElementBranch skippedBranch = new ElementBranch(element, skippedName, branchingPoint);
                            element.Branches[skippedName] = skippedBranch;
                            if (!AddVersionToBranch(skippedBranch, 0, isDir, null, cleartool))
                            {
                                throw new Exception("Failed to add missing branch " + skippedName + " for element " + elementName + "!");
                            }
                            branchingPoint = skippedBranch.Versions.Last();
                        }
                    }
                    branch = new ElementBranch(element, branchName, branchingPoint);
                    element.Branches[branchName] = branch;
                }
                bool added = AddVersionToBranch(branch, versionNumber, isDir, null, cleartool);
                if (!added)
                {
                    // versions was too recent
                    if (branch.Versions.Count == 0)
                    {
                        // do not leave an empty branch
                        element.Branches.Remove(branchName);
                    }
                    // versions are retrieved in order of creation only within a branch :
                    // we still may have eligible versions on a parent branch, so we must continue
                }
            }
            Logger.TraceData(TraceEventType.Stop | TraceEventType.Verbose, (int)TraceId.ReadCleartool, "Stop reading element", elementName);
        }
        private void ReadElement(string elementName, bool isDir, Cleartool cleartool)
        {
            // canonical name of elements is without the trailing '@@'
            if (elementName.EndsWith("@@"))
            {
                elementName = elementName.Substring(0, elementName.Length - 2);
            }
            string oid;

            lock (cleartool)
                oid = cleartool.GetOid(elementName);
            if (string.IsNullOrEmpty(oid))
            {
                Logger.TraceData(TraceEventType.Warning, (int)TraceId.ReadCleartool, "Could not find oid for element " + elementName);
                return;
            }
            lock (_oidsToCheck)
                _oidsToCheck.Remove(oid);
            lock (ElementsByOid)
                if (ElementsByOid.ContainsKey(oid))
                {
                    return;
                }

            Logger.TraceData(TraceEventType.Start | TraceEventType.Verbose, (int)TraceId.ReadCleartool,
                             "Start reading " + (isDir ? "directory" : "file") + " element", elementName);
            var element = new Element(elementName, isDir)
            {
                Oid = oid
            };

            lock (ElementsByOid)
                ElementsByOid[oid] = element;
            List <string> versionStrings;

            lock (cleartool)
                versionStrings = cleartool.Lsvtree(elementName);
            foreach (string versionString in versionStrings)
            {
                // there is a first "version" for each branch, without a version number
                if (!_isFullVersionRegex.IsMatch(versionString))
                {
                    continue;
                }
                Logger.TraceData(TraceEventType.Verbose, (int)TraceId.ReadCleartool, "Creating version", versionString);
                string[]      versionPath   = versionString.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                string        branchName    = versionPath[versionPath.Length - 2];
                int           versionNumber = int.Parse(versionPath[versionPath.Length - 1]);
                ElementBranch branch;
                if (!element.Branches.TryGetValue(branchName, out branch))
                {
                    // a new branch always start from the last seen version of the parent branch
                    ElementVersion branchingPoint = null;
                    if (versionPath.Length > 2)
                    {
                        branchingPoint = element.Branches[versionPath[versionPath.Length - 3]].Versions.Last();
                    }
                    branch = new ElementBranch(element, branchName, branchingPoint);
                    element.Branches[branchName] = branch;
                }
                bool added = AddVersionToBranch(branch, versionNumber, isDir, null, cleartool);
                if (!added)
                {
                    // versions was too recent
                    if (branch.Versions.Count == 0)
                    {
                        // do not leave an empty branch
                        element.Branches.Remove(branchName);
                    }
                    // versions are retrieved in order of creation only within a branch :
                    // we still may have eligible versions on a parent branch, so we must continue
                }
            }
            Logger.TraceData(TraceEventType.Stop | TraceEventType.Verbose, (int)TraceId.ReadCleartool, "Stop reading element", elementName);
        }