-
Notifications
You must be signed in to change notification settings - Fork 899
Make Pull and Fetch commands #1288
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections.Generic; | ||
using LibGit2Sharp; | ||
using LibGit2Sharp.Core; | ||
using LibGit2Sharp.Core.Handles; | ||
|
||
namespace LibGit2Sharp | ||
{ | ||
/// <summary> | ||
/// Class to serve as namespacing for the command-emulating methods | ||
/// </summary> | ||
public static partial class Commands | ||
{ | ||
private static RemoteHandle RemoteFromNameOrUrl(RepositoryHandle repoHandle, string remote) | ||
{ | ||
RemoteHandle handle = null; | ||
handle = Proxy.git_remote_lookup(repoHandle, remote, false); | ||
|
||
// If that wasn't the name of a remote, let's use it as a url | ||
if (handle == null) | ||
{ | ||
handle = Proxy.git_remote_create_anonymous(repoHandle, remote); | ||
} | ||
|
||
return handle; | ||
} | ||
|
||
/// <summary> | ||
/// Perform a fetch | ||
/// </summary> | ||
/// <param name="repository">The repository in which to fetch.</param> | ||
/// <param name="remote">The remote to fetch from. Either as a remote name or a URL</param> | ||
/// <param name="options">Fetch options.</param> | ||
/// <param name="logMessage">Log message for any ref updates.</param> | ||
/// <param name="refspecs">List of refspecs to apply as active.</param> | ||
public static void Fetch(Repository repository, string remote, IEnumerable<string> refspecs, FetchOptions options, string logMessage) | ||
{ | ||
Ensure.ArgumentNotNull(remote, "remote"); | ||
|
||
options = options ?? new FetchOptions(); | ||
using (var remoteHandle = RemoteFromNameOrUrl(repository.Handle, remote)) | ||
{ | ||
|
||
var callbacks = new RemoteCallbacks(options); | ||
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); | ||
|
||
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of | ||
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation | ||
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug | ||
// where the managed layer could move the git_remote_callbacks to a different location in memory, | ||
// but libgit2 would still reference the old address. | ||
// | ||
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against | ||
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords. | ||
var fetchOptions = new GitFetchOptions | ||
{ | ||
RemoteCallbacks = gitCallbacks, | ||
download_tags = Proxy.git_remote_autotag(remoteHandle), | ||
}; | ||
|
||
if (options.TagFetchMode.HasValue) | ||
{ | ||
fetchOptions.download_tags = options.TagFetchMode.Value; | ||
} | ||
|
||
if (options.Prune.HasValue) | ||
{ | ||
fetchOptions.Prune = options.Prune.Value ? FetchPruneStrategy.Prune : FetchPruneStrategy.NoPrune; | ||
} | ||
else | ||
{ | ||
fetchOptions.Prune = FetchPruneStrategy.FromConfigurationOrDefault; | ||
} | ||
|
||
Proxy.git_remote_fetch(remoteHandle, refspecs, fetchOptions, logMessage); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using LibGit2Sharp; | ||
using LibGit2Sharp.Core; | ||
|
||
namespace LibGit2Sharp | ||
{ | ||
/// <summary> | ||
/// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD. | ||
/// </summary> | ||
public static partial class Commands | ||
{ | ||
/// <summary> | ||
/// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD. | ||
/// </summary> | ||
/// <param name="repository">The repository.</param> | ||
/// <param name="merger">The signature to use for the merge.</param> | ||
/// <param name="options">The options for fetch and merging.</param> | ||
public static MergeResult Pull(Repository repository, Signature merger, PullOptions options) | ||
{ | ||
Ensure.ArgumentNotNull(repository, "repository"); | ||
Ensure.ArgumentNotNull(merger, "merger"); | ||
Ensure.ArgumentNotNull(options, "options"); | ||
|
||
|
||
options = options ?? new PullOptions(); | ||
Branch currentBranch = repository.Head; | ||
|
||
if (!currentBranch.IsTracking) | ||
{ | ||
throw new LibGit2SharpException("There is no tracking information for the current branch."); | ||
} | ||
|
||
if (currentBranch.RemoteName == null) | ||
{ | ||
throw new LibGit2SharpException("No upstream remote for the current branch."); | ||
} | ||
|
||
Commands.Fetch(repository, currentBranch.RemoteName, new string[0], options.FetchOptions, null); | ||
return repository.MergeFetchedRefs(merger, options.MergeOptions); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this cause an issue, since
Remote
is nowIDisposable
?I know this is a test method, but we should show the _right thing_™
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's in the same situation as before. We dispose of the remotes on repository disposal.