Beispiel #1
0
 public RawHistoryBuilder(VobDB vobDB)
 {
     if (vobDB != null)
     {
         _elementsByOid = vobDB.ElementsByOid;
     }
     Labels = new Dictionary <string, LabelInfo>();
 }
Beispiel #2
0
 public void Add(VobDB other)
 {
     foreach (var pair in other.ElementsByOid)
     {
         Element existing;
         if (!ElementsByOid.TryGetValue(pair.Key, out existing))
         {
             ElementsByOid.Add(pair.Key, pair.Value);
             continue;
         }
         // TODO : we should keep the one with the most versions/branches
         if (existing.Name != pair.Value.Name)
         {
             Program.Logger.TraceData(TraceEventType.Information, 0,
                                      string.Format("element with oid {0} has a different name : keeping {1}, ignoring {2}", existing.Oid, existing.Name, pair.Value.Name));
         }
     }
 }
        internal void Init(VobDB vobDB, IEnumerable <Element> elements)
        {
            ElementsByOid = vobDB != null ? vobDB.ElementsByOid : new Dictionary <string, Element>();

            Logger.TraceData(TraceEventType.Start | TraceEventType.Information, (int)TraceId.ReadCleartool, "Start fetching oids of exported elements");
            int i          = 0;
            var allActions = new List <Action>();

            foreach (var element in elements)
            {
                int     iTask          = ++i;
                Element currentElement = element;
                allActions.Add(() =>
                {
                    if (iTask % 500 == 0)
                    {
                        Logger.TraceData(TraceEventType.Information, (int)TraceId.ReadCleartool, "Fetching oid for element " + iTask);
                    }
                    string oid;
                    lock (_cleartools[iTask % _nbCleartool])
                        oid = _cleartools[iTask % _nbCleartool].GetOid(currentElement.Name);
                    if (string.IsNullOrEmpty(oid))
                    {
                        Logger.TraceData(TraceEventType.Warning, (int)TraceId.ReadCleartool, "Could not find oid for element " + currentElement.Name);
                        return;
                    }
                    currentElement.Oid = oid;
                    lock (ElementsByOid)
                        ElementsByOid[oid] = currentElement;
                    // these elements come from a non-filtered clearcase export : there may be unwanted elements
                    lock (_oidsToCheck)
                        _oidsToCheck.Add(oid);
                });
            }
            Parallel.Invoke(new ParallelOptions {
                MaxDegreeOfParallelism = _nbCleartool * 2
            }, allActions.ToArray());
            Logger.TraceData(TraceEventType.Stop | TraceEventType.Information, (int)TraceId.ReadCleartool, "Stop fetching oids of exported elements");
        }
Beispiel #4
0
        static int Main(string[] args)
        {
            Console.Error.WriteLine("GitImporter called with {0} arguments :", args.Length);
            foreach (string arg in args)
            {
                Console.Error.WriteLine("    " + arg);
            }
            Logger.TraceData(TraceEventType.Information, 0, string.Format("GitImporter called with {0} arguments : {1}", args.Length, string.Join(" ", args)));
            var importerArguments = new ImporterArguments();

            if (!CommandLine.Parser.ParseArgumentsWithUsage(args, importerArguments))
            {
                return(1);
            }
            if (!importerArguments.CheckArguments())
            {
                Console.Error.WriteLine(CommandLine.Parser.ArgumentsUsage(typeof(ImporterArguments)));
                return(1);
            }

            try
            {
                Logger.TraceData(TraceEventType.Start | TraceEventType.Information, 0, "Start program");
                VobDB vobDB = null;

                if (!string.IsNullOrEmpty(importerArguments.FetchFileContent))
                {
                    using (var gitWriter = new GitWriter(importerArguments.ClearcaseRoot, importerArguments.NoFileContent, importerArguments.Labels, importerArguments.Roots))
                    {
                        if (File.Exists(importerArguments.ThirdpartyConfig))
                        {
                            var thirdPartyConfig = ThirdPartyConfig.ReadFromFile(importerArguments.ThirdpartyConfig);
                            var hook             = new ThirdPartyHook(thirdPartyConfig);
                            gitWriter.PreWritingHooks.AddRange(hook.PreWritingHooks);
                            gitWriter.PostWritingHooks.AddRange(hook.PostWritingHooks);
                        }
                        gitWriter.WriteFile(importerArguments.FetchFileContent);
                    }
                    Logger.TraceData(TraceEventType.Stop | TraceEventType.Information, 0, "Stop program");
                    return(0);
                }
                if (importerArguments.LoadVobDB != null && importerArguments.LoadVobDB.Length > 0)
                {
                    foreach (string vobDBFile in importerArguments.LoadVobDB)
                    {
                        using (var stream = new FileStream(vobDBFile, FileMode.Open))
                            if (vobDB == null)
                            {
                                vobDB = Serializer.Deserialize <VobDB>(stream);
                            }
                            else
                            {
                                vobDB.Add(Serializer.Deserialize <VobDB>(stream));
                            }
                        Logger.TraceData(TraceEventType.Information, 0, "Clearcase data successfully loaded from " + vobDBFile);
                    }
                }

                var exportReader = new ExportReader(importerArguments.OriginDate, importerArguments.Labels);
                foreach (var file in importerArguments.ExportFiles)
                {
                    exportReader.ReadFile(file);
                }

                List <ElementVersion> newVersions = null;

                if (!string.IsNullOrWhiteSpace(importerArguments.DirectoriesFile) ||
                    !string.IsNullOrWhiteSpace(importerArguments.ElementsFile) ||
                    !string.IsNullOrWhiteSpace(importerArguments.VersionsFile))
                {
                    using (var cleartoolReader = new CleartoolReader(importerArguments.ClearcaseRoot, importerArguments.OriginDate, importerArguments.Labels))
                    {
                        cleartoolReader.Init(vobDB, exportReader.Elements);
                        // first save of exportReader with oid (if something was actually read)
                        vobDB = cleartoolReader.VobDB;
                        if (importerArguments.ExportFiles.Length > 0 && !string.IsNullOrWhiteSpace(importerArguments.SaveVobDB))
                        {
                            using (var stream = new FileStream(importerArguments.SaveVobDB + ".export_oid", FileMode.Create))
                                Serializer.Serialize(stream, vobDB);
                            Logger.TraceData(TraceEventType.Information, 0, "Clearcase export with oid successfully saved in " + importerArguments.SaveVobDB + ".export_oid");
                        }
                        newVersions = cleartoolReader.Read(importerArguments.DirectoriesFile, importerArguments.ElementsFile, importerArguments.VersionsFile);
                        vobDB       = cleartoolReader.VobDB;
                        if (!string.IsNullOrWhiteSpace(importerArguments.SaveVobDB))
                        {
                            using (var stream = new FileStream(importerArguments.SaveVobDB, FileMode.Create))
                                Serializer.Serialize(stream, vobDB);
                            Logger.TraceData(TraceEventType.Information, 0, "Clearcase data successfully saved in " + importerArguments.SaveVobDB);
                        }
                    }
                }

                if (!importerArguments.GenerateVobDBOnly)
                {
                    HistoryBuilder historyBuilder = null;
                    // we only use an existing HistoryBuilder for incremental import, ie when newVersions != null
                    if (newVersions != null && !string.IsNullOrWhiteSpace(importerArguments.History) && File.Exists(importerArguments.History))
                    {
                        using (var stream = new FileStream(importerArguments.History, FileMode.Open))
                            historyBuilder = Serializer.Deserialize <HistoryBuilder>(stream);
                        Logger.TraceData(TraceEventType.Information, 0, "History data successfully loaded from " + importerArguments.History);
                        historyBuilder.Fixup(vobDB);
                    }
                    if (historyBuilder == null)
                    {
                        historyBuilder = new HistoryBuilder(vobDB);
                    }

                    // command-line arguments take precedence
                    historyBuilder.SetRoots(importerArguments.ClearcaseRoot, importerArguments.Roots);
                    historyBuilder.SetBranchFilters(importerArguments.Branches);

                    var changeSets   = historyBuilder.Build(newVersions);
                    var branchRename = historyBuilder.GetBranchRename();
                    var labels       = historyBuilder.GetLabels();

                    using (var gitWriter = new GitWriter(importerArguments.ClearcaseRoot, importerArguments.NoFileContent, importerArguments.Labels, importerArguments.Roots, branchRename))
                    {
                        if (File.Exists(importerArguments.IgnoreFile))
                        {
                            gitWriter.InitialFiles.Add(new Tuple <string, string>(".gitignore", importerArguments.IgnoreFile));
                        }
                        if (File.Exists(importerArguments.ThirdpartyConfig))
                        {
                            var thirdPartyConfig = ThirdPartyConfig.ReadFromFile(importerArguments.ThirdpartyConfig);
                            var hook             = new ThirdPartyHook(thirdPartyConfig);
                            gitWriter.PreWritingHooks.AddRange(hook.PreWritingHooks);
                            gitWriter.PostWritingHooks.AddRange(hook.PostWritingHooks);
                            gitWriter.InitialFiles.Add(new Tuple <string, string>(".gitmodules", hook.ModulesFile));
                        }
                        gitWriter.WriteChangeSets(changeSets, labels, vobDB.LabelMetas);
                    }

                    if (!string.IsNullOrWhiteSpace(importerArguments.History))
                    {
                        if (File.Exists(importerArguments.History))
                        {
                            File.Move(importerArguments.History, importerArguments.History + ".bak");
                        }
                        using (var stream = new FileStream(importerArguments.History, FileMode.Create))
                            Serializer.Serialize(stream, historyBuilder);
                        Logger.TraceData(TraceEventType.Information, 0, "History data successfully saved in " + importerArguments.History);
                    }
                }
                return(0);
            }
            catch (Exception ex)
            {
                Logger.TraceData(TraceEventType.Critical, 0, "Exception during import : " + ex);
                Console.Error.WriteLine("Exception during import : " + ex);
                return(1);
            }
            finally
            {
                Logger.TraceData(TraceEventType.Stop | TraceEventType.Information, 0, "Stop program");
                Logger.Flush();
            }
        }
Beispiel #5
0
 public void Fixup(VobDB vobDB)
 {
     _rawHistoryBuilder = new RawHistoryBuilder(null);
     _rawHistoryBuilder.SetBranchFilters(_branchFilters);
     if (_rawElementsNamesByBranch != null)
     {
         foreach (var pair in _rawElementsNamesByBranch)
         {
             var elements = new Dictionary <Element, HashSet <string> >();
             _elementsNamesByBranch.Add(pair.Key, elements);
             foreach (var nameByOid in pair.Value)
             {
                 Element element;
                 if (!vobDB.ElementsByOid.TryGetValue(nameByOid.Key, out element))
                 {
                     Logger.TraceData(TraceEventType.Warning, (int)TraceId.CreateChangeSet, "Could not find element with oid " + nameByOid.Key + " in loaded vobDB");
                     continue;
                 }
                 var names = new HashSet <string>(nameByOid.Value);
                 elements.Add(element, names);
             }
         }
         _rawElementsNamesByBranch = null;
     }
     if (_rawElementsVersionsByBranch != null)
     {
         foreach (var pair in _rawElementsVersionsByBranch)
         {
             var elements = new Dictionary <Element, ElementVersion>();
             _elementsVersionsByBranch.Add(pair.Key, elements);
             foreach (var versionByOid in pair.Value)
             {
                 Element element;
                 if (!vobDB.ElementsByOid.TryGetValue(versionByOid.Key, out element))
                 {
                     Logger.TraceData(TraceEventType.Warning, (int)TraceId.CreateChangeSet, "Could not find element with oid " + versionByOid.Key + " in loaded vobDB");
                     continue;
                 }
                 elements.Add(element, element.GetVersion(versionByOid.Value.BranchName, versionByOid.Value.VersionNumber));
             }
         }
         _rawElementsVersionsByBranch = null;
     }
     if (_rawBranchTips != null)
     {
         foreach (var branchTip in _rawBranchTips)
         {
             // creating dummy changeSets : they will never be used except for their Id
             _branchTips.Add(branchTip.Key, new ChangeSet("dummy", "dummy", branchTip.Key, DateTime.Now)
             {
                 Id = branchTip.Value
             });
         }
         _rawBranchTips = null;
     }
     if (_rawStartedBranches != null)
     {
         foreach (var startedBranch in _rawStartedBranches)
         {
             // creating dummy changeSets : they will never be used except for their Id
             _startedBranches.Add(startedBranch.Key, startedBranch.Value == 0 ? null : new ChangeSet("dummy", "dummy", startedBranch.Key, DateTime.Now)
             {
                 Id = startedBranch.Value
             });
         }
         _rawStartedBranches = null;
     }
 }
Beispiel #6
0
 public HistoryBuilder(VobDB vobDB)
 {
     _rawHistoryBuilder = new RawHistoryBuilder(vobDB);
 }