Ref is a named symbolic reference that is a pointing to a specific git object. It is not resolved until you explicitly retrieve the link target. The Target is not cached.
Inheritance: IReferenceObject
Exemplo n.º 1
0
 public RefUpdate(RefDatabase refDb, Ref r, FileInfo f)
 {
     db = refDb;
     this._ref = r;
     this.OldObjectId = r.ObjectId;
     looseFile = f;
     this.Result = RefUpdateResult.NotAttempted;
 }
Exemplo n.º 2
0
 public RefUpdate(RefDatabase refDb, Ref r, FileInfo f)
 {
     _db = refDb;
     _ref = r;
     OldObjectId = r.ObjectId;
     _looseFile = f;
     Result = RefUpdateResult.NotAttempted;
 }
Exemplo n.º 3
0
 ///	<summary>
 /// An set of update operations for renaming a ref
 ///	</summary>
 ///	<param name="fromRef"> Old ref name </param>
 ///	<param name="toRef"> New ref name </param>
 ///	<returns> a RefUpdate operation to rename a ref </returns>
 ///	<exception cref="IOException"> </exception>
 public RefRename NewRename(string fromRef, string toRef)
 {
     RefreshPackedRefs();
     Ref f = ReadRefBasic(fromRef, 0);
     var t = new Ref(Ref.Storage.New, toRef, null);
     var refUpdateFrom = new RefUpdate(this, f, FileForRef(f.Name));
     var refUpdateTo = new RefUpdate(this, t, FileForRef(t.Name));
     return new RefRename(refUpdateTo, refUpdateFrom);
 }
Exemplo n.º 4
0
 private void SelectRef(Ref r)
 {
     if (r == null)
         return;
     var obj = m_repository.OpenObject(r.ObjectId);
     if (obj.getType() == Constants.OBJ_COMMIT)
     {
         DisplayCommit(m_repository.MapCommit(r.ObjectId), "Commit history of " + r.Name);
         return;
     }
     else if (obj.getType() == Constants.OBJ_TAG)
     {
         var tag = m_repository.MapTag(r.Name, r.ObjectId);
         if (tag.TagId == r.ObjectId) // it sometimes happens to have self referencing tags
         {
             return;
         }
         var tagged_commit = m_repository.MapCommit(tag.TagId);
         DisplayCommit(tagged_commit, "Commit history of " + tag.TagName);
         return;
     }
     else if (obj.getType() == Constants.OBJ_TREE)
     {
         // hmm, display somehow
     }
     else if (obj.getType() == Constants.OBJ_BLOB)
     {
         // hmm, display somehow
     }
     else
     {
         Debug.Fail("don't know how to display this object: "+obj.ToString());
     }
 }
Exemplo n.º 5
0
        private void RefreshPackedRefs()
        {
            if (!_packedRefsFile.Exists)
                return;

            DateTime currTime = _packedRefsFile.LastWriteTime;
            long currLen = currTime == DateTime.MinValue ? 0 : _packedRefsFile.Length;
            if (currTime == packedRefsLastModified && currLen == packedRefsLength)
                return;
            if (currTime == DateTime.MinValue)
            {
                packedRefsLastModified = DateTime.MinValue;
                packedRefsLength = 0;
                packedRefs = new Dictionary<string, Ref>();
                return;
            }

            Dictionary<string, Ref> newPackedRefs = new Dictionary<string, Ref>();
            try
            {
                using (var b = OpenReader(_packedRefsFile))
                {
                    string p;
                    Ref last = null;
                    while ((p = b.ReadLine()) != null)
                    {
                        if (p[0] == '#')
                            continue;

                        if (p[0] == '^')
                        {
                            if (last == null)
                                throw new IOException("Peeled line before ref.");

                            ObjectId id = ObjectId.FromString(p.Substring(1));
                            last = new Ref(Ref.Storage.Packed, last.Name, last.Name, last.ObjectId, id, true);
                            if (!newPackedRefs.ContainsKey(last.Name))
                                newPackedRefs[last.Name] = last; // [henon] <--- sometimes the same tag exits twice for some reason (i.e. a tag referencing itself) ... so we silently ignore the problem. hope this is the expected behavior
                            continue;
                        }

                        int sp = p.IndexOf(' ');
                        ObjectId id2 = ObjectId.FromString(p.Substring(0, sp));
                        string name = p.Substring(sp + 1);
                        last = new Ref(Ref.Storage.Packed, name, id2);
                        newPackedRefs.Add(last.Name, last);
                    }
                }
                packedRefsLastModified = currTime;
                packedRefsLength = currLen;
                packedRefs = newPackedRefs;
            }
            catch (FileNotFoundException)
            {
                // Ignore it and leave the new map empty.
                //
                packedRefsLastModified = DateTime.MinValue;
                packedRefsLength = 0;
                packedRefs = newPackedRefs;
            }
            catch (IOException e)
            {
                throw new GitException("Cannot read packed refs", e);
            }
        }
Exemplo n.º 6
0
        private Ref ReadRefBasic(String origName, string name, int depth)
        {
            // Prefer loose ref to packed ref as the loose
            // file can be more up-to-date than a packed one.
            //
            FileInfo loose = FileForRef(name);
            DateTime mtime = loose.LastWriteTime;
            Ref @ref = null;

            if (looseRefs.ContainsKey(name))
            {
                @ref = looseRefs[name];
                DateTime cachedlastModified = looseRefsMTime[name];
                if (cachedlastModified != null && cachedlastModified == mtime)
                {
                    if (packedRefs.ContainsKey(origName))
                        return new Ref(GitSharp.Ref.Storage.LoosePacked, origName, @ref.ObjectId, @ref.PeeledObjectId, @ref.Peeled);
                    else
                        return @ref;
                }
                looseRefs.Remove(origName);
                looseRefsMTime.Remove(origName);
            }

            if (!loose.Exists)
            {
                // File does not exist.
                // Try packed cache.
                //
                packedRefs.TryGetValue(name, out @ref);
                if (@ref != null)
                    if ([email protected](origName))
                        @ref = new Ref(GitSharp.Ref.Storage.LoosePacked, origName, name, @ref.ObjectId);
                return @ref;
            }

            String line = null;
            try
            {
                DateTime cachedlastModified = DateTime.MinValue;
                looseRefsMTime.TryGetValue(name, out cachedlastModified);
                if (cachedlastModified != null && cachedlastModified == mtime)
                {
                    looseSymRefs.TryGetValue(name, out line);
                }
                if (line == null)
                {
                    line = ReadLine(loose);
                    looseRefsMTime[name] = mtime;
                    looseSymRefs[name] = line;
                }
            }
            catch (FileNotFoundException)
            {
                return packedRefs[name];
            }

            if (line == null || line.Length == 0)
            {
                looseRefs.Remove(origName);
                looseRefsMTime.Remove(origName);
                return new Ref(Ref.Storage.Loose, origName, name, null);
            }

            if (line.StartsWith("ref: "))
            {
                if (depth >= 5)
                    throw new IOException("Exceeded maximum ref depth of " + depth + " at " + name + ".  Circular reference?");

                string target = line.Substring("ref: ".Length);
                Ref r = ReadRefBasic(target, depth + 1);
                var cachedMtime = DateTime.MinValue;
                looseRefsMTime.TryGetValue(name, out cachedMtime);
                if (cachedMtime != null && cachedMtime != mtime)
                    setModified();
                looseRefsMTime[name]=mtime;
                if (r == null)
                    return new Ref(Ref.Storage.Loose, origName, target, null);
                if (!origName.Equals(r.Name))
                    r = new Ref(Ref.Storage.LoosePacked, origName, r.Name, r.ObjectId, r.PeeledObjectId, true);
                return r;
            }

            setModified();

            ObjectId id;
            try
            {
                id = ObjectId.FromString(line);
            }
            catch (ArgumentException)
            {
                throw new IOException("Not a ref: " + name + ": " + line);
            }

            GitSharp.Ref.Storage storage;
            if (packedRefs.ContainsKey(name))
                storage = Ref.Storage.LoosePacked;
            else
                storage = Ref.Storage.Loose;
            @ref = new Ref(storage, name, id);
            looseRefs[name]= @ref;
            looseRefsMTime[name]= mtime;

            if (!origName.Equals(name))
            {
                @ref = new Ref(Ref.Storage.Loose, origName, name, id);
                looseRefs[origName]= @ref;
            }

            return @ref;
        }
Exemplo n.º 7
0
 /// <summary>
 /// Updates this ref by linking it to the given ref's target.
 /// </summary>
 /// <param name="other">The ref this ref shall reference.</param>
 public void Update(Ref reference)
 {
     Update(reference.Target);
 }
Exemplo n.º 8
0
 public Branch(Ref @ref)
     : base(@ref._repo, @ref.Name)
 {
 }
Exemplo n.º 9
0
        private Ref ReadRefBasic(String origName, string name, int depth)
        {
            // Prefer loose ref to packed ref as the loose
            // file can be more up-to-date than a packed one.
            //
            Ref @ref;
            _looseRefs.TryGetValue(name, out @ref);
            FileInfo loose = FileForRef(name);
            loose.Refresh();
            DateTime mtime = loose.Exists ? loose.LastWriteTime : DateTime.MinValue;	// [ammachado] If the file does not exists, LastWriteTimes returns '1600-12-31 22:00:00'

            if (@ref != null)
            {
                DateTime cachedLastModified;
                if (_looseRefsMTime.TryGetValue(name, out cachedLastModified) && cachedLastModified == mtime)
                {
                    return _packedRefs.ContainsKey(origName) ?
                        new Ref(Ref.Storage.LoosePacked, origName, @ref.ObjectId, @ref.PeeledObjectId, @ref.Peeled)
                        : @ref;
                }

                _looseRefs.Remove(origName);
                _looseRefsMTime.Remove(origName);
            }

            if (!loose.Exists)
            {
                // File does not exist.
                // Try packed cache.
                //
                _packedRefs.TryGetValue(name, out @ref);
                if (@ref != null && [email protected](origName))
                {
                    @ref = new Ref(Ref.Storage.LoosePacked, origName, name, @ref.ObjectId);
                }
                return @ref;
            }

            string line = null;
            try
            {
                DateTime cachedLastModified;
                if (_looseRefsMTime.TryGetValue(name, out cachedLastModified) && cachedLastModified == mtime)
                {
                    _looseSymRefs.TryGetValue(name, out line);
                }

                if (string.IsNullOrEmpty(line))
                {
                    line = ReadLine(loose);
                    _looseRefsMTime[name] = mtime;
                    _looseSymRefs[name] = line;
                }
            }
            catch (FileNotFoundException)
            {
                return _packedRefs[name];
            }

            if (string.IsNullOrEmpty(line))
            {
                _looseRefs.Remove(origName);
                _looseRefsMTime.Remove(origName);
                return new Ref(Ref.Storage.Loose, origName, name, null);
            }

            if (line.StartsWith("ref: "))
            {
                if (depth >= 5)
                {
                    throw new IOException("Exceeded maximum ref depth of " + depth + " at " + name + ".  Circular reference?");
                }

                string target = line.Substring("ref: ".Length);
                Ref r = ReadRefBasic(target, depth + 1);
                DateTime cachedMtime;
                if (_looseRefsMTime.TryGetValue(name, out cachedMtime) && cachedMtime != mtime)
                {
                    SetModified();
                }
                _looseRefsMTime[name] = mtime;

                if (r == null)
                {
                    return new Ref(Ref.Storage.Loose, origName, target, null);
                }

                if (!origName.Equals(r.Name))
                {
                    r = new Ref(Ref.Storage.LoosePacked, origName, r.Name, r.ObjectId, r.PeeledObjectId, true);
                }

                return r;
            }

            SetModified();

            ObjectId id;
            try
            {
                id = ObjectId.FromString(line);
            }
            catch (ArgumentException)
            {
                throw new IOException("Not a ref: " + name + ": " + line);
            }

            Ref.Storage storage = _packedRefs.ContainsKey(name) ? Ref.Storage.LoosePacked : Ref.Storage.Loose;
            @ref = new Ref(storage, name, id);
            _looseRefs[name] = @ref;
            _looseRefsMTime[name] = mtime;

            if (!origName.Equals(name))
            {
                @ref = new Ref(Ref.Storage.Loose, origName, name, id);
                _looseRefs[origName] = @ref;
            }

            return @ref;
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
 public void ReloadRefs()
 {
     _refs = null;
     _tags = null;
     _branches = null;
     _remoteBranches = null;
     _head = null;
 }
Exemplo n.º 12
0
 public Branch(Ref @ref)
     : base(@ref._repo, @ref.Name)
 {
 }
Exemplo n.º 13
0
 private void ResetHard(Commit commit)
 {
     commit.Checkout(_repo.WorkingDirectory);
     _repo._internal_repo.Index.write();
     Ref.Update("HEAD", commit);
 }
Exemplo n.º 14
0
 private static void ResetSoft(Commit commit)
 {
     Ref.Update("HEAD", commit);
 }
Exemplo n.º 15
0
        /// <summary>
        /// Returns the object that this object points to if this is a commit.
        /// </summary>
        /// <param name="dref">The ref.</param>
        /// <returns></returns>
        internal Ref Peel(Ref dref)
        {
            if (dref.Peeled) return dref;

            ObjectId peeled = null;
            try
            {
                object target = Repository.MapObject(dref.ObjectId, dref.Name);

                while (target is Tag)
                {
                    var tag = (Tag)target;
                    peeled = tag.Id;

                    if (tag.TagType == Constants.TYPE_TAG)
                    {
                        target = Repository.MapObject(tag.Id, dref.Name);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (IOException)
            {
                // Ignore a read error.  Callers will also get the same error
                // if they try to use the result of getPeeledObjectId.
            }
            return new Ref(dref.StorageFormat, dref.Name, dref.ObjectId, peeled, true);
        }
Exemplo n.º 16
0
 internal void Stored(string origName, string name, ObjectId id, DateTime time)
 {
     lock (this)
     {
         _looseRefs[name] = new Ref(Ref.Storage.Loose, origName, name, id);
         _looseRefsMTime[name] = time;
         SetModified();
     }
     Repository.fireRefsMaybeChanged();
 }
Exemplo n.º 17
0
 /// <summary>
 /// Updates this ref by linking it to the given ref's target.
 /// </summary>
 /// <param name="reference">The ref this ref shall reference.</param>
 public void Update(Ref reference)
 {
     Update(reference.Target);
 }
Exemplo n.º 18
0
        private void RefreshPackedRefs()
        {
            _packedRefsFile.Refresh();
            if (!_packedRefsFile.Exists) return;

            DateTime currTime = _packedRefsFile.LastWriteTime;
            long currLen = currTime == DateTime.MinValue ? 0 : _packedRefsFile.Length;
            if (currTime == _packedRefsLastModified && currLen == _packedRefsLength) return;

            if (currTime == DateTime.MinValue)
            {
                _packedRefsLastModified = DateTime.MinValue;
                _packedRefsLength = 0;
                _packedRefs = new Dictionary<string, Ref>();
                return;
            }

            var newPackedRefs = new Dictionary<string, Ref>();
            try
            {
                using (var b = OpenReader(_packedRefsFile))
                {
                    string p;
                    Ref last = null;
                    while ((p = b.ReadLine()) != null)
                    {
                        if (p[0] == '#') continue;

                        if (p[0] == '^')
                        {
                            if (last == null)
                            {
                                throw new IOException("Peeled line before ref.");
                            }

                            ObjectId id = ObjectId.FromString(p.Substring(1));
                            last = new Ref(Ref.Storage.Packed, last.Name, last.Name, last.ObjectId, id, true);
                            newPackedRefs.put(last.Name,  last);
                            continue;
                        }

                        int sp = p.IndexOf(' ');
                        ObjectId id2 = ObjectId.FromString(p.Slice(0, sp));
                        string name = p.Substring(sp + 1);
                        last = new Ref(Ref.Storage.Packed, name, name, id2);
                        newPackedRefs.Add(last.Name, last);
                    }
                }

                _packedRefsLastModified = currTime;
                _packedRefsLength = currLen;
                _packedRefs = newPackedRefs;
                SetModified();
            }
            catch (FileNotFoundException)
            {
                // Ignore it and leave the new map empty.
                //
                _packedRefsLastModified = DateTime.MinValue;
                _packedRefsLength = 0;
                _packedRefs = newPackedRefs;
            }
            catch (IOException e)
            {
                throw new GitException("Cannot read packed refs", e);
            }
        }
Exemplo n.º 19
0
 /**
  * Create a command to update, create or delete a ref in this repository.
  * 
  * @param name
  *            name of the ref the caller wants to modify.
  * @return an update command. The caller must finish populating this command
  *         and then invoke one of the update methods to actually make a
  *         change.
  * @throws IOException
  *             a symbolic ref was passed in and could not be resolved back
  *             to the base ref, as the symbolic ref could not be read.
  */
 public RefUpdate NewUpdate(string name)
 {
     RefreshPackedRefs();
     Ref r = ReadRefBasic(name, 0);
     if (r == null)
         r = new Ref(Ref.Storage.New, name, null);
     return new RefUpdate(this, r, FileForRef(r.Name));
 }
Exemplo n.º 20
0
 public Ref Peel(Ref pRef)
 {
     return _refDb.Peel(pRef);
 }
Exemplo n.º 21
0
        //public void Stored(string name, ObjectId id, DateTime time)
        //{
        //    looseRefs.Add(name, new CachedRef(Ref.Storage.Loose, name, id, time));
        //}

        public void stored(String origName, String name, ObjectId id, DateTime time)
        {
            lock (this)
            {
                looseRefs[name] = new Ref(Ref.Storage.Loose, origName, name, id);
                looseRefsMTime[name] = time;
                setModified();
            }
            this.Repository.fireRefsMaybeChanged();
        }