1+ /*
2+ * Copyright (C) 2005 Junio C Hamano
3+ */
14#include <fcntl.h>
25#include <unistd.h>
36#include <string.h>
1013
1114static int use_link = 0 ;
1215static int use_symlink = 0 ;
16+ static int use_filecopy = 1 ;
1317static int verbose = 0 ;
1418
1519static char * path ;
@@ -25,9 +29,6 @@ int fetch(unsigned char *sha1)
2529 static char filename [PATH_MAX ];
2630 char * hex = sha1_to_hex (sha1 );
2731 const char * dest_filename = sha1_file_name (sha1 );
28- int ifd , ofd , status ;
29- struct stat st ;
30- void * map ;
3132
3233 if (object_name_start < 0 ) {
3334 strcpy (filename , path ); /* e.g. git.git */
@@ -46,33 +47,47 @@ int fetch(unsigned char *sha1)
4647 say ("Symlinked %s.\n" , hex );
4748 return 0 ;
4849 }
49- ifd = open (filename , O_RDONLY );
50- if (ifd < 0 || fstat (ifd , & st ) < 0 ) {
50+ if (use_filecopy ) {
51+ int ifd , ofd , status ;
52+ struct stat st ;
53+ void * map ;
54+ ifd = open (filename , O_RDONLY );
55+ if (ifd < 0 || fstat (ifd , & st ) < 0 ) {
56+ close (ifd );
57+ fprintf (stderr , "Cannot open %s\n" , filename );
58+ return -1 ;
59+ }
60+ map = mmap (NULL , st .st_size , PROT_READ , MAP_PRIVATE , ifd , 0 );
5161 close (ifd );
52- fprintf (stderr , "Cannot open %s\n" , filename );
53- return -1 ;
54- }
55- map = mmap (NULL , st .st_size , PROT_READ , MAP_PRIVATE , ifd , 0 );
56- close (ifd );
57- if (-1 == (int )(long )map ) {
58- fprintf (stderr , "Cannot mmap %s\n" , filename );
59- return -1 ;
62+ if (-1 == (int )(long )map ) {
63+ fprintf (stderr , "Cannot mmap %s\n" , filename );
64+ return -1 ;
65+ }
66+ ofd = open (dest_filename , O_WRONLY | O_CREAT | O_EXCL , 0666 );
67+ status = ((ofd < 0 ) ||
68+ (write (ofd , map , st .st_size ) != st .st_size ));
69+ munmap (map , st .st_size );
70+ close (ofd );
71+ if (status )
72+ fprintf (stderr , "Cannot write %s (%ld bytes)\n" ,
73+ dest_filename , st .st_size );
74+ else
75+ say ("Copied %s.\n" , hex );
76+ return status ;
6077 }
61- ofd = open (dest_filename , O_WRONLY | O_CREAT | O_EXCL , 0666 );
62- status = ((ofd < 0 ) || (write (ofd , map , st .st_size ) != st .st_size ));
63- munmap (map , st .st_size );
64- close (ofd );
65- if (status )
66- fprintf (stderr , "Cannot write %s (%ld bytes)\n" ,
67- dest_filename , st .st_size );
68- else
69- say ("Copied %s.\n" , hex );
70- return status ;
78+ fprintf (stderr , "No copy method was provided to copy %s.\n" , hex );
79+ return -1 ;
7180}
7281
7382static const char * local_pull_usage =
74- "git-local-pull [-c] [-t] [-a] [-l] [-s] [-v] commit-id path" ;
83+ "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [- v] commit-id path" ;
7584
85+ /*
86+ * By default we only use file copy.
87+ * If -l is specified, a hard link is attempted.
88+ * If -s is specified, then a symlink is attempted.
89+ * If -n is _not_ specified, then a regular file-to-file copy is done.
90+ */
7691int main (int argc , char * * argv )
7792{
7893 char * commit_id ;
@@ -92,6 +107,8 @@ int main(int argc, char **argv)
92107 use_link = 1 ;
93108 else if (argv [arg ][1 ] == 's' )
94109 use_symlink = 1 ;
110+ else if (argv [arg ][1 ] == 'n' )
111+ use_filecopy = 0 ;
95112 else if (argv [arg ][1 ] == 'v' )
96113 verbose = 1 ;
97114 else
0 commit comments