Improve sha1 object file writing.
[git/git.git] / local-pull.c
CommitLineData
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
14static int use_link = 0;
15static int use_symlink = 0;
b02a26cb 16static int use_filecopy = 1;
dfcb4057
JH
17static int verbose = 0;
18
19static char *path;
20
21static void say(const char *fmt, const char *hex) {
22 if (verbose)
23 fprintf(stderr, fmt, hex);
24}
25
26int 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);
42 if (use_link && !link(filename, dest_filename)) {
43 say("Hardlinked %s.\n", hex);
44 return 0;
45 }
46 if (use_symlink && !symlink(filename, dest_filename)) {
47 say("Symlinked %s.\n", hex);
48 return 0;
49 }
b02a26cb
JH
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);
dfcb4057 61 close(ifd);
b02a26cb
JH
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;
dfcb4057 77 }
b02a26cb
JH
78 fprintf(stderr, "No copy method was provided to copy %s.\n", hex);
79 return -1;
dfcb4057
JH
80}
81
82static const char *local_pull_usage =
b02a26cb 83"git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
dfcb4057 84
b02a26cb
JH
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 */
dfcb4057
JH
91int main(int argc, char **argv)
92{
93 char *commit_id;
94 int arg = 1;
95
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;
105 }
106 else if (argv[arg][1] == 'l')
107 use_link = 1;
108 else if (argv[arg][1] == 's')
109 use_symlink = 1;
b02a26cb
JH
110 else if (argv[arg][1] == 'n')
111 use_filecopy = 0;
dfcb4057
JH
112 else if (argv[arg][1] == 'v')
113 verbose = 1;
114 else
115 usage(local_pull_usage);
116 arg++;
117 }
118 if (argc < arg + 2)
119 usage(local_pull_usage);
120 commit_id = argv[arg];
121 path = argv[arg + 1];
122
123 if (pull(commit_id))
124 return 1;
125
126 return 0;
127}