Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Changing project files to link to codeanalysis files #458

Merged
merged 24 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
975357b
Changing project files to link to codeanalysis-small.ruleset
StanleyGoldman Nov 21, 2017
4bb9fc3
Merge branch 'master' into fixes/code-analysis
StanleyGoldman Jan 4, 2018
739584a
Changing to full profile
StanleyGoldman Jan 4, 2018
b1403ec
Merge branch 'fixes/code-analysis' into fixes/string-compare
StanleyGoldman Jan 4, 2018
dc1bb42
Fixing some string comparisons
StanleyGoldman Jan 4, 2018
f0f73a4
Merge branch 'fixes/code-analysis' into fixes/serializable-exceptions
StanleyGoldman Jan 4, 2018
e6280b6
Adding Serializable attribute to exception classes
StanleyGoldman Jan 4, 2018
a93e05b
Merge branch 'fixes/code-analysis' into fixes/string-null-or-empty
StanleyGoldman Jan 4, 2018
264141d
Using string.empty
StanleyGoldman Jan 4, 2018
f2ec270
Merge branch 'fixes/code-analysis' into fixes/remove-unused-fields
StanleyGoldman Jan 4, 2018
659f547
Refactor RepositoryWatcher to use a HashSet and remove an unused object
StanleyGoldman Jan 4, 2018
0ed4781
Removing other unused fields
StanleyGoldman Jan 4, 2018
184512d
Merge pull request #560 from github-for-unity/fixes/remove-unused-fields
StanleyGoldman Jan 5, 2018
03bb996
Merge pull request #559 from github-for-unity/fixes/string-compare
StanleyGoldman Jan 5, 2018
182a9bf
Merge pull request #557 from github-for-unity/fixes/string-null-or-empty
StanleyGoldman Jan 5, 2018
dc447df
Merge pull request #558 from github-for-unity/fixes/serializable-exce…
StanleyGoldman Jan 5, 2018
7c20770
Merge branch 'master' into fixes/code-analysis
shana Jan 31, 2018
e6166ca
Set rulesets and code analysis and suppress some special cases
shana Jan 31, 2018
2a7643c
Add default exception constructors
shana Jan 31, 2018
de373f2
Make abstract class constructors protected
shana Jan 31, 2018
d9fde96
String comparison fixes
shana Jan 31, 2018
da286ca
Implementing equality ops in all structs to avoid the default reflect…
shana Jan 31, 2018
df58256
Some more code analysis fixes
shana Jan 31, 2018
154a687
Disable code analysis in the dev configuration, it's sloooow
shana Jan 31, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions common/build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
<Compile Include="$(SolutionDir)common\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<None Include="$(SolutionDir)common\GitHub.ruleset">
<Link>Properties\GitHub.ruleset</Link>
<None Include="$(SolutionDir)common\codeanalysis-release.ruleset">
<Link>Properties\codeanalysis-release.ruleset</Link>
</None>
<None Include="$(SolutionDir)common\codeanalysis-debug.ruleset">
<Link>Properties\codeanalysis-debug.ruleset</Link>
</None>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@

<!-- link demand -->
<Rule Id="CA2123" Action="None" />

<!-- disposable -->
<Rule Id="CA1001" Action="None" />

<!-- no we like our event handler signatures thankyouverymuch -->
<Rule Id="CA1009" Action="None" />

<!-- calling TryParse without using its return value is not a crime, Microsoft -->
<Rule Id="CA1806" Action="None" />

<!-- we're returning arrays from properties -->
<Rule Id="CA1819" Action="None" />

<!-- Unity does its own serialization and doesn't need special serializable type constructors -->
<Rule Id="CA2229" Action="None" />
<Rule Id="CA2240" Action="None" />

<!-- the compiler already warns about this -->
<Rule Id="CA1823" Action="None" />

</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0001" Action="None" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@
<Rule Id="CA2227" Action="None" />
<Rule Id="CA2235" Action="None" />
<Rule Id="IDE001" Action="None" />

<!-- link demand -->
<Rule Id="CA2123" Action="None" />

<!-- disposable -->
<Rule Id="CA1001" Action="None" />

<!-- no we like our event handler signatures thankyouverymuch -->
<Rule Id="CA1009" Action="None" />

<!-- calling TryParse without using its return value is not a crime, Microsoft -->
<Rule Id="CA1806" Action="None" />

<!-- we're returning arrays from properties -->
<Rule Id="CA1819" Action="None" />

<!-- Unity does its own serialization and doesn't need special serializable type constructors -->
<Rule Id="CA2229" Action="None" />
<Rule Id="CA2240" Action="None" />

<!-- the compiler already warns about this -->
<Rule Id="CA1823" Action="None" />
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0001" Action="None" />
Expand Down
11 changes: 11 additions & 0 deletions common/properties.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@
<UnityDir Condition="$(UnityDir) == '' and Exists('\Applications\Unity\Unity.app\Contents\Managed\UnityEditor.dll')">\Applications\Unity\Unity.app\Contents\Managed\</UnityDir>
<BuildConfig Condition=" '$(BuildConfig)' == '' ">Debug</BuildConfig>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<CodeAnalysisRuleSet>$(SolutionDir)\common\codeanalysis-debug.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<CodeAnalysisRuleSet>$(SolutionDir)\common\codeanalysis-release.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'dev|AnyCPU'">
<CodeAnalysisRuleSet>$(SolutionDir)\common\codeanalysis-debug.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

</Project>
19 changes: 18 additions & 1 deletion src/GitHub.Api/Application/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Octokit;
using System.Runtime.Serialization;

namespace GitHub.Unity
{
Expand Down Expand Up @@ -332,7 +333,8 @@ class GitHubRepository
public string CloneUrl { get; set; }
}

class ApiClientException : Exception
[Serializable]
public class ApiClientException : Exception
{
public ApiClientException()
{ }
Expand All @@ -342,8 +344,12 @@ public ApiClientException(string message) : base(message)

public ApiClientException(string message, Exception innerException) : base(message, innerException)
{ }

protected ApiClientException(SerializationInfo info, StreamingContext context) : base(info, context)
{ }
}

[Serializable]
class TokenUsernameMismatchException : ApiClientException
{
public string CachedUsername { get; }
Expand All @@ -354,11 +360,22 @@ public TokenUsernameMismatchException(string cachedUsername, string currentUsern
CachedUsername = cachedUsername;
CurrentUsername = currentUsername;
}
protected TokenUsernameMismatchException(SerializationInfo info, StreamingContext context) : base(info, context)
{ }
}

[Serializable]
class KeychainEmptyException : ApiClientException
{
public KeychainEmptyException()
{ }
public KeychainEmptyException(string message) : base(message)
{ }

public KeychainEmptyException(string message, Exception innerException) : base(message, innerException)
{ }

protected KeychainEmptyException(SerializationInfo info, StreamingContext context) : base(info, context)
{ }
}
}
42 changes: 42 additions & 0 deletions src/GitHub.Api/Authentication/Keychain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@ public struct Connection
{
public UriString Host;
public string Username;

public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + (Host?.GetHashCode() ?? 0);
hash = hash * 23 + (Username?.GetHashCode() ?? 0);
return hash;
}

public override bool Equals(object other)
{
if (other is Connection)
return Equals((Connection)other);
return false;
}

public bool Equals(Connection other)
{
return
object.Equals(Host, other.Host) &&
String.Equals(Username, other.Username)
;
}

public static bool operator ==(Connection lhs, Connection rhs)
{
// If both are null, or both are same instance, return true.
if (ReferenceEquals(lhs, rhs))
return true;

// If one is null, but not both, return false.
if (((object)lhs == null) || ((object)rhs == null))
return false;

// Return true if the fields match:
return lhs.Equals(rhs);
}

public static bool operator !=(Connection lhs, Connection rhs)
{
return !(lhs == rhs);
}
}

class ConnectionCacheItem
Expand Down
52 changes: 23 additions & 29 deletions src/GitHub.Api/Events/RepositoryWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public int CheckAndProcessEvents()

private int ProcessEvents(Event[] fileEvents)
{
Dictionary<EventType, List<EventData>> events = new Dictionary<EventType, List<EventData>>();
var events = new HashSet<EventType>();
foreach (var fileEvent in fileEvents)
{
if (!running)
Expand Down Expand Up @@ -177,90 +177,90 @@ private int ProcessEvents(Event[] fileEvents)
// handling events in .git/*
if (fileA.IsChildOf(paths.DotGitPath))
{
if (!events.ContainsKey(EventType.ConfigChanged) && fileA.Equals(paths.DotGitConfig))
if (!events.Contains(EventType.ConfigChanged) && fileA.Equals(paths.DotGitConfig))
{
events.Add(EventType.ConfigChanged, null);
events.Add(EventType.ConfigChanged);
}
else if (!events.ContainsKey(EventType.HeadChanged) && fileA.Equals(paths.DotGitHead))
else if (!events.Contains(EventType.HeadChanged) && fileA.Equals(paths.DotGitHead))
{
events.Add(EventType.HeadChanged, null);
events.Add(EventType.HeadChanged);
}
else if (!events.ContainsKey(EventType.IndexChanged) && fileA.Equals(paths.DotGitIndex))
else if (!events.Contains(EventType.IndexChanged) && fileA.Equals(paths.DotGitIndex))
{
events.Add(EventType.IndexChanged, null);
events.Add(EventType.IndexChanged);
}
else if (!events.ContainsKey(EventType.RemoteBranchesChanged) && fileA.IsChildOf(paths.RemotesPath))
else if (!events.Contains(EventType.RemoteBranchesChanged) && fileA.IsChildOf(paths.RemotesPath))
{
events.Add(EventType.RemoteBranchesChanged, null);
events.Add(EventType.RemoteBranchesChanged);
}
else if (!events.ContainsKey(EventType.LocalBranchesChanged) && fileA.IsChildOf(paths.BranchesPath))
else if (!events.Contains(EventType.LocalBranchesChanged) && fileA.IsChildOf(paths.BranchesPath))
{
events.Add(EventType.LocalBranchesChanged, null);
events.Add(EventType.LocalBranchesChanged);
}
else if (!events.ContainsKey(EventType.RepositoryCommitted) && fileA.IsChildOf(paths.DotGitCommitEditMsg))
else if (!events.Contains(EventType.RepositoryCommitted) && fileA.IsChildOf(paths.DotGitCommitEditMsg))
{
events.Add(EventType.RepositoryCommitted, null);
events.Add(EventType.RepositoryCommitted);
}
}
else
{
if (events.ContainsKey(EventType.RepositoryChanged) || ignoredPaths.Any(ignoredPath => fileA.IsChildOf(ignoredPath)))
if (events.Contains(EventType.RepositoryChanged) || ignoredPaths.Any(ignoredPath => fileA.IsChildOf(ignoredPath)))
{
continue;
}
events.Add(EventType.RepositoryChanged, null);
events.Add(EventType.RepositoryChanged);
}
}

return FireEvents(events);
}

private int FireEvents(Dictionary<EventType, List<EventData>> events)
private int FireEvents(HashSet<EventType> events)
{
int eventsProcessed = 0;
if (events.ContainsKey(EventType.ConfigChanged))
if (events.Contains(EventType.ConfigChanged))
{
Logger.Trace("ConfigChanged");
ConfigChanged?.Invoke();
eventsProcessed++;
}

if (events.ContainsKey(EventType.HeadChanged))
if (events.Contains(EventType.HeadChanged))
{
Logger.Trace("HeadChanged");
HeadChanged?.Invoke();
eventsProcessed++;
}

if (events.ContainsKey(EventType.LocalBranchesChanged))
if (events.Contains(EventType.LocalBranchesChanged))
{
Logger.Trace("LocalBranchesChanged");
LocalBranchesChanged?.Invoke();
eventsProcessed++;
}

if (events.ContainsKey(EventType.RemoteBranchesChanged))
if (events.Contains(EventType.RemoteBranchesChanged))
{
Logger.Trace("RemoteBranchesChanged");
RemoteBranchesChanged?.Invoke();
eventsProcessed++;
}

if (events.ContainsKey(EventType.IndexChanged))
if (events.Contains(EventType.IndexChanged))
{
Logger.Trace("IndexChanged");
IndexChanged?.Invoke();
eventsProcessed++;
}

if (events.ContainsKey(EventType.RepositoryChanged))
if (events.Contains(EventType.RepositoryChanged))
{
Logger.Trace("RepositoryChanged");
RepositoryChanged?.Invoke();
eventsProcessed++;
}

if (events.ContainsKey(EventType.RepositoryCommitted))
if (events.Contains(EventType.RepositoryCommitted))
{
Logger.Trace("RepositoryCommitted");
RepositoryCommitted?.Invoke();
Expand Down Expand Up @@ -306,11 +306,5 @@ private enum EventType
RepositoryChanged,
RepositoryCommitted
}

private class EventData
{
public string Origin;
public string Branch;
}
}
}
40 changes: 40 additions & 0 deletions src/GitHub.Api/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,45 @@ public struct StringResult
public string Chunk;
public int Start;
public int End;

public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + (Chunk?.GetHashCode() ?? 0);
hash = hash * 23 + Start.GetHashCode();
hash = hash * 23 + End.GetHashCode();
return hash;
}

public override bool Equals(object other)
{
if (other is StringResult)
return Equals((StringResult)other);
return false;
}

public bool Equals(StringResult other)
{
return String.Equals(Chunk, other.Chunk) && Start == other.Start && End == other.End;
}

public static bool operator ==(StringResult lhs, StringResult rhs)
{
// If both are null, or both are same instance, return true.
if (ReferenceEquals(lhs, rhs))
return true;

// If one is null, but not both, return false.
if (((object)lhs == null) || ((object)rhs == null))
return false;

// Return true if the fields match:
return lhs.Equals(rhs);
}

public static bool operator !=(StringResult lhs, StringResult rhs)
{
return !(lhs == rhs);
}
}
}
Loading