Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 4 | #include "cache.h" |
| 5 | #include "commit.h" |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 6 | #include "pull.h" |
| 7 | |
| 8 | static int use_link = 0; |
| 9 | static int use_symlink = 0; |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 10 | static int use_filecopy = 1; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 11 | |
| 12 | static char *path; |
| 13 | |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 14 | int fetch(unsigned char *sha1) |
| 15 | { |
| 16 | static int object_name_start = -1; |
| 17 | static char filename[PATH_MAX]; |
| 18 | char *hex = sha1_to_hex(sha1); |
| 19 | const char *dest_filename = sha1_file_name(sha1); |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 20 | |
| 21 | if (object_name_start < 0) { |
| 22 | strcpy(filename, path); /* e.g. git.git */ |
| 23 | strcat(filename, "/objects/"); |
| 24 | object_name_start = strlen(filename); |
| 25 | } |
| 26 | filename[object_name_start+0] = hex[0]; |
| 27 | filename[object_name_start+1] = hex[1]; |
| 28 | filename[object_name_start+2] = '/'; |
| 29 | strcpy(filename + object_name_start + 3, hex + 2); |
Junio C Hamano | fd0ffd3 | 2005-05-04 01:28:45 -0700 | [diff] [blame] | 30 | if (use_link) { |
| 31 | if (!link(filename, dest_filename)) { |
Junio C Hamano | e78d977 | 2005-05-06 01:37:21 -0700 | [diff] [blame] | 32 | pull_say("link %s\n", hex); |
Junio C Hamano | fd0ffd3 | 2005-05-04 01:28:45 -0700 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | /* If we got ENOENT there is no point continuing. */ |
| 36 | if (errno == ENOENT) { |
| 37 | fprintf(stderr, "does not exist %s\n", filename); |
| 38 | return -1; |
| 39 | } |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 40 | } |
| 41 | if (use_symlink && !symlink(filename, dest_filename)) { |
Junio C Hamano | e78d977 | 2005-05-06 01:37:21 -0700 | [diff] [blame] | 42 | pull_say("symlink %s\n", hex); |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | } |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 45 | if (use_filecopy) { |
| 46 | int ifd, ofd, status; |
| 47 | struct stat st; |
| 48 | void *map; |
| 49 | ifd = open(filename, O_RDONLY); |
| 50 | if (ifd < 0 || fstat(ifd, &st) < 0) { |
| 51 | close(ifd); |
Junio C Hamano | fd0ffd3 | 2005-05-04 01:28:45 -0700 | [diff] [blame] | 52 | fprintf(stderr, "cannot open %s\n", filename); |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0); |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 56 | close(ifd); |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 57 | if (-1 == (int)(long)map) { |
Junio C Hamano | fd0ffd3 | 2005-05-04 01:28:45 -0700 | [diff] [blame] | 58 | fprintf(stderr, "cannot mmap %s\n", filename); |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | ofd = open(dest_filename, O_WRONLY | O_CREAT | O_EXCL, 0666); |
| 62 | status = ((ofd < 0) || |
| 63 | (write(ofd, map, st.st_size) != st.st_size)); |
| 64 | munmap(map, st.st_size); |
| 65 | close(ofd); |
| 66 | if (status) |
Junio C Hamano | b4635be | 2005-05-09 23:48:21 -0700 | [diff] [blame] | 67 | fprintf(stderr, "cannot write %s\n", dest_filename); |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 68 | else |
Junio C Hamano | e78d977 | 2005-05-06 01:37:21 -0700 | [diff] [blame] | 69 | pull_say("copy %s\n", hex); |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 70 | return status; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 71 | } |
Junio C Hamano | fd0ffd3 | 2005-05-04 01:28:45 -0700 | [diff] [blame] | 72 | fprintf(stderr, "failed to copy %s with given copy methods.\n", hex); |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 73 | return -1; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 76 | int fetch_ref(char *ref, unsigned char *sha1) |
| 77 | { |
| 78 | return -1; |
| 79 | } |
| 80 | |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 81 | static const char *local_pull_usage = |
Junio C Hamano | a48e1d6 | 2005-06-04 23:11:38 -0700 | [diff] [blame] | 82 | "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] [-d] [--recover] commit-id path"; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 83 | |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 84 | /* |
| 85 | * By default we only use file copy. |
| 86 | * If -l is specified, a hard link is attempted. |
| 87 | * If -s is specified, then a symlink is attempted. |
| 88 | * If -n is _not_ specified, then a regular file-to-file copy is done. |
| 89 | */ |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 90 | int main(int argc, char **argv) |
| 91 | { |
| 92 | char *commit_id; |
| 93 | int arg = 1; |
| 94 | |
| 95 | while (arg < argc && argv[arg][0] == '-') { |
| 96 | if (argv[arg][1] == 't') |
| 97 | get_tree = 1; |
| 98 | else if (argv[arg][1] == 'c') |
| 99 | get_history = 1; |
Junio C Hamano | 4a62b61 | 2005-06-02 15:19:00 -0700 | [diff] [blame] | 100 | else if (argv[arg][1] == 'd') |
| 101 | get_delta = 0; |
Junio C Hamano | a48e1d6 | 2005-06-04 23:11:38 -0700 | [diff] [blame] | 102 | else if (!strcmp(argv[arg], "--recover")) |
| 103 | get_delta = 2; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 104 | else if (argv[arg][1] == 'a') { |
| 105 | get_all = 1; |
| 106 | get_tree = 1; |
| 107 | get_history = 1; |
| 108 | } |
| 109 | else if (argv[arg][1] == 'l') |
| 110 | use_link = 1; |
| 111 | else if (argv[arg][1] == 's') |
| 112 | use_symlink = 1; |
Junio C Hamano | b02a26c | 2005-05-01 21:10:04 -0700 | [diff] [blame] | 113 | else if (argv[arg][1] == 'n') |
| 114 | use_filecopy = 0; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 115 | else if (argv[arg][1] == 'v') |
Junio C Hamano | e78d977 | 2005-05-06 01:37:21 -0700 | [diff] [blame] | 116 | get_verbosely = 1; |
Junio C Hamano | dfcb405 | 2005-05-01 21:09:28 -0700 | [diff] [blame] | 117 | else |
| 118 | usage(local_pull_usage); |
| 119 | arg++; |
| 120 | } |
| 121 | if (argc < arg + 2) |
| 122 | usage(local_pull_usage); |
| 123 | commit_id = argv[arg]; |
| 124 | path = argv[arg + 1]; |
| 125 | |
| 126 | if (pull(commit_id)) |
| 127 | return 1; |
| 128 | |
| 129 | return 0; |
| 130 | } |