Beispiel #1
0
        async Task <CommitID> TestCreateCommit(TreeID treeid, int num)
        {
            var db = getDataContext();

            ICommitRepository cmrepo = new CommitRepository(db);
            IRefRepository    rfrepo = new RefRepository(db);

            Tuple <Ref, Commit> parent = await cmrepo.GetCommitByRef("HEAD");

            Commit cm = new Commit.Builder(
                pParents:       parent == null ? new List <CommitID>(0) : new List <CommitID>(1)
            {
                parent.Item2.ID
            },
                pTreeID:        treeid,
                pCommitter:     "James Dunne <*****@*****.**>",
                pDateCommitted: DateTimeOffset.Parse("2011-08-29 00:00:00 -0500"),
                pMessage:       "Commit #" + num.ToString() + "."
                );

            Console.WriteLine("CommitID {0}", cm.ID);

            // Persist the commit:
            await cmrepo.PersistCommit(cm);

            // Once the commit is persisted, update HEAD ref:
            await rfrepo.PersistRef(new Ref.Builder("HEAD", cm.ID));

            return(cm.ID);
        }
Beispiel #2
0
        async Task DemonstrateHashing()
        {
            var db = getDataContext();

            IBlobRepository   blrepo = new BlobRepository(db);
            ITreeRepository   trrepo = new TreeRepository(db);
            ICommitRepository cmrepo = new CommitRepository(db);
            IRefRepository    rfrepo = new RefRepository(db);

            // Create a sample set of blobs:
            Blob readmeBlob;
            ImmutableContainer <BlobID, Blob> blobs = new ImmutableContainer <BlobID, Blob>(
                bl => bl.ID,
                readmeBlob = new Blob.Builder(pContents: Encoding.UTF8.GetBytes(@"Hello world"))
                );

            Console.Out.WriteAsync(String.Format("Blob {0} = \"{1}\"" + Environment.NewLine, readmeBlob.ID.ToString(firstLength: 7), Encoding.UTF8.GetString(readmeBlob.Contents)));
            // Persist them:
            var persistingBlobs = blrepo.PersistBlobs(blobs);

            // Create an initial tree:
            Tree trRoot;
            ImmutableContainer <TreeID, Tree> trees = new ImmutableContainer <TreeID, Tree>(
                tr => tr.ID,
                new Tree[] {
                trRoot = new Tree.Builder(
                    pTrees: new List <TreeTreeReference>(0),
                    pBlobs: new List <TreeBlobReference> {
                    new TreeBlobReference.Builder(pName: "README", pBlobID: readmeBlob.ID)
                }
                    )
            }
                );

            // Dump the tree:
            RecursivePrint(trRoot.ID, trees);

            // Now wait for the blob persistence to complete:
            await persistingBlobs;
            // Persist our tree now:
            var   persistTrees = trrepo.PersistTree(trRoot.ID, trees);
            await persistTrees;

            // Let's make a commit out of all that:
            Commit cm1 = new Commit.Builder(
                pParents:       new List <CommitID>(0),
                pTreeID:        trRoot.ID,
                pCommitter:     @"James Dunne <*****@*****.**>",
                pDateCommitted: DateTimeOffset.Now,
                pMessage:       "Initial commit."
                );

            // Persist that commit:
            Console.Out.WriteAsync(String.Format("Persisting commit {0}..." + Environment.NewLine, cm1.ID.ToString(firstLength: 7)));
            await cmrepo.PersistCommit(cm1);

            // Let's create a ref to point to it:
            await rfrepo.DestroyRefByName("demo/HEAD");

            await rfrepo.PersistRef(new Ref.Builder(pName: "demo/HEAD", pCommitID: cm1.ID));

            await Console.Out.WriteAsync(String.Format("Pointed demo/HEAD to commit {0}" + Environment.NewLine, cm1.ID.ToString(firstLength: 7)));

            // Now let's create a new blob with some revised contents: (adding a period at the end)
            Blob readmeBlob2;

            blobs = new ImmutableContainer <BlobID, Blob>(
                bl => bl.ID,
                readmeBlob2 = new Blob.Builder(Encoding.UTF8.GetBytes(@"Hello world."))
                );
            var persistBlobs2 = blrepo.PersistBlobs(blobs);

            // Make a new tree out of the old tree:
            Tree.Builder trRoot2b = new Tree.Builder(trRoot);
            // Point README to the new BlobID:
            trRoot2b.Blobs[0] = new TreeBlobReference.Builder(trRoot2b.Blobs[0])
            {
                BlobID = readmeBlob2.ID
            };

            // Freeze our Tree.Builder to a Tree:
            Tree trRoot2;

            trees = new ImmutableContainer <TreeID, Tree>(tr => tr.ID, trRoot2 = trRoot2b);

            // Wait for the blobs to persist:
            await persistBlobs2;
            // Now persist the new tree:
            await trrepo.PersistTree(trRoot2.ID, trees);

            // Load a streamed blob:
            IStreamedBlob strbl = await blrepo.GetStreamedBlob(readmeBlob.ID);

            await strbl.ReadStream(blsr =>
            {
                Console.WriteLine("blob is {0} length", blsr.Length);

                byte[] dum = new byte[8040];
                int count  = 8040;

                try
                {
                    while ((count = blsr.Read(dum, 0, 8040)) > 0)
                    {
                        for (int i = 0; i < (count / 40) + ((count % 40) > 0 ? 1 : 0); ++i)
                        {
                            Console.WriteLine(dum.ToHexString(i * 40, Math.Min(count - (i * 40), 40)));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                }
            });
        }