Handle when just owner has been deleted

Don't include old the repository name when next repository is selected.
This commit is contained in:
Jamie Cansdale 2018-09-11 11:12:28 +01:00
Родитель fca3a96eee
Коммит 785c5f6659
1 изменённых файлов: 31 добавлений и 17 удалений

Просмотреть файл

@ -25,7 +25,7 @@ namespace GitHub.ViewModels.Dialog.Clone
readonly IRepositoryCloneService service; readonly IRepositoryCloneService service;
readonly IReadOnlyList<IRepositoryCloneTabViewModel> tabs; readonly IReadOnlyList<IRepositoryCloneTabViewModel> tabs;
string path; string path;
string previousOwner; IRepositoryModel previousRepository;
ObservableAsPropertyHelper<string> pathError; ObservableAsPropertyHelper<string> pathError;
int selectedTabIndex; int selectedTabIndex;
@ -143,41 +143,55 @@ namespace GitHub.ViewModels.Dialog.Clone
{ {
if (repository != null) if (repository != null)
{ {
var basePath = GetBasePath(Path, previousOwner); var basePath = GetUpdatedBasePath(Path);
previousOwner = repository.Owner; previousRepository = repository;
Path = System.IO.Path.Combine(basePath, repository.Owner, repository.Name); Path = System.IO.Path.Combine(basePath, repository.Owner, repository.Name);
} }
} }
string GetBasePath(string path, string owner) string GetUpdatedBasePath(string path)
{ {
if (string.IsNullOrEmpty(path)) if (string.IsNullOrEmpty(path))
{ {
return service.DefaultClonePath; return service.DefaultClonePath;
} }
if (string.IsNullOrEmpty(owner)) if (previousRepository == null)
{ {
return path; return path;
} }
var dir = path; if (FindDirWithout(path, previousRepository?.Owner, 2) is string dirWithoutOwner)
for (var i = 0; i < 2; i++)
{ {
if (string.IsNullOrEmpty(dir)) return dirWithoutOwner;
{ }
break;
}
var name = System.IO.Path.GetFileName(dir); if (FindDirWithout(path, previousRepository?.Name, 1) is string dirWithoutRepo)
dir = System.IO.Path.GetDirectoryName(dir); {
if (name == owner) return dirWithoutRepo;
{
return dir;
}
} }
return path; return path;
string FindDirWithout(string dir, string match, int levels)
{
for (var i = 0; i < 2; i++)
{
if (string.IsNullOrEmpty(dir))
{
break;
}
var name = System.IO.Path.GetFileName(dir);
dir = System.IO.Path.GetDirectoryName(dir);
if (name == match)
{
return dir;
}
}
return null;
}
} }
string ValidatePath(IRepositoryModel repository, string path) string ValidatePath(IRepositoryModel repository, string path)