public virtual void TestCheckoutRemoteTrackingWithoutLocalBranch() { try { // create second repository Repository db2 = CreateWorkRepository(); Git git2 = new Git(db2); // setup the second repository to fetch from the first repository StoredConfig config = db2.GetConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db.Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.Update(config); config.Save(); // fetch from first repository RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*"); git2.Fetch().SetRemote("origin").SetRefSpecs(spec).Call(); // checkout remote tracking branch in second repository // (no local branches exist yet in second repository) git2.Checkout().SetName("remotes/origin/test").Call(); NUnit.Framework.Assert.AreEqual("[Test.txt, mode:100644, content:Some change]", IndexState (db2, CONTENT)); } catch (Exception e) { NUnit.Framework.Assert.Fail(e.Message); } }
public virtual void TestFetch() { // create other repository Repository db2 = CreateWorkRepository(); Git git2 = new Git(db2); // setup the first repository to fetch from the second repository StoredConfig config = ((FileBasedConfig)db.GetConfig()); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(db2.Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.Update(config); config.Save(); // create some refs via commits and tag RevCommit commit = git2.Commit().SetMessage("initial commit").Call(); Ref tagRef = git2.Tag().SetName("tag").Call(); Git git1 = new Git(db); RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); git1.Fetch().SetRemote("test").SetRefSpecs(spec).Call(); NUnit.Framework.Assert.AreEqual(commit.Id, db.Resolve(commit.Id.GetName() + "^{commit}" )); NUnit.Framework.Assert.AreEqual(tagRef.GetObjectId(), db.Resolve(tagRef.GetObjectId ().GetName())); }
public Models.Repository GetIncomingChanges(Models.Repository repository) { try { NGit.Api.Git git = NGit.Api.Git.Open(repository.Path); Repository repo = git.GetRepository(); repo.GetIndexFile(); //Repository repo = new FileRepository(new Sharpen.FilePath(path)); //NGit.Api.Git git = new NGit.Api.Git(repo); //TODO: Find out how git works really, and fix this. FetchCommand fc = git.Fetch(); FetchResult fr = fc.Call(); LogCommand lc = git.Log(); Iterable <RevCommit> lr = lc.Call(); if (git != null && repo != null && fr != null && lr != null) { List <Watchtower.Models.Changeset> commits = new List <Watchtower.Models.Changeset>(); foreach (RevCommit rc in lr) { //TODO: Find out if these values are right. Watchtower.Models.Changeset c = new Watchtower.Models.Changeset(rc.GetParent(0).GetHashCode().ToString(), rc.GetHashCode().ToString(), rc.GetCommitterIdent().GetEmailAddress(), rc.GetCommitterIdent().GetWhen(), rc.GetCommitterIdent().GetName(), rc.GetFullMessage()); commits.Add(c); } repository.IncomingChangesets = commits; } } catch { } return(repository); }
public virtual void TestFetch() { // create other repository Repository db2 = CreateWorkRepository(); Git git2 = new Git(db2); // setup the first repository to fetch from the second repository StoredConfig config = ((FileBasedConfig)db.GetConfig()); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(db2.Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.Update(config); config.Save(); // create some refs via commits and tag RevCommit commit = git2.Commit().SetMessage("initial commit").Call(); RevTag tag = git2.Tag().SetName("tag").Call(); Git git1 = new Git(db); RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); git1.Fetch().SetRemote("test").SetRefSpecs(spec).Call(); NUnit.Framework.Assert.AreEqual(commit.Id, db.Resolve(commit.Id.GetName() + "^{commit}" )); NUnit.Framework.Assert.AreEqual(tag.Id, db.Resolve(tag.Id.GetName())); }
/// <exception cref="System.Exception"></exception> private Git SetUpRepoWithRemote() { Repository remoteRepository = CreateWorkRepository(); Git remoteGit = new Git(remoteRepository); // commit something WriteTrashFile("Test.txt", "Hello world"); remoteGit.Add().AddFilepattern("Test.txt").Call(); initialCommit = remoteGit.Commit().SetMessage("Initial commit").Call(); WriteTrashFile("Test.txt", "Some change"); remoteGit.Add().AddFilepattern("Test.txt").Call(); secondCommit = remoteGit.Commit().SetMessage("Second commit").Call(); // create a master branch RefUpdate rup = remoteRepository.UpdateRef("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.ForceUpdate(); Repository localRepository = CreateWorkRepository(); Git localGit = new Git(localRepository); StoredConfig config = localRepository.GetConfig(); RemoteConfig rc = new RemoteConfig(config, "origin"); rc.AddURI(new URIish(remoteRepository.Directory.GetPath())); rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); rc.Update(config); config.Save(); FetchResult res = localGit.Fetch().SetRemote("origin").Call(); NUnit.Framework.Assert.IsFalse(res.GetTrackingRefUpdates().IsEmpty()); rup = localRepository.UpdateRef("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.ForceUpdate(); rup = localRepository.UpdateRef(Constants.HEAD); rup.Link("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.Update(); return(localGit); }
public override Repository Publish (string serverPath, FilePath localPath, FilePath[] files, string message, IProgressMonitor monitor) { // Initialize the repository RootRepository = GitUtil.Init (localPath, Url, monitor); NGit.Api.Git git = new NGit.Api.Git (RootRepository); try { var refs = git.Fetch ().Call ().GetAdvertisedRefs (); if (refs.Count > 0) { throw new UserException ("The remote repository already contains branches. MonoDevelop can only publish to an empty repository"); } } catch { try { RootRepository.Close (); } catch { } if (Directory.Exists (RootRepository.Directory)) Directory.Delete (RootRepository.Directory, true); RootRepository = null; throw; } RootPath = localPath; // Add the project files ChangeSet cs = CreateChangeSet (localPath); var cmd = git.Add (); foreach (FilePath fp in files) { cmd.AddFilepattern (RootRepository.ToGitPath (fp)); cs.AddFile (fp); } cmd.Call (); // Create the initial commit cs.GlobalComment = message; Commit (cs, monitor); // Push to remote repo Push (monitor, "origin", "master"); return this; }
public void Fetch(Git git, BusyIndicatorProgressMonitor monitor) { FetchCommand command = git.Fetch(); RefSpec spec = new RefSpec("refs/heads/master:refs/heads/FETCH_HEAD"); command.SetRefSpecs(spec); command.SetProgressMonitor(monitor); BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (s, evt) => { monitor.StartAction(); try { command.Call(); } catch (JGitInternalException) { // TODO: } }; bw.RunWorkerCompleted += (s, evt) => { monitor.CompleteAction(); }; bw.RunWorkerAsync(); }
/// <summary> /// ใใงใใ /// </summary> /// <param name="git"></param> /// <param name="privateKeyData"></param> /// <param name="publicKeyData"></param> /// <param name="monitor"></param> public void Fetch(Git git, CloneEntity entity, string privateKeyData, string publicKeyData, BusyIndicatorProgressMonitor monitor) { var customConfigSessionFactory = new CustomConfigSessionFactory(); customConfigSessionFactory.PrivateKey = privateKeyData; customConfigSessionFactory.PublicKey = publicKeyData; NGit.Transport.JschConfigSessionFactory.SetInstance(customConfigSessionFactory); UsernamePasswordCredentialsProvider creds = new UsernamePasswordCredentialsProvider(entity.UserName, entity.PassWord); FetchCommand command = git.Fetch(); RefSpec spec = new RefSpec("refs/heads/master:refs/heads/FETCH_HEAD"); command.SetRemoveDeletedRefs(true); command.SetRefSpecs(spec); command.SetProgressMonitor(monitor); command.SetCredentialsProvider(creds); BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (s, evt) => { monitor.StartAction(); try { command.Call(); } catch (JGitInternalException) { // TODO: } }; bw.RunWorkerCompleted += (s, evt) => { monitor.CompleteAction(); }; bw.RunWorkerAsync(); }
public virtual void TestCheckoutRemoteTrackingWithoutLocalBranch() { // create second repository Repository db2 = CreateWorkRepository(); Git git2 = new Git(db2); // setup the second repository to fetch from the first repository StoredConfig config = db2.GetConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db.Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.Update(config); config.Save(); // fetch from first repository RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*"); git2.Fetch().SetRemote("origin").SetRefSpecs(spec).Call(); // checkout remote tracking branch in second repository // (no local branches exist yet in second repository) git2.Checkout().SetName("remotes/origin/test").Call(); NUnit.Framework.Assert.AreEqual("[Test.txt, mode:100644, content:Some change]", IndexState (db2, CONTENT)); }
/// <exception cref="System.Exception"></exception> private Git SetUpRepoWithRemote() { Repository remoteRepository = CreateWorkRepository(); Git remoteGit = new Git(remoteRepository); // commit something WriteTrashFile("Test.txt", "Hello world"); remoteGit.Add().AddFilepattern("Test.txt").Call(); initialCommit = remoteGit.Commit().SetMessage("Initial commit").Call(); WriteTrashFile("Test.txt", "Some change"); remoteGit.Add().AddFilepattern("Test.txt").Call(); secondCommit = remoteGit.Commit().SetMessage("Second commit").Call(); // create a master branch RefUpdate rup = remoteRepository.UpdateRef("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.ForceUpdate(); Repository localRepository = CreateWorkRepository(); Git localGit = new Git(localRepository); StoredConfig config = localRepository.GetConfig(); RemoteConfig rc = new RemoteConfig(config, "origin"); rc.AddURI(new URIish(remoteRepository.Directory.GetPath())); rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); rc.Update(config); config.Save(); FetchResult res = localGit.Fetch().SetRemote("origin").Call(); NUnit.Framework.Assert.IsFalse(res.GetTrackingRefUpdates().IsEmpty()); rup = localRepository.UpdateRef("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.ForceUpdate(); rup = localRepository.UpdateRef(Constants.HEAD); rup.Link("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.Update(); return localGit; }