Skip to content

code_style: use switch expressions for simple value mapping #1579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 2 additions & 20 deletions src/Commands/CompareRevisions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,8 @@ private void ParseLine(List<Models.Change> outs, string line)

var change = new Models.Change() { Path = match.Groups[2].Value };
var status = match.Groups[1].Value;

switch (status[0])
{
case 'M':
change.Set(Models.ChangeState.Modified);
outs.Add(change);
break;
case 'A':
change.Set(Models.ChangeState.Added);
outs.Add(change);
break;
case 'D':
change.Set(Models.ChangeState.Deleted);
outs.Add(change);
break;
case 'C':
change.Set(Models.ChangeState.Copied);
outs.Add(change);
break;
}
change.Set(Models.Change.ChangeStateFromCode(status[0]));
outs.Add(change);
}
}
}
45 changes: 19 additions & 26 deletions src/Commands/GitFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,13 @@ public static async Task<bool> StartAsync(string repo, Models.GitFlowBranchType
start.WorkingDirectory = repo;
start.Context = repo;

switch (type)
var typeStr = BranchTypeToString(type);
if (typeStr == null)
{
case Models.GitFlowBranchType.Feature:
start.Args = $"flow feature start {name}";
break;
case Models.GitFlowBranchType.Release:
start.Args = $"flow release start {name}";
break;
case Models.GitFlowBranchType.Hotfix:
start.Args = $"flow hotfix start {name}";
break;
default:
App.RaiseException(repo, "Bad git-flow branch type!!!");
return false;
App.RaiseException(repo, "Bad git-flow branch type!!!");
return false;
}
start.Args = $"flow {typeStr} start {name}";

return await start.Use(log).ExecAsync().ConfigureAwait(false);
}
Expand All @@ -54,21 +46,13 @@ public static async Task<bool> FinishAsync(string repo, Models.GitFlowBranchType
var builder = new StringBuilder();
builder.Append("flow ");

switch (type)
var typeStr = BranchTypeToString(type);
if (typeStr == null)
{
case Models.GitFlowBranchType.Feature:
builder.Append("feature");
break;
case Models.GitFlowBranchType.Release:
builder.Append("release");
break;
case Models.GitFlowBranchType.Hotfix:
builder.Append("hotfix");
break;
default:
App.RaiseException(repo, "Bad git-flow branch type!!!");
return false;
App.RaiseException(repo, "Bad git-flow branch type!!!");
return false;
}
builder.Append(typeStr);

builder.Append(" finish ");
if (squash)
Expand All @@ -85,5 +69,14 @@ public static async Task<bool> FinishAsync(string repo, Models.GitFlowBranchType
finish.Args = builder.ToString();
return await finish.Use(log).ExecAsync().ConfigureAwait(false);
}

private static string BranchTypeToString(Models.GitFlowBranchType type) =>
type switch
{
Models.GitFlowBranchType.Feature => "feature",
Models.GitFlowBranchType.Release => "release",
Models.GitFlowBranchType.Hotfix => "hotfix",
_ => null
};
}
}
132 changes: 19 additions & 113 deletions src/Commands/QueryLocalChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,120 +38,26 @@ public QueryLocalChanges(string repo, bool includeUntracked = true)
var change = new Models.Change() { Path = match.Groups[2].Value };
var status = match.Groups[1].Value;

switch (status)
change.ConflictReason = status switch
{
case " M":
change.Set(Models.ChangeState.None, Models.ChangeState.Modified);
break;
case " T":
change.Set(Models.ChangeState.None, Models.ChangeState.TypeChanged);
break;
case " A":
change.Set(Models.ChangeState.None, Models.ChangeState.Added);
break;
case " D":
change.Set(Models.ChangeState.None, Models.ChangeState.Deleted);
break;
case " R":
change.Set(Models.ChangeState.None, Models.ChangeState.Renamed);
break;
case " C":
change.Set(Models.ChangeState.None, Models.ChangeState.Copied);
break;
case "M":
change.Set(Models.ChangeState.Modified);
break;
case "MM":
change.Set(Models.ChangeState.Modified, Models.ChangeState.Modified);
break;
case "MT":
change.Set(Models.ChangeState.Modified, Models.ChangeState.TypeChanged);
break;
case "MD":
change.Set(Models.ChangeState.Modified, Models.ChangeState.Deleted);
break;
case "T":
change.Set(Models.ChangeState.TypeChanged);
break;
case "TM":
change.Set(Models.ChangeState.TypeChanged, Models.ChangeState.Modified);
break;
case "TT":
change.Set(Models.ChangeState.TypeChanged, Models.ChangeState.TypeChanged);
break;
case "TD":
change.Set(Models.ChangeState.TypeChanged, Models.ChangeState.Deleted);
break;
case "A":
change.Set(Models.ChangeState.Added);
break;
case "AM":
change.Set(Models.ChangeState.Added, Models.ChangeState.Modified);
break;
case "AT":
change.Set(Models.ChangeState.Added, Models.ChangeState.TypeChanged);
break;
case "AD":
change.Set(Models.ChangeState.Added, Models.ChangeState.Deleted);
break;
case "D":
change.Set(Models.ChangeState.Deleted);
break;
case "R":
change.Set(Models.ChangeState.Renamed);
break;
case "RM":
change.Set(Models.ChangeState.Renamed, Models.ChangeState.Modified);
break;
case "RT":
change.Set(Models.ChangeState.Renamed, Models.ChangeState.TypeChanged);
break;
case "RD":
change.Set(Models.ChangeState.Renamed, Models.ChangeState.Deleted);
break;
case "C":
change.Set(Models.ChangeState.Copied);
break;
case "CM":
change.Set(Models.ChangeState.Copied, Models.ChangeState.Modified);
break;
case "CT":
change.Set(Models.ChangeState.Copied, Models.ChangeState.TypeChanged);
break;
case "CD":
change.Set(Models.ChangeState.Copied, Models.ChangeState.Deleted);
break;
case "DD":
change.ConflictReason = Models.ConflictReason.BothDeleted;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "AU":
change.ConflictReason = Models.ConflictReason.AddedByUs;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "UD":
change.ConflictReason = Models.ConflictReason.DeletedByThem;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "UA":
change.ConflictReason = Models.ConflictReason.AddedByThem;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "DU":
change.ConflictReason = Models.ConflictReason.DeletedByUs;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "AA":
change.ConflictReason = Models.ConflictReason.BothAdded;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "UU":
change.ConflictReason = Models.ConflictReason.BothModified;
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
break;
case "??":
change.Set(Models.ChangeState.None, Models.ChangeState.Untracked);
break;
"DD" => Models.ConflictReason.BothDeleted,
"AU" => Models.ConflictReason.AddedByUs,
"UD" => Models.ConflictReason.DeletedByThem,
"UA" => Models.ConflictReason.AddedByThem,
"DU" => Models.ConflictReason.DeletedByUs,
"AA" => Models.ConflictReason.BothAdded,
"UU" => Models.ConflictReason.BothModified,
_ => Models.ConflictReason.None
};
if (change.ConflictReason != Models.ConflictReason.None)
change.Set(Models.ChangeState.None, Models.ChangeState.Conflicted);
else if (status == "??")
change.Set(Models.ChangeState.None, Models.ChangeState.Untracked);
else
{
var indexStatus = Models.Change.ChangeStateFromCode(status[0]);
var worktreeStatus = status.Length > 1 ? Models.Change.ChangeStateFromCode(status[1]) : Models.ChangeState.None;
change.Set(indexStatus, worktreeStatus);
}

if (change.Index != Models.ChangeState.None || change.WorkTree != Models.ChangeState.None)
Expand Down
19 changes: 1 addition & 18 deletions src/Commands/QueryStagedChangesWithAmend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,7 @@ public QueryStagedChangesWithAmend(string repo, string parent)
};

var type = match.Groups[3].Value;
switch (type)
{
case "A":
change.Set(Models.ChangeState.Added);
break;
case "C":
change.Set(Models.ChangeState.Copied);
break;
case "D":
change.Set(Models.ChangeState.Deleted);
break;
case "M":
change.Set(Models.ChangeState.Modified);
break;
case "T":
change.Set(Models.ChangeState.TypeChanged);
break;
}
change.Set(Models.Change.ChangeStateFromCode(type[0]));
changes.Add(change);
}
}
Expand Down
23 changes: 8 additions & 15 deletions src/Commands/QuerySubmodules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,15 @@ public QuerySubmodules(string repo)
var path = match.Groups[3].Value;

var module = new Models.Submodule() { Path = path, SHA = sha };
switch (stat[0])
module.Status = stat[0] switch
{
case '-':
module.Status = Models.SubmoduleStatus.NotInited;
break;
case '+':
module.Status = Models.SubmoduleStatus.RevisionChanged;
break;
case 'U':
module.Status = Models.SubmoduleStatus.Unmerged;
break;
default:
module.Status = Models.SubmoduleStatus.Normal;
needCheckLocalChanges = true;
break;
}
'-' => Models.SubmoduleStatus.NotInited,
'+' => Models.SubmoduleStatus.RevisionChanged,
'U' => Models.SubmoduleStatus.Unmerged,
_ => Models.SubmoduleStatus.Normal
};
if (module.Status == Models.SubmoduleStatus.Normal)
needCheckLocalChanges = true;

map.Add(path, module);
submodules.Add(module);
Expand Down
13 changes: 13 additions & 0 deletions src/Models/Change.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,18 @@ public void Set(ChangeState index, ChangeState workTree = ChangeState.None)
"Both added",
"Both modified"
];

public static ChangeState ChangeStateFromCode(char code) =>
code switch
{
'M' => ChangeState.Modified,
'T' => ChangeState.TypeChanged,
'A' => ChangeState.Added,
'D' => ChangeState.Deleted,
'R' => ChangeState.Renamed,
'C' => ChangeState.Copied,
'U' => ChangeState.Untracked,
_ => ChangeState.None,
};
}
}
9 changes: 3 additions & 6 deletions src/Models/DiffOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ public DiffOption(Change change, bool isUnstaged)

if (isUnstaged)
{
switch (change.WorkTree)
if (change.WorkTree is ChangeState.Added or ChangeState.Untracked)
{
case ChangeState.Added:
case ChangeState.Untracked:
_extra = "--no-index";
_orgPath = "/dev/null";
break;
_extra = "--no-index";
_orgPath = "/dev/null";
}
}
else
Expand Down
43 changes: 17 additions & 26 deletions src/ViewModels/Conflict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,24 @@ public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).GetResultAsync().Result;
}

switch (wc.InProgressContext)
if (wc.InProgressContext is RebaseInProgress rebase)
{
case CherryPickInProgress cherryPick:
Theirs = cherryPick.Head;
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
break;
case RebaseInProgress rebase:
var b = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
if (b != null)
Theirs = new ConflictSourceBranch(b.Name, b.Head, rebase.StoppedAt);
else
Theirs = new ConflictSourceBranch(rebase.HeadName, rebase.StoppedAt?.SHA ?? "----------", rebase.StoppedAt);

Mine = rebase.Onto;
break;
case RevertInProgress revert:
Theirs = revert.Head;
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
break;
case MergeInProgress merge:
Theirs = merge.Source;
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
break;
default:
Theirs = "Stash or Patch";
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
break;
var b = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
Theirs = b != null
? new ConflictSourceBranch(b.Name, b.Head, rebase.StoppedAt)
: new ConflictSourceBranch(rebase.HeadName, rebase.StoppedAt?.SHA ?? "----------", rebase.StoppedAt);
Mine = rebase.Onto;
}
else
{
Theirs = wc.InProgressContext switch
{
CherryPickInProgress cherryPick => cherryPick.Head,
RevertInProgress revert => revert.Head,
MergeInProgress merge => merge.Source,
_ => "Stash or Patch"
};
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
}
}

Expand Down
Loading
Loading