public virtual void EmptyRepositoryFormatVersion() { FileRepository r = CreateWorkRepository(); FileBasedConfig config = ((FileBasedConfig)r.GetConfig()); config.SetString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION , string.Empty); config.Save(); new FileRepository(r.Directory); }
public virtual void Test007_Open() { FileRepository db2 = new FileRepository(db.Directory); NUnit.Framework.Assert.AreEqual(db.Directory, db2.Directory); NUnit.Framework.Assert.AreEqual(((ObjectDirectory)db.ObjectDatabase).GetDirectory (), ((ObjectDirectory)db2.ObjectDatabase).GetDirectory()); NUnit.Framework.Assert.AreNotSame(((FileBasedConfig)db.GetConfig()), ((FileBasedConfig )db2.GetConfig())); }
public virtual void InvalidRepositoryFormatVersion() { FileRepository r = CreateWorkRepository(); FileBasedConfig config = ((FileBasedConfig)r.GetConfig()); config.SetString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION , "notanumber"); config.Save(); try { new FileRepository(r.Directory); NUnit.Framework.Assert.Fail("IllegalArgumentException not thrown"); } catch (ArgumentException e) { NUnit.Framework.Assert.IsNotNull(e.Message); } }
public virtual void Test000_openrepo_default_relative_workdirconfig() { FilePath repo1Parent = new FilePath(trash.GetParentFile(), "r1"); FilePath workdir = new FilePath(trash.GetParentFile(), "rw"); FileUtils.Mkdir(workdir); FileRepository repo1initial = new FileRepository(new FilePath(repo1Parent, Constants .DOT_GIT)); repo1initial.Create(); FileBasedConfig cfg = ((FileBasedConfig)repo1initial.GetConfig()); cfg.SetString("core", null, "worktree", "../../rw"); cfg.Save(); repo1initial.Close(); FilePath theDir = new FilePath(repo1Parent, Constants.DOT_GIT); FileRepository r = new FileRepositoryBuilder().SetGitDir(theDir).Build(); AssertEqualsPath(theDir, r.Directory); AssertEqualsPath(workdir, r.WorkTree); AssertEqualsPath(new FilePath(theDir, "index"), r.GetIndexFile()); AssertEqualsPath(new FilePath(theDir, "objects"), ((ObjectDirectory)r.ObjectDatabase ).GetDirectory()); }
public InstallerMessage SetRepository(string uri, string user, string password, bool replaceExisting = false) { LastException = null; try { if (RepoIsClonned()) { if (replaceExisting) { try { Directory.Delete(_repoPath, true); } catch (Exception e) { throw new Exception("Could not delete current repository. " + e.Message); } } else { if (!GitHelper.isValidLocalRepository(Path.Combine(_repoPath, @".git"))) throw new Exception("There are files stored where the repository would be cloned. Clone canceled"); var repo = new FileRepository(Path.Combine(_repoPath, @".git")); var c = repo.GetConfig(); var remoteUrl = c.GetString("remote", "origin", "url"); var isSameRepo = remoteUrl != uri; if (!isSameRepo) { var remotes = c.GetSubsections("remote"); foreach (var remote in remotes) { var rUrl = c.GetString("remote", remote, "url"); if (String.IsNullOrEmpty(remoteUrl)) // let's keep the first we find remoteUrl = rUrl; if (rUrl == uri) { isSameRepo = true; break; } } if (!isSameRepo) { if (!String.IsNullOrEmpty(remoteUrl)) throw new Exception("There is already a repository pointing to " + remoteUrl + " where the wiki should be cloned."); throw new Exception("There is already a repository where the wiki should be cloned."); } } return InstallerMessage.Success; } } if (!Directory.Exists(_repoPath)) { if (!Directory.Exists(Path.Combine(_appRoot, "App_Data"))) Directory.CreateDirectory(Path.Combine(_appRoot, "App_Data")); Directory.CreateDirectory(_repoPath); } var cmd = new CloneCommand(); if(!String.IsNullOrEmpty(user)) cmd.SetCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)); cmd.SetURI(uri); cmd.SetCloneSubmodules(true); cmd.SetDirectory(_repoPath); cmd.Call(); } catch (Exception e) { LastException = e; return InstallerMessage.ExceptionThrown; } if (RepoIsClonned()) { WG.Settings.Repository = uri; return InstallerMessage.Success; } return InstallerMessage.UnkownFailure; }
public override void SetUp() { base.SetUp(); dbTarget = CreateWorkRepository(); source = new Git(db); target = new Git(dbTarget); // put some file in the source repo sourceFile = new FilePath(db.WorkTree, "SomeFile.txt"); WriteToFile(sourceFile, "Hello world"); // and commit it source.Add().AddFilepattern("SomeFile.txt").Call(); source.Commit().SetMessage("Initial commit for source").Call(); // configure the target repo to connect to the source via "origin" StoredConfig targetConfig = ((FileBasedConfig)dbTarget.GetConfig()); targetConfig.SetString("branch", "master", "remote", "origin"); targetConfig.SetString("branch", "master", "merge", "refs/heads/master"); RemoteConfig config = new RemoteConfig(targetConfig, "origin"); config.AddURI(new URIish(source.GetRepository().WorkTree.GetPath())); config.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); config.Update(targetConfig); targetConfig.Save(); targetFile = new FilePath(dbTarget.WorkTree, "SomeFile.txt"); // make sure we have the same content target.Pull().Call(); target.Checkout().SetStartPoint("refs/remotes/origin/master").SetName("master").Call (); targetConfig.SetString("branch", "master", "merge", "refs/heads/master"); targetConfig.SetBoolean("branch", "master", "rebase", true); targetConfig.Save(); AssertFileContentsEqual(targetFile, "Hello world"); }
/// <summary> /// Like "git prune" this method tries to prune all loose objects which are /// unreferenced. /// </summary> /// <remarks> /// Like "git prune" this method tries to prune all loose objects which are /// unreferenced. If certain objects can't be pruned (e.g. because the /// filesystem delete operation fails) this is silently ignored. /// </remarks> /// <param name="objectsToKeep">a set of objects which should explicitly not be pruned /// </param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> /// <exception cref="Sharpen.ParseException"> /// If the configuration parameter "gc.pruneexpire" couldn't be /// parsed /// </exception> public virtual void Prune(ICollection <ObjectId> objectsToKeep) { long expireDate = long.MaxValue; if (expire == null && expireAgeMillis == -1) { string pruneExpireStr = ((FileBasedConfig)repo.GetConfig()).GetString(ConfigConstants .CONFIG_GC_SECTION, null, ConfigConstants.CONFIG_KEY_PRUNEEXPIRE); if (pruneExpireStr == null) { pruneExpireStr = PRUNE_EXPIRE_DEFAULT; } expire = GitDateParser.Parse(pruneExpireStr, null); expireAgeMillis = -1; } if (expire != null) { expireDate = expire.Value.GetTime(); } if (expireAgeMillis != -1) { expireDate = Runtime.CurrentTimeMillis() - expireAgeMillis; } // Collect all loose objects which are old enough, not referenced from // the index and not in objectsToKeep IDictionary <ObjectId, FilePath> deletionCandidates = new Dictionary <ObjectId, FilePath >(); ICollection <ObjectId> indexObjects = null; FilePath objects = repo.ObjectsDirectory; string[] fanout = objects.List(); if (fanout != null && fanout.Length > 0) { pm.BeginTask(JGitText.Get().pruneLooseUnreferencedObjects, fanout.Length); try { foreach (string d in fanout) { pm.Update(1); if (d.Length != 2) { continue; } FilePath[] entries = new FilePath(objects, d).ListFiles(); if (entries == null) { continue; } foreach (FilePath f in entries) { string fName = f.GetName(); if (fName.Length != Constants.OBJECT_ID_STRING_LENGTH - 2) { continue; } if (f.LastModified() >= expireDate) { continue; } try { ObjectId id = ObjectId.FromString(d + fName); if (objectsToKeep.Contains(id)) { continue; } if (indexObjects == null) { indexObjects = ListNonHEADIndexObjects(); } if (indexObjects.Contains(id)) { continue; } deletionCandidates.Put(id, f); } catch (ArgumentException) { // ignoring the file that does not represent loose // object continue; } } } } finally { pm.EndTask(); } } if (deletionCandidates.IsEmpty()) { return; } // From the set of current refs remove all those which have been handled // during last repack(). Only those refs will survive which have been // added or modified since the last repack. Only these can save existing // loose refs from being pruned. IDictionary <string, Ref> newRefs; if (lastPackedRefs == null || lastPackedRefs.IsEmpty()) { newRefs = GetAllRefs(); } else { newRefs = new Dictionary <string, Ref>(); for (Iterator <KeyValuePair <string, Ref> > i = GetAllRefs().EntrySet().Iterator(); i.HasNext();) { KeyValuePair <string, Ref> newEntry = i.Next(); Ref old = lastPackedRefs.Get(newEntry.Key); if (!Equals(newEntry.Value, old)) { newRefs.Put(newEntry.Key, newEntry.Value); } } } if (!newRefs.IsEmpty()) { // There are new/modified refs! Check which loose objects are now // referenced by these modified refs (or their reflogentries). // Remove these loose objects // from the deletionCandidates. When the last candidate is removed // leave this method. ObjectWalk w = new ObjectWalk(repo); try { foreach (Ref cr in newRefs.Values) { w.MarkStart(w.ParseAny(cr.GetObjectId())); } if (lastPackedRefs != null) { foreach (Ref lpr in lastPackedRefs.Values) { w.MarkUninteresting(w.ParseAny(lpr.GetObjectId())); } } RemoveReferenced(deletionCandidates, w); } finally { w.Dispose(); } } if (deletionCandidates.IsEmpty()) { return; } // Since we have not left the method yet there are still // deletionCandidates. Last chance for these objects not to be pruned is // that they are referenced by reflog entries. Even refs which currently // point to the same object as during last repack() may have // additional reflog entries not handled during last repack() ObjectWalk w_1 = new ObjectWalk(repo); try { foreach (Ref ar in GetAllRefs().Values) { foreach (ObjectId id in ListRefLogObjects(ar, lastRepackTime)) { w_1.MarkStart(w_1.ParseAny(id)); } } if (lastPackedRefs != null) { foreach (Ref lpr in lastPackedRefs.Values) { w_1.MarkUninteresting(w_1.ParseAny(lpr.GetObjectId())); } } RemoveReferenced(deletionCandidates, w_1); } finally { w_1.Dispose(); } if (deletionCandidates.IsEmpty()) { return; } // delete all candidates which have survived: these are unreferenced // loose objects foreach (FilePath f_1 in deletionCandidates.Values) { f_1.Delete(); } ((ObjectDirectory)repo.ObjectDatabase).Close(); }