[PATCH] git-local-pull updates
[git/gitweb.git] / local-pull.c
blobea38d87dfd4de9b2952ce0465a0374f4681be474
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
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"
14 static int use_link = 0;
15 static int use_symlink = 0;
16 static int use_filecopy = 1;
17 static int verbose = 0;
19 static char *path;
21 static void say(const char *fmt, const char *hex) {
22 if (verbose)
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);
42 if (use_link && !link(filename, dest_filename)) {
43 say("Hardlinked %s.\n", hex);
44 return 0;
46 if (use_symlink && !symlink(filename, dest_filename)) {
47 say("Symlinked %s.\n", hex);
48 return 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;
60 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
61 close(ifd);
62 if (-1 == (int)(long)map) {
63 fprintf(stderr, "Cannot mmap %s\n", filename);
64 return -1;
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;
78 fprintf(stderr, "No copy method was provided to copy %s.\n", hex);
79 return -1;
82 static const char *local_pull_usage =
83 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
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.
91 int main(int argc, char **argv)
93 char *commit_id;
94 int arg = 1;
96 while (arg < argc && argv[arg][0] == '-') {
97 if (argv[arg][1] == 't')
98 get_tree = 1;
99 else if (argv[arg][1] == 'c')
100 get_history = 1;
101 else if (argv[arg][1] == 'a') {
102 get_all = 1;
103 get_tree = 1;
104 get_history = 1;
106 else if (argv[arg][1] == 'l')
107 use_link = 1;
108 else if (argv[arg][1] == 's')
109 use_symlink = 1;
110 else if (argv[arg][1] == 'n')
111 use_filecopy = 0;
112 else if (argv[arg][1] == 'v')
113 verbose = 1;
114 else
115 usage(local_pull_usage);
116 arg++;
118 if (argc < arg + 2)
119 usage(local_pull_usage);
120 commit_id = argv[arg];
121 path = argv[arg + 1];
123 if (pull(commit_id))
124 return 1;
126 return 0;