programmatically find commits in one branch but not the other - c#

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.

Related

filtering of files and paths from gitignore

I would like to find all file paths that are not filtered by a .gitignore (or any nested .gitignore files within sub-directories) using C#. This is similar to the question here with regard to PHP. I'm wondering if someone knows if this code had already been made available (in C#) somewhere online.
UPDATE: To answer what I want this for, it is so I can run my own little periodic backup of my source files for certain projects (zipping the result), for added peace of mind. The hard part is getting a robust .gitignore parser to get the filtered file paths (and exclude the others), without wanting to become too embroiled in learning that spec if someone else already has done it for me.
Well, the best way to parse .gitignore files (and the other files Git uses, such as $GIT_DIR/info/exclude) is to get Git to do it for you. :-) (In your case, most cases in fact, this does involve executing a git subprocess.)
git check-ignore
The git check-ignore command can be used to detect which files are ignored and why. The --non-matching option makes it tell you about files that are not ignored as well, though since it still tells you about ignored files, too, and in a special format, you'll need to do a little bit of further work to get a simple list of non-ignored files. This Bourne shell function does the trick:
find_nonignored() {
find . -path ./.git -prune -o -print \
| git check-ignore --verbose --non-matching --stdin \
| sed -n -e 's,\t./,\t,' -e 's,^::\t*,,p' \
}
How It Works
The find command finds all files in and below the current working directory, which should be somewhere in the tree you're trying to filter. We exclude the top-level .git subdirectory and everything under it from the output, if present; /.git/ is not in a typical .gitignore file because Git ignores it automatically and thus is is normally considered "not ignored" by git check-ignore.
git check-ignore will print out --non-matching files only in --verbose mode because it's only in that mode where it prints out the extra information that would tell you if the file is ignored or not. (It always prints ignored files.) The paths come out one per line in the format
source:linenum:pattern<TAB>path
The colon-separated fields are information about what caused the path to be ignored (such as a line in the .gitignore file) and will be empty if the file is not ignored.
The sed command then filters the output to show only the paths of the ignored files. The -n option tells it not to print out the input lines by default. The first substitution pattern replaces <TAB>./ with just <TAB>, removing the leading ./, for purely aesthetic reasons. The second substitution does the real work, removing any ::<TAB> (indicating no "ignore" information) that starts a line and, if that substitution happened, printing what's left of the line which is a non-ignored path.
You can filter this further to do additional processing; I built this for a script that does markdown checking along these lines:
markdownlint $(find_nonignored | grep '\.md$')
Notes
This code includes untracked files (i.e., have never been added to the Git repo or staged) in the output, which is usually what you want. (Test systems, for example, should still check new files even before they've had git add run on them.) Beware that other solutions involving git ls-files and the like usually don't do this.
The above code relies on using GNU sed, which interprets \t as a tab. If you're using BSD sed (such as on MacOS) you probably need to tweak this slightly. Check the comments to see if someone has a hint for this.
All the code here breaks on paths with spaces or other "unusual" characters; it needs to be modified in several places (such as using -print0 with find) to fix this. I do not address issues like this here in order to keep the explanation simple. I also leave for others the generalization of the function to work on arbitrary paths rather than just the current working directory.
It's difficult to make suggestions without knowing exactly what you want to do with the list (use it in a build script, process the files in some way, just view them on a UI, etc.)
I couldn't find one in C#, but this JavaScript gitignore parser doesn't have a lot of code to convert and it exposes both an accepts and a denies method to get a list of included or ignored files. It is fairly well documented, has tests, and the regular expressions it uses would work just as well in C# as they do in JavaScript.
This answer would work from C#, provided you have Git installed on the machine where your C# code is running.
Also note that the Git Source Control Provider plugin for Visual Studio provides the list right in the IDE, along with the ability to check boxes and commit certain files together and a lot of other functionality that is difficult to do on the command line.
NOTE: The Git Source Control Provider is open source (written in C#) and you can view the source here, but it may be much more involved to reverse engineer than the JavaScript project.
For those looking for a C# library, you can check this out as well.
.gitignore based parser implemented in C# according to the .gitignore spec 2.29.2. The library is tested against real git status outputs. The tests use LibGit2Sharp for that.
https://github.com/goelhardik/ignore
It's kind of a port of other open source libraries and so far looks like it works well for my other projects.

Automate commands in git cmd?

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.

Remove some commits in GIT

I am new to Git, although I just managed to change file in my previous commit.
First commit in redbox let's call it R1 is the one with commited password. I made it accidentally, so I wanted to make change - just change the file where that password was. I managed to do it successfully, not from the first time.
So I the commits the "path" to my head - last commit is the green box. These commits are same as the red one, but without the password.
So everything is as I wanted it to be, but I want to remove the 3 commits that are in a red box, cause password is still there.
Can you tell me how to do it?
I understand that this might be a duplicate, but I have a picture so maybe it will help to understand better.
It looks like what you may be looking for is $ git rebase -i which will bring up the interactive mode for rebasing your commits. In interactive mode you can edit, squash, discard previous commits. This is a previous discussion about using interactive rebasing. source
This should be enough to help you clear away the previous commits that contain the details that shouldn't be public.
In the future you can create a .gitignore file that will tell git to ignore certain files, which helps greatly with privacy and security issues. You can find more about .gitignore files here.
In the future if this occurs you can easily erase a commit by using git reset --hard HEAD~1 which will bring you back to the commit before the head.
You can use git log at any time to find a specific id of a commit that you want to jump back to with git reset --hard <sha1-commit-id>, but be advised this will delete the changes at your local working level.

anksvn checkin file

I am using the most recent version of anksvn for a visual studio 2008 project file. I now
want to check this code into anksvn, but I am having a problem.
The situtation is, I checked in the most current version of code into anksvn. That is fine.
However I have another version of this code that I did not check out from subversion initially. This other copy of the code was for a 'demo' only. However now this code needs to become the production code. Thus I am trying to determine how to check this code into anksvn.
What I know I can do is to 'remove' the most curent code folder that is in anksvn. I could then place this project folder into that location. since the origianl 'demo' code also includes the current production code.
However I am trying to see if there is a better method to accomplish this goal. Could I possibly use the branch/switch option?
Is the demo code checked out of Subversion at all? I know you didn't check it out, but was it checked out? If it was, you could commit this code back into Subversion, then update your working directory.
It his code has nothing to do with Subversion, you will have to take a more complex route: You will have to copy the changes manually to your code.
Since you're using Windows, you should take a look at Beyond Compare, This is commercial code, but you can download a limited time demo for free -- more than enough time to handle your situation. I use Beyond Compare all the time to compare two different directories or Java jar files or zip archives, etc. It not only can quickly show you the differences, but makes it each to copy those differences from one to the other.
I have no relationship with Scooter software, the makers of Beyond Compare except as a customer.

Visual SVN - How to maintain separate user settings and publish settings?

For a new MVC web development project, I'm collaborating with a couple of other developers and we want to use Visual SVN to manage source control.
Following the "Getting Started" instructions at the VisualSVN website (http://www.visualsvn.com/visualsvn/getting-started/) seems to to commit everything within the Solution folder including all the settings file (.suo, user, .Publish.Xml)
However, we want to maintain separate Publish Settings within Visual Studio as we publish to our local machines for testing.
Is that possible?
P.S. Shouldn't VisualSVN Client automatically ignore the .suo and .user files?
it doesn't you'll need to either
add them to the ignore on commit lists - you can do this while committing but its a per user setting
remove them from svn - delete them from svn using tortoise as visual svn cant see them (take copies first, as I think this will actually delete them), commit the delete. Put them back into the folder and commit again, svn will show up these files as uncommited, right click on them and select ignore in the commit window, and commit them, this will apply to everyone. Its easier to not commit them in the first place :)
I use SVN as my source control as well. I also use VisualSVN (but only server side). The main thing I would suggest is to use VisualSVN to host your repositories, but use something else to commit/update/checkout your repositories to your local machine.
I would suggest TortoiseSVN for this. Use TortoiseSVN to control your workflow on local machines. You can then use it to simply right-click/ignore your *.suo files. Or any other files/folders you wish to keep out of the repository!
It may take a bit of research to get it setup. But this is what I use on an every day basis, and it is very user friendly.
I've never used VisualSVN, but I would be surprised...no shocked if what you said was true.
Does VisualSVN really by default automatically add and commit user files? You'd think a solution that's built for VisualStudio would simply know better. I would call the company and verify this.
If VisualStudio does commit local user files, I would recommend that you use AnkhSVN instead.
Not only does AnkhSVN know better than to commit user files, it's also open source and you can save yourself the $49 per user you need for VisualSVN. And, it's not just the $50 you're paying per user that you pay with VisualSVN either. It's also the fact that you have another license you need to track while users come in and leave the project. Who do you think is going to get that fun job?
However, if you must use VisualSVN, and VisualSVN does commit user local files by default, You need to get my kitchen sink pre-commit hook. One of the things it does is allow you to completely ban the addition of files such as Visual Studio's *.csuser` files and the other types of VisualStudio detritus.
Of course, you should let developers know how they can set global-ignores and autoproperties in Subversion. This will prevent them from accidentally adding them. But, there's no way you can configure that globally, or to prevent someone from purposefully adding them. Only my pre-commit hook can keep them out of your repository. After a few failed commits because your developers tried to add in these private user files, your developers will quickly fall into line and set up their global-ignores.

Categories