@@ -98,7 +98,7 @@ pub fn clone(repo_path: &Path) -> Result<(), git2::Error> {
9898 Ok ( ( ) )
9999}
100100
101- pub fn fetch ( repo : & Path ) -> Result < ( ) , git2:: Error > {
101+ pub fn pull ( repo : & Path ) -> Result < ( ) , git2:: Error > {
102102 let repo = Repository :: open ( repo) ?;
103103 let remote = "origin" ;
104104
@@ -188,5 +188,50 @@ pub fn fetch(repo: &Path) -> Result<(), git2::Error> {
188188 // needed objects are available locally.
189189 remote. update_tips ( None , true , AutotagOption :: Unspecified , None ) ?;
190190
191+ let fetch_head = repo. find_reference ( "FETCH_HEAD" ) ?;
192+ let fetch_commit = repo. reference_to_annotated_commit ( & fetch_head) ?;
193+
194+ let refname = "refs/heads/master" ;
195+ match repo. find_reference ( refname) {
196+ Ok ( mut r) => {
197+ fast_forward ( & repo, & mut r, & fetch_commit) ?;
198+ }
199+ Err ( _) => {
200+ // The branch doesn't exist so just set the reference to the
201+ // commit directly. Usually this is because you are pulling
202+ // into an empty repository.
203+ repo. reference (
204+ refname,
205+ fetch_commit. id ( ) ,
206+ true ,
207+ & format ! ( "Setting master to {}" , fetch_commit. id( ) ) ,
208+ ) ?;
209+ repo. set_head ( refname) ?;
210+ repo. checkout_head ( Some (
211+ git2:: build:: CheckoutBuilder :: default ( )
212+ . allow_conflicts ( true )
213+ . conflict_style_merge ( true )
214+ . force ( ) ,
215+ ) ) ?;
216+ }
217+ }
218+
219+ Ok ( ( ) )
220+ }
221+
222+ fn fast_forward (
223+ repo : & Repository ,
224+ lb : & mut git2:: Reference ,
225+ rc : & git2:: AnnotatedCommit ,
226+ ) -> Result < ( ) , git2:: Error > {
227+ let name = match lb. name ( ) {
228+ Some ( s) => s. to_string ( ) ,
229+ None => String :: from_utf8_lossy ( lb. name_bytes ( ) ) . to_string ( ) ,
230+ } ;
231+ let msg = format ! ( "Fast-Forward: Setting {} to id: {}" , name, rc. id( ) ) ;
232+ println ! ( "{}" , msg) ;
233+ lb. set_target ( rc. id ( ) , & msg) ?;
234+ repo. set_head ( & name) ?;
235+ repo. checkout_head ( None ) ?;
191236 Ok ( ( ) )
192237}
0 commit comments