Esempio n. 1
0
        public Stash Create(string message)
        {
            var    parent = _repo.CurrentBranch.CurrentCommit;
            Author author = new Author(_repo.Config["user.name"] ?? "unknown", _repo.Config["user.email"] ?? "unknown@(none).");

            if (message == null)
            {
                // Use the commit summary as message
                message = parent.ShortHash + " " + parent.Message;
                int i = message.IndexOfAny(new char[] { '\r', '\n' });
                if (i != -1)
                {
                    message = message.Substring(0, i);
                }
            }

            // Create the index tree commit
            GitIndex index = _repo.Index.GitIndex;

            index.RereadIfNecessary();
            var    tree_id     = index.writeTree();
            Tree   indexTree   = new Tree(_repo, tree_id);
            string commitMsg   = "index on " + _repo.CurrentBranch.Name + ": " + message;
            var    indexCommit = Commit.Create(commitMsg + "\n", parent, indexTree, author);

            // Create the working dir commit
            tree_id   = WriteWorkingDirectoryTree(parent.Tree.InternalTree, index);
            commitMsg = "WIP on " + _repo.CurrentBranch.Name + ": " + message;
            var wipCommit = Commit.Create(commitMsg + "\n", new Commit[] { parent, indexCommit }, new Tree(_repo, tree_id), author, author, DateTimeOffset.Now);

            string   prevCommit = null;
            FileInfo sf         = StashRefFile;

            if (sf.Exists)
            {
                prevCommit = File.ReadAllText(sf.FullName);
            }

            Stash s = new Stash(prevCommit, wipCommit.Hash, author, commitMsg);

            FileInfo stashLog = StashLogFile;

            File.AppendAllText(stashLog.FullName, s.FullLine + "\n");
            File.WriteAllText(sf.FullName, s.CommitId + "\n");

            // Wipe all local changes
            _repo.CurrentBranch.Reset(ResetBehavior.Hard);

            s.StashCollection = this;
            return(s);
        }
Esempio n. 2
0
        public Commit CommitChanges(string message, Author author)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("Commit message must not be null or empty!", "message");
            }
            if (string.IsNullOrEmpty(author.Name))
            {
                throw new ArgumentException("Author name must not be null or empty!", "author");
            }
            GitIndex.RereadIfNecessary();
            var tree_id = GitIndex.writeTree();
            // check if tree is different from current commit's tree
            var parent = _repo.CurrentBranch.CurrentCommit;

            if ((parent == null && GitIndex.Members.Count == 0) || (parent != null && parent.Tree._id == tree_id))
            {
                throw new InvalidOperationException("There are no changes to commit");
            }
            var commit = Commit.Create(message, parent, new Tree(_repo, tree_id), author);

            Ref.Update("HEAD", commit);
            return(commit);
        }