]> git.ipfire.org Git - thirdparty/git.git/blame - local-pull.c
[PATCH] Add git-local-pull.
[thirdparty/git.git] / local-pull.c
CommitLineData
dfcb4057
JH
1#include <fcntl.h>
2#include <unistd.h>
3#include <string.h>
4#include <stdlib.h>
5#include "cache.h"
6#include "commit.h"
7#include <errno.h>
8#include <stdio.h>
9#include "pull.h"
10
11static int use_link = 0;
12static int use_symlink = 0;
13static int verbose = 0;
14
15static char *path;
16
17static void say(const char *fmt, const char *hex) {
18 if (verbose)
19 fprintf(stderr, fmt, hex);
20}
21
22int fetch(unsigned char *sha1)
23{
24 static int object_name_start = -1;
25 static char filename[PATH_MAX];
26 char *hex = sha1_to_hex(sha1);
27 const char *dest_filename = sha1_file_name(sha1);
28 int ifd, ofd, status;
29 struct stat st;
30 void *map;
31
32 if (object_name_start < 0) {
33 strcpy(filename, path); /* e.g. git.git */
34 strcat(filename, "/objects/");
35 object_name_start = strlen(filename);
36 }
37 filename[object_name_start+0] = hex[0];
38 filename[object_name_start+1] = hex[1];
39 filename[object_name_start+2] = '/';
40 strcpy(filename + object_name_start + 3, hex + 2);
41 if (use_link && !link(filename, dest_filename)) {
42 say("Hardlinked %s.\n", hex);
43 return 0;
44 }
45 if (use_symlink && !symlink(filename, dest_filename)) {
46 say("Symlinked %s.\n", hex);
47 return 0;
48 }
49 ifd = open(filename, O_RDONLY);
50 if (ifd < 0 || fstat(ifd, &st) < 0) {
51 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;
60 }
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;
71}
72
73static const char *local_pull_usage =
74"git-local-pull [-c] [-t] [-a] [-l] [-s] [-v] commit-id path";
75
76int main(int argc, char **argv)
77{
78 char *commit_id;
79 int arg = 1;
80
81 while (arg < argc && argv[arg][0] == '-') {
82 if (argv[arg][1] == 't')
83 get_tree = 1;
84 else if (argv[arg][1] == 'c')
85 get_history = 1;
86 else if (argv[arg][1] == 'a') {
87 get_all = 1;
88 get_tree = 1;
89 get_history = 1;
90 }
91 else if (argv[arg][1] == 'l')
92 use_link = 1;
93 else if (argv[arg][1] == 's')
94 use_symlink = 1;
95 else if (argv[arg][1] == 'v')
96 verbose = 1;
97 else
98 usage(local_pull_usage);
99 arg++;
100 }
101 if (argc < arg + 2)
102 usage(local_pull_usage);
103 commit_id = argv[arg];
104 path = argv[arg + 1];
105
106 if (pull(commit_id))
107 return 1;
108
109 return 0;
110}