SharpSvn - Is File or Folder is under svn repository - sharpsvn

i intend to use SharpSvn methods such as 'add' and 'lock' for file or folder under SVN repository.
if the file is not mapped i receive an error indication - 'the node ... is not found'.
prior to SvnClient.Info() or SvnClient.Status(), how can i know the status of the file - is it or is it not added to SVN repository?

You can use the 'GetStatus' method:
static bool IsInRepository(string filename)
{
bool isVersioned = false;
using (SvnClient client = new SvnClient())
{
SvnStatusArgs sa = new SvnStatusArgs();
sa.Depth = SvnDepth.Empty;
sa.RetrieveAllEntries = true;
Collection<SvnStatusEventArgs> statuses;
client.GetStatus(filename, sa, out statuses);
isVersioned = !statuses[0].LocalContentStatus.Equals(SvnStatus.NotVersioned);
}
return isVersioned;
}

Related

Add new Files and Folders to Azure Git Repository with Azure DevOps REST API

How to Add new Files and Folders to Azure Git Repository with Azure DevOps REST API?
I want to add some static files to my repository using Azure DevOps REST APIs.
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/repositories?view=azure-devops-rest-5.1
Is there any option available via REST API?.
or anyother automated way available, either CICD or through c#?
I found the answer, we can use the Git Push REST API uri
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-5.1
Follow below steps in your C# code
call GetRef REST https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/refs{3}
this should return the object of your repository branch which you can use to push your changes
Next, call Push REST API to create folder or file into your repository
https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/pushes{3}
var changes = new List<ChangeToAdd>();
//Add Files
//pnp_structure.yml
var jsonContent = File.ReadAllText(#"./static-files/somejsonfile.json");
ChangeToAdd changeJson = new ChangeToAdd()
{
changeType = "add",
item = new ItemBase() { path = string.Concat(path, "/[your-folder-name]/somejsonfile.json") },
newContent = new Newcontent()
{
contentType = "rawtext",
content = jsonContent
}
};
changes.Add(changeJson);
CommitToAdd commit = new CommitToAdd();
commit.comment = "commit from code";
commit.changes = changes.ToArray();
var content = new List<CommitToAdd>() { commit };
var request = new
{
refUpdates = refs,
commits = content
};
var personalaccesstoken = _configuration["azure-devOps-configuration-token"];
var authorization = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccesstoken)));
_logger.LogInformation($"[HTTP REQUEST] make a http call with uri: {uri} ");
//here I making http client call
// https://dev.azure.com/{orgnizationName}/{projectName}/_apis/git/repositories/{repositoryId}/pushes{?api-version}
var result = _httpClient.SendHttpWebRequest(uri, method, data, authorization);

Using the Azure Devops Server client library, how do you create the master branch in a new repository?

Using the client API I can create a new repository, but it hasn't been initialized and so doesn't contain a master branch.
public void CreateRepository(string name)
{
TeamProjectReference tpr = new TeamProjectReference();
tpr.Name = "AppDev";
tpr.Id = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
GitRepositoryCreateOptions grco = new GitRepositoryCreateOptions();
grco.ProjectReference = tpr;
grco.Name = name;
newRepo = gitClient.CreateRepositoryAsync(grco).Result;
}
I can't figure out how to create a master branch. The methods to create branches all require a parent branch to create from. How do I create master using the client libraries?
How do I create master using the client libraries?
Apart from creating the empty repo, you also need to initialize it with initial commit. Hint from this rest api Initial commit, and also from this official document. You can follow my working sample below:
static void Main(string[] args)
{
//Connect to Azure Devops Service and create an empty git repo.
ConnectToAzureDevopsService(OrgUrl, PAT);
TeamProjectReference teamProjectReference = new TeamProjectReference();
teamProjectReference.Name = "MyProjectName";
teamProjectReference.Id = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
GitRepositoryCreateOptions gitRepositoryCreateOptions = new GitRepositoryCreateOptions();
gitRepositoryCreateOptions.ProjectReference = teamProjectReference;
gitRepositoryCreateOptions.Name = "MyTestRepo";
GitRepository gitRepo = GitClient.CreateRepositoryAsync(gitRepositoryCreateOptions).Result;
//You need extra steps below to initialize your newly created repo.
//Craft the branch and commit that we'll push.
GitRefUpdate newBranch = new GitRefUpdate()
{
RepositoryId = gitRepo.Id,
Name = $"refs/heads/master",
OldObjectId = "0000000000000000000000000000000000000000",
};
string newFileName = $"README.md";
GitCommitRef newCommit = new GitCommitRef()
{
Comment = "Initial the repo with custom README.md",
Changes = new GitChange[]
{
new GitChange()
{
ChangeType = VersionControlChangeType.Add,
Item = new GitItem() { Path = $"/{newFileName}" },
NewContent = new ItemContent()
{
Content = "# Thank you for using VSTS!",
ContentType = ItemContentType.RawText,
},
}
},
};
//Start initializing the repo via creating the push.
GitPush InitialPush = GitClient.CreatePushAsync(new GitPush()
{
RefUpdates = new GitRefUpdate[] { newBranch },
Commits = new GitCommitRef[] { newCommit },
}, gitRepo.Id).Result;
//Let me know if it succeeds.
Console.WriteLine("Done");
}
The result:
Let me know if it helps.

How to checkout a file using sharpsvn

I tried to checkout a file using sharpsvn from remote repository,but i found sharpsvn can not to checkout single file only checkout folder,please help me to know how to checkout a file?Thx.
My code
SvnUpdateResult result;
SvnCheckOutArgs checkoutArgs = new SvnCheckOutArgs();
string target = txtRepository.Text.Trim();
SvnUriTarget url = new SvnUriTarget(target);
string fileName = url.FileName;
string path = folder + "\\" + fileName;
using (SvnClient client = new SvnClient())
{
try
{
client.CheckOut(url,txtLocalFilePath.Text.Trim(),out result);//.Update(path,updateArgs,out result);
if (result != null)
{
WriteCheckOutTime(txtRepository.Text.Trim(), result.Revision);
MessageBox.Show("Check out success!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
catch (SvnException svnException)
{
MessageBox.Show(svnException.Message + "Check out error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (UriFormatException uriException)
{
MessageBox.Show(uriException.Message + "Check out error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
The smallest element you can check out with Subversion is a directory. It is not possible to check out a single file.
You can check out a directory, but leave it empty, via the Sparse Directories feature. Then update only the file you're interested in. But you must start with a directory.
FYI if you want to checkout empty use following syntax
//first define args
SvnCheckOutArgs args = new SvnCheckOutArgs();
// then for checkout only forlder empty
args.Depth = SvnDepth.Empty;
//checkout folder
client.CheckOut(url,txtLocalFilePath.Text.Trim(),args,out result)

How to test if a URL from an Eclipse bundle is a directory?

I'm trying to populate a directory from the contents of a bundle built into my plug-in. The following code works when the bundle is a file-system, but fails when the bundle is a JAR.
What is the best way to test if a URL is a directory? Or is there a completely different, better approach for creating a file structure from a resource bundle?
static private void bundleCopy(String dir, String destination) throws IOException {
Bundle bundle = com.mds.apg.Activator.getDefault().getBundle();
for (#SuppressWarnings("unchecked")
Enumeration<URL> en = (Enumeration<URL>) bundle.findEntries(dir, "*", true);
en.hasMoreElements();) {
URL url = en.nextElement();
String toFileName = destination + url.getPath().substring(dir.length());
File toFile = new File(toFileName);
InputStream in;
try {
in = url.openStream();
} catch (FileNotFoundException e) {
// this exception get thrown for file system directories but not for jar file ones
if (!toFile.mkdir()) {
throw new IOException("bundleCopy: " + "directory Creation Failed: "
+ toFileName);
}
continue;
}
FileCopy.coreStreamCopy(in, toFile);
}
}
I found an answer:
The key point is that the Enumeration entries for directories end in a '/'.
The following correctly distinguishes between directories and files for both JARs and file systems:
static private void bundleCopy(String dir, String destination)
throws IOException, URISyntaxException {
Bundle bundle = com.mds.apg.Activator.getDefault().getBundle();
Enumeration<URL> en = bundle.findEntries(dir, "*", true);
while (en.hasMoreElements()) {
URL url = en.nextElement();
String pathFromBase = url.getPath().substring(dir.length()+1);
String toFileName = destination + pathFromBase;
File toFile = new File(toFileName);
if (pathFromBase.lastIndexOf('/') == pathFromBase.length() - 1) {
// This is a directory - create it and recurse
if (!toFile.mkdir()) {
throw new IOException("bundleCopy: " + "directory Creation Failed: " + toFileName);
}
} else {
// This is a file - copy it
FileCopy.coreStreamCopy(url.openStream(), toFile);
}
}
}
You could try something like new File(FileLocator.resolve(url).toUri()) to convert from the Eclipse-specific URL to one using a native Java protocol.

Reading from an assembly with embedded resources

I built an assembly containing one js file.
I marked the file as Embedded Resource and added it into AssemblyInfo file.
I can't refernce the Assembly from a web site. It is in the bin folder but I don't see the reference to it.
It seems like not having at least a class inside the assembly I can't reference it.
I would include the js file into my pages from the assembly.
How should I do this?
Thanks
I do exactly the same thing in one of my projects. I have a central ScriptManager class that actually caches the scripts as it pulls them, but the core of extracting the script file from the embedded resource looks like this:
internal static class ScriptManager
{
private static Dictionary<string, string> m_scriptCache =
new Dictionary<string, string>();
public static string GetScript(string scriptName)
{
return GetScript(scriptName, true);
}
public static string GetScript(string scriptName, bool encloseInCdata)
{
StringBuilder script = new StringBuilder("\r\n");
if (encloseInCdata)
{
script.Append("//<![CDATA[\r\n");
}
if (!m_scriptCache.ContainsKey(scriptName))
{
var asm = Assembly.GetExecutingAssembly();
var stream = asm.GetManifestResourceStream(scriptName);
if (stream == null)
{
var names = asm.GetManifestResourceNames();
// NOTE: you passed in an invalid name.
// Use the above line to determine what tyhe name should be
// most common is not setting the script file to be an embedded resource
if (Debugger.IsAttached) Debugger.Break();
return string.Empty;
}
using (var reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
m_scriptCache.Add(scriptName, text);
}
}
script.Append(m_scriptCache[scriptName]);
if (encloseInCdata)
{
script.Append("//]]>\r\n");
}
return script.ToString();
}
}
EDIT
To provide more clarity, I've posted my ScriptManager class. To extract a script file, I simply call it like this:
var script = ScriptManager.GetScript("Fully.Qualified.Script.js");
The name you pass in it the full, case-sensitive resource name (the exception handler gets a list of them by calling GetManifestResourceNames()).
This gives you the script as a string - you can then put it out into a file, inject it into the page (which is what I'm doing) or whatever you like.
Assembly myAssembly = // Get your assembly somehow (see below)...
IList<string> resourceNames = myAssembly.GetManifestResourceNames();
This will return a list of all resource names that have been set as 'Embedded Resource'. The name is usually the fully qualified namespace of wherever you put that JS file. So if your project is called My.Project and you store your MyScript.js file inside a folder in your project called Resources, the full name would be My.Project.Resources.MyScript.js
If you then want to use that JS file:
Stream stream = myAssembly.GetManifestResourceStream(myResourceName);
Where myResourceName argument might be "My.Project.Resources.MyScript.js". To get that JS file in that Stream object, you'll need to write it as a file to the hard drive, and serve it from your website as a static file, something like this:
Stream stream = executingAssembly.GetManifestResourceStream(imageResourcePath);
if (stream != null)
{
string directory = Path.GetDirectoryName("C:/WebApps/MyApp/Scripts/");
using (Stream file = File.OpenWrite(directory + "MyScript.js"))
{
CopyStream(stream, file);
}
stream.Dispose();
}
And the code for CopyStream method:
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
You might want to stick all this code in an Application_Start event in your Global.asax. You don't want it to run for each request
Now getting a reference to your Assembly is a different matter, there are many ways. One way is to include all the above code in your Assembly in question, then make sure you reference that Assembly from your main WebApp project in Visual Studio, then get a reference to the currently executing Assembly like so.
namespace My.Project
{
public class ResourceLoader
{
public static void LoadResources()
{
Assembly myAssembly = Assembly.GetExecutingAssembly();
// rest of code from above (snip)
}
}
}
Then call ResourceLoader.LoadResources() from your Application_Start event in your Global.asax.
Hope this helps
Fully working example (I hope):
namespace TestResources.Assembly
{
public class ResourceLoader
{
public static void LoadResources()
{
Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream stream = myAssembly
.GetManifestResourceStream("TestResources.Assembly.CheckAnswer.js");
if (stream != null)
{
string directory = Path.GetDirectoryName("C:/WebApps/MyApp/Scripts/");
using (Stream file = File.OpenWrite(directory + "MyScript.js"))
{
CopyStream(stream, file);
}
stream.Dispose();
}
}
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
}
}
Some caveats:
Change "C:/WebApps/MyApp/" to wherever your web app is located, maybe write some code to work this out dynamically
Make sure the /Scripts folder exists in your webapp root
I think it will overwrite the 'MyScript.js' file if it already exists, but just in case you might want to add some code to check for that file and delete it
Then stick a call to this code in your Global.asax file:
protected void Application_Start()
{
ResourceLoader.LoadResources();
}
Then the path for your web site will be /Scripts/MyScript.js eg:
<head>
<!-- Rest of head (snip) -->
<script type="text/javascript" src="/Scripts/MyScript.js"></script>
</head>