Is there a way with Git that future merges ignore the difference in version numbers in the pom file between branches?

My team plans to move from Perforce to Git, and I'm trying to find a way to make Git ignore the differences in the pom version between branches. This works well in Perforce, and I am unable to reproduce the behavior using Git.

Here are my steps:

  • Checkout parent branch

    ndeckard@ws /c/dev/proj/testgit (master) $ git checkout release/1.0 Switched to branch 'release/1.0' Your branch is up-to-date with 'origin/release/1.0'. 
  • Create a child branch from it

     ndeckard@ws /c/dev/proj/testgit (release/1.0) $ git branch branch/FEA-650 
  • Switch to a new branch

     ndeckard@ws /c/dev/proj/testgit (release/1.0) $ git checkout branch/FEA-650 Switched to branch 'branch/FEA-650' 
  • Update version of pom child branch

     <version>1.0.0-FEA-650-SNAPSHOT</version> 
  • Add and copy

     ndeckard@ws /c/dev/proj/testgit (branch/FEA-650) $ git status On branch branch/FEA-650 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: pom.xml no changes added to commit (use "git add" and/or "git commit -a") ndeckard@ws /c/dev/proj/testgit (branch/FEA-650) $ git add pom.xml ndeckard@ws /c/dev/proj/testgit (branch/FEA-650) $ git commit -m "set feature branch pom version" [branch/FEA-650 59e156e] set feature branch pom version 1 file changed, 1 insertion(+), 1 deletion(-) 
  • Go back to the parent branch

     ndeckard@ws /c/dev/proj/testgit (branch/FEA-650) $ git checkout release/1.0 Switched to branch 'release/1.0' Your branch is up-to-date with 'origin/release/1.0'. 
  • Merge the child branch into the parent automatically accepting the parent version of the branch (using the "ours" merge strategy)

     ndeckard@ws /c/dev/proj/testgit (release/1.0) $ git merge branch/FEA-650 -s ours Merge made by the 'ours' strategy. 
  • Attempted 2nd merger of the child with the parent (already updated). Good. This is what I want

     ndeckard@ws /c/dev/proj/testgit (release/1.0) $ git merge branch/FEA-650 Already up-to-date. 
  • Stylize the children and merge the parent into a child (it quickly forwards the parent branches of the pom version for the child). Not good. I need him to say “already updated” as above, and save the pOM version of the child branch as it is already on the child branch.

     ndeckard@ws /c/dev/proj/testgit (release/1.0) $ git checkout branch/FEA-650 Switched to branch 'branch/FEA-650' ndeckard@ws /c/dev/proj/testgit (branch/FEA-650) $ git merge release/1.0 Updating 59e156e..2f3a2a0 Fast-forward pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 

After step 7, I would like to combine in any direction between the parent and the child word to say (already updated.)

Is there a way with Git that future merges ignore the difference in version numbers in the pom file between branches?

+7
git merge maven perforce
source share
2 answers

My initial thought was to automate your POM version numbers, partially crowding out the POM <version> calculations, see later in the answer. If you don't want to do this, you need to reevaluate the Git workflow.

Merging the two paths, in master , and then back to the function branch causes problems. Your git merge branch/FEA-650 -s ours , using our strategy, tells Git that you have included everything and changes from the function branch to master , including changing the version of pom.xml that is lost, with master saving its version. The leading branch now considers that the HEAD function branch is a common ancestor (it is the parent commit at merge), so when you merge it back into the function branch, Git says: “everything was allowed when you merged with the master, there are no changes. .. fast forward. "

The simple answer is that after merging with the master, you need to transfer the branch to a new function branch, providing the branches of your function to a new / younger common ancestor, then you need to somehow reset the POM <version> change. You should not continue to work with the original function branch since it has been merged. To get new changes from the wizard, you must flip. It seems that there are some prerequisites that you may have left the changes in the function branch not related to the wizard, otherwise you will no longer need it, and you can translate the branch, or it can only be the <version> change tags that interest you .

There are several ways to simplify / speed up / automate version number management during repeated branching, from emergency (see below) to cherry-picking it from the placeholder branch using the Maven plugin, for example. Versions of Maven Plugin .

Original answer

Another approach would be to externally calculate the final / effective <version> outside your POM file based on branch metadata or other input from your CI. This is a little tricky to do in Maven without first pom.xml yourself after checking, but check out the Maven External Version Plugin .

It connects to the Maven life cycle and will create the pom.xml.new-version file “on the fly” (which you can .gitignore ), dynamically replacing all or part of the version number based on what you supply — the name, Git attribute branches commit hash etc.

Create / deploy the plugin and add it to your POM:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-external-version-plugin</artifactId> <version>0.1.0-SNAPSHOT</version> <extensions>true</extensions> <configuration> <strategy hint="sysprop"/> </configuration> </plugin> 

... then create an ad with a replacement version string, for example, you can:

 mvn install -Dexternal.version-qualifier=$(git symbolic-ref --short HEAD| sed s_^master\$__) 

... which will change 1.2.3-SNAPSHOT to 1.2.3-FEA-650-SNAPSHOT if the currently posted Git branch is FEA-650 . Maybe you should consider replacing / with - in your branch naming strategy, depending on what Maven thinks about it (I find / confusing, but it's just me) or modifying sed accordingly.

In the worst case scenario, this will allow you to remove your version number from your POM, so other POM changes can be safely combined and, as you know, are not related to the version number - if necessary, you can save the version numbers in another file using the Maven Properties plugin to download and if necessary replace the entire version number.

+4
source share

It would probably be burdensome to do this for every merger, but here is the idea.

git merge --no-ff --no-commit <other-branch>

git checkout HEAD -- pom.xml

The first command allows you to modify the content before the merge is completed, even if merge conflict conflicts do not exist. The second replaces pom.xml with what was in the current branch of the previous commit, effectively ignoring pom.xml from another branch.

Using the --ours merge strategy is problematic if there are conflicts in any file other than pom.xml, so I would be tired of using it for this purpose.

0
source share

All Articles