How can we change the remote url of a repository?
using (var repository = new Repository(repositoryPath))
{
//Change the remote url first
//Then checkout
}
How can we change the remote url of a repository?
var newUrl = "https://github.com/owner/my_repository.git";";
Remote remote = repo.Network.Remotes[name];
// This will update the remote configuration, persist it
// and return a new instance of the updated remote
Remote updatedremote = repo.Network.Remotes.Update(remote, r => r.Url = newUrl);
For what it's worth, most of the remote properties can be updated by following the same pattern. Feel free to take a peek at the RemoteFixture.cs test suite for more detailed examples.
They had made public virtual Remote Update(Remote remote, params Action[] actions) obsolete.
var newUrl = "https://github.com/owner/my_repository.git";
WorkingRepository.Network.Remotes.Update("origin", r => { r.Url = uri; });
Related
My existing Fetch method looks like this.
public void Fetch(string remote) => CommandLine.Run($"git fetch {remote}", _repoFolder);
I would like to implement the same feature using libgit2sharp.
This is what I have came up with:
public void Fetch(string remote)
{
var repo = new Repository(_folder);
var options = new FetchOptions();
options.Prune = true;
options.TagFetchMode = TagFetchMode.Auto;
var refSpecs = $"+refs/heads/*:refs/remotes/{remote}/*";
Commands.Fetch(repo, remote, new [] {refSpecs}, options, "Fetching remote");
}
However this fails with the following error message:
System.InvalidOperationException: 'authentication cancelled'
I have also tried with the libgit2sharp-ssh package, where the result is error code 401 (unathorized client).
I presume the git command line tool works because it knows how to authorize the access (since there is already a remote). How can I achieve the same using libgit2sharp?
Have you tried something like
using(var repo = new Repository(_folder))
{
var options = new FetchOptions();
options.Prune = true;
options.TagFetchMode = TagFetchMode.Auto;
options.CredentialsProvider = new CredentialsHandler(
(url, usernameFromUrl, types) =>
new UsernamePasswordCredentials()
{
Username = "username",
Password = "password"
});
var remote = repo.Network.Remotes["origin"];
var msg = "Fetching remote";
var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
Commands.Fetch(repo, remote.Name, refSpecs, options, msg);
}
I'm trying to figure out how to get the commit log for a sub directory. According to this thread, it should work fine in libgit2sharp.
For testing I'm using a small repo, https://github.com/ornatwork/nugetpackages.git that has 8 total entries for the whole repository.
I cloned it locally in root c:/nugetpackages, from that folder I can do.
git log -- devbox
on the command line and I will get two commit entries for the /devbox sub directory as expected.
Sample Xunit test code to do the same using libgit2sharp
[Fact]
public void testSub()
{
// Extract the git commit history
using (var repo = new Repository(#"C:\nugetpackages"))
{
Trace.WriteLine("repo count=" + repo.Commits.Count());
// absolute path
IEnumerable<LogEntry> history = repo.Commits.QueryBy(#"C:\nugetpackages\devbox");
Trace.WriteLine("subdir count=" + history.Count());
}
}
I'm expecting count of 8 and 2, but this is what I get.
repo count=8
subdir count=0
What am I missing ?
Use a relative path from the repository's base diretory:
using (var repo = new Repository(#"/Users/sushi/code/sushi/Xamarin.Forms.Renderer.Tests"))
{
D.WriteLine("repo count=" + repo.Commits.Count());
IEnumerable<LogEntry> history = repo.Commits.QueryBy(#"AlarmSO");
D.WriteLine("subdir count=" + history.Count());
}
ref: FileHistoryFixture.cs
Update:
Follow up, is there a way to combine subdir with a filter, for example. CommitFilter filter = new CommitFilter(); filter.FirstParentOnly = true;
Not sure if this is what you are looking for... if not please in a new question, thanks.
using (var repo = new Repository(#"/Users/sushi/code/sushi/RealmJson"))
{
var subDir = "media";
var commits = repo.Commits.QueryBy(new CommitFilter { FirstParentOnly = true }).Where(
(Commit c) => c.Tree.Any(
(TreeEntry te) => te.Path.StartsWith(subDir, StringComparison.Ordinal)));
foreach (var commit in commits)
{
D.WriteLine($"{commit.Sha} : {commit.MessageShort}");
}
}
Is there a way to download specific TFS build log? we’re using TFS 2013.
I’ve already tried to use the IBuildDetail, yet failed.
THX.
You can use the menthod “Get Build”. It returns IBuildDetail , use IBuildDetail.LogLocation property to get the log file for this build. Finally download or query it. Below is a demo code:
publicstaticvoid GetBuildLogAndReadThroughTheLog()
{
// Get all the team projects using the TFS API
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ConfigurationManager.AppSettings["TfsUri"]));
var versionControlService = tfs.GetService<VersionControlServer>();
var teamProject = versionControlService.GetAllTeamProjects(false)[0];
// Use the build service to get build details for a team project
var buildService = tfs.GetService<IBuildServer>();
// Get the build details
var build = buildService.QueryBuilds(teamProject.Name)[0];
// Get the location of the build Log
var buildLogLocation = build.LogLocation;
// Get the log file
var logFile = versionControlService.GetItem(buildLogLocation);
// Do you want to download the file to a local path?
TextReader tr = new StreamReader(logFile.DownloadFile());
string input = null;
while ((input = tr.ReadLine()) != null)
{
}
}
}
The entire build log can be found in the Tfs_YourTeamProjectCollection database in the Tbl_BuildInformation. You can also query and get it from DataBase
More ways for you reference: https://paulselles.wordpress.com/2013/11/15/tfs-build-log-querying-build-log-data/
UPdate
Using "StreamReader" will read the log file line by line.
using (StreamReader sr = new StreamReader(buildLogLocation)
{
while (sr.Peek() >= 0)
{
// do your work here...
}
}
I'm currently trying to update my local repository from a remote one.
This works as long as I don't have any local changes in my files.
using (var r = new LibGit2Sharp.Repository(repo))
{
var options = new MergeOptions
{
MergeFileFavor = MergeFileFavor.Theirs,
};
var result = r.Network.Pull(localSignature, new PullOptions { MergeOptions = options });
}
I found that answer before: StackOverflow.
However, while trying this with the above code, it's just marking my repo as "Up2Date" (status of the result) instead of actually merging my local changes with the remote updates.
How can I actually achieve this?
We're not talking about SSH since it's not yet implemented, but how can I supply credentials for the repository before I perform a fetch via HTTP/HTTPS? There doesn't seem to be a parameter for a Credentials instance, and nothing when constructing a Repository instance for storing credentials.
Within the FetchFixture.cs file, there is a Fetch() test using credentials:
[SkippableFact]
public void CanFetchIntoAnEmptyRepositoryWithCredentials()
{
InconclusiveIf(() => string.IsNullOrEmpty(Constants.PrivateRepoUrl),
"Populate Constants.PrivateRepo* to run this test");
string repoPath = InitNewRepository();
using (var repo = new Repository(repoPath))
{
Remote remote = repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);
// Perform the actual fetch
repo.Network.Fetch(remote, new FetchOptions
{
Credentials = new Credentials
{
Username = Constants.PrivateRepoUsername,
Password = Constants.PrivateRepoPassword
}
});
}
}