Beispiel #1
0
        public async Task Sync(string commitId, bool checkForStaleRevision = true)
        {
            VersionsBlob versionsBlob = null;

            try
            {
                versionsBlob = await _client.GetJSON <VersionsBlob>("versions");
            }
            catch (ObjectNotFoundException)
            {
            }

            if (!String.IsNullOrWhiteSpace(versionsBlob?.Latest))
            {
                if (versionsBlob.Latest == commitId && await _client.Exists(commitId))
                {
                    return;
                }
                if (checkForStaleRevision)
                {
                    try{
                        await _shellExecutor.ExecTask("git", $"merge-base --is-ancestor {versionsBlob.Latest} {commitId}");
                    }
                    catch (Exception ex) {
                        // https://git-scm.com/docs/git-merge-base#git-merge-base---is-ancestor
                        if ((int)ex.Data["ExitCode"] == 1)
                        {
                            _metrics.Measure.Counter.Increment(_staleRevision);
                            throw new RevisionException(commitId, versionsBlob.Latest, "Stale Revision");
                        }

                        _metrics.Measure.Counter.Increment(_badRevision);
                        throw new RevisionException(commitId, versionsBlob.Latest, "Bad Revision");
                    }
                }
            }
            ;

            var newVersionBlob = new VersionsBlob
            {
                Latest   = commitId,
                Previous = versionsBlob?.Latest,
            };


            var(p, exited) = _shellExecutor("git", $"archive --format=zip {commitId}");
            using (var ms = new MemoryStream())
            {
                await p.StandardOutput.BaseStream.CopyToAsync(ms);

                ms.Position = 0;
                await exited;

                if (p.ExitCode != 0)
                {
                    await HandleArchiveError(p);
                }

                using (var zip = new ZipArchive(ms, ZipArchiveMode.Read, false))
                {
                    var files  = zip.Entries.Select(x => x.FullName).ToList();
                    var readFn = GetZipReader(zip);

                    foreach (var Converter in Converters)
                    {
                        var(fileName, fileContent, fileMimeType) = Converter.Convert(commitId, files, readFn);
                        await _client.PutString(fileName, fileContent, fileMimeType);

                        _metrics.Measure.Counter.Increment(_fileUpload, new MetricTags("FileName", fileName));
                    }
                }
            }

            await _client.PutJSON("versions", newVersionBlob);

            _metrics.Measure.Counter.Increment(_fileUpload, new MetricTags("FileName", "versions"));

            if (versionsBlob?.Previous != null)
            {
                await _client.Delete(versionsBlob.Previous);

                _metrics.Measure.Counter.Increment(_deletePrevious);
            }
        }
Beispiel #2
0
        public async Task Sync(string commitId, bool checkForStaleRevision = true)
        {
            VersionsBlob versionsBlob = null;

            try
            {
                versionsBlob = await _client.GetJSON <VersionsBlob>("versions");
            }
            catch (ObjectNotFoundException)
            {
            }

            if (!String.IsNullOrWhiteSpace(versionsBlob?.Latest))
            {
                if (versionsBlob.Latest == commitId && await _client.Exists(commitId))
                {
                    return;
                }
                if (checkForStaleRevision)
                {
                    try{
                        await _shellExecutor.ExecTask("git", $"merge-base --is-ancestor {versionsBlob.Latest} {commitId}");
                    }
                    catch (Exception ex) {
                        throw new StaleRevisionException(commitId, versionsBlob.Latest);
                    }
                }
            }
            ;

            var(p, exited) = _shellExecutor("git", $"archive --format=zip {commitId}");
            using (var ms = new MemoryStream())
            {
                await p.StandardOutput.BaseStream.CopyToAsync(ms);

                ms.Position = 0;
                await exited;

                if (p.ExitCode != 0)
                {
                    throw new Exception("Git archive failed")
                          {
                              Data =
                              {
                                  ["stderr"] = await p.StandardError.ReadToEndAsync(),
                                  ["code"]   = p.ExitCode,
                              },
                          };
                }

                using (var zip = new ZipArchive(ms, ZipArchiveMode.Read, false))
                {
                    var files  = zip.Entries.Select(x => x.FullName).ToList();
                    var bundle = _packer.Pack(files, GetZipReader(zip));
                    await _client.PutJSON(commitId, bundle);
                }
            }

            if (commitId != versionsBlob?.Latest)
            {
                var newVersionBlob = new VersionsBlob
                {
                    Latest   = commitId,
                    Previous = versionsBlob?.Latest,
                };
                await _client.PutJSON("versions", newVersionBlob);

                if (versionsBlob?.Previous != null)
                {
                    await _client.Delete(versionsBlob.Previous);
                }
            }
        }