public static void Init(this Flow gitFlow, GitFlowRepoSettings settings, Signature author) { var repo = gitFlow.Repository; //Only init if it is not initialized if (gitFlow.IsInitialized()) //TODO: Does Init do anything if already initialized? Is it sufficient to just check the ConfigValues? Should it check branch existance as well? return; if (gitFlow.IsEmptyRepository()) { Log("New Repository Detected - Creating Master Branch"); //TODO: Decide if Init should create necesarry branches too? //Create Master branch Signature committer = author; var opts = new CommitOptions {AllowEmptyCommit = true}; repo.Commit("Initial commit", author, committer, opts); //Now make Dev } SetupMasterBranch(repo, settings.Settings[GitFlowSetting.Master], author); SetupDevBranch(repo, settings.Settings[GitFlowSetting.Develop], settings.Settings[GitFlowSetting.Master], author); //TODO.. Repo has branches settings.Settings .ToList() .ForEach(item => repo.Config.Set(GetConfigKey(item.Key), item.Value, settings.ConfigurationLocation) ); Log("Init Complete"); }
private void CommitCore(string message) { using var repo = new Git.Repository(Location); var statusOptions = new Git.StatusOptions { DetectRenamesInIndex = true, DetectRenamesInWorkDir = true, IncludeIgnored = false, IncludeUntracked = true, RecurseUntrackedDirs = true, RecurseIgnoredDirs = false }; if (!repo.RetrieveStatus(statusOptions).IsDirty) { return; } Git.Commands.Stage(repo, "*"); var author = new Git.Signature("JournalCli", "@journalCli", DateTime.Now); var committer = author; var options = new Git.CommitOptions { PrettifyMessage = true }; var commit = repo.Commit(message, author, committer, options); }
private string Commit(Repository repository, string commitMessage) { var author = new LibGit2Sharp.Signature("GitUITests", "*****@*****.**", DateTimeOffset.Now); var committer = author; var options = new LibGit2Sharp.CommitOptions() { PrettifyMessage = false }; var commit = repository.Commit(commitMessage, author, committer, options); return(commit.Id.Sha); }
/// <summary> /// Inserts a <see cref="LibGit2Sharp.Commit" /> into the object database by applying a <see cref="TreeDefinition"/>. /// </summary> /// <param name="repository">The repository.</param> /// <param name="definition">The tree definition.</param> /// <param name="message">The message.</param> /// <param name="author">The author.</param> /// <param name="committer">The committer.</param> /// <param name="options">The options.</param> /// <param name="mergeParent">The parent commit for a merge.</param> /// <returns>The created <see cref="LibGit2Sharp.Commit" />.</returns> internal static Commit Commit(this IRepository repository, TreeDefinition definition, string message, Signature author, Signature committer, CommitOptions options = null, Commit mergeParent = null) { if (options == null) { options = new CommitOptions(); } var parents = RetrieveParentsOfTheCommitBeingCreated(repository, options.AmendPreviousCommit, mergeParent).ToList(); var tree = repository.ObjectDatabase.CreateTree(definition); var commit = repository.ObjectDatabase.CreateCommit(author, committer, message, tree, parents, false); var logMessage = commit.BuildCommitLogMessage(options.AmendPreviousCommit, repository.Info.IsHeadUnborn, parents.Count > 1); UpdateHeadAndTerminalReference(repository, commit, logMessage); return(commit); }
public ReferenceRepository(GitModuleTestHelper moduleTestHelper) { _moduleTestHelper = moduleTestHelper; using (var repository = new LibGit2Sharp.Repository(_moduleTestHelper.Module.WorkingDir)) { _moduleTestHelper.CreateRepoFile("A.txt", "A"); repository.Index.Add("A.txt"); var message = "A commit message"; var author = new LibGit2Sharp.Signature("GitUITests", "*****@*****.**", DateTimeOffset.Now); var committer = author; var options = new LibGit2Sharp.CommitOptions(); var commit = repository.Commit(message, author, committer, options); CommitHash = commit.Id.Sha; } }
private void ValidateGitRepo() { if (Git.Repository.IsValid(Location)) { return; } Git.Repository.Init(Location); using var repo = new Git.Repository(Location); Git.Commands.Stage(repo, "*"); var author = new Git.Signature("JournalCli", "@journalCli", DateTime.Now); var committer = author; var options = new Git.CommitOptions { PrettifyMessage = true }; var commit = repo.Commit("Initial commit", author, committer, options); }
/// <summary> /// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository. /// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit. /// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it. /// <para>The Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para> /// </summary> /// <param name="repository">The <see cref="Repository"/> being worked with.</param> /// <param name="author">The <see cref="Signature"/> of who made the change.</param> /// <param name="message">The description of why a change was made to the repository.</param> /// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param> /// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns> public static Commit Commit(this IRepository repository, string message, Signature author, CommitOptions options = null) { Signature committer = repository.Config.BuildSignature(DateTimeOffset.Now, true); return(repository.Commit(message, author, committer, options)); }
public GitActionResult<string> Commit(string message, bool amend = false, bool signoff = false) { var result = new GitActionResult<string>(); using (var repository = GetRepository()) { if (string.IsNullOrEmpty(message)) { result.Succeeded = false; result.ErrorMessage = "Commit message must not be null or empty!"; //throw new ArgumentException("Commit message must not be null or empty!", "message"); } else { Signature author = repository.Config.BuildSignature(DateTimeOffset.Now); Signature committer = author; CommitOptions opts = new CommitOptions(); opts.AmendPreviousCommit = amend; var commit = repository.Commit(message, author, committer, opts); result.Succeeded = true; result.Item = commit.Sha; } return result; } }
public static Commit Commit(this IRepository repository, string message, CommitOptions options) { Signature author = repository.Config.BuildSignatureOrThrow(DateTimeOffset.Now); return(repository.Commit(message, author, options)); }
public MergeResult StalkUpTo(IReadOnlyList<Commit> commits) { var mergeFiles = new List<String> (); var startingCommit = head.Tip; var newCommits = new List<Commit> (); MergeResult mergeResult = null; var mergeOptions = new MergeOptions (); var signature = new Signature (userName, userEmail, DateTime.Now); //var checkoutOptions = new CheckoutOptions (); // var commitMsg = new List<String> (); commitMsg = (String.Format ("Automated Stalker (StalkUpTo) merge commit from: '{0}' to: '{1}' into {2}", commits.First ().Sha.Substring (0, 6), commits.Last ().Sha.Substring (0, 6), head.Tip.Sha.Substring (0, 6))); // Console.WriteLine ("Working on {0} commits", commits.Count); var commit = commits.Last (); Array.ForEach (CommitLog (commit, true).ToArray(), Console.WriteLine); mergeOptions.FastForwardStrategy = FastForwardStrategy.FastForwardOnly; mergeOptions.CommitOnSuccess = true; //checkoutOptions.CheckoutModifiers = CheckoutModifiers.None; try { Console.WriteLine ("Merge FF: {0} : {1}", commit.Sha.Substring (0,6), commit.MessageShort); mergeResult = repo.Merge (commit, signature, mergeOptions); var tmpMsg = mergeResult.Commit.Message; var commitOptions = new CommitOptions (); commitOptions.AmendPreviousCommit = true; repo.Commit (commitMsg + "\n\n" + tmpMsg, signature, commitOptions); } catch { Console.WriteLine ("Merge FF Failed: CheckoutFileConflictStrategy.Theirs"); //commitMsg = CommitLog (commit, true, commitMsg); // this will happen as libgit2sharp throws ex if fast forward not possible // so lets get a commit message ready mergeFiles.AddRange (FilesToMerge (commit)); mergeOptions.FastForwardStrategy = FastForwardStrategy.NoFastForward; mergeOptions.FileConflictStrategy = CheckoutFileConflictStrategy.Theirs; mergeOptions.CommitOnSuccess = true; try { var fgColor = Console.ForegroundColor; var bgColor = Console.BackgroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Merging commit : {0}", commit.Sha); Console.WriteLine (" - Info : {0} / {1} / {2}", commit.Author.When, commit.Author.Name, commit.Author.Email); Console.WriteLine (" - Msg : {0}", commit.Message); Console.ForegroundColor = fgColor; Console.BackgroundColor = bgColor; mergeResult = repo.Merge (commit, signature, mergeOptions); var tmpMsg = mergeResult.Commit.Message; var commitOptions = new CommitOptions (); commitOptions.AmendPreviousCommit = true; repo.Commit (commitMsg + "\n\n" + tmpMsg, signature, commitOptions); //Array.ForEach (MergeResultLog (mergeResult).ToArray (), Console.WriteLine); } catch (Exception e) { Console.WriteLine (e.Message); } } return mergeResult; }
/// <summary> /// Commits all staged files. /// </summary> /// <param name="repositoryPath"></param> /// <param name="message"></param> /// <param name="allowEmptyCommit"></param> /// <returns>CommandStatus</returns> private ECommandStatus Commit(string repositoryPath, string message, bool allowEmptyCommit) { try { using (var repo = new Repository(repositoryPath)) { Signature author = new Signature(Config.Username, Config.Email, DateTime.Now); Signature commiter = author; CommitOptions commitOptions = new CommitOptions(); commitOptions.AllowEmptyCommit = allowEmptyCommit; repo.Stage("*"); Commit commit = repo.Commit(message, author, commiter, commitOptions); return ECommandStatus.OK; } } catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return ECommandStatus.FAIL; } }
public string Commit(string message, bool amend = false, bool signoff = false) { using (var repository = GetRepository()) { if (string.IsNullOrEmpty(message)) { throw new ArgumentException("Commit message must not be null or empty!", "message"); } Signature author = repository.Config.BuildSignature(DateTimeOffset.Now); Signature committer = author; CommitOptions opts = new CommitOptions(); opts.AmendPreviousCommit = amend; var commit = repository.Commit(message, author, committer); return commit.Sha; } }
public void Init_FlowWithExistingBranches(string masterBranch, string devBranch) { using (_testRepository = new Repository(_testPath)) { var settings = new GitFlowRepoSettings(); Signature committer = author; var opts = new CommitOptions { AllowEmptyCommit = true }; _testRepository.Commit("Initial commit", author, committer, opts); TestHelpers.CreateLocalTestBranch(_testRepository, masterBranch, GitFlowSetting.Master, settings); TestHelpers.CreateLocalTestBranch(_testRepository, devBranch, GitFlowSetting.Develop, settings); _testRepository.Flow().Init(settings, author); Assert.NotNull(_testRepository.Branches.FirstOrDefault( x => string.Equals(x.FriendlyName, settings.GetSetting(GitFlowSetting.Master), StringComparison.OrdinalIgnoreCase))); Assert.NotNull(_testRepository.Branches.FirstOrDefault( x => string.Equals(x.FriendlyName, settings.GetSetting(GitFlowSetting.Develop), StringComparison.OrdinalIgnoreCase))); } }
internal static Commit CommitChanges(this IRepository repository, ObjectRepositoryChangeCollection changes, IObjectRepositorySerializer serializer, string message, Signature author, Signature committer, GitHooks hooks, CommitOptions options = null, Commit mergeParent = null) { TreeDefinition definition; Commit startCommit; if (changes.OldRepository?.CommitId != null) { if (repository.Head.Tip.Id != changes.OldRepository.CommitId) { throw new GitObjectDbException("Changes are not based on the HEAD commit."); } startCommit = repository.Lookup <Commit>(changes.OldRepository.CommitId); definition = TreeDefinition.From(startCommit); } else if (repository.Info.IsHeadUnborn) { startCommit = null; definition = new TreeDefinition(); } else { throw new GitObjectDbException("Changes are not based on the HEAD commit."); } if (!hooks.OnCommitStarted(changes, message)) { return(null); } repository.UpdateTreeDefinition(changes, definition, serializer, startCommit); var result = Commit(repository, definition, message, author, committer, options, mergeParent); hooks.OnCommitCompleted(changes, message, result.Id); return(result); }
private void GitSendChanges(string cloneUrl, string localPath, string branch) { //const string cloneUrl = "http://*****:*****@"%TMP%\NtlmGitTest"); // ensure that local folder does not exist if (Directory.Exists(localPath)) //NtlmGitTest.Program.DirectoryDeleteAll(localPath); { using (new NtlmGitSession()) { var repo = new Repository(localPath); Remote remote = repo.Network.Remotes["origin"]; //string content = "Test libgit2sharp commit!"; string filePath = Path.Combine(localPath, "config.xml"); // Committer and author Signature committer = new Signature("Sergey Zhurbenko", "*****@*****.**", DateTime.Now); Signature author = committer; // Create binary stream from the text CommitOptions commitOptions = new CommitOptions() { AmendPreviousCommit = false, AllowEmptyCommit = false }; //File.WriteAllText(filePath, "ABCDEFGH"); repo.Index.Stage("config.xml"); try { Commit commit = repo.Commit("Checkin commit", author, commitOptions); } catch (Exception ex) { Label2.Text = ex.Message; } repo.Network.Push(remote, @"refs/heads/" + branch); } } else { using (new NtlmGitSession()) { var err = Repository.Clone(cloneUrl, localPath); var repo = new Repository(localPath); Label1.Text = err; Remote remote = repo.Network.Remotes["origin"]; //string content = "Test libgit2sharp commit!"; string filePath = Path.Combine(localPath, "config.xml"); // Committer and author Signature committer = new Signature("Sergey Zhurbenko", "*****@*****.**", DateTime.Now); Signature author = committer; // Create binary stream from the text CommitOptions commitOptions = new CommitOptions() { AmendPreviousCommit = false, AllowEmptyCommit = false }; repo.Index.Stage("config.xml"); Commit commit = repo.Commit("Checkin commit", author, commitOptions); repo.Network.Push(remote, @"refs/heads/" + branch); Commit commit_lbl = repo.Lookup<Commit>("4cf4f4"); Label2.Text = commit_lbl.MessageShort; } } }
/// <summary> /// Inserts a <see cref="LibGit2Sharp.Commit" /> into the object database by applying a <see cref="TreeDefinition"/>. /// </summary> /// <param name="repository">The repository.</param> /// <param name="changes">The changes.</param> /// <param name="message">The message.</param> /// <param name="author">The author.</param> /// <param name="committer">The committer.</param> /// <param name="hooks">The hooks.</param> /// <param name="options">The options.</param> /// <param name="mergeParent">The parent commit for a merge.</param> /// <returns>The created <see cref="LibGit2Sharp.Commit" />.</returns> internal static Commit CommitChanges(this IRepository repository, MetadataTreeChanges changes, string message, Signature author, Signature committer, GitHooks hooks, CommitOptions options = null, Commit mergeParent = null) { TreeDefinition definition; if (changes.OldRepository?.CommitId != null) { if (repository.Head.Tip.Id != changes.OldRepository.CommitId) { throw new NotSupportedException("Changes are not based on the HEAD commit."); } var startCommit = repository.Lookup <Commit>(changes.OldRepository.CommitId); definition = TreeDefinition.From(startCommit); } else if (repository.Info.IsHeadUnborn) { definition = new TreeDefinition(); } else { throw new NotSupportedException("Changes are not based on the HEAD commit."); } if (!hooks.OnCommitStarted(changes, message)) { return(null); } changes.UpdateTreeDefinition(repository, definition); var result = Commit(repository, definition, message, author, committer, options, mergeParent); hooks.OnCommitCompleted(changes, message, result.Id); return(result); }
public Commit Commit(string message, Signature author, Signature committer, CommitOptions options = null) { throw new NotImplementedException(); }
public Commit StalkerCommit() { var signature = new Signature (userName, userEmail, DateTime.Now); var options = new CommitOptions (); options.AllowEmptyCommit = false; options.AmendPreviousCommit = false; var com = repo.Commit (commitMsg, signature, options); return com; }
/// <summary> /// Permite hacer un guardado /// </summary> /// <param name="name"></param> /// <param name="email"></param> /// <param name="message"></param> public void git_commit(string name, string email, string message) { //feed.Logs.WriteText("Commit", "Se hace commit normal de " + this.NombreProyecto); //repo.Index.Remove(estado.FilePath,false); Commit newC = null; RepositoryStatus status = getStatus(); if (status != null) { ExplicitPathsOptions explicitOptions = new ExplicitPathsOptions(); if (status.Modified.Count() > 0) { foreach (var archivo in status.Modified) { if (extensionesPermitidas(archivo.FilePath, null)) { Repo.Index.Stage(archivo.FilePath); } } } } CommitOptions options = new CommitOptions(); Signature signature = new Signature(name, email, DateTimeOffset.Now); try { options.AmendPreviousCommit = false; newC = Repo.Commit(message, signature, signature, options); // /*Deprecated*/ newC = Repo.Commit(message, signature, signature, false); // newC.Parents } catch (Exception ex) { options.AmendPreviousCommit = true; newC = Repo.Commit(message, signature, signature, options); //newC = Repo.Commit(message, signature, signature, true); } }
public GitBackupStatus InPlaceGitBackup(string repositoryFolder) { var isValidGitRepository = LibGit2Sharp.Repository.IsValid(repositoryFolder); var createdRepository = false; if (!isValidGitRepository) { _logger.InfoFormat("Initializing git repository in folder '{0}'...", repositoryFolder); repositoryFolder = LibGit2Sharp.Repository.Init(repositoryFolder, false); createdRepository = true; } var numberOfFilesAdded = 0; var numberOfFilesChanged = 0; var numberOfFilesRemoved = 0; using (var repository = new LibGit2Sharp.Repository(repositoryFolder)) { var options = new StatusOptions(); var status = repository.RetrieveStatus(options); _logger.Debug("Repository Status: " + JsonConvert.SerializeObject(status)); if (status.IsDirty) { var untractedFiles = status.Untracked; foreach (var untrackedFile in untractedFiles) { _logger.Info("Added: " + untrackedFile.FilePath); var stageOptions = new StageOptions(); repository.Stage(untrackedFile.FilePath, stageOptions); numberOfFilesAdded++; } var modifiedFiles = status.Modified; foreach (var modifiedFile in modifiedFiles) { _logger.Info("Modified: " + modifiedFile.FilePath); var stageOptions = new StageOptions(); repository.Stage(modifiedFile.FilePath, stageOptions); numberOfFilesChanged++; } var removedFiles = status.Missing; foreach (var removedFile in removedFiles) { _logger.Info("Removed: " + removedFile.FilePath); var stageOptions = new StageOptions(); repository.Stage(removedFile.FilePath, stageOptions); numberOfFilesRemoved++; } var author = new Signature(Environment.UserName, "*****@*****.**", System.DateTimeOffset.Now); var committer = new Signature(Environment.UserName, "*****@*****.**", System.DateTimeOffset.Now); var commitOptions = new CommitOptions(); _logger.Info("Commiting..."); repository.Commit(DateTime.Now.ToString(CultureInfo.InvariantCulture), author, committer, commitOptions); } } var gitBackupStatus = new GitBackupStatus(createdRepository, numberOfFilesAdded, numberOfFilesChanged, numberOfFilesRemoved); return gitBackupStatus; }