-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathRemote.cs
188 lines (161 loc) · 5.76 KB
/
Remote.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// A remote repository whose branches are tracked.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Remote : IBelongToARepository, IDisposable
{
internal readonly Repository repository;
private readonly RefSpecCollection refSpecs;
readonly RemoteHandle handle;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Remote()
{ }
internal Remote(RemoteHandle handle, Repository repository)
{
this.repository = repository;
this.handle = handle;
refSpecs = new RefSpecCollection(this, handle);
repository.RegisterForCleanup(this);
}
/// <summary>
/// The finalizer for the <see cref="Remote"/> class.
/// </summary>
~Remote()
{
Dispose(false);
}
#region IDisposable
bool disposedValue = false; // To detect redundant calls
/// <summary>
/// Release the unmanaged remote object
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (!disposedValue)
{
if (handle != null)
{
handle.Dispose();
}
disposedValue = true;
}
}
#endregion
/// <summary>
/// Gets the alias of this remote repository.
/// </summary>
public virtual string Name
{
get { return Proxy.git_remote_name(handle); }
}
/// <summary>
/// Gets the url to use to communicate with this remote repository.
/// </summary>
public virtual string Url
{
get { return Proxy.git_remote_url(handle); }
}
/// <summary>
/// Gets the distinct push url for this remote repository, if set.
/// Defaults to the fetch url (<see cref="Url"/>) if not set.
/// </summary>
public virtual string PushUrl
{
get { return Proxy.git_remote_pushurl(handle) ?? Url; }
}
/// <summary>
/// Gets the Tag Fetch Mode of the remote - indicating how tags are fetched.
/// </summary>
public virtual TagFetchMode TagFetchMode
{
get { return Proxy.git_remote_autotag(handle); }
}
/// <summary>
/// Gets the list of <see cref="RefSpec"/>s defined for this <see cref="Remote"/>
/// </summary>
public virtual IEnumerable<RefSpec> RefSpecs { get { return refSpecs; } }
/// <summary>
/// Gets the list of <see cref="RefSpec"/>s defined for this <see cref="Remote"/>
/// that are intended to be used during a Fetch operation
/// </summary>
public virtual IEnumerable<RefSpec> FetchRefSpecs
{
get { return refSpecs.Where(r => r.Direction == RefSpecDirection.Fetch); }
}
/// <summary>
/// Gets the list of <see cref="RefSpec"/>s defined for this <see cref="Remote"/>
/// that are intended to be used during a Push operation
/// </summary>
public virtual IEnumerable<RefSpec> PushRefSpecs
{
get { return refSpecs.Where(r => r.Direction == RefSpecDirection.Push); }
}
/// <summary>
/// Transform a reference to its source reference using the <see cref="Remote"/>'s default fetchspec.
/// </summary>
/// <param name="reference">The reference to transform.</param>
/// <returns>The transformed reference.</returns>
internal unsafe string FetchSpecTransformToSource(string reference)
{
using (RemoteHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, Name, true))
{
git_refspec* fetchSpecPtr = Proxy.git_remote_get_refspec(remoteHandle, 0);
return Proxy.git_refspec_rtransform(new IntPtr(fetchSpecPtr), reference);
}
}
/// <summary>
/// Determines if the proposed remote name is well-formed.
/// </summary>
/// <param name="name">The name to be checked.</param>
/// <returns>true if the name is valid; false otherwise.</returns>
public static bool IsValidName(string name)
{
return Proxy.git_remote_is_valid_name(name);
}
/// <summary>
/// Gets the configured behavior regarding the deletion
/// of stale remote tracking branches.
/// <para>
/// If defined, will return the value of the <code>remote.<name>.prune</code> entry.
/// Otherwise return the value of <code>fetch.prune</code>.
/// </para>
/// </summary>
public virtual bool AutomaticallyPruneOnFetch
{
get
{
var remotePrune = repository.Config.Get<bool>("remote", Name, "prune");
if (remotePrune != null)
{
return remotePrune.Value;
}
var fetchPrune = repository.Config.Get<bool>("fetch.prune");
return fetchPrune != null && fetchPrune.Value;
}
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "{0} => {1}", Name, Url);
}
}
IRepository IBelongToARepository.Repository { get { return repository; } }
}
}