@@ -1962,16 +1962,49 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1962
1962
return 0 ;
1963
1963
}
1964
1964
1965
+ /**
1966
+ * Merges a tree into the index. The index's stat info will take precedence
1967
+ * over the merged tree's. Returns 0 on success, -1 on failure.
1968
+ */
1969
+ static int merge_tree (struct tree * tree )
1970
+ {
1971
+ struct lock_file * lock_file ;
1972
+ struct unpack_trees_options opts ;
1973
+ struct tree_desc t [1 ];
1974
+
1975
+ if (parse_tree (tree ))
1976
+ return -1 ;
1977
+
1978
+ lock_file = xcalloc (1 , sizeof (struct lock_file ));
1979
+ hold_locked_index (lock_file , 1 );
1980
+
1981
+ memset (& opts , 0 , sizeof (opts ));
1982
+ opts .head_idx = 1 ;
1983
+ opts .src_index = & the_index ;
1984
+ opts .dst_index = & the_index ;
1985
+ opts .merge = 1 ;
1986
+ opts .fn = oneway_merge ;
1987
+ init_tree_desc (& t [0 ], tree -> buffer , tree -> size );
1988
+
1989
+ if (unpack_trees (1 , t , & opts )) {
1990
+ rollback_lock_file (lock_file );
1991
+ return -1 ;
1992
+ }
1993
+
1994
+ if (write_locked_index (& the_index , lock_file , COMMIT_LOCK ))
1995
+ die (_ ("unable to write new index file" ));
1996
+
1997
+ return 0 ;
1998
+ }
1999
+
1965
2000
/**
1966
2001
* Clean the index without touching entries that are not modified between
1967
2002
* `head` and `remote`.
1968
2003
*/
1969
2004
static int clean_index (const unsigned char * head , const unsigned char * remote )
1970
2005
{
1971
- struct lock_file * lock_file ;
1972
2006
struct tree * head_tree , * remote_tree , * index_tree ;
1973
2007
unsigned char index [GIT_SHA1_RAWSZ ];
1974
- struct pathspec pathspec ;
1975
2008
1976
2009
head_tree = parse_tree_indirect (head );
1977
2010
if (!head_tree )
@@ -1996,18 +2029,8 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
1996
2029
if (fast_forward_to (index_tree , remote_tree , 0 ))
1997
2030
return -1 ;
1998
2031
1999
- memset (& pathspec , 0 , sizeof (pathspec ));
2000
-
2001
- lock_file = xcalloc (1 , sizeof (struct lock_file ));
2002
- hold_locked_index (lock_file , 1 );
2003
-
2004
- if (read_tree (remote_tree , 0 , & pathspec )) {
2005
- rollback_lock_file (lock_file );
2032
+ if (merge_tree (remote_tree ))
2006
2033
return -1 ;
2007
- }
2008
-
2009
- if (write_locked_index (& the_index , lock_file , COMMIT_LOCK ))
2010
- die (_ ("unable to write new index file" ));
2011
2034
2012
2035
remove_branch_state ();
2013
2036
0 commit comments