git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* check if one branch contains another branch
@ 2020-05-07 22:59 Alexander Mills
  2020-05-07 23:08 ` Junio C Hamano
  2020-05-07 23:31 ` Randall S. Becker
  0 siblings, 2 replies; 12+ messages in thread
From: Alexander Mills @ 2020-05-07 22:59 UTC (permalink / raw)
  To: git

I am looking for a command:

1>  if branch x contains branch y

right now all I can find is

2> if current branch contains commit y

can someone please accomplish #1 ?
Crazy hard to find an answer to #1 online.
The user case is to delete old local branches by comparing them with
the remote integration branch.

more info if needed:
https://stackoverflow.com/questions/61669056/how-to-determine-if-integration-branch-contains-feature-branch

-alex

--
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 22:59 check if one branch contains another branch Alexander Mills
@ 2020-05-07 23:08 ` Junio C Hamano
  2020-05-07 23:12   ` Alexander Mills
  2020-05-07 23:31 ` Randall S. Becker
  1 sibling, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2020-05-07 23:08 UTC (permalink / raw)
  To: Alexander Mills; +Cc: git

Alexander Mills <alexander.d.mills@gmail.com> writes:

> I am looking for a command:
>
> 1>  if branch x contains branch y
>
> right now all I can find is
>
> 2> if current branch contains commit y
>
> can someone please accomplish #1 ?

Study "git merge-base --is-ancestor" perhaps?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:08 ` Junio C Hamano
@ 2020-05-07 23:12   ` Alexander Mills
  2020-05-07 23:15     ` Alexander Mills
  2020-05-07 23:24     ` brian m. carlson
  0 siblings, 2 replies; 12+ messages in thread
From: Alexander Mills @ 2020-05-07 23:12 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

so it would be:

feature_branch='my_branch'  # the branch that I want to ensure is
completely merged into origin/dev
git merge-base  origin/dev --is-ancestor "$feature_branch"

that won't work?  since git merge-base only works with current branch?

On Thu, May 7, 2020 at 4:08 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Alexander Mills <alexander.d.mills@gmail.com> writes:
>
> > I am looking for a command:
> >
> > 1>  if branch x contains branch y
> >
> > right now all I can find is
> >
> > 2> if current branch contains commit y
> >
> > can someone please accomplish #1 ?
>
> Study "git merge-base --is-ancestor" perhaps?



-- 
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:12   ` Alexander Mills
@ 2020-05-07 23:15     ` Alexander Mills
  2020-05-07 23:24     ` brian m. carlson
  1 sibling, 0 replies; 12+ messages in thread
From: Alexander Mills @ 2020-05-07 23:15 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

I tried this:

git checkout origin/dev
# now in detached head state
git branch --contains "$(git rev-parse "$feature_branch")"

and I got this output:
<name of feature branch>

not sure what that means

On Thu, May 7, 2020 at 4:12 PM Alexander Mills
<alexander.d.mills@gmail.com> wrote:
>
> so it would be:
>
> feature_branch='my_branch'  # the branch that I want to ensure is
> completely merged into origin/dev
> git merge-base  origin/dev --is-ancestor "$feature_branch"
>
> that won't work?  since git merge-base only works with current branch?
>
> On Thu, May 7, 2020 at 4:08 PM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > Alexander Mills <alexander.d.mills@gmail.com> writes:
> >
> > > I am looking for a command:
> > >
> > > 1>  if branch x contains branch y
> > >
> > > right now all I can find is
> > >
> > > 2> if current branch contains commit y
> > >
> > > can someone please accomplish #1 ?
> >
> > Study "git merge-base --is-ancestor" perhaps?
>
>
>
> --
> Alexander D. Mills
> New cell phone # (415)730-1805
> linkedin.com/in/alexanderdmills



-- 
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:12   ` Alexander Mills
  2020-05-07 23:15     ` Alexander Mills
@ 2020-05-07 23:24     ` brian m. carlson
  2020-05-07 23:28       ` Alexander Mills
  1 sibling, 1 reply; 12+ messages in thread
From: brian m. carlson @ 2020-05-07 23:24 UTC (permalink / raw)
  To: Alexander Mills; +Cc: Junio C Hamano, git

[-- Attachment #1: Type: text/plain, Size: 644 bytes --]

On 2020-05-07 at 23:12:09, Alexander Mills wrote:
> so it would be:
> 
> feature_branch='my_branch'  # the branch that I want to ensure is
> completely merged into origin/dev
> git merge-base  origin/dev --is-ancestor "$feature_branch"
> 
> that won't work?  since git merge-base only works with current branch?

No, that's not the case.  You can write this:

  git merge-base --is-ancestor $feature_branch origin/dev

This works from any branch and exits 0 if the branch is in origin/dev
and 1 if it is not.  In neither case does it print anything.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 263 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:24     ` brian m. carlson
@ 2020-05-07 23:28       ` Alexander Mills
  2020-05-07 23:47         ` brian m. carlson
  0 siblings, 1 reply; 12+ messages in thread
From: Alexander Mills @ 2020-05-07 23:28 UTC (permalink / raw)
  To: brian m. carlson, Alexander Mills, Junio C Hamano, git

Thanks will check that out.

This command does not seem to work :(

> git branch --contains branchB

I do this:

git checkout branchB
git commit --allow-empty -am 'empty commit message'
git checkout dev
git branch --contains branchB  ==> exit code 0
git branch --contains $(git rev-parse branchB)  ==> exit code 0

this seems like a bug or something.  Why wouldn't it exit with 1,
since it obviously does not contain that commit?
This kinda sucks tbh :(





On Thu, May 7, 2020 at 4:25 PM brian m. carlson
<sandals@crustytoothpaste.net> wrote:
>
> On 2020-05-07 at 23:12:09, Alexander Mills wrote:
> > so it would be:
> >
> > feature_branch='my_branch'  # the branch that I want to ensure is
> > completely merged into origin/dev
> > git merge-base  origin/dev --is-ancestor "$feature_branch"
> >
> > that won't work?  since git merge-base only works with current branch?
>
> No, that's not the case.  You can write this:
>
>   git merge-base --is-ancestor $feature_branch origin/dev
>
> This works from any branch and exits 0 if the branch is in origin/dev
> and 1 if it is not.  In neither case does it print anything.
> --
> brian m. carlson: Houston, Texas, US
> OpenPGP: https://keybase.io/bk2204



-- 
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: check if one branch contains another branch
  2020-05-07 22:59 check if one branch contains another branch Alexander Mills
  2020-05-07 23:08 ` Junio C Hamano
@ 2020-05-07 23:31 ` Randall S. Becker
  2020-05-07 23:37   ` Alexander Mills
  1 sibling, 1 reply; 12+ messages in thread
From: Randall S. Becker @ 2020-05-07 23:31 UTC (permalink / raw)
  To: 'Alexander Mills', git

On May 7, 2020 6:59 PM Alexander Mills, Wrote:
> To: git@vger.kernel.org
> Subject: check if one branch contains another branch
> 
> I am looking for a command:
> 
> 1>  if branch x contains branch y
> 
> right now all I can find is
> 
> 2> if current branch contains commit y
> 
> can someone please accomplish #1 ?
> Crazy hard to find an answer to #1 online.
> The user case is to delete old local branches by comparing them with the
> remote integration branch.
> 
> more info if needed:
> https://stackoverflow.com/questions/61669056/how-to-determine-if-
> integration-branch-contains-feature-branch

Looking at this slightly differently, if you try to delete a branch, git branch -d feature-branch, and the branch has not been merged, then the delete will fail. A simple way of looking at it is if the HEAD of the branch has no successor commits then it is not merged (not 100% decisive, but git branch -d is). It is not really that a branch has been merged, but that a commit has successors, meaning that it has been merged. However, unless you are using GitLab, a git merge --squash will not answer your question even if the branch was merged.

A better way of looking at this is in terms of Pull (GitHub, BitBucket) or Merge (GitLab) requests. Has there been a Pull Request for a branch and has the branch been closed? Meaning that when you do a git fetch --prune, your merged/deleted branches go away unless you are on that branch. Looking at the Pull Request history is much more useful in determining whether a branch has been integrated into a main development branch or production branch in a GitFlow process.

It is a different way of looking at the problem, but IMHO, a more representative way when taking developers and deployment into account.

Regards,
Randall


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:31 ` Randall S. Becker
@ 2020-05-07 23:37   ` Alexander Mills
  2020-05-07 23:41     ` Junio C Hamano
  2020-05-08  1:01     ` Alexander Mills
  0 siblings, 2 replies; 12+ messages in thread
From: Alexander Mills @ 2020-05-07 23:37 UTC (permalink / raw)
  To: Randall S. Becker; +Cc: git

I assume that:

git branch -d  xxx

will prevent a delete if the current branch doesn't contain xxx..
I don't want to have to checkout  origin/dev in order to run that command,
that's one part of the problem


On Thu, May 7, 2020 at 4:31 PM Randall S. Becker <rsbecker@nexbridge.com> wrote:
>
> On May 7, 2020 6:59 PM Alexander Mills, Wrote:
> > To: git@vger.kernel.org
> > Subject: check if one branch contains another branch
> >
> > I am looking for a command:
> >
> > 1>  if branch x contains branch y
> >
> > right now all I can find is
> >
> > 2> if current branch contains commit y
> >
> > can someone please accomplish #1 ?
> > Crazy hard to find an answer to #1 online.
> > The user case is to delete old local branches by comparing them with the
> > remote integration branch.
> >
> > more info if needed:
> > https://stackoverflow.com/questions/61669056/how-to-determine-if-
> > integration-branch-contains-feature-branch
>
> Looking at this slightly differently, if you try to delete a branch, git branch -d feature-branch, and the branch has not been merged, then the delete will fail. A simple way of looking at it is if the HEAD of the branch has no successor commits then it is not merged (not 100% decisive, but git branch -d is). It is not really that a branch has been merged, but that a commit has successors, meaning that it has been merged. However, unless you are using GitLab, a git merge --squash will not answer your question even if the branch was merged.
>
> A better way of looking at this is in terms of Pull (GitHub, BitBucket) or Merge (GitLab) requests. Has there been a Pull Request for a branch and has the branch been closed? Meaning that when you do a git fetch --prune, your merged/deleted branches go away unless you are on that branch. Looking at the Pull Request history is much more useful in determining whether a branch has been integrated into a main development branch or production branch in a GitFlow process.
>
> It is a different way of looking at the problem, but IMHO, a more representative way when taking developers and deployment into account.
>
> Regards,
> Randall
>


-- 
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:37   ` Alexander Mills
@ 2020-05-07 23:41     ` Junio C Hamano
  2020-05-08  1:01     ` Alexander Mills
  1 sibling, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2020-05-07 23:41 UTC (permalink / raw)
  To: Alexander Mills; +Cc: Randall S. Becker, git

Alexander Mills <alexander.d.mills@gmail.com> writes:

> I assume that:
>
> git branch -d  xxx
>
> will prevent a delete if the current branch doesn't contain xxx..
> I don't want to have to checkout  origin/dev in order to run that command,
> that's one part of the problem

In that case, don't you want to learn, not just if your 'dev' has
anything that is not merged/accepted by origin/dev, what commits
in 'dev' are not yet merged in origin/dev?

 $ git log origin/dev..dev

would show them.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:28       ` Alexander Mills
@ 2020-05-07 23:47         ` brian m. carlson
  0 siblings, 0 replies; 12+ messages in thread
From: brian m. carlson @ 2020-05-07 23:47 UTC (permalink / raw)
  To: Alexander Mills; +Cc: Junio C Hamano, git

[-- Attachment #1: Type: text/plain, Size: 1890 bytes --]

On 2020-05-07 at 23:28:40, Alexander Mills wrote:
> Thanks will check that out.
> 
> This command does not seem to work :(
> 
> > git branch --contains branchB
> 
> I do this:
> 
> git checkout branchB
> git commit --allow-empty -am 'empty commit message'
> git checkout dev
> git branch --contains branchB  ==> exit code 0
> git branch --contains $(git rev-parse branchB)  ==> exit code 0
> 
> this seems like a bug or something.  Why wouldn't it exit with 1,
> since it obviously does not contain that commit?

Because that command operates differently.  From git-branch(1):

  With --contains, shows only the branches that contain the named commit
  (in other words, the branches whose tip commits are descendants of the
  named commit)…..

git branch --contains branchB prints only branchB, because no other
branch contains it.  In other words, this asks to list the branches
which contain the specified commit, and as long as it has done so
successfully (even if that answer is "none of them"), it exits 0.  The
current branch has no effect on it because it's listing branches which
match a criterion, not comparing the specified revision to the current
branch.

Note that this is not a porcelain command (that is, it is not intended
for scripting) and need not be especially performant.  git merge-base
--is-ancestor is the better way if you want to script things or get a
more performant answer because it does less work, especially if there
are many branches.

> This kinda sucks tbh :(

Certainly it fails to do the thing you wanted to do with it, but since
that isn't what it's documented to do, I don't see that as a particular
problem.

Is the documentation unclear in some way or could it be more helpful?
If so, please tell us so we can improve it.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 263 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: check if one branch contains another branch
  2020-05-07 23:37   ` Alexander Mills
  2020-05-07 23:41     ` Junio C Hamano
@ 2020-05-08  1:01     ` Alexander Mills
  2020-05-08 12:07       ` Randall S. Becker
  1 sibling, 1 reply; 12+ messages in thread
From: Alexander Mills @ 2020-05-08  1:01 UTC (permalink / raw)
  To: Randall S. Becker; +Cc: git

git branch -d foo   # safe delete

the above doesn't seem to work if you squash commits into an
integration branch, I will get something like:

========================================================
 git branch -d CP-10-master
warning: deleting branch 'CP-10-master' that has been merged to
         'refs/remotes/origin/CP-10-master', but not yet merged to HEAD.
Deleted branch CP-10-master (was faeb801).
==========================================================


If a branch gets squashed into another, how does merge-base do it's
thing, since it's not a fast-forward merge etc?
I assume that it's not possible, for example this script shows that if
 git merge --squash is used, git doesn't know that the unsquashed
branch is already merged:

==========================================================

#!/bin/bash

set -e

git checkout master
git branch -D delete-me-1 || echo
git branch -D delete-me-2 || echo

git checkout -b delete-me-1
git checkout -b delete-me-2
git commit --allow-empty -am "first new one"
git commit --allow-empty -am "second new one"
git commit --allow-empty -am "third new one"
git checkout delete-me-1
git merge --squash delete-me-2  ### compare without squash

if git merge-base --is-ancestor delete-me-2 delete-me-1; then
  echo 'delete-me-1 is an ancestor of delete-me-2'
else
  echo 'not an ancestor'
fi
========================================================

that will print "not an ancestor" if  --squash is used..

-alex




On Thu, May 7, 2020 at 4:37 PM Alexander Mills
<alexander.d.mills@gmail.com> wrote:
>
> I assume that:
>
> git branch -d  xxx
>
> will prevent a delete if the current branch doesn't contain xxx..
> I don't want to have to checkout  origin/dev in order to run that command,
> that's one part of the problem
>
>
> On Thu, May 7, 2020 at 4:31 PM Randall S. Becker <rsbecker@nexbridge.com> wrote:
> >
> > On May 7, 2020 6:59 PM Alexander Mills, Wrote:
> > > To: git@vger.kernel.org
> > > Subject: check if one branch contains another branch
> > >
> > > I am looking for a command:
> > >
> > > 1>  if branch x contains branch y
> > >
> > > right now all I can find is
> > >
> > > 2> if current branch contains commit y
> > >
> > > can someone please accomplish #1 ?
> > > Crazy hard to find an answer to #1 online.
> > > The user case is to delete old local branches by comparing them with the
> > > remote integration branch.
> > >
> > > more info if needed:
> > > https://stackoverflow.com/questions/61669056/how-to-determine-if-
> > > integration-branch-contains-feature-branch
> >
> > Looking at this slightly differently, if you try to delete a branch, git branch -d feature-branch, and the branch has not been merged, then the delete will fail. A simple way of looking at it is if the HEAD of the branch has no successor commits then it is not merged (not 100% decisive, but git branch -d is). It is not really that a branch has been merged, but that a commit has successors, meaning that it has been merged. However, unless you are using GitLab, a git merge --squash will not answer your question even if the branch was merged.
> >
> > A better way of looking at this is in terms of Pull (GitHub, BitBucket) or Merge (GitLab) requests. Has there been a Pull Request for a branch and has the branch been closed? Meaning that when you do a git fetch --prune, your merged/deleted branches go away unless you are on that branch. Looking at the Pull Request history is much more useful in determining whether a branch has been integrated into a main development branch or production branch in a GitFlow process.
> >
> > It is a different way of looking at the problem, but IMHO, a more representative way when taking developers and deployment into account.
> >
> > Regards,
> > Randall
> >
>
>
> --
> Alexander D. Mills
> New cell phone # (415)730-1805
> linkedin.com/in/alexanderdmills



--
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: check if one branch contains another branch
  2020-05-08  1:01     ` Alexander Mills
@ 2020-05-08 12:07       ` Randall S. Becker
  0 siblings, 0 replies; 12+ messages in thread
From: Randall S. Becker @ 2020-05-08 12:07 UTC (permalink / raw)
  To: 'Alexander Mills'; +Cc: git

On May 7, 2020 9:01 PM, Alexander Mills Wrote:
> git branch -d foo   # safe delete
> 
> the above doesn't seem to work if you squash commits into an integration
> branch, I will get something like:
> 
> ========================================================
>  git branch -d CP-10-master
> warning: deleting branch 'CP-10-master' that has been merged to
>          'refs/remotes/origin/CP-10-master', but not yet merged to HEAD.
> Deleted branch CP-10-master (was faeb801).
> ==========================================================
> 
> 
> If a branch gets squashed into another, how does merge-base do it's thing,
> since it's not a fast-forward merge etc?
> I assume that it's not possible, for example this script shows that if  git merge
> --squash is used, git doesn't know that the unsquashed branch is already
> merged:
> 

Correct. That is what I pointed out in my first response.

> ==========================================================
> 
> #!/bin/bash
> 
> set -e
> 
> git checkout master
> git branch -D delete-me-1 || echo
> git branch -D delete-me-2 || echo
> 
> git checkout -b delete-me-1
> git checkout -b delete-me-2
> git commit --allow-empty -am "first new one"
> git commit --allow-empty -am "second new one"
> git commit --allow-empty -am "third new one"
> git checkout delete-me-1
> git merge --squash delete-me-2  ### compare without squash
> 
> if git merge-base --is-ancestor delete-me-2 delete-me-1; then
>   echo 'delete-me-1 is an ancestor of delete-me-2'
> else
>   echo 'not an ancestor'
> fi
> ========================================================
> 
> that will print "not an ancestor" if  --squash is used..

Also correct.

> 
> -alex
> 
> 
> 
> 
> On Thu, May 7, 2020 at 4:37 PM Alexander Mills
> <alexander.d.mills@gmail.com> wrote:
> >
> > I assume that:
> >
> > git branch -d  xxx
> >
> > will prevent a delete if the current branch doesn't contain xxx..
> > I don't want to have to checkout  origin/dev in order to run that
> > command, that's one part of the problem
> >
> >
> > On Thu, May 7, 2020 at 4:31 PM Randall S. Becker
> <rsbecker@nexbridge.com> wrote:
> > >
> > > On May 7, 2020 6:59 PM Alexander Mills, Wrote:
> > > > To: git@vger.kernel.org
> > > > Subject: check if one branch contains another branch
> > > >
> > > > I am looking for a command:
> > > >
> > > > 1>  if branch x contains branch y
> > > >
> > > > right now all I can find is
> > > >
> > > > 2> if current branch contains commit y
> > > >
> > > > can someone please accomplish #1 ?
> > > > Crazy hard to find an answer to #1 online.
> > > > The user case is to delete old local branches by comparing them
> > > > with the remote integration branch.
> > > >
> > > > more info if needed:
> > > > https://stackoverflow.com/questions/61669056/how-to-determine-if-
> > > > integration-branch-contains-feature-branch
> > >
> > > Looking at this slightly differently, if you try to delete a branch, git branch
> -d feature-branch, and the branch has not been merged, then the delete will
> fail. A simple way of looking at it is if the HEAD of the branch has no
> successor commits then it is not merged (not 100% decisive, but git branch -d
> is). It is not really that a branch has been merged, but that a commit has
> successors, meaning that it has been merged. However, unless you are using
> GitLab, a git merge --squash will not answer your question even if the branch
> was merged.
> > >
> > > A better way of looking at this is in terms of Pull (GitHub, BitBucket) or
> Merge (GitLab) requests. Has there been a Pull Request for a branch and has
> the branch been closed? Meaning that when you do a git fetch --prune, your
> merged/deleted branches go away unless you are on that branch. Looking at
> the Pull Request history is much more useful in determining whether a branch
> has been integrated into a main development branch or production branch in
> a GitFlow process.
> > >
> > > It is a different way of looking at the problem, but IMHO, a more
> representative way when taking developers and deployment into account.

As I indicated, this may be more appropriate to do what you want to do on the upstream enterprise git server rather than on your local clone.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-05-08 12:07 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-07 22:59 check if one branch contains another branch Alexander Mills
2020-05-07 23:08 ` Junio C Hamano
2020-05-07 23:12   ` Alexander Mills
2020-05-07 23:15     ` Alexander Mills
2020-05-07 23:24     ` brian m. carlson
2020-05-07 23:28       ` Alexander Mills
2020-05-07 23:47         ` brian m. carlson
2020-05-07 23:31 ` Randall S. Becker
2020-05-07 23:37   ` Alexander Mills
2020-05-07 23:41     ` Junio C Hamano
2020-05-08  1:01     ` Alexander Mills
2020-05-08 12:07       ` Randall S. Becker

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).