LibGit2Sharp Getting the last version of a remote repository - c#

I want to track a project that uses git in my winforms project. I don't want to clone the full repository and the full history, I just want the latest version, and I want to be able to update to new revisions from the remote project.
I have tried this
co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = userName, Password = passWord };
Repository.Clone("Git/repo", #tmpRepoFolder, co);
, but this creates a copy of the entire repository (huge file size), and tracking changes makes the disk space even bigger (100mb of files now takes up over 2gb).
I don't need the history and i don't need the tags. I just want the latest version .

Basically you want a shallow clone (the equivalent of the git clone --depth command) which is not actually supported, there is an open issue for that
As an alternative you can launch a Process which do what you want using the git application.
Here an example:
using(System.Diagnostics.Process p = new Process())
{
p.StartInfo = new ProcessStartInfo()
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
FileName = #"C:\Program Files\Git\bin\git.exe",
Arguments = "clone http://username:password#path/to/repo.git" + " --depth 1"
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}

Related

Get and Set the position in Process.StandardOutput

See as an example I open CMD and navigate to the folder I want to get the data from then I use it to open the app with arugents with standard input(all synchronously) the code so far
public static Process Start(bool DoNotShowWindow = false)
{
ProcessStartInfo cmdStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = DoNotShowWindow
};
Process cmdProcess = new Process
{
StartInfo = cmdStartInfo,
EnableRaisingEvents = true
};
return cmdProcess;
}
//in other method
Process cli = InteractWithConsoleApp.Start();
cli.Start();
cli.StandardInput.WriteLine("cd /");
cli.StandardInput.WriteLine("cd " + path);
cli.StandardInput.WriteLine("fantasygold-cli getbalance abc");
Thread.Sleep(5000);
Problem
Now when I use StandardOutput.Readline, it starts from the beginning and returns me everything like first two lines of copyright,empty lines and even the input which in my case, after waiting 5 secs for the result I want to read line or to the end depending the input from where I had inputted.
possible solution
One solution I found was to change the position but it turned out it doesn't support it and even copying to another stream reader doesn't works(the position is not by line).
Well I can use filters like check a double or for an address starts with F and has a length of 36. The problem comes when I want to get the whole JSON say for like the past transactions, for which I think using filters like '{' and then check for '}'Caveat in this would be bad code, which I don't want.
TLDR
So, what could be the solution to my problem here :)
I found the answer, to open the file in subdirectory just use this cli.StartInfo.FileName = Directory.GetCurrentDirectory() + #"\SubfolderName\fantasygold-cli";
and the arguements like getbalance as cli.StartInfo.Arguments = "getbalance amunim"

How to Execute a command or process in Console application using C#

I am developing one console application that will convert all spss (.sav) files to .csv files. For this I created a SPSS job (spssJob1.spj) manually (using one .sps file) and I am iterating through all the input files (all .sav files) and trying to run that job by updating the input and output path in the .sps file (text.sps). But I don't know how to call that job execution command from my app.
Currently, the command is:
stats C:\Users\10522\Desktop\spssJob1.spj -production
and this should be executed from
C:\Program Files\IBM\SPSS\Statistics\22
because this stats command will be available only in this directory.
So in my app I need to call this process from this path; I am able to call one .exe file by using my app but I don't know how to call one command form a specific directory.
This is my code:
// getting all spss files from the from the input path
FileInfo[] Files = new DirectoryInfo("D:\Input").GetFiles("*.sav");
// looping each files and calling the job
foreach (FileInfo file in Files)
{
if (file.Name != "")
{
// updating the text.sps file for each job
System.IO.File.WriteAllText("D:\Input\text.sps", string.Empty);
System.IO.File.WriteAllText("D:\Input\text.sps", (Content for the file));
// calling the process
var p = new Process();
// this code will work fine simply calling one exe
p.StartInfo = new ProcessStartInfo((#"D:\Input\temp.exe"), "-n")
// instead of this I need to call something like this
// stats C:\Users\10522\Desktop\spssJob1.spj -production from this
// path C:\Program Files\IBM\SPSS\Statistics\22
{
UseShellExecute = false
};
p.Start();
p.WaitForExit();
}
}
ProcessStartInfo pi = new ProcessStartInfo("stats");
pi.Arguments = #"C:\Users\10522\Desktop\spssJob1.spj -production";
pi.WorkingDirectory = #"C:\Program Files\IBM\SPSS\Statistics\22";
pi.UseShellExecute = false;
Process.Start(pi);
you can do this by altering ProcessStartInfo's properties.
Not sure what stats is, if its an exe then you can specify the full exe path and just omit working directory.
var p = new Process();
p.StartInfo = new ProcessStartInfo("stats")
{
//UseShellExecute = false,
Arguments= #"C:\Users\10522\Desktop\spssJob1.spj -production",
WorkingDirectory = #"C:\Program Files\IBM\SPSS\Statistics\22",
};
p.Start();
p.WaitForExit();

Run app as other user in C#

I've got this project that needs to get finished in two days and I'm nearly done with it. Except there's a part of it that needs to start another app and run it as a different domain user.
So far I've got this in code:
Process proc = new Process();
System.Security.SecureString ssPwd = new System.Security.SecureString();
string password = "SomePassword";
for (int x = 0; x < password.Length; x++)
{
ssPwd.AppendChar(password[x]);
}
ProcessStartInfo StartInfo = new ProcessStartInfo
{
FileName = "Translink.exe",
WorkingDirectory = #"C:\Program Files\Western Union\Universal-Release",
UserName = "someuser",
Domain = "somedomain",
Password = ssPwd,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
proc = Process.Start(StartInfo);
proc.WaitForExit();
So when I make a call with and without user and password for example:
C:\Windows\System32\notepad.exe
it runs in both cases but everytime I try to run that particular app "Translink.exe" from either Program Files or Program Files (x86) it just won't run at all with or without runas.
And I've ran out of things to try, I think I've tried everything so far in stackoverflow as there were other similar articles but I think I must be going wrong somewhere specifically.

Error while executing binary file

Ok, hello there. I have some code which should execute my binary file and print all output:
Process Program = new Process();
Program.StartInfo.FileName = "file.bin";
Program.StartInfo.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Build." + this.name;
Program.StartInfo.RedirectStandardOutput = true;
Program.StartInfo.UseShellExecute = false;
Program.Start();
string output = Program.StandardOutput.ReadToEnd();
Program.WaitForExit(1000);
Console.Out.WriteLine(output);
But when i ran it i get this error:
At the screenshot you can see file name and full path. Ok, we go in console:
Hey! But that file exists! I already tried that with relative path. Still not working.
P.S. Mono, Ubuntu 14.04
P.P.S. When i remove UseShellExecute = false my file is opening in gedit.
P.P.P.S. File is 100% exists:
var fi = new FileInfo(Path.Combine("Build." + this.name, "file.bin"));
Console.Out.WriteLine(fi.Exists); //true
From the documentation for ProcessStartInfo.UseShellExecute
"When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used only by the process that is started and has meaning only within the context of the new process. When UseShellExecute is false, the FileName property must be a fully qualified path to the executable."

Launching a batch file

I have the following code:
String Antcbatchpath = #"C:\GUI\antc.bat";
System.Diagnostics.Process runantc = new System.Diagnostics.Process();
runantc.StartInfo.FileName = Antcbatchpath;
runantc.StartInfo.UseShellExecute = false;
runantc.StartInfo.RedirectStandardOutput = true;
runantc.StartInfo.RedirectStandardError = true;
runantc.Start();
Will this load the batch file from C:\GUI\antc.bat?
Or runantc.StartInfo.FileName is only for a root directory? Root directory is where the application is located
EDIT 1:
hi instead of #"C:\GUI\antc.bat" i have a path:
String Antcbatchpath =#"C:\GUI Lab Tools\Build Machine\antc.bat";
which essentially contains white spaces. will it affect the runantc.StartInfo.Filename = Antcbatchpath; ?
UseShellExecute = true should do it.
Alternatively, if you need redirection, use:
runantc.StartInfo.FileName = "CMD.EXE";
runantc.StartInfo.Arguments = "/C " + Antcbatchpath;
You can try to set WorkingDirectory to prevent any ambiguity, but in my experience, it is not necessary.
The problem you're having is because antc.bat is not an executable. It requires UseShellExecute to be true, but that would prevent you from redirecting the output. I guess you will have to choose either one.

Categories

Resources