Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 197c7f1

Browse files
committed
Add more functionality to SimpleRepositoryModel
Now that ActiveRepo is a SimpleRepositoryModel, we need a way to update the url, for when a user adds or removes the origin remote manually inside Visual Studio (in the git repo settings), or via the command line. This is required so that the UI updates properly and rechecks whether the repo is ours or not.
1 parent 41e7c28 commit 197c7f1

File tree

5 files changed

+58
-17
lines changed

5 files changed

+58
-17
lines changed

src/GitHub.App/Extensions/RepositoryModelExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public static ISimpleRepositoryModel ToModel([AllowNull] this IGitRepositoryInfo
1515
{
1616
if (repo == null)
1717
return null;
18-
var uri = repo.GetUriFromRepository();
19-
var name = uri?.NameWithOwner;
20-
return name != null ? new SimpleRepositoryModel(name, uri, repo.RepositoryPath) : null;
18+
return SimpleRepositoryModel.Create(repo.RepositoryPath);
2119
}
2220
}
2321
}

src/GitHub.App/SampleData/SampleViewModels.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public long PrivateReposInPlan
388388
}
389389

390390
[ExcludeFromCodeCoverage]
391-
public class RepositoryModelDesigner : IRepositoryModel
391+
public class RepositoryModelDesigner : NotificationAwareObject, IRepositoryModel
392392
{
393393
public RepositoryModelDesigner() : this("repo")
394394
{
@@ -416,6 +416,8 @@ public void SetIcon(bool isPrivate, bool isFork)
416416
public Octicon Icon { get; set; }
417417

418418
public IAccount Owner { get; set; }
419+
420+
public void Refresh() { }
419421
}
420422

421423
public class RepositoryCloneViewModelDesigner : BaseViewModelDesigner, IRepositoryCloneViewModel
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
using GitHub.Primitives;
22
using GitHub.UI;
3+
using System.ComponentModel;
34

45
namespace GitHub.Models
56
{
6-
public interface ISimpleRepositoryModel
7+
public interface ISimpleRepositoryModel : INotifyPropertyChanged
78
{
89
string Name { get; }
910
UriString CloneUrl { get; }
1011
string LocalPath { get; }
1112
Octicon Icon { get; }
1213

1314
void SetIcon(bool isPrivate, bool isFork);
15+
16+
17+
/// <summary>
18+
/// Updates the url information based on the local path
19+
/// </summary>
20+
void Refresh();
1421
}
1522
}

src/GitHub.Exports/Models/SimpleRepositoryModel.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using GitHub.Primitives;
1+
using GitHub.Extensions;
2+
using GitHub.Primitives;
23
using GitHub.UI;
34
using GitHub.VisualStudio.Helpers;
45
using System;
56
using System.Diagnostics;
67
using System.Globalization;
8+
using System.IO;
79

810
namespace GitHub.Models
911
{
@@ -18,6 +20,31 @@ public SimpleRepositoryModel(string name, UriString cloneUrl, string localPath =
1820
Icon = Octicon.repo;
1921
}
2022

23+
public SimpleRepositoryModel(string path)
24+
{
25+
if (path == null)
26+
throw new ArgumentNullException("path");
27+
if (!Directory.Exists(path))
28+
throw new ArgumentException("Path does not exist", path);
29+
var uri = GitHelpers.GetRepoFromPath(path)?.GetUri();
30+
var name = uri?.NameWithOwner;
31+
if (name == null)
32+
name = Path.GetDirectoryName(name);
33+
Name = name;
34+
LocalPath = path;
35+
CloneUrl = uri;
36+
Icon = Octicon.repo;
37+
}
38+
39+
public static ISimpleRepositoryModel Create(string path)
40+
{
41+
if (path == null)
42+
return null;
43+
if (!Directory.Exists(path))
44+
return null;
45+
return new SimpleRepositoryModel(path);
46+
}
47+
2148
public void SetIcon(bool isPrivate, bool isFork)
2249
{
2350
Icon = isPrivate
@@ -27,15 +54,30 @@ public void SetIcon(bool isPrivate, bool isFork)
2754
: Octicon.repo;
2855
}
2956

57+
public void Refresh()
58+
{
59+
if (LocalPath == null)
60+
return;
61+
var uri = GitHelpers.GetRepoFromPath(LocalPath)?.GetUri();
62+
if (CloneUrl != uri)
63+
CloneUrl = uri;
64+
}
65+
3066
public string Name { get; private set; }
31-
public UriString CloneUrl { get; private set; }
67+
UriString cloneUrl;
68+
public UriString CloneUrl { get { return cloneUrl; } set { cloneUrl = value; this.RaisePropertyChange(); } }
3269
public string LocalPath { get; private set; }
3370
Octicon icon;
3471
public Octicon Icon { get { return icon; } set { icon = value; this.RaisePropertyChange(); } }
3572

73+
/// <summary>
74+
/// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime
75+
/// of a repository. Equals takes care of any hash collisions because of this
76+
/// </summary>
77+
/// <returns></returns>
3678
public override int GetHashCode()
3779
{
38-
return (Name?.GetHashCode() ?? 0) ^ (CloneUrl?.GetHashCode() ?? 0) ^ (LocalPath?.TrimEnd('\\').ToUpperInvariant().GetHashCode() ?? 0);
80+
return (Name?.GetHashCode() ?? 0) ^ (LocalPath?.TrimEnd('\\').ToUpperInvariant().GetHashCode() ?? 0);
3981
}
4082

4183
public override bool Equals(object obj)

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,8 @@ static IEnumerable<ISimpleRepositoryModel> PokeTheRegistryForRepositoryList()
117117
{
118118
using (var subkey = key.OpenSubKey(x))
119119
{
120-
var path = subkey?.GetValue("Path") as string;
121-
if (path != null)
122-
{
123-
var uri = VisualStudio.Services.GetRepoFromPath(path)?.GetUri();
124-
var name = uri?.NameWithOwner;
125-
if (name != null)
126-
return new SimpleRepositoryModel(name, uri, path);
127-
}
120+
return SimpleRepositoryModel.Create(subkey?.GetValue("Path") as string);
128121
}
129-
return null;
130122
})
131123
.Where(x => x != null)
132124
.ToList();

0 commit comments

Comments
 (0)