Exemplo n.º 1
0
        /// <summary>Execute this batch update.</summary>
        /// <remarks>
        /// Execute this batch update.
        /// <p>
        /// The default implementation of this method performs a sequential reference
        /// update over each reference.
        /// </remarks>
        /// <param name="walk">
        /// a RevWalk to parse tags in case the storage system wants to
        /// store them pre-peeled, a common performance optimization.
        /// </param>
        /// <param name="update">progress monitor to receive update status on.</param>
        /// <exception cref="System.IO.IOException">
        /// the database is unable to accept the update. Individual
        /// command status must be tested to determine if there is a
        /// partial failure, or a total failure.
        /// </exception>
        public virtual void Execute(RevWalk walk, ProgressMonitor update)
        {
            update.BeginTask(JGitText.Get().updatingReferences, commands.Count);
            foreach (ReceiveCommand cmd in commands)
            {
                try
                {
                    update.Update(1);
                    if (cmd.GetResult() == ReceiveCommand.Result.NOT_ATTEMPTED)
                    {
                        cmd.UpdateType(walk);
                        RefUpdate ru = NewUpdate(cmd);
                        switch (cmd.GetType())
                        {
                        case ReceiveCommand.Type.DELETE:
                        {
                            cmd.SetResult(ru.Delete(walk));
                            continue;
                            goto case ReceiveCommand.Type.CREATE;
                        }

                        case ReceiveCommand.Type.CREATE:
                        case ReceiveCommand.Type.UPDATE:
                        case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:
                        {
                            cmd.SetResult(ru.Update(walk));
                            continue;
                        }
                        }
                    }
                }
                catch (IOException err)
                {
                    cmd.SetResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, MessageFormat.Format(JGitText
                                                                                                    .Get().lockError, err.Message));
                }
            }
            update.EndTask();
        }
            /// <summary>
            /// Check if an entry's content is different from the cache,
            /// File status information is used and status is same we
            /// consider the file identical to the state in the working
            /// directory.
            /// </summary>
            /// <remarks>
            /// Check if an entry's content is different from the cache,
            /// File status information is used and status is same we
            /// consider the file identical to the state in the working
            /// directory. Native git uses more stat fields than we
            /// have accessible in Java.
            /// </remarks>
            /// <param name="wd">working directory to compare content with</param>
            /// <param name="forceContentCheck">
            /// True if the actual file content
            /// should be checked if modification time differs.
            /// </param>
            /// <returns>true if content is most likely different.</returns>
            public virtual bool IsModified(FilePath wd, bool forceContentCheck)
            {
                if (this.IsAssumedValid())
                {
                    return(false);
                }
                if (this.IsUpdateNeeded())
                {
                    return(true);
                }
                FilePath file   = this.GetFile(wd);
                long     length = file.Length();

                if (length == 0)
                {
                    if (!file.Exists())
                    {
                        return(true);
                    }
                }
                if (length != this.size)
                {
                    return(true);
                }
                // JDK1.6 has file.canExecute
                // if (file.canExecute() != FileMode.EXECUTABLE_FILE.equals(mode))
                // return true;
                int exebits = FileMode.EXECUTABLE_FILE.GetBits() ^ FileMode.REGULAR_FILE.GetBits(
                    );

                if (this._enclosing.Config_filemode() && FileMode.EXECUTABLE_FILE.Equals(this.mode
                                                                                         ))
                {
                    if (!this._enclosing.File_canExecute(file) && this._enclosing.File_hasExecute())
                    {
                        return(true);
                    }
                }
                else
                {
                    if (FileMode.REGULAR_FILE.Equals(this.mode & ~exebits))
                    {
                        if (!file.IsFile())
                        {
                            return(true);
                        }
                        if (this._enclosing.Config_filemode() && this._enclosing.File_canExecute(file) &&
                            this._enclosing.File_hasExecute())
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        if (FileMode.SYMLINK.Equals(this.mode))
                        {
                            return(true);
                        }
                        else
                        {
                            if (FileMode.TREE.Equals(this.mode))
                            {
                                if (!file.IsDirectory())
                                {
                                    return(true);
                                }
                            }
                            else
                            {
                                System.Console.Out.WriteLine(MessageFormat.Format(JGitText.Get().doesNotHandleMode
                                                                                  , this.mode, file));
                                return(true);
                            }
                        }
                    }
                }
                // Git under windows only stores seconds so we round the timestamp
                // Java gives us if it looks like the timestamp in index is seconds
                // only. Otherwise we compare the timestamp at millisecond prevision.
                long javamtime = this.mtime / 1000000L;
                long lastm     = file.LastModified();

                if (javamtime % 1000 == 0)
                {
                    lastm = lastm - lastm % 1000;
                }
                if (lastm != javamtime)
                {
                    if (!forceContentCheck)
                    {
                        return(true);
                    }
                    try
                    {
                        InputStream @is = new FileInputStream(file);
                        try
                        {
                            ObjectId newId = new ObjectInserter.Formatter().IdFor(Constants.OBJ_BLOB, file.Length
                                                                                      (), @is);
                            return(!newId.Equals(this.sha1));
                        }
                        catch (IOException e)
                        {
                            Sharpen.Runtime.PrintStackTrace(e);
                        }
                        finally
                        {
                            try
                            {
                                @is.Close();
                            }
                            catch (IOException e)
                            {
                                // can't happen, but if it does we ignore it
                                Sharpen.Runtime.PrintStackTrace(e);
                            }
                        }
                    }
                    catch (FileNotFoundException e)
                    {
                        // should not happen because we already checked this
                        Sharpen.Runtime.PrintStackTrace(e);
                        throw new Error(e);
                    }
                }
                return(false);
            }
        /// <summary>Write content of index to disk.</summary>
        /// <remarks>Write content of index to disk.</remarks>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public virtual void Write()
        {
            CheckWriteOk();
            FilePath tmpIndex = new FilePath(cacheFile.GetAbsoluteFile() + ".tmp");
            FilePath Lock     = new FilePath(cacheFile.GetAbsoluteFile() + ".lock");

            if (!Lock.CreateNewFile())
            {
                throw new IOException(JGitText.Get().indexFileIsInUse);
            }
            try
            {
                FileOutputStream fileOutputStream = new FileOutputStream(tmpIndex);
                FileChannel      fc  = fileOutputStream.GetChannel();
                ByteBuffer       buf = ByteBuffer.Allocate(4096);
                MessageDigest    newMessageDigest = Constants.NewMessageDigest();
                header = new GitIndex.Header(entries);
                header.Write(buf);
                buf.Flip();
                newMessageDigest.Update(((byte[])buf.Array()), buf.ArrayOffset(), buf.Limit());
                fc.Write(buf);
                buf.Flip();
                buf.Clear();
                for (Iterator i = entries.Values.Iterator(); i.HasNext();)
                {
                    GitIndex.Entry e = (GitIndex.Entry)i.Next();
                    e.Write(buf);
                    buf.Flip();
                    newMessageDigest.Update(((byte[])buf.Array()), buf.ArrayOffset(), buf.Limit());
                    fc.Write(buf);
                    buf.Flip();
                    buf.Clear();
                }
                buf.Put(newMessageDigest.Digest());
                buf.Flip();
                fc.Write(buf);
                fc.Close();
                fileOutputStream.Close();
                if (cacheFile.Exists())
                {
                    if (db.FileSystem.RetryFailedLockFileCommit())
                    {
                        // file deletion fails on windows if another
                        // thread is reading the file concurrently
                        // So let's try 10 times...
                        bool deleted = false;
                        for (int i_1 = 0; i_1 < 10; i_1++)
                        {
                            if (cacheFile.Delete())
                            {
                                deleted = true;
                                break;
                            }
                            try
                            {
                                Sharpen.Thread.Sleep(100);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        // ignore
                        if (!deleted)
                        {
                            throw new IOException(JGitText.Get().couldNotRenameDeleteOldIndex);
                        }
                    }
                    else
                    {
                        if (!cacheFile.Delete())
                        {
                            throw new IOException(JGitText.Get().couldNotRenameDeleteOldIndex);
                        }
                    }
                }
                if (!tmpIndex.RenameTo(cacheFile))
                {
                    throw new IOException(JGitText.Get().couldNotRenameTemporaryIndexFileToIndex);
                }
                changed       = false;
                statDirty     = false;
                lastCacheTime = cacheFile.LastModified();
                db.FireEvent(new IndexChangedEvent());
            }
            finally
            {
                if (!Lock.Delete())
                {
                    throw new IOException(JGitText.Get().couldNotDeleteLockFileShouldNotHappen);
                }
                if (tmpIndex.Exists() && !tmpIndex.Delete())
                {
                    throw new IOException(JGitText.Get().couldNotDeleteTemporaryIndexFileShouldNotHappen
                                          );
                }
            }
        }
Exemplo n.º 4
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadTree(byte[] raw)
        {
            int rawSize = raw.Length;
            int rawPtr  = 0;

            TreeEntry[] temp;
            int         nextIndex = 0;

            while (rawPtr < rawSize)
            {
                while (rawPtr < rawSize && raw[rawPtr] != 0)
                {
                    rawPtr++;
                }
                rawPtr++;
                rawPtr += Constants.OBJECT_ID_LENGTH;
                nextIndex++;
            }
            temp      = new TreeEntry[nextIndex];
            rawPtr    = 0;
            nextIndex = 0;
            while (rawPtr < rawSize)
            {
                int c = raw[rawPtr++];
                if (c < '0' || c > '7')
                {
                    throw new CorruptObjectException(GetId(), JGitText.Get().corruptObjectInvalidEntryMode
                                                     );
                }
                int mode = c - '0';
                for (; ;)
                {
                    c = raw[rawPtr++];
                    if (' ' == c)
                    {
                        break;
                    }
                    else
                    {
                        if (c < '0' || c > '7')
                        {
                            throw new CorruptObjectException(GetId(), JGitText.Get().corruptObjectInvalidMode
                                                             );
                        }
                    }
                    mode <<= 3;
                    mode  += c - '0';
                }
                int nameLen = 0;
                while (raw[rawPtr + nameLen] != 0)
                {
                    nameLen++;
                }
                byte[] name = new byte[nameLen];
                System.Array.Copy(raw, rawPtr, name, 0, nameLen);
                rawPtr += nameLen + 1;
                ObjectId id = ObjectId.FromRaw(raw, rawPtr);
                rawPtr += Constants.OBJECT_ID_LENGTH;
                TreeEntry ent;
                if (FileMode.REGULAR_FILE.Equals(mode))
                {
                    ent = new FileTreeEntry(this, id, name, false);
                }
                else
                {
                    if (FileMode.EXECUTABLE_FILE.Equals(mode))
                    {
                        ent = new FileTreeEntry(this, id, name, true);
                    }
                    else
                    {
                        if (FileMode.TREE.Equals(mode))
                        {
                            ent = new NGit.Tree(this, id, name);
                        }
                        else
                        {
                            if (FileMode.SYMLINK.Equals(mode))
                            {
                                ent = new SymlinkTreeEntry(this, id, name);
                            }
                            else
                            {
                                if (FileMode.GITLINK.Equals(mode))
                                {
                                    ent = new GitlinkTreeEntry(this, id, name);
                                }
                                else
                                {
                                    throw new CorruptObjectException(GetId(), MessageFormat.Format(JGitText.Get().corruptObjectInvalidMode2
                                                                                                   , Sharpen.Extensions.ToOctalString(mode)));
                                }
                            }
                        }
                    }
                }
                temp[nextIndex++] = ent;
            }
            contents = temp;
        }
Exemplo n.º 5
0
        /// <summary>Parse an encoded type string into a type constant.</summary>
        /// <remarks>Parse an encoded type string into a type constant.</remarks>
        /// <param name="id">
        /// object id this type string came from; may be null if that is
        /// not known at the time the parse is occurring.
        /// </param>
        /// <param name="typeString">string version of the type code.</param>
        /// <param name="endMark">
        /// character immediately following the type string. Usually ' '
        /// (space) or '\n' (line feed).
        /// </param>
        /// <param name="offset">
        /// position within <code>typeString</code> where the parse
        /// should start. Updated with the new position (just past
        /// <code>endMark</code> when the parse is successful.
        /// </param>
        /// <returns>
        /// a type code constant (one of
        /// <see cref="OBJ_BLOB">OBJ_BLOB</see>
        /// ,
        /// <see cref="OBJ_COMMIT">OBJ_COMMIT</see>
        /// ,
        /// <see cref="OBJ_TAG">OBJ_TAG</see>
        /// ,
        /// <see cref="OBJ_TREE">OBJ_TREE</see>
        /// .
        /// </returns>
        /// <exception cref="NGit.Errors.CorruptObjectException">there is no valid type identified by <code>typeString</code>.
        ///     </exception>
        public static int DecodeTypeString(AnyObjectId id, byte[] typeString, byte endMark
                                           , MutableInteger offset)
        {
            try
            {
                int position = offset.value;
                switch (typeString[position])
                {
                case (byte)('b'):
                {
                    if (typeString[position + 1] != 'l' || typeString[position + 2] != 'o' || typeString
                        [position + 3] != 'b' || typeString[position + 4] != endMark)
                    {
                        throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
                    }
                    offset.value = position + 5;
                    return(NGit.Constants.OBJ_BLOB);
                }

                case (byte)('c'):
                {
                    if (typeString[position + 1] != 'o' || typeString[position + 2] != 'm' || typeString
                        [position + 3] != 'm' || typeString[position + 4] != 'i' || typeString[position
                                                                                               + 5] != 't' || typeString[position + 6] != endMark)
                    {
                        throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
                    }
                    offset.value = position + 7;
                    return(NGit.Constants.OBJ_COMMIT);
                }

                case (byte)('t'):
                {
                    switch (typeString[position + 1])
                    {
                    case (byte)('a'):
                    {
                        if (typeString[position + 2] != 'g' || typeString[position + 3] != endMark)
                        {
                            throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
                        }
                        offset.value = position + 4;
                        return(NGit.Constants.OBJ_TAG);
                    }

                    case (byte)('r'):
                    {
                        if (typeString[position + 2] != 'e' || typeString[position + 3] != 'e' || typeString
                            [position + 4] != endMark)
                        {
                            throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
                        }
                        offset.value = position + 5;
                        return(NGit.Constants.OBJ_TREE);
                    }

                    default:
                    {
                        throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
                    }
                    }
                    goto default;
                }

                default:
                {
                    throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
                }
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
            }
        }
Exemplo n.º 6
0
 public override string GetDescription()
 {
     return(JGitText.Get().repositoryState_bisecting);
 }
Exemplo n.º 7
0
 public override string GetDescription()
 {
     return(JGitText.Get().repositoryState_rebaseInteractive);
 }
Exemplo n.º 8
0
 public override string GetDescription()
 {
     return(JGitText.Get().repositoryState_rebaseWithMerge);
 }
Exemplo n.º 9
0
 public override string GetDescription()
 {
     return(JGitText.Get().repositoryState_applyMailbox);
 }
Exemplo n.º 10
0
 public override string GetDescription()
 {
     return(JGitText.Get().repositoryState_conflicts);
 }