]>
Commit | Line | Data |
---|---|---|
b02a26cb JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
dfcb4057 JH |
4 | #include <fcntl.h> |
5 | #include <unistd.h> | |
6 | #include <string.h> | |
7 | #include <stdlib.h> | |
8 | #include "cache.h" | |
9 | #include "commit.h" | |
10 | #include <errno.h> | |
11 | #include <stdio.h> | |
12 | #include "pull.h" | |
13 | ||
14 | static int use_link = 0; | |
15 | static int use_symlink = 0; | |
b02a26cb | 16 | static int use_filecopy = 1; |
dfcb4057 JH |
17 | static int verbose = 0; |
18 | ||
19 | static char *path; | |
20 | ||
21 | static void say(const char *fmt, const char *hex) { | |
22 | if (verbose) | |
23 | fprintf(stderr, fmt, hex); | |
24 | } | |
25 | ||
26 | int fetch(unsigned char *sha1) | |
27 | { | |
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); | |
dfcb4057 JH |
32 | |
33 | if (object_name_start < 0) { | |
34 | strcpy(filename, path); /* e.g. git.git */ | |
35 | strcat(filename, "/objects/"); | |
36 | object_name_start = strlen(filename); | |
37 | } | |
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); | |
fd0ffd3a JH |
42 | if (use_link) { |
43 | if (!link(filename, dest_filename)) { | |
44 | say("link %s\n", hex); | |
45 | return 0; | |
46 | } | |
47 | /* If we got ENOENT there is no point continuing. */ | |
48 | if (errno == ENOENT) { | |
49 | fprintf(stderr, "does not exist %s\n", filename); | |
50 | return -1; | |
51 | } | |
dfcb4057 JH |
52 | } |
53 | if (use_symlink && !symlink(filename, dest_filename)) { | |
fd0ffd3a | 54 | say("symlink %s\n", hex); |
dfcb4057 JH |
55 | return 0; |
56 | } | |
b02a26cb JH |
57 | if (use_filecopy) { |
58 | int ifd, ofd, status; | |
59 | struct stat st; | |
60 | void *map; | |
61 | ifd = open(filename, O_RDONLY); | |
62 | if (ifd < 0 || fstat(ifd, &st) < 0) { | |
63 | close(ifd); | |
fd0ffd3a | 64 | fprintf(stderr, "cannot open %s\n", filename); |
b02a26cb JH |
65 | return -1; |
66 | } | |
67 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0); | |
dfcb4057 | 68 | close(ifd); |
b02a26cb | 69 | if (-1 == (int)(long)map) { |
fd0ffd3a | 70 | fprintf(stderr, "cannot mmap %s\n", filename); |
b02a26cb JH |
71 | return -1; |
72 | } | |
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); | |
77 | close(ofd); | |
78 | if (status) | |
fd0ffd3a | 79 | fprintf(stderr, "cannot write %s (%ld bytes)\n", |
b02a26cb JH |
80 | dest_filename, st.st_size); |
81 | else | |
fd0ffd3a | 82 | say("copy %s\n", hex); |
b02a26cb | 83 | return status; |
dfcb4057 | 84 | } |
fd0ffd3a | 85 | fprintf(stderr, "failed to copy %s with given copy methods.\n", hex); |
b02a26cb | 86 | return -1; |
dfcb4057 JH |
87 | } |
88 | ||
89 | static const char *local_pull_usage = | |
b02a26cb | 90 | "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path"; |
dfcb4057 | 91 | |
b02a26cb JH |
92 | /* |
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. | |
97 | */ | |
dfcb4057 JH |
98 | int main(int argc, char **argv) |
99 | { | |
100 | char *commit_id; | |
101 | int arg = 1; | |
102 | ||
103 | while (arg < argc && argv[arg][0] == '-') { | |
104 | if (argv[arg][1] == 't') | |
105 | get_tree = 1; | |
106 | else if (argv[arg][1] == 'c') | |
107 | get_history = 1; | |
108 | else if (argv[arg][1] == 'a') { | |
109 | get_all = 1; | |
110 | get_tree = 1; | |
111 | get_history = 1; | |
112 | } | |
113 | else if (argv[arg][1] == 'l') | |
114 | use_link = 1; | |
115 | else if (argv[arg][1] == 's') | |
116 | use_symlink = 1; | |
b02a26cb JH |
117 | else if (argv[arg][1] == 'n') |
118 | use_filecopy = 0; | |
dfcb4057 JH |
119 | else if (argv[arg][1] == 'v') |
120 | verbose = 1; | |
121 | else | |
122 | usage(local_pull_usage); | |
123 | arg++; | |
124 | } | |
125 | if (argc < arg + 2) | |
126 | usage(local_pull_usage); | |
127 | commit_id = argv[arg]; | |
128 | path = argv[arg + 1]; | |
129 | ||
130 | if (pull(commit_id)) | |
131 | return 1; | |
132 | ||
133 | return 0; | |
134 | } |