Skip to content

Show how to clone/commit/push from a local bare repository #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions LibGit2Sharp.Tests/CloneFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,51 @@ private void AssertLocalClone(string path)
}
}

[Fact]
public void CanLocallyCloneAndCommitAndPush()
{
var scd = BuildSelfCleaningDirectory();
using (var originalRepo = new Repository(BuildTemporaryCloneOfTestRepo(BareTestRepoPath).RepositoryPath))
using (Repository clonedRepo = Repository.Clone(originalRepo.Info.Path, scd.RootedDirectoryPath))
{
Remote remote = clonedRepo.Network.Remotes["origin"];

// Compare before
Assert.Equal(originalRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier,
clonedRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier);
Assert.Equal(clonedRepo.Network.ListReferences(remote).Single(r => r.CanonicalName == "refs/heads/master"),
clonedRepo.Refs.Head.ResolveToDirectReference());

// Change local state (commit)
const string relativeFilepath = "new_file.txt";
string filePath = Path.Combine(clonedRepo.Info.WorkingDirectory, relativeFilepath);
File.WriteAllText(filePath, "__content__");
clonedRepo.Index.Stage(relativeFilepath);
clonedRepo.Commit("__commit_message__", DummySignature, DummySignature);

// Assert local state has changed
Assert.NotEqual(originalRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier,
clonedRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier);
Assert.NotEqual(clonedRepo.Network.ListReferences(remote).Single(r => r.CanonicalName == "refs/heads/master"),
clonedRepo.Refs.Head.ResolveToDirectReference());

// Push the change upstream (remote state is supposed to change)
clonedRepo.Network.Push(remote, "HEAD", @"refs/heads/master", OnPushStatusError);

// Assert that both local and remote repos are in sync
Assert.Equal(originalRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier,
clonedRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier);
Assert.Equal(clonedRepo.Network.ListReferences(remote).Single(r => r.CanonicalName == "refs/heads/master"),
clonedRepo.Refs.Head.ResolveToDirectReference());
}
}

private void OnPushStatusError(PushStatusError pushStatusErrors)
{
Assert.True(false, string.Format("Failed to update reference '{0}': {1}",
pushStatusErrors.Reference, pushStatusErrors.Message));
}

[Fact]
public void CanCloneALocalRepositoryFromALocalUri()
{
Expand Down