-
-
Notifications
You must be signed in to change notification settings - Fork 810
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
Remove merged branches #159
Conversation
Verb 'Remove' was used instead of 'Delete' because it is approved verb: http://msdn.microsoft.com/en-us/library/ms714428%28v=vs.85%29.aspx. There are three cmdlets: * Remove-MergedLocalGitBranches -- removes local merged branches. * Remove-MergedRemoteGitBranches -- removes remote merged branches. * Remove-MergedGitBranches -- removes both local and remote merged branches. This is draft so there are no settings like target branch or remote repository name or list of branches to ignore.
Default list is master and develop.
Terribly sorry for the delay in getting to a review of this (:new: :baby:) - this is a great start! Specific feedback incoming... |
GitRemoveMergedBranches.ps1
Outdated
!$branch.StartsWith("*") | ||
} | ||
|
||
function TrimRemote-GitBranch($branch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe switch this to a filter
so you can replace ... | foreach {TrimRemote-GitBranch $_} | ...
with ... | TrimRemote-GitBranch | ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it looks better now (e093459).
General question: would it be useful for any/all of these to accept a parameter (defaulting to |
I have no strong opinion on this. As for me, it is unlikely I will ever need this parameter. |
I found a bug.
If we have two remotes then merged branches will be deleted in the
I'll fix it on this week. |
GitRemoveMergedBranches.ps1
Outdated
} | ||
} | ||
|
||
function Get-MergedLocalGitBranches($ignore) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop unused $ignore
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$ignore
was completely removed in 47e6c16.
Revisions from six months ago (ugh) look great. Looking forward to the fix for multiple remotes! Would be simpler/cleaner to export a single |
I haven't used it in the last 6 month. This functionality is likely redundant.
"Remote" switches between remote and local branches like `git branch --remote`. With this switch we don't need to export `Remove-MergedLocalGitBranches` and `Remove-MergedRemoteGitBranches` so we export only `Remove-MergedGitBranches` now. "All" switches between both remote and local branches and local only.
Default value is "origin". Also, it is not required to pass PSBoundParameters. Is works automatically.
@dahlbyk Your idea about Bug with multiple remotes fixed in 98956d1. I've tested different combinations of parameters but not in depth. I used three repositories:
I used this script to create this test rig: https://gist.github.com/tkirill/6dd2606d1ae8b9b1e0f5. Also, I reformatted GitRemoveMergedBranches.ps1 in 7cf9803 basing on the coding style of this project. |
Conflicts: GitPrompt.ps1
GitRemoveMergedBranches.ps1
Outdated
foreach ($item in $branches) { | ||
if ($PSCmdlet.ShouldProcess($item, "delete remote branch")) { | ||
Write-Host "Deleting remote branch $item..." | ||
git push $RemoteName :$item |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No whitespace escaping for $RemoteName
but I think it is safe since it is not possible to use whitespaces in remote's name in Git. @dahlbyk What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, no need to handle whitespace in remote name.
On the Maybe someone more in tune with the Zen of PowerShell can chime in, but it feels like most of the |
@dahlbyk I added support for multiple remotes in 1b881b5. This is really convenient, good that you noticed this!
The only potential problem I can think of is read-only remote. If I fork some project on GitHub I have two remotes:
I think it is a frequent case and in this case working with all remotes by default doesn't work well. However, we definitely need ability to work with all remotes if we want. I thought to add flag
Flags
Surprisingly |
9287c11
to
23e23a2
Compare
Default behavior is to delete remote branches in origin only.
23e23a2
to
1b881b5
Compare
Remote operations being more "destructive" than their local counterparts, how about we remove any sort of default remote behavior?
|
@dahlbyk I like this idea! 👍 We have only two flags To be honest I don't find removing merged branches in P.S. You wrote |
Yes, mistyped. Remove is cool. Dangerousness likely depends on team size and whether or not you use forks - I've dealt with work people doing a "matching" |
Now there is no default set of remotes. You have to specify list of remotes manually. There is `-Local` switch. If you want to remove only remote branches you can pass only `-Remote string[]`. If you want to remove local branches too you can pass both parameters: `-Local -Remote string[]`.
@dahlbyk Take a look at new arguments please 27e75e5. Surprisingly validation for empty set of remotes' names works automatically. I didn't found this in documentation. I wrote some tests and this test finds another bug with remote branches:
It will remove I tried to fix this bug in 0f51d0d by checking remote branches against remote-tracking branch corresponding to current branch but this is not enough for the case of multiple remotes. So I change the way we work with remote branches (again 😕). How it works now:
Working with remotes is more difficult than I thought 😕 Maybe I just don't see some simple way... What do you think about this new idea? |
…edGitBranches. If branch merged in remote repository but not merged locally we should remove only remote branch. After you remove remote branches you should pull changes from remote so the branch becomes merged locally.
Now cmdlet requests target branch in `-RemoteBranch` parameter. It checks merged branches against target in the same remote as target branch.
2e6b470
to
74a3cf5
Compare
Now after few days I doubt the new way is better. Your suggestion about |
I'm not really a fan of doing any sort of remote manipulation based on local names because upstream branches don't necessarily have to track a local branch of the same name. I suppose you could remove remote branches that have been merged into the remote branch tracked by the current local branch, if applicable, but that feels like a stretch? Since we don't specify a reference to the commit we're checking is merged, I can really only think of two reasonable behaviors for
Between the two I lean toward the latter, with an error if More generally, earlier I asked:
Especially for the |
This parameter identify the ref to check mergedness against.
e016be9
to
7658e2f
Compare
@dahlbyk I tried to implement
I write it as warning since it is not fatal. What do you think? This is how it looks: Also, I added a test with multiple remotes and custom target branch. These cases are currently tested:
|
No time to review code now, but API seems just right and test coverage is impressive! Will try to revisit over the weekend - bug me next week if I don't. Thanks for iterating on this! |
@dahlbyk 👋 |
Just discovered one more corner case. If we have branch which points at the same commit as I don't know if we could fix this since the same situation can occur with fast-forward merge. Also, after few month of using current |
Closing this in favor of PR #663. |
Add the cmdlets for removing merged branches:
Implementation placed in the separate file GitRemoveMergedBranches.ps1 although most of other exported cmdlets sit in GitUtils.ps1. Hope it's not a code style rule to place everything inside GitUtils.ps1.
This is how these cmdlets work.
Remove-MergedLocalGitBranches
git branch --merged
.git branch -d $item
.Remove-MergedRemoteGitBranches
Works same as Remove-MergedLocalGitBranches except three things:
git branch -r --merged
.<remote-name>/
part from a beginning of each line.git push origin :$item
.Remove-MergedGitBranches
This one simply calls previous two cmdlets.
All three cmdlets support -WhatIf and -Confirm (link1, link2). Also, they all support -Ignore parameter for overriding list of ignored branches.
I haven't tested it enough yet to my shame but I think it is a good idea to create PR to receive feedback as soon as possible.
fixes #79