示例#1
0
        public static bool TryLoad(int index, out GitIgnoreRule result)
        {
            result = new GitIgnoreRule();

            int effect;

            if (!int.TryParse(EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(EffectKey, index), "-1"), out effect) || effect < 0)
            {
                return(false);
            }

            result.Effect = (GitIgnoreRuleEffect)effect;

            result.FileString = EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(FileKey, index));

            try
            {
                result.File = new Regex(result.FileString);
            }
            catch (ArgumentException)
            {
                result.File = null;
            }

            result.LineString = EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(LineKey, index));

            try
            {
                result.Line = new Regex(result.LineString);
            }
            catch (ArgumentException)
            {
                result.Line = null;
            }

            result.TriggerText = EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(TriggerTextKey, index));

            return(true);
        }
示例#2
0
        private void OnGitIgnoreRulesGUI()
        {
            var gitignoreRulesWith = Position.width - Styles.GitIgnoreRulesTotalHorizontalMargin - Styles.GitIgnoreRulesSelectorWidth - 16f;
            var effectWidth        = gitignoreRulesWith * Styles.GitIgnoreRulesEffectRatio;
            var fileWidth          = gitignoreRulesWith * Styles.GitIgnoreRulesFileRatio;
            var lineWidth          = gitignoreRulesWith * Styles.GitIgnoreRulesLineRatio;

            GUILayout.Label(GitIgnoreRulesTitle, EditorStyles.boldLabel);
            GUILayout.BeginVertical(GUI.skin.box);
            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            {
                GUILayout.Space(Styles.GitIgnoreRulesSelectorWidth);
                TableCell(GitIgnoreRulesEffect, effectWidth);
                TableCell(GitIgnoreRulesFile, fileWidth);
                TableCell(GitIgnoreRulesLine, lineWidth);
            }
            GUILayout.EndHorizontal();

            var count = GitIgnoreRule.Count;

            for (var index = 0; index < count; ++index)
            {
                GitIgnoreRule rule;
                if (GitIgnoreRule.TryLoad(index, out rule))
                {
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(Styles.GitIgnoreRulesSelectorWidth);

                        if (gitIgnoreRulesSelection == index && Event.current.type == EventType.Repaint)
                        {
                            var selectorRect = GUILayoutUtility.GetLastRect();
                            selectorRect.Set(selectorRect.x, selectorRect.y + 2f, selectorRect.width - 2f, EditorGUIUtility.singleLineHeight);
                            EditorStyles.foldout.Draw(selectorRect, false, false, false, false);
                        }

                        TableCell(rule.Effect.ToString(), effectWidth);
                        // TODO: Tint if the regex is null
                        TableCell(rule.FileString, fileWidth);
                        TableCell(rule.LineString, lineWidth);
                    }
                    GUILayout.EndHorizontal();

                    if (Event.current.type == EventType.MouseDown && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
                    {
                        newGitIgnoreRulesSelection = index;
                        Event.current.Use();
                    }
                }
            }

            GUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();
                if (GUILayout.Button(NewGitIgnoreRuleButton, EditorStyles.miniButton))
                {
                    GitIgnoreRule.New();
                    GUIUtility.hotControl = GUIUtility.keyboardControl = -1;
                }
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);

            // Selected gitignore rule edit

            GitIgnoreRule selectedRule;

            if (GitIgnoreRule.TryLoad(gitIgnoreRulesSelection, out selectedRule))
            {
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button(DeleteGitIgnoreRuleButton, EditorStyles.miniButton))
                        {
                            GitIgnoreRule.Delete(gitIgnoreRulesSelection);
                            newGitIgnoreRulesSelection = gitIgnoreRulesSelection - 1;
                        }
                    }
                    GUILayout.EndHorizontal();
                    EditorGUI.BeginChangeCheck();
                    var newEffect = (GitIgnoreRuleEffect)EditorGUILayout.EnumPopup(GitIgnoreRulesEffect, selectedRule.Effect);
                    var newFile   = EditorGUILayout.TextField(GitIgnoreRulesFile, selectedRule.FileString);
                    var newLine   = EditorGUILayout.TextField(GitIgnoreRulesLine, selectedRule.LineString);
                    GUILayout.Label(GitIgnoreRulesDescription);
                    var newDescription = EditorGUILayout.TextArea(selectedRule.TriggerText, Styles.CommitDescriptionFieldStyle);
                    if (EditorGUI.EndChangeCheck())
                    {
                        GitIgnoreRule.Save(gitIgnoreRulesSelection, newEffect, newFile, newLine, newDescription);
                        // TODO: Fix this
                    }
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();
        }
示例#3
0
        private IEnumerable <ProjectConfigurationIssue> EvaluateGitIgnore()
        {
            // Read rules
            var rules = new List <GitIgnoreRule>(GitIgnoreRule.Count);

            for (var index = 0; index < rules.Capacity; ++index)
            {
                GitIgnoreRule rule;
                if (GitIgnoreRule.TryLoad(index, out rule))
                {
                    rules.Add(rule);
                }
            }

            if (!rules.Any())
            {
                yield break;
            }

            // Read gitignore files
            GitIgnoreFile[] files;
            files =
                Directory.GetFiles(Utility.GitRoot, GitIgnoreFilePattern, SearchOption.AllDirectories)
                .Select(p => new GitIgnoreFile(p))
                .ToArray();

            if (files.Length < 1)
            {
                yield break;
            }

            // Evaluate each rule
            for (var ruleIndex = 0; ruleIndex < rules.Count; ++ruleIndex)
            {
                var rule = rules[ruleIndex];
                for (var fileIndex = 0; fileIndex < files.Length; ++fileIndex)
                {
                    var file = files[fileIndex];
                    // Check against all files with matching path
                    if (rule.File == null || !rule.File.IsMatch(file.Path))
                    {
                        continue;
                    }

                    // Validate all lines in that file
                    for (var lineIndex = 0; lineIndex < file.Contents.Length; ++lineIndex)
                    {
                        var line  = file.Contents[lineIndex];
                        var match = rule.Line != null && rule.Line.IsMatch(line);

                        if (rule.Effect == GitIgnoreRuleEffect.Disallow && match)
                        // This line is not allowed
                        {
                            yield return(new GitIgnoreIssue(file.Path, line, rule.TriggerText));
                        }
                        else if (rule.Effect == GitIgnoreRuleEffect.Require)
                        // If the line is required, see if we're there
                        {
                            if (match)
                            // We found it! No sense in searching further in this file.
                            {
                                break;
                            }
                            else if (lineIndex == file.Contents.Length - 1)
                            // We reached the last line without finding it
                            {
                                yield return(new GitIgnoreIssue(file.Path, string.Empty, rule.TriggerText));
                            }
                        }
                    }
                }
            }
        }