Skip to content

Commit

Permalink
Revert "shallow clone for better performance (fixes #96)"
Browse files Browse the repository at this point in the history
  • Loading branch information
dabutvin committed Dec 20, 2018
1 parent 3f7c765 commit ff39276
Showing 1 changed file with 10 additions and 49 deletions.
59 changes: 10 additions & 49 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -15,40 +14,20 @@ namespace CompressImagesFunction
{
public static class CompressImages
{
private static string azGitPath = "D:\\Program Files\\Git\\bin\\git.exe";

public static bool Run(CompressimagesParameters parameters, ILogger logger)
{
var gitPath = azGitPath;
if (File.Exists(gitPath) == false)
{
gitPath = "git"; // rely on $PATH
}

CredentialsHandler credentialsProvider =
(url, user, cred) =>
new UsernamePasswordCredentials { Username = KnownGitHubs.Username, Password = parameters.Password };

// clone
var authCloneUrl = parameters.CloneUrl
.Replace("https://github.com", $"https://{KnownGitHubs.Username}:{parameters.Password}@github.com");
var cloneArgs = new[]
var cloneOptions = new CloneOptions
{
"clone",
"--depth 1",
authCloneUrl,
parameters.LocalPath
CredentialsProvider = credentialsProvider,
};
var gitClone = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = gitPath,
Arguments = string.Join(" ", cloneArgs),
}
};
gitClone.Start();
gitClone.WaitForExit();

Repository.Clone(parameters.CloneUrl, parameters.LocalPath, cloneOptions);

var repo = new Repository(parameters.LocalPath);
var remote = repo.Network.Remotes["origin"];

Expand Down Expand Up @@ -95,9 +74,6 @@ public static bool Run(CompressimagesParameters parameters, ILogger logger)
return false;
}

// save a pointer to the tip
var tipSha = repo.Head.Tip.Sha;

// check out the branch
repo.CreateBranch(KnownGitHubs.BranchName);
var branch = Commands.Checkout(repo, KnownGitHubs.BranchName);
Expand All @@ -109,16 +85,14 @@ public static bool Run(CompressimagesParameters parameters, ILogger logger)
var imagePaths = ImageQuery.FindImages(parameters.LocalPath, repoConfiguration);
var optimizedImages = OptimizeImages(repo, parameters.LocalPath, imagePaths, logger, repoConfiguration.AggressiveCompression);
if (optimizedImages.Length == 0)
{
return false;
}

// create commit message based on optimizations
foreach (var image in optimizedImages)
{
Commands.Stage(repo, image.OriginalPath);
}

// create commit message based on optimizations
var commitMessage = CommitMessage.Create(optimizedImages);

// commit
Expand All @@ -138,31 +112,18 @@ public static bool Run(CompressimagesParameters parameters, ILogger logger)

var signedCommitData = CommitSignature.Sign(commitBuffer + "\n", parameters.PgpPrivateKeyStream, parameters.PgPPassword);

repo.Reset(ResetMode.Soft, tipSha);
repo.Reset(ResetMode.Soft, repo.Head.Commits.Skip(1).First().Sha);
var commitToKeep = repo.ObjectDatabase.CreateCommitWithSignature(commitBuffer, signedCommitData);

repo.Refs.UpdateTarget(repo.Refs.Head, commitToKeep);
var branchAgain = Commands.Checkout(repo, KnownGitHubs.BranchName);
repo.Reset(ResetMode.Hard, commitToKeep.Sha);

// push to GitHub
var pushArgs = new[]
{
"push",
remote.Name,
KnownGitHubs.BranchName,
};
var gitpush = new Process
repo.Network.Push(remote, $"refs/heads/{KnownGitHubs.BranchName}", new PushOptions
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = parameters.LocalPath,
FileName = gitPath,
Arguments = string.Join(" ", pushArgs),
}
};
gitpush.Start();
gitpush.WaitForExit();
CredentialsProvider = credentialsProvider,
});

return true;
}
Expand Down

0 comments on commit ff39276

Please sign in to comment.