internal static void Revert(FileInfo file) { using (var repository = new LibGit2Sharp.Repository(file.DirectoryName)) { repository.CheckoutPaths(repository.Head.Name, new[] { file.FullName }, ForceCheckoutOptions); } }
public override bool Revert(string path, ref string error) { try { using (var repo = new LibGit2Sharp.Repository(RepositoryRootFolder)) { if (Path.GetFullPath(new Uri(RepositoryRootFolder).LocalPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToUpperInvariant() == Path.GetFullPath(new Uri(path).LocalPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToUpperInvariant()) { //undo all changes repo.Reset(ResetMode.Hard); } else { //undo specific changes string committishOrBranchSpec = "master"; CheckoutOptions checkoutOptions = new CheckoutOptions(); checkoutOptions.CheckoutModifiers = CheckoutModifiers.Force; checkoutOptions.CheckoutNotifyFlags = CheckoutNotifyFlags.Ignored; repo.CheckoutPaths(committishOrBranchSpec, new[] { path }, checkoutOptions); } } } catch (Exception e) { error = e.Message + Environment.NewLine + e.InnerException; return(false); } return(true); }
public static void Revert(FileInfo file) { using (var repository = new LibGit2Sharp.Repository(file.DirectoryName)) { repository.CheckoutPaths("master", new[] { file.FullName }, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force }); Log.Add($"Revert: {file.Name}"); } }
/// <summary> /// Discards local changes to a specific file and the file is updated with latest remote commit (origin/master) /// by checking out the specific file. /// </summary> /// <param name="org">Unique identifier of the organisation responsible for the app.</param> /// <param name="repository">The name of the repository</param> /// <param name="fileName">the name of the file</param> public void CheckoutLatestCommitForSpecificFile(string org, string repository, string fileName) { string localServiceRepoFolder = _settings.GetServicePath(org, repository, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext)); using (LibGit2Sharp.Repository repo = new LibGit2Sharp.Repository(localServiceRepoFolder)) { CheckoutOptions checkoutOptions = new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force, }; repo.CheckoutPaths("origin/master", new[] { fileName }, checkoutOptions); } }
public override void Undo(string filePath) { try { _repo.CheckoutPaths(this.CurrentBranch, new List <string> { filePath }); base.Undo(filePath); } catch (LibGit2SharpException ex) { throw new SourceControlException("Undo failed.", ex); } }
public override void Undo(string filePath) { try { var tip = _repo.Branches.First(b => !b.IsRemote && b.IsCurrentRepositoryHead).Tip; var options = new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force }; _repo.CheckoutPaths(tip.Sha, new List <string> { filePath }, options); base.Undo(filePath); } catch (LibGit2SharpException ex) { throw new SourceControlException(SourceControlText.GitUndoFailed, ex); } }
public override bool Revert(string path, ref string error) { try { using (var repo = new LibGit2Sharp.Repository(RepositoryRootFolder)) { string committishOrBranchSpec = "master"; CheckoutOptions checkoutOptions = new CheckoutOptions(); checkoutOptions.CheckoutModifiers = CheckoutModifiers.Force; checkoutOptions.CheckoutNotifyFlags = CheckoutNotifyFlags.Ignored; repo.CheckoutPaths(committishOrBranchSpec, new[] { path }, checkoutOptions); } } catch (Exception e) { error = e.Message + Environment.NewLine + e.InnerException; return(false); } return(true); }
/// <summary> /// Synchronizes a folder /// </summary> /// <param name="repoPath">Main repository path</param> /// <param name="folderPath">The folder to synchronize (checkout). Could be the model path or the layer path</param> /// <param name="forceCheckout">Forces the update from the latest commit (head tip)</param> /// <returns>A SysVersionControlItem with all the files that have been affected</returns> public static SysVersionControlTmpItem FolderSync(string repoPath, string folderPath, bool forceCheckout) { SysVersionControlTmpItem tmpItem = new SysVersionControlTmpItem(); CheckoutOptions checkoutOptions = new CheckoutOptions { CheckoutModifiers = forceCheckout ? CheckoutModifiers.Force : CheckoutModifiers.None }; string tipSha; string folderName = folderPath.Split('\\').Last(); using (Repository repo = new Repository(repoPath)) { repo.CheckoutPaths(repo.Head.Tip.Id.Sha, new[] { folderPath }, checkoutOptions); tipSha = repo.Head.Tip.Id.Sha; InitTmpItemFromTree(repoPath, repo.Head.Tip.Tree[folderName].Target as Tree, ref tmpItem); } return tmpItem; }
/// <summary> /// Resets the changes of a file to it's HEAD last commit /// </summary> /// <param name="repoPath">Repository main path</param> /// <param name="fileName">The file path</param> /// <returns>True if reset was successful false if not</returns> public static bool FileUndoCheckout(string repoPath, string fileName, bool forceCheckout) { FileInfo fileInfo = new FileInfo(fileName); using (Repository repo = new Repository(repoPath)) { string indexPath = fileInfo.FullName.Replace(repo.Info.WorkingDirectory, string.Empty); CheckoutOptions checkoutOptions = new CheckoutOptions { CheckoutModifiers = forceCheckout ? CheckoutModifiers.Force : CheckoutModifiers.None }; var fileCommits = repo.Head.Commits.Where(c => c.Parents.Count() == 1 && c.Tree[indexPath] != null && (c.Parents.FirstOrDefault().Tree[indexPath] == null || c.Tree[indexPath].Target.Id != c.Parents.FirstOrDefault().Tree[indexPath].Target.Id) ); if (fileCommits.Any()) { var lastCommit = fileCommits.First(); repo.CheckoutPaths(lastCommit.Id.Sha, new[] { fileName }, checkoutOptions); } return true; } }
/// <summary> /// Get a single file version from the git repository /// </summary> /// <param name="repoPath">Repository main path</param> /// <param name="tmpItem">The temporary item table holding the sha commit</param> /// <returns>a temporary file path</returns> public static string FileGetVersion(string repoPath, string fileName, SysVersionControlTmpItem tmpItem) { string indexPath = tmpItem.InternalFilename.Replace(repoPath, string.Empty); CheckoutOptions options = new CheckoutOptions(); options.CheckoutModifiers = CheckoutModifiers.Force; using (Repository repo = new Repository(repoPath)) { var commit = repo.Lookup<Commit>(tmpItem.GTXSha); if (commit != null) { try { repo.CheckoutPaths(commit.Id.Sha, new[] { fileName }, options); } catch (MergeConflictException ex) { //should not reach here as we're forcing checkout throw ex; } } } return fileName; }
public static string Update(string url, Log log, string directory, string repoDirectory = null) { if (string.IsNullOrWhiteSpace(url)) { Utility.Log(LogStatus.Skipped, "Updater", string.Format("No Url specified - {0}", url), log); } else { try { var dir = Path.Combine(directory, url.GetHashCode().ToString("X"), "trunk"); if (Repository.IsValid(dir)) { using (var repo = new Repository(dir)) { repo.Config.Set("user.name", Config.Instance.Username); repo.Config.Set("user.email", Config.Instance.Username + "@joduska.me"); repo.Fetch("origin"); if (repoDirectory != null) { repo.CheckoutPaths("origin/master", new List<String>() { repoDirectory }, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force }); } else { repo.Checkout("origin/master", new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force }); } } } else { var oldPath = Path.Combine(directory, url.GetHashCode().ToString("X")); if (Directory.Exists(oldPath)) { Directory.Delete(oldPath, true); } Repository.Clone(url, dir, new CloneOptions { Checkout = true }); using (var repo = new Repository(dir)) { repo.Config.Set("user.name", Config.Instance.Username); repo.Config.Set("user.email", Config.Instance.Username + "@joduska.me"); } } return dir; } catch (Exception ex) { Utility.Log(LogStatus.Error, "Updater", string.Format("{0} - {1}", ex.Message, url), log); } } return string.Empty; }