Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

add @AfterExtras #928

Closed
wants to merge 18 commits into from
Closed

Conversation

dodgex
Copy link
Member

@dodgex dodgex commented Mar 2, 2014

this PR adds an @AfterExtras annotation, that works similar to @AfterInject, but the methods are called every time a new intent is set to the activty by setIntent(Intent intent).

this allows an activity to get notified about new intents and so new extras.

the generated code looks like this

    private void injectExtras_() {
        Bundle extras_ = getIntent().getExtras();
        if (extras_!= null) {
            if (extras_.containsKey(OPEN_ALARMCLOCK_EXTRA)) {
                openAlarmclock = extras_.getBoolean(OPEN_ALARMCLOCK_EXTRA);
            }
            myAfterExtraMethod();
        }
    }

for multiple extras and/or methods, first all extras get injected and then the methods get called.


to be useful this currently needs this code block in the activity
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

currently known "issues", the first call to an @AfterExtras annotated method does not yet have @Bean fields set. this would require to move the ExtraHandler below the BeanHandler in AnnotationHandlers. i don't think that this would cause any issues. but i'll open a seperate PR for this change.

@dodgex dodgex mentioned this pull request Mar 2, 2014
@DayS DayS added this to the 3.2 milestone May 11, 2014
@DayS
Copy link
Contributor

DayS commented May 11, 2014

Could you add some functional tests ?

@dodgex
Copy link
Member Author

dodgex commented May 11, 2014

actually i can't even run the functional-tests-1-5 successfully

here is a snippet of the log when doing mvn clean install in top level project (develop branch, no changes)

.... / CUT / ....
[INFO] ------------------------------------------------------------------------
[INFO] Building functional-test-1-5 3.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ functional-test-1-5 ---
[INFO] 
[INFO] --- maven-enforcer-plugin:1.0:enforce (enforce-maven) @ functional-test-1-5 ---
[INFO] 
[INFO] --- android-maven-plugin:3.6.0:generate-sources (default-generate-sources) @ functional-test-1-5 ---
[WARNING] Error injecting: com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo
java.lang.NoClassDefFoundError: Lorg/sonatype/aether/RepositorySystem;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2387)
    at java.lang.Class.getDeclaredFields(Class.java:1796)
    at com.google.inject.spi.InjectionPoint.getInjectionPoints(InjectionPoint.java:661)
    at com.google.inject.spi.InjectionPoint.forInstanceMethodsAndFields(InjectionPoint.java:366)
    at com.google.inject.internal.ConstructorBindingImpl.getInternalDependencies(ConstructorBindingImpl.java:165)
    at com.google.inject.internal.InjectorImpl.getInternalDependencies(InjectorImpl.java:609)
    at com.google.inject.internal.InjectorImpl.cleanup(InjectorImpl.java:565)
    at com.google.inject.internal.InjectorImpl.initializeJitBinding(InjectorImpl.java:551)
    at com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:865)
    at com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:790)
    at com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:278)
    at com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:210)
    at com.google.inject.internal.InjectorImpl.getProviderOrThrow(InjectorImpl.java:986)
    at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1019)
    at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:982)
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1032)
    at org.eclipse.sisu.space.AbstractDeferredClass.get(AbstractDeferredClass.java:48)
    at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:86)
    at com.google.inject.internal.InternalFactoryToInitializableAdapter.provision(InternalFactoryToInitializableAdapter.java:55)
    at com.google.inject.internal.ProviderInternalFactory$1.call(ProviderInternalFactory.java:70)
    at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:100)
    at org.eclipse.sisu.plexus.PlexusLifecycleManager.onProvision(PlexusLifecycleManager.java:133)
    at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:109)
    at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:55)
    at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:68)
    at com.google.inject.internal.InternalFactoryToInitializableAdapter.get(InternalFactoryToInitializableAdapter.java:47)
    at com.google.inject.internal.InjectorImpl$2$1.call(InjectorImpl.java:997)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1047)
    at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:993)
    at com.google.inject.Scopes$1$1.get(Scopes.java:59)
    at org.eclipse.sisu.inject.LazyBeanEntry.getValue(LazyBeanEntry.java:82)
    at org.eclipse.sisu.plexus.LazyPlexusBean.getValue(LazyPlexusBean.java:51)
    at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:260)
    at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:252)
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:462)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:120)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.ClassNotFoundException: org.sonatype.aether.RepositorySystem
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:259)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:235)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:227)
    ... 57 more
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] AndroidAnnotations Parent ......................... SUCCESS [  0.207 s]
[INFO] AndroidAnnotations API ............................ SUCCESS [  5.227 s]
[INFO] AndroidAnnotations Annotation Processor ........... SUCCESS [ 13.954 s]
[INFO] functional-test-1-5 ............................... FAILURE [  0.287 s]
[INFO] functional-test-1-5-tests ......................... SKIPPED
[INFO] AndroidAnnotations With CodeModel ................. SKIPPED
[INFO] AndroidAnnotations Bundle ......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.357 s
[INFO] Finished at: 2014-05-11T17:50:28+01:00
[INFO] Final Memory: 36M/316M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.0:generate-sources (default-generate-sources) on project functional-test-1-5: Execution default-generate-sources of goal com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.0:generate-sources failed: A required class was missing while executing com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.0:generate-sources: Lorg/sonatype/aether/RepositorySystem;
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Users/Dodge/.m2/repository/com/jayway/maven/plugins/android/generation2/android-maven-plugin/3.6.0/android-maven-plugin-3.6.0.jar
[ERROR] urls[1] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/build/builder/0.4/builder-0.4.jar
[ERROR] urls[2] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/sdklib/22.0/sdklib-22.0.jar
[ERROR] urls[3] = file:/C:/Users/Dodge/.m2/repository/org/apache/httpcomponents/httpclient/4.1.1/httpclient-4.1.1.jar
[ERROR] urls[4] = file:/C:/Users/Dodge/.m2/repository/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar
[ERROR] urls[5] = file:/C:/Users/Dodge/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
[ERROR] urls[6] = file:/C:/Users/Dodge/.m2/repository/commons-codec/commons-codec/1.4/commons-codec-1.4.jar
[ERROR] urls[7] = file:/C:/Users/Dodge/.m2/repository/org/apache/httpcomponents/httpmime/4.1/httpmime-4.1.jar
[ERROR] urls[8] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/layoutlib/layoutlib-api/22.0/layoutlib-api-22.0.jar
[ERROR] urls[9] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/dvlib/22.0/dvlib-22.0.jar
[ERROR] urls[10] = file:/C:/Users/Dodge/.m2/repository/org/apache/commons/commons-compress/1.0/commons-compress-1.0.jar
[ERROR] urls[11] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/build/builder-test-api/0.4/builder-test-api-0.4.jar
[ERROR] urls[12] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/build/builder-model/0.4/builder-model-0.4.jar
[ERROR] urls[13] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/common/22.0/common-22.0.jar
[ERROR] urls[14] = file:/C:/Users/Dodge/.m2/repository/com/google/guava/guava/13.0.1/guava-13.0.1.jar
[ERROR] urls[15] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/sdk-common/22.0/sdk-common-22.0.jar
[ERROR] urls[16] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/ddms/ddmlib/22.0/ddmlib-22.0.jar
[ERROR] urls[17] = file:/C:/Users/Dodge/.m2/repository/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.jar
[ERROR] urls[18] = file:/C:/Users/Dodge/.m2/repository/com/android/tools/build/manifest-merger/22.0/manifest-merger-22.0.jar
[ERROR] urls[19] = file:/C:/Users/Dodge/.m2/repository/org/bouncycastle/bcpkix-jdk15on/1.48/bcpkix-jdk15on-1.48.jar
[ERROR] urls[20] = file:/C:/Users/Dodge/.m2/repository/org/bouncycastle/bcprov-jdk15on/1.48/bcprov-jdk15on-1.48.jar
[ERROR] urls[21] = file:/C:/Users/Dodge/.m2/repository/org/sonatype/sisu/sisu-inject-bean/2.1.1/sisu-inject-bean-2.1.1.jar
[ERROR] urls[22] = file:/C:/Users/Dodge/.m2/repository/org/sonatype/sisu/sisu-guice/2.9.4/sisu-guice-2.9.4-no_aop.jar
[ERROR] urls[23] = file:/C:/Users/Dodge/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar
[ERROR] urls[24] = file:/C:/Users/Dodge/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar
[ERROR] urls[25] = file:/C:/Users/Dodge/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar
[ERROR] urls[26] = file:/C:/Users/Dodge/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
[ERROR] urls[27] = file:/C:/Users/Dodge/.m2/repository/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.jar
[ERROR] urls[28] = file:/C:/Users/Dodge/.m2/repository/emma/emma/2.1.5320/emma-2.1.5320.jar
[ERROR] urls[29] = file:/C:/Users/Dodge/.m2/repository/org/codehaus/plexus/plexus-archiver/2.3/plexus-archiver-2.3.jar
[ERROR] urls[30] = file:/C:/Users/Dodge/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[ERROR] urls[31] = file:/C:/Users/Dodge/.m2/repository/org/codehaus/plexus/plexus-io/2.0.6/plexus-io-2.0.6.jar
[ERROR] urls[32] = file:/C:/Users/Dodge/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.10/plexus-utils-3.0.10.jar
[ERROR] urls[33] = file:/C:/Users/Dodge/.m2/repository/commons-jxpath/commons-jxpath/1.3/commons-jxpath-1.3.jar
[ERROR] urls[34] = file:/C:/Users/Dodge/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar
[ERROR] urls[35] = file:/C:/Users/Dodge/.m2/repository/org/ow2/asm/asm/4.1/asm-4.1.jar
[ERROR] urls[36] = file:/C:/Users/Dodge/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
[ERROR] urls[37] = file:/C:/Users/Dodge/.m2/repository/org/sonatype/aether/aether-util/1.13.1/aether-util-1.13.1.jar
[ERROR] urls[38] = file:/C:/Users/Dodge/.m2/repository/com/github/rtyley/android-screenshot-paparazzo/1.9/android-screenshot-paparazzo-1.9.jar
[ERROR] urls[39] = file:/C:/Users/Dodge/.m2/repository/com/madgag/animated-gif-lib/1.0/animated-gif-lib-1.0.jar
[ERROR] urls[40] = file:/C:/Users/Dodge/.m2/repository/com/github/rtyley/android-screenshot-celebrity/1.8/android-screenshot-celebrity-1.8.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[project>org.androidannotations:functional-test-1-5:3.1-SNAPSHOT, parent: ClassRealm[maven.api, parent: null]]]
[ERROR] 
[ERROR] -----------------------------------------------------: org.sonatype.aether.RepositorySystem
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :functional-test-1-5

@WonderCsabo
Copy link
Member

I guess you are using a newer version of maven. Currently maven 3.0.5 is supported.

@dodgex
Copy link
Member Author

dodgex commented May 11, 2014

yea, i'm using 3.2.1.

with 3.0.5 and after installing android 2.2 SDK stuff it works...

only thing i found is this, but as the tests pass, i assume this is OK?

Running org.androidannotations.test15.AbstractActivityTest
Warning: an error occurred while binding shadow class: ShadowGeoPoint
Warning: an error occurred while binding shadow class: ShadowItemizedOverlay
Warning: an error occurred while binding shadow class: ShadowMapController
Warning: an error occurred while binding shadow class: ShadowMapActivity
Warning: an error occurred while binding shadow class: ShadowMapView
Warning: an error occurred while binding shadow class: ShadowOverlayItem

i'll try to add some tests, but be warned. i haven't done any unit testing for android yet - and not much testing at all... (SHAME ON ME!) but this might be a good start ;-)

@WonderCsabo
Copy link
Member

These warnings are OK.

@dodgex
Copy link
Member Author

dodgex commented May 11, 2014

currently i have a test that tests setIntent(), but i also would like to test the injectExtras_() coming form onCreate() but for that i'm missing the knowledge how to create an activity that has an intent set, before onCreate() is called without using setIntent()... is there a way to do this?

i could try to set the intent using reflection, but i don't think that this is something that should be in a test?

@WonderCsabo
Copy link
Member

I think currently the only possibility is using reflection. In Robolectric 2.2, there is a much better way:

Robolectric.buildActivity(YourActivity.class).withIntent(intent).create().get();

Unfortunately my PR 973 which is upgrading to Robolectric 2.2 is not done and not merged yet.

@dodgex
Copy link
Member Author

dodgex commented May 11, 2014

it seems that reflection also does not work. Activity.class.getDeclaredFields() returns only the following fields:

  • RESULT_CANCELED
  • RESULT_OK
  • RESULT_FIRST_USER
  • FOCUSED_STATE_SET
  • DEFAULT_KEYS_DISABLE
  • DEFAULT_KEYS_DIALER
  • DEFAULT_KEYS_SHORTCUT
  • DEFAULT_KEYS_SEARCH_LOCAL
  • DEFAULT_KEYS_SEARCH_GLOBAL
  • shadow

not sure why, but the __shadow__ looks like it is from Robolectric.

what shall i/we do now?

@dodgex
Copy link
Member Author

dodgex commented May 11, 2014

i added the setIntent() test. i hope this one is okay so far.

@WonderCsabo
Copy link
Member

The problem here that the Activity is actually just a stub, and Robolectric decorates it to work, so there is no mIntent field at all. However you can try to get it work by getting the __shadow__ field which is a ShadowActivity and set the Intent reflectively to that.

@dodgex
Copy link
Member Author

dodgex commented May 12, 2014

there it is. i hope it is okay the way i did it. :)

@dodgex
Copy link
Member Author

dodgex commented May 28, 2014

any news on this?

@dodgex
Copy link
Member Author

dodgex commented Jun 5, 2014

my test now uses the Robolectric 2.x syntax.

@WonderCsabo
Copy link
Member

@DayS , @yDelouis can you look at this PR? From the comment of Days, i guess he was willing to accept this feature. However i think the commits are really need a rebase...

@yDelouis
Copy link
Contributor

This seems very great to me. There are a lot of commits for only 5 files changed but most of them are merges of upstream changes.

@dodgex
Copy link
Member Author

dodgex commented Jun 30, 2014

if wanted i can open a new PR that has less commits. :)

@WonderCsabo
Copy link
Member

Also read here how to squash multiple commits into one.

http://git-scm.com/book/en/Git-Tools-Rewriting-History

@dodgex
Copy link
Member Author

dodgex commented Aug 15, 2014

I'll give it a try tomorrow. Thank you for the links.

@dodgex
Copy link
Member Author

dodgex commented Aug 16, 2014

Ok, i think i fkd it up. :(

after the rebase finished, i got 66 commits to push and 10 to pull. when i pull i get an empty Merge branch 'AfterExtra' of https://github.com/dodgex/androidannotations into AfterExtra commit.

as i tried to rebase instead of merge when pulling the 10 commits i'll got hit by a bunch of merge conflicts.

i think i shouldn't have pushed the commits. now i have everything duplicated in the log and the merge develop commits are still there :(

@dodgex
Copy link
Member Author

dodgex commented Aug 16, 2014

i added a new branch https://github.com/dodgex/androidannotations/tree/AfterExtraNew but no PR yet.

if you got some time maybe we could try to rebase this together somehow that i can learn how it works?

@WonderCsabo
Copy link
Member

I am still afk for a week, but then i get back to this and write down the
commands how to rebase.

@dodgex dodgex mentioned this pull request Aug 16, 2014
@dodgex
Copy link
Member Author

dodgex commented Aug 16, 2014

i opened a new PR. i hope for the next time i learned the rebasing stuff to clean a PR. sorry for the trouble

@dodgex dodgex closed this Aug 16, 2014
@dodgex dodgex deleted the AfterExtra branch August 21, 2014 18:10
@WonderCsabo
Copy link
Member

@dodgex Can you restore you branch so i can illustrate rebase on it?

@dodgex dodgex restored the AfterExtra branch August 22, 2014 20:22
@dodgex
Copy link
Member Author

dodgex commented Aug 22, 2014

done

@WonderCsabo
Copy link
Member

OK, this is messed up so i created an example project.

It has two branches, a master branch and a feature_branch which is where you add a new feature to this great project. :) The feature_branch is created when the project had the initial state, then a commit added. Since the the master branch also updated, so the two branches diverged:

untitled

Feature branch:

72ecb19 2014-08-22 WonderCsabo enhanced readme on feature branched
a9e9291 2014-08-22 WonderCsabo Initial commit

Master branch:

551394b 2014-08-22 WonderCsabo edited readme on master
a9e9291 2014-08-22 WonderCsabo Initial commit

This is exactly the same problem that you had in this PR. Now we have to option to make the feature_branch up-to-date with the master branch. One is merging what you already done, so you created those merge commits. The other one is rebasing. In this case it means taking the patch that was introduced in the commit in the feature_branch and applying it on the top of the last commit of the master branch. The command is easy, we are on the feature_branch:

git rebase master

This will rewrite your commit 72ecb19 to be based on 551394b. So the log for the feature_branch looks like this:

8be1f36 2014-08-22 WonderCsabo enhanced readme on feature branched
551394b 2014-08-22 WonderCsabo edited readme on master
a9e9291 2014-08-22 Csaba Kozák Initial commit

As you can see 72ecb19 changed to 8be1f36 because the patch has now different content.

Now if you switch to the feature branch again, git warns you with this message: Your branch and 'origin/feature_branch' have diverged, and have 2 and 1 different commit each, respectively. This is clear because we rewritten the commits, so the log for the remote branch and the local branch is diverged. Now we have to make sure our rebased branch is on the remote. We cannot simply run git push, becase we would get this error message:

error: failed to push some refs to 'git@github.com:WonderCsabo/rebaseexample.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

We have to force push our local commits to the remote branch, so it is totally overwritten with our local commits:

git push -f

Now the remote branch is up-to-date with the master branch, and has no annoying merge commits. :)

@dodgex
Copy link
Member Author

dodgex commented Aug 23, 2014

okay. thank you. i think i got it :)

i think i was confused by the Your branch and 'origin/feature_branch' have diverged, and have 2 and 1 different commit each, respectively. message. and did the mistake to PULL instead of force push (what is currently impossible with my git client btw -.-)

i would be really thankfull if you could also do a short sample of squashing commits like

1234 add test
5678 other stuff
abcd fix typo in test
12cd add tests 
56ab other stuff

where 12cd contains the tests from 1234 including the fixed typo from abcd.

thank you alot! :)

@WonderCsabo
Copy link
Member

I created the squashthis branch for the demo. It has three new commits:

3697963 Fixed typo in first line
d7efae8 Added second line
a30b1eb Added first line

We want to meld 3697963 into a30b1eb, so we have to reorder and squash. This can be done with the "interactive rebase": git rebase -i HEAD~3. I added HEAD~3 parameter so it shows the last three commits. This command opens a text file in your configured text editor:

pick a30b1eb Added first line
pick d7efae8 Added second line
pick 3697963 Fixed typo in first line

# Rebase 551394b..3697963 onto 551394b
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

I think the commands are clear. This text file should be edited and saved, and when you exit the text editor application git-rebase will read it and execute the commands in it. We have to modify the file to achieve our goal to this:

pick   a30b1eb Added first line
squash 3697963 Fixed typo in first line
pick   d7efae8 Added second line

After executing this, we will get this message:

error: could not apply 3697963... Fixed typo in first line

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

Could not apply 3697963c70929f3c9abee2ed8e069c6e47705ee3... Fixed typo in first line

This is because we ran into a merge conflict. We can see it by running git status:

You are currently rebasing branch 'squashthis' on '551394b'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   README.md

We have to resolve the conflict, i hope you already know how to do that. Here is a small tutorial.
After we resolved the conflicts, we have to continue our rebase: git rebase --continue. Another text editor will be displayed with this message:

# This is a combination of 2 commits.
# The first commit's message is:

Added first line

# This is the 2nd commit message:

Fixed typo in first line

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 551394b
# You are currently rebasing branch 'squashthis' on '551394b'.
#
# Changes to be committed:
#   modified:   README.md
#
# Untracked files:
#   README.md.orig
#

Edit the message as you want and exit the editor. We got another merge conflict again when git-rebase want apply to commit with the second line:

[detached HEAD efe1a1c] Added first line
 1 file changed, 2 insertions(+)
error: could not apply d7efae8... Added second line

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply d7efae8796cb1b1bda58ad9a19193ed3b1e6d04d... Added second line

Resolve this conflict, and run git rebase --continue. Now our rebase is finally done:

[detached HEAD 70cd25e] Added second line
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/squashthis.

We can see the log:

70cd25e Added second line
efe1a1c Added first line

@yDelouis yDelouis modified the milestones: 3.2, Someday Aug 23, 2014
@dodgex dodgex deleted the AfterExtra branch August 29, 2014 17:34
@WonderCsabo
Copy link
Member

@yDelouis This is already merged in another PR. Why do you added the Someday milestone?

@yDelouis
Copy link
Contributor

To have only merged PRs when searching for PR with milestone 3.1.

@WonderCsabo
Copy link
Member

Ok, but the milestone is not needed here at all, since 3.1 is added to the
clean one.

@yDelouis yDelouis removed this from the Someday milestone Aug 30, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants