In Muhammad, Karen describes the historical context that gave rise to the apparent Muslim law to require Muslims give shelter to other Muslims and in so doing also swear to defend them against their enemies.
When Muhammad had been expelled from Mecca, tribes in Medina agreed to help him and his converts. They believed these people would ultimately provide some material gain for their tribes. At the time they were not Muslims although many converted. As part of this agreement to aid Muhammad and the Muslims by taking them into their city, they also agreed to defend them against their former tribes of Mecca. The tribes of Medina providing the aid had no quarrel of their own with the tribes of Mecca. This event undoubtedly set the stage to establish the law of mutual defense. Two other aspects of this event that seems to be overlooked; the requirement for both parties to be completely honest and for the party that has wronged the Muslim to do so as an act of preventing them from practicing their religion.
It is easy to see how unscrupulous people hiding behind the Muslim laws could force a devout Muslim to fight with him against what is presented as an enemy that is trying to prevent the Muslim from practicing their religion.