This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathRepositoryLayoutUtilities.cs
49 lines (44 loc) · 2 KB
/
RepositoryLayoutUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.IO;
using GitHub.Primitives;
namespace GitHub.Services
{
public static class RepositoryLayoutUtilities
{
// Exclude directories with a case sensitive name of "GitHub" from being a possible owner.
// GitHub Desktop uses "GitHub" as its default directory and this isn't a user or organization name.
const string GitHubDirectoryName = "GitHub";
public static string GetDefaultRepositoryPath(UriString cloneUrl, string defaultPath, RepositoryLayout repositoryLayout)
{
switch (repositoryLayout)
{
case RepositoryLayout.Name:
return Path.Combine(defaultPath, cloneUrl.RepositoryName);
case RepositoryLayout.Default:
case RepositoryLayout.OwnerName:
return Path.Combine(defaultPath, cloneUrl.Owner, cloneUrl.RepositoryName);
default:
throw new ArgumentException($"Unknown repository layout: {repositoryLayout}");
}
}
public static RepositoryLayout GetRepositoryLayout(string repositoryLayoutSetting)
{
return Enum.TryParse(repositoryLayoutSetting, out RepositoryLayout repositoryLayout) ?
repositoryLayout : RepositoryLayout.Default;
}
public static (string, RepositoryLayout) GetDefaultPathAndLayout(string repositoryPath, UriString cloneUrl)
{
var possibleOwnerPath = Path.GetDirectoryName(repositoryPath);
var possibleOwner = Path.GetFileName(possibleOwnerPath);
if (string.Equals(possibleOwner, cloneUrl.Owner, StringComparison.OrdinalIgnoreCase) &&
!string.Equals(possibleOwner, GitHubDirectoryName, StringComparison.Ordinal))
{
return (Path.GetDirectoryName(possibleOwnerPath), RepositoryLayout.OwnerName);
}
else
{
return (possibleOwnerPath, RepositoryLayout.Name);
}
}
}
}