Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get files changed since <branch> rather than just most recent commit #106

Open
FISHMANPET opened this issue May 30, 2019 · 2 comments
Open

Comments

@FISHMANPET
Copy link
Contributor

Digging deep into the past looking at #7, I've got a similar need but mine's slightly different. As the title suggests I want a way to not see changes in the current commit, but all the changes against a specific branch. The case is I want to be able to trigger a deploy to a test environment to deploy everything that's changed since but I'm not necessarily merging into a specific branch where all the changes will be present.

I think I can do this with git diff --name-only master... (for master) and that will give me the files that are ahead of master in my branch, but not the ones that are behind (git diff --name-only ...master would get everything where I'm behind master but not files ahead, the inverse).

Furthermore I'm only interested in certain types of changes, which I can get from either diff or diff-tree with the --diff-filter= option.

I don't know enough about git to really understand the difference between diff and diff-tree or to know that what I want to do is possible with diff-tree or what you're doing is possible with diff. But I could see expanding Get-GitChangedFile with a -DiffFilter parameter that could be passed to both commands, and then some options (-Since or possibly even more?) and if specified it would use diff instead of diff-tree but still do the filtering the command already does.

Or if there's some way to do both cases (changes in this commit, changes since ) with either diff or diff-tree then the code could just build that command based on passed parameters?

@FISHMANPET
Copy link
Contributor Author

Digging further, you can get the previous commit hash with git log --format=%P and then you can do git diff $previoushash...$currenthash -r --name-only (or just git diff $previoushash... -r --name-only), but I don't think there's any way to replicate what I did with git diff using git diff-tree.

@FISHMANPET
Copy link
Contributor Author

So I used the built in command in the master branch, thinking it would grab all the changes that got merged into master, but a merge into master that doesn't squash commits gives you a commit where nothing actually changes. So the command works in that it does exactly what it's designed to, get the changes in this commit, I'm not sure how much use that is to anyone.

So I know what my two use cases are. The first is described originally, git diff origin/master... to get changes in the current branch that are ahead of master, to predict what will be changed when my code gets pulled into master. Other people might have a similar use case but comparing against upstream/master if they're in a fork or origin/branch if their workflow involves merging into a branch named something other than master.

My second case is what I thought Get-GitChangedFile would do, in a build against master I want to be able to know what has changed. Since master is a protected branch the actual change is going to be a pull request that contains many commits so looking at a single commit doesn't help. So what I've determined actually works for my case is git diff HEAD~1..HEAD. I think that would also replicate what Get-GitChangedFile actually does if you're within a branch.

I guess at this point the reason I bring any of this up rather than just running those commands in my pipeline is that I want to be able to take advantage of Invoke-Git and the Exclude code in Get-GitChangedFile because otherwise I'm implementing it myself in my pipeline.

Having learned slightly more about parents (apparently commits can have multiple parents) my code above to reimplment current functionality with git diff wouldn't work but I believe this would, when called with no parameters: git diff <commit>~1...<commit>. I believe that would be the same if you use rev-parse to find the current commit or pass in a commit directly. So my proposal would be to modify the function to use git diff instead of git diff-tree and add two parameters, something like To and From, which, if specified, would use the ... notation (or an optional third parameter to specify if you want to use double dot or triple dot) with git diff but still run the code through Invoke-Git and the exclusion built in to Get-GitChangedFile.

So I'm gonna write that and see what happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant