Is there a way to automate input in git-cmd without actually typing in the input command? Let's say write the input to a script to run automatically?
start git.cmd
connect to git server.
clone git repository.
input password.
I have this exact same situation and I automated it using expect
#!/usr/bin/expect
set timeout 600
log_user 0
spawn git clone http://myuser#myserver/group/repo.git tmp/repo
expect "Password for 'http://myuser#myserver':"
send "mypassword\r"
expect eof
Key was setting timeout appropriately.
I see that expect for Windows is available.
Assuming "git-cmd" is the Windows command shell setup for Git then all you need to do is write a command script (or batch file). You can find general instructions here. It is worth noting that the Git bin folder will need to be in your path. The Git command to clone a repository is described here. As was mentioned in another answer you could write a program to spawn a command shell and run the command script, but that seems the long way around.
Hope that helps.
Related
With TFS 2015, we had made a vNext (VSTS) task that would find a selected file, substitute a token (version number is where it started,) wrote out the changes to the file, and checked the file in with a comment as to the nature of the change. It did this all through automation during the build process.
We are about to upgrade to DevOps 2020 and the TFS management tools from 2015 were deprecated, which in itself would be fine, however, we still need to automate these file changes during the build process, including the check-ins noting the nature of the change.
The old task fails miserably ported over directly. I have re-written the process as a C# console app project and planned to call it during a PowerShell script, but I am encountering a number of road blocks to this plan.
What I have done so far.
I have written a task.json for the VSTS task that accepts parameters that it passes to a PowerShell script.
I have written a PowerShell script that call a C# console application to both locate and change the tokens in the file(s) as specified by the task. After changing the file content, it overwrites the original file.
I appear to have two issues that, as yet, I am unable to solve.
I am expecting (if task parameters dictate,) to alter the pipeline environment variable $env:BUILD_BUILDNUMBER to a new value, from the C# code. I am using the following C# command using the third parameter expecting that this will allow the pipeline to see the changes it has made to the variable: Environment.SetEnvironmentVariable("BUILD_BUILDNUMBER", buildNumber, EnvironmentVariableTarget.Machine ); (I have also tried without the parameter and also User and Process, to no avail.) The variable will not 'set' for the pipeline to see outside of the console app.
I need to check the changes made in step 2 from the C# code, back into the code repository with a short comment. The initial 'Get' that the pipeline does calls tf, but I have not had the same success. I have found that if I call VS2019's copy of tf.exe or the agents copy in 'externals' on the build machine, whether from C# or from the later PowerShell script, I get "##[error]Unable to determine the workspace. You may be able to correct this by running 'tf workspaces /collection:TeamProjectCollectionUrl'" Needless to say, this later instruction to run tf workspaces does not help out.
I am hoping these things have simple solutions. I have searched for, but not found an API call to DevOps 2020 that will check in the code. Perhaps that does not exist. As for the Environment Variable, I am at a little bit of mystery as to why the EnvironmentVariableTarget.Machine is not working. I suspect it has something to do with the ##vso[] method not being called but I am unsure of how to pass out my findings from the console app (which also needs to return 0 for success, else failure,) to the PowerShell script that can change the variable more easily.
If there are any bright ideas out there about this, I would really appreciate the insight you might lend. I have been at this for a while and I am not sure what else to consider to make this work.
Using TFVC rather than Git, and a multitude of other little legacy technology items, makes working with the new tools a bit harder to figure out as it was in this case.
So my first problem item, updating the environment Build Number in the build tasks before the build starts. I was able to set the variable from my C# code, by opening a PowerShell process that takes an argument to update the variable with the ##vso[Build.UpdateBuildNumber] function in PowerShell. The C# code to do this looks like the following:
var ps = #$"C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe";
var script = $#"SetEnvironmentBuildNumber.ps1";
if (File.Exists( script )){
if ( File.Exists( ps ) )
{
await Task.Run( () =>
{
var scriptWithArguments = string.Concat( Path.Combine( Directory.GetCurrentDirectory(), script ), " -newBuildNumber ", newBuildNumber);
Process.Start( ps, scriptWithArguments );
} );
}
else
{
Console.WriteLine( $"PowerShell File Not found at: {ps}" );
}
}
else
{
Console.WriteLine( $"The current directory is: {Directory.GetCurrentDirectory()}");
Console.WriteLine($"Script File Not found at: {script}");
}
The PowerShell script in SetEnvronemntBuildNumber.ps1 looks like this:
[CmdletBinding()]
param(
[string][Parameter(Mandatory=$true)]$newBuildNumber
)
Write-Host( "Setting the new build number - " + $newBuildNumber )
Write-Host( "##vso[Build.UpdateBuildNumber]$newBuildNumber" )
As it turns out, this exercise helped me understand the Undetermined Workspace, my problem #2.
If you look at the if statements of my C# code, you'll note a check to see just what directory is the current working directory. Finding out that my new task changed the current working directory to the location of my Task and not the pipeline's working directory gave me reason to pause.
Even though, I fall out of the C# code, back into my PowerShell script, I am not looking at the solution's directory, I am looking at the Task directory. If I call tf from this view, there is no source code, and hence no way to determine the WorkSpace.
The solution was simple, there is a pipeline System environment variable that will get me back home, so to speak. Calling the PS Set-Location as below, just before I call tf vc checkin... allowed tf to find the Workspace and save my changes.
Set-Location $env:System_DefaultWorkingDirectory
This was all it took, all these tweaks were innocent enough, but very troublesome to diagnose without any formal training in the ways of the DevOps pipeline. I look forward to more 'fun' with DevOps, when we try to figure out how to get to Git from TFVC... I am sure not all our legacy will see such significant changes.
Thanks for the suggestions, and I hope this can help some other poor soul trapped in a legacy world and trying to get out of the hole.
Given two branches, feature/SomeWork and develop
How do I find the list of commits in feature/SomeWork that have not yet been merged to develop using libgit2sharp?
I am currently programmatically walking through each of the commits and checking to make sure they are present in the target branch
I am hoping there is a more straightforward and faster way of doing it
Note: I need to do this programmatically within my application using libgit2sharp, I know the way to do this via command line, but would prefer to avoid shelling out to process and reading output, etc. if possible, thank you
I think you're looking for the git cherry command:
git cherry -v develop feature/SomeWork
That command should list all the commits in feature/SomeWork that are not in develop.
This command should give you what is needed.
git log feature/SomeWork ^^develop --no-merges
Please note ^^ , one of ^ is used as escape character on windows.
As a fun little project, I am trying to use C# to operate the bash.exe provided by Git. I want the process to behave just as if I ran it in the Git Bash Application. By this, I mean I want to be able to execute command and get the output of said commands (i.e. if I enter the command "curl --version", I want to get the same output as the image here and be able to store it in a variable)
I have come very close to accomplishing this with the code here. However, with some commands, I find that the Process in C# never terminates. For example, if I try to execute the command "curl --help", I find the the Process never exits where "curl --version" did. As a quick hack, I figured out that I could fix this by changing the command to
curl --help >> output.txt
and then reading the .txt file. This does cause the command to exit and to write the correct output to the file, however, I don't like having to do this and I am sure there is a better solution to make commands of this sort exit properly. Thanks for the help!
I have c# program (.exe), I will give it to other people, and want that exe to work only from where it was run the first time, any copy should no not work.
How can I do it?
VERY THANKS
It is not possible. A copy being a precise copy will have no way of knowing that it is a copy and not the original. Therefore at each first run of a copy on any machine the game will start again.
The other approach you can think of is to use activation. Your program will talk to the server online and will report the first run, regardless of whether it was a copy or not. Then at each successive run the program will ask the server if it's been running on the same machine as the first time. If yes, it will run, if no, it will immediately exit.
For this to work, you will need to come up with some kind of machine signature that your program will generate and transmit to the server.
record the exe location in the registry when first launched. and check the location when it's launched next.
you can use CD and protect the CD from copying, and then you should allow your program to run from the CD drive only.
Use click once deployment, it will be difficult for simple user to find and copy from download chache, and than you can write crypted exe location somewhere in the registry and check it every time the program is run
A lot of things that you can do here.
All the suggestions above are good too. Another option is you can have a installer that generates a key that binds your application to the machine installed. Transferring to other machine won't work since it will have a different machine key signature.
sorry its an old thread. Why not read the hard drive serial number (which should be unique) sent it to a server as part of activation and make sure that all that is the same.
I would post a snippet, but I honestly have no idea what part of my code could possibly be doing this. The program is sizable, I don't want to make you all wade through it. What kinds of things could possibly be the cause of this? Everything works perfectly when called from the command prompt: "readoo.exe". But when I click the exe in its file. . . "readoo.exe has encountered a problem and needs to close. . ."
this is intended to eventually be a scheduled task -> i'm worried, will it work?
i've never debugged, all i've ever used is notepad. I am learning, and feel that this strengthens my understanding of a project.
it crashes nearly immediately. there are no shortcuts, though the file paths are relative.
trying this method: shortcut -> properties -> shortcut -> Start In. I don't have a "shortcut" option
my program reads log files, parses, and creates 4 new files based on the found content
Microsoft Error Report says file not found. But how can this be? the files are there, albeit relative.
Take a copy of your project, and then start hacking bits out of it. When it no longer crashes, you've removed the bit causing the problem.
At what point does it fail when you double-click on it? Immediately, or only when you take a certain action?
You could also add a lot of logging to it, which could indicate where the problem is too.
This is probably looking for a dll that it can't find or is finding a different version from what it wants.
You could try Process Monitor or Process Explorer from sysinternals to see what dlls it loads when it does work and where it finds them.
Try putting a System.Diagnostics.Debugger.Break()call as the first thing in Main() and you'll be asked to attach a debugger - this should definitely show you what is different betweent the 2 invocations.
I would start with identifying what is different in the two methods of execution. Is there a shortcut modifying anything?
The starting directory?
The execution account?
command line arguments?
There are 2 things that it could be:
The current directory could be different when you click on the program or run from the command prompt.
The settings and path could be different when you click on the programe you are using the standard command prompt, are you opening the visual studio command prompt when you run the program from the prompt.
If your application relies on some file that should be on the same path of that exe, that can occurr.
You will have to change the properties of the exe (or shortcut to the exe) to "Start In" the directory where your exe is. For a shortcut, right click on the shortcut -> properties -> shortcut -> Start In.
I guess that is what I think could be the cause.
EDIT: Add a Console.ReadLine towards the end of your code to make it pause for you to see any exception thrown. That should help when you run it using windows explorer.
Put a try/catch around your code and output the exception message to the console in the catch block. That should give you some clues.