2 * Copyright (C) 2005 Junio C Hamano
14 static int use_link
= 0;
15 static int use_symlink
= 0;
16 static int use_filecopy
= 1;
17 static int verbose
= 0;
21 static void say(const char *fmt
, const char *hex
) {
23 fprintf(stderr
, fmt
, hex
);
26 int fetch(unsigned char *sha1
)
28 static int object_name_start
= -1;
29 static char filename
[PATH_MAX
];
30 char *hex
= sha1_to_hex(sha1
);
31 const char *dest_filename
= sha1_file_name(sha1
);
33 if (object_name_start
< 0) {
34 strcpy(filename
, path
); /* e.g. git.git */
35 strcat(filename
, "/objects/");
36 object_name_start
= strlen(filename
);
38 filename
[object_name_start
+0] = hex
[0];
39 filename
[object_name_start
+1] = hex
[1];
40 filename
[object_name_start
+2] = '/';
41 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
43 if (!link(filename
, dest_filename
)) {
44 say("link %s\n", hex
);
47 /* If we got ENOENT there is no point continuing. */
48 if (errno
== ENOENT
) {
49 fprintf(stderr
, "does not exist %s\n", filename
);
53 if (use_symlink
&& !symlink(filename
, dest_filename
)) {
54 say("symlink %s\n", hex
);
61 ifd
= open(filename
, O_RDONLY
);
62 if (ifd
< 0 || fstat(ifd
, &st
) < 0) {
64 fprintf(stderr
, "cannot open %s\n", filename
);
67 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
69 if (-1 == (int)(long)map
) {
70 fprintf(stderr
, "cannot mmap %s\n", filename
);
73 ofd
= open(dest_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
74 status
= ((ofd
< 0) ||
75 (write(ofd
, map
, st
.st_size
) != st
.st_size
));
76 munmap(map
, st
.st_size
);
79 fprintf(stderr
, "cannot write %s (%ld bytes)\n",
80 dest_filename
, st
.st_size
);
82 say("copy %s\n", hex
);
85 fprintf(stderr
, "failed to copy %s with given copy methods.\n", hex
);
89 static const char *local_pull_usage
=
90 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
93 * By default we only use file copy.
94 * If -l is specified, a hard link is attempted.
95 * If -s is specified, then a symlink is attempted.
96 * If -n is _not_ specified, then a regular file-to-file copy is done.
98 int main(int argc
, char **argv
)
103 while (arg
< argc
&& argv
[arg
][0] == '-') {
104 if (argv
[arg
][1] == 't')
106 else if (argv
[arg
][1] == 'c')
108 else if (argv
[arg
][1] == 'a') {
113 else if (argv
[arg
][1] == 'l')
115 else if (argv
[arg
][1] == 's')
117 else if (argv
[arg
][1] == 'n')
119 else if (argv
[arg
][1] == 'v')
122 usage(local_pull_usage
);
126 usage(local_pull_usage
);
127 commit_id
= argv
[arg
];
128 path
= argv
[arg
+ 1];