You can see some rough details about what is used to calculate the tree SHA here.
Further information about the binary format that the tree is stored in can be found here.
The actual SHA that is used is just the SHA of the buffer described in that detailed version.
The key points to take home is that the SHA is dependent on the filenames of all contained objects or trees, their SHAs and their permissions. Change any of those and you change the SHA. If two trees have the same SHAs then all those components must match (excluding the possibility of collisions as they're nigh on impossible).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…