Update() public method

Updates this ref by forwarding it to the given object.
public Update ( AbstractObject reference ) : void
reference AbstractObject The ref this object shall reference.
return void
Example #1
0
 /// <summary>
 /// Create a new branch based on HEAD.
 /// </summary>
 /// <param name="repo"></param>
 /// <param name="name">The name of the branch to create (i.e. "master", not "refs/heads/master")</param>
 /// <returns>returns the newly created Branch object</returns>
 public static Branch Create(Repository repo, string name)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentException("Branch name must not be null or empty");
     }
     Ref.Update(GetRefName(name), repo.Head.CurrentCommit);
     return(new Branch(repo, name));
 }
Example #2
0
        private void ResetMixed(Commit commit)
        {
            if (commit.Tree == null || commit.Tree.InternalTree == null)
            {
                throw new InvalidOperationException("The given commit '" + commit.Hash + "'contains no valid tree.");
            }
            var index = _repo.Index.GitIndex;

            index.ReadTree(commit.Tree.InternalTree);
            index.write();
            Ref.Update("HEAD", commit);
        }
Example #3
0
 /// <summary>
 /// Create a new branch basing on the given commit
 /// </summary>
 /// <param name="repo"></param>
 /// <param name="name">The name of the branch to create (i.e. "master", not "refs/heads/master")</param>
 /// <param name="commit">The commit to base the branch on.</param>
 /// <returns>returns the newly created Branch object</returns>
 public static Branch Create(Repository repo, string name, Commit commit)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentException("Branch name must not be null or empty", "name");
     }
     if (commit == null || !commit.IsCommit)
     {
         throw new ArgumentException("Invalid commit", "commit");
     }
     Ref.Update(GetRefName(name), commit);
     return(new Branch(repo, name));
 }
Example #4
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);
        }
Example #5
0
 private void ResetHard(Commit commit)
 {
     commit.Checkout();
     _repo._internal_repo.Index.write();
     Ref.Update("HEAD", commit);
 }
Example #6
0
 private static void ResetSoft(Commit commit)
 {
     Ref.Update("HEAD", commit);
 }
Example #7
0
        /// <summary>
        /// Commit changes in the specified files and update HEAD
        /// </summary>
        /// <param name='message'>The commit message</param>
        /// <param name="author">The author of the content to be committed</param>
        /// <param name="paths">List of files to commit</param>
        /// <returns>Returns the newly created commit</returns>
        public Commit Commit(string message, Author author, params string[] paths)
        {
            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");
            }

            int basePathLength = Path.GetFullPath(WorkingDirectory).TrimEnd('/', '\\').Length;

            // Expand directory paths into file paths. Convert paths to full paths.
            List <string> filePaths = new List <string>();

            foreach (var path in paths)
            {
                string fullPath = path;
                if (!Path.IsPathRooted(fullPath))
                {
                    fullPath = Path.Combine(WorkingDirectory, fullPath);
                }
                fullPath = Path.GetFullPath(fullPath).TrimEnd('/', '\\');
                DirectoryInfo dir = new DirectoryInfo(fullPath);
                if (dir.Exists)
                {
                    filePaths.AddRange(GetDirectoryFiles(dir));
                }
                else
                {
                    filePaths.Add(fullPath);
                }
            }

            // Read the tree of the last commit. We are going to update it.
            GitSharp.Core.Tree tree = _internal_repo.MapTree(CurrentBranch.CurrentCommit._id);

            // Keep a list of trees that have been modified, since they have to be written.
            HashSet <GitSharp.Core.Tree> modifiedTrees = new HashSet <GitSharp.Core.Tree>();

            // Update the tree
            foreach (string fullPath in filePaths)
            {
                string    relPath   = fullPath.Substring(basePathLength + 1).Replace('\\', '/');
                TreeEntry treeEntry = tree.FindBlobMember(relPath);

                if (File.Exists(fullPath))
                {
                    // Looks like an old directory is now a file. Delete the subtree and create a new entry for the file.
                    if (treeEntry != null && !(treeEntry is FileTreeEntry))
                    {
                        treeEntry.Delete();
                    }

                    FileTreeEntry fileEntry  = treeEntry as FileTreeEntry;
                    var           writer     = new ObjectWriter(_internal_repo);
                    bool          executable = GitSharp.Core.Util.FS.canExecute(new FileInfo(fullPath));
                    ObjectId      id         = writer.WriteBlob(new FileInfo(fullPath));
                    if (fileEntry == null)
                    {
                        // It's a new file. Add it.
                        fileEntry = (FileTreeEntry)tree.AddFile(relPath);
                        treeEntry = fileEntry;
                    }
                    else if (fileEntry.Id == id && executable == fileEntry.IsExecutable)
                    {
                        // Same file, ignore it
                        continue;
                    }

                    fileEntry.Id = id;
                    fileEntry.SetExecutable(executable);
                }
                else
                {
                    // Deleted file or directory. Remove from the tree
                    if (treeEntry != null)
                    {
                        GitSharp.Core.Tree ptree = treeEntry.Parent;
                        treeEntry.Delete();
                        // Remove the subtree if it's now empty
                        while (ptree != null && ptree.MemberCount == 0)
                        {
                            GitSharp.Core.Tree nextParent = ptree.Parent;
                            ptree.Delete();
                            ptree = nextParent;
                        }
                    }
                    else
                    {
                        continue; // Already deleted.
                    }
                }
                modifiedTrees.Add(treeEntry.Parent);
            }

            // check if tree is different from current commit's tree
            if (modifiedTrees.Count == 0)
            {
                throw new InvalidOperationException("There are no changes to commit");
            }

            // Create new trees if there is any change
            ObjectId tree_id = SaveTree(tree, modifiedTrees);

            // Create the commit
            var parent = CurrentBranch.CurrentCommit;
            var commit = GitSharp.Commit.Create(message, parent, new Tree(this, tree_id), author);

            Ref.Update("HEAD", commit);

            // Unstage updated files
            Index.Unstage(paths);

            return(commit);
        }