@@ -787,29 +787,42 @@ static struct object_id *get_cache_tree_oid(struct index_state *istate)
787787static int is_index_unchanged (struct repository * r )
788788{
789789 struct object_id head_oid , * cache_tree_oid ;
790+ const struct object_id * head_tree_oid ;
790791 struct commit * head_commit ;
791792 struct index_state * istate = r -> index ;
793+ const char * head_name ;
794+
795+ if (!resolve_ref_unsafe ("HEAD" , RESOLVE_REF_READING , & head_oid , NULL )) {
796+ /* Check to see if this is an unborn branch */
797+ head_name = resolve_ref_unsafe ("HEAD" ,
798+ RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE ,
799+ & head_oid , NULL );
800+ if (!head_name ||
801+ !starts_with (head_name , "refs/heads/" ) ||
802+ !is_null_oid (& head_oid ))
803+ return error (_ ("could not resolve HEAD commit" ));
804+ head_tree_oid = the_hash_algo -> empty_tree ;
805+ } else {
806+ head_commit = lookup_commit (r , & head_oid );
792807
793- if (!resolve_ref_unsafe ("HEAD" , RESOLVE_REF_READING , & head_oid , NULL ))
794- return error (_ ("could not resolve HEAD commit" ));
795-
796- head_commit = lookup_commit (r , & head_oid );
808+ /*
809+ * If head_commit is NULL, check_commit, called from
810+ * lookup_commit, would have indicated that head_commit is not
811+ * a commit object already. repo_parse_commit() will return failure
812+ * without further complaints in such a case. Otherwise, if
813+ * the commit is invalid, repo_parse_commit() will complain. So
814+ * there is nothing for us to say here. Just return failure.
815+ */
816+ if (repo_parse_commit (r , head_commit ))
817+ return -1 ;
797818
798- /*
799- * If head_commit is NULL, check_commit, called from
800- * lookup_commit, would have indicated that head_commit is not
801- * a commit object already. repo_parse_commit() will return failure
802- * without further complaints in such a case. Otherwise, if
803- * the commit is invalid, repo_parse_commit() will complain. So
804- * there is nothing for us to say here. Just return failure.
805- */
806- if (repo_parse_commit (r , head_commit ))
807- return -1 ;
819+ head_tree_oid = get_commit_tree_oid (head_commit );
820+ }
808821
809822 if (!(cache_tree_oid = get_cache_tree_oid (istate )))
810823 return -1 ;
811824
812- return oideq (cache_tree_oid , get_commit_tree_oid ( head_commit ) );
825+ return oideq (cache_tree_oid , head_tree_oid );
813826}
814827
815828static int write_author_script (const char * message )
@@ -1736,34 +1749,25 @@ static int allow_empty(struct repository *r,
17361749 int index_unchanged , originally_empty ;
17371750
17381751 /*
1739- * Four cases:
1740- *
1741- * (1) we do not allow empty at all and error out.
1752+ * For a commit that is initially empty, allow_empty determines if it
1753+ * should be kept or not
17421754 *
1743- * (2) we allow ones that were initially empty, and
1744- * just drop the ones that become empty
1745- *
1746- * (3) we allow ones that were initially empty, but
1747- * halt for the ones that become empty;
1748- *
1749- * (4) we allow both.
1755+ * For a commit that becomes empty, keep_redundant_commits and
1756+ * drop_redundant_commits determine whether the commit should be kept or
1757+ * dropped. If neither is specified, halt.
17501758 */
1751- if (!opts -> allow_empty )
1752- return 0 ; /* let "git commit" barf as necessary */
1753-
17541759 index_unchanged = is_index_unchanged (r );
17551760 if (index_unchanged < 0 )
17561761 return index_unchanged ;
17571762 if (!index_unchanged )
17581763 return 0 ; /* we do not have to say --allow-empty */
17591764
1760- if (opts -> keep_redundant_commits )
1761- return 1 ;
1762-
17631765 originally_empty = is_original_commit_empty (commit );
17641766 if (originally_empty < 0 )
17651767 return originally_empty ;
17661768 if (originally_empty )
1769+ return opts -> allow_empty ;
1770+ else if (opts -> keep_redundant_commits )
17671771 return 1 ;
17681772 else if (opts -> drop_redundant_commits )
17691773 return 2 ;
@@ -2943,6 +2947,9 @@ static int populate_opts_cb(const char *key, const char *value,
29432947 else if (!strcmp (key , "options.allow-empty-message" ))
29442948 opts -> allow_empty_message =
29452949 git_config_bool_or_int (key , value , ctx -> kvi , & error_flag );
2950+ else if (!strcmp (key , "options.drop-redundant-commits" ))
2951+ opts -> drop_redundant_commits =
2952+ git_config_bool_or_int (key , value , ctx -> kvi , & error_flag );
29462953 else if (!strcmp (key , "options.keep-redundant-commits" ))
29472954 opts -> keep_redundant_commits =
29482955 git_config_bool_or_int (key , value , ctx -> kvi , & error_flag );
@@ -3487,6 +3494,9 @@ static int save_opts(struct replay_opts *opts)
34873494 if (opts -> allow_empty_message )
34883495 res |= git_config_set_in_file_gently (opts_file ,
34893496 "options.allow-empty-message" , "true" );
3497+ if (opts -> drop_redundant_commits )
3498+ res |= git_config_set_in_file_gently (opts_file ,
3499+ "options.drop-redundant-commits" , "true" );
34903500 if (opts -> keep_redundant_commits )
34913501 res |= git_config_set_in_file_gently (opts_file ,
34923502 "options.keep-redundant-commits" , "true" );
0 commit comments