public void PushChanges() { string master = GetTempPath(); using (GitClient git = new GitClient()) { GitInitArgs ia = new GitInitArgs(); ia.CreateBareRepository = true; ia.Description = "Harry & Sally root"; git.Init(master, ia); } Uri masterUri = new Uri(master); string harry = GetTempPath(); string sally = GetTempPath(); using (GitClient git = new GitClient()) // Harry { git.Clone(masterUri, harry); using (GitRepository harryRepo = new GitRepository(harry)) { harryRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.name", "Harry"); harryRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.email", "*****@*****.**"); } string src = Path.Combine(harry, "src"); Directory.CreateDirectory(src); string index = Path.Combine(src, "index.txt"); File.WriteAllText(index, "This is index.txt\n"); string appCode = Path.Combine(harry, "app.c"); File.WriteAllText(appCode, @" #include <stdio.h> int main(int argc, const char **argv) { printf(""hello world\n""); return 0; } "); git.Add(index); git.Add(appCode); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); git.Commit(harry, cma, out result); using (GitRepository harryRepo = new GitRepository(harry)) { GitCommit commit; Assert.That(harryRepo.Lookup(result, out commit)); Assert.That(commit.Author.Name, Is.EqualTo("Harry")); } } using (GitClient git = new GitClient()) // Sally { git.Clone(masterUri, sally); using (GitRepository sallyRepo = new GitRepository(sally)) { sallyRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.name", "Sally"); sallyRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.email", "*****@*****.**"); } string src = Path.Combine(sally, "src"); string index = Path.Combine(src, "index.txt"); Assert.That(File.Exists(index), Is.False); } using (GitClient git = new GitClient()) // Harry { GitPushArgs pa = new GitPushArgs(); pa.Mode = GitPushMode.All; git.Push(harry, pa); } using (GitClient git = new GitClient()) // Sally { GitPullArgs pa = new GitPullArgs(); pa.FetchArgs.All = true; git.Pull(sally, pa); string src = Path.Combine(sally, "src"); string index = Path.Combine(src, "index.txt"); Assert.That(File.Exists(index), Is.True); string appCode = Path.Combine(sally, "app.c"); File.WriteAllText(appCode, @" #include <stdio.h> int main(int argc, const char **argv) { int i; if (argc != 1) { fprintf(stderr, ""Usage %s <int>\n"", argv[0]); return 1; } for (i = 0; i < atoi(argv[1]); i++ printf(""hello world %d\n"", i); return 0; } "); git.Add(appCode); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 2, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 2, 0, 0, 0, DateTimeKind.Utc); git.Commit(sally, cma, out result); GitPushArgs ph = new GitPushArgs(); ph.Mode = GitPushMode.All; git.Push(sally, ph); } using (GitClient git = new GitClient()) // Harry { string appCode = Path.Combine(harry, "app.c"); File.WriteAllText(appCode, @" #include <stdio.h> int main(int argc, const char **argv) { if (argc > 0 && strcmp(argv[1], ""-V"") { printf(""%s version 1.0 (c) QQn\n""); return 0; } printf(""hello world\n""); return 0; } "); git.Add(appCode); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 3, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 3, 0, 0, 0, DateTimeKind.Utc); git.Commit(harry, cma, out result); // Local commit will succeed GitPushArgs ph = new GitPushArgs(); ph.Mode = GitPushMode.All; try { git.Push(harry, ph); // But push fails, as it conflicts Assert.Fail("Should have failed"); // ### } catch(GitException ge) { Assert.That(ge.Message, Is.StringContaining("Cannot push")); } GitPullArgs pa = new GitPullArgs(); pa.FetchArgs.All = true; git.Pull(harry, pa); bool gotConflict = false; git.Status(harry, delegate(object sender, GitStatusEventArgs e) { switch (e.RelativePath) { case "app.c": gotConflict = true; Assert.That(e.WorkingDirectoryStatus, Is.EqualTo(GitStatus.Normal)); Assert.That(e.IndexStatus, Is.EqualTo(GitStatus.Normal)); Assert.That(e.Conflicted, Is.True, "Conflicted"); break; default: Assert.Fail("Unexpected path: {0}", e.RelativePath); break; } }); Assert.That(gotConflict, "Found conflict status"); try { git.Push(harry, ph); // But push fails, as it conflicts Assert.Fail("Should still fail"); } catch (GitException ge) { Assert.That(ge.Message, Is.StringContaining("Cannot push")); } GitResetArgs ra = new GitResetArgs(); ra.Mode = GitResetMode.Hard; git.Reset(harry, ra); } }
public void ClonePublic() { string repos = GetTempPath(); using (GitClient git = new GitClient()) { GitCloneArgs ca = new GitCloneArgs(); ca.Synchronous = true; git.Clone(new Uri("https://github.com/libgit2/TestGitRepository.git"), repos, ca); List<string> found = new List<string>(); GitStatusArgs sa = new GitStatusArgs(); sa.IncludeUnmodified = true; sa.UseGlobPath = true; git.Status(repos, sa, delegate(object sender, GitStatusEventArgs e) { found.Add(e.RelativePath); }); Assert.That(found.Count, Is.EqualTo(8)); Assert.That(found, Is.All.Not.Null); Assert.That(found, Is.Unique); found.Clear(); git.Status(repos + "/a/*", sa, delegate(object sender, GitStatusEventArgs e) { found.Add(e.RelativePath); }); Assert.That(found.Count, Is.EqualTo(3)); Assert.That(found, Is.All.Not.Null); Assert.That(found, Is.Unique); GitFetchArgs fa = new GitFetchArgs(); fa.All = true; git.Fetch(repos, fa); using (GitRepository repo = new GitRepository(repos)) { Assert.That(repo.HeadBranch, Is.Not.Null); Assert.That(repo.HeadBranch.Name, Is.EqualTo("refs/heads/master")); Assert.That(repo.HeadBranch.UpstreamReference, Is.Not.Null); Assert.That(repo.HeadBranch.UpstreamReference.Name, Is.EqualTo("refs/remotes/origin/master")); Assert.That(repo.HeadBranch.IsHead, "Head knows that it is head"); //Assert.That(repo.HeadBranch.TrackedBranch, Is.Null); Assert.That(repo.HeadBranch.Name, Is.EqualTo("refs/heads/master")); Assert.That(repo.HeadBranch.IsRemote, Is.False, "Local branch"); Assert.That(repo.HeadBranch.RemoteName, Is.EqualTo("origin")); foreach (GitBranch b in repo.Branches.Remote) { Assert.That(b.IsRemote, "Remote branch"); Assert.That(b.IsLocal, Is.False, "Not local"); Assert.That(b.Name, Is.StringStarting("refs/remotes/origin/")); Assert.That(b.IsHead, Is.False); Assert.That(b.UpstreamReference, Is.Null); Assert.That(b.LocalUpstreamName, Is.Null); Assert.That(b.RemoteName, Is.EqualTo("origin")); } foreach (GitBranch b in repo.Branches.Local) { Assert.That(b.IsLocal, "Local branch"); Assert.That(b.IsRemote, Is.False, "Not remote"); Assert.That(b.Name, Is.StringStarting("refs/").Or.EqualTo("master")); Assert.That(b.IsHead, Is.EqualTo(b.ShortName == "master")); Assert.That(b.RemoteName, Is.EqualTo("origin")); if (!b.IsHead) { Assert.That(b.LocalUpstreamName, Is.Not.Null); GitBranch tracked = b.TrackedBranch; Assert.That(tracked, Is.Not.Null, "Have tracked"); Assert.That(b.RemoteName, Is.Not.Null); } } foreach (GitRemote r in repo.Remotes) { Assert.That(r.Name, Is.Not.Null); Assert.That(r.TagSynchronize, Is.EqualTo(GitTagSynchronize.Auto)); //Assert.That(r.Save(new GitFetchArgs())); foreach (GitRefSpec rs in r.FetchRefSpecs) { } foreach (GitRefSpec rs in r.PushRefSpecs) { } } } git.Pull(repos); // libgit2's local push code only supports bare repositories at // this time, so we use a few more clones to test the real push string cloneDir = GetTempPath(); GitCloneArgs cca = new GitCloneArgs(); cca.InitArgs.CreateBareRepository = true; git.Clone(repos, cloneDir, cca); string clone2Dir = GetTempPath(); git.Clone(cloneDir, clone2Dir); GitPushArgs pa = new GitPushArgs(); pa.Mode = GitPushMode.All; git.Push(clone2Dir, pa); } }
public void DualStart() { string master = GetTempPath(); using (GitClient git = new GitClient()) { GitInitArgs ia = new GitInitArgs(); ia.CreateBareRepository = true; ia.Description = "Harry & Sally root"; git.Init(master, ia); } Uri masterUri = new Uri(master); string harry = GetTempPath(); string sally = GetTempPath(); using (GitClient git = new GitClient()) // Harry { git.Clone(masterUri, harry); using (GitRepository harryRepo = new GitRepository(harry)) { harryRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.name", "Harry"); harryRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.email", "*****@*****.**"); } string src = Path.Combine(harry, "src"); Directory.CreateDirectory(src); string index = Path.Combine(src, "index.txt"); File.WriteAllText(index, "This is index.txt\n"); git.Add(index); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); git.Commit(harry, cma, out result); using (GitRepository harryRepo = new GitRepository(harry)) { GitCommit commit; Assert.That(harryRepo.Lookup(result, out commit)); Assert.That(commit.Author.Name, Is.EqualTo("Harry")); } } using (GitClient git = new GitClient()) // Sally { git.Clone(masterUri, sally); using (GitRepository sallyRepo = new GitRepository(sally)) { sallyRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.name", "Sally"); sallyRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.email", "*****@*****.**"); } string iota = Path.Combine(sally, "iota.txt"); File.WriteAllText(iota, "This is iota\n"); git.Stage(iota); GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc); git.Commit(sally, cma); string src = Path.Combine(sally, "src"); string index = Path.Combine(src, "index.txt"); Assert.That(File.Exists(index), Is.False); } using (GitClient git = new GitClient()) // Harry { GitPushArgs pa = new GitPushArgs(); pa.Mode = GitPushMode.All; git.Push(harry, pa); } using (GitClient git = new GitClient()) // Sally { GitPullArgs pa = new GitPullArgs(); pa.FetchArgs.All = true; git.Pull(sally, pa); string src = Path.Combine(sally, "src"); string index = Path.Combine(src, "index.txt"); Assert.That(File.Exists(index), Is.True); } }
public void PushChanges() { string master = GetTempPath(); using (GitClient git = new GitClient()) { GitInitArgs ia = new GitInitArgs(); ia.CreateBareRepository = true; ia.Description = "Harry & Sally root"; git.Init(master, ia); } Uri masterUri = new Uri(master); string harry = GetTempPath(); string sally = GetTempPath(); using (GitClient git = new GitClient()) // Harry { git.Clone(masterUri, harry); using (GitRepository harryRepo = new GitRepository(harry)) { harryRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.name", "Harry"); harryRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.email", "*****@*****.**"); } string src = Path.Combine(harry, "src"); Directory.CreateDirectory(src); string index = Path.Combine(src, "index.txt"); File.WriteAllText(index, "This is index.txt\n"); string appCode = Path.Combine(harry, "app.c"); File.WriteAllText(appCode, @" #include <stdio.h> int main(int argc, const char **argv) { printf(""hello world\n""); return 0; } "); git.Add(index); git.Add(appCode); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); git.Commit(harry, cma, out result); using (GitRepository harryRepo = new GitRepository(harry)) { GitCommit commit; Assert.That(harryRepo.Lookup(result, out commit)); Assert.That(commit.Author.Name, Is.EqualTo("Harry")); } } using (GitClient git = new GitClient()) // Sally { git.Clone(masterUri, sally); using (GitRepository sallyRepo = new GitRepository(sally)) { sallyRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.name", "Sally"); sallyRepo.Configuration.Set(GitConfigurationLevel.Repository, "user.email", "*****@*****.**"); } string src = Path.Combine(sally, "src"); string index = Path.Combine(src, "index.txt"); Assert.That(File.Exists(index), Is.False); } using (GitClient git = new GitClient()) // Harry { GitPushArgs pa = new GitPushArgs(); pa.Mode = GitPushMode.All; git.Push(harry, pa); } using (GitClient git = new GitClient()) // Sally { GitPullArgs pa = new GitPullArgs(); pa.FetchArgs.All = true; git.Pull(sally, pa); string src = Path.Combine(sally, "src"); string index = Path.Combine(src, "index.txt"); Assert.That(File.Exists(index), Is.True); string appCode = Path.Combine(sally, "app.c"); File.WriteAllText(appCode, @" #include <stdio.h> int main(int argc, const char **argv) { int i; if (argc != 1) { fprintf(stderr, ""Usage %s <int>\n"", argv[0]); return 1; } for (i = 0; i < atoi(argv[1]); i++ printf(""hello world %d\n"", i); return 0; } "); git.Add(appCode); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 2, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 2, 0, 0, 0, DateTimeKind.Utc); git.Commit(sally, cma, out result); GitPushArgs ph = new GitPushArgs(); ph.Mode = GitPushMode.All; git.Push(sally, ph); } using (GitClient git = new GitClient()) // Harry { string appCode = Path.Combine(harry, "app.c"); File.WriteAllText(appCode, @" #include <stdio.h> int main(int argc, const char **argv) { if (argc > 0 && strcmp(argv[1], ""-V"") { printf(""%s version 1.0 (c) QQn\n""); return 0; } printf(""hello world\n""); return 0; } "); git.Add(appCode); GitId result; GitCommitArgs cma = new GitCommitArgs(); cma.Signature.When = new DateTime(2000, 1, 3, 0, 0, 0, DateTimeKind.Utc); cma.Author.When = new DateTime(2000, 1, 3, 0, 0, 0, DateTimeKind.Utc); git.Commit(harry, cma, out result); // Local commit will succeed GitPushArgs ph = new GitPushArgs(); ph.Mode = GitPushMode.All; try { git.Push(harry, ph); // But push fails, as it conflicts Assert.Fail("Should have failed"); // ### } catch (GitException ge) { Assert.That(ge.Message, Contains.Substring(/*C*/ "annot push")); } GitPullArgs pa = new GitPullArgs(); pa.FetchArgs.All = true; git.Pull(harry, pa); bool gotConflict = false; git.Status(harry, delegate(object sender, GitStatusEventArgs e) { switch (e.RelativePath) { case "app.c": gotConflict = true; Assert.That(e.WorkingDirectoryStatus, Is.EqualTo(GitStatus.Normal)); Assert.That(e.IndexStatus, Is.EqualTo(GitStatus.Normal)); Assert.That(e.Conflicted, Is.True, "Conflicted"); break; default: Assert.Fail("Unexpected path: {0}", e.RelativePath); break; } }); Assert.That(gotConflict, "Found conflict status"); try { git.Push(harry, ph); // But push fails, as it conflicts Assert.Fail("Should still fail"); } catch (GitException ge) { Assert.That(ge.Message, Contains.Substring(/*C*/ "annot push")); } GitResetArgs ra = new GitResetArgs(); ra.Mode = GitResetMode.Hard; git.Reset(harry, ra); } }
public void ClonePublic() { string repos = GetTempPath(); using (GitClient git = new GitClient()) { GitCloneArgs ca = new GitCloneArgs(); ca.Synchronous = true; git.Clone(new Uri("https://github.com/libgit2/TestGitRepository.git"), repos, ca); List <string> found = new List <string>(); GitStatusArgs sa = new GitStatusArgs(); sa.IncludeUnmodified = true; sa.UseGlobPath = true; git.Status(repos, sa, delegate(object sender, GitStatusEventArgs e) { found.Add(e.RelativePath); }); Assert.That(found.Count, Is.EqualTo(8)); Assert.That(found, Is.All.Not.Null); Assert.That(found, Is.Unique); found.Clear(); git.Status(repos + "/a/*", sa, delegate(object sender, GitStatusEventArgs e) { found.Add(e.RelativePath); }); Assert.That(found.Count, Is.EqualTo(3)); Assert.That(found, Is.All.Not.Null); Assert.That(found, Is.Unique); GitFetchArgs fa = new GitFetchArgs(); fa.All = true; git.Fetch(repos, fa); using (GitRepository repo = new GitRepository(repos)) { Assert.That(repo.HeadBranch, Is.Not.Null); Assert.That(repo.HeadBranch.Name, Is.EqualTo("refs/heads/master")); Assert.That(repo.HeadBranch.UpstreamReference, Is.Not.Null); Assert.That(repo.HeadBranch.UpstreamReference.Name, Is.EqualTo("refs/remotes/origin/master")); Assert.That(repo.HeadBranch.IsHead, "Head knows that it is head"); //Assert.That(repo.HeadBranch.TrackedBranch, Is.Null); Assert.That(repo.HeadBranch.Name, Is.EqualTo("refs/heads/master")); Assert.That(repo.HeadBranch.IsRemote, Is.False, "Local branch"); Assert.That(repo.HeadBranch.RemoteName, Is.EqualTo("origin")); foreach (GitBranch b in repo.Branches.Remote) { Assert.That(b.IsRemote, "Remote branch"); Assert.That(b.IsLocal, Is.False, "Not local"); Assert.That(b.Name, Is.SubPathOf("refs/remotes/origin/")); Assert.That(b.IsHead, Is.False); Assert.That(b.UpstreamReference, Is.Null); Assert.That(b.LocalUpstreamName, Is.Null); Assert.That(b.RemoteName, Is.EqualTo("origin")); } foreach (GitBranch b in repo.Branches.Local) { Assert.That(b.IsLocal, "Local branch"); Assert.That(b.IsRemote, Is.False, "Not remote"); Assert.That(b.Name, Is.SubPathOf("refs/").Or.EqualTo("master")); Assert.That(b.IsHead, Is.EqualTo(b.ShortName == "master")); Assert.That(b.RemoteName, Is.EqualTo("origin")); if (!b.IsHead) { Assert.That(b.LocalUpstreamName, Is.Not.Null); GitBranch tracked = b.TrackedBranch; Assert.That(tracked, Is.Not.Null, "Have tracked"); Assert.That(b.RemoteName, Is.Not.Null); } } foreach (GitRemote r in repo.Remotes) { Assert.That(r.Name, Is.Not.Null); Assert.That(r.TagSynchronize, Is.EqualTo(GitTagSynchronize.Auto)); //Assert.That(r.Save(new GitFetchArgs())); foreach (GitRefSpec rs in r.FetchRefSpecs) { } foreach (GitRefSpec rs in r.PushRefSpecs) { } } } git.Pull(repos); // libgit2's local push code only supports bare repositories at // this time, so we use a few more clones to test the real push string cloneDir = GetTempPath(); GitCloneArgs cca = new GitCloneArgs(); cca.InitArgs.CreateBareRepository = true; git.Clone(repos, cloneDir, cca); string clone2Dir = GetTempPath(); git.Clone(cloneDir, clone2Dir); GitPushArgs pa = new GitPushArgs(); pa.Mode = GitPushMode.All; git.Push(clone2Dir, pa); } }