[Android] jabza:
Resolving Conflicts from Cherry-Picking

Resolving Conflicts from Cherry-Picking

So you've found the commit you want to cherry-pick. You've gone to the right place in your source code to apply it. You've told your computer to apply the cherry-pick. It said it couldn't. Now what?

Well let's see what's wrong.

git status

Your terminal will now tell you what files have been automatically changed for you and what you need to change yourself. In green are the files that are done, and in red are the files you need to tell it what to do with. Here's what your terminal should look like after “git status”:

If you cherry-picked from gerrit with me and did [1/2] Settings: HALO, it'll look something like this:

If you cherry-picked from GitHub with me and did [2/2] Frameworks: HALO, it'll look something like this:

In looking at the files in red, we see two different tags.

deleted by us – this means that there's a file in the commit that doesn't exist on our computer

both modified – this means that there's a file that was supposed to be modified, but that was modified on our computer from what was expected in the commit

deleted by them – this is a third tag you may see, though it's not here. This means that the commit deletes a file which we stil have on our computer

So, what do we do? Let's start with the ones tagged “both modified”. The first file in red underneath the frameworks/base commit is packages/SystemUI/res/layout/status_bar_expanded_header.xml. We'll open it in our favorite text editor and see what it's all about.

gedit packages/SystemUI/res/layout/status_bar_expanded_header.xml

Now we search for what's messing with our cherry-pick. It's enclosed in the tags “<<<<<<< HEAD” and “>>>>>>> ab72e4a... [2/2] Frameworks: HALO”. Lines 78-118 look like this:

<<<<<<< HEAD
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/ic_notify_clear"
android:background="@drawable/ic_notify_button_bg"
android:contentDescription="@string/accessibility_clear_all"
/>
=======
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:scaleType="center"
android:src="@drawable/ic_notify_clear"
android:background="@drawable/ic_notify_button_bg"
android:contentDescription="@string/accessibility_clear_all"
/>

<FrameLayout android:id="@+id/context_button_holder"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="4dp"
>
<ImageView android:id="@+id/halo_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/ic_notify_halo"
android:contentDescription="@string/accessibility_halo"
/>
<ImageView android:id="@+id/edit_mode_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/ic_notify_edit_normal"
android:background="@drawable/ic_notify_button_bg"
android:visibility="gone"
android:contentDescription="@string/accessibility_clear_all"
/>
</FrameLayout>
>>>>>>> ab72e4a... [2/2] Frameworks: HALO

Between the <<<<<< HEAD and ======== is code that wasn't expected to be there. Between ====== and >>>>>> ab72e4a... [2/2] Frameworks: HALO is the code that was added in by the commit. Here is where we use our judgement to edit the code to what we need. Because I can see that the code between <<<<<< HEAD and ====== is duplicated by the commit, I can just delete that snippet altogether. We also need to delete the tags added by our cherry pick. In the end, the code looks like this:

android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:scaleType="center"
android:src="@drawable/ic_notify_clear"
android:background="@drawable/ic_notify_button_bg"
android:contentDescription="@string/accessibility_clear_all"
/>

<FrameLayout android:id="@+id/context_button_holder"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="4dp"
>
<ImageView android:id="@+id/halo_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/ic_notify_halo"
android:contentDescription="@string/accessibility_halo"
/>
<ImageView android:id="@+id/edit_mode_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/ic_notify_edit_normal"
android:background="@drawable/ic_notify_button_bg"
android:visibility="gone"
android:contentDescription="@string/accessibility_clear_all"
/>
</FrameLayout>

Now we can save and quit.

We've resolved the conflict (assuming that was the only one), and now we can mark it as such in the terminal. To do so, we use this command:

git add packages/SystemUI/res/layout/status_bar_expanded_header.xml

Now, we can do “git status” again and work on the next file we need to modify.

But what about the “deleted by us” files? Well, git is smart. All we need to do is determine if we need those files (hint: unless you really know what you're doing, you do need them). If we need them, git already loaded the files up, so we just have to mark them as resolved by doing

git add res/values/pa_arrays.xml

You can replace “res/values/pa_arrays.xml” with the rest of the files labeled “deleted by us”. You can also do more than one file at once, like so:

git add res/values/pa_arrays.xml res/values/pa_strings.xml whatever/files/I/want.java

Continue until you've fixed all the issues.

Note: when you open a file in gedit, it creates a file with the same name but with a ~ added to the end. When you do “git status” these files will show up as “untracked files”. You can either delete them with the command “rm path/to/file.java~” or just ignore them.

Once you do “git status” and all the files turn up green, you have finished resolving the conflicts. Now is the best time to compile your ROM and see if your changes worked. If you're in a rush (or lazy or don't feel like waiting for a ROM to compile or you've already tested it), you can now commit your changes.

git commit

It'll take you to a screen to write your commit message. It opens it in the in-terminal editor “nano”. You can edit it to say whatever you want, and when you're done press CTRL + X to exit, and then press Y to save the changes. Then your terminal will say you've committed the changes.

Congratulations! You've successfully cherry-picked a commit.