Ejemplo n.º 1
0
 internal static void Revert(FileInfo file)
 {
     using (var repository = new LibGit2Sharp.Repository(file.DirectoryName))
     {
         repository.CheckoutPaths(repository.Head.Name, new[] { file.FullName }, ForceCheckoutOptions);
     }
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 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}");
     }
 }
Ejemplo n.º 4
0
        /// <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);
            }
        }
Ejemplo n.º 5
0
 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);
     }
 }
Ejemplo n.º 6
0
 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);
     }
 }
Ejemplo n.º 7
0
 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);
 }
Ejemplo n.º 8
0
 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}");
     }
 }
Ejemplo n.º 9
0
        /// <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;
        }
Ejemplo n.º 10
0
        /// <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;
            }
        }
Ejemplo n.º 11
0
        /// <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;
        }
Ejemplo n.º 12
-1
        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;
        }