Beispiel #1
0
        //Work with check out.
        public void CombineAndOverwrite()
        {
            var CurrentHash = SHA256Hash.ComputeSHA256(Path.Combine(Parent.RepoPath, RelativeFilePath));

            if (CurrentHash == FileHash)
            {
                //The file can be considered as 'same'.
                return;
            }
            else
            {
                var p = PreProcess_Overwrite();
                if (!File.Exists(p))
                {
                    File.Create(p).Close();
                }
                var FW = File.OpenWrite(p);
                for (int i = 0; i < Chunks.Count; i++)
                {
                    var    f     = Chunks[i];
                    var    chunk = Path.Combine(Parent.RepoPath, ".extgit", ".extgitconf", "Traces", Path.GetFileNameWithoutExtension(IndexFile.FullName), f);
                    var    FR    = File.OpenRead(chunk);
                    byte[] CHUNK = new byte[128 * 1024];
                    int    RL    = 0;
                    while ((RL = FR.Read(CHUNK, 0, CHUNK.Length)) != 0)
                    {
                        FW.Write(CHUNK, 0, RL);
                        FW.Flush();
                    }
                    FR.Close();
                }
                FW.Close();
                PostProcess_Overwrite();
            }
        }
Beispiel #2
0
 public static void Test00()
 {
     {
         Console.WriteLine("");
         Console.WriteLine("Test Field");
         Console.WriteLine("");
         try
         {
             Console.WriteLine("\tTest ID: {04EE0BD8-57E6-4013-B972-BFE4FB081235}");
             Console.WriteLine("\t\tFile Hash:" + SHA256Hash.ComputeSHA256("./ExtGit.exe"));
             Console.WriteLine("\t\t[OK]");
         }
         catch (Exception e)
         {
             Console.WriteLine("\tError:" + e);
         }
         try
         {
             Console.WriteLine("\tTest ID: {CA51CA71-DAEF-45F2-A3EA-6445DAA8A950}");
             var master = (new DirectoryInfo("./")).Parent.FullName;
             var slave  = (new FileInfo("./ExtGit.dll")).FullName;
             Console.WriteLine("\t\tRelative Path:" + PathHelper.GetRelativePath(master, slave));
             Console.WriteLine("\t\tAbsolute Path(Master):" + master);
             Console.WriteLine("\t\tAbsolute Path(Slave):" + slave);
             Console.WriteLine("\t\t[OK]");
         }
         catch (Exception e)
         {
             Console.WriteLine("\tError:" + e);
         }
     }
 }
Beispiel #3
0
        //Work with Commit
        public void DifferAndUpdate()
        {
            if (isDeletionScheduled)
            {
                //When deletion is scheduled, delete all related chunks.
                var chunk = Path.Combine(Parent.RepoPath, ".extgit", ".extgitconf", "Traces", Path.GetFileNameWithoutExtension(IndexFile.FullName));
                if (Directory.Exists(chunk))
                {
                    DirectoryInfo d = new DirectoryInfo(chunk);

                    foreach (var item in d.EnumerateFiles())
                    {
                        item.Delete();
                    }
                    d.Delete();
                }
                return;
            }
            var CurrentHash = SHA256Hash.ComputeSHA256(Path.Combine(Parent.RepoPath, RelativeFilePath));

            if (CurrentHash == FileHash)
            {
                //The file can be considered as 'same'.
                return;
            }
            else
            {
                FileInfo f              = new FileInfo(PreProcess_Differ());
                int      ChunkID        = 0;
                long     ChunkMaxLength = Parent.MaxFileSize;
                var      FR             = f.OpenRead();
                byte[]   ByteBlock      = new byte[1024];//1KB.
                {
                    var chunk = Path.Combine(Parent.RepoPath, ".extgit", ".extgitconf", "Traces", Path.GetFileNameWithoutExtension(IndexFile.FullName));
                    Directory.CreateDirectory(chunk);
                }
                for (long i = 0; i < f.Length;)
                {
                    long Position = 0;
                    if (Chunks.ContainsKey(ChunkID))
                    {
                        int AP = 0;
                        //Locate Chunk File;
                        var FN    = Chunks[ChunkID];
                        var TN    = Path.GetFileNameWithoutExtension(IndexFile.Name);
                        var chunk = Path.Combine(Parent.RepoPath, ".extgit", ".extgitconf", "Traces", Path.GetFileNameWithoutExtension(IndexFile.FullName), FN);
                        Debugger.CurrentDebugger.Log("Chunk find:" + chunk);
                        File.Delete(chunk);//Clear chunk before overwrite it.
                        var FW = File.Create(chunk);

                        while ((AP = FR.Read(ByteBlock, 0, ByteBlock.Length)) != 0)
                        {
                            {
                                FW.Write(ByteBlock, 0, ByteBlock.Length);
                                FW.Flush();
                                //Write To File
                            }
                            Position += AP;
                            if (Position >= Parent.MaxFileSize)
                            {
                                //i += Position;
                                break;
                            }
                        }
                        ChunkID++;
                        FW.Close();
                    }
                    else
                    {
                        //Create Chunk.

                        int AP = 0;
                        //Locate Chunk File;
                        var FN    = Guid.NewGuid() + ".Data-Chunk";
                        var TN    = Path.GetFileNameWithoutExtension(IndexFile.Name);
                        var chunk = Path.Combine(Parent.RepoPath, ".extgit", ".extgitconf", "Traces", Path.GetFileNameWithoutExtension(IndexFile.FullName), FN);

                        Debugger.CurrentDebugger.Log("Chunk created:" + chunk, Utilities.LogLevel.Development);
                        var FW = File.Create(chunk);
                        while ((AP = FR.Read(ByteBlock, 0, ByteBlock.Length)) != 0)
                        {
                            {
                                FW.Write(ByteBlock, 0, ByteBlock.Length);
                                FW.Flush();
                                //Write To File
                            }
                            Position += AP;
                            if (Position >= Parent.MaxFileSize)
                            {
                                break;
                            }
                        }
                        FW.Close();
                        //When Chunk is written, the chunk will be add into index file.
                        Chunks.Add(ChunkID, FN);
                        ChunkID++;
                    }

                    i += Position;
                }
                {
                    //Process when ths file is smaller than before.
                    //ChunkID is 1 larger than real last ID, so it can mean the count in the same time.
                    if (Chunks.Count > ChunkID)
                    {
                        for (int i = ChunkID; i < Chunks.Count; i++)
                        {
                            var FN    = Chunks[i];
                            var chunk = Path.Combine(Parent.RepoPath, ".extgit", ".extgitconf", "Traces", Path.GetFileNameWithoutExtension(IndexFile.FullName), FN);
                            File.Delete(chunk);
                            Chunks.Remove(i);
                        }
                    }
                }


                FR.Close();
                //Finalize, only when the file is different, index file will be updated.
                {
                    //Save index.
                    var NewContent = "#Generated by ExtGit.Core\r\n";
                    NewContent += $"TraceVersion={TraceVersion}\r\n";
                    NewContent += $"RelateiveFilePath={RelativeFilePath}\r\n";
                    NewContent += $"FileHash={CurrentHash}\r\n";
                    NewContent += $"Compressed={Compressed}\r\n";
                    foreach (var item in Chunks)
                    {
                        NewContent += $"Chunck.{item.Key}={item.Value}\r\n";
                    }
                    File.WriteAllText(IndexFile.FullName, NewContent);
                }
                PostProcess_Differ();
            }
        }
Beispiel #4
0
        public void OverwriteDiretory(DirectoryInfo directory)
        {
            Debugger.CurrentDebugger.Log($"Current Folder: {directory.FullName}", Utilities.LogLevel.Development);
            if (directory.FullName.ToUpper() == Path.Combine(RepoPath, ".extgit").ToUpper() || directory.FullName.ToUpper() == Path.Combine(RepoPath, ".git").ToUpper())
            {
                Debugger.CurrentDebugger.Log($"Folder: {directory.FullName} is ignored", Utilities.LogLevel.Development);
                //Don't self copy :P.
                return;
            }
            var RP = PathHelper.GetRelativePath(RepoPath, directory.FullName);

            {
                {
                    //Get Relative Path.
                    try
                    {
                        Debugger.CurrentDebugger.Log("Check:" + RP, Utilities.LogLevel.Development);
                        if (GitRepo.Ignore.IsPathIgnored(RP + Path.DirectorySeparatorChar))
                        {
                            return;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            {
                if (!Directory.Exists(Path.Combine(RepoPath, ".extgit", RP)))
                {
                    PathHelper.RemoveFolderR(directory);
                    return;
                }
            }
            foreach (var item in directory.EnumerateDirectories())
            {
                Debugger.CurrentDebugger.Log($"Folder: {item.FullName}", Utilities.LogLevel.Development);
                CheckDirectory(item);
            }
            Debugger.CurrentDebugger.Log($"Deal with: {directory.FullName}", Utilities.LogLevel.Development);
            Directory.SetLastAccessTime(Path.Combine(RepoPath, ".extgit", RP), directory.LastAccessTime);
            Directory.SetLastWriteTime(Path.Combine(RepoPath, ".extgit", RP), directory.LastWriteTime);
            foreach (var item in directory.EnumerateFiles())
            {
                Debugger.CurrentDebugger.Log($"File: {item.FullName}", Utilities.LogLevel.Development);
                var FRP = PathHelper.GetRelativePath(RepoPath, item.FullName);
                var FAP = Path.Combine(RepoPath, ".extgit", FRP);
                Debugger.CurrentDebugger.Log($"File: RelatedPath:{FRP}", Utilities.LogLevel.Development);
                if (!File.Exists(FAP))
                {
                    //Check trace;
                    bool isTracked = false;
                    foreach (var titem in TracedFiles)
                    {
                        if (titem.RelativeFilePath == FRP)
                        {
                            Debugger.CurrentDebugger.Log($"Traced File:{titem.RelativeFilePath}, Now relative file:{FRP}", Utilities.LogLevel.Development);
                            isTracked = true;
                            break;
                        }
                    }
                    if (isTracked == false)
                    {
                        item.Delete();
                    }
                    continue;
                }
                var NFHASH = SHA256Hash.ComputeSHA256(item.FullName);
                var OFHASH = SHA256Hash.ComputeSHA256(FAP);
                if (NFHASH == OFHASH)
                {
                    //the same file, ignore.
                    continue;
                }
                //Different File.
                //Overwrite.
                File.Copy(FAP, item.FullName, true);
            }
        }
Beispiel #5
0
        public void CheckDirectory(DirectoryInfo directory)
        {
            Debugger.CurrentDebugger.Log($"Current Folder: {directory.FullName}", Utilities.LogLevel.Development);
            if (directory.FullName.ToUpper() == Path.Combine(RepoPath, ".extgit").ToUpper() || directory.FullName.ToUpper() == Path.Combine(RepoPath, ".git").ToUpper())
            {
                Debugger.CurrentDebugger.Log($"Folder: {directory.FullName} is ignored", Utilities.LogLevel.Development);
                //Don't self copy :P.
                return;
            }
            var RP = PathHelper.GetRelativePath(RepoPath, directory.FullName);
            {
                {
                    //Get Relative Path.
                    try
                    {
                        Debugger.CurrentDebugger.Log("Check:" + RP, Utilities.LogLevel.Development);
                        if (GitRepo.Ignore.IsPathIgnored(RP + Path.DirectorySeparatorChar))
                        {
                            return;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            {
                if (!Directory.Exists(Path.Combine(RepoPath, ".extgit", RP)))
                {
                    Directory.CreateDirectory(Path.Combine(RepoPath, ".extgit", RP));
                    Directory.SetCreationTime(Path.Combine(RepoPath, ".extgit", RP), directory.CreationTime);
                }
            }
            var DirectoriesInWorkLoad = new List <String>();
            var FilesInWorkLoad       = new List <String>();

            {
                var           Workload      = Path.Combine(RepoPath, ".extgit", RP);
                DirectoryInfo directoryInfo = new DirectoryInfo(Workload);
                Debugger.CurrentDebugger.Log("Check:" + directoryInfo.FullName, Utilities.LogLevel.Development);
                foreach (var item in directoryInfo.EnumerateDirectories())
                {
                    if (item.Name.ToUpper() == ".GIT" || item.Name.ToUpper() == ".EXTGITCONF" || item.Name.ToUpper() == ".EXTGIT")
                    {
                        continue;
                    }
                    DirectoriesInWorkLoad.Add(item.Name);
                }
            }
            {
                var           Workload      = Path.Combine(RepoPath, ".extgit", RP);
                DirectoryInfo directoryInfo = new DirectoryInfo(Workload);
                foreach (var item in directoryInfo.EnumerateFiles())
                {
                    FilesInWorkLoad.Add(item.Name);
                }
            }
            foreach (var item in directory.EnumerateDirectories())
            {
                if (DirectoriesInWorkLoad.Contains(item.Name))
                {
                    DirectoriesInWorkLoad.Remove(item.Name);
                }
                Debugger.CurrentDebugger.Log($"Folder: {item.FullName}", Utilities.LogLevel.Development);
                CheckDirectory(item);
            }
            {
                foreach (var item in DirectoriesInWorkLoad)
                {
                    PathHelper.RemoveFolderR(new DirectoryInfo(Path.Combine(RepoPath, ".extgit", RP, item)));
                }
            }
            Debugger.CurrentDebugger.Log($"Deal with: {directory.FullName}", Utilities.LogLevel.Development);
            Directory.SetLastAccessTime(Path.Combine(RepoPath, ".extgit", RP), directory.LastAccessTime);
            Directory.SetLastWriteTime(Path.Combine(RepoPath, ".extgit", RP), directory.LastWriteTime);
            foreach (var item in directory.EnumerateFiles())
            {
                if (FilesInWorkLoad.Contains(item.Name))
                {
                    FilesInWorkLoad.Remove(item.Name);
                }
                Debugger.CurrentDebugger.Log($"File: {item.FullName}", Utilities.LogLevel.Development);
                var FRP = PathHelper.GetRelativePath(RepoPath, item.FullName);
                var FAP = Path.Combine(RepoPath, ".extgit", FRP);
                Debugger.CurrentDebugger.Log($"File: RelatedPath:{FRP}", Utilities.LogLevel.Development);

                {
                    bool isTracked = false;
                    {
                        //Even it is existed.
                        foreach (var titem in TracedFiles)
                        {
                            if (titem.RelativeFilePath == FRP)
                            {
                                Debugger.CurrentDebugger.Log($"Traced File:{titem.RelativeFilePath}, Now relative file:{FRP}", Utilities.LogLevel.Development);
                                isTracked = true;
                                break;
                            }
                        }
                        if (isTracked == false)
                        {
                            //File is not tracked.
                            // Detect weather to trace;
                            if (AutoTrack)
                            {
                                if (item.Length > realDetectionLine)
                                {
                                    //Add to trace.
                                    TracedFiles.Add(TraceIndex.TrackAndRecord(item, this));
                                    isTracked = true;
                                    //TraceIndex.Track(item, this);
                                    continue;
                                }
                            }
                            foreach (var type in AutoTraceFileTypes)
                            {
                                if (item.FullName.ToUpper().EndsWith(type.ToUpper()))
                                {
                                    TracedFiles.Add(TraceIndex.TrackAndRecord(item, this));
                                    isTracked = true;
                                    break;
                                }
                            }
                            foreach (var path in AutoTraceFilePaths)
                            {
                                if (FRP.StartsWith(path))
                                {
                                    TracedFiles.Add(TraceIndex.TrackAndRecord(item, this));
                                    isTracked = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (File.Exists(FAP))
                    {
                        if (isTracked == false)
                        {
                            {
                                var NFHASH = SHA256Hash.ComputeSHA256(item.FullName);
                                var OFHASH = SHA256Hash.ComputeSHA256(FAP);
                                if (NFHASH == OFHASH)
                                {
                                    continue;
                                }
                                else
                                {
                                    item.CopyTo(FAP);
                                }
                            }
                        }
                        else
                        {
                            File.Delete(FAP);
                        }
                    }
                    else
                    {
                        if (isTracked == false)
                        {
                            item.CopyTo(FAP);
                        }
                        else
                        {
                        }
                    }
                }
            }
            {
                foreach (var item in FilesInWorkLoad)
                {
                    File.Delete(Path.Combine(RepoPath, ".extgit", RP, item));
                }
            }
        }