public static LocalGitRepository Init(string targetLocalPath, string url) { InitCommand ci = new InitCommand(); ci.SetDirectory(targetLocalPath); ci.Call(); LocalGitRepository repo = new LocalGitRepository(Path.Combine(targetLocalPath, Constants.DOT_GIT)); string branch = Constants.R_HEADS + "master"; RefUpdate head = repo.UpdateRef(Constants.HEAD); head.DisableRefLog(); head.Link(branch); if (url != null) { RemoteConfig remoteConfig = new RemoteConfig(repo.GetConfig(), "origin"); remoteConfig.AddURI(new URIish(url)); string dst = Constants.R_REMOTES + remoteConfig.Name; RefSpec wcrs = new RefSpec(); wcrs = wcrs.SetForceUpdate(true); wcrs = wcrs.SetSourceDestination(Constants.R_HEADS + "*", dst + "/*"); remoteConfig.AddFetchRefSpec(wcrs); remoteConfig.Update(repo.GetConfig()); } // we're setting up for a clone with a checkout repo.GetConfig().SetBoolean("core", null, "bare", false); repo.GetConfig().Save(); return(repo); }
public virtual void TestUpdateSmudgedEntries() { git.BranchCreate().SetName("test2").Call(); RefUpdate rup = db.UpdateRef(Constants.HEAD); rup.Link("refs/heads/test2"); FilePath file = new FilePath(db.WorkTree, "Test.txt"); long size = file.Length(); long mTime = file.LastModified() - 5000L; NUnit.Framework.Assert.IsTrue(file.SetLastModified(mTime)); DirCache cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem); DirCacheEntry entry = cache.GetEntry("Test.txt"); NUnit.Framework.Assert.IsNotNull(entry); entry.SetLength(0); entry.LastModified = 0; cache.Write(); NUnit.Framework.Assert.IsTrue(cache.Commit()); cache = DirCache.Read(db.GetIndexFile(), db.FileSystem); entry = cache.GetEntry("Test.txt"); NUnit.Framework.Assert.IsNotNull(entry); NUnit.Framework.Assert.AreEqual(0, entry.Length); NUnit.Framework.Assert.AreEqual(0, entry.LastModified); db.GetIndexFile().SetLastModified(db.GetIndexFile().LastModified() - 5000); NUnit.Framework.Assert.IsNotNull(git.Checkout().SetName("test").Call()); cache = DirCache.Read(db.GetIndexFile(), db.FileSystem); entry = cache.GetEntry("Test.txt"); NUnit.Framework.Assert.IsNotNull(entry); NUnit.Framework.Assert.AreEqual(size, entry.Length); NUnit.Framework.Assert.AreEqual(mTime, entry.LastModified); }
/// <exception cref="System.IO.IOException"></exception> private RebaseResult Abort(RebaseResult result) { try { string commitId = ReadFile(repo.Directory, Constants.ORIG_HEAD); monitor.BeginTask(MessageFormat.Format(JGitText.Get().abortingRebase, commitId), ProgressMonitor.UNKNOWN); DirCacheCheckout dco; RevCommit commit = walk.ParseCommit(repo.Resolve(commitId)); if (result.GetStatus().Equals(RebaseResult.Status.FAILED)) { RevCommit head = walk.ParseCommit(repo.Resolve(Constants.HEAD)); dco = new DirCacheCheckout(repo, head.Tree, repo.LockDirCache(), commit.Tree); } else { dco = new DirCacheCheckout(repo, repo.LockDirCache(), commit.Tree); } dco.SetFailOnConflict(false); dco.Checkout(); walk.Release(); } finally { monitor.EndTask(); } try { string headName = ReadFile(rebaseDir, HEAD_NAME); if (headName.StartsWith(Constants.R_REFS)) { monitor.BeginTask(MessageFormat.Format(JGitText.Get().resettingHead, headName), ProgressMonitor .UNKNOWN); // update the HEAD RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD, false); RefUpdate.Result res = refUpdate.Link(headName); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException(JGitText.Get().abortingRebaseFailed); } } } // cleanup the files FileUtils.Delete(rebaseDir, FileUtils.RECURSIVE); return(result); } finally { monitor.EndTask(); } }
public override void Setup() { // Generate directories and a svn util. RemotePath = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar); LocalPath = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar); Directory.CreateDirectory(RemotePath.FullPath + "repo.git"); RemoteUrl = "file:///" + RemotePath.FullPath + "repo.git"; // Initialize the bare repo. var ci = new InitCommand(); ci.SetDirectory(new Sharpen.FilePath(RemotePath.FullPath + "repo.git")); ci.SetBare(true); ci.Call(); var bare = new FileRepository(new Sharpen.FilePath(RemotePath.FullPath + "repo.git")); string branch = Constants.R_HEADS + "master"; RefUpdate head = bare.UpdateRef(Constants.HEAD); head.DisableRefLog(); head.Link(branch); // Check out the repository. Checkout(LocalPath, RemoteUrl); Repo = GetRepo(LocalPath, RemoteUrl); DotDir = ".git"; }
public override void Setup() { // Generate directories and a svn util. rootUrl = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar); rootCheckout = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar); Directory.CreateDirectory(rootUrl.FullPath + "repo.git"); repoLocation = "file:///" + rootUrl.FullPath + "repo.git"; // Initialize the bare repo. InitCommand ci = new InitCommand(); ci.SetDirectory(new Sharpen.FilePath(rootUrl.FullPath + "repo.git")); ci.SetBare(true); ci.Call(); FileRepository bare = new FileRepository(new Sharpen.FilePath(rootUrl.FullPath + "repo.git")); string branch = Constants.R_HEADS + "master"; RefUpdate head = bare.UpdateRef(Constants.HEAD); head.DisableRefLog(); head.Link(branch); // Check out the repository. Checkout(rootCheckout, repoLocation); repo = GetRepo(rootCheckout, repoLocation); DOT_DIR = ".git"; }
private bool LinkHEAD(RefUpdate target) { try { RefUpdate u = ((RefDirectoryUpdate)refdb.NewUpdate(Constants.HEAD, false)); u.DisableRefLog(); switch (u.Link(target.GetName())) { case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { return(true); } default: { return(false); break; } } } catch (IOException) { return(false); } }
/// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> /// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> private void Checkout(Repository repo, FetchResult result) { if (branch.StartsWith(Constants.R_HEADS)) { RefUpdate head = repo.UpdateRef(Constants.HEAD); head.DisableRefLog(); head.Link(branch); } Ref head_1 = result.GetAdvertisedRef(branch); if (head_1 == null || head_1.GetObjectId() == null) { return; } // throw exception? RevCommit commit = ParseCommit(repo, head_1); bool detached = !head_1.GetName().StartsWith(Constants.R_HEADS); RefUpdate u = repo.UpdateRef(Constants.HEAD, detached); u.SetNewObjectId(commit.Id); u.ForceUpdate(); if (!bare) { DirCache dc = repo.LockDirCache(); DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.Tree); co.Checkout(); } }
/// <summary> /// Create a new Git repository initializing the necessary files and /// directories. /// </summary> /// <remarks> /// Create a new Git repository initializing the necessary files and /// directories. /// </remarks> /// <param name="bare">if true, a bare repository is created.</param> /// <exception cref="System.IO.IOException">in case of IO problem</exception> public override void Create(bool bare) { FileBasedConfig cfg = ((FileBasedConfig)GetConfig()); if (cfg.GetFile().Exists()) { throw new InvalidOperationException(MessageFormat.Format(JGitText.Get().repositoryAlreadyExists , Directory)); } FileUtils.Mkdirs(Directory, true); refs.Create(); objectDatabase.Create(); FileUtils.Mkdir(new FilePath(Directory, "branches")); FileUtils.Mkdir(new FilePath(Directory, "hooks")); RefUpdate head = UpdateRef(Constants.HEAD); head.DisableRefLog(); head.Link(Constants.R_HEADS + Constants.MASTER); bool fileMode; if (FileSystem.SupportsExecute()) { FilePath tmp = FilePath.CreateTempFile("try", "execute", Directory); FileSystem.SetExecute(tmp, true); bool on = FileSystem.CanExecute(tmp); FileSystem.SetExecute(tmp, false); bool off = FileSystem.CanExecute(tmp); FileUtils.Delete(tmp); fileMode = on && !off; } else { fileMode = false; } cfg.SetInt(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION , 0); cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE , fileMode); if (bare) { cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_BARE , true); } cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES , !bare); if (SystemReader.GetInstance().IsMacOS()) { // Java has no other way cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_PRECOMPOSEUNICODE , true); } cfg.Save(); }
/// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.IO.IOException"></exception> private void CheckoutBranch(string branchName) { RevWalk walk = new RevWalk(db); RevCommit head = walk.ParseCommit(db.Resolve(Constants.HEAD)); RevCommit branch = walk.ParseCommit(db.Resolve(branchName)); DirCacheCheckout dco = new DirCacheCheckout(db, head.Tree.Id, db.LockDirCache(), branch.Tree.Id); dco.SetFailOnConflict(true); dco.Checkout(); walk.Release(); // update the HEAD RefUpdate refUpdate = db.UpdateRef(Constants.HEAD); refUpdate.Link(branchName); }
public override void SetUp() { base.SetUp(); git = new Git(db); // commit something WriteTrashFile("Test.txt", "Hello world"); git.Add().AddFilepattern("Test.txt").Call(); initialCommit = git.Commit().SetMessage("Initial commit").Call(); // create a master branch and switch to it git.BranchCreate().SetName("test").Call(); RefUpdate rup = db.UpdateRef(Constants.HEAD); rup.Link("refs/heads/test"); // commit something on the test branch WriteTrashFile("Test.txt", "Some change"); git.Add().AddFilepattern("Test.txt").Call(); secondCommit = git.Commit().SetMessage("Second commit").Call(); }
/// <exception cref="System.IO.IOException"></exception> private void WriteSymref(string src, string dst) { RefUpdate u = db.UpdateRef(src); switch (u.Link(dst)) { case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { NUnit.Framework.Assert.Fail("link " + src + " to " + dst); break; } } }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> private void Checkout(Repository clonedRepo, FetchResult result) { Ref head = result.GetAdvertisedRef(branch); if (branch.Equals(Constants.HEAD)) { Ref foundBranch = FindBranchToCheckout(result); if (foundBranch != null) { head = foundBranch; } } if (head == null || head.GetObjectId() == null) { return; } // throw exception? if (head.GetName().StartsWith(Constants.R_HEADS)) { RefUpdate newHead = clonedRepo.UpdateRef(Constants.HEAD); newHead.DisableRefLog(); newHead.Link(head.GetName()); AddMergeConfig(clonedRepo, head); } RevCommit commit = ParseCommit(clonedRepo, head); bool detached = !head.GetName().StartsWith(Constants.R_HEADS); RefUpdate u = clonedRepo.UpdateRef(Constants.HEAD, detached); u.SetNewObjectId(commit.Id); u.ForceUpdate(); if (!bare) { DirCache dc = clonedRepo.LockDirCache(); DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc, commit.Tree); co.Checkout(); if (cloneSubmodules) { CloneSubmodules(clonedRepo); } } }
/// <exception cref="System.IO.IOException"></exception> private void UpdateHead(string headName, RevCommit newHead) { // point the previous head (if any) to the new commit if (headName.StartsWith(Constants.R_REFS)) { RefUpdate rup = repo.UpdateRef(headName); rup.SetNewObjectId(newHead); RefUpdate.Result res = rup.ForceUpdate(); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } rup = repo.UpdateRef(Constants.HEAD); res = rup.Link(headName); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } } }
/// <exception cref="System.Exception"></exception> public override void SetUp() { base.SetUp(); tr = new TestRepository <Repository>(db); git = new Git(db); // commit something WriteTrashFile("Test.txt", "Hello world"); git.Add().AddFilepattern("Test.txt").Call(); git.Commit().SetMessage("Initial commit").Call(); // create a master branch and switch to it git.BranchCreate().SetName("test").Call(); RefUpdate rup = db.UpdateRef(Constants.HEAD); rup.Link("refs/heads/test"); // commit something on the test branch WriteTrashFile("Test.txt", "Some change"); git.Add().AddFilepattern("Test.txt").Call(); git.Commit().SetMessage("Second commit").Call(); RevBlob blob = tr.Blob("blob-not-in-master-branch"); git.Tag().SetName("tag-for-blob").SetObjectId(blob).Call(); }
/// <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); }
/// <summary> /// Executes the /// <code>Rebase</code> /// command with all the options and parameters /// collected by the setter methods of this class. Each instance of this /// class should only be used for one invocation of the command. Don't call /// this method twice on an instance. /// </summary> /// <returns>an object describing the result of this command</returns> /// <exception cref="NGit.Api.Errors.NoHeadException"></exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException"></exception> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override RebaseResult Call() { RevCommit newHead = null; bool lastStepWasForward = false; CheckCallable(); CheckParameters(); try { switch (operation) { case RebaseCommand.Operation.ABORT: { try { return(Abort()); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } goto case RebaseCommand.Operation.SKIP; } case RebaseCommand.Operation.SKIP: case RebaseCommand.Operation.CONTINUE: { // fall through string upstreamCommitName = ReadFile(rebaseDir, ONTO); this.upstreamCommit = walk.ParseCommit(repo.Resolve(upstreamCommitName)); break; } case RebaseCommand.Operation.BEGIN: { RebaseResult res = InitFilesAndRewind(); if (res != null) { return(res); } break; } } if (monitor.IsCancelled()) { return(Abort()); } if (this.operation == RebaseCommand.Operation.CONTINUE) { newHead = ContinueRebase(); } if (this.operation == RebaseCommand.Operation.SKIP) { newHead = CheckoutCurrentHead(); } ObjectReader or = repo.NewObjectReader(); IList <RebaseCommand.Step> steps = LoadSteps(); foreach (RebaseCommand.Step step in steps) { PopSteps(1); ICollection <ObjectId> ids = or.Resolve(step.commit); if (ids.Count != 1) { throw new JGitInternalException("Could not resolve uniquely the abbreviated object ID" ); } RevCommit commitToPick = walk.ParseCommit(ids.Iterator().Next()); if (monitor.IsCancelled()) { return(new RebaseResult(commitToPick)); } monitor.BeginTask(MessageFormat.Format(JGitText.Get().applyingCommit, commitToPick .GetShortMessage()), ProgressMonitor.UNKNOWN); // if the first parent of commitToPick is the current HEAD, // we do a fast-forward instead of cherry-pick to avoid // unnecessary object rewriting newHead = TryFastForward(commitToPick); lastStepWasForward = newHead != null; if (!lastStepWasForward) { // TODO if the content of this commit is already merged here // we should skip this step in order to avoid confusing // pseudo-changed newHead = new Git(repo).CherryPick().Include(commitToPick).Call(); } monitor.EndTask(); if (newHead == null) { return(Stop(commitToPick)); } } if (newHead != null) { // point the previous head (if any) to the new commit string headName = ReadFile(rebaseDir, HEAD_NAME); if (headName.StartsWith(Constants.R_REFS)) { RefUpdate rup = repo.UpdateRef(headName); rup.SetNewObjectId(newHead); RefUpdate.Result res_1 = rup.ForceUpdate(); switch (res_1) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } rup = repo.UpdateRef(Constants.HEAD); res_1 = rup.Link(headName); switch (res_1) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } } FileUtils.Delete(rebaseDir, FileUtils.RECURSIVE); if (lastStepWasForward) { return(new RebaseResult(RebaseResult.Status.FAST_FORWARD)); } return(new RebaseResult(RebaseResult.Status.OK)); } return(new RebaseResult(RebaseResult.Status.UP_TO_DATE)); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } }
/// <exception cref="NGit.Api.Errors.RefAlreadyExistsException"> /// when trying to create (without force) a branch with a name /// that already exists /// </exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException">if the start point or branch can not be found /// </exception> /// <exception cref="NGit.Api.Errors.InvalidRefNameException"> /// if the provided name is <code>null</code> or otherwise /// invalid /// </exception> /// <returns>the newly created branch</returns> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> public override Ref Call() { CheckCallable(); ProcessOptions(); try { if (createBranch) { Git git = new Git(repo); CreateBranchCommand command = git.BranchCreate(); command.SetName(name); command.SetStartPoint(GetStartPoint().Name); if (upstreamMode != null) { command.SetUpstreamMode(upstreamMode); } command.Call(); } Ref headRef = repo.GetRef(Constants.HEAD); string refLogMessage = "checkout: moving from " + headRef.GetTarget().GetName(); ObjectId branch = repo.Resolve(name); if (branch == null) { throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved , name)); } RevWalk revWalk = new RevWalk(repo); AnyObjectId headId = headRef.GetObjectId(); RevCommit headCommit = headId == null ? null : revWalk.ParseCommit(headId); RevCommit newCommit = revWalk.ParseCommit(branch); RevTree headTree = headCommit == null ? null : headCommit.Tree; DirCacheCheckout dco = new DirCacheCheckout(repo, headTree, repo.LockDirCache(), newCommit.Tree); dco.SetFailOnConflict(true); try { dco.Checkout(); } catch (NGit.Errors.CheckoutConflictException e) { status = new CheckoutResult(CheckoutResult.Status.CONFLICTS, dco.GetConflicts()); throw; } Ref @ref = repo.GetRef(name); if (@ref != null && [email protected]().StartsWith(Constants.R_HEADS)) { @ref = null; } RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD, @ref == null); refUpdate.SetForceUpdate(force); refUpdate.SetRefLogMessage(refLogMessage + " to " + newCommit.GetName(), false); RefUpdate.Result updateResult; if (@ref != null) { updateResult = refUpdate.Link(@ref.GetName()); } else { refUpdate.SetNewObjectId(newCommit); updateResult = refUpdate.ForceUpdate(); } SetCallable(false); bool ok = false; switch (updateResult) { case RefUpdate.Result.NEW: { ok = true; break; } case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: { ok = true; break; } default: { break; break; } } if (!ok) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().checkoutUnexpectedResult , updateResult.ToString())); } if (!dco.GetToBeDeleted().IsEmpty()) { status = new CheckoutResult(CheckoutResult.Status.NONDELETED, dco.GetToBeDeleted( )); } else { status = CheckoutResult.OK_RESULT; } return(@ref); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } finally { if (status == null) { status = CheckoutResult.ERROR_RESULT; } } }