[Script]GIT Conflict Fixer

Search This thread

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
GIT CONFLICT FIXER

I made this little script called Git Conflict Fixer, the name says all I guess. The script does not have a brain that can solve REAL issues in the code, what it does is the following:

- Search in current dir for merge derps inside the files (usually happens after merging a branch or maybe just 1 commit)
- Edits the derped files by the standard procedure:

<<<<<<< HEAD
OLDCONTENT GETS REMOVED
UNTIL THIS LINE

=======
NEW CONTENT WILL BE USED
UNTIL THIS LINE
>>>>>>>
- Commits if wanted

In 90% of the cases this easy way is the right way to fix it (luckily). so the 10% that's left is up to yourself to fix

This is not a wonder tool from outer space that will fix all your problems, but it will definitely help you when doing merges. So don't blame me if your code is not working ;)


Github Repository:
https://github.com/broodplank/GitConflictResolver



The main script

Code:
#!/bin/bash
#Auto fix git merge/cherry-pick conflicts in files
#Revision 1

#Startup check
if [[ -e /tmp/conflicts ]]; then
        rm -f /tmp/conflicts
fi;

#HEADER
echo "--- GIT Conflict Resolver v1.0"
echo "-- Created by broodplank"
echo "- broodplank.net"
echo
echo "-> Checking for .git folder"
echo -n "Result: "

#Check for .git folder for behavior
if [[ -d ${PWD}/.git ]]; then

    echo "found, using git diff"
    echo
    echo "-> Finding conflicts..."
    git diff --name-only --diff-filter=U > /tmp/conflicts

else 

    echo "not found, using native tools"
    echo
    echo "-> Finding conflicts..."
    grep -l -H -r '<<<<<<< HEAD' ${PWD}/* | awk '!a[$0]++' > /tmp/conflicts

fi;


#Check if conflicts exist
if [[ `cat /tmp/conflicts` != "" ]]; then
    echo 
    echo "-> Conflicts found in files:"
    while read F  ; do
            echo '- '$F
    done </tmp/conflicts
else
    echo "STOP, No conflicts found!"
    exit
fi;

#Start executing standard conflict resolve strategy
echo 
echo "-> Fixing conflicts..."
echo
while read G  ; do
        echo "--> Working on file: $G"
        echo "Removing text between HEAD and middle"
        sed -i -s '/<<<<<<< HEAD/,/=======/d' $G
        echo "Removing conflict footer"
        sed -i -s '/>>>>>>>/d' $G
        echo
done </tmp/conflicts

#Assume conflicts are actually solved
echo "--> Conflicts have been automatically fixed!"
echo
echo "Please note:"
echo "Although most of the conflicts can be resolved this way, It does not count for all conflicts."
echo "If you experience errors on compiling please review the changes made"
echo

#Stage commit?
if [[ -d ${PWD}/.git ]]; then
    echo "Would you like to stage the commit? () [Y/n]"
    echo -n ": "
    read choice
    if [[ $choice != "n" ]]; then
        git add .
        git commit
    fi


fi;
echo
echo "All done!"
Example output:

Merging AOSP master in frameworks/av:

broodplank@Bulldozer ~/repos/platform_frameworks_av $ fixmerge
--- GIT Conflict Resolver v1.0
-- Created by broodplank
- broodplank.net

-> Checking for .git folder
Result: found, using git diff

-> Finding conflicts...

-> Conflicts found in files:
- cmds/screenrecord/screenrecord.cpp
- media/libmedia/AudioTrack.cpp
- media/libmediaplayerservice/Android.mk
- media/libstagefright/AwesomePlayer.cpp
- media/libstagefright/MPEG4Extractor.cpp
- media/libstagefright/TimedEventQueue.cpp
- media/libstagefright/Utils.cpp
- media/libstagefright/httplive/LiveSession.cpp
- media/libstagefright/wifi-display/source/TSPacketizer.cpp
- services/audioflinger/Threads.cpp

-> Fixing conflicts...

--> Working on file: cmds/screenrecord/screenrecord.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libmedia/AudioTrack.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libmediaplayerservice/Android.mk
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libstagefright/AwesomePlayer.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libstagefright/MPEG4Extractor.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libstagefright/TimedEventQueue.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libstagefright/Utils.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libstagefright/httplive/LiveSession.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: media/libstagefright/wifi-display/source/TSPacketizer.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Working on file: services/audioflinger/Threads.cpp
Removing text between HEAD and middle
Removing conflict footer

--> Conflicts have been automatically fixed!

Please note:
Although most of the conflicts can be resolved this way, It does not count for all conflicts.
If you experience errors on compiling please review the changes made

Would you like to stage the commit? () [Y/n]
:
[kk-4.4 6ffce17] Merge remote-tracking branch 'a/master' into kk-4.4

All done!
 

desalesouche

Senior Member
Feb 6, 2012
3,651
4,297
his sir, is there a way to use that tool for kernel instead git?
i mean to merge all from old kernel to new virgen kernel?
 

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
his sir, is there a way to use that tool for kernel instead git?
i mean to merge all from old kernel to new virgen kernel?

Well it just "fixes" all conflict merges that are made by the way git marks it (<<<<<< ,=====, >>>>)

So the title says git but that just means it's only fixes that kind of conflicts. if you merge the old kernel to new kernel (with git I assume, what else, copy paste is not an option). and you remove the .git folder afterwards it will also works, it's just a matter of conflict types.

so basically:

in new kernel folder
Code:
git remote add old https://github.com/desalesouche/yourrepo
git fetch old
git merge old/branchname (now you will get the errors)
fixmerge (choose not to commit it)

Now try to compile the rom, if it works you can commit it, if not you will have to do some fixing yourself, since it just do basic replacing, in 90% of the cases this enough and will fix the problem but it's not guaranteed!
 
  • Like
Reactions: desalesouche

sorg

Senior Member
Sep 5, 2006
1,059
1,136
台灣
OMG. You offer blind-fix for problems? Good it's not Nuclear plant firmware ;)
Never ever do this if you want quality software. Everything which can be automatically solved is already solved by git. Everything else should be manually examined before commit.

Git is more strict than patch app while cherry-picking, so it may produce such false-conflicts. In this case i suggest to make format-patch and then apply it through patch app. This way is also "automatic" conflict fixer, but in more proper (read: more success chances) way. This will eliminate false-conflicts and will adjust patch lines where possible, although you won't be able to use 3-way mergetool option (which you won't use anyway according to OP).
 
  • Like
Reactions: broodplank1337

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
OMG. You offer blind-fix for problems? Good it's not Nuclear plant firmware ;)
Never ever do this if you want quality software. Everything which can be automatically solved is already solved by git. Everything else should be manually examined before commit.

Git is more strict than patch app while cherry-picking, so it may produce such false-conflicts. In this case i suggest to make format-patch and then apply it through patch app. This way is also "automatic" conflict fixer, but in more proper (read: more success chances) way. This will eliminate false-conflicts and will adjust patch lines where possible, although you won't be able to use 3-way mergetool option (which you won't use anyway according to OP).

I do not offer a real fix, just a replacement, the script has no brain as described and only does basic replacement. I know this is not a real problem solver, but when I look at my history of merge conflicts when doing mass merges I can say that 99% of the case it was just the standard replacement procedure that was the solution. Only in some rare cases (in my experience) the default replacement does not fix the code but break it instead.
The actual reason why I post shell scripts in this section, because mostly I've just found out how some function works in shell script (in this case it was reading ranges), and then I write an script that is related to android. I have a lot of little scraps like these floating around on my pc and I usually share them once in a while.

And thanks explanation on the "real" conflict solving way, I will take a look in it sometimes whether it allows automation
 

Top Liked Posts

  • There are no posts matching your filters.
  • 9
    GIT CONFLICT FIXER

    I made this little script called Git Conflict Fixer, the name says all I guess. The script does not have a brain that can solve REAL issues in the code, what it does is the following:

    - Search in current dir for merge derps inside the files (usually happens after merging a branch or maybe just 1 commit)
    - Edits the derped files by the standard procedure:

    <<<<<<< HEAD
    OLDCONTENT GETS REMOVED
    UNTIL THIS LINE

    =======
    NEW CONTENT WILL BE USED
    UNTIL THIS LINE
    >>>>>>>
    - Commits if wanted

    In 90% of the cases this easy way is the right way to fix it (luckily). so the 10% that's left is up to yourself to fix

    This is not a wonder tool from outer space that will fix all your problems, but it will definitely help you when doing merges. So don't blame me if your code is not working ;)


    Github Repository:
    https://github.com/broodplank/GitConflictResolver



    The main script

    Code:
    #!/bin/bash
    #Auto fix git merge/cherry-pick conflicts in files
    #Revision 1
    
    #Startup check
    if [[ -e /tmp/conflicts ]]; then
            rm -f /tmp/conflicts
    fi;
    
    #HEADER
    echo "--- GIT Conflict Resolver v1.0"
    echo "-- Created by broodplank"
    echo "- broodplank.net"
    echo
    echo "-> Checking for .git folder"
    echo -n "Result: "
    
    #Check for .git folder for behavior
    if [[ -d ${PWD}/.git ]]; then
    
        echo "found, using git diff"
        echo
        echo "-> Finding conflicts..."
        git diff --name-only --diff-filter=U > /tmp/conflicts
    
    else 
    
        echo "not found, using native tools"
        echo
        echo "-> Finding conflicts..."
        grep -l -H -r '<<<<<<< HEAD' ${PWD}/* | awk '!a[$0]++' > /tmp/conflicts
    
    fi;
    
    
    #Check if conflicts exist
    if [[ `cat /tmp/conflicts` != "" ]]; then
        echo 
        echo "-> Conflicts found in files:"
        while read F  ; do
                echo '- '$F
        done </tmp/conflicts
    else
        echo "STOP, No conflicts found!"
        exit
    fi;
    
    #Start executing standard conflict resolve strategy
    echo 
    echo "-> Fixing conflicts..."
    echo
    while read G  ; do
            echo "--> Working on file: $G"
            echo "Removing text between HEAD and middle"
            sed -i -s '/<<<<<<< HEAD/,/=======/d' $G
            echo "Removing conflict footer"
            sed -i -s '/>>>>>>>/d' $G
            echo
    done </tmp/conflicts
    
    #Assume conflicts are actually solved
    echo "--> Conflicts have been automatically fixed!"
    echo
    echo "Please note:"
    echo "Although most of the conflicts can be resolved this way, It does not count for all conflicts."
    echo "If you experience errors on compiling please review the changes made"
    echo
    
    #Stage commit?
    if [[ -d ${PWD}/.git ]]; then
        echo "Would you like to stage the commit? () [Y/n]"
        echo -n ": "
        read choice
        if [[ $choice != "n" ]]; then
            git add .
            git commit
        fi
    
    
    fi;
    echo
    echo "All done!"
    Example output:

    Merging AOSP master in frameworks/av:

    broodplank@Bulldozer ~/repos/platform_frameworks_av $ fixmerge
    --- GIT Conflict Resolver v1.0
    -- Created by broodplank
    - broodplank.net

    -> Checking for .git folder
    Result: found, using git diff

    -> Finding conflicts...

    -> Conflicts found in files:
    - cmds/screenrecord/screenrecord.cpp
    - media/libmedia/AudioTrack.cpp
    - media/libmediaplayerservice/Android.mk
    - media/libstagefright/AwesomePlayer.cpp
    - media/libstagefright/MPEG4Extractor.cpp
    - media/libstagefright/TimedEventQueue.cpp
    - media/libstagefright/Utils.cpp
    - media/libstagefright/httplive/LiveSession.cpp
    - media/libstagefright/wifi-display/source/TSPacketizer.cpp
    - services/audioflinger/Threads.cpp

    -> Fixing conflicts...

    --> Working on file: cmds/screenrecord/screenrecord.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libmedia/AudioTrack.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libmediaplayerservice/Android.mk
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libstagefright/AwesomePlayer.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libstagefright/MPEG4Extractor.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libstagefright/TimedEventQueue.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libstagefright/Utils.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libstagefright/httplive/LiveSession.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: media/libstagefright/wifi-display/source/TSPacketizer.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Working on file: services/audioflinger/Threads.cpp
    Removing text between HEAD and middle
    Removing conflict footer

    --> Conflicts have been automatically fixed!

    Please note:
    Although most of the conflicts can be resolved this way, It does not count for all conflicts.
    If you experience errors on compiling please review the changes made

    Would you like to stage the commit? () [Y/n]
    :
    [kk-4.4 6ffce17] Merge remote-tracking branch 'a/master' into kk-4.4

    All done!
    1
    his sir, is there a way to use that tool for kernel instead git?
    i mean to merge all from old kernel to new virgen kernel?

    Well it just "fixes" all conflict merges that are made by the way git marks it (<<<<<< ,=====, >>>>)

    So the title says git but that just means it's only fixes that kind of conflicts. if you merge the old kernel to new kernel (with git I assume, what else, copy paste is not an option). and you remove the .git folder afterwards it will also works, it's just a matter of conflict types.

    so basically:

    in new kernel folder
    Code:
    git remote add old https://github.com/desalesouche/yourrepo
    git fetch old
    git merge old/branchname (now you will get the errors)
    fixmerge (choose not to commit it)

    Now try to compile the rom, if it works you can commit it, if not you will have to do some fixing yourself, since it just do basic replacing, in 90% of the cases this enough and will fix the problem but it's not guaranteed!
    1
    OMG. You offer blind-fix for problems? Good it's not Nuclear plant firmware ;)
    Never ever do this if you want quality software. Everything which can be automatically solved is already solved by git. Everything else should be manually examined before commit.

    Git is more strict than patch app while cherry-picking, so it may produce such false-conflicts. In this case i suggest to make format-patch and then apply it through patch app. This way is also "automatic" conflict fixer, but in more proper (read: more success chances) way. This will eliminate false-conflicts and will adjust patch lines where possible, although you won't be able to use 3-way mergetool option (which you won't use anyway according to OP).